Repository navigation
[BugFix][KV Pool] Protect asynchronous layerwise GVA puts and loads - #24532
zouyida2052 wants to merge 2 commits into
Conversation
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>
|
👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:
If CI fails, you can run linting and testing checks locally according Contributing and Testing. 🤖 How to trigger CI
Note Until the CI gate has run on a commit, the required Tip 💡 Consider Linking a Related Issue or RFCYour 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:
🙏 Thanks for helping us keep the project well-organized! |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| 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() |
There was a problem hiding this comment.
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.
| 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()) |
There was a problem hiding this comment.
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
- Every user-facing PR must add
release_notes/fragments/<PR number>.yamlfollowingrelease_notes/TEMPLATE.yaml. (link)
Coverage
More info see: Summary |
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
aea4e8b48before 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.
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