8.7.16. dasLLAMA-15 — The Prefix Cache by Hand

Tutorial 13’s scheduler ran a prefix cache for us. Here we drive the same cache by hand — the shape you need when you build your own scheduler, or when you want to see exactly where the skipped prefill goes.

The idea in one line: two requests that start with the same system prompt compute the same KV rows for it, so the first request can donate those rows and the second can attach them instead of recomputing. The cache works in whole KV pages (the pool’s page_rows), so the match is page-granular.

Run it like tutorial 01 (SmolLM2-135M-Instruct works):

daslang.exe -jit tutorials/dasLLAMA/15_prefix_cache.das -- path/to/model.gguf

8.7.16.2. What the cache holds, and giving it back

prefix_chain_list snapshots every donation — tokens covered, pages still alive, hit count, and the preview label. prefix_held_groups counts the pool pages the cache keeps for reuse — pages a plain release would have returned. The cache borrows from the pool, so teardown order matters: prefix_release hands every cached page back, and only then is the pool safe to delete:

var inscope chains <- prefix_chain_list(cache)
for (c in chains) {
    print("chain {c.id}: {c.n_tokens} tokens, {c.hits} hit(s), '{c.preview}'\n")
}
print("cache holds {prefix_held_groups(cache)} page(s)\n")
prefix_release(cache, pool)
delete cache
delete pool

See also

Full source: tutorials/dasLLAMA/15_prefix_cache.das

The scheduler that automates this: dasLLAMA-13 — Serving Many Conversations

Sessions and the KV cache: dasLLAMA-04 — Sessions, the KV Cache, and Memory