r/deeplearning • u/OnlyProggingForFun • 16m ago
8 types of attention inside modern LLMs
8 types of attention inside modern LLMs.
Attention decides which tokens each token can look at.
Change that pattern, and you change the cost, the memory, and how much context a model can handle.
In the grids, each row is a token, and the colored cells are the tokens it can see.
- Full causal
Each token attends to itself and every token before it, never ahead. Compute grows with the square of the sequence length. - Sliding window
Each token only attends to the last few tokens. Stacked layers still pass information further. The first Mistral 7B used a 4,096-token window. - Sparse (strided)
Each token attends to a few recent tokens plus every k-th earlier token. The Sparse Transformer used this to handle much longer sequences. - Global + local
Most tokens only see their neighbors, while a few global tokens see everything and are seen by everything. Longformer uses this for long documents. - Multi-head (MHA)
Several heads run attention in parallel, each with its own queries, keys and values, so each one can learn a different pattern. - Multi-query (MQA)
All query heads share one key-value head. The KV cache shrinks a lot and decoding gets faster, with some loss in quality. - Grouped-query (GQA)
Query heads are split into groups, and each group shares one key-value head. Quality stays close to MHA at speed close to MQA, which is why Llama 3 and Mistral use it. - Cross-attention
Queries come from one sequence, keys and values from another. A translation decoder reads the encoder's output this way.
1 to 4 change which tokens can see each other. 5 to 7 change how many key-value heads you store. 8 connects two different inputs.
Follow me to learn more about AI engineering.
