News for hackers. Stuff that matters.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @12:56PM (#35978097)
The Yannic kilcher review is quite good.

https://youtu.be/iDulhoQ2pro

I can't ELI5 but I can ELI-junior-dev. Tl;dw:

Transformers work by basically being a differentiable lookup/hash table. First your input is tokenized and (N) tokens (this constitutes the attention frame) are encoded both based on token identity and position in the attention frame.

Then there is an NxN matrix that is applied to your attention frame "performing the lookup query" over all other tokens in the attention frame, so every token gets a "contextual semantic understanding" that takes in both all the other stuff in the attention frame and it's relative position.

Gpt is impressive because the N is really huge and it has many layers. A big N means you can potentially access information farther away. Each layer gives more opportunities to summarize and integrate long range information in a fractal process.

Two key takeaways:

- differentiable hash tables

- encoding relative position using periodic functions

NB: the attention frame tokens are actually K-vectors (so the frame is a KxN matrix) and the query matrix is an NxNxK tensor IIRC but it's easier to describe it this way

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by jimkleiber on Wednesday May 17, 2023 @01:39PM (#35978714)
I had to look up "tl;dw" and realized it meant "too long; didn't watch" and not my first AI-laden instinct of "too long, didn't write" :-D
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by rekttrader on Wednesday May 17, 2023 @01:13PM (#35978332)
I came here to post this video. It’s a great primer on the topic and it gives you ideas to prompt gpt and have it output more.

It’s how I got an understanding of beam search, a technique employed in some of the response building.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by sva_ on Wednesday May 17, 2023 @02:06PM (#35979043)
One thing that might be worth pointing out is that the transformer architecture owes a great deal of its success to the fact that it can be implemented in a way that it can be massively parallelized in a very efficient manner.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @02:11PM (#35979120)
Compared to rnns... maybe? The big nxn is really a killer.

I don't know how to judge parallelizability of different DNN models, you're comparing apples to oranges

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by Salgat on Wednesday May 17, 2023 @02:27PM (#35979322)
When you train a transformer, you're training what the next expected token is. You can train all positions of the sequence each in parallel rather than having to sequentially build up the memory state as you generate the sequence with an LSTM. Mind you the inference portion of a transformer is still sequentially bottlenecked since you don't know what the output sequence is supposed to be.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by saynay on Wednesday May 17, 2023 @02:41PM (#35979472)
I believe the other big thing is it allowed you to parallelize the training as well, so you could split those colossal training sets across many machines in a way you could not do with LSTMs.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @02:50PM (#35979564)
I don't really believe this because in theory it's possible to split up training of arbitrary neural nets using decoupled neural interfaces (not that it's easy mind you) but at the point where you have openAI scale resources the bottleneck to achieving that is the difficulty of Python.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by golly_ned on Thursday May 18, 2023 @01:17AM (#35984357)
What’s a neural interface?
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Thursday May 18, 2023 @07:42AM (#35986605)
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by rocho on Thursday May 18, 2023 @03:47AM (#35985108)
If you train in parallel, how do you combine the weights generated from different trainings in parallel?
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Thursday May 18, 2023 @08:06AM (#35986778)
You take a huge hit.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by Salgat on Thursday May 18, 2023 @11:19AM (#35989214)
Believe it or not, it's a simple as averaging or adding the gradients of each training result before adding it to the model weights. The same thing happens when you train a model using batches of inputs.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Friday May 19, 2023 @09:17AM (#36001575)
It actually isn't. You have to have a synchronizer, batchsize one or else "strange things" can happen and you waste a lot of cycles. Alternatively you can do non-simple changes to your network structure to enable distributed training.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by Salgat on Friday May 19, 2023 @11:18AM (#36002984)
It really is that simple. Yes, there's many different approaches to this (which can become quite clever and complex, which is true of training in general), but it all really boils down to adding or averaging the gradients in most cases.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by zorr on Wednesday May 17, 2023 @02:31PM (#35979372)
How does N relate to the number of parameters that is frequently mentioned?
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @02:52PM (#35979595)
In my screed, N is the attention width. (How many token it looks at at a time) number of parameters is O(KxNxNxL) where k is the vector size of your tokens, and l is the # of layers. There are other parameters floating around, like in the encoder and decoder matrices, but the NXN matrix dominates.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by 8thcross on Wednesday May 17, 2023 @03:33PM (#35980107)
This is an awesome explanation. You guys are the real heroes
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by kenjackson on Wednesday May 17, 2023 @02:39PM (#35979453)
What does it mean for a lookup/hash table to be differentiable?
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by weinzierl on Wednesday May 17, 2023 @02:44PM (#35979505)
I wanted to ask the same and especially I've always been wondering: How is the meaning of aforementioned 'differentiable' related to the same term in math?
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by visarga on Wednesday May 17, 2023 @03:16PM (#35979900)
The neural net is just a math function, continuous even, fully differentiable in all input points. In order to "learn" anything we compute gradients towards the function parameters. They get "nudged" slightly towards a better response, and we do this billions of times. It's like carving a raw stone block into a complex scene. If you put your data into the system it flows towards the desired output because the right path has been engraved during training.

<rant> This explains a bit how neural nets work but from this to chatGPT is another whole leap. You'd have to assign some of the merits of the AI to the training data itself, it's not just the algorithm for learning, but what is being learned that matters. The neural net is the same, but using 1T tokens of text is making it smart. What's so magic about this data that it can turn a random init into a language programmable system? And the same language information make a baby into a modern human, instead of just another animal. </>

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by bjourne on Wednesday May 17, 2023 @03:46PM (#35980296)
No, it's the loss function we differentiate. The input to the loss function are the network weights. The input to the network are samples and those we do not differentiate.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by lhnz on Wednesday May 17, 2023 @06:22PM (#35981984)
While it's true that we don't differentiate the input samples, we do differentiate the loss function's output with respect to each of the network weight. We use the chain rule to calculate each of these "gradients" and that process is known as backpropagation.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by kccqzy on Wednesday May 17, 2023 @02:53PM (#35979600)
Same thing. You need everything to be differentiable in order to run gradient descent. To first approximation, training a neural network is just gradient descent.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by dcre on Wednesday May 17, 2023 @04:52PM (#35981141)
Not an expert (so this could well be slightly off), but here is a physics analogy:

You're rolling a ball down a plank of wood and you want it to roll off the plank and continue to a certain spot on the ground. You know that if the ball rolls past the target, you have to angle the ramp lower. If it fails to reach the target, you angle the ramp higher. If it goes past by a lot, you make a big change to the ramp angle. If it goes past by a little, you make a small change to the ramp angle. In this way your error tells you something about the adjustment you need to make to the system to produce the desired output.

Think of a function from the ramp angle (network weights) to how far the ball lands from the target (the error). The derivative of this function tells you what kind of change in angle results in what kind of change in the error, and you can use that to update the ramp until you hit the target. To say that the model is differentiable is to say that it's possible to infer from the error what kind of changes you need to make to the weights.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by meowkit on Wednesday May 17, 2023 @02:49PM (#35979557)
Going to go out on a limb and say they are probably referring to the gradient calculus required for updating the model.

https://en.wikipedia.org/wiki/Differentiable_programming

See automatic differentiation.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @02:58PM (#35979662)
Correct, but note that if you subject a standard hash table algo to AD it won't magically become a transformer. (Hashes in the "normal construction" are discrete functions and thus aren't really continuous or differentiable, neither are lookup tables)
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by taylorius on Wednesday May 17, 2023 @02:53PM (#35979610)
Differentiable in this context, means that it can be learned by gradient descent, which uses derivatives to adjust a neural network model's parameters to minimise some error measure. As for how that applies to a hash function, I think the lookup gives some sort of weighting for each possible output, for each input, with the largest weightings corresponding to a "match". But tbh I'm not certain on that last part...
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @03:01PM (#35979699)
> As for how that applies to a hash function, I think the lookup gives some sort of weighting for each possible output, for each input, with the largest weightings corresponding to a "match"

Note that in general one "matches" more than one thing in which case (IIRC) the result will be a weighted linear combination of the query results.

I do think that this collapses to a datastructure equivalent in function to the "normal hash table" (but not in performance, lol) in the degenerate case where every input yields strictly a single result with zero activation in all other results, so it's not invalid to call it a differentiable lookup table.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @02:55PM (#35979636)
You can take a (calculus) derivative of the mathematical construct that represents the lookup table. And yeah, it is very not obvious how a "lookup/hash table" could be differentiable, based on how it's implemented say, in a leetcode exercise. That's part of the genius of the transformer.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by contravariant on Wednesday May 17, 2023 @02:59PM (#35979668)
Well, a hash table is basically a function.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by srcreigh on Wednesday May 17, 2023 @03:46PM (#35980292)
Not a continuous function
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by theGnuMe on Wednesday May 17, 2023 @03:09PM (#35979810)
The lookup/hashtable can be viewed as a matrix which is accessed by multiplying it with a vector.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by visarga on Wednesday May 17, 2023 @03:12PM (#35979848)
It's not really doing hashing, which is random, but instead it makes meaningful connections, like connecting an adjective to its noun, or a pronoun to a previous reference of the name. That is why it is called "Attention is all you need" and not "Hashing is all you need". Attention includes some meaning, it is attentive to some aspect or another.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @04:41PM (#35981005)
Hashing does not have to be random, it just has to compress the domain. Modulo prime number is a perfectly valid hash for some use cases.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by tomp on Wednesday May 17, 2023 @04:10PM (#35980603)
I'm not a ML expert but I know a bit about math.

It's "differentiable" in the same way that e.g. the "jump function" (Heaviside step function) is differentiable (not as a function from real numbers to real numbers, but as a distribution). It's derivative is the "point impulse function" (Dirac delta function), which, again, is a distribution, not a real function.

Distributions are nicely defined in math, but can't really be operated with numerically (at least not in the same way as real/float functions), but you can approximate them using continuous functions. So instead of having a function jump from 0 to 1, you "spread" the jump and implement it as a continuous transition from e.g. `0-epsilon` to `0+epsilon` for some tiny epsilon. Then you can differentiate it as usual, even numerically.

Similarly, hash table lookup is a dis-continuous function - the result of `hash.get(lookup)` is just `value` (or `null`). To make it continuous, you "spread" the value, so that nearby keys (for some definition of "nearby") will return nearby values.

One way to do this, is to use the scalar product between `lookup` and all keys in the hashtable (normalized, the scalar product is close to 1 if the arguments are "nearby"), and use the result as weights to multiply with all values in the hashtable. That's what the transformer does.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by hoosieree on Wednesday May 17, 2023 @08:49PM (#35982996)
Thanks for this explanation. I couldn't wrap my mind around the "differentiable hash table" analogy, but "distribution of keys" -> "distribution of values" starts to click.

I'm not an ML expert either but I have taken graduate level courses and published papers with "machine learning" in the title, so I feel like I should be able to understand these things better. The field just moves so fast. It's a lot of work to keep up. Easy-to-digest explanations like this are underrated.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by HybridCurve on Thursday May 18, 2023 @05:05PM (#35994042)
>The field just moves so fast. It's a lot of work to keep up. Easy-to-digest explanations like this are underrated.

This is really the truth. I can't possibly understand how people in this field who are talented can still keep up. I have a binder full of seminal papers that I have to cull to make room for more recent and relevant research every few months. I feel there is a lot of potential in simplifying the details of the mechanisms that drive a lot of it, but nobody has time to stop, consolidate the information and publish it. And if they did, it would just be another outdated textbook in a few years.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by kylewatson on Thursday May 18, 2023 @12:52AM (#35984240)
thank you. This made it click.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by coppsilgold on Saturday May 20, 2023 @03:53AM (#36010151)
What actually happens is that each vector grabs a fraction of each other vector and adds it to itself (simplification omitting some transforms along the way). Equating this with the concept of a hash function does not make sense in my opinion but some authors do it anyway.

It's differentiable because how much of a fraction to grab is the result of a simple dot product followed by a softmax.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by dpcx on Wednesday May 17, 2023 @04:42PM (#35981017)
I appreciate the explanation, but I don't know what junior-dev would understand most of this. I may be just a web developer, but I couldn't understand most of this. I'd still have to read for 30m to grok it all.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @04:51PM (#35981133)
Yeah sorry, it still requires math and probably some exposure to ML basics.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by shadowgovt on Wednesday May 17, 2023 @05:04PM (#35981261)
I think one hole in the description for simplicity is that "differentiable" it's not an adjective that applies to hash tables.

Differentiable relative to what? What is (x) in the d(hashtable)/d(x) equation?

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by lhnz on Wednesday May 17, 2023 @05:33PM (#35981528)
I think it applies because lookups can be done by multiplying one-hot vectors (or masks) with matrices; this is roughly analogous to what happens when we multiply Q with K^T in a self-attention head.

Read this: https://e2eml.school/transformers.html#table_lookup

And then read: https://e2eml.school/transformers.html#attention

Also, multiplication is differentiable as you can calculate a partial derivative of this operation, and the ability to do that is what allows gradient descent optimization via the chain rule.

Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by throwawaymaths on Wednesday May 17, 2023 @05:42PM (#35981635)
That's exactly the point, though! It's surprising. A hashtable is a map from keys to values. Making it differentiable means that a small change in the key also makes a small change in the value!
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by moultano on Wednesday May 17, 2023 @09:41PM (#35983315)
differentiable relative to the model parameters. The attention mechanism does store(key(x), value(x)), followed by lookup(query(y)), where key(), value(), lookup(), query() are all designed to be differentiable.
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by ogoparootbbo2 on Wednesday May 17, 2023 @05:54PM (#35981750)
what is a differentiable hash table? I understand differentiation, I don't understand differential hash table ... does that mean for every tiny gradient of a key, a corresponding gradient in value can be expected?
Re:Ask HN: Can someone ELI5 Transformers and the “Attention ... by legalizemoney on Wednesday May 17, 2023 @06:21PM (#35981976)
Having read the paper myself, I'm impressed with the quality of your explanation. Well done!