[Model Runner V2] support qwen35 / mamba hybrid model (#35520)
Signed-off-by: zhuhaoran <zhuhaoran.zhr@alibaba-inc.com>
This commit is contained in:
@@ -18,6 +18,11 @@ def init_model_state(
|
||||
|
||||
return WhisperModelState(vllm_config, model, encoder_cache, device)
|
||||
|
||||
if vllm_config.model_config.is_hybrid:
|
||||
from vllm.v1.worker.gpu.model_states.mamba_hybrid import MambaHybridModelState
|
||||
|
||||
return MambaHybridModelState(vllm_config, model, encoder_cache, device)
|
||||
|
||||
from vllm.v1.worker.gpu.model_states.default import DefaultModelState
|
||||
|
||||
return DefaultModelState(vllm_config, model, encoder_cache, device)
|
||||
|
||||
@@ -194,5 +194,6 @@ class DefaultModelState(ModelState):
|
||||
seq_lens_cpu_upper_bound=seq_lens_cpu_upper_bound,
|
||||
dcp_local_seq_lens=input_batch.dcp_local_seq_lens,
|
||||
positions=input_batch.positions,
|
||||
for_cudagraph_capture=for_capture,
|
||||
)
|
||||
return attn_metadata
|
||||
|
||||
@@ -17,6 +17,24 @@ from vllm.v1.worker.gpu.states import RequestState
|
||||
from vllm.v1.worker.utils import AttentionGroup
|
||||
|
||||
|
||||
class ModelSpecificAttnMetadata:
|
||||
"""Base class for model-specific attention metadata."""
|
||||
|
||||
def get_extra_common_attn_kwargs(
|
||||
self,
|
||||
kv_cache_group_id: int,
|
||||
num_reqs: int,
|
||||
) -> dict[str, Any]:
|
||||
return {}
|
||||
|
||||
def get_extra_attn_kwargs(
|
||||
self,
|
||||
attn_metadata_builder: Any,
|
||||
num_reqs: int,
|
||||
) -> dict[str, Any]:
|
||||
return {}
|
||||
|
||||
|
||||
class ModelState(ABC):
|
||||
@abstractmethod
|
||||
def __init__(
|
||||
@@ -38,6 +56,13 @@ class ModelState(ABC):
|
||||
def apply_staged_writes(self) -> None:
|
||||
return None
|
||||
|
||||
def postprocess_state(
|
||||
self,
|
||||
input_batch: InputBatch,
|
||||
num_sampled: torch.Tensor,
|
||||
) -> None:
|
||||
return None
|
||||
|
||||
@abstractmethod
|
||||
def get_mm_embeddings(
|
||||
self,
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.config.compilation import CUDAGraphMode
|
||||
from vllm.v1.attention.backends.gdn_attn import GDNAttentionMetadataBuilder
|
||||
from vllm.v1.attention.backends.mamba2_attn import Mamba2AttentionMetadataBuilder
|
||||
from vllm.v1.kv_cache_interface import KVCacheConfig
|
||||
from vllm.v1.worker.gpu.attn_utils import build_attn_metadata
|
||||
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.utils import AttentionGroup
|
||||
|
||||
|
||||
@dataclass
|
||||
class MambaHybridAttnMetadata(ModelSpecificAttnMetadata):
|
||||
is_prefilling: torch.Tensor
|
||||
num_accepted_tokens: torch.Tensor | None = None
|
||||
num_decode_draft_tokens_cpu: torch.Tensor | None = None
|
||||
|
||||
def get_extra_common_attn_kwargs(
|
||||
self,
|
||||
kv_cache_group_id: int,
|
||||
num_reqs: int,
|
||||
) -> dict[str, Any]:
|
||||
return {"is_prefilling": self.is_prefilling[:num_reqs]}
|
||||
|
||||
def get_extra_attn_kwargs(
|
||||
self,
|
||||
attn_metadata_builder: Any,
|
||||
num_reqs: int,
|
||||
) -> dict[str, Any]:
|
||||
if not isinstance(
|
||||
attn_metadata_builder,
|
||||
(Mamba2AttentionMetadataBuilder, GDNAttentionMetadataBuilder),
|
||||
):
|
||||
return {}
|
||||
return {
|
||||
"num_accepted_tokens": None
|
||||
if self.num_accepted_tokens is None
|
||||
else self.num_accepted_tokens[:num_reqs],
|
||||
"num_decode_draft_tokens_cpu": None
|
||||
if self.num_decode_draft_tokens_cpu is None
|
||||
else self.num_decode_draft_tokens_cpu[:num_reqs],
|
||||
}
|
||||
|
||||
|
||||
class MambaHybridModelState(DefaultModelState):
|
||||
"""Model state for hybrid attention + Mamba / linear-attention models."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vllm_config: VllmConfig,
|
||||
model: nn.Module,
|
||||
encoder_cache: EncoderCache | None,
|
||||
device: torch.device,
|
||||
) -> None:
|
||||
super().__init__(vllm_config, model, encoder_cache, device)
|
||||
self.num_accepted_tokens_gpu = torch.ones(
|
||||
self.max_num_reqs, dtype=torch.int32, device=self.device
|
||||
)
|
||||
|
||||
def prepare_attn(
|
||||
self,
|
||||
input_batch: InputBatch,
|
||||
cudagraph_mode: CUDAGraphMode,
|
||||
block_tables: tuple[torch.Tensor, ...],
|
||||
slot_mappings: torch.Tensor,
|
||||
attn_groups: list[list[AttentionGroup]],
|
||||
kv_cache_config: KVCacheConfig,
|
||||
for_capture: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
if cudagraph_mode == CUDAGraphMode.FULL:
|
||||
num_reqs = input_batch.num_reqs_after_padding
|
||||
num_tokens = input_batch.num_tokens_after_padding
|
||||
else:
|
||||
num_reqs = input_batch.num_reqs
|
||||
num_tokens = input_batch.num_tokens
|
||||
query_start_loc_cpu = torch.from_numpy(input_batch.query_start_loc_np)
|
||||
max_query_len = input_batch.num_scheduled_tokens.max().item()
|
||||
|
||||
is_prefilling = torch.zeros(num_reqs, dtype=torch.bool, device="cpu")
|
||||
is_prefilling[: input_batch.num_reqs] = torch.from_numpy(
|
||||
input_batch.is_prefilling_np
|
||||
)
|
||||
# During CUDAGraph capture, num_decode_draft_tokens_cpu and num_accepted_tokens
|
||||
# are created by attn_metadata_builder.build_for_cudagraph_capture, so we only
|
||||
# compute them during actual (non-capture) forward execution.
|
||||
num_accepted_tokens = None
|
||||
num_decode_draft_tokens_cpu = None
|
||||
if not for_capture:
|
||||
num_accepted_tokens = self.num_accepted_tokens_gpu.new_ones(num_reqs)
|
||||
num_accepted_tokens[: input_batch.num_reqs] = self.num_accepted_tokens_gpu[
|
||||
input_batch.idx_mapping
|
||||
]
|
||||
|
||||
# GDN uses >= 0 to select spec-decode rows, so non-decode rows
|
||||
# need the -1 sentinel rather than a raw zero draft count.
|
||||
num_decode_draft_tokens_np = np.full(num_reqs, -1, dtype=np.int32)
|
||||
if input_batch.num_draft_tokens_per_req is not None:
|
||||
spec_decode_mask = (
|
||||
input_batch.num_draft_tokens_per_req > 0
|
||||
) & ~input_batch.is_prefilling_np
|
||||
num_decode_draft_tokens_np[: input_batch.num_reqs] = np.where(
|
||||
spec_decode_mask,
|
||||
input_batch.num_draft_tokens_per_req,
|
||||
-1,
|
||||
)
|
||||
num_decode_draft_tokens_cpu = torch.from_numpy(num_decode_draft_tokens_np)
|
||||
|
||||
mamba_attn_metadata = MambaHybridAttnMetadata(
|
||||
is_prefilling=is_prefilling,
|
||||
num_accepted_tokens=num_accepted_tokens,
|
||||
num_decode_draft_tokens_cpu=num_decode_draft_tokens_cpu,
|
||||
)
|
||||
return build_attn_metadata(
|
||||
attn_groups=attn_groups,
|
||||
num_reqs=num_reqs,
|
||||
num_tokens=num_tokens,
|
||||
query_start_loc_gpu=input_batch.query_start_loc,
|
||||
query_start_loc_cpu=query_start_loc_cpu,
|
||||
max_query_len=max_query_len,
|
||||
seq_lens=input_batch.seq_lens,
|
||||
max_seq_len=self.max_model_len,
|
||||
block_tables=block_tables,
|
||||
slot_mappings=slot_mappings,
|
||||
kv_cache_config=kv_cache_config,
|
||||
dcp_local_seq_lens=input_batch.dcp_local_seq_lens,
|
||||
model_specific_attn_metadata=mamba_attn_metadata,
|
||||
for_cudagraph_capture=for_capture,
|
||||
)
|
||||
|
||||
def postprocess_state(
|
||||
self,
|
||||
input_batch: InputBatch,
|
||||
num_sampled: torch.Tensor,
|
||||
) -> None:
|
||||
# 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.
|
||||
self.num_accepted_tokens_gpu[input_batch.idx_mapping] = torch.clamp(
|
||||
num_sampled, min=1
|
||||
)
|
||||
@@ -1,5 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
@@ -13,11 +14,33 @@ from vllm.v1.worker.gpu.attn_utils import build_attn_metadata
|
||||
from vllm.v1.worker.gpu.input_batch import InputBatch
|
||||
from vllm.v1.worker.gpu.mm.encoder_cache import EncoderCache
|
||||
from vllm.v1.worker.gpu.mm.encoder_runner import EncoderRunner
|
||||
from vllm.v1.worker.gpu.model_states.interface import ModelState
|
||||
from vllm.v1.worker.gpu.model_states.interface import (
|
||||
ModelSpecificAttnMetadata,
|
||||
ModelState,
|
||||
)
|
||||
from vllm.v1.worker.gpu.states import RequestState
|
||||
from vllm.v1.worker.utils import AttentionGroup
|
||||
|
||||
|
||||
@dataclass
|
||||
class WhisperAttnMetadata(ModelSpecificAttnMetadata):
|
||||
encoder_seq_lens: dict[int, tuple[torch.Tensor, np.ndarray]]
|
||||
|
||||
def get_extra_common_attn_kwargs(
|
||||
self,
|
||||
kv_cache_group_id: int,
|
||||
num_reqs: int,
|
||||
) -> dict[str, Any]:
|
||||
encoder_seq_lens = self.encoder_seq_lens.get(kv_cache_group_id)
|
||||
if encoder_seq_lens is None:
|
||||
return {}
|
||||
encoder_seq_lens_gpu, encoder_seq_lens_cpu = encoder_seq_lens
|
||||
return {
|
||||
"encoder_seq_lens": encoder_seq_lens_gpu[:num_reqs],
|
||||
"encoder_seq_lens_cpu": encoder_seq_lens_cpu[:num_reqs],
|
||||
}
|
||||
|
||||
|
||||
class WhisperModelState(ModelState):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -111,8 +134,8 @@ class WhisperModelState(ModelState):
|
||||
else:
|
||||
num_reqs = input_batch.num_reqs
|
||||
num_tokens = input_batch.num_tokens
|
||||
encoder_seq_lens = self._get_encoder_seq_lens(
|
||||
input_batch.req_ids, attn_groups, for_capture
|
||||
whisper_attn_metadata = WhisperAttnMetadata(
|
||||
self._get_encoder_seq_lens(input_batch.req_ids, attn_groups, for_capture)
|
||||
)
|
||||
|
||||
query_start_loc_cpu = torch.from_numpy(input_batch.query_start_loc_np)
|
||||
@@ -136,7 +159,8 @@ class WhisperModelState(ModelState):
|
||||
kv_cache_config=kv_cache_config,
|
||||
seq_lens_cpu_upper_bound=seq_lens_cpu_upper_bound,
|
||||
dcp_local_seq_lens=input_batch.dcp_local_seq_lens,
|
||||
encoder_seq_lens=encoder_seq_lens,
|
||||
model_specific_attn_metadata=whisper_attn_metadata,
|
||||
for_cudagraph_capture=for_capture,
|
||||
)
|
||||
return attn_metadata
|
||||
|
||||
|
||||
Reference in New Issue
Block a user