[Model] Support DeepSeek-V4.1-Flash (#56214)

Signed-off-by: Yongye Zhu <zyy1102000@gmail.com>
Signed-off-by: Jee Jee Li <jeejeelee@inferact.ai>
Signed-off-by: andyluo7 <andy.luo@amd.com>
Signed-off-by: zjy0516 <riverclouds.zhu@qq.com>
Co-authored-by: Jee Jee Li <jeejeelee@inferact.ai>
Co-authored-by: andyluo7 <andy.luo@amd.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
Co-authored-by: zjy0516 <riverclouds.zhu@qq.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yongye Zhu
2026-09-11 02:11:19 -07:00
committed by GitHub
co-authored by Jee Jee Li andyluo7 OpenAI Codex zjy0516 Claude Opus 5
parent e6821ceac9
commit e77daef89e
49 changed files with 3996 additions and 138 deletions
+14 -3
View File
@@ -245,9 +245,20 @@ class DeepseekV4IndexerBackend(DeepseekV32IndexerBackend):
@staticmethod
def get_supported_kernel_block_sizes() -> list[int | MultipleOf]:
# Block sizes count uncompressed tokens: C4 indexer pages hold 64 rows.
return [256]
class DeepseekV41IndexerBackend(DeepseekV4IndexerBackend):
@staticmethod
def get_name() -> str:
return "DEEPSEEK_V41_INDEXER"
@staticmethod
def get_supported_kernel_block_sizes() -> list[int | MultipleOf]:
return [64 if current_platform.is_device_capability_family(90) else 128]
@dataclass
class DeepseekV32IndexerPrefillChunkMetadata:
block_table: torch.Tensor
@@ -1239,9 +1250,9 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
)
block_table = self.indexer_decode_block_table_buffer[:rows, :cols]
seq_lens_is_buffer_view = (use_native and next_n > 1) or (
not use_native and max_decode_len > 1
)
# Flattening always returns a buffer view, including single-token
# batches. Keep its address stable across varlen graph replays.
seq_lens_is_buffer_view = not use_native or next_n > 1
# DCP: localize the now-expanded per-token global bounds to this
# rank's owned KV. Done here (after expansion) so each token's global
+107 -5
View File
@@ -11,7 +11,7 @@ import torch.nn.functional as F
import vllm.envs as envs
from vllm.compilation.breakable_cudagraph import eager_break_during_capture
from vllm.config import CUDAGraphMode
from vllm.config import CUDAGraphMode, get_current_vllm_config
from vllm.forward_context import get_forward_context
from vllm.logger import init_logger
from vllm.platforms import current_platform
@@ -819,6 +819,32 @@ def rocm_fp8_mqa_logits(
return fp8_mqa_logits_torch(q, kv, weights, cu_seqlen_ks, cu_seqlen_ke)
def _max_decode_logits_rows(num_batched_tokens: int) -> int:
"""Upper bound on decode rows the paged-MQA logits buffer can ever hold.
``rocm_fp8_paged_mqa_logits`` sizes its workspace as
``(batch_size * next_n, max_model_len)``. ``batch_size`` is bounded by
``max_num_seqs`` and ``next_n`` by ``1 + num_speculative_tokens``, which is
far tighter than ``max_num_batched_tokens`` -- 192 vs 16384 for a typical
32-seq DSpark-5 deployment. The loose bound is harmless at short contexts
but scales with ``max_model_len``, so at the model's full context it asks
for tens of TiB and the engine cannot start. Take whichever valid bound is
smaller; the workspace is locked after profiling, so it must not be under-
estimated.
"""
try:
vllm_config = get_current_vllm_config()
except Exception:
return num_batched_tokens
scheduler_config = getattr(vllm_config, "scheduler_config", None)
max_num_seqs = getattr(scheduler_config, "max_num_seqs", None)
if not max_num_seqs:
return num_batched_tokens
speculative_config = getattr(vllm_config, "speculative_config", None)
num_spec = getattr(speculative_config, "num_speculative_tokens", 0) or 0
return min(num_batched_tokens, max_num_seqs * (1 + num_spec))
def rocm_aiter_sparse_attn_indexer_fake(
hidden_states: torch.Tensor,
k_cache_prefix: LayerNameType,
@@ -835,6 +861,9 @@ def rocm_aiter_sparse_attn_indexer_fake(
topk_indices_buffer: torch.Tensor | None,
skip_k_cache_insert: bool = False,
compress_ratio: int = 1,
candidate_blocks: torch.Tensor | None = None,
candidate_block_size: int = 0,
candidate_write: bool = False,
) -> torch.Tensor:
return topk_indices_buffer
@@ -856,6 +885,9 @@ def rocm_aiter_sparse_attn_indexer(
topk_indices_buffer: torch.Tensor | None,
skip_k_cache_insert: bool = False,
compress_ratio: int = 1,
candidate_blocks: torch.Tensor | None = None,
candidate_block_size: int = 0,
candidate_write: bool = False,
) -> torch.Tensor:
# careful! this will be None in dummy run
forward_context = get_forward_context()
@@ -881,15 +913,15 @@ def rocm_aiter_sparse_attn_indexer(
)
# Decode logits buffer, used by rocm_fp8_paged_mqa_logits.
# batch_size * next_n <= hidden_states.shape[0] == max_num_batched_tokens
decode_rows = _max_decode_logits_rows(hidden_states.shape[0])
if _ON_GFX942 or _ON_GFX950:
workspace_manager.get_simultaneous(
((hidden_states.shape[0], max_model_len), torch.float32),
((decode_rows, max_model_len), torch.float32),
)
else:
workspace_manager.get_simultaneous(
(
(q_fp8.shape[1], hidden_states.shape[0], max_model_len),
(q_fp8.shape[1], decode_rows, max_model_len),
torch.float32,
),
)
@@ -919,6 +951,9 @@ def rocm_aiter_sparse_attn_indexer(
topk_indices_buffer,
skip_k_cache_insert,
compress_ratio,
candidate_blocks,
candidate_block_size,
candidate_write,
)
layer_attn_metadata = attn_metadata[k_cache_prefix]
assert isinstance(layer_attn_metadata, DeepseekV32IndexerMetadata)
@@ -974,6 +1009,30 @@ def rocm_aiter_sparse_attn_indexer(
chunk.cu_seqlen_ks,
chunk.cu_seqlen_ke,
)
if candidate_blocks is not None:
from vllm.model_executor.layers.sparse_attn_indexer import (
_apply_candidate_mask,
_select_candidate_blocks,
)
chunk_candidates = candidate_blocks[chunk.token_start : chunk.token_end]
if candidate_write:
_select_candidate_blocks(
logits,
chunk.cu_seqlen_ks,
chunk.cu_seqlen_ke,
chunk_candidates.shape[1],
candidate_block_size,
chunk_candidates,
)
else:
_apply_candidate_mask(
logits,
chunk.cu_seqlen_ks,
chunk.cu_seqlen_ke,
chunk_candidates,
candidate_block_size,
)
topk_indices = topk_indices_buffer[
chunk.token_start : chunk.token_end, :topk_tokens
]
@@ -1041,6 +1100,37 @@ def rocm_aiter_sparse_attn_indexer(
max_model_len=max_model_len,
)
if candidate_blocks is not None:
from vllm.model_executor.layers.sparse_attn_indexer import (
_apply_candidate_mask,
_select_candidate_blocks,
)
num_rows = logits.shape[0]
visible = decode_metadata.seq_lens.reshape(-1)
if visible.numel() != num_rows:
visible = visible.repeat_interleave(next_n)
visible = visible[:num_rows].to(torch.int64)
row_starts = torch.zeros_like(visible)
decode_candidates = candidate_blocks[:num_rows]
if candidate_write:
_select_candidate_blocks(
logits,
row_starts,
visible,
decode_candidates.shape[1],
candidate_block_size,
decode_candidates,
)
else:
_apply_candidate_mask(
logits,
row_starts,
visible,
decode_candidates,
candidate_block_size,
)
topk_indices = topk_indices_buffer[:num_padded_tokens, :topk_tokens]
num_rows = logits.shape[0]
@@ -1097,6 +1187,10 @@ def _decode_e8m0_scales(scale: torch.Tensor) -> torch.Tensor:
)
return _upcast_e8m0_to_fp32(scale).contiguous()
if scale.dtype == torch.uint8:
# MXFP8 parameters preserve E8M0 scales as their raw exponent bytes.
# They are biased exponents, not numeric uint8 scale values.
return torch.exp2(scale.to(torch.int16).to(torch.float32) - 127.0)
return scale.to(torch.float32)
@@ -1226,7 +1320,15 @@ def _get_cached_wo_a_bf16(
)
wo_a_scale_param = get_fp8_block_weight_scale(wo_a)
if wo_a_scale_param is not None:
if wo_a_scale_param is None:
# ModelOpt MXFP8 stores the multiplicative E8M0 scale without the
# historical ``_inv`` suffix.
wo_a_scale_param = getattr(wo_a, "weight_scale", None)
# Emulated MXFP8 kernels can replace the original one-byte weight with an
# already-dequantized BF16 tensor while retaining the scale attribute for
# metadata. Applying that retained scale again would double-dequantize the
# weight. Block scaling is only valid while the one-byte FP8 storage remains.
if wo_a_scale_param is not None and wo_a.weight.element_size() == 1:
wo_a_weight = wo_a.weight.view(n_local_groups, o_lora_rank, hidden_dim).to(
torch.float32
)
+23 -14
View File
@@ -22,6 +22,7 @@ from vllm.utils.torch_utils import get_dtype_size
from vllm.v1.kv_cache_interface import (
AttentionSpec,
ChunkedLocalAttentionSpec,
CircularBufferSpec,
FullAttentionSpec,
HiddenStateCacheSpec,
KpoolTailSpec,
@@ -2013,15 +2014,22 @@ def _get_packed_kv_cache_groups(
cdiv(len(names), n) * page for page, names in page_size_layers.items()
)
# Bytes a block must hold however the mamba buckets end up split: a mamba
# bucket can go down to one state per group, every other bucket's split is
# already fixed by the repeat pattern.
# Bytes a block must hold however the state buckets end up split: mamba,
# circular-buffer and unsplit sliding-window buckets can go down to one
# state per group, every other bucket's split is fixed by the repeat pattern.
def is_state_bucket(spec: UniformTypeKVCacheSpecs) -> bool:
if isinstance(spec.first_spec, (MambaSpec, CircularBufferSpec)):
return True
return repeats_per_group is None and isinstance(
spec.first_spec, SlidingWindowSpec
)
anchor_bytes = max(
(
widest_group_bytes(
page_size_layers,
len(spec.kv_cache_specs)
if isinstance(spec.first_spec, MambaSpec)
if is_state_bucket(spec)
else num_groups_for(spec, balanced),
)
for spec, page_size_layers, balanced in bucketed
@@ -2032,10 +2040,9 @@ def _get_packed_kv_cache_groups(
groups = []
for spec, page_size_layers, balanced in bucketed:
num_groups = num_groups_for(spec, balanced)
# `_align_hybrid_block_size` pads a mamba state up to one attention
# page, so cap a mamba group at the states a block already fits rather
# than let it widen the block.
if anchor_bytes and isinstance(spec.first_spec, MambaSpec):
# Cap a state group at the states a block already fits rather than let
# it widen the block.
if anchor_bytes and is_state_bucket(spec):
states_per_block = max(anchor_bytes // spec.first_spec.page_size_bytes, 1)
num_groups = max(
num_groups, cdiv(len(spec.kv_cache_specs), states_per_block)
@@ -2071,8 +2078,9 @@ def _is_deepseek_v4_eagle(vllm_config: VllmConfig) -> bool:
if spec_config is None or not spec_config.use_eagle():
return False
model_config = vllm_config.model_config
return (
model_config is not None and model_config.hf_config.model_type == "deepseek_v4"
return model_config is not None and model_config.hf_config.model_type in (
"deepseek_v4",
"deepseek_v41",
)
@@ -2093,9 +2101,9 @@ def _annotate_eagle_groups(
spec merging, wherever grouping happens to land. It is sufficient but
not necessary: a drafter whose spec is indistinguishable from the
target's cannot be found this way.
2. Model-scoped positional fallback for DeepseekV4, whose MTP block reuses
the target's own decoder layer and so carries no spec marker. Its draft
attention layer is always the last registered layer, so flag whichever
2. Model-scoped positional fallback for DeepseekV4/V4.1, whose MTP block
reuses the target's own decoder layer and so carries no spec marker. Its
draft attention layer is always the last registered layer, so flag whichever
group holds it. This rule is only valid where the groups partition
exactly the layers of ``kv_cache_spec``, which is true on the packed
grouping path and not in general; other callers must leave
@@ -2108,7 +2116,8 @@ def _annotate_eagle_groups(
kv_cache_spec: The kv cache spec of each attention layer, in layer
registration order. Only read by rule 2.
kv_cache_groups: Groups to annotate in place.
use_deepseek_v4_fallback: Enable rule 2 for a DeepseekV4 packed group.
use_deepseek_v4_fallback: Enable rule 2 for a DeepseekV4/V4.1 packed
group.
"""
spec_config = vllm_config.speculative_config
if spec_config is None or not spec_config.use_eagle_block_drop():
+26 -8
View File
@@ -51,7 +51,7 @@ from vllm.v1.simple_kv_offload.metadata import (
if TYPE_CHECKING:
from vllm.v1.core.kv_cache_manager import KVCacheBlocks
from vllm.v1.core.kv_cache_utils import KVCacheBlock
from vllm.v1.core.kv_cache_utils import BlockHashList, KVCacheBlock
from vllm.v1.kv_cache_interface import KVCacheConfig
from vllm.v1.request import Request
@@ -783,6 +783,18 @@ class SimpleCPUOffloadScheduler:
req_ids = list(dict.fromkeys(req_ids))
return merged_gpu_block_ids, merged_cpu_block_ids, req_ids, merged_block_meta
def _cached_gpu_block(
self, resolved_hashes: "BlockHashList", block_idx: int, group_id: int
) -> "KVCacheBlock | None":
"""Return the GPU block still cached under this group's block hash."""
if block_idx >= len(resolved_hashes):
return None
assert self._gpu_block_pool is not None
blocks = self._gpu_block_pool.get_cached_block(
resolved_hashes[block_idx], [group_id]
)
return blocks[0] if blocks else None
def _select_eager_blocks_to_store(
self,
state: StoreRequestState,
@@ -827,18 +839,25 @@ class SimpleCPUOffloadScheduler:
# the middle of the request.
group_size = self.group_block_sizes[g]
ready = min(len(group_gpu_ids), aligned_tokens // group_size)
resolved_hashes = (
resolve_block_hashes(
request.block_hashes, self.hash_block_size, group_size
)
if block_meta is not None
else None
resolved_hashes = resolve_block_hashes(
request.block_hashes, self.hash_block_size, group_size
)
curr_mm_idx = 0
secondary_mm_idx = 0
start = state.num_stored_blocks[g]
for i, gpu_block_id in enumerate(group_gpu_ids[start:ready], start=start):
gpu_block = self._gpu_block_pool.blocks[gpu_block_id]
if gpu_block.is_null:
# Sliding-window groups null pages that left the window
# before the connector sees the block table, but the
# retained prefix-cache tail stays hashed in the GPU free
# queue; store it from there.
recovered = self._cached_gpu_block(resolved_hashes, i, g)
if recovered is None:
advanced_per_group[g] += 1
continue
gpu_block = recovered
gpu_block_id = gpu_block.block_id
if (
self._classify_store_candidate(gpu_block)
is not _StoreAdmission.READY
@@ -854,7 +873,6 @@ class SimpleCPUOffloadScheduler:
if block_meta is not None:
token_start = i * group_size
token_end = token_start + group_size
assert resolved_hashes is not None
parent_hash = (
None
if i == 0
@@ -17,9 +17,9 @@ def _resolve_dspark_attention_backend(
) -> AttentionBackendEnum | None:
if draft_backend is not None:
return draft_backend
# DeepSeek-V4 draft layers share the target's KV-cache layout. Other
# DeepSeek-V4(.1) draft layers share the target's KV-cache layout. Other
# DSpark architectures may use a different attention kind.
if draft_model_config.hf_config.model_type == "deepseek_v4":
if draft_model_config.hf_config.model_type in ("deepseek_v4", "deepseek_v41"):
if target_backend is not None:
logger.info_once(
"Using the target model's %s attention backend for the "
@@ -112,7 +112,14 @@ def get_eagle3_aux_layers_from_config(
if not layer_ids:
dspark_layer_ids = getattr(hf_config, "dspark_target_layer_ids", None)
if dspark_layer_ids:
layer_ids = [i + 1 for i in dspark_layer_ids]
if getattr(hf_config, "model_type", None) == "deepseek_v41":
# v4.1 reads the attention *inputs* of its target layers, and
# the target model captures the entry stream of layer L when
# idx+1 == L, so the ids are used as-is. (v4's ids are in
# capture-after semantics and keep the +1.)
layer_ids = list(dspark_layer_ids)
else:
layer_ids = [i + 1 for i in dspark_layer_ids]
if not layer_ids:
# Dense DSpark (e.g. Qwen3) also uses different aux layer semantics.
target_layer_ids = getattr(hf_config, "target_layer_ids", None)
+38 -3
View File
@@ -813,6 +813,9 @@ class GPUModelRunner(
# Persistent buffers for CUDA graphs.
self.input_ids = self._make_buffer(self.max_num_tokens, dtype=torch.int32)
# [max_num_reqs, depth] prompt token ids preceding each request's chunk
# start, for models exposing `token_lookback_depth`. Sized after load.
self.lookback_token_ids: CpuGpuBuffer | None = None
self.positions = torch.zeros(
self.max_num_tokens, dtype=torch.int64, device=self.device
)
@@ -1065,9 +1068,36 @@ class GPUModelRunner(
)
return self._mamba_bufs
def _init_model_kwargs(self):
def _prepare_lookback_token_ids(self, num_reqs: int) -> torch.Tensor:
"""Gather, per request, the `depth` prompt token ids preceding its
first scheduled token (column j is position start - 1 - j); -1 where
the position is before the prompt or already past it. Generated
positions are left to the model: under async scheduling the CPU token
table holds placeholders for them."""
buf = self.lookback_token_ids
assert buf is not None
buf.np.fill(-1)
if num_reqs > 0:
depth = buf.np.shape[1]
starts = self.input_batch.num_computed_tokens_cpu[:num_reqs, None]
pos = starts - np.arange(1, depth + 1)
num_prompt = self.input_batch.num_prompt_tokens[:num_reqs, None]
valid = (pos >= 0) & (pos < num_prompt)
rows = np.arange(num_reqs)[:, None]
ids = self.input_batch.token_ids_cpu[rows, np.clip(pos, 0, None)]
buf.np[:num_reqs] = np.where(valid, ids, -1)
return buf.copy_to_gpu()
def _init_model_kwargs(self, num_reqs: int | None = None):
model_kwargs = dict[str, Any]()
if self.lookback_token_ids is not None:
if num_reqs is None:
num_reqs = self.input_batch.num_reqs
model_kwargs["lookback_token_ids"] = self._prepare_lookback_token_ids(
num_reqs
)
if not self.is_pooling_model:
return model_kwargs
@@ -5314,6 +5344,11 @@ class GPUModelRunner(
self.model = model_loader.load_model(
vllm_config=self.vllm_config, model_config=self.model_config
)
lookback_depth = getattr(self.model, "token_lookback_depth", 0)
if lookback_depth > 0:
self.lookback_token_ids = self._make_buffer(
self.max_num_reqs, lookback_depth, dtype=torch.int32
)
if self.lora_config:
self.model = self.load_lora_model(
self.model, self.vllm_config, self.device
@@ -6076,7 +6111,7 @@ class GPUModelRunner(
):
# Make sure padding doesn't exceed max_num_tokens
assert num_tokens_padded <= self.max_num_tokens
model_kwargs = self._init_model_kwargs()
model_kwargs = self._init_model_kwargs(num_reqs=0)
if self.supports_mm_inputs and not self.model_config.is_encoder_decoder:
input_ids, inputs_embeds = self._prepare_mm_inputs(num_tokens_padded)
@@ -6087,7 +6122,7 @@ class GPUModelRunner(
elif self.enable_prompt_embeds:
input_ids = None
inputs_embeds = self.inputs_embeds.gpu[:num_tokens_padded]
model_kwargs = self._init_model_kwargs()
model_kwargs = self._init_model_kwargs(num_reqs=0)
else:
input_ids = self.input_ids.gpu[:num_tokens_padded]
inputs_embeds = None