Guide

Self-attention is one sublayer. A transformer is the full stack: token and position embeddings, repeated blocks of attention and MLPs with residuals and norms, and an LM head that turns the final residual stream into vocabulary logits. Modern open models (Llama, Mistral, Qwen, and others) are decoder-only: causal attention, no encoder.

If self-attention is the verb, the architecture is the sentence. Practitioners who only memorize "stack of layers" struggle to estimate VRAM, read model cards, or spot when a paper quietly switched from multi-head attention to grouped-query attention.

Concepts in this section

Read them in order. Each one builds on the one before.

StepConceptWhat it answers
1Embeddings and rotary positionsHow does a token id become a vector with order?
2The transformer blockWhat is inside one layer?
3Parameter and FLOP countsHow big is the model, and how many FLOPs per token?
4Encoder vs decoder transformersBERT, T5 or GPT — which attention pattern?

The data path in one paragraph

Id t becomes row E[t]. RoPE rotates Q and K by position. Each layer does RMSNorm, attention, residual add, RMSNorm, MLP, residual add. A final norm and the LM head produce logits over the vocabulary. Training runs all positions in parallel with a causal mask. Decode runs one token at a time against a KV cache.

Non-embedding parameters are about 12 L d² for classic blocks. Forward is about 2N FLOPs per token. Training is about 6N. A 7B-class model with d=4096 and L=32 lands near 6.4B non-embedding parameters, plus about 0.5B for a 128k embedding table.

When a paper says "7B transformer," decode L, d, heads, vocab, tied embeddings, GQA and MoE before comparing to another 7B. Residual plus pre-norm is why deep stacks train. If you remove residuals, depth collapses. Width (d) buys a richer residual stream. Depth buys more serial computation inside one forward pass. Frontier recipes grow both together.

GPT-2-style blocks use LayerNorm, learned absolute positions and a GELU 4d MLP. Llama-style blocks swap in RMSNorm, RoPE, SwiGLU and often grouped-query attention. Same skeleton, different defaults. Prefer reading one reference implementation end to end over collecting blog diagrams.

Common mistakes

  • Comparing two 7B models by the badge. The layout (GQA, SwiGLU, vocab, tying) moves the real count.
  • Using 2N as the training cost. Training is about 6N per token.
  • Skipping a reference implementation. nanoGPT or Llama labeled against this map beats a pile of blog diagrams.

Key takeaways

  • Decoder-only LLMs: embed → stack of (RMSNorm→attn→residual→RMSNorm→MLP→residual) → LM head.
  • RoPE encodes relative position by rotating Q/K. It replaced absolute learned positions in most new models.
  • The residual stream is the information highway. Sublayers read and write additives.
  • Non-embedding params ≈ 12 L d² for classic blocks. Forward ≈ 2N FLOPs/token. Train ≈ 6N.
  • BERT versus T5 versus GPT differ mainly in attention visibility and whether there is an encoder.

Further learning

Karpathy builds a GPT end to end in code — watch after you can sketch the block on a whiteboard.

Let's build GPT: from scratch, in code, spelled out.Andrej Karpathy
The Annotated Transformer (Harvard NLP)The Annotated Transformernlp.seas.harvard.edu RoFormer: Enhanced Transformer with Rotary Position EmbeddingPosition encoding recently has shown effective in the transformer architecture. It enables valuable supervision for dependency modeling between elements at different positions of the sequence. In this paper, we first …arXiv.org