Step inside the core mechanism of Large Language Models: Scaled Dot-Product Attention, Multi-Head Routing, and Residual Feed-Forward Blocks.
Click each building block to see exact tensor shapes, operations, and intermediate outputs.
Unlike recurrent networks (RNNs) that process words sequentially, Transformers process all words simultaneously in parallel. To retain the order of words, a positional vector (derived from sine and cosine functions of varying frequencies) is added to the word embedding vector.
The final output vector of the last token passes through a linear projection ($d_{model} \to |V|$) and Softmax to yield next-token probabilities.
Self-attention compares every token to every other token, requiring $O(N^2)$ memory and compute where $N$ is sequence length.
Static embeddings like Word2Vec give the word "bank" one vector. In a Transformer, self-attention lets "bank" attend to "river" or "deposit" to dynamically construct distinct meanings depending on context.
Multiple heads project queries and keys into different vector subspaces. Head 1 might track pronouns ("it" $\to$ "animal"), while Head 2 tracks syntax ("did" $\to$ "cross"), and Head 3 captures adjacent bigrams.
The residual shortcut $\text{LayerNorm}(x + \text{SubLayer}(x))$ allows information and error gradients to propagate through dozens or hundreds of stacked layers without vanishing or exploding.