Skip to content

Lab 03 — Cached generation and GQA

Compare cached and uncached generation, verify one KV append per layer per step, compute raw MHA versus GQA cache bytes, and diagnose a broken cache write.

Track 1 practice lab

Practice material, not a published lesson. Not counted in the Track 1 chapter count.

Prerequisite lessons

Setup

  • Python 3.12.10, PyTorch 2.15.0 (CPU), float32, seed 42.
  • Run from `labs/track1/03-cache-and-gqa`.
  • Cache-byte arithmetic uses a stated FP16 assumption of 2 bytes per element; model runs are float32.

Exercise

Run the broken cache path that overwrites slot 0, observe mismatched logits, then use the correct append at `write_position` and confirm parity.

Verification commands

cd labs/track1/03-cache-and-gqa
python3 test_logit_parity.py
python3 test_cache_growth.py
python3 test_mha_gqa_memory.py
python3 test_broken_cache_position.py

Recorded results

Logit parity passes for 5 decode steps. Cache grows by one column per layer per step. MHA uses 8,192 bytes per layer (16,384 total); GQA uses 4,096 per layer (8,192 total), calculated. Broken cache mismatches all 3 steps; the fix restores parity.

Runnable files

All practice labs