Skip to content

[Store] add TP chunk metadata put APIs - #1908

Closed
zxpdemonio wants to merge 7 commits into
kvcache-ai:mainfrom
openanolis:cruz/tp_chunk
Closed

zxpdemonio wants to merge 7 commits into
kvcache-ai:mainfrom
openanolis:cruz/tp_chunk

Conversation

@zxpdemonio

@zxpdemonio zxpdemonio commented Apr 16, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This PR extracts the PR1-scoped TP metadata write path from the larger TP/CCRP work.

It adds chunk-oriented TP put APIs in the Python store bindings so callers that already own the sharding plan can write shard payloads together with TP metadata, without depending
on later reconstruction/CCRP changes.

Changes in this PR include:

  • adding GlobalMetadata and ChunkMetadata for TP shard metadata
  • adding TP chunk put APIs:
    • put_tensor_chunk_with_tp
    • batch_put_tensor_chunk_with_tp
    • put_tensor_chunk_with_tp_from
    • batch_put_tensor_chunk_with_tp_from
  • routing existing TP put paths through the same metadata-aware chunk put flow
  • adding PR1-scoped tests for chunk put and zero-copy chunk put
  • updating Python API docs for the new APIs

Motivation

This PR introduces tensor chunk write APIs that allow distributed processes to write their own tensor shards directly, instead of requiring the full tensor to be materialized on a single process first. The main motivation is to let each rank write its local tensor chunk concurrently, which avoids the extra all-gather cost and reduces both communication overhead and temporary memory pressure in distributed workloads. This is especially useful when each process already owns its local shard and only needs to persist it together with enough metadata for later reconstruction.

It also prepares the storage interface for future heterogeneous tensor-parallel (TP) read/write scenarios. By making shard-level writes and metadata handling explicit now, we can support more flexible TP layouts and heterogeneous execution environments in follow-up work, where different devices or ranks may not share the same data placement or reconstruction path.

Module

  • Transfer Engine (mooncake-transfer-engine)
  • Mooncake Store (mooncake-store)
  • Mooncake EP (mooncake-ep)
  • Integration (mooncake-integration)
  • P2P Store (mooncake-p2p-store)
  • Python Wheel (mooncake-wheel)
  • PyTorch Backend (mooncake-pg)
  • Mooncake RL (mooncake-rl)
  • CI/CD
  • Docs
  • Other

Type of Change

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

How Has This Been Tested?

  • Performed self-review for:
    • maintainability / readability / duplicate logic
    • stability and memory-safety risks
    • security concerns
    • static performance issues
    • doc/code consistency
  • Ran:
    • python -m py_compile scripts/test_tensor_chunk_api.py
  • Added PR1-scoped tests in scripts/test_tensor_chunk_api.py covering:
    • single chunk put
    • batch chunk put
    • zero-copy chunk put
    • metadata existence/content checks
    • invalid split_dim negative case

Note:

  • full integration test execution was not run here because the current test script connects to a shared store and calls remove_all().

Checklist

  • I have performed a self-review of my own code.
  • I have formatted my own code using ./scripts/code_format.sh before submitting.
  • I have updated the documentation.
  • I have added tests to prove my changes are effective.

zxpdemonio and others added 3 commits April 16, 2026 21:16
Add TP chunk put APIs and metadata serialization in the Python store bindings, with scoped tests and API docs for the PR1 split.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Run clang-format on the TP chunk metadata store bindings after the PR1 split commit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Validate split_dim, clean up partial batch writes, and tighten docs/tests for TP chunk metadata APIs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces new APIs for storing individual tensor-parallel shards and their associated metadata, including put_tensor_chunk_with_tp, batch_put_tensor_chunk_with_tp, and their variants for registered buffers. It also defines GlobalMetadata and ChunkMetadata structures to manage shard placement and full tensor shapes. Review feedback highlights the need for better atomicity and cleanup logic when metadata storage fails to prevent orphaned objects in the store, specifically within the put_tensor_chunk_with_tp and batch_put_tensor_with_tp_impl functions. Additionally, the sharding range calculation logic was flagged for assuming a specific distribution strategy that may not be compatible with all user-defined sharding plans.

Comment thread mooncake-integration/store/store_py.cpp Resolved
Comment thread mooncake-integration/store/store_py.cpp Resolved
Comment thread mooncake-integration/store/store_py.cpp Resolved
@zxpdemonio

