[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
@@ -39,6 +39,7 @@ from vllm.model_executor.layers.quantization.utils.fp8_utils import (
is_deep_gemm_e8m0_used,
)
from vllm.model_executor.layers.quantization.utils.quant_utils import (
FP8_DTYPE,
QuantKey,
kFp8Dynamic128Sym,
kFp8DynamicTensorSym,
@@ -153,6 +154,19 @@ class TritonExperts(LoRAExpertsMixin, mk.FusedMoEExpertsModular):
(kFp8StaticTensorSym, kFp8StaticTensorSym),
(kFp8StaticTensorSym, kFp8DynamicTensorSym),
]
# Block-quantized FP8 with arbitrary block shape: the Triton
# kernels take the block shape as a runtime argument.
if (
weight_key is not None
and activation_key == kFp8Dynamic128Sym
and weight_key.dtype == FP8_DTYPE
and weight_key.symmetric
and weight_key.scale.static
and weight_key.scale.dtype == torch.float32
and weight_key.scale.group_shape.row > 1
and weight_key.scale.group_shape.col > 1
):
return True
return (weight_key, activation_key) in supported
@staticmethod
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import math
from enum import Enum
from typing import Any
@@ -268,6 +269,39 @@ def map_fp8_backend(runner_backend: MoEBackend) -> Fp8MoeBackend:
)
def refine_fp8_moe_block_shape(
config: FusedMoEConfig,
weight_block_size: list[int],
) -> list[int] | None:
"""
Compute a refined block shape for block-quantized FP8 MoE weights whose
checkpoint blocks cannot be sharded exactly across TP ranks.
TP shards the intermediate dim of the expert weights, so a per-shard size
that is not a multiple of the checkpoint's block size makes the
checkpoint's block scales impossible to shard exactly. When a finer block
size (>= 32) divides both the checkpoint blocks and all involved dims,
the weight scales can be refined to that granularity at load time (a
lossless upsampling, since the refined block divides the checkpoint
block). Only Triton-based kernels can consume the refined block shape:
they take it as a runtime argument, while the other backends require the
native 128x128 blocks. The refined shape is encoded in the QuantKey used
for backend selection, so backends that only support 128x128 blocks are
rejected by the oracle automatically.
Returns the refined [block_n, block_k] shape, or None if no refinement
is needed or possible.
"""
block_n, block_k = weight_block_size
ispp = config.intermediate_size_per_partition
if ispp % block_n == 0 and (config.tp_size == 1 or ispp % block_k == 0):
return None
refine = math.gcd(block_n, block_k, ispp, config.hidden_dim)
if refine < 32:
return None
return [refine, refine]
def select_fp8_moe_backend(
config: FusedMoEConfig,
weight_key: QuantKey | None,
@@ -474,6 +474,39 @@ def _get_requested_backends(
return _filter_by_activation(backends, requested_activation_key)
def _requires_qwen38_tep8_emulation(
config: FusedMoEConfig,
activation_key: QuantKey | None,
) -> bool:
"""Avoid the inaccurate native gfx950 kernel for Qwen3.8 TEP8.
Qwen3.8 Flash Next's routed experts use the distinctive
``E=512, H=2560, N=640`` W4A4 shape. With eight-way expert parallelism,
AITER operates on 64 local experts and pads ``N`` to 768. That native path
is not numerically reliable on gfx950, while OCP MX emulation preserves
model accuracy. Keep this guard exact so other MXFP4 shapes and smaller EP
configurations retain the native backend.
"""
parallel = config.moe_parallel_config
if not (
activation_key == kMxfp4Dynamic
and parallel.use_ep
and parallel.ep_size == 8
and config.num_experts == 512
and config.num_local_experts == 64
and config.hidden_dim == 2560
and config.intermediate_size == 640
):
return False
if not current_platform.is_rocm():
return False
from vllm.platforms.rocm import on_gfx950
return on_gfx950()
def select_mxfp4_moe_backend(
config: FusedMoEConfig,
activation_key: QuantKey | None = None,
@@ -532,6 +565,21 @@ def select_mxfp4_moe_backend(
assert last_error is not None
raise last_error
if _requires_qwen38_tep8_emulation(config, requested_activation_key):
backend = Mxfp4MoeBackend.EMULATION
logger.warning_once(
"Using OCP MX emulation for the Qwen3.8 Flash Next TEP8 routed "
"experts on gfx950 because the native AITER W4A4 kernel is not "
"numerically reliable for this shape. Performance will be lower."
)
return _return_or_raise(
backend,
config,
kMxfp4Static,
requested_activation_key,
activation_format,
)
# Select kernels in order of backend.
AVAILABLE_BACKENDS = _filter_by_activation(
_get_priority_backends_for_gpt_oss(), requested_activation_key
@@ -345,14 +345,13 @@ def convert_to_unquantized_kernel_format(
elif unquantized_backend == UnquantizedMoeBackend.FLASHINFER_TRTLLM:
is_act_and_mul = moe_config.is_act_and_mul
if not is_act_and_mul:
# Kernel requires intermediate_size_per_partition % 128 == 0 (BlockMajorK
# weight layout uses block_k=128). Pad along the intermediate dim when
# the model + TP split don't satisfy the constraint.
w13_weight, w2_weight, padded_intermediate = align_moe_weights_for_fi(
w13_weight, w2_weight, is_act_and_mul, min_alignment=128
)
moe_config.intermediate_size_per_partition = padded_intermediate
# Kernel requires intermediate_size_per_partition % 128 == 0 (BlockMajorK
# weight layout uses block_k=128). Pad along the intermediate dim when
# the model + TP split don't satisfy the constraint.
w13_weight, w2_weight, padded_intermediate = align_moe_weights_for_fi(
w13_weight, w2_weight, is_act_and_mul, min_alignment=128
)
moe_config.intermediate_size_per_partition = padded_intermediate
_cache_permute_indices: dict[torch.Size, torch.Tensor] = {}
w13_weight, w2_weight = convert_moe_weights_to_flashinfer_trtllm_block_layout(
@@ -832,6 +832,20 @@ class RoutedExperts(PluggableLayer):
FusedMoeWeightScaleSupported.GROUP.value,
FusedMoeWeightScaleSupported.BLOCK.value,
]:
scale_refine = getattr(self.quant_method, "weight_scale_refine", None)
if (
quant_method == FusedMoeWeightScaleSupported.BLOCK.value
and scale_refine is not None
):
# FP8 block scales are stored per (block_n, block_k) tile
# of the unsharded weight, while the TP-sharded parameters
# use a refined block grid (see Fp8MoEMethod). Upsample the
# scales to the refined grid (lossless: the refined block
# divides the checkpoint block) so per-rank slicing stays
# exact. Dim -2 is the weight's N dim, dim -1 is K.
loaded_weight = loaded_weight.repeat_interleave(
scale_refine[0], dim=-2
).repeat_interleave(scale_refine[1], dim=-1)
self._load_model_weight_or_group_weight_scale(
shard_id=shard_id,
shard_dim=shard_dim,