Lesson deck

Transformers, large language models, sampling and fine-tuning flashcards

Reviewing needs JavaScript. You can read every card in the list below.

Loading your cards...

Back to the lesson14 cards in this deck

Read all 14 cards as a list
  1. Front
    What does attention compute, and what are the shapes?
    Back
    softmax(Q K^T / sqrt(d_k)) V. Q is n_q x d_k, K is n_k x d_k, V is n_k x d_v; scores n_q x n_k, output n_q x d_v. Self-attention takes Q, K, V from the same X. With n = 10, d_model = 256, h = 4 each head has 10 x 64 queries and a 10 x 10 score matrix.
  2. Front
    Why divide by sqrt(d_k)?
    Back
    With independent components of mean 0 and variance 1, q.k has variance d_k. Dividing by sqrt(d_k) restores variance 1 so softmax is not saturated. In the script, unscaled scores at d_k = 256 gave a largest weight of 0.9981 and a Jacobian norm of 0.00366; scaled gave 0.2970 and 0.37223 (one draw).
  3. Front
    How is the causal mask applied, and what goes wrong if it is applied late?
    Back
    Set scores for keys later than the query to minus infinity before the softmax, so those weights are exactly 0 and rows still sum to 1. Zeroing weights after the softmax leaves the future scores in the denominator (information leaks) and rows no longer sum to 1.
  4. Front
    What is the cost of attention in sequence length?
    Back
    The score matrix is n x n: O(n^2 d) time and O(n^2) memory if stored. Going from 1,000 to 2,000 tokens takes the entries per head from 1,000,000 to 4,000,000, four times as many. Recurrent layers need O(n) sequential steps; attention needs O(1).
  5. Front
    Why does attention need positions, and how are they added?
    Back
    Unmasked attention treats its input as a set: shuffling the rows shuffles the outputs the same way (a causal mask leaks some position implicitly, per Haviv et al.). Add a position encoding to the embeddings: sinusoidal PE(p,2i) = sin(p / 10000^(2i/d)), PE(p,2i+1) = cos(same angle), a learned table (nearly identical results in the original paper), or rotary embeddings that rotate queries and keys by position.
  6. Front
    What is byte pair encoding and what is an embedding lookup?
    Back
    BPE starts from characters and repeatedly merges the most frequent adjacent pair into a new token, so rare words become pieces. An embedding lookup selects a row of a V x d_model matrix, equal to a one-hot vector times the matrix. Vocabulary 50,000 and width 768 give 38,400,000 parameters (illustrative).
  7. Front
    Describe the pre-norm block and why it is preferred.
    Back
    h = x + Attention(Norm(x)), then out = h + MLP(Norm(h)). The residual path is never normalised, and Xiong et al. report well-behaved gradients at initialisation and training without warm-up with comparable results; post-norm, LayerNorm(x + Sublayer(x)), has large gradients near the output at the start.
  8. Front
    Estimate the parameters of a transformer stack.
    Back
    Per layer 4d^2 (attention) + 2 d d_ff (MLP) = 12d^2 when d_ff = 4d; about 12 L d^2 for L layers, ignoring biases, norms and embeddings. d = 1024, L = 24: 301,989,888. d = 4096, L = 32: 6,442,450,944. Two thirds of a layer is the MLP.
  9. Front
    GELU versus SiLU?
    Back
    GELU = x Phi(x) with the normal CDF; SiLU = x sigmoid(x). Both are smooth, slightly negative for negative inputs and close to x for large positive ones. At x = -1: GELU -0.1587, SiLU -0.2689; at x = 3: 2.9960 and 2.8577. Which is better is empirical.
  10. Front
    How is a decoder-only model trained, and how does it generate?
    Back
    Teacher forcing: feed the true sequence once, the causal mask hides the future, position t predicts token t+1, so n tokens give n-1 targets; loss is mean cross-entropy (a uniform guess over 50 tokens: ln 50 = 3.9120). Generation is autoregressive: sample a token, append, repeat.
  11. Front
    What does the KV cache save and cost?
    Back
    Earlier keys and values do not change, so store them instead of recomputing: 5,050 projections for 100 tokens without a cache against 100 with. Memory: 2 x L x H_kv x d_head x T x bytes per sequence; 32 layers, 32 heads, width 128, 4,096 tokens, 2 bytes: 2.0 GiB; with 8 KV heads: 0.5 GiB.
  12. Front
    Temperature, top-k and top-p?
    Back
    Temperature divides logits: lower is sharper. Logits [2,1,0.5,0,-1] at T=1 give top probability 0.5630; at T=0.5, 0.8292. Top-k keeps k tokens (k=2: 0.7311, 0.2689). Top-p keeps the smallest set reaching p, so it shrinks when the model is confident. Both renormalise.
  13. Front
    What does LoRA train, and what does merging do?
    Back
    W' = W + (alpha/r) B A with A r x d_in and B d_out x r, W frozen, B starting at 0. Trained weights r(d_in + d_out): a 4096 x 4096 matrix has 16,777,216 weights, rank 8 trains 65,536 (0.3906%). Merging gives one matrix again, so no extra inference cost.
  14. Front
    How do SFT, RLHF and DPO differ, and when use RAG or fine-tuning?
    Back
    SFT trains on example answers; RLHF uses ranked answers to train a reward model, then optimises the model against it; DPO uses a classification-style loss on preference pairs without a separate reward model or RL loop. Missing or changing facts and citations: retrieval. Behaviour and style: fine-tuning. Hallucination follows from a likelihood objective with no truth signal.