[Fix] dsv4: Write idle SWA stores to an idle request-window layout (#45586)

This commit is contained in:
Liangsheng Yin
2026-10-21 15:18:34 -07:00
committed by GitHub
parent aa5880e2b8
commit 99cf403c9e
2 changed files with 26 additions and 2 deletions
@@ -3948,10 +3948,15 @@ class DeepseekV4AttnBackend(
)
def get_swa_out_cache_loc(self, forward_batch: ForwardBatch) -> torch.Tensor:
"""Idle always re-translates at store time: its metadata may be stale, and
translating the zero-padded out_cache_loc writes to the dummy slot."""
"""Idle never trusts its metadata, which may be stale or unset: it writes
the request window's idle layout, or translates the zero-padded
out_cache_loc to the dummy slot."""
metadata = self.forward_metadata
if self.token_to_kv_pool.request_window is not None:
if forward_batch.forward_mode.is_idle():
return self.token_to_kv_pool.request_window.activate_idle(
forward_batch.out_cache_loc.shape[0]
)
layout = metadata.core_attn_metadata.request_window_layout
self.token_to_kv_pool.request_window.activate(layout)
return layout.write_loc
@@ -222,6 +222,7 @@ class RequestWindow:
self._ensure_workspace(workspace_rows)
self.layout = None
self.prepared = None
self._idle_layout = None
def _ensure_workspace(self, rows: int) -> None:
if self.workspace is not None and self.workspace.size >= rows:
@@ -262,6 +263,24 @@ class RequestWindow:
f"for a layout of {layout.size}"
)
def activate_idle(self, num_rows: int) -> torch.Tensor:
"""Activate a layout for an idle forward, whose attention metadata is
stale or unset. Its rows write the workspace past an empty history, and
an idle forward never commits them."""
layout = self._idle_layout
if layout is None or layout.pos.numel() != num_rows:
zeros = torch.zeros(num_rows, dtype=torch.int64, device=self.tags.device)
layout = window_layout(
zeros,
zeros,
zero_row=self.zero_row,
capacity=self.capacity,
num_groups=1,
)
self._idle_layout = layout
self.activate(layout)
return layout.write_loc
def initialize_dummy_history(self):
layout = self.layout
self.tags.fill_(-1)