feat: add staged page-level prefix reuse - #138
Draft
Andrewxu313 wants to merge 230 commits into
Draft
Conversation
…-9408 # Conflicts: # batchgen/batchgen_worker.py
lausannel
force-pushed
the
feature/add-staged-page-level-prefix-reuse
branch
from
April 27, 2026 21:47
81abb1b to
a17cc3f
Compare
…HEAD # Conflicts: # batchgen/models/moonshotai/kimi_k25/model.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add staged page-level prefix reuse
Goal
Implement opt-in, page-level prefix KV reuse for BatchGen. Do not implement token-level radix granularity or partial-page sharing in this PR.
The staged outcome is:
Background
The older prefix-cache approach must not be revived directly. It is prior-art risk context only.
This work should build on the request-pool and dynamic host-KV infrastructure, including request-pool scheduling, host-KV reservation/growth, host-KV eviction/re-entry, GPU on-hold/resume, and repeated-prefix workloads.
BatchGen-specific findings:
host_paged_kv_backend.cpptrackspage_owners[page] = sequence_id, andReleaseSequence()frees all pages for that sequence.sequence_id -> SequenceRecord{pages}and cannot represent[shared prefix pages + private suffix pages].gpu_paged_kv_manager.pystores one linear page vector per sequence, and release returns all pages for that sequence.Design
Implement this as a staged, page-level feature. Each milestone must be gated so reviewers can distinguish host memory savings, prefill compute/offload savings, and GPU page-pressure savings.
Milestone 1: Host KV-cache page reuse
Implement host-side page reuse and host page efficiency first:
(model/cache namespace, page_size, page_index, parent_page_hash, current_page_token_hash).shared_prefix_tokens, and private start position.allocate_pages_for_sequences_with_prefix(requests).ReleaseSequence(), host unregister/release, allocation rollback,AsyncOffloadLayerKVToHost(), andAsyncAppendDecodeKVToHost()shared-page safe.Milestone 2: Prefill compute/offload reduction
After Milestone 1 host rows are correct, reduce prefill work for prefix hits:
prefix_shared_tokens,suffix_input_ids,suffix_start_pos,suffix_length, and full logical context length.suffix_length == 0with a decode-like or cached-prefill path. Do not silently fall back to full prefill.Milestone 3: Decode-batch GPU page materialization
After the decode batch is formed, reduce GPU page pressure:
[shared prefix host pages + private suffix/decode host pages].IN_DECODE -> ON_HOLD,ON_HOLD -> IN_DECODE, completion, and extension failure.Acceptance criteria
--enable-prefix-reuse.[shared prefix pages + private suffix pages]while preserving the legacy combinedPages(sequence_id)view.PREFILLED,IN_DECODE,ON_HOLD,EVICTED, andCOMPLETED.IN_DECODE -> ON_HOLD -> IN_DECODE, host eviction/re-entry, and completion release.