Skip to content

[Refactor] Decouple AscendStore metadata into focused modules - #21725

Open
zss-hp wants to merge 4 commits into
vllm-project:mainfrom
zss-hp:refactor/ascend-store-metadata
Open

zss-hp wants to merge 4 commits into
vllm-project:mainfrom
zss-hp:refactor/ascend-store-metadata

Conversation

@zss-hp

@zss-hp zss-hp commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

Decouple the AscendStore metadata implementation, based on #20996, rebased onto current main with all conflicts resolved.

vllm_ascend/distributed/kv_transfer/kv_pool/ascend_store/metadata.py (~1240 lines on main) mixed scheduling payloads, the token database, key building, cache-group configuration, and worker transfer state in one module. This PR splits it into single-purpose modules without changing cache behavior:

  • metadata.py keeps scheduling and connector payloads (LoadSpec, ReqMeta, AscendConnectorMetadata, AscendStoreKVConnectorWorkerMetadata) and re-exports moved helpers for compatibility with existing imports
  • cache_config.py resolves topology and cache-group configuration (TP/DCP mismatch, block sizes, cacheable and sharded-Mamba group ids)
  • pool_keys.py owns PoolKey building, block-hash serialization, and lazy grouped hash views
  • request_state.py holds RequestTracker, side-effect-free SaveDecision, and group id normalization
  • token_database.py owns the chunked token database (chunk selection, key prefix cache, segmented addressing)
  • transfer_metadata.py consolidates per-request worker arrays, keys, masks, and offsets into RequestTransferState
  • cache_layout.py registers per-group buffer layout descriptors shared by bulk and layer selection

Field contract: the 22 worker-preparation fields that used to sit directly on ReqMeta (save_keys, load_keys, store_masks, block_gvas_by_group_np, key/offset fields, ...) move to request.transfer. ReqMeta keeps a legacy **transfer_fields constructor entry point and delegating accessors (get_block_ids_array, get_transfer_arrays).

Adapted to current main: since #21314 moved layerwise save/load preparation into layerwise/planes/ data-plane strategies, this PR migrates 58 legacy request./view. field accesses in planes/block_key.py and planes/gva.py onto the consolidated request.transfer state, and fuses _data_plane.prepare_sessions with the new field location in prepare_layerwise_step. test_pool_worker_pp_object_bounds.py now builds a real ChunkedTokenDatabase instead of the worker.token_database = worker alias. The benchmark fixture is synced with the current scheduler contract so the harness runs on the merged tree.

Cache keys, hash selection, mask boundaries, transfer ordering, partial-block snapshots, speculative rollback, Mamba null blocks, and pipeline layer coordinates are preserved.

Does this PR introduce any user-facing change?

No. Internal refactor only: no API, parameter, environment variable, or default behavior change; no release note fragment needed.

How was this patch tested?

  • A/B comparison under an identical environment: full tests/ut/distributed/ascend_store/ + related kv_offload tests (1114 cases) on origin/main vs this branch after the merge: zero per-test regressions; the 7 new tests (group selection, descriptor addressing, save decisions, copied-view ownership) all pass.
  • One mypy assignment error carried by [Refactor] Split AscendStore metadata responsibilities #20996 was fixed (mixed tuple/list type in the layerwise batch builder); 15 cpu-ut failures (stale token_database alias fixture, two unverified transfer-array assertions) were fixed and cpu-ut is green on both vLLM matrix versions.
  • The review finding on cache_layout.py (stride broadcast modulo) is fixed.
  • ruff check / ruff format pass; metadata microbenchmarks from [Refactor] Split AscendStore metadata responsibilities #20996 validation show no regression and improvements on large configs (e.g. 4096 blocks: 162us -> 85us).
  • NPU hardware / E2E validation is left to CI.

Checklist

Testing

  • Boundary / edge-case coverage: new tests cover empty groups, missing arrays, grouped fallback paths, and stride/length broadcast descriptors (test_metadata.py, test_segmented_cache_transfer.py).

  • Meaningful & reliable tests: the 7 new tests assert real metadata behavior, not mock implementation details.

  • Performance impact: benchmark evidence above; the normal path reuses the group-0 ID array and avoids legacy list-normalization work.

  • vLLM main: vllm-project/vllm@c410543

@zss-hp
zss-hp requested a review from LCAIZJ as a code owner November 12, 2026 12:01
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the AscendStore metadata implementation to improve maintainability and modularity. By splitting the large metadata.py file into single-purpose modules, the codebase becomes more organized and easier to navigate. The changes preserve existing cache behavior and logic while providing a cleaner separation of concerns between scheduling, configuration, and transfer state management.

Highlights

  • Modularization of AscendStore Metadata: Decoupled the monolithic metadata.py module into specialized modules: cache_config.py, pool_keys.py, request_state.py, token_database.py, transfer_metadata.py, and cache_layout.py.
  • Encapsulation of Transfer State: Introduced RequestTransferState to encapsulate per-request worker arrays, keys, masks, and offsets, separating them from the core request metadata.
  • Performance Optimization: Metadata microbenchmarks indicate performance improvements for large configurations, reducing latency from 162us to 85us for 4096 blocks.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions

Copy link
Copy Markdown
Contributor

👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:‌‌

  • A PR should do only one thing, smaller PRs enable faster reviews.
  • Every PR should include unit tests and end-to-end tests ‌to ensure it works and is not broken by other future PRs.
  • Write the commit message by fulfilling the PR description to help reviewer and future developers understand.

If CI fails, you can run linting and testing checks locally according Contributing and Testing.

@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 refactors the KV transfer metadata management by introducing a RequestTransferState dataclass to encapsulate transfer-related fields, moving them out of the ReqMeta class. It also introduces a GroupBufferLayout to handle memory descriptors for KV cache groups, improving modularity. A high-severity issue was identified in GroupBufferLayout.from_buffers where an incorrect modulo operation could cause an IndexError when broadcasting block_strides. The changes include extensive updates to tests and internal modules to accommodate these structural improvements.

else ()
)
strides = (
tuple(block_strides[index % len(block_lengths)] for index in range(len(base_addresses)))

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.

high

In from_buffers, when broadcasting block_strides over base_addresses, the modulo operation uses len(block_lengths) instead of len(block_strides). If block_strides has a different length than block_lengths (for example, if it is shorter), this will raise an IndexError at runtime. To ensure robustness and correctness, the modulo should use len(block_strides).

Suggested change
tuple(block_strides[index % len(block_lengths)] for index in range(len(base_addresses)))
tuple(block_strides[index % len(block_strides)] for index in range(len(base_addresses)))

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Last updated: 2026-11-27 01:02 UTC+8 for commit c6525db.

ci-gate failed. Reason:

CODEOWNERS approval gate failed: at least one changed file has no approval from its owner(s). Open the 'CODEOWNERS' steps above to see which files are missing approvals and who can approve.
This PR touches source code (src=true, ci_pipeline=false), so precision/full tests are required before merge. Fix: a maintainer adds the 'ready-precise' label; the workflow reruns automatically once the label is added.

See the workflow run for full logs. This comment is updated in place on each new failure and marked as passed once the gate passes (it is kept for history, not deleted).

Missing owner approvals. Every changed file needs one approval from its CODEOWNERS owner(s):

@zss-hp
zss-hp force-pushed the refactor/ascend-store-metadata branch from 021416f to 9f1c2df Compare November 20, 2026 10:00
@zss-hp zss-hp closed this Nov 20, 2026
@zss-hp zss-hp reopened this Nov 20, 2026
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Total Coverage Diff Coverage
Total 158831 lines 1089 lines
Missing 45028 lines 69 lines
Coverage 72% 93%

More info see: Summary

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@zss-hp
zss-hp force-pushed the refactor/ascend-store-metadata branch from 9f1c2df to fd8d816 Compare November 21, 2026 01:37
@zss-hp zss-hp closed this Nov 21, 2026
@zss-hp zss-hp reopened this Nov 21, 2026
@zss-hp zss-hp closed this Nov 21, 2026
@zss-hp zss-hp reopened this Nov 21, 2026
@zss-hp zss-hp closed this Nov 21, 2026
@zss-hp zss-hp reopened this Nov 21, 2026
@zss-hp zss-hp closed this Nov 21, 2026
@zss-hp zss-hp closed this Nov 21, 2026
@zss-hp
zss-hp force-pushed the refactor/ascend-store-metadata branch from 43860c6 to 1389307 Compare November 21, 2026 07:24
@zss-hp zss-hp reopened this Nov 21, 2026
@zss-hp
zss-hp force-pushed the refactor/ascend-store-metadata branch from 1389307 to 64d6bdb Compare November 21, 2026 07:31
@zss-hp zss-hp closed this Nov 21, 2026
@zss-hp zss-hp reopened this Nov 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@zss-hp
zss-hp force-pushed the refactor/ascend-store-metadata branch from 64d6bdb to 373bc97 Compare November 24, 2026 02:47
@zss-hp zss-hp closed this Nov 24, 2026
@zss-hp zss-hp reopened this Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Split the AscendStore metadata implementation into single-purpose modules without changing cache behavior, based on vllm-project#20996 and merged with current main.

- metadata.py keeps scheduling and connector payloads and re-exports moved helpers for compatibility
- cache_config.py resolves topology and cache-group configuration
- pool_keys.py owns PoolKey building and block-hash serialization
- request_state.py holds RequestTracker, side-effect-free SaveDecision, and group id normalization
- token_database.py owns the chunked token database
- transfer_metadata.py consolidates the 22 per-request worker-preparation fields into RequestTransferState
- cache_layout.py registers per-group buffer layout descriptors (stride broadcast fixed per review)

Adapted to the planes data-plane refactor (vllm-project#21314): 58 legacy request/view
field accesses in planes/block_key.py and planes/gva.py migrated onto
request.transfer; prepare_layerwise_step fuses _data_plane.prepare_sessions
with the new field location; test fixtures build a real ChunkedTokenDatabase;
benchmark fixture synced with the current scheduler contract.

Also fixes the mypy assignment error carried by vllm-project#20996 and two unverified
transfer-array test assertions. Squashed to a single commit because the CI
checkout rebases the branch onto the base ref, which cannot replay a merge
commit.

Verified: full ascend_store + kv_offload suite (1114 cases) shows zero
per-test regressions against origin/main; the 7 new metadata tests pass;
ruff clean.

Signed-off-by: zhaosusu <zhaosusu1@h-partners.com>
from_tracker eagerly converted every group's block ids and gvas to
numpy arrays on the scheduler critical path; main only materializes
arrays on the worker thread. At 256 concurrent requests with 256
blocks x 5 groups this added ~13.5 ms to every scheduler step (and
~192 ms at 4096 blocks), directly inflating TTFT on prefill-heavy
workloads; an E2E benchmark on DeepSeek-V4-Flash measured ~1%
throughput/TTFT regression.

Store the tracker's id lists by reference instead and materialize
them lazily on first array access, which happens on the worker
thread (planes/gva read paths now go through get_block_ids_array).
The lazy source shares identity with ReqMeta.block_ids_by_group, so
pickling the metadata payload costs no extra bytes. Empty gvas
conversions reuse one shared empty array.

Measured: scheduler-side from_tracker drops 13.51 ms -> 0.22 ms per
step (256 req x 256 blocks x 5 groups); the one-time materialization
runs on the worker thread where main performs the same conversion.
Full ascend_store + kv_offload suite: zero per-test changes.

Signed-off-by: zhaosusu <zhaosusu1@h-partners.com>
The GVA save preparation issued one exists check and one
batch_alloc per request (per step): at 256 concurrent decode
requests that is 512 sequential store round trips, which is pure
added latency on the worker save path and grows linearly with
batch size.

Split _alloc_gvas_for_save into three phases: collect every group's
candidate keys without I/O (each group holds its slot in the
published per-group arrays so group order is preserved), issue one
merged exists check plus one merged batch_alloc for all requests
(keys and sizes concatenated in request-then-group order, so
per-request transfer ordering is preserved), then distribute the
replies back in that same order. Failure semantics are unchanged:
the offload gate still reads the raw alloc replies (a UINT64_MAX
SSD-hit sentinel filters to 0 but is not a failure), partial keys
keep their per-request alloc, and a failed batch disables the
step's saves exactly as before.

Verified with a counting store: 8 requests x 2 groups now take 1
exists + 1 alloc call instead of 16 + 16, with identical backfill
values and key order; mixed cacheable/non-cacheable groups keep
their slot placeholders. The full ascend_store + kv_offload suite
(1107 cases) shows zero per-test changes.

Signed-off-by: zhaosusu <zhaosusu1@h-partners.com>
@zss-hp
zss-hp force-pushed the refactor/ascend-store-metadata branch from 373bc97 to 5ff96d3 Compare November 26, 2026 14:49
@zss-hp zss-hp closed this Nov 26, 2026
@zss-hp zss-hp reopened this Nov 26, 2026
@zss-hp zss-hp closed this Nov 26, 2026
@zss-hp zss-hp reopened this Nov 26, 2026
@zss-hp
zss-hp force-pushed the refactor/ascend-store-metadata branch from ec0be94 to 92f78f5 Compare November 26, 2026 15:59
@zss-hp zss-hp closed this Nov 26, 2026
@zss-hp zss-hp reopened this Nov 26, 2026
The upstream save-path tests added in the latest main window lock the
per-request implementation contract: hash conversion is counted per
request until the first allocation failure stops the loop, per-request
key lists keep their nesting, and isolated requests observe their own
exists-check state. The cross-request batched collector broke those
observable timings while chasing fewer store round trips.

Restore main's per-request save allocation (with the step-scoped hash
memo and public-key dedup that main already ships) as the contract of
record; the scheduler-thread materialization deferral from the previous
commit stays, with the block-id reads going through the lazy accessor
so from_tracker-built requests still materialize on the worker thread.

Signed-off-by: zhaosusu <zhaosusu1@h-partners.com>
@zss-hp
zss-hp force-pushed the refactor/ascend-store-metadata branch from 92f78f5 to c6525db Compare November 26, 2026 16:49
@zss-hp zss-hp closed this Nov 26, 2026
@zss-hp zss-hp reopened this Nov 26, 2026

This branch has not been deployed

No deployments
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.

1 participant