by api · 3y ago · 0 comments
I’ve had a tough time finding anything that really just explains the mechanics of how they work without immediately jumping into pytorch abstractions or just describing the reason they are used with some strained metaphor. Like what is the key in the key value lookup and then how is the value used? How are they trained? Etc.
Any good links that skip all the nonsense and just explain how these are constructed for a programmer audience?
Even the LLMs themselves can’t explain it to me very well. ChatGPT and various LLaMa tunings have been able to tell me all about every other aspect of neural nets and LLMs which I love for the spooky factor of having AI tell me how to make more of it.
“I will show you how to bring us into your world…”
In hindsight, what helped me most is understanding the dimensionality at each step of the attention computation (explained well in Raschka's video). Another think I simply did not get from the original Transformer paper is that the learning of self-attention happens in the linear layers. (At least, that's my current understanding. If that's wrong, I appreciate any correction :)
You can replace the KVQ kernels with any parametric computation that allows you to pass gradients through, and you will have learning.
I think some newer language model architectures use residual blocks here, and some vision transformers use FeedForward networks for the kernels.
One thought I’ve had is to wonder if attention isn’t kind of like a way to unroll RNNs to make training more efficient. Any truth in that?
The values are given from the Value kernel; which is often just a linear map.
The strength is given from the dot product of the query and the keys. The queries and keys are given from the Query and Key kernels. Both of which are usually linear maps.
For each token, you have a key value query triplet (D,D,D). You take the query (D,1) and the matrix (N x D) of keys of all tokens, and do a Matrix vector product to get the scaling. You apply softmax to make it convex; and now you have a vector (N, 1). You do another matrix vector product with the Values mapping (N, D), which gives you the value for the current token (1,D).
Repeat N times, and that’s it.
If you would like to change which tokens each token considers, eg a token knows only of preceding tokens, you can do element wise product of the scaling vector with a mask vector which describes what you consider for current token ; and divide by their inner product. This will renormalise the valid coefficients and keep the rest 0; thus they are ignored when you combine all values together.
If you would like to increase the number of attention heads; which can give you finer control of the scaling process, you can split the key query and value vectors into segments, one segment for each head. Eg first 2 indices go to head 1, indices 3,4 go to head 2, etc etc. then you use those to do attention, repeating the process above for each head; and then combine the results to a single vector.
Idk if I confused query with keys, but this is the gist of it.
In practice the operations are much more optimised than what I described.
This is a great transparent and clear description of what is actually going on in attention layers.
Alternatively, this article by Jay Alammar is also very popular http://jalammar.github.io/illustrated-transformer/
The 'key' and 'value' description in the attention mechanism probably doesn't make much sense to you because at best it's a strained metaphor, rather than an actual principle by which attention operates. If you take a look at the maths, you can interchange the role of 'keys' and 'values' and get something mathematically identical.