[Model] Support Qwen3.8-Flash-Next (#53896)

Signed-off-by: huanghaoyan.hhy <huanghaoyan.hhy@alibaba-inc.com>
Signed-off-by: andyluo7 <andy.luo@amd.com>
Signed-off-by: lishunyang12 <lishunyang03@gmail.com>
Signed-off-by: Harry Huang <huanghaoyan.hhy@alibaba-inc.com>
Signed-off-by: Thien Tran <gau.nernst@yahoo.com.sg>
Signed-off-by: Isotr0py <Isotr0py@outlook.com>
Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com>
Signed-off-by: Yifan Qiao <yifanqiao@inferact.ai>
Signed-off-by: zjy0516 <riverclouds.zhu@qq.com>
Co-authored-by: zjy0516 <riverclouds.zhu@qq.com>
Co-authored-by: KungYork <firelikest@163.com>
Co-authored-by: Thien Tran <gau.nernst@yahoo.com.sg>
Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai>
Co-authored-by: andyluo7 <43718156+andyluo7@users.noreply.github.com>
Co-authored-by: Isotr0py <mozf@inferact.ai>
Co-authored-by: andyluo7 <andy.luo@amd.com>
Co-authored-by: OpenAI Codex <noreply@openai.com>
Co-authored-by: lishunyang12 <lishunyang03@gmail.com>
Co-authored-by: Vadim Gimpelson <156319763+vadiklyutiy@users.noreply.github.com>
Co-authored-by: Isotr0py <Isotr0py@outlook.com>
Co-authored-by: Lucas Wilkinson <lwilkins@redhat.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Harry Huang
2026-08-31 05:57:56 +00:00
committed by GitHub
co-authored by zjy0516 KungYork Thien Tran Yifan Qiao andyluo7 Isotr0py andyluo7 OpenAI Codex lishunyang12 Vadim Gimpelson Isotr0py Lucas Wilkinson Claude Opus 5 OpenAI Codex
parent 687db59744
commit e126687a9a
124 changed files with 19424 additions and 450 deletions
+15 -1
View File
@@ -26,6 +26,7 @@ class BlockTables:
cp_size: int = 1,
cp_rank: int = 0,
cp_interleave: int = 1,
slot_mapping_enabled: list[bool] | None = None,
):
self.block_sizes = block_sizes
self.kernel_block_sizes = kernel_block_sizes
@@ -39,6 +40,10 @@ class BlockTables:
self.num_kv_cache_groups = len(self.block_sizes)
assert len(max_num_blocks_per_group) == self.num_kv_cache_groups
if slot_mapping_enabled is None:
slot_mapping_enabled = [True] * self.num_kv_cache_groups
assert len(slot_mapping_enabled) == self.num_kv_cache_groups
self._slot_mapping_enabled = slot_mapping_enabled
self.blocks_per_kv_block = [
bs // kbs for bs, kbs in zip(block_sizes, kernel_block_sizes)
@@ -100,6 +105,9 @@ class BlockTables:
self.kernel_block_sizes_tensor = torch.tensor(
self.kernel_block_sizes, dtype=torch.int32, device=self.device
)
self.slot_mapping_enabled = torch.tensor(
self._slot_mapping_enabled, dtype=torch.bool, device=self.device
)
self.input_block_table_ptrs = self._make_ptr_tensor(self.input_block_tables)
def append_block_ids(
@@ -202,6 +210,7 @@ class BlockTables:
self.block_table_strides,
self.block_sizes_tensor,
self.kernel_block_sizes_tensor,
self.slot_mapping_enabled,
slot_mappings,
slot_mappings.stride(0),
self.cp_rank,
@@ -274,6 +283,7 @@ def _compute_slot_mappings_kernel(
block_table_strides, # [num_kv_cache_groups]
block_sizes, # [num_kv_cache_groups]
kernel_block_sizes, # [num_kv_cache_groups]
slot_mapping_enabled, # [num_kv_cache_groups]
slot_mappings_ptr, # [num_kv_cache_groups, max_num_tokens]
slot_mappings_stride,
cp_rank,
@@ -302,6 +312,7 @@ def _compute_slot_mappings_kernel(
block_table_stride = tl.load(block_table_strides + group_id)
kv_block_size = tl.load(block_sizes + group_id)
kernel_block_size = tl.load(kernel_block_sizes + group_id)
mapping_enabled = tl.load(slot_mapping_enabled + group_id)
req_state_idx = tl.load(idx_mapping + batch_idx)
start_idx = tl.load(query_start_loc + batch_idx)
@@ -325,7 +336,9 @@ def _compute_slot_mappings_kernel(
local_offsets = rounds * CP_INTERLEAVE + remainder
local_positions = virtual_block_indices * kv_block_size + local_offsets
block_indices = local_positions // kernel_block_size
block_indices = tl.where(
mapping_enabled, local_positions // kernel_block_size, 0
)
block_offsets = local_positions % kernel_block_size
block_numbers = tl.load(
block_table_ptr + req_state_idx * block_table_stride + block_indices,
@@ -336,4 +349,5 @@ def _compute_slot_mappings_kernel(
if CP_SIZE != 1:
slot_ids = tl.where(is_local, slot_ids, PAD_ID)
slot_ids = tl.where(mapping_enabled, slot_ids, PAD_ID)
tl.store(slot_mapping_ptr + offset, slot_ids, mask=offset < end_idx)
+17 -2
View File
@@ -64,7 +64,12 @@ from vllm.tasks import SupportedTask
from vllm.utils.mem_utils import DeviceMemoryProfiler, format_gib
from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE
from vllm.v1.core.sched.output import GrammarOutput, SchedulerOutput
from vllm.v1.kv_cache_interface import KVCacheConfig, MambaSpec
from vllm.v1.kv_cache_interface import (
CircularBufferSpec,
KVCacheConfig,
MambaSpec,
UniformTypeKVCacheSpecs,
)
from vllm.v1.outputs import (
DraftTokenIds,
ECConnectorOutput,
@@ -548,9 +553,14 @@ class GPUModelRunner(LoRAModelRunnerMixin):
block_sizes = []
max_num_blocks_per_group = []
slot_mapping_enabled = []
for kv_cache_group in kv_cache_config.kv_cache_groups:
spec = kv_cache_group.kv_cache_spec
block_sizes.append(spec.block_size)
layer_spec = (
spec.first_spec if isinstance(spec, UniformTypeKVCacheSpecs) else spec
)
slot_mapping_enabled.append(not isinstance(layer_spec, CircularBufferSpec))
# Let each cache type account for CP. Attention KV is DCP-sharded,
# while Mamba/GDN recurrent state is replicated across DCP ranks.
max_num_blocks = spec.max_num_blocks_per_req(
@@ -558,7 +568,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
)
# Preserve each cache type's alignment requirements after applying
# its topology-aware block-table width.
if isinstance(spec, MambaSpec):
if isinstance(layer_spec, (MambaSpec, CircularBufferSpec)):
max_num_blocks = get_block_table_width(
max_num_blocks, spec.block_size, token_alignment=None
)
@@ -605,6 +615,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
max_num_blocks_per_group=max_num_blocks_per_group,
device=self.device,
kernel_block_sizes=self.kernel_block_sizes,
slot_mapping_enabled=slot_mapping_enabled,
cp_size=self.dcp_size,
cp_rank=self.dcp_rank,
cp_interleave=self.cp_interleave,
@@ -1192,6 +1203,10 @@ class GPUModelRunner(LoRAModelRunnerMixin):
total_num_draft_tokens = int(num_draft_tokens_per_req.sum())
total_num_logits = num_reqs * num_bonus_tokens + total_num_draft_tokens
num_logits = num_draft_tokens_per_req + num_bonus_tokens
# combine_sampled_and_draft_tokens places a request's logits rows
# at [query_end - num_logits, query_end). Fewer query rows than
# that would silently select the preceding request's hidden states.
assert (num_scheduled_tokens_np >= num_logits).all()
cu_num_logits_np = np.empty(num_reqs + 1, dtype=np.int32)
cu_num_logits_np[0] = 0
np.cumsum(num_logits, out=cu_num_logits_np[1:])
+29 -15
View File
@@ -9,10 +9,14 @@ import torch.nn as nn
from vllm.config import VllmConfig
from vllm.config.compilation import CUDAGraphMode
from vllm.model_executor.layers.mamba.mamba_utils import MambaStateCopyFuncsByType
from vllm.triton_utils import tl, triton
from vllm.v1.attention.backends.gdn_attn import GDNAttentionMetadataBuilder
from vllm.v1.attention.backends.mamba2_attn import Mamba2AttentionMetadataBuilder
from vllm.v1.attention.backends.short_conv_attn import ShortConvAttentionMetadataBuilder
from vllm.v1.attention.backends.short_conv_attn import (
PleShortConvAttentionMetadataBuilder,
ShortConvAttentionMetadataBuilder,
)
from vllm.v1.core.sched.output import NewRequestData
from vllm.v1.kv_cache_interface import KVCacheConfig, MambaSpec
from vllm.v1.utils import CpuGpuBuffer
@@ -24,7 +28,10 @@ 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,
get_mamba_group_ids,
get_mamba_groups,
preprocess_mamba_align_fused_kernel,
validate_mamba_state_copy_funcs,
)
from vllm.v1.worker.utils import AttentionGroup
@@ -53,6 +60,7 @@ class MambaHybridAttnMetadata(ModelSpecificAttnMetadata):
Mamba2AttentionMetadataBuilder,
GDNAttentionMetadataBuilder,
ShortConvAttentionMetadataBuilder,
PleShortConvAttentionMetadataBuilder,
),
):
return {}
@@ -102,6 +110,7 @@ class MambaHybridModelState(DefaultModelState):
self._mamba_ctx: MambaSpecDecodeGPUContext | None = None
self._mamba_group_ids: list[int] = []
self._mamba_spec: MambaSpec | None = None
self._mamba_state_copy_funcs: MambaStateCopyFuncsByType | None = None
def add_request(self, req_index: int, new_req_data: NewRequestData) -> None:
super().add_request(req_index, new_req_data)
@@ -117,17 +126,16 @@ class MambaHybridModelState(DefaultModelState):
self, kv_cache_config: KVCacheConfig
) -> tuple[list[int], MambaSpec]:
if self._mamba_spec is None:
group_ids: list[int] = []
specs: list[MambaSpec] = []
for i, group in enumerate(kv_cache_config.kv_cache_groups):
spec = group.kv_cache_spec
if isinstance(spec, MambaSpec):
group_ids.append(i)
specs.append(spec)
assert specs, "no mamba layers in the model"
assert all(specs[0] == s for s in specs)
self._mamba_group_ids = group_ids
self._mamba_spec = specs[0]
mamba_groups = get_mamba_groups(kv_cache_config)
mamba_spec = next(iter(mamba_groups))
assert all(
spec.block_size == mamba_spec.block_size
and spec.num_speculative_blocks == mamba_spec.num_speculative_blocks
and spec.mamba_cache_mode == mamba_spec.mamba_cache_mode
for spec in mamba_groups
), "all mamba groups must share cache scheduling parameters"
self._mamba_group_ids = get_mamba_group_ids(mamba_groups)
self._mamba_spec = mamba_spec
return self._mamba_group_ids, self._mamba_spec
def _ensure_align_ctx(
@@ -136,8 +144,14 @@ class MambaHybridModelState(DefaultModelState):
mamba_group_ids: list[int],
block_tables: tuple[torch.Tensor, ...],
) -> MambaSpecDecodeGPUContext:
if self._mamba_state_copy_funcs is None:
mamba_groups = get_mamba_groups(kv_cache_config)
mamba_types = {spec.mamba_type for spec in mamba_groups}
copy_funcs = self.model.get_mamba_state_copy_funcs(mamba_types)
validate_mamba_state_copy_funcs(mamba_groups, copy_funcs)
self._mamba_state_copy_funcs = copy_funcs
copy_funcs = self._mamba_state_copy_funcs
if self._mamba_ctx is None:
copy_funcs = self.model.get_mamba_state_copy_func()
# Both SD and DS conv layouts support a >0 spec-decode shift: the
# fused pre-copy kernel (``_copy_mamba_state_block``) applies the
# ``token_bias = num_accepted - 1`` window shift per conv layout
@@ -146,7 +160,7 @@ class MambaHybridModelState(DefaultModelState):
self._mamba_ctx = MambaSpecDecodeGPUContext.create(
max_num_reqs=self.max_num_reqs,
kv_cache_config=kv_cache_config,
num_state_types=len(copy_funcs),
copy_funcs=copy_funcs,
device=self.device,
make_buffer=lambda n, dtype: CpuGpuBuffer(
n, dtype=dtype, device=self.device
@@ -161,7 +175,7 @@ class MambaHybridModelState(DefaultModelState):
ctx.initialize_from_forward_context(
kv_cache_config,
forward_context,
self.model.get_mamba_state_copy_func(),
copy_funcs,
[block_tables[gid] for gid in mamba_group_ids],
)
return ctx
+12 -1
View File
@@ -18,7 +18,13 @@ from vllm.v1.core.sched.output import (
NewRequestData,
SchedulerOutput,
)
from vllm.v1.kv_cache_interface import CrossAttentionSpec, KVCacheSpec, MambaSpec
from vllm.v1.kv_cache_interface import (
CircularBufferSpec,
CrossAttentionSpec,
KVCacheSpec,
MambaSpec,
UniformTypeKVCacheSpecs,
)
from vllm.v1.request import Request
from vllm.v1.worker.gpu.model_runner import GPUModelRunner
@@ -39,6 +45,11 @@ def _reserved_block_count(
`KVCacheManager.allocate_slots` reserves: the token range plus
`num_lookahead_tokens`, where the speculator writes the KV of its drafts.
"""
if isinstance(kvcache_spec, UniformTypeKVCacheSpecs):
kvcache_spec = kvcache_spec.first_spec
if isinstance(kvcache_spec, CircularBufferSpec):
# Circular caches keep one physical ring block for the request lifetime.
return 1
if isinstance(kvcache_spec, CrossAttentionSpec):
# Cross-attention blocks cover the encoder sequence only.
return cdiv(max_encoder_len, kvcache_spec.block_size)