Trace cached projections
Optional arithmetic and percentage refresher — return to this activity
A cache stores intermediate keys and values for already processed tokens. In a causal model, earlier token representations cannot depend on future tokens. Reusing those values saves projections, but each new query still compares against the available prefix.
Instrumented generator: local teaching model contract
All people, records and numbers in this pack are authored teaching examples. They are not records or performance claims about real organizations.
- G1 · The downloadable Python generator uses four token IDs and fixed authored two-number embeddings with causal attention. It is untrained and is not a pretrained language model. It demonstrates execution and cache equivalence, not text quality.
- G2 · Input is 1–16 token IDs; output limit is 0–16 tokens. EOS is token 3. Greedy selection chooses the lowest ID on a tie. Reaching a limit and emitting EOS are reported separately.
- G3 · The clock starts after imports. Validation/token preparation, prefill, first-token selection, decode steps and total generation are timed with a monotonic high-resolution clock. Report serialization and disk writing are outside total generation. Loading/import/terminal display are not measured.
- G4 · Cached attention retains projected keys and values for exactly the prior token prefix. Changed prefixes require a fresh cache or rejection. Every step’s scores, not just chosen IDs, must agree with uncached execution within 1e-12.
- G5 · Timing varies by machine and run. Compare the same prompt, output limit and stopping behavior, repeat runs, report all outcomes and the measurement boundary. Projection and attention-term counts are not milliseconds.
Count the work before claiming a speedup
| Call | Full prefix | New projections with cache |
|---|---|---|
| 1 | 2 tokens | 2 tokens |
| 2 | 3 tokens | 1 token |
| 3 | 4 tokens | 1 token |
| Total | 9 | 4 |
A projection converts a token’s numbers into a key used for comparison and a value used for the weighted result. The cache retains earlier keys and values. The new query still compares with every token in the prefix: 2 + 3 + 4 = 9 attention terms in either path. Fewer projections do not guarantee a proportional time saving.
Worked example
For prompt length two and three appended tokens, the uncached path projects prefixes of lengths 2, 3 and 4: nine token projections. The cached path projects 2 then 1 then 1: four. Both must return matching scores at each step; counts alone do not establish speed.
Check the boundary
This two-dimensional single-layer model demonstrates cache correctness, not the memory or performance of a full language model. Exact-prefix and model identity are prerequisites for reuse; equal input length is insufficient.