Copy link
Copy Markdown
Collaborator Author

This pr is split from #1389

Restore the serialized-tensor decode helpers and make TP metadata key helpers static so the Python store bindings build cleanly across CI configurations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Apr 16, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 0% with 464 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
mooncake-integration/store/store_py.cpp 0.00% 464 Missing ⚠️

📢 Thoughts on this report? Let us know!

Tighten TP chunk put validation to reject shard shapes that don't match the default TP sharding plan, and clean up partially written shard/metadata objects when later metadata writes fail.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
return TensorDtype::UNKNOWN;
}

inline size_t get_element_size(int32_t dtype) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not directly use sizeof?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This follows the existing put_tensor serialization path.
In this code path we only have the runtime TensorDtype value from TensorMetadata, not a concrete compile-time C++ type, so sizeof(...) is not directly applicable here.
We therefore keep a runtime dtype-to-element-size mapping, consistent with the current put_tensor / put_tensor_from logic.

Reject mismatched TP chunk payloads in put_tensor_chunk_with_tp_from before storing shard data so the from-buffer API matches the other TP chunk write paths.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@zxpdemonio

zxpdemonio commented Apr 17, 2026 •

Copy link
Copy Markdown
Collaborator Author

This PR introduces tensor chunk write APIs that allow distributed processes to write their own tensor shards directly, instead of requiring the full tensor to be materialized on a single process first. The main motivation is to let each rank write its local tensor chunk concurrently, which avoids the extra all-gather cost and reduces both communication overhead and temporary memory pressure in distributed workloads. This is especially useful when each process already owns its local shard and only needs to persist it together with enough metadata for later reconstruction.

It also prepares the storage interface for future heterogeneous tensor-parallel (TP) read/write scenarios. By making shard-level writes and metadata handling explicit now, we can support more flexible TP layouts and heterogeneous execution environments in follow-up work, where different devices or ranks may not share the same data placement or reconstruction path.

@zxpdemonio

Copy link
Copy Markdown
Collaborator Author

Performence test with different sizes:

256MB

  • put: 39.14 ms
  • put_from: 34.57 ms
  • chunk_put: 42.93 ms
  • chunk_put_from: 34.49 ms

512MB

  • put: 85.75 ms
  • put_from: 72.54 ms
  • chunk_put: 94.01 ms
  • chunk_put_from: 74.27 ms

1024MB

  • put: 162.71 ms
  • put_from: 130.16 ms
  • chunk_put: 158.90 ms
  • chunk_put_from: 134.58 ms

And I profiled the overhead of metadata put:just 0.6ms。

We have not tested the performance of multiple threads concurrently writing their own chunks versus all-gather + put yet, because even when multiple threads write in parallel today, the underlying path is still effectively serialized. As a result, the expected advantage cannot be observed yet. We need to fix that issue first before the benefit can be reflected in benchmark results. For now, these performance tests show that there is no obvious performance regression.

Add a tensor API benchmark that warms up large zero-copy TP writes before comparing put and TP chunk put paths. This verifies the new TP chunk APIs do not show an obvious regression once Mooncake reaches steady-state performance.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@zxpdemonio

Copy link
Copy Markdown
Collaborator Author

We decided not to redesign the underlying metadata layer for TP chunk metadata at this stage.
The main reason is that this requirement is currently very specific to tensor semantics, especially TP weight storage, rather than a general object-store requirement. The current global metadata + chunk metadata model is mainly used to describe tensor layout information such as full shape, split dimension, TP size, and per-chunk range. Other important workloads, such as KV cache, do not currently need the same abstraction. Because of that, pushing this logic down into a generic metadata mechanism would introduce broader system complexity for a use case that is still narrow in scope.
We also do not think this is the highest-leverage optimization for the current performance issue. Based on our profiling so far, the main cost is still in the large shard data write path itself, not in the small metadata objects. Redesigning metadata storage would likely add significant implementation and maintenance complexity, while providing limited near-term performance benefit.
For now, we believe it is better to keep TP-specific metadata in the tensor integration layer, where it remains clearly scoped to tensor/weight use cases. If similar requirements later appear in other workloads, or if we need a more general typed-object abstraction in the future, we can revisit a broader metadata redesign then.

@zxpdemonio zxpdemonio closed this Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants