Skip to content

[Refactor][MRV2] Unify FullAttention drafts and GLM layerwise reuse views - #23404

Closed
recky-c wants to merge 2 commits into
vllm-project:mainfrom
recky-c:codex/glm-kv-cache-step2
Closed

recky-c wants to merge 2 commits into
vllm-project:mainfrom
recky-c:codex/glm-kv-cache-step2

Conversation

@recky-c

@recky-c recky-c commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

Part of RFC: #21053 (stacked follow-up on top of #22440).

Review tip: This Draft PR is stacked on #22440 (872effd). The Step 2 changes live in the single independent top commit ($step2Sha), touching 4 files (+145 / -11).

This PR extends GLM-5.2 / GLM-5.3 (glm_moe_dsa) unified KV cache allocation (unified_allocate_kv_cache / create_kv_cache_runtime_views) to cover two additional production paths:

  1. Standard AscendAttentionBackend runtime views (heterogeneous FullAttentionSpec speculative drafts such as DSpark / DFlash2):
    • Declare uses_contiguous_cache_components = True, wrap_kv_cache_views ( uple), and get_runtime_shapes on AscendAttentionBackend, supporting symmetric/asymmetric K/V head sizes and kernel-block splitting while guarding subclasses (e.g., AscendC8MXFPAttentionBackend) and HND layouts.
    • Keep is_c8_mxfp_kv_quant, enable_fa_quant, and padded overlay specs (page_size_padded is not None) routed safely to the legacy allocator.
  2. Single-group GLM Layerwise Buffer Reuse via unified_allocate_kv_cache:
    • LayerwiseReuseLayout already expresses slot aliasing through a single �acking_bytes allocation with layer_stride == 0 descriptors. Narrowing the legacy fallback in _uses_specialized_kv_allocator to multi-group layerwise reuse (len(kv_cache_config.kv_cache_groups) > 1) lets GLM-5.2/5.3 layerwise prefill reuse go straight through unified_allocate_kv_cache + create_kv_cache_runtime_views without ^Gllocate_cache_backing / prepare_layerwise_cache_view / _reshape_kv_cache_v2.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

  • Updated ests/ut/worker/test_glm_dsa_layerwise_cache.py to assert GLM layerwise reuse goes exclusively through unified_allocate_kv_cache (blocking legacy _allocate_kv_cache and _reshape_kv_cache_v2) while preserving end-to-end MooncakePushConnectorWorker transfer correctness.
  • Added unit tests in ests/ut/attention/test_glm_cache_views.py for GLM + FullAttentionSpec drafts (symmetric/asymmetric K/V, kernel-block splitting, byte-oracle write isolation), AscendAttentionBackend subclass/HND guards, and CacheOnlyAttentionBackend / page_size_padded fallback boundaries.

Checklist

Testing

  • Boundary / edge-case coverage: Covered asymmetric head_size_v, kernel-block splitting, subclass/HND rejection, and multi-group / padded-page fallback boundaries.

  • Regression test: Locked down GLM layerwise reuse routing and FullAttentionSpec draft byte isolation in unit tests.

  • Error / failure path coverage: Verified NotImplementedError on derived backends and ValueError on HND layout.

  • Meaningful & reliable tests: Tests assert real storage sharing, layout contiguity, and byte-exact isolation.

  • Performance impact: N/A (allocation-time refactor only).

  • vLLM main: vllm-project/vllm@c410543

gcw_DTa4OOyR added 2 commits November 26, 2026 17:14
…r GLM 5.2/5.3

Signed-off-by: gcw_DTa4OOyR <akIbL05P0REysxO0.gcw_DTa4OOyR@noreply.gitcode.com>
…iews

Signed-off-by: gcw_DTa4OOyR <akIbL05P0REysxO0.gcw_DTa4OOyR@noreply.gitcode.com>
@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.

🤖 How to trigger CI

CI runs automatically when the PR is opened or reopened. Pushing a new commit no longer re-runs the E2E CI (to save NPU runner resources), so use the comment commands below when needed. They are available to the PR author and users with triage permission or above:

  • /run — run the full PR CI gate on the latest commit.
  • /rerun — re-run the failed CI jobs (e.g. after a flaky failure).
  • /e2e — run selected E2E tests instead of the full gate.
  • /nightly / /presmoke / /weekly — run the corresponding large-scale tests (triage permission or above; blocked during the nightly maintenance window).
  • /cancel — cancel a running CI on this PR.

Note

Until the CI gate has run on a commit, the required ci-gate check stays pending and the PR cannot merge — remember to comment /run after pushing.

@github-actions

Copy link
Copy Markdown
Contributor

Last updated: 2026-11-26 17:42 UTC+8 for commit efd4a90.

ci-gate failed. Reason:

