Audit Generation and Cache State

Debug generation in dependency order: checkpoint and logits, cache tensors, positions and ownership, numerical equivalence, policy state, then stopping. Use invariants and failure signatures instead of judging output alone.

Generation failures can come from the model, policy, cache, positions, stopping logic, or request bookkeeping. Separate those layers before tuning output.

1. Establish the Uncached Reference

  • load the intended checkpoint and vocabulary;
  • set evaluation and inference mode;
  • run the complete prompt without a cache;
  • record its final logits row.

If this result is already wrong, a cache cannot repair it.

2. Audit Cache Construction

  • verify one K and V tensor per layer;
  • verify (B,h,t,dh)(B,h,t,d_h) axes and sequence-axis append;
  • verify layer ownership;
  • verify cache length equals consumed input count;
  • compare each new K/V record with full-prefix projections.

3. Audit Position and Request State

  • continue positions from prompt length;
  • keep cache and counter per request;
  • reject stale caches after parameter changes;
  • never combine one prompt's cache with another prompt's token stream;
  • reject context overflow before reading a nonexistent position row.

4. Audit Policy after Logit Equivalence

Hold logits fixed. Check temperature domain, top-kk range, renormalization, tie behavior, and random-generator state. A different sampled token does not by itself prove a cache error.

Failure Matrix

ObservationFirst check
mismatch begins at token plus positionposition index or token ownership
layer 1 matches, layer 2 differslayer 2 cache source and append
cache length grows by twoduplicate append or duplicated prompt token
batch row changes when another request arrivescache mixing across batch or request axes
greedy differs but logits matchpolicy or tie rule
sampled runs differ but probabilities matchseed and RNG call order
cache works until weights updatestale projected K/V
generated token cannot be fed backcontext limit reached

The executed wrong-position and wrong-layer failures preserve valid tensor ranks while producing logit errors 23.0252 and 3.6990 respectively.

Q1. Diagnose two cache failures

Full and cached logits match through layer 1. At layer 2, cached length is correct but values equal layer 1's cache. A second request also changes the first request's cached logits. Identify both failures.

Choose both failures

Select one choice, then check.

Hint
Ask who owns each cache along two separate axes.
Solution
Layer 2 is reading layer 1's K/V records, and request-local state is being shared or overwritten across requests.
Not attempted
Review

Not marked done.

Inspection Comes Next

Once cached and uncached generation agree, Chapter 9 can intervene on attention, residual updates, activations, and logits without confusing an implementation mismatch with a model behavior.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.

LLM PrimerAudit Generation and Cache Statehttps://llmprimer.com/transformers/autoregressive-generation-and-efficient-inference/audit-generation-and-cache-state© 2026 LLM Primer