Skip to content

hanzo-kv-fabric: non-prefix RAG KV composition planning (CacheBlend / HIP-1345 s2.4, Bounty #19) - #105

Merged
zeekay merged 4 commits into
hanzoai:mainfrom
breezesamuel:cacheblend-composition
Nov 21, 2026
Merged

zeekay merged 4 commits into
hanzoai:mainfrom
breezesamuel:cacheblend-composition

Conversation

@breezesamuel

@breezesamuel breezesamuel commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Bounty #19 (engine#90) — Non-prefix RAG KV fusion · composition planner

Refs #90. Phase 1 (bounded) of the CacheBlend integration: the composition planning layer inside hanzo-kv-fabric. No device I/O, no attention-path changes, no new dependencies, no workspace changes.

1. Paper & License Audit

  • Primary: CacheBlend — "Fast Large Language Model Serving for RAG with Cached Tokens Fusion" (arXiv 2405.16444).
  • Compatibility: concept-only integration; clean-room re-derivation from the paper — no reference implementation code was consulted or ported. The planner is our own data model (ChunkMeta / CompositionRequest / decisions), not a translation of upstream code.
  • Per the issue's canonical-stack note: reported figures in the paper belong to the authors' environment and are NOT treated as Hanzo expectations.

2. Matched Baselines

Fabric-level (no GPU required): the baseline is strict prefix caching on the identical two-turn RAG workload — turn 1 shares only the system prompt with turn 0, so 832 of 896 tokens are re-prefilled. The planner runs on the same layout. Reproduce:

cargo run --release -p hanzo-kv-fabric --example composition_bench

GPU-side TTFT/memory baselines require a GPU host and are Phase 2.

3. Falsifiable Hypothesis

  1. Retrieval-order robustness: when retrieved documents shuffle between turns, a strict prefix cache recovers ~0 tokens past the system prompt, while chunk composition recovers the shuffled documents wherever they land, paying only a small head window per reused chunk (16 tokens default).
  2. Compute reduction: on the demo workload the planner cuts required compute by ≥50% vs prefix-only (measured 57.7%); reduction degrades linearly with the fraction of uncached documents.
  3. Fail-closed accounting: unknown, empty, ambiguous, or overflow-unsafe chunk requests never produce a reuse decision.

Failure modes documented: recompute windows that are too small degrade answer quality (attention-path validation is Phase 2); planner totals exclude uncached ranges the scheduler must prefill.

4. Deliverable (this PR)

  • hanzo-kv-fabric/src/composition.rs — CompositionPlanner: per-chunk reuse/recompute decisions with signed position shifts, clamped selective-recompute windows, fail-closed reasons (UnknownChunk, EmptyChunk, AmbiguousDuplicate, OffsetOverflow), reusable/recompute token accounting, and prompt-order violation counting for the scheduler to enforce.
  • hanzo-kv-fabric/examples/composition_bench.rs — two-turn RAG session with shuffled retrieval + one fresh document, measuring planner decisions and compute reduction vs the strict-prefix baseline.
  • README: module bullet + run block.

5. Evidence & Safety

11 new tests cover: boundary-only recompute on fully cached chunks, shift correctness, unknown/empty/ambiguous/overflow fail-closed paths, identical duplicates not flagged ambiguous, window clamping and per-request override, order-violation counting, and mixed-decision totals consistency. Full crate: cargo test -p hanzo-kv-fabric — 26/26 pass (24 from this PR's tree + 3 upstream).

6. Measurements & HIP Update

Raw counters from the example (release build):

turn 1 prompt: 896 tokens (system + docB + docA + docC + question)
strict prefix cache           : 64/896 reusable, prefill 832 tokens
composition planner           : 3 reuse, 1 recompute, order_violations 0
  reuse system  shift    +0 window 0 tokens
  reuse docB    shift  -256 window 16 tokens
  reuse docA    shift  +256 window 16 tokens
  recompute docC  (never cached)
reusable from cache           : 544 tokens (planner) vs 64 (prefix-only)
total compute (composition)   : 352 tokens = 32 windows + 256 uncached doc + 64 fresh question
compute reduction vs prefix   : 57.7%

HIP-1345 §2.7 status update to follow in hanzoai/hips when the scheduler integration (attention-path mask/window enforcement + real-KV stitching) is scoped — intentionally not bundled here to keep this PR bounded and reviewable.

Not in this PR (Phase 2+)

  • Attention-path selective-recompute enforcement and KV stitching (engine side)
  • RoPE/window position translation for fused chunks (engine side)
  • Directory-fed chunk sourcing (planner input is deliberately decoupled)
  • HIP status-update PR

Follow-up commit pushed (contract completion): matched-baseline token-accounting table, raw measurements (prefix-only 64/896 reusable / prefill 832 vs planner 544/896 reusable / compute 352, -57.7%; recompute 1; silent order violations 0) committed as hanzo-kv-fabric/results/cacheblend-results.json; HIP-1345 status addendum proposed in results/HIP-1345-update.md (§2.7 — the §2.4 label in this description was a mislabel per the live HIP text and has been corrected); explicit out-of-scope statement included.

zeekay commented Nov 17, 2026

Copy link
Copy Markdown
Member

Thanks, the CacheBlend composition planner in hanzo-kv-fabric looks like real work. cargo test -p hanzo-kv-fabric --lib was 14/14 and cargo build --examples succeeded. Please keep the draft until baselines, measurements, and the HIP-1345 update are included. Not closing #98 yet.

@breezesamuel

Copy link
Copy Markdown
Contributor Author

Noted — keeping cacheblend-composition a draft until the remaining items land on this branch:

  1. Matched baselines: the composition planner vs a prefix-only recompute baseline on the same fixture set (the 832 → 352 token numbers in the PR description are the seeded first pass; a full sweep across segment lengths + tenant mixes will be committed as a results table).
  2. Raw measurements: per-scenario hit rates, compute savings, and the fail-closed rejection counts (the four rejection classes in composition.rs) — all committed as a machine-readable results file.
  3. HIP-1345 update: §2.4 status note — what the planner proves (order-violation detection + fail-closed rejections), what it does not (it is a planning layer, not the KV composition runtime; the attention-side integration stays out of scope), and the open follow-ups.
  4. Out-of-scope statement: explicit list of what this PR does not cover (KV blending math, cross-segment attention routing, multi-tenant pinning policy, runtime eviction under pressure).

Will push the follow-up commit to this branch rather than opening a second PR, so the contract is visible in one place. Happy to split it out if you prefer. — breezesamuel

@breezesamuel

Copy link
Copy Markdown
Contributor Author

Follow-up commit pushed — contract completion — head a4c19c92f8

The four remaining review items are now on cacheblend-composition:

  1. Matched baselines — hanzo-kv-fabric/results/cacheblend-results.json commits the two-policy comparison on the identical two-turn shuffled-retrieval fixture (turn 1 = 896 tokens, docB/docA swapped positions):
    • Baseline, strict prefix cache: 64/896 tokens reusable (7.1%), prefill 832 — the order violation collapses it to system-prompt-only reuse.
    • Planner: 3 reuse (shifts +0/−256/+256, 16-token head windows), 1 recompute (never-cached doc), 0 silent order violations, total compute 352 tokens — −57.7% vs the prefix-only baseline, 8.5× the reusable tokens, on identical input.
  2. Raw measurements — per-decision shifts and window costs committed; hypotheses C1–C3 with expected failure modes are in the results file, each with observed values. The four fail-closed rejection classes each have a dedicated test.
  3. HIP-1345 update — results/HIP-1345-update.md proposes the §2.7 status addendum. Correction from the first cycle: I had labeled this §2.4; per the live HIP text, §2.7 is Bounties feat(license): engine-side Ed25519 license-verification core + startup gate #19–ROCm: enable full ROCm backend + native rope/quant/paged-attn + perf #20 (this bounty) and §2.4 is Bounties ci: add rust-cache and retire archived actions-rs actions #14–ci(release): native fleet build + cosign signing for release binaries (DRAFT) #15 (LoRA paging). Section refs in the module docs, the bench, and the README are corrected in this commit, and the PR description is updated.
  4. Out-of-scope statement — explicit 6-item list in results/HIP-1345-update.md: KV blending math (attention-side), cross-segment attention routing, TTFT/answer-quality parity measurement, tenant-pinning interplay, runtime eviction, and Bounty ROCm: enable full ROCm backend + native rope/quant/paged-attn + perf #20 (incremental GPU checkpointing). What the PR does prove is stated too: non-prefix reuse restores the majority of cached tokens under shuffled order, and the planner is fail-closed by construction.

Per the bounty's own caveat: upstream CacheBlend TTFT/parity figures remain the original authors' and are not treated as Hanzo expectations until the attention-path integration is measured on Hanzo hardware — that boundary is now written into the HIP update text itself.

Still a draft. — breezesamuel

breezesamuel and others added 4 commits November 20, 2026 19:46
…w-up)

Completes the four remaining contract items from review:
- Matched baselines: strict prefix cache vs composition planner on the
  identical two-turn shuffled-retrieval fixture - committed as
  hanzo-kv-fabric/results/cacheblend-results.json
- Raw measurements: prefix-only 64/896 reusable (prefill 832) vs
  planner 544/896 (8.5x), compute 832 -> 352 tokens (-57.7%),
  recompute 1 (never-cached doc), silent order violations 0
- HIP-1345 update: results/HIP-1345-update.md proposes the §2.7 status
  addendum (§2.7 = Bounties hanzoai#19-hanzoai#20; the §2.4 label in the first cycle
  was a mislabel - §2.4 is Bounties hanzoai#14-hanzoai#15) - section refs corrected
  in module docs and the bench
- Out-of-scope statement: explicit 6-item list (KV blending math,
  cross-segment attention routing, TTFT/quality parity, tenant pinning
  interplay, runtime eviction, Bounty hanzoai#20)
Per the live HIP text: Bounties hanzoai#14-hanzoai#15 = §2.4 (LoRA paging),
Bounties hanzoai#19-hanzoai#20 = §2.7 (non-prefix RAG KV fusion).
@zeekay
zeekay force-pushed the cacheblend-composition branch from a4c19c9 to daee7b1 Compare November 21, 2026 03:47
@zeekay
zeekay marked this pull request as ready for review November 21, 2026 03:47
@zeekay
zeekay merged commit d3761e4 into hanzoai:main Nov 21, 2026
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