pre-commit failed (lint / markdownlint / mypy / gitleaks). Open the failed 'pre-commit' job above, fix the reported issues in your PR, and push again. No test label is needed for this failure.

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).

@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 unified KV cache allocation support for GLM models (glm_moe_dsa) by implementing contiguous cache component views and refactoring AscendMLAAttentionSpec to use num_query_heads instead of shadowing num_heads. While the changes are well-covered by updated unit tests, several issues need to be addressed: first, part.numel() should be replaced with part.nbytes in attn_utils.py to robustly calculate byte sizes across different data types; second, the strict=True argument in zip() must be removed from both source and test files to maintain compatibility with Python 3.9; and finally, the missing import of get_dtype_size must be added to indexer.py and sfa_v1.py to prevent runtime NameError exceptions.

num_kernel_blocks = num_blocks * group_spec.block_size // kernel_block_size

raw_parts = raw_cache if isinstance(raw_cache, tuple) else (raw_cache,)
total_bytes = sum(part.numel() for part in raw_parts)

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

Using part.numel() to calculate total_bytes assumes that the raw cache tensors are always of a 1-byte data type (like torch.int8). If any of the raw parts are allocated with their actual data types (e.g., torch.float16 for scales), part.numel() will return the number of elements instead of the byte size, leading to an incorrect total_bytes calculation and assertion failures.\n\nPlease use part.nbytes instead, which correctly and robustly returns the byte size of the tensor regardless of its data type.

Suggested change
total_bytes = sum(part.numel() for part in raw_parts)
total_bytes = sum(part.nbytes for part in raw_parts)

(blocks, tokens, heads, content_bytes // get_dtype_size(dtype))
for (blocks, heads, tokens, content_bytes), dtype in zip(shapes, dtypes)
(blocks, tokens, heads, *inner, content_bytes // get_dtype_size(dtype))
for (blocks, heads, tokens, *inner, content_bytes), dtype in zip(shapes, dtypes, strict=True)

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

The strict keyword argument in zip() was introduced in Python 3.10 (PEP 618). Since vLLM supports Python 3.9 (python_requires >= 3.9), using strict=True will cause a TypeError at runtime on Python 3.9 environments.\n\nPlease remove strict=True to maintain compatibility with Python 3.9. If length validation is desired, you can perform an explicit length check beforehand.

Suggested change
for (blocks, heads, tokens, *inner, content_bytes), dtype in zip(shapes, dtypes, strict=True)
for (blocks, heads, tokens, *inner, content_bytes), dtype in zip(shapes, dtypes)


kv_caches[layer_name] = tuple(
part.view(dtype).view(shape)
for part, dtype, shape in zip(raw_parts, dtypes, shapes, strict=True)

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

The strict keyword argument in zip() is a Python 3.10+ feature. To prevent runtime TypeError crashes on Python 3.9 (which is supported by vLLM), please remove strict=True.

Suggested change
for part, dtype, shape in zip(raw_parts, dtypes, shapes, strict=True)
for part, dtype, shape in zip(raw_parts, dtypes, shapes)

assert local_addrs == [key[dst_block].data_ptr(), scale[dst_block].data_ptr()]
assert remote_addrs == [prefill_key[1].data_ptr(), prefill_scale[1].data_ptr()]
assert lengths == [key[dst_block].nbytes, scale[dst_block].nbytes]
for local, source, length in zip(local_addrs, remote_addrs, lengths, strict=True):

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

The strict keyword argument in zip() is not supported in Python 3.9. Please remove strict=True to ensure the unit tests can run successfully on Python 3.9 environments.

Suggested change
for local, source, length in zip(local_addrs, remote_addrs, lengths, strict=True):
for local, source, length in zip(local_addrs, remote_addrs, lengths):

tokens = kernel_block_size or spec.block_size
blocks = num_blocks * spec.sfa_dcp_replicated_indexer_size * (spec.block_size // tokens)
prefix = (blocks, spec.num_kv_heads, tokens)
key_shape: RuntimeComponentShape = (*prefix, spec.head_size * get_dtype_size(spec.dtype))

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

The function get_dtype_size is used here but does not appear to be imported from vllm.utils.torch_utils in this file. This will cause a NameError at runtime.\n\nPlease add get_dtype_size to the imports from vllm.utils.torch_utils at the top of the file.

f"Invalid split SFA cache widths: nope={nope_dim}, rope={rope_dim}, head_size={spec.head_size}."
)
widths = (nope_dim, rope_dim)
elem_size = get_dtype_size(spec.dtype)

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

The function get_dtype_size is used here but does not appear to be imported from vllm.utils.torch_utils in this file, which will lead to a NameError at runtime.\n\nPlease add get_dtype_size to the imports from vllm.utils.torch_utils at the top of the file.

@recky-c

recky-c commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #22440 as commit 725476e.

@recky-c recky-c closed this Nov 26, 2026
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