LearnQuestion bank

A PyTorch notebook trains a small transformer, but the loss plateaus high and evaluation numbers look wrong. Find the bugs. Follow-up: add a KV cache to its generation loop and prove it matches full recomputation.

Applied LLMCodingHard

Theme reported publicly: Wayve

Answer it yourself first, out loud or on paper, then compare.

Answer

Model answer, written in a candidate's voice

In short

  • Lead with a reproducible baseline and narrate each hypothesis before changing code.
  • Overfit one batch; check shapes/broadcasting, causality mask direction, next-token label shift, and loss on logits rather than probabilities.
  • Check train/eval mode, no_grad, optimiser zero_grad, LR/scheduler, shuffling and leakage with small assertions.
  • Add per-layer K/V cache, feed only the new token, keep positions/RoPE offsets aligned, and compare logits to full recomputation.
  • Prove equivalence with fixed seeds and tolerances, then re-run eval and report any mismatch honestly.

In full

I would treat this as hypothesis-driven debugging, not code-staring: fix seeds, freeze the data split, and first overfit a single batch to prove the model can learn at all. Then I would check shapes and broadcasting, the causality mask direction, next-token label shift, and that the loss is computed from raw logits rather than probabilities, while also verifying model.train()/eval(), no_grad in evaluation, optimiser zero_grad, LR/scheduler behaviour, and that shuffling and train/eval splits are not leaking. I would make each check a small reproducible assertion, such as loss dropping on one batch, masked positions being exactly -inf, labels equal to inputs shifted by one, and eval metrics computed under eval plus no_grad, rather than trusting a plateau. For the follow-up, I would cache K and V per layer, append only the new token’s K/V, feed just that token through the model, and keep position indices and RoPE offsets aligned with the full sequence length. The proof is a fixed-seed comparison: run full recomputation over the whole prefix and run the cached loop token by token, then assert logits match within a tight float32 tolerance and that greedy/sampled outputs match when sampling is seeded. I would narrate each hypothesis and its verification, and if the cache or eval still disagrees I would report the failing assertion and the smallest failing case instead of tuning until the numbers look plausible.

Corrected by an independent AI reviewer.

What a strong answer covers

  • Systematic checks: overfit a single batch; shapes and broadcasting; mask direction/causality; loss inputs (logits vs probabilities, label shift for next-token); model.train()/eval() and no_grad; optimiser zero_grad; learning rate/scheduler; data leakage or shuffling bugs
  • Uses small reproducible tests and assertions rather than staring at code
  • KV cache: cache K/V per layer, feed only the new token, correct positions (RoPE offsets), compare logits against full recomputation within tolerance
  • Narrates hypotheses and verification

Common mistakes

  • Tweaking hyperparameters before checking correctness
  • forgetting position offsets when adding the cache