Pretraining & Fine-Tuning LLMs
Time limit: 90 minutes Total marks: 60 Instructions: Answer all three questions. Show all derivations. Coding answers may be written in PyTorch-style pseudocode but must be dimensionally correct.
Question 1 — LoRA / QLoRA: memory, math, and correctness (22 marks)
A dense linear layer of a frozen LLM has weight with , . You apply LoRA replacing the update with , where , , rank , scaling constant .
(a) Derive the forward pass and state the initialization of and that guarantees at step 0. Explain why this initialization matters for training stability. (4 marks)
(b) Compute the number of trainable parameters introduced by LoRA on this one layer, and express it as a percentage of the full-fine-tuning trainable count for the same layer. (4 marks)
(c) The model has layers, and LoRA is applied to the query and value projections only (2 matrices per layer, all ). Compute total trainable LoRA parameters. (3 marks)
(d) QLoRA stores in NF4 (4-bit) while keeping LoRA adapters in bf16 (16-bit). For the entire 7B-parameter frozen backbone, compute the memory (in GB, bytes) to store frozen weights in (i) fp16 and (ii) NF4, ignoring quantization constants. State the compression ratio. (4 marks)
(e) Prove that for any target update matrix , the LoRA parameterization can represent exactly if and only if . (4 marks)
(f) State one reason why QLoRA uses double quantization and what it quantizes. (3 marks)
Question 2 — Pretraining objectives & the text-to-text unification (20 marks)
(a) Write the training loss for (i) GPT-style causal language modeling and (ii) BERT-style masked language modeling, using explicit conditioning notation over a sequence . Clearly indicate which tokens contribute to the loss in each case. (6 marks)
(b) T5 uses a span-corruption objective. Given the input sentence "the cat sat on the mat", show a corrupted input/target pair using sentinel tokens <X>, <Y>, assuming the model corrupts the spans {"cat"} and {"on the"}. (4 marks)
(c) A BERT model uses 15% masking: of masked positions, 80% → [MASK], 10% → random token, 10% → unchanged. Explain mathematically/informally why the 10% "unchanged" and 10% "random" fractions exist — i.e., what train/inference mismatch they mitigate. (4 marks)
(d) Consider a corpus of tokens and a model with parameters. Using the Chinchilla-style compute heuristic (FLOPs), compute the training compute . Then, given the compute-optimal Chinchilla ratio of tokens per parameter, state whether this model is over- or under-trained relative to compute-optimality, with justification. (6 marks)
Question 3 — Fine-tuning strategy, forgetting, and distillation (18 marks)
(a) Distinguish feature extraction from full fine-tuning in terms of which parameters receive gradients, and give one scenario where feature extraction is preferable. (4 marks)
(b) Knowledge distillation trains a student on the soft targets of a teacher. With teacher logits and student logits , temperature , the distillation loss is the KL divergence between softened distributions. Write the combined loss , define each term, and explain why gradients from the soft-target term are commonly scaled by . (6 marks)
(c) Define catastrophic forgetting formally as a change in performance on task A after training on task B. Describe how (i) LoRA and (ii) a small replay/rehearsal set each mitigate it, and why. (5 marks)
(d) For INT8 symmetric quantization of a weight tensor with per-tensor absolute maximum , compute the scale factor (mapping to the range ), then quantize and dequantize the value . Report the dequantization error. (3 marks)
Answer keyMark scheme & solutions
Question 1
(a) (4 marks) Forward: . Compute first (-dim), then — cheaper than forming . (2) Initialization: (or small random), . Then at step 0. (1) Why: the model starts exactly as the pretrained model, so early training doesn't inject random noise into a well-tuned function; gradients flow gradually. If both were random, initial output would be corrupted. (1)
(b) (4 marks) LoRA params . (2) Full FT params for layer . (1) Percentage . (1)
(c) (3 marks) Per layer: 2 matrices → . (1) Total: M trainable params. (2)
(d) (4 marks) (i) fp16: bytes B GB. (1.5) (ii) NF4: bytes B GB. (1.5) Compression ratio . (1)
(e) (4 marks) () If , by SVD with nonzero singular values. Set and ; then . (2) () since has rows and has columns. Scaling by doesn't change rank. So if then . (2)
(f) (3 marks) Double quantization quantizes the quantization constants (the per-block scale/absmax factors) themselves — a second-level quantization of the metadata. It saves additional memory (~0.4 bits/param) at negligible accuracy cost.
Question 2
(a) (6 marks) (i) Causal LM: . Every token contributes; strictly left-context. (3) (ii) Masked LM: let be masked positions. . Only masked tokens contribute; bidirectional context. (3)
(b) (4 marks)
Input: the <X> sat <Y> the mat
Target: <X> cat <Y> on the <Z>
(Sentinels replace corrupted spans in the input; the target lists sentinel+original-span, terminated by final sentinel <Z>.) (4; 2 input, 2 target)
(c) (4 marks)
At inference there is no [MASK] token, but if 100% of masked positions were [MASK] during training the model would only learn representations conditioned on seeing [MASK], creating a pretrain/finetune mismatch. (2)
Keeping 10% unchanged forces the model to build good representations for real tokens too; 10% random tokens force it not to blindly trust the observed token, encouraging robust context use. (2)
(d) (6 marks) FLOPs. (3) Compute-optimal tokens . (1) Actual tokens (ratio ). (1) Therefore the model is over-trained relative to Chinchilla compute-optimality (trained on far more tokens than compute-optimal — common for inference-efficient deployment). (1)
Question 3
(a) (4 marks) Feature extraction: backbone frozen (no gradients to pretrained weights); only a new head is trained. Full fine-tuning: all parameters receive gradients and update. (2) Preferable when: very small target dataset (avoids overfitting), limited compute, or when preserving the general representation / avoiding forgetting is important. (2)
(b) (6 marks) , , combined . (3) Definitions: teacher/student logits, temperature softening the distributions, hard labels. (1) scaling: the gradient of the softened KL term is proportional to (softmax with shrinks logit gradients by ; the KL derivative brings another ). Multiplying by keeps the KD gradient magnitude comparable to the hard-label CE gradient so their relative weighting stays across temperatures. (2)
(c) (5 marks) Formal: forgetting ; positive means degradation on A after training on B. (1) (i) LoRA: freezes original weights ; task B only edits low-rank adapters. Base capability on A stored in is untouched, so removing/swapping adapters recovers A. (2) (ii) Replay: interleave a small set of task-A examples during B training; gradients keep A's loss low, constraining the update to a region good for both tasks. (2)
(d) (3 marks) Scale (1) Quantize: . (1) Dequantize: ; error . (1)
[
{"claim":"LoRA per-layer params r*k+d*r = 131072", "code":"r,d,k=16,4096,4096\nresult=(r*k+d*r==131072)"},
{"claim":"LoRA fraction of full FT is 0.78125%", "code":"r,d,k=16,4096,4096\nresult=(Rational(r*k+d*r, d*k)*100==Rational(78125,100000))"},
{"claim":"Total QV LoRA params over 32 layers = 8388608", "code":"r,d,k=16,4096,4096\nper=2*(r*k+d*r)\nresult=(32*per==8388608)"},
{"claim":"NF4 compression vs fp16 is 4x and C=4.2e22 FLOPs", "code":"fp16=7e9*2\nnf4=7e9*0.5\nC=6*7e9*1e12\nresult=(abs(fp16/nf4-4)<1e-9 and abs(C-4.2e22)<1e15)"},
{"claim":"INT8 dequant of 0.31 gives error ~0.001024", "code":"s=Rational(1,2)/127\nq=round(float(Rational(31,100)/s))\ndeq=q*s\nerr=abs(float(deq)-0.31)\nresult=(q==79 and abs(err-0.001024)<1e-5)"}
]