Transformers Explained Visually
Posted by aray07 1 day ago
Comments
Comment by andblac 1 day ago
Comment by bonoboTP 23 hours ago
But yes, a Transformer block can be thought of as basically input-dependently deciding the weight vector of a dense layer. In classical MLP-like networks there was never any multiplication between input-dependent values (there are exceptions, like Squeeze-and-Excitation layers). Multiplication is always between parameters (that don't depend on the input) and activations that depend on the input. With Transformers attention provides a lot of multiplicative interactions between input dependent activations.
Also obligatory: Schmidhuber talked about this a long time ago.
Comment by logicchains 16 hours ago
And again recently: https://arxiv.org/abs/2102.11174
Comment by groby_b 6 hours ago
That is basically axiomatic ;) (Seriously, Schmidhuber's body of research is mind-blowing)
Comment by abirch 9 hours ago
Comment by encrux 1 day ago
Comment by bilsbie 1 day ago
Comment by andblac 1 day ago
[1] https://www.welchlabs.com/store/mladeepseek-attention-poster...
[2] https://www.youtube.com/watch?v=0VLAoVGf_74
[3] When multiplying A by V, we perform the same linear transform Av_i for each i-th column of V.
Comment by Betelbuddy 12 hours ago
Comment by octoberfranklin 15 hours ago
Comment by est 18 hours ago
Or is it?
Comment by DevelopingElk 17 hours ago
I do think if Transformers weren't invented you would still be able to train powerful language models, but they would take more ram and be slower so nobody does this.
Comment by ActorNightly 16 hours ago
most people in ML have no idea what transformers actually are.
Traditional networks, at every layer, used to be output = [weights matrix][input], where input is a vector, and weights matrix is the weights, where each row corresponds to the set of weights for each neuron.
Transformers upscale the dimension of the data. Instead of the above, transformers do [output] = [input][weights_matrix]. When you multiply an input by a matrix, you get an output matrix back. Thats all that happens. Nothing fancy. You have weights matricies for K/Q/V, which when post multiplied with the input, give you the KQV vectors, and then you just simply multiply them together and apply a scaling factor.
There is nothing magical about K/Q/V. There is nothing about any one doing any querying or any one representing some keys. The naming is just a carry over from how they that selection process is used in pre llm data science fields where you manually define the key and query matricies to define relationships between components.
The reason of why it works is because is an extension of something called kernel tricks from pre LLM machine learning days - you map a lower dimensional space to an extra dimension based on some equation, and it lets you apply some classifier on the combination of existing values and new value. Thats what transformers are doing - they are mapping the individual token to the dk x n_heads latent space, which allows for a higher dimensional representation of the data, capturing complex relationships.
You can do Transformers with 5 matricies instead of 3, you can do this with 4-dimentional tensors, and so on. The thing is, there really isn't any way to tell if any of that gives you more advantage - it certainly would give you more granularity, but as of right now, in terms of training to generate a specific token given previous ones before it, it seems that you don't need any more dimentions than dk x n_heads. Interestingly enough, you also can mathematically represent any such transformer including the starting one with a sequence of linear layers like in traditional networks, the only thing is that it becomes computationally inefficient due to having duplicates of data.
The reason why RNNs and others and others didn't work is because RNN training is effectively trying to linearly regress on chaotic effects - i.e what set of starting conditions would evolve with a given process into what you want. This is an NP hard problem, and you can't really do it linearly.
Transformer models on the other hand, use breadth instead of compute to capture interactions. In those learned weight matrices, you have a latent space of a bunch of "knowledge" compressed, and an algorithm to search on that "knowledge".
But, its very possible that an RNN can be smarter than a frontier model while being much smaller in size - in the same way that its very possible that you can have the right set of prompts for an existing local inference smaller model that can basically be very close to AGI in terms of being able to solve any problem across any domain. Right now, the space is about exploring those prompts, which is the frameworks and harnesses, to get to there, as well as making the compute portion more efficient so you can explore that space faster.
And the thing that comes after harnesses/efficiency in terms of progress should be obvious if you understand all of the above.
Comment by est 16 hours ago
> You can do this with 5 matricies instead of 3, you can do this with 4-dimentional tensors, and so on
As a outsider I have many dumb quesion like these. I am trying to understand transformers in a Occam's razor way. It's a complicated machinary after all.
Comment by alansaber 12 hours ago
Comment by ActorNightly 6 hours ago
In the contest of LLMs, you cant have these types of coded function. Your function has to be a mathematical equation that is smooth - i.e no discrete steps, no singularities. The reason for this is when any neural net is trained, you use backpropagation of the error to adjust weights, and how much you adjust them is directly proportional to the weights effect on the final output, and in order to compute this, you have to have smooth functions from start to finish.
So what you do instead is you add data to your 4 values, that capture different relationship between them. If your 4 values are x,y,k,and h, your first data point can be a1x + b1y + c1k + d1h. The second point can be a2x + b3y + c4k + d5h. And so on. You can have as many of those values as you want. And then you can add, combine, and scale those values in any way you chose.
This basically gives you a map of 4 values into a binary decision whether the ball will end up in a goal or not, after sufficient training. However, the total number of extra values that you chose has to be large enough to capture all possibilities - if you don't have enough, you will start to make mistakes for some initial conditions.
Comment by octoberfranklin 15 hours ago
This means that it can't capture unidirectional relationships, like "ball" is the object on which the verb "threw" acts in the sentence "I threw the ball". This relationship is true in only one direction; it isn't true to say "threw" is the object on which the verb "ball" acts.
I do think it would be fair to say that transformers generalize the kernel trick to noncommutative relations by applying a different projection function (W^Q and W^K) to the two tokens being considered. This makes the overall operation (project then dot product) a noncommutative operation.
And the thing that comes after harnesses/efficiency in terms of progress should be obvious if you understand all of the above.
Nirvana? Singularity? Paperclips? Vernor Vinge rising from the dead? I'm curious; please share!
Comment by ActorNightly 7 hours ago
>Nirvana? Singularity? Paperclips? Vernor Vinge rising from the dead? I'm curious; please share!
Simulated evolution. Thats how you "solve" highly nonlinear chaotic systems. And generally, if you think about it, you have to have some secondary system on top of the knowledge embedded in LLMs to drive them to select certain tokens, which then starts to eerily resemble what humans call emotions in themselves.
Comment by ActorNightly 6 hours ago
...You have weights matricies for K/Q/V, which when post multiplied with the input, give you the KQV ** matricies **...
Comment by imtringued 10 hours ago
Transformer training can be parallelized easily, making it possible to use brute force to train the neural network quickly (bitter lesson rewards compute friendly scalable architectures).
Transformers have perfect retrieval, they re-read the entire context window from scratch for every token.
Explanation over.
If you extrapolate this, then the logical conclusion is that the next model architecture would use even more brute force.
Right now transformers can only append a token at the end. This means they can read any input, but write only one specific output.
If you wanted to extend this, you would want to make the transformer read from any input and write to any output, i.e make it capable of updating the entire KV cache every iteration.
Comment by fithisux 11 hours ago
There is not much theoretical explanation yet. Or why others do not work.
Hand-waving mostly.
Comment by octoberfranklin 15 hours ago
Transformers are conventional feed-forward neural networks alternated with attention blocks. You can think of them as big huge "ordinary" neural networks augmented with this new kind of block.
Attention blocks are basically just a differentiable hashmap. Think of it like a scratchpad memory.
It turns out that a hashmap/scratchpad is pretty essential to being able to untangle language. I don't find this too hard to believe. Somewhere in there, you have to build the graph of which object is acting via which verb on which object.
What is surprising is that this is all it takes! These simple little hashmap/scratchpad units (and massive scale) are really the only thing you need to tack on to a feed-forward neural network to get essentially general intelligence. This is totally surprising to me.
Comment by robrenaud 1 day ago
> "Instead of picking the highest-probability token, we can use different selection strategies to balance safety and creativity in the generated text".
Safety is definitely the wrong word here.
Temperature 0 generated text actually has a weird "lack of surprise" character that makes it seem artificial. [1]
> "high-probability texts can be dull or repetitive. Humans use language as a means of communicating information, aiming to do so in a simultaneously efficient and error-minimizing manner; in fact, psycholinguistics research suggests humans choose each word in a string with this subconscious goal in mind."
I'd completely drop the dropout explanation. It's just not part of the modern recipe anymore, AFAICT.
As for the ambitious goal of explaining transformers with a single interactive visualization, I just have a hard time imagining a person is going to newly understand both word embeddings (word2vec blew my mind in 2014) and also gain an understanding of attention.
I am making my own visualizations for a presentation on "Full Bandwidth Transformers"[2] that I am giving tomorrow at the Deep Learning Study Group (SF) (on zoom for the non-locals)[3]. It's not meant to be stand alone/context free, but I'd love some feedback.
https://rrenaud.github.io/fullbandwidth_transformer_viz/
[1] https://arxiv.org/abs/2202.00666 [2] https://arxiv.org/abs/2608.08888 [3] https://www.meetup.com/deep-learning-sf/events/316601593/
Comment by jventura 10 hours ago
Is there any other mechanism replacing it? As far as I understood, while following the LLMs from Scratch book, the dropout is a mechanism to prevent overfitting, and it makes a lot of sense (basically cripple some neuron connections randomly during training such that their weights aren't updated).
Edit: well, I could have searched on the internet, but other people may stumble on your post and you seem quite knowledgeable of the inner workings of these things. :)
Comment by robrenaud 5 hours ago
Also see the argument around figure 3 here. https://arxiv.org/pdf/2503.02113
Comment by octoberfranklin 14 hours ago
Hey, take it easy on the guy. Words are just probabilities in some high-dimensional space; it's a probabalistic selection anyways; he probably just nudged the latent vector a little too hard. Spymarking and watermarking are for safety reasons, you know. There was zero loss of quality there.
Comment by echelon 2 hours ago
Comment by utopcell 1 day ago
Comment by dang 19 hours ago
LLM Visualization - https://news.ycombinator.com/item?id=45130260 - Sept 2025 (46 comments)
LLM Visualization - https://news.ycombinator.com/item?id=38505211 - Dec 2023 (131 comments)
Comment by gyanchawdhary 1 day ago
Comment by raluk 11 hours ago
Comment by mhl47 12 hours ago
Comment by laurentiurad 14 hours ago
Comment by maciejzj 16 hours ago
Comment by shinyoo 14 hours ago
Comment by jwpapi 1 day ago
Comment by shagie 1 day ago
"Try examples while GPT-2 model is being downloaded (600MB)"
That's a hefty chunk of download and likely compute too.
Comment by ianand 18 hours ago
Comment by jwpapi 1 day ago
Comment by stdatomic 9 hours ago
Comment by dgellow 8 hours ago
Comment by ShinyLeftPad 7 hours ago
Comment by jasonjmcghee 1 day ago
Comment by hangonhn 23 hours ago
Comment by neilv 22 hours ago
https://www.ebooks.com/en-us/book/211460386/hands-on-large-l...
Comment by misiti3780 22 hours ago
Comment by whattheheckheck 20 hours ago
Comment by misiti3780 10 hours ago
Comment by israrkhan 21 hours ago
Comment by vitaljudge 18 hours ago
Comment by E-Reverance 1 day ago
edit: I know that it mentions its not modern, but these kinds of details have major implications in terms of the representations a model can learn, which is in many ways the most important part!
Comment by ViscountPenguin 22 hours ago
Comment by ianand 18 hours ago
Comment by foobazgt 23 hours ago
Comment by ViscountPenguin 22 hours ago
Comment by E-Reverance 23 hours ago
Comment by bilsbie 1 day ago
Comment by kingstnap 1 day ago
Wq projects it to the space of queries. I.E What questions is this token asking?
Wk projects it to the space of keys. I.E What questions does this token answer.
Wk projects it to the space of values. I.E What are those answers?
Of course this explanation is prescribed onto the matrixes after the fact.
You can in fact do weird stuff like construct weights so attention calculates least squares, or sorts numbers, or other weird constructions like a transformer that calculates gradient descent steps. It seems to be very flexible in terms of what functions on data it can encode.
Comment by penguin_booze 15 hours ago
This sort of explanation, whilst being technically correct, is right up there with "A monad is a monoid in the category of endofunctors. What's your problem?".
Comment by kingstnap 2 hours ago
Even if you have less background the jargon doesn't unpack as insanely as you might imagine. It's not quite the monad is a monoid level stuff.
A projection is just the linear algebra word for a function that takes vectors and produces vectors. And function in this case is just like a code function.
After that you need to believe that vectors (an array of numbers) can encode meaning abstractly (this is what "latent space" means). I mean this is sort of a given since what is a picture other than a list of numbers and obviously pictures can have meaning.
Finally you arrive at the statement that Wq is a projection (a function) from a token to the questions such a token could ask.
For example a token of "dog" could ask "what is the dogs name" as a query vector. Abstractly this is what Wq might do.
Maybe an earlier token "bella" turns into "this is a name" as a key. And "the name is bella" as a value.
The key and query in this case might line up, i.e directionally point the same way.
The real leap of faith is recognizing that this sort of abstract model of language can exist and be represented as just a vector of numbers. Which admittedly is a bit bizzare.
Comment by octoberfranklin 15 hours ago
It's a "fuzzy" hashmap; insead of hashmap.get("ball")=="threw" it assigns a probability to every pair of words.
Each hashmap captures some kind of relationship between words.
For example, every LLM has lots of heads whose relationship measures "is token1 the noun on which the verb token2 is acting"? So "I threw the ball" would have a high probability for ("ball", "threw").
But most of the hashmaps don't capture such easy-to-explain relationships. Some of them do. The rest probably capture relationships that we haven't figured out yet. This is the truly mysterious stuff.
But it's just hashmaps. Hashmaps all the way down.
Comment by ftumminello 23 hours ago
Comment by jaggederest 23 hours ago
Comment by kenanfyi 16 hours ago
For high power stuff though, you need some equipment to do that since copper will hard to bend neatly.
Comment by CTDOCodebases 23 hours ago
I suspect it will be getting more popular as flyback transformers get harder and harder to find.
Comment by jaggederest 23 hours ago
Comment by Vaslo 23 hours ago
Comment by hollowturtle 11 hours ago
Comment by phoghed 11 hours ago
Mobile first, almost never in my experience. Mobile also, usually.
What gives you the impression an LLM made this? It doesn’t have any overt signs.
Side scrolling and letting me zoom is also an acceptable compromise for mobile if you’re not going to put the time in to craft the experience for it imo. And some things just won’t be great on mobile, like downloading a 600MB GPT-2 model.
Comment by hollowturtle 9 hours ago
It's lazy, it doesn't look good on desktop too. The navbar screams justice. If it hasn't been made by an LLM it would still be a lazy human
Comment by noncovalence 21 hours ago
The nefarious "2.2 GB RAM usage within 10 seconds" tab open in the background:
Comment by tanseydavid 1 day ago
Comment by trumbitta2 15 hours ago
Comment by trumbitta2 14 hours ago
Comment by unixhero 19 hours ago
Comment by dominotw 9 hours ago
Comment by dionian 22 hours ago
Comment by esseph 1 day ago
Comment by npodbielski 15 hours ago
That is good too though.
Comment by throw0101a 1 day ago
(Also "cryto" for cryptocurrency rather than cryptography.)
Comment by cobbzilla 22 hours ago
Comment by kQq9oHeAz6wLLS 1 day ago
Comment by BobbyTables2 19 hours ago
Comment by aashishverma112 11 hours ago