← All mindmapsMAIN TOPIC · 08 / 14Transformers
Attention, blocks, architectures and generation
Explore full topic ↗ Readable revision notes (18 concepts)
The same content as the visual sheet, optimized for mobile and assistive technology.
01 · Input representation
- Token IDs → learned embeddings + position information
- Absolute positions, relative biases or RoPE
- Padding mask excludes padding; causal mask hides future
02 · Self-attention
- Q=XWQ; K=XWK; V=XWV
- Attention(Q,K,V)=softmax(QKᵀ/√dₖ)V
- Weights combine value vectors based on query-key scores
03 · Multi-head attention
- Parallel heads use different learned projections
- Concat heads → output projection
- Grouped-query attention shares K/V across query heads
04 · Transformer block
- Attention + residual; FFN + residual
- LayerNorm stabilizes activations; pre/post norm variants
- FFN applies token-wise nonlinear projection
05 · Architectures
- Encoder-only: bidirectional understanding (BERT-style)
- Decoder-only: causal next-token generation
- Encoder-decoder: cross-attention for conditional generation
06 · Training objectives
- MLM predicts masked tokens; CLM next token
- Teacher forcing feeds known previous tokens
- Cross-entropy over eligible target tokens
07 · Position & context
- RoPE rotates Q/K using position-dependent angles
- Dense attention score matrix scales O(n²) in length
- Context limits constrain input + generated output
08 · Inference
- Autoregressive decoding appends one token per step
- KV cache avoids recomputing previous keys/values
- Temperature, top-k/top-p alter sampling
09 · Efficiency & interview
- FlashAttention reduces memory I/O for exact attention
- Quantization saves memory with possible accuracy loss
- Compare latency, throughput and context budget
10 · Scaled dot-product attention
- Q and K compute compatibility QKᵀ/√dₖ; softmax across allowed keys.
- Multiply weights by V; scale avoids large logit variance.
- With causal mask, future logits set to −∞ before softmax.
11 · Multi-head vs MQA/GQA
- MHA independent Q/K/V heads; concatenate and project.
- MQA shares one K/V head; GQA shares per group; reduces KV cache size.
- d_head typically d_model/h; head count impacts compute and memory.
12 · Positional encodings
- Sinusoidal PE adds fixed vectors; learned absolute positions limited by training.
- RoPE rotates Q/K to encode relative position in dot products.
- ALiBi adds distance-related bias; extrapolation requires testing.
13 · Encoder, decoder & cross-attention
- Encoder self-attention bidirectional; causal decoder predicts next token.
- Cross-attention Q from decoder, K/V from encoder.
- Encoder-only for representation; decoder-only generation; seq2seq conditional tasks.
14 · Feed-forward & normalization
- FFN typically expands then projects token channels; gated variants like SwiGLU.
- Pre-norm applies norm before sublayer; residual adds input to output.
- LayerNorm subtracts mean; RMSNorm scales by root mean square.
15 · Training objectives & batching
- Causal LM predicts shifted next token; masked LM predicts selected masked tokens.
- Padding/ignored labels excluded from loss; packed sequences need correct boundaries.
- Teacher forcing vs autoregressive inference may create exposure mismatch.
16 · Decoding & KV cache
- Prefill processes prompt in parallel; decode repeatedly generates tokens.
- KV cache bytes ≈ 2 × layers × tokens × KV heads × head dimension × bytes per element, per sequence.
- Temperature and top-p/top-k affect sampling; beam search is task-dependent.
17 · Attention complexity & FlashAttention
- Dense attention has O(n²) pairwise scores; hidden dimension also affects cost.
- FlashAttention tiles computation to reduce memory traffic, exact attention.
- Sparse/sliding windows reduce attention connections; context tradeoffs.
18 · Parameter efficiency
- LoRA ΔW=BA with rank r; train adapters, freeze W.
- Quantization reduces weight bits; KV cache may remain higher precision.
- Benchmark context, throughput, memory and accuracy; distinguish training vs serving.
References & further study
Original schematics illustrate concepts, not measured data. Review these references for full derivations and implementation details.