Skip to content

[Reshard] Plan Store-backed KV cache reuse across host layouts - #4741

Closed
kunkunblueberry wants to merge 1 commit into
kvcache-ai:mainfrom
kunkunblueberry:rfc-4719-kv-store-reshard-planning
Closed

kunkunblueberry wants to merge 1 commit into
kvcache-ai:mainfrom
kunkunblueberry:rfc-4719-kv-store-reshard-planning

Conversation

@kunkunblueberry

Copy link
Copy Markdown
Contributor

Description

First slice of RFC #4719 (Mooncake Store KV Cache Reshard). This PR implements the logical planning half of the RFC: model semantic domain, logical page keys, source layout manifests, target-driven byte-range generation, and read-template caching. It is a new pure-Python subpackage (mooncake.reshard.kv_cache.store_reshard) next to the existing runtime-to-runtime planner and changes no existing module.

Deliberately out of scope, to keep review focused and to avoid colliding with in-flight work:

Design

  • Logical keys are the reuse contract. ModelSemanticDomain carries only what changes KV content: namespace, model identity and revision, dtype, page size, the global layer set, KV-head count, and K/V head dims. TP, PP, rank identities and physical layout are excluded, so the same token prefix maps to the same Store key across topologies, and the domain digest is embedded in both the page key (<prefix>_<token hash>_<domain digest>) and the manifest key (kv/<domain>/manifests/<layout digest>).
  • The manifest is the only description of a stored layout. Parts declare the global layers and KV-head interval they cover; part byte geometry (lengths, K/V offsets, strides) is derived from the layout plus the logical descriptor, so there is exactly one representation of byte geometry. Manifests are content-addressed: from_json recomputes the digest and rejects tampered or mismatched payloads, and validation rejects layer gaps, cell overlap, non-dense part indices and head-axis gaps.
  • Planning is target-driven. The reader's own host layout decides target offsets; the manifest decides source offsets; the two meet only at logical (layer, head) coordinates. Atomic units are single (layer, head) elements — starting from part-level head intervals would wrongly assume a part's head run is contiguous, which fails in HPLD where head is the outermost axis. Runs are merged only when the merged run is contiguous on both sides, so a plan never reads or overwrites unrelated bytes to enlarge I/O; a strided side simply yields more, smaller ranges. Coverage is verified element-exactly at the end of planning.
  • Templates hold no addresses. ReadTemplateCache keys relative-offset plans by (manifest digest, target binding id, requirement fingerprint); host-pool reallocation invalidates a binding's templates explicitly, and a cache hit never replaces the Store's own readability and lease checks.

Supported layouts

PLHD, LPHD and HPLD (SGLang page_first / page_first_direct|layer_first / page_head), with K before V inside each Part.

Suggested Merge Priority

  • P0 — Urgent
  • P1 — High
  • P2 — Normal
  • P3 — Low

Module

  • Transfer Engine (mooncake-transfer-engine)
  • Mooncake Store (mooncake-store)
  • Mooncake Conductor (mooncake-conductor)
  • Reshard (mooncake-reshard)
  • Mooncake EP (mooncake-ep)
  • Mooncake PG (mooncake-pg)
  • Integration (mooncake-integration)
  • P2P Store (mooncake-p2p-store)
  • Python Wheel (mooncake-wheel)
  • Common (mooncake-common)
  • Mooncake RL (mooncake-rl)
  • CI/CD
  • Docs
  • Other

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Breaking change
  • Documentation update
  • Performance improvement
  • Other

How Has This Been Tested?

Test commands:

cd mooncake-reshard
PYTHONPATH=../python:python python -m pytest tests/kv_cache_store_reshard/ -q
# 78 passed
PYTHONPATH=../python:python python -m pytest tests/ -q --ignore=tests/test_reshard_geometry.py
# 884 passed, 1 failed (pre-existing, environment-only: native-store test needs the compiled extension)

Test results:

  • Unit tests pass
  • Integration tests pass (if applicable)
  • Manual testing done

Coverage of the new suite:

  • the full 3x3 host-layout conversion matrix (PLHD/LPHD/HPLD as both source and target), verified element-by-element on real byte buffers;
  • K and V components independently;
  • uneven PP layer subsets (reader owns layers 1 and 3), with untouched target bytes asserted zero;
  • head-sharded sources (two Parts covering all layers with disjoint head intervals) and requirements whose head interval spans Part boundaries;
  • merge-only-when-contiguous-on-both-sides (PLHD source into LPHD target stays split; same-side layouts collapse to one run);
  • manifest digest tamper rejection, schema/digest validation, layer-gap and cell-overlap rejection, head-split accounting;
  • logical key stability across topologies and sensitivity to every semantic field;
  • template cache hit/miss, binding invalidation, insertion-order eviction.

tests/test_reshard_geometry.py is excluded from the regression run because hypothesis is not installed in the test environment used here (pre-existing, unrelated to this PR).

Checklist

  • I have performed a self-review of my own code
  • I have formatted my code using ./scripts/code_format.sh
  • I have run pre-commit on the files changed in this PR and all hooks pass
  • I have updated the documentation (if applicable)
  • I have added tests to prove my changes are effective
  • For changes >500 LOC: I have filed an RFC issue (RFC already exists: [RFC]: Mooncake Store KV Cache Reshard #4719)

AI Assistance Disclosure

  • No AI tools were used
  • AI tools were used (specify below)

The change was drafted with AI assistance (module decomposition, geometry derivation, planner merge algorithm, and the test suite); the human submitter reviewed every changed line and can defend the change end to end. Tests were run on a private build host.

@kunkunblueberry

Copy link
Copy Markdown
Contributor Author

CI status update: two separate failures, both addressed.

  1. Check Python with Ruff was mine — two unused imports (F401). Fixed, and both ruff check and ruff format --diff are now clean with ruff 0.6.9 (the version the workflow pins).
  2. CTest unit tests failed while building, not testing: main no longer compiles its test tree because [Store] Add LastHitOnly lease selection to BatchProbeKey #4314 changed MasterService::BatchProbeKey to take a GrantLeasePolicy while the MasterScenario DSL kept the two-argument call. This branch now carries the one-line fix from [Bugfix] Pass a lease policy from the BatchProbe DSL step #4742 so the C++ tests can actually run; I will drop that commit and rebase once [Bugfix] Pass a lease policy from the BatchProbe DSL step #4742 lands.

@github-actions github-actions Bot added the Store label Oct 17, 2026
@kunkunblueberry
kunkunblueberry force-pushed the rfc-4719-kv-store-reshard-planning branch from d7ff9b4 to 1930976 Compare October 18, 2026 05:19
@kunkunblueberry

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (19e0ab28) and dropped the temporary [Bugfix] Pass a lease policy... commit — #4729 already fixed that call site, so this branch is back to a single commit containing only the reshard planning subpackage. Re-ran the new suite after the rebase: 78/78.

First slice of RFC kvcache-ai#4719 (Mooncake Store KV Cache Reshard). It implements
the logical planning half of the RFC -- model semantic domain, logical
page keys, source layout manifests, target-driven byte-range generation
and read-template caching -- as a new pure-Python subpackage next to the
existing runtime-to-runtime planner. It deliberately does not touch the
Store write protocol (multipart RFC kvcache-ai#4366) or the SGLang integration:
those are separate slices.

Why this shape:

- Logical keys are the reuse contract. ModelSemanticDomain carries only
  what changes KV content (namespace, model identity and revision, dtype,
  page size, the global layer set, KV-head count and head dims); TP, PP,
  rank identities and physical layout are excluded, so the same token
  prefix maps to the same Store key across topologies. The domain digest
  is embedded in the page key and in the manifest key.
- The source manifest is the only description of a stored layout. Parts
  declare the global layers and KV-head interval they cover; part byte
  geometry (lengths, K/V offsets, strides) is *derived* from the layout
  and the logical descriptor, so there is exactly one representation of
  byte geometry. Manifests are content-addressed: decoding recomputes the
  digest and rejects tampered or mismatched payloads.
- Planning is target-driven. The reader's own host layout decides target
  offsets; the manifest decides source offsets; the two meet only at
  logical (layer, head) coordinates. Ranges are merged only when the
  merged run is contiguous on *both* sides, so a plan never reads or
  overwrites unrelated bytes to enlarge I/O, and a strided side simply
  yields more, smaller ranges.
- Three host layouts are supported and tested byte-exactly: PLHD, LPHD
  and HPLD (SGLang page_first / page_first_direct|layer_first /
  page_head). The full 3x3 conversion matrix is verified element by
  element, including uneven PP layer subsets, head-sharded sources, and
  requirements whose head interval spans several Parts.

Read templates cache the relative offsets of a plan keyed by the
manifest digest, the target binding identity and the requirement; they
hold no live addresses and never replace the Store's own readability and
lease checks.

Tests: 78 new cases in tests/kv_cache_store_reshard, all passing; the
existing reshard suites are unchanged (884 passed, 1 environment-only
failure in a native-store test that needs the compiled extension).

RFC: kvcache-ai#4719
@kunkunblueberry
kunkunblueberry force-pushed the rfc-4719-kv-store-reshard-planning branch from 1930976 to e16c7ae Compare October 18, 2026 12:01
@kunkunblueberry

Copy link
Copy Markdown
Contributor Author

CI note: the last CTest unit tests run failed on MasterServiceEvictScenarioTest.OpLogDurabilityGatesTenantQuotaReclamation — assertion at dsl/scenario.cpp:2281, PutStart(after-durable) failed: TENANT_QUOTA_EXCEEDED. This is not caused by this PR, and I do not think it should block review:

To get a clean signal I re-pushed the same tree (new commit hash, identical tree) so CI starts a fresh run. If the same scenario test fails again, please treat it as #4722 flakiness rather than a regression here.

@kunkunblueberry
kunkunblueberry marked this pull request as ready for review October 22, 2026 08:22
Copilot AI balanced review requested due to automatic review settings October 22, 2026 08:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@LLLL114

LLLL114 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Hi @kunkunblueberry, sorry for missing this. I'm the author of RFC:#4719. logical plan of KV reshard have been merge into mooncake.reshard. I’ve discussed some design-related points with the author of RFC #4366; further adjustments may follow once the multi-part feature is fully supported. If you have any thoughts or questions, we can discuss them on Slack or elsewhere.

@kunkunblueberry

Copy link
Copy Markdown
Contributor Author

Closing this as superseded by #4762, which the RFC author (@LLLL114) has since merged and which covers this slice. I compared the merged mooncake.reshard.kv_cache._store package against this PR before closing, and it matches on every design axis I had argued for:

  • same three host layouts (KVCacheStoreFormat: PLHD / LPHD / HPLD);
  • shard coverage persisted, byte geometry derived — KVCacheStoreLayout states "Persist only shard geometry and axis order. Sizes and strides are derived", and fragments() derives affine offsets without enumerating bytes;
  • content-addressed manifests: kv_cache_store_layout_from_json validates exact fields, full shard coverage and the canonical digest, comparing (layout_id, digest);
  • address-free plans, with the load plan's digest keyed by placement + layout + target DP rank, i.e. the same "relative offsets only" contract.

It also goes further than this PR (upload planning, Store binding and serde, TP/PP placement, and a key_suffix writer label that I had not modelled). Its tests/kv_cache/test_store_ranges.py already parametrizes the full 3x3 layout matrix and verifies byte equality across a TP/PP change, which was the main test asset here.

Keeping a second kv_cache/store_reshard package would only duplicate the abstraction, so I am closing rather than adapting. Happy to help with the follow-up once the multi-part write protocol (#4366) lands.

@kunkunblueberry
kunkunblueberry deleted the rfc-4719-kv-store-reshard-planning branch October 22, 2026 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants