Repository navigation
[BugFix][KV Pool] Protect asynchronous layerwise GVA puts and loads #24532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| pr: 24532 | ||
| entries: | ||
| - category: bugfix | ||
| text: Fixed incorrect first tokens and reduced throughput on external prefix hits when serving DeepSeek-V4.1 Flash with layerwise MemCache KV pooling. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -678,6 +678,7 @@ def prepare_layerwise_step(self, metadata: AscendConnectorMetadata) -> None: | |||||
| self._dflash_save_pending = False | ||||||
| self.layerwise_retrievers: list[Any] = [] | ||||||
| self.next_layer_to_submit = 0 | ||||||
| self._gva_load_boundary: torch.npu.Event | None = None | ||||||
| reset_attention_load_gate() | ||||||
| if self.use_kvpp: | ||||||
| assert self.layer_load_finished_events is not None | ||||||
|
|
@@ -1691,6 +1692,11 @@ def submit_layer_load(layer_id: int, boundary: torch.npu.Event | None) -> bool: | |||||
| and not getattr(self, "has_shared_kv", False) | ||||||
| ): | ||||||
| load_gate = get_attention_load_gate() | ||||||
| if (gva_boundary := getattr(self, "_gva_load_boundary", None)) is not None: | ||||||
| # The receiving thread processes tasks in order. A single | ||||||
| # event on the first queued task protects the entire step. | ||||||
| boundary = gva_boundary | ||||||
| self._gva_load_boundary = None | ||||||
| recv_thread.add_request( | ||||||
| LayerLoadTask( # type: ignore[arg-type] | ||||||
| wait_for_save_layers=reuse_sources, | ||||||
|
|
@@ -1777,6 +1783,16 @@ def save_dflash_draft_kv(self, metadata: AscendConnectorMetadata) -> None: | |||||
| self.save_kv_layer(metadata) | ||||||
| self._drain_deferred_last_save() | ||||||
|
|
||||||
| def wait_for_save(self, metadata: AscendConnectorMetadata) -> None: | ||||||
| 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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In
Suggested change
|
||||||
| if 0 < self.current_layer < expected_layers: | ||||||
| # A chunk can skip its draft layers. The nominal final-layer | ||||||
| # fence then never runs, but the next step can reuse these pages. | ||||||
| self._drain_attention_transfers(drain_recv=False, full=True) | ||||||
|
|
||||||
| def _shared_read_ahead_enabled(self) -> bool: | ||||||
| # Only extend the window for the persistent full-attention pages. | ||||||
| return ( | ||||||
|
|
@@ -1790,6 +1806,16 @@ def wait_for_layer_load(self) -> None: | |||||
| return | ||||||
| assert self.layer_load_finished_events is not None | ||||||
| assert self.kv_recv_thread is not None | ||||||
| if ( | ||||||
| self.current_layer == 0 | ||||||
| and any(self.layer_load_tasks) | ||||||
| and self.use_layerwise_transfer | ||||||
| and self.kv_role in ("kv_producer", "kv_both") | ||||||
| ): | ||||||
| # 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() | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To optimize NPU performance and reduce overhead in the hot path, it is recommended to initialize the synchronization event with
Suggested change
|
||||||
| self._gva_load_boundary.record() | ||||||
| load_gate = reset_attention_load_gate() | ||||||
| try: | ||||||
| self.kv_recv_thread.raise_if_failed() | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>.yamlfollowingrelease_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
release_notes/fragments/<PR number>.yamlfollowingrelease_notes/TEMPLATE.yaml. (link)