News for hackers. Stuff that matters.
Re:Deep Learning in JavaScript by modeless on Thursday March 28, 2024 @09:12PM (#39859491)
Someone needs to do a TypeScript compiler plugin to add multidimensional array slicing and operator overloading to the language, so these libraries can actually work the way PyTorch does.

I know operator overloading is controversial, but the way it allows automatic differentiation to work transparently through regular arithmetic expressions is very helpful. Without it these libraries will never feel like PyTorch.

JavaScript is so much faster than Python, and the package management experience is so much better, that I really think a JS library would have a chance to take market share from PyTorch in the long term if it had those features.

Re:Deep Learning in JavaScript by sroussey on Thursday March 28, 2024 @09:38PM (#39859637)
Agreed. My suggestion is for Bun to do that since they are already transforming typescript to JS.

But you can do it —- there a babel plugin in the TC39 proposal:

https://github.com/tc39/proposal-operator-overloading

Re:Deep Learning in JavaScript by csjh on Friday March 29, 2024 @01:15AM (#39860950)
I'm glad that proposal was withdrawn; it would've been by far, in my opinion, the worst implementation of operator overloading, in any (mainstream) language
Re:Deep Learning in JavaScript by sroussey on Friday March 29, 2024 @04:07PM (#39868447)
I had glanced through it a long time ago. Maybe it’s time for someone to create a new (and better!) proposal.
Re:Deep Learning in JavaScript by eduardoleao052 on Thursday March 28, 2024 @09:26PM (#39859570)
Could not agree more hahaha! I tried to work around it building methods like “torch.add(a, b)” for operator overloading and “torch.at(index)” for slicing. But it’s definitely not as seamless as these features you proposed.
Re:Deep Learning in JavaScript by noduerme on Thursday March 28, 2024 @11:06PM (#39860222)
Curious, why do you need to construct these as class instances, like operation = new Exp() ? Seems like a lot of extra overhead constructing those objects. Why not just have Exp contain static methods for forwards and backwards?

[edit] nevermind, I missed the cache step. Still not sure it wouldn't be more performant to centralize caches as plain objects somewhere rather than to call new() on every op...?

Re:Deep Learning in JavaScript by eduardoleao052 on Thursday March 28, 2024 @11:51PM (#39860513)
I centralized the entire backpropagation around the Operation objects. They store the data about the forward prop in the cache, and serve as the connections in the graphs between tensors. Each tensor has a “parent”, “child” and “operation”. These store who generated the tensor, what tensors it generated, and how it was generated (what operation). I could store the backward function inside of each tensor instead of an Operation object, but I chose the slightly more verbose option because I think it is a little more interpretable and simpler to add new operations.
Re:Deep Learning in JavaScript by modeless on Thursday March 28, 2024 @09:29PM (#39859582)
You should do it! If you actually had a solution for operator overloading you'd really stand out from the other various JS deep learning libraries. Save me from pip and conda please :)
Re:Deep Learning in JavaScript by eduardoleao052 on Thursday March 28, 2024 @11:48PM (#39860493)
I can try implementing it in the future lol It would surely be a quality of life improvement. But with the current tools, I tried my best to make the syntax as similar as possible to PyTorch’s!
Re:Deep Learning in JavaScript by throwuwu on Saturday March 30, 2024 @10:52AM (#39875435)
If you do want to add evaluation of mathematical expressions you should check out Math.js since they provide a parser among other utilities. Please make it optional though, it would be a nightmare to debug if everything was written in strings.

https://mathjs.org/docs/expressions/parsing.html

Re:Deep Learning in JavaScript by eduardoleao052 on Saturday March 30, 2024 @08:30PM (#39880131)
Thanks for the tip, will look into it! Yes, I think it would always be better to leave a "vanilla" option available.
Re:Deep Learning in JavaScript by throwaway4aday on Friday March 29, 2024 @06:34AM (#39862473)
What's wrong with creating a function that does those things? It would be less surprising to people new to the library, would be self-documenting by having a name and an easily inspected declaration with named arguments, and it would be idiomatic JS. You could also have variants that are purely functional and return a new value or ones that mutate in place that you could use depending on your needs.
Re:Deep Learning in JavaScript by QuadmasterXLII on Friday March 29, 2024 @07:03AM (#39862619)
In python when I try a new gpu accelerated array library, to write norm_arr = arr / lib.sqrt( lib.sum( arr*2, axis=-1, keepdims=True) , I have to read documentation for sum to see whther they use axis or dim in sum.

In javascript, to write the same thing I need to read documentation for a sum function, a broadcasted division function, a multiply function. I can probably assume that the sqrt function behaves and is named as I would expect

Re:Deep Learning in JavaScript by 4hg4ufxhy on Friday March 29, 2024 @09:26AM (#39863843)
Why can you make assumptions about operation overloads but not functions?
Re:Deep Learning in JavaScript by foolswisdom on Friday March 29, 2024 @10:03AM (#39864237)
Because there is nothing to make assumptions about. In the example code, both multiplication and division have a scalar on one side, there's no possible ambiguity of behavior. But there is the eternal question of terminology: do you specify dimensions by "axis" or "dim" and does your API actually use both terms in different places?

(that's what I think the GP meant, anyway).

Re:Deep Learning in JavaScript by throwaway4aday on Friday March 29, 2024 @10:30AM (#39864537)
If each of those operators were implemented as functions then you'd have different names for different implementations in order to avoid confusion over what type of division or multiplication they were performing. It's more verbose but that's a good thing since it prevents you from making incorrect assumptions about what's going to happen when you do a * b.
Re:Deep Learning in JavaScript by modeless on Friday March 29, 2024 @11:52AM (#39865536)
What's wrong is it obscures simple math expressions behind tons of dots and parentheses. The thing is that the core of deep learning algorithms is usually very simple math. It's useful to be able to translate that math directly from research papers into straightforward expressions that mirror the structure in the paper like a = b / c + d * e rather than something less similar like a = b.divide(c).add(d.multiply(e)).
Re:Deep Learning in JavaScript by throwaway4aday on Friday March 29, 2024 @09:02PM (#39870818)
Depending on what you learned first, dots and parentheses are a lot simpler to understand than math expressions.
Re:Deep Learning in JavaScript by DanielHB on Saturday March 30, 2024 @03:58AM (#39872797)
You could probably build tagged template literal like:

a = e`${b} / ${c}`

Not ideal, but much better and without magic pre processors

Re:Deep Learning in JavaScript by goatlover on Friday March 29, 2024 @09:42AM (#39864022)
Would JS be faster than Python when it comes to Pytorch? For example, I seriously doubt that would be the case for Numpy, since it's a wrapper for C code, with the ability to use Fortran libraries for optimization.
Re:Deep Learning in JavaScript by dagw on Friday March 29, 2024 @11:54AM (#39865552)
There is often a lot of work that has to be done before and after PyTorch gets involved. For example the code I'm working on right now involves reading and parsing a bunch of files, filtering and extracting a bunch of data based on various criteria, formatting that data, passing it to a PyTorch model and then taking the results from PyTorch, validating it, reformatting it and then writing it to disk. The PyTorch part is probably as fast as it can get, but most of the overall runtime is spent doing all that other stuff and if you can speed that up then that is a clear win in many cases.
Re:Deep Learning in JavaScript by modeless on Friday March 29, 2024 @11:41AM (#39865408)
C running on the CPU isn't fast enough for ML. You need to run on GPUs or TPUs if you're serious.

Yes, most of the tensor operations in PyTorch do their math in native code. However, Python still does orchestration and other tasks like data loading and because it is so slow it still ends up causing a ton of overhead in many cases despite offloading most of the work. It's very common for the GPU to sit idle between kernels while Python spins. So JavaScript being faster could still be a big advantage.

Re:Deep Learning in JavaScript by throwaway4aday on Friday March 29, 2024 @10:39AM (#39864625)
The benefit of having it in JS is not speed but portability and access to the JS ecosystem of tools. Having the code run in the browser without needing a complex setup is a huge benefit for sharing demos. Node.js provides a way to use native code as well and it's quite commonly used https://github.com/nodejs/node-gyp so there's no reason you couldn't use those same or similar libraries in a JS implementation.
Re:Deep Learning in JavaScript by fulafel on Friday March 29, 2024 @10:54AM (#39864801)
Very much the opposite, since this is pure JS. PyTorch uses tuned native-code and GPU components for the heavy lifting and can in some cases compile Python code using PyTorch JIT / torch.compiler / torch.fx.
Re:Deep Learning in JavaScript by jsight on Friday March 29, 2024 @10:59AM (#39864860)
It'd be shocking if it was. PyTorch isn't particularly slow.
Re:Deep Learning in JavaScript by mejutoco on Saturday March 30, 2024 @07:18AM (#39873781)
You could use eval to expand whatever syntax you like into function calls. It could be done once at the start of the program and act like a js preprocessor.

I know eval is not kosher but this problem is also not real, so why not.

Re:Deep Learning in JavaScript by eduardoleao052 on Saturday March 30, 2024 @08:53AM (#39874387)
That's an interesting idea as well, could definitely see that working in some use cases.