[K3] support recoverssm for K3 (#51855)

Signed-off-by: zjy0516 <riverclouds.zhu@qq.com>
Signed-off-by: Benjamin Chislett <bchislett@nvidia.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
Co-authored-by: Benjamin Chislett <bchislett@nvidia.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Jiangyun Zhu
2026-08-17 19:34:14 +08:00
committed by GitHub
co-authored by OpenAI Codex Benjamin Chislett mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent f27ae25473
commit 70afdedc10
19 changed files with 2235 additions and 75 deletions
+37 -13
View File
@@ -20,6 +20,7 @@ from vllm.v1.worker.gpu.input_batch import InputBatch
from vllm.v1.worker.gpu.mm.encoder_cache import EncoderCache
from vllm.v1.worker.gpu.model_states.default import DefaultModelState
from vllm.v1.worker.gpu.model_states.interface import ModelSpecificAttnMetadata
from vllm.v1.worker.gpu.model_states.recoverssm import RecoverSSMState
from vllm.v1.worker.mamba_utils import (
MambaSpecDecodeGPUContext,
preprocess_mamba_align_fused_kernel,
@@ -80,6 +81,9 @@ class MambaHybridModelState(DefaultModelState):
# kernel reusing the postprocess copy machinery, so the per-step src
# 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
)
if self._align_mode:
self._mamba_state_idx_gpu = torch.zeros(
self.max_num_reqs, dtype=torch.int32, device=self.device
@@ -267,7 +271,7 @@ class MambaHybridModelState(DefaultModelState):
num_accepted_tokens=num_accepted_tokens,
num_decode_draft_tokens_cpu=num_decode_draft_tokens_cpu,
)
return build_attn_metadata(
attn_metadata = build_attn_metadata(
attn_groups=attn_groups,
num_reqs=num_reqs,
num_tokens=num_tokens,
@@ -285,6 +289,13 @@ class MambaHybridModelState(DefaultModelState):
for_cudagraph_capture=for_capture,
rswa_prefix_lens=input_batch.prompt_lens,
)
if self.recoverssm is not None:
self.recoverssm.record_step(
attn_metadata,
attn_groups,
for_capture=for_capture,
)
return attn_metadata
def postprocess_state(
self,
@@ -295,21 +306,34 @@ class MambaHybridModelState(DefaultModelState):
# Chunked prefill does not sample a token, so num_sampled can be 0.
# Mamba treats num_accepted_tokens=1 as the neutral non-spec value.
num_reqs = idx_mapping.shape[0]
if num_reqs:
if not isinstance(num_sampled, int):
# idx_mapping may contain -1 sentinels (filtered rows) under PP; the
# kernel skips them rather than scattering with a host-side gather.
_scatter_num_accepted_kernel[(num_reqs,)](
idx_mapping,
num_sampled,
self.num_accepted_tokens_gpu,
)
else:
# Fill with single value.
_fill_num_accepted_kernel[(num_reqs,)](
idx_mapping,
self.num_accepted_tokens_gpu,
max(num_sampled, 1),
)
if self.recoverssm is not None:
self.recoverssm.commit_step(
num_sampled,
idx_mapping,
state_indices=(self._mamba_state_idx_gpu if self._align_mode else None),
num_accepted_tokens=self.num_accepted_tokens_gpu,
)
if not num_reqs:
return
if not isinstance(num_sampled, int):
# idx_mapping may contain -1 sentinels (filtered rows) under PP; the
# kernel skips them rather than scattering with a host-side gather.
_scatter_num_accepted_kernel[(num_reqs,)](
idx_mapping, num_sampled, self.num_accepted_tokens_gpu
)
else:
# Fill with single value.
_fill_num_accepted_kernel[(num_reqs,)](
idx_mapping, self.num_accepted_tokens_gpu, max(num_sampled, 1)
)
# Align: save the running state to the block-aligned position when
# spec-decode acceptance leaves the sequence non-block-aligned (mirrors
# the V1 align postprocess). num_computed_tokens already holds the
@@ -0,0 +1,101 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import Any
import torch
from vllm.triton_utils import tl, triton
from vllm.v1.attention.backends.recoverssm_metadata import RecoverSSMMetadata
from vllm.v1.worker.utils import AttentionGroup
class RecoverSSMState:
"""Coordinates RecoverSSM metadata between attention and postprocessing."""
def __init__(self) -> None:
self._step: tuple[RecoverSSMMetadata, ...] | None = None
def record_step(
self,
attn_metadata: dict[str, Any],
attn_groups: list[list[AttentionGroup]],
*,
for_capture: bool,
) -> None:
if for_capture:
self._step = None
return
step: list[RecoverSSMMetadata] = []
for group_list in attn_groups:
for group in group_list:
metadata = attn_metadata[group.layer_names[0]]
if isinstance(metadata, RecoverSSMMetadata):
step.append(metadata)
self._step = tuple(step)
def commit_step(
self,
num_sampled: torch.Tensor | int,
idx_mapping: torch.Tensor,
*,
state_indices: torch.Tensor | None,
num_accepted_tokens: torch.Tensor,
) -> None:
step = self._step
self._step = None
if isinstance(num_sampled, int) or step is None:
return
for metadata in step:
postprocess_meta = metadata.commit_recoverssm_state(num_sampled)
if postprocess_meta is None:
continue
assert state_indices is not None
# RecoverSSM already restored the accepted state. Update its running
# column and reset the next-step copy bias to the neutral value.
_postprocess_recoverssm_align_kernel[(postprocess_meta.num_spec_decodes,)](
idx_mapping,
num_sampled,
postprocess_meta.request_indices,
postprocess_meta.num_computed_tokens,
state_indices,
num_accepted_tokens,
MAMBA_BLOCK_SIZE=postprocess_meta.block_size,
BLOCK_TABLE_WIDTH=postprocess_meta.block_table.shape[1],
)
@triton.heuristics(
{"HAS_REQUEST_INDICES": lambda args: args["request_indices_ptr"] is not None}
)
@triton.jit
def _postprocess_recoverssm_align_kernel(
idx_mapping_ptr,
num_sampled_ptr,
request_indices_ptr,
num_computed_ptr,
state_idx_ptr,
num_accepted_ptr,
HAS_REQUEST_INDICES: tl.constexpr,
MAMBA_BLOCK_SIZE: tl.constexpr,
BLOCK_TABLE_WIDTH: tl.constexpr,
):
spec_idx = tl.program_id(0)
batch_idx = spec_idx
if HAS_REQUEST_INDICES:
batch_idx = tl.load(request_indices_ptr + spec_idx)
req_state_idx = tl.load(idx_mapping_ptr + batch_idx)
if req_state_idx < 0:
return
num_sampled = tl.load(num_sampled_ptr + batch_idx)
num_computed = tl.load(num_computed_ptr + batch_idx)
tl.store(
state_idx_ptr + req_state_idx,
tl.minimum(
(num_computed + num_sampled) // MAMBA_BLOCK_SIZE,
BLOCK_TABLE_WIDTH - 1,
),
)
tl.store(num_accepted_ptr + req_state_idx, 1)