- It removes all recurrent connections. During training, all positions of a sequence are then processed in parallel, and at inference the model reads the whole prompt in one parallel pass. Generating text is still sequential, because each new token depends on the tokens before it. Since the processing no longer follows the order of the tokens, the order has to be encoded explicitly, through positional information, either in the input embeddings or inside attention. The cross-attention and position encoding section covers the methods.
- It uses attention to adjust context-free embeddings, such as the word2vec embeddings, based on the context of the input sequence. Attention can sit in the encoder, in the decoder, or in both. The name comes from what the mechanism does: each token attends to the other tokens of the input sequence, and the attention weights decide how much each token contributes to the update.
- GPT-1, Figure 1 (left): a 12-layer decoder-only transformer with masked self-attention, trained to predict the next token (Radford et al., n.d.).
- Llama 3, Figure 1: a dense decoder-only transformer, close to the original design. Its attention uses grouped-query attention, where several query heads share one key-value head (Grattafiori et al., 2024).
- Mixtral, Figure 1: the MLP of each layer becomes a mixture of experts. A router sends each token to 2 of 8 MLPs and adds their weighted outputs (Jiang et al., 2024).
- DeepSeek-V3, Figure 2: a mixture of experts in place of the MLP, and multi-head latent attention, which compresses the keys and values so that less memory is needed during generation (DeepSeek-AI et al., 2024).
- I love bears
- She bears the pain
- Bears won the game
Simple self-attention mechanism
The simplest way to let the other tokens affect a context-free representation is a dot product between the input token and every token of the context. This is called simple self-attention. The word “self” means that each token attends to the tokens of its own sequence. The simple version uses the input vectors directly, with no learned transformation. The next figure shows the input context matrix with dimensions , where is the length of the input context and is the embedding dimension. In this example each token occupies a row with features with , and there are tokens. Real models are much larger: Qwen3.8-27B uses and a context of up to tokens.Code
Code
Code
Code
The output as a move inside the convex hull
The weights of each row are non-negative and sum to one, so . Subtracting this from the output gives You can read the update as a move. The token starts at and is displaced by , the weighted average of the vectors that point from to each token of the context. The move is bounded. A weighted average with non-negative weights that sum to one always lies in the convex hull of the tokens, the smallest convex region that contains all of them. The move can bring the token closer to some tokens and farther from others, but it cannot carry the token outside that region. The next figure shows the move of in a context of four tokens with .PyTorch reference
Key references: (Bahdanau et al., 2014; Luong et al., 2015; Vaswani et al., 2017)
References
- Bahdanau, D., Cho, K., Bengio, Y. (2014). Neural Machine Translation by Jointly Learning to Align and Translate. arXiv [cs.CL]. Link
- Grattafiori, A., Dubey, A., Jauhri, A., Pandey, A., Kadian, A., et al. (2024). The Llama 3 herd of models. arXiv [cs.AI]. Link
- Jiang, A., Sablayrolles, A., Roux, A., Mensch, A., Savary, B., et al. (2024). Mixtral of Experts. arXiv [cs.LG]. Link
- Luong, M., Pham, H., Manning, C. (2015). Effective Approaches to Attention-based Neural Machine Translation. arXiv [cs.CL]. Link
- Radford, A., Narasimhan, K., Salimans, T., Sutskever, I. (n.d.). Improving Language Understanding by Generative Pre-Training. Link
- Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., et al. (2017). Attention Is All You Need. arXiv [cs.CL]. Link
- DeepSeek-AI, Liu, A., Feng, B., Xue, B., Wang, B., et al. (2024). DeepSeek-V3 Technical Report. arXiv [cs.CL]. DOI
Found an error on this page? Report an issue.

