[Mamba] Share RecoverSSM commit kernels and helpers across models
RecoverSSM's commit-plan and conv-window compaction kernels are model independent. Move them from models/kimi_k3 into model_executor/layers/mamba/ops/recoverssm.py (Kimi-K3 keeps its private names as import aliases; kernel source unchanged), add mamba/recoverssm_utils.py (activation predicate, argument validation) and the generic spec-row/request mapping helpers to v1/attention/backends/recoverssm_metadata.py. CacheConfig.use_kda_recoverssm becomes use_recoverssm: the switch now selects RecoverSSM for any model whose recurrent layers implement it, not only KDA. No functional change for Kimi-K3. Signed-off-by: Jürgen Schmied <juergenschmied70@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -24,3 +24,41 @@ class RecoverSSMMetadata(abc.ABC):
|
||||
self, num_accepted_tokens: torch.Tensor
|
||||
) -> RecoverSSMPostprocessMetadata | None:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def recoverssm_spec_rows(
|
||||
is_prefilling_cpu: torch.Tensor,
|
||||
query_start_loc_cpu: torch.Tensor,
|
||||
num_decode_draft_tokens_cpu: torch.Tensor,
|
||||
num_reqs: int,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Which batch rows take the RecoverSSM spec path, shared by the RecoverSSM metadata
|
||||
builders (GDN, PLE).
|
||||
|
||||
Every active decode row does (a draft-less step is a window of one token), plus
|
||||
every row that has drafts. Only the first ``num_reqs`` rows are real; padding rows
|
||||
never take the spec path. Returns the draft-count vector to hand to the base builder
|
||||
(1 for spec rows, -1 otherwise, padding included) and the spec rows in batch order,
|
||||
which is also the order of the base builder's spec state indices."""
|
||||
query_lens = query_start_loc_cpu[: num_reqs + 1].diff()
|
||||
active_decode = (~is_prefilling_cpu[:num_reqs]) & (query_lens > 0)
|
||||
spec_mask = (num_decode_draft_tokens_cpu[:num_reqs] >= 0) | active_decode
|
||||
drafts = torch.full_like(num_decode_draft_tokens_cpu, -1)
|
||||
drafts[:num_reqs] = torch.where(spec_mask, 1, -1).to(drafts.dtype)
|
||||
return drafts, spec_mask.nonzero().flatten()
|
||||
|
||||
|
||||
def recoverssm_request_indices(
|
||||
rows: torch.Tensor, num_spec_decodes: int, device: torch.device
|
||||
) -> torch.Tensor | None:
|
||||
"""None when the spec rows are exactly 0..n-1, else their batch rows (the commit
|
||||
plan's request mapping). Raises if the base builder counted a different number of
|
||||
spec rows than the classification above."""
|
||||
if rows.numel() != num_spec_decodes:
|
||||
raise ValueError(
|
||||
f"RecoverSSM: {rows.numel()} spec rows classified, but the builder "
|
||||
f"produced {num_spec_decodes}"
|
||||
)
|
||||
if num_spec_decodes == 0 or int(rows[-1]) == num_spec_decodes - 1:
|
||||
return None
|
||||
return rows.to(torch.int32).to(device, non_blocking=True)
|
||||
|
||||
@@ -342,7 +342,7 @@ class CudaGraphManager:
|
||||
|
||||
# recoverSSM cannot capture a dummy query wider than its workspace.
|
||||
if mixed_mode and (
|
||||
not self.vllm_config.cache_config.use_kda_recoverssm
|
||||
not self.vllm_config.cache_config.use_recoverssm
|
||||
or num_tokens <= max_decode_tokens
|
||||
):
|
||||
# for PIECEWISE graphs there is no limit on requests when replaying
|
||||
|
||||
@@ -91,7 +91,7 @@ class MambaHybridModelState(DefaultModelState):
|
||||
# columns and the running state_idx are kept GPU-resident.
|
||||
self._align_mode = self.cache_config.mamba_cache_mode == "align"
|
||||
self.recoverssm = (
|
||||
RecoverSSMState() if self.cache_config.use_kda_recoverssm else None
|
||||
RecoverSSMState() if self.cache_config.use_recoverssm else None
|
||||
)
|
||||
if self._align_mode:
|
||||
self._mamba_state_idx_gpu = torch.zeros(
|
||||
|
||||
Reference in New Issue
Block a user