Reading — step 1 of 5
Read
~1 min readFull Transformer
Transformer Block
One transformer block = attention + FFN + residuals + norms.
python
Residual connections:
- x = x + sublayer(x).
- Crucial: gradient flows directly through.
- Without: deep networks fail to train.
Layer norm position:
- Pre-norm: norm before sublayer (modern, more stable).
- Post-norm: norm after residual (original).
Stacking blocks:
python
Output shape: (T, vocab_size). Each position predicts next token's distribution.
Training: shift labels by 1 (predict token i+1 from positions 0..i).
- Inputs: tokens[:-1].
- Labels: tokens[1:].
- Cross-entropy loss between predicted distribution and true next token.
Inference (text generation):
- Encode prompt → tokens.
- Forward → next-token logits.
- Sample (greedy / top-k / nucleus / temperature).
- Append to context.
- Repeat.
Key sizes (per layer of Llama 70B):
- d_model = 8192.
- n_heads = 64.
- FFN hidden = 28672.
- Per layer: ~860M params (the SwiGLU FFN alone is 3 × 8192 × 28672 ≈ 700M).
- 80 layers → ~68B params — nearly all of the 70B lives in the transformer blocks.
The architecture is simple. Scaling it to billions of params + trillions of tokens is the engineering feat.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…