Pretraining & Fine-Tuning LLMs
Level: 4 (Application — novel problems, no hints) Time limit: 60 minutes Total marks: 60
Q1. LoRA parameter budgeting & memory. (14 marks)
You fine-tune a decoder-only LLM. A target attention projection is a weight matrix . You apply LoRA with rank to this single matrix, updating where and .
(a) Compute the number of trainable parameters introduced by LoRA on this matrix, and express it as a percentage of the full matrix's parameters. (4)
(b) The model has 32 such attention layers, each with 4 projection matrices () of the same shape. LoRA is applied to all of them. How many trainable LoRA parameters total, and what fraction is this of the ~7B total model parameters? (4)
(c) You now switch to QLoRA: the frozen base weights are stored in 4-bit (NF4) while LoRA adapters remain in 16-bit. Compare the memory to store the frozen 7B base weights in FP16 vs NF4 (in GB, using 1 GB = bytes). (3)
(d) With , at initialization is random and . State what equals at step 0 and explain in one sentence why this initialization is chosen. (3)
Q2. Choosing a fine-tuning strategy. (12 marks)
A startup has a frozen 13B pretrained LLM and three deployment constraints: (i) 20 different downstream client tasks must be served, (ii) only one copy of base weights can fit in GPU memory, (iii) each client's labeled dataset is small (~2000 examples).
(a) Explain why full fine-tuning is a poor fit here, citing storage and catastrophic-forgetting concerns. (4)
(b) Recommend a PEFT approach that lets all 20 tasks share one base model in memory, and describe how task-specific serving works at inference. (4)
(c) The team considers pure feature extraction (freeze base, train only a linear head on pooled hidden states) as a baseline. Give one situation where this would outperform PEFT and one where it would clearly underperform. (4)
Q3. Pretraining objectives — design a masking scheme. (12 marks)
You are designing pretraining objectives for two models on the same corpus.
(a) A classification-focused encoder needs bidirectional context. Name the appropriate self-supervised objective and describe how masking is applied, including the reason 15% masking (with the 80/10/10 split) is used rather than 100% masking of chosen tokens. (5)
(b) A generation-focused model needs to produce fluent left-to-right text. Name its objective and explain why bidirectional attention cannot be used during its pretraining. (3)
(c) T5 uses a unified text-to-text framework with span corruption. Given the input sentence "the quick brown fox jumps", show a plausible corrupted input and the corresponding target using sentinel tokens if the spans "quick brown" and "jumps" are corrupted. (4)
Q4. Quantization & distillation trade-offs. (12 marks)
(a) A 7B model in FP16 requires 14 GB. You quantize to INT8 and INT4. Compute the memory footprints and the percentage reduction of INT4 relative to FP16. (4)
(b) GPTQ is a post-training quantization method. Explain in 2–3 sentences why GPTQ's layer-wise error-compensation approach preserves accuracy better than naive round-to-nearest INT4 quantization. (4)
(c) You instead distill the 7B teacher into a 1.3B student. Write the combined distillation loss with a temperature-scaled KL term and a hard-label cross-entropy term, define each symbol, and state the role of temperature . (4)
Q5. Instruction tuning & catastrophic forgetting. (10 marks)
You perform SFT on a base LLM using an instruction dataset, then observe the tuned model has lost some general reasoning ability present in the base model.
(a) Distinguish instruction tuning from ordinary supervised fine-tuning on a single narrow task in terms of data format and generalization goal. (3)
(b) Explain the mechanism of catastrophic forgetting here and propose two concrete mitigations (other than "use less data"), justifying each. (4)
(c) Suppose after SFT you apply knowledge distillation from the original base model as a regularizer during further training. Explain how this reduces forgetting. (3)
Answer keyMark scheme & solutions
Q1 (14)
(a) LoRA params sizes . (2) Full matrix . Percentage . (2)
(b) Per matrix ; matrices . Total . (2) Fraction of 7B . (2)
(c) FP16: 7e9 × 2 bytes GB. (1.5) NF4 (4-bit = 0.5 byte): 7e9 × 0.5 GB. (1.5)
(d) At step 0, (since ), so (base unchanged). (1.5) Chosen so training starts as an identity/no-op perturbation, preventing a random shock to the pretrained model and giving stable initial gradients. (1.5)
Q2 (12)
(a) Full FT of 13B updates all weights → must store a full 13B-parameter copy per task (20 copies ≈ huge storage). (2) Also with only ~2000 examples per task, full FT overfits and shifts weights far from pretrained values, causing catastrophic forgetting of general capabilities. (2)
(b) Use LoRA / adapters (PEFT): freeze the single shared base, train small per-task adapter weights (a few MB each). (2) At inference, keep one base in memory and hot-swap/merge the relevant task's small adapter for each request — 20 adapters cost negligible memory vs 20 full models. (2)
(c) Outperforms when the pretrained representations already linearly separate the task and data is tiny — few trainable params → less overfitting, faster, no forgetting. (2) Underperforms when the task needs the internal features to adapt (domain shift, complex mapping) — a frozen backbone can't learn new representations that PEFT/LoRA can inject. (2)
Q3 (12)
(a) Masked Language Modeling (MLM). (1) Randomly select ~15% of tokens; of those, 80% replaced with [MASK], 10% replaced with a random token, 10% left unchanged; model predicts the original tokens using full bidirectional context. (2) Not 100% masking because at fine-tuning/inference [MASK] never appears — the 80/10/10 split reduces the pretrain–finetune mismatch and forces the model to build robust representations of all tokens, not just masked positions. (2)
(b) Causal / autoregressive Language Modeling (next-token prediction). (1) Bidirectional attention would leak future tokens into the prediction of the current token, trivially letting the model "see the answer," so a causal mask restricting attention to left context is required. (2)
(c) Example (sentinels <X>, <Y>):
Input: the <X> fox <Y>
Target: <X> quick brown <Y> jumps <Z> (4)
(Full marks if corrupted spans replaced by sentinels in input and target lists sentinel + original span content, terminated by a final sentinel.)
Q4 (12)
(a) INT8: 7e9 × 1 byte GB. (1.5) INT4: 7e9 × 0.5 byte GB. (1.5) Reduction of INT4 vs FP16 . (1)
(b) GPTQ quantizes weights column/group-wise while using a Hessian-based (second-order) approximation to measure the output error each rounding causes, then updates the not-yet-quantized weights to compensate for the error already introduced. Naive round-to-nearest treats each weight independently and lets errors accumulate; GPTQ's compensation keeps the layer's output close to the original, preserving accuracy at 4-bit. (4)
(c) (2) where = teacher/student logits, = softmax, = ground-truth label, = weighting, = temperature. (1) softens the distributions to expose the teacher's "dark knowledge" (relative probabilities of wrong classes); the factor rescales the KL gradient. (1)
Q5 (10)
(a) SFT on a narrow task: input–output pairs for one task, goal = maximize performance on that task. (1.5) Instruction tuning: diverse (instruction, response) pairs across many tasks phrased in natural language, goal = generalize to unseen instructions/tasks (zero-shot following). (1.5)
(b) Mechanism: gradient updates on the new distribution overwrite weights encoding previously learned general skills (no rehearsal of old data). (1) Mitigations (any two, 1.5 each): (i) PEFT/LoRA — freeze base, limit weight drift so original knowledge is retained; (ii) rehearsal / data mixing — include general-domain data in the SFT mix to keep those gradients present; (iii) low learning rate / early stopping / regularization toward base weights (e.g., EWC) — penalize moving important parameters. (3)
(c) Adding a KL/distillation term to the original base model's output distribution anchors the tuned model's predictions to the base behavior on general inputs, penalizing drift away from pretrained knowledge while still allowing the SFT loss to teach the new task — effectively a functional regularizer against forgetting. (3)
[
{"claim":"LoRA params on 4096x4096 with r=16 = 131072 and 0.78125%","code":"p=4096*16+16*4096; full=4096**2; result=(p==131072 and abs(p/full-0.0078125)<1e-9)"},
{"claim":"Total LoRA params over 128 matrices = 16777216 and ~0.24% of 7B","code":"tot=131072*128; frac=tot/7e9; result=(tot==16777216 and abs(frac-0.002396)<1e-4)"},
{"claim":"FP16 14GB vs NF4 3.5GB for 7B; INT4 75% reduction vs FP16","code":"fp16=7e9*2/1e9; nf4=7e9*0.5/1e9; red=(14-3.5)/14; result=(fp16==14 and nf4==3.5 and abs(red-0.75)<1e-9)"},
{"claim":"INT8 7GB, INT4 3.5GB for 7B","code":"i8=7e9*1/1e9; i4=7e9*0.5/1e9; result=(i8==7 and i4==3.5)"}
]