Skip to content

feat(core): a paged prompt cache, a radix tree over the backend's own KV pages - #625

Merged
ashhart merged 2 commits into
ashhart:mainfrom
BlivionIaG:core-prompt-radix
Oct 10, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
BlivionIaG:core-prompt-radix

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The prompt-cache slice tracked in #491: a paged prompt cache, core/prompt_radix.zig, for a backend whose attention cache is paged. No backend uses it yet; the Qwen3.5 HIP engine will. This is the mechanism #505 cites from #490 (hip_prefix.zig, prefix.zig), moved to core on prompt_cache.zig's types.

  • A request takes its longest match in a radix tree over token pages. The pages below its resume point are shared, not copied, and it resumes the deepest recurrent-state snapshot on that path.
  • Marks follow prompt_cache.zig's rules: the history, shared system blocks, and with Rules.tail the last whole page.
  • reclaim frees the least valuable leaf that no stream holds.
  • The lane stream carries its request's history_len and shared_prefixes. LaneHost passes them through, because a backend that keeps its own states plans marks from them (second commit, lane_host.zig and lanes/stream.zig).
  • prompt_cache.zig gains only the types the two stores share: Pages (who holds each of the backend's pages), Rules.page, Rules.tail and Kept. Their defaults leave every existing store as it was.

Receipt

  • Environment: base main at 1184cdb (with feat(lanes): a prompt pass a chunk a round, between the other streams' rounds #623), head d6ffe84. Zig 0.17.0, on macOS (Apple silicon) and Linux 7.1.7.
  • Exactness: prompt_radix_test.zig runs over host memory. A resumed pass keeps exactly the pages and states it should: resume at the history, shared pages below it, marks, a shared cut outliving its conversation, eviction order, refusals past the limits, a failed copy keeping nothing, and reclaim sparing pages a stream holds. "a lane host hands the backend the request's history and shared prefix lengths with its stream" fails with the passthrough removed (expected 6, found 0). With reclaim's holder check broken, "reclaim frees the pages of the least valuable leaf nobody holds" fails. The existing prompt_cache and lane_host_reuse tests are unchanged and pass.
  • Decode and prompt speed: not applicable (no backend uses it yet).
  • Tests run:
    • macOS zig build test test-golden -Dcpu=apple_m1: 141/141 steps, 993/995 tests (2 skipped); golden 309/319 equal, 0 unexpected.
    • Linux zig build test: 37/37 steps, 398/398 tests, the same count as main: Linux CI doesn't run engine_api's tests, so these run on macOS.
    • zig fmt --check is clean on the changed files.
    • lean_check.py: 542 problems, the same as main.
  • Not run, and why: device runs (no backend uses it in this change).

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (No engine path touched.)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Its user, the HIP engine, will be held to "resumed equals fresh" on device.)
  • No precision traded for speed.
  • Prompt processing is no slower than the last release. (No prompt path touched.)
  • The description says which platforms I ran and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

… KV pages

prompt_radix.zig keeps prompt reuse for a backend whose attention cache is paged: a request takes its longest
match in a radix tree over token pages, shares the pages below its resume point instead of copying them, and
resumes the deepest recurrent-state snapshot on that path. Marks follow prompt_cache.zig's rules (the history,
shared system blocks, the last whole page); reclaim frees the least valuable leaf no stream holds.

prompt_cache.zig gains only the types it shares: `Pages` (the backend's page holders), `Rules.page` and
`Rules.tail`, and `Kept`. Their defaults leave every existing store as it was. No backend uses the paged store
yet; the Qwen3.5 HIP engine will. Six host-memory tests cover resume, marks, shared cuts, eviction order,
refusals and reclaim.
… lengths

A backend that keeps prompt states itself (the paged store) needs the lengths prompt_cache.zig plans marks from:
the rendered history and the shared system blocks. LaneHost now passes `history_len` and `shared_prefixes`
from the request into the lane stream; nothing reads them in a backend that does not keep its own states.
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG. The paged prompt cache is merged into main (1d8c625) with both your commits as they were, and it ships in 1.0.6. It and #623 both added tests at the end of lane_host_test.zig, so the merge keeps both. The lane host and stream changes leave today's engines as they were: on a GB10, Nemotron's 29 served replies (prompt cache on, resumed at 6,144 and 8,192 tokens) and its four release prompts equal main's, and the Mac and Linux host tests pass.

When the HIP engine first uses it, please gate it as #571 did for the other stores: chat with the cache equals chat without it, through delete, edit and regenerate rewinds.

@ashhart
ashhart merged commit 30ed6cf into ashhart:main Oct 10, 2026
2 checks passed
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Your rebase landed a few seconds after we fetched, so we merged the rebased head too (30ed6cf). It changes no files, since it matches what's already in main, and GitHub now shows this as merged. Thanks again.

BlivionIaG added a commit to BlivionIaG/TensorFold that referenced this pull request Oct 10, 2026
… and model checks

The HIP branch's engine (ashhart#490) on ashhart#463's runtime and the kernel launches of ashhart#617 and ashhart#621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (ashhart#625), chunked prompts (ashhart#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (ashhart#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 10, 2026
…r the backend's own KV pages

Thanks @BlivionIaG.

# Conflicts:
#	zig/src/core/lane_host_test.zig
ashhart pushed a commit that referenced this pull request Oct 16, 2026
… and model checks

The HIP branch's engine (#490) on #463's runtime and the kernel launches of #617 and #621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (#625), chunked prompts (#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
ashhart pushed a commit that referenced this pull request Oct 16, 2026
… and model checks

The HIP branch's engine (#490) on #463's runtime and the kernel launches of #617 and #621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (#625), chunked prompts (#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…r the backend's own KV pages

Thanks @BlivionIaG.

# Conflicts:
#	zig/src/core/lane_host_test.zig
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG added a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… and model checks

The HIP branch's engine (ashhart#490) on ashhart#463's runtime and the kernel launches of ashhart#617 and ashhart#621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (ashhart#625), chunked prompts (ashhart#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (ashhart#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
BlivionIaG added a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… and model checks

The HIP branch's engine (ashhart#490) on ashhart#463's runtime and the kernel launches of ashhart#617 and ashhart#621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (ashhart#625), chunked prompts (ashhart#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (ashhart#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants