---
title: A PyTorch notebook trains a small transformer, but the loss plateaus high and evaluation numbers look wrong. Find the…
url: https://gurkanozkan.ai/learn/questions/applied-llm-dbg-10/
markdown: https://gurkanozkan.ai/learn/questions/applied-llm-dbg-10.md
kind: question
role: Applied LLM
stage: Coding
difficulty: Hard
json: https://gurkanozkan.ai/api/learn/questions/applied-llm-dbg-10.json
robots: noindex
---

# 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 LLM · Coding · Hard

Theme reported publicly: [Wayve](https://api.ashbyhq.com/posting-api/job-board/wayve)

## 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

## Topic guides

- [Debugging ML and LLM systems](https://gurkanozkan.ai/learn/topics/debugging.md)
- [KV cache](https://gurkanozkan.ai/learn/topics/kv-cache.md)
- [Training neural networks: losses, optimisers, numerics and scale](https://gurkanozkan.ai/learn/topics/training.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
