Skip to content

[BugFix][KV Pool] Protect asynchronous layerwise GVA puts and loads - #24532

Open
zouyida2052 wants to merge 2 commits into
vllm-project:mainfrom
zouyida2052:fix/layerwise-gva-async-lifetime
Open

zouyida2052 wants to merge 2 commits into
vllm-project:mainfrom
zouyida2052:fix/layerwise-gva-async-lifetime

Conversation

@zouyida2052

@zouyida2052 zouyida2052 commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it?

A queued layerwise MemCache GVA put can outlive the step that built it. When no filtering or deduplication allocates a new array, the queued task holds views into the builder's scratch space. The next step can replace the HBM block IDs and pool addresses while the old task still holds the previous keys. A later external prefix hit can then restore the wrong KV.

Give each GVA put batch its own address arrays. If a chunk finishes before the final layer hook, drain submitted puts before the next step can reuse the pages. On the read side, record a compute-stream event and attach it to the first queued GVA load. The receiving thread waits on the event before copying into newly initialized KV blocks. Subsequent loads retain the existing prefetch schedule.

Does this PR introduce any user-facing change?

Yes. DeepSeek-V4.1 Flash prefill with layerwise MemCache pooling can return incorrect first tokens and lose throughput on external prefix hits. The fix requires no configuration change.

How was this patch tested?

The event-based implementation was tested on vLLM-Ascend aea4e8b48 before porting it to current main. The AscendStore unit suite on that revision reported 1317 passed, 12 skipped.

AISBench used 8 prefill and 8 decode NPUs, EPLB disabled, 131072 input tokens, 1 output token, concurrency 60, 60 prefixes, 90% prefix sharing, request rate 0, and 240 measured requests after 480 warmup requests per restart. First-token agreement is compared by request ID against the HBM reference.

Configuration Requests/s Mean TTFT Matching first tokens
HBM prefix cache, median of 3 runs 8.4048 6.40 s 720/720
Original layerwise pool, median of 3 runs 3.0386 17.59 s 11/720
Event-based fix, run 1 7.9199 6.70 s 240/240
Event-based fix, run 2 7.8846 6.73 s 240/240

Both patched runs recorded 28,293,120 externally cached tokens and zero local prefix-cache hits. The two runs averaged 94.0% of the HBM median. A third run interrupted during warmup was excluded.

On the rebased PR branch, the changed Python files passed Ruff lint, Ruff formatting and byte compilation. Local pytest collection stopped because PyTorch is unavailable in the Windows environment. The rebased branch has not been rerun on NPUs.

Checklist

Testing

  • Covered an empty step, a partial forward and a full forward.

  • Added regression coverage for queued address ownership and first-read ordering.

  • Verified that only the first queued read gets the event and that the receiver waits before H2D.

  • vLLM main: vllm-project/vllm@65ada0c

Keep queued put address maps alive, drain writes on partial forwards and order the first GVA read after KV initialization. Add regression coverage for buffer ownership and transfer ordering.

Signed-off-by: zouyida2052 <zouyida2002@gmail.com>
Signed-off-by: zouyida2052 <zouyida2002@gmail.com>
@github-actions

github-actions Bot commented Dec 3, 2026

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.


Tip

💡 Consider Linking a Related Issue or RFC

Your PR title contains the [BugFix] tag, indicating a bug fix or new feature.

Linking a related issue or RFC in the PR description is strongly encouraged — it gives reviewers helpful context and speeds up the review. You can use any of these keywords:

  • Fixes #<issue_number>
  • Closes #<issue_number>
  • Resolves #<issue_number>
  • Refs #<rfc_or_issue_number> (for RFCs)

🙏 Thanks for helping us keep the project well-organized!

@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 enhances the layerwise KV transfer mechanism by introducing GVA load boundary synchronization, managing queued GVA puts across steps, and draining attention transfers during saves. It also adds corresponding unit tests to verify these behaviors. The review feedback suggests using getattr defensively for pp_size to avoid potential AttributeError crashes, initializing the NPU synchronization event with enable_timing=False to optimize performance, and adding a release notes fragment in compliance with the repository style guide.

self.save_dflash_draft_kv(metadata)
if not self.use_layerwise_transfer or self.kv_send_thread is None:
return
expected_layers = (self.layerwise_key_layers or self.num_layers) if self.pp_size > 1 else self.num_layers

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 wait_for_save, self.pp_size is accessed directly. However, throughout the rest of the file (e.g., lines 221, 403, 404), getattr(self, "pp_size", 1) is used defensively because self.pp_size is not guaranteed to be initialized or defined on self (for instance, during partial initialization in unit tests). To prevent potential AttributeError runtime crashes, please use getattr here as well.

Suggested change
expected_layers = (self.layerwise_key_layers or self.num_layers) if self.pp_size > 1 else self.num_layers
expected_layers = (self.layerwise_key_layers or self.num_layers) if getattr(self, "pp_size", 1) > 1 else self.num_layers

):
# The native GVA receiver has its own stream. Order its first read
# after KV block initialization while the model can keep issuing work.
self._gva_load_boundary = torch.npu.Event()

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

To optimize NPU performance and reduce overhead in the hot path, it is recommended to initialize the synchronization event with enable_timing=False. This avoids the overhead of tracking elapsed time when the event is only used for stream synchronization.

Suggested change
self._gva_load_boundary = torch.npu.Event()
self._gva_load_boundary = torch.npu.Event(enable_timing=False)

worker = cast(LayerwiseKVPoolWorker, self.connector_worker)
if self.has_connector_metadata():
worker.save_dflash_draft_kv(self._get_connector_metadata())
worker.wait_for_save(self._get_connector_metadata())

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

According to the Repository Style Guide (Release notes, lines 45-47), every user-facing PR must add a release notes fragment under release_notes/fragments/<PR number>.yaml following release_notes/TEMPLATE.yaml. Since this PR introduces user-facing changes (as noted in the PR description), please ensure a release notes fragment is added.

References
  1. Every user-facing PR must add release_notes/fragments/<PR number>.yaml following release_notes/TEMPLATE.yaml. (link)

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Coverage

Total Coverage Diff Coverage
Total 167102 lines 24 lines
Missing 45280 lines 0 lines
Coverage 73% 100%

More info see: Summary

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