Merge remote-tracking branch 'origin/main' into sync/lesj/qwen4exp-bugfixes

Signed-off-by: lesj0610 <lesj0610@users.noreply.github.com>
This commit is contained in:
lesj0610
2026-09-17 11:29:18 +09:00
64 changed files with 2251 additions and 715 deletions
+1
View File
@@ -717,6 +717,7 @@ class ParallelConfig:
"allgather_reducescatter",
"deepep_high_throughput",
"deepep_low_latency",
"deepep_v2",
"flashinfer_nvlink_one_sided",
"mori_high_throughput",
"mori_low_latency",
+19
View File
@@ -67,6 +67,16 @@ class SchedulerConfig:
In real usage, this should be set in `EngineArgs.create_engine_config`.
"""
max_num_active_seqs: int | None = Field(default=None, ge=1)
"""Maximum number of requests the scheduler admits into RUNNING.
``max_num_seqs`` sizes the model runner (per-request buffers and CUDA
graph capture) and is also the default admission limit. Setting this
lowers only the number of requests that may occupy RUNNING, so decode
batches stay smaller without shrinking runner or graph capacity. Must
be ``<= max_num_seqs``. ``None`` (default) keeps current behavior.
"""
long_prefill_token_threshold: int = Field(default=0, ge=0)
"""For chunked prefill, a request is considered long if the prompt is
longer than this number of tokens. 0 disables the cap (default)."""
@@ -310,6 +320,15 @@ class SchedulerConfig:
f"({self.max_num_seqs})."
)
if (
self.max_num_active_seqs is not None
and self.max_num_active_seqs > self.max_num_seqs
):
raise ValueError(
f"max_num_active_seqs ({self.max_num_active_seqs}) cannot be "
f"greater than max_num_seqs ({self.max_num_seqs})."
)
if self.max_num_batched_tokens > self.max_num_seqs * max_model_len:
logger.warning(
"max_num_batched_tokens (%d) exceeds max_num_seqs "
+13 -3
View File
@@ -1545,9 +1545,13 @@ class SpeculativeConfig:
)
)
# Use the final draft config, after any architecture overrides,
# so EP agrees with ModelConfig's expert-count validation.
self.draft_parallel_config = (
SpeculativeConfig.create_draft_parallel_config(
self.target_parallel_config, self.draft_tensor_parallel_size
self.target_parallel_config,
self.draft_tensor_parallel_size,
draft_model_config=self.draft_model_config,
)
)
@@ -1743,15 +1747,21 @@ class SpeculativeConfig:
def create_draft_parallel_config(
target_parallel_config: ParallelConfig,
speculative_draft_tensor_parallel_size: int,
draft_model_config: ModelConfig | None = None,
) -> ParallelConfig:
"""Create a parallel config for use by the draft worker.
This is mostly a copy of the target parallel config, except the tp_size.
Use the draft TP size and disable inherited EP for known dense drafts.
Without a draft model config, preserve the previous EP inheritance.
"""
enable_ep = target_parallel_config.enable_expert_parallel
if draft_model_config is not None:
enable_ep = enable_ep and draft_model_config.is_moe
draft_parallel_config = ParallelConfig(
pipeline_parallel_size=1,
tensor_parallel_size=speculative_draft_tensor_parallel_size,
enable_expert_parallel=target_parallel_config.enable_expert_parallel,
enable_expert_parallel=enable_ep,
distributed_executor_backend=target_parallel_config.distributed_executor_backend,
max_parallel_loading_workers=target_parallel_config.max_parallel_loading_workers,
disable_custom_all_reduce=target_parallel_config.disable_custom_all_reduce,
+6
View File
@@ -556,6 +556,7 @@ class EngineArgs:
max_num_scheduled_tokens: int | None = None
long_prefill_token_threshold: int = SchedulerConfig.long_prefill_token_threshold
max_num_seqs: int | None = None
max_num_active_seqs: int | None = SchedulerConfig.max_num_active_seqs
max_num_queued_reqs: int | None = None
max_num_queued_tokens: int | None = None
max_logprobs: int = ModelConfig.max_logprobs
@@ -1589,6 +1590,10 @@ class EngineArgs:
"default": None,
},
)
scheduler_group.add_argument(
"--max-num-active-seqs",
**scheduler_kwargs["max_num_active_seqs"],
)
scheduler_group.add_argument(
"--max-num-queued-reqs", **scheduler_kwargs["max_num_queued_reqs"]
)
@@ -2421,6 +2426,7 @@ class EngineArgs:
max_num_batched_tokens=self.max_num_batched_tokens,
max_num_scheduled_tokens=self.max_num_scheduled_tokens,
max_num_seqs=self.max_num_seqs,
max_num_active_seqs=self.max_num_active_seqs,
max_num_queued_reqs=self.max_num_queued_reqs,
max_num_queued_tokens=self.max_num_queued_tokens,
max_model_len=model_config.max_model_len,
@@ -276,6 +276,7 @@ def maybe_make_prepare_finalize(
num_topk=moe.experts_per_token,
use_fp8_dispatch=use_fp8_dispatch,
use_cudagraph=use_cudagraph,
sp_size=moe.moe_parallel_config.sp_size,
)
elif moe.use_mori_kernels:
@@ -284,13 +285,20 @@ def maybe_make_prepare_finalize(
# Note: We may want to use FP8 dispatch just to reduce
# data movement.
use_fp8_dispatch = (
quant_config.is_per_act_token or quant_config.is_block_quantized
quant_config.is_per_act_token
or quant_config.is_block_quantized
or quant_config.is_per_tensor
)
if use_fp8_dispatch:
# For PTPC (per token per channel) quant, scale dim is 1
# For 1x128 quant, scale dim is hidden_dim // 128
# For PTPC (per token per channel) or per-tensor quant,
# scale dim is 1. For 1x128 quant, scale dim is
# hidden_dim // 128
quant_dtype = quant_config.quant_dtype
scale_dim = 1 if quant_config.is_per_act_token else moe.hidden_dim // 128
scale_dim = (
1
if (quant_config.is_per_act_token or quant_config.is_per_tensor)
else moe.hidden_dim // 128
)
else:
# Unquantized dispatch (e.g. AITER with defer_input_quant):
# dispatch raw BF16/FP16 data, no scales needed.
@@ -17,14 +17,19 @@ from vllm.model_executor.layers.quantization.utils.ocp_mx_utils import (
)
from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape
from vllm.platforms import current_platform
from vllm.utils.import_utils import has_triton_kernels
from vllm.utils.import_utils import get_triton_kernels_version
from vllm.utils.math_utils import cdiv
logger = init_logger(__name__)
if has_triton_kernels():
_triton_kernels_version = get_triton_kernels_version()
if _triton_kernels_version is not None:
try:
from triton_kernels.matmul_ogs import PrecisionConfig
if _triton_kernels_version == "3.8":
from triton_kernels.matmul import PrecisionConfig
else:
from triton_kernels.matmul_ogs import PrecisionConfig
except (ImportError, AttributeError) as e:
logger.error(
"Failed to import Triton kernels. Please make sure your triton "
@@ -12,9 +12,6 @@ from vllm.model_executor.layers.fused_moe.config import (
FusedMoEQuantConfig,
RoutingMethodType,
)
from vllm.model_executor.layers.fused_moe.experts.aiter_mxfp4_w4a8_moe import (
patch_gating_output,
)
from vllm.model_executor.layers.quantization.utils.quant_utils import (
QuantKey,
kMxfp4Static,
@@ -26,17 +23,6 @@ __all__ = [
]
def _aiter_raw(t):
"""Unwrap the `triton_kernels.tensor.Tensor` that `_swizzle_mxfp4` returns."""
if t is None or isinstance(t, torch.Tensor):
return t
assert hasattr(t, "storage"), (
f"expected a triton_kernels wrapped tensor with a .storage attribute, "
f"got {type(t)}"
)
return t.storage.data
def _aiter_w4a16_silu_via_a8w4(
hidden_states: torch.Tensor,
w1_data,
@@ -168,11 +154,6 @@ def aiter_triton_kernel_w4a16_moe_forward(
_routing_mod.is_tdm_avail = lambda: False
aiter_routing = _routing_mod.routing
# See context in #50859.
# TODO: Remove once https://github.com/ROCm/aiter/pull/4530 is merged,
# AITER released, and AITER pin in vLLM increased from 0.1.19.
gating_output = patch_gating_output(gating_output, global_num_experts)
if score_mode is not None:
use_grouped_topk = num_expert_group is not None and num_expert_group > 1
routing_data, gather_idx, scatter_idx = aiter_routing(
@@ -201,10 +182,14 @@ def aiter_triton_kernel_w4a16_moe_forward(
assert quant_config.w1_precision is not None
assert quant_config.w2_precision is not None
w1_data = _aiter_raw(w1)
w2_data = _aiter_raw(w2)
w1_wscale = _aiter_raw(quant_config.w1_precision.weight_scale)
w2_wscale = _aiter_raw(quant_config.w2_precision.weight_scale)
w1_data = w1.storage.data
w2_data = w2.storage.data
from vllm.model_executor.layers.quantization.utils.mxfp4_utils import (
weight_mx_scale,
)
w1_wscale = weight_mx_scale(quant_config.w1_precision).storage.data
w2_wscale = weight_mx_scale(quant_config.w2_precision).storage.data
gammas = routing_data.gate_scal if routing_data else None
@@ -5,7 +5,6 @@ import torch
import vllm.model_executor.layers.fused_moe.modular_kernel as mk
from vllm._aiter_ops import rocm_aiter_ops
from vllm.forward_context import get_forward_context, is_forward_context_available
from vllm.model_executor.layers.fused_moe.activation import MoEActivation
from vllm.model_executor.layers.fused_moe.config import (
FusedMoEConfig,
@@ -25,60 +24,6 @@ __all__ = [
]
# TODO(akaratza): Remove
# _get_padding_mask and patch_gating_output after
# https://github.com/ROCm/aiter/pull/4530 is released and consumed by vLLM.
def _get_padding_mask() -> torch.Tensor | None:
"""Retrieves a boolean mask with non-padding (0) and padding (1) tokens.
`slot_mapping < 0` comes from:
slot_mapping[num_tokens_unpadded:num_tokens_padded].fill_(-1)
in gpu_model_runner.py.
Direct kernel callers do not have model-runner padding metadata, so no
mask is needed when there is no forward context.
"""
if not is_forward_context_available():
return None
forward_context = get_forward_context()
# model runner v2.
if forward_context.is_padding is not None:
return forward_context.is_padding
# model runner v1.
slot_mapping = forward_context.slot_mapping
if isinstance(slot_mapping, list):
slot_mapping_dict = slot_mapping[0]
else:
slot_mapping_dict = slot_mapping
if isinstance(slot_mapping_dict, dict):
slot_mapping_sample = next(iter(slot_mapping_dict.values()), None)
if isinstance(slot_mapping_sample, torch.Tensor):
return slot_mapping_sample < 0
else:
return None
def patch_gating_output(
gating_output: torch.Tensor, global_num_experts: int
) -> torch.Tensor:
if global_num_experts != 128:
is_padding = _get_padding_mask()
if is_padding is not None:
assert is_padding.shape[0] == gating_output.shape[0]
gating_output = gating_output.masked_fill(is_padding[:, None], 0.0)
return gating_output
def aiter_triton_kernel_w4a8_moe_forward(
hidden_states: torch.Tensor,
w1, # Tensor or triton_kernels.Tensor
@@ -112,11 +57,6 @@ def aiter_triton_kernel_w4a8_moe_forward(
_routing_mod.is_tdm_avail = lambda: False
aiter_routing = _routing_mod.routing
# See context in #50859.
# TODO: Remove once https://github.com/ROCm/aiter/pull/4530 is merged,
# AITER released, and AITER pin in vLLM increased from 0.1.19.
gating_output = patch_gating_output(gating_output, global_num_experts)
routing_data, gather_idx, scatter_idx = aiter_routing(
gating_output, topk, sm_first=not renormalize
)
@@ -191,6 +131,7 @@ def triton_kernel_fused_mxfp4_w4a8_experts(
from vllm.model_executor.layers.quantization.utils.mxfp4_utils import (
should_use_cdna4_mx_scale_swizzle,
weight_mx_scale,
)
_swizzle_mx_scale = "CDNA4_SCALE" if should_use_cdna4_mx_scale_swizzle() else None
@@ -210,7 +151,7 @@ def triton_kernel_fused_mxfp4_w4a8_experts(
hidden_states,
w1.storage.data,
None,
quant_config.w1_precision.weight_scale.storage.data,
weight_mx_scale(quant_config.w1_precision).storage.data,
quant_config.w1_precision.flex_ctx.lhs_data.scale,
quant_config.w2_precision.flex_ctx.lhs_data.scale,
quant_config.w1_bias,
@@ -230,7 +171,7 @@ def triton_kernel_fused_mxfp4_w4a8_experts(
intermediate_cache1,
w2.storage.data,
None,
quant_config.w2_precision.weight_scale.storage.data,
weight_mx_scale(quant_config.w2_precision).storage.data,
quant_config.w2_precision.flex_ctx.lhs_data.scale,
None,
quant_config.w2_bias,
@@ -26,7 +26,7 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
)
from vllm.platforms import current_platform
from vllm.triton_utils import tl, triton
from vllm.utils.import_utils import has_triton_kernels
from vllm.utils.import_utils import get_triton_kernels_version, has_triton_kernels
from ..utils import swiglu_limit_func
@@ -441,48 +441,59 @@ def _patch_legacy_routing_for_nonpow2_topk() -> None:
_routing.sort_tokens = _sort_tokens_pow2
# Two API generations of triton_kernels are supported:
# - v3.5.1 (the version bundled with vLLM): exposes `routing()` and
# `routing_from_bitmatrix()` in triton_kernels.routing; the `Bitmatrix`
# constructor takes a `scratchpad` argument.
# - v3.6.0+: removes the `routing` module in favor of a `SparseMatrix`
# based path, and adds a `dtype=BIT` kwarg to `Bitmatrix`. Used only
# when the user has triton_kernels installed system-wide at v3.6.0+.
#
# `use_legacy_triton_kernels` selects between them at import time based on
# whether `SparseMatrix` is importable.
use_legacy_triton_kernels = False
triton_kernels_version = get_triton_kernels_version()
if has_triton_kernels():
if triton_kernels_version is not None:
try:
import triton_kernels.swiglu
from triton_kernels.matmul_ogs import (
FnSpecs,
FusedActivation,
GatherIndx,
RoutingData,
ScatterIndx,
matmul_ogs,
)
from triton_kernels.tensor import (
BIT,
Bitmatrix,
)
if triton_kernels_version == "3.8":
from triton_kernels.matmul import FnSpecs, FusedActivation, matmul
from triton_kernels.matmul_details.opt_flags import (
update_opt_flags_constraints,
)
from triton_kernels.tensor import make_ragged_tensor_metadata
from vllm.platforms.rocm import on_gfx950
if on_gfx950():
# block_n=64/block_k=256 fit gfx950's 160KB LDS + the CDNA4
# scale swizzle; block_m=32 avoids an autotuner block_m=128
# padding cliff (~15x) at decode-sized ragged MoE shapes.
update_opt_flags_constraints(
{"block_m": 32, "block_n": 64, "block_k": 256}
)
else:
from triton_kernels.matmul_ogs import (
FnSpecs,
FusedActivation,
GatherIndx,
RoutingData,
ScatterIndx,
matmul_ogs,
)
if triton_kernels_version == "3.6":
from triton_kernels.tensor import (
SparseMatrix,
make_ragged_tensor_metadata,
)
# BIT/Bitmatrix exist in v3.5.1/v3.6, gone in v3.8.
try:
from triton_kernels.tensor import (
SparseMatrix,
make_ragged_tensor_metadata,
BIT,
Bitmatrix,
)
except ImportError:
# TODO(mgoin): drop the v3.5.1 pin and remove this fallback once
# the gpt-oss perf regression in v3.6.0+ is resolved upstream.
# Tracking: https://github.com/triton-lang/triton/issues/9969
use_legacy_triton_kernels = True
if not use_legacy_triton_kernels:
BIT = None
Bitmatrix = None
# Patch the routing kernels for non-pow2 top_k (DeepSeek-V4 top_k=6);
# v3.8 rebuilds routing from topk ids and needs no patch.
if triton_kernels_version == "3.6":
_patch_make_bitmatrix_metadata()
else:
# Legacy routing fails to compile for non-pow2 top_k (DeepSeek-V4).
elif triton_kernels_version == "3.5.1":
_patch_legacy_routing_for_nonpow2_topk()
except (AttributeError, ImportError) as e:
logger.error(
@@ -492,6 +503,21 @@ if has_triton_kernels():
)
if triton_kernels_version == "3.8":
# 3.8's matmul dropped matmul_ogs's in-kernel scatter+topk-reduce, so the
# forward combines externally via index_add; this is the ragged routing
# bundle the (inlined) 3.8 paths pass around, in place of matmul_ogs's.
class RoutingData: # type: ignore[no-redef]
__slots__ = ("ragged", "gate_scal", "gather_tok", "n_expts_act", "n_valid")
def __init__(self, ragged, gate_scal, gather_tok, n_expts_act, n_valid):
self.ragged = ragged
self.gate_scal = gate_scal
self.gather_tok = gather_tok
self.n_expts_act = n_expts_act
self.n_valid = n_valid
@triton.jit
def pack_bitmatrix(
bitmatrix,
@@ -562,7 +588,7 @@ def triton_kernel_moe_forward(
# three separate kernels used by the generic path below.
# Only available in the legacy (v3.5.1) API; the v3.6.0+ path inlines
# equivalent logic via SparseMatrix in `make_routing_data`.
if use_legacy_triton_kernels and expert_map is None:
if triton_kernels_version == "3.5.1" and expert_map is None:
from triton_kernels.routing import routing as fused_routing
routing_data, gather_idx, scatter_idx = fused_routing(
@@ -662,6 +688,52 @@ def triton_kernel_fused_experts(
assert activation == MoEActivation.SWIGLUOAI, (
"Only SWIGLUOAI activation is supported"
)
if triton_kernels_version == "3.8":
# 3.8 has no in-kernel scatter+topk-reduce, so the two matmuls are
# followed by an external index_add combine.
assert quant_config is not None
M, K = hidden_states.shape[-2:]
a_ragged = routing_data.ragged
gather_tok = routing_data.gather_tok
gammas = routing_data.gate_scal
act = FusedActivation(
FnSpecs(
"swiglu",
triton_kernels.swiglu.swiglu_fn,
("alpha", "limit"),
reduction_n=2,
),
(swiglu_alpha, swiglu_limit),
)
inter = matmul(
hidden_states,
w1,
quant_config.w1_bias,
a_ragged,
None,
gather_tok,
None,
quant_config.w1_precision,
gammas=gammas if apply_router_weight_on_input else None,
fused_activation=act,
)
y = matmul(
inter,
w2,
quant_config.w2_bias,
a_ragged,
None,
None,
None,
quant_config.w2_precision,
gammas=None if apply_router_weight_on_input else gammas,
)
acc = torch.zeros((M, K), dtype=torch.float32, device=hidden_states.device)
acc.index_add_(0, gather_tok.to(torch.int64), y.to(torch.float32))
out = output_tensor.view(M, K)
out.copy_(acc.to(out.dtype))
return out
assert quant_config is not None
# type check, uint8 means mxfp4
@@ -704,7 +776,7 @@ def triton_kernel_fused_experts(
),
(swiglu_alpha, swiglu_limit),
)
if not use_legacy_triton_kernels
if triton_kernels_version != "3.5.1"
else FusedActivation(
FnSpecs("swiglu", triton_kernels.swiglu.swiglu_fn, ("alpha", "limit")),
(swiglu_alpha, swiglu_limit),
@@ -744,6 +816,24 @@ def make_routing_data(
topk_weights: torch.Tensor,
num_local_experts: int,
) -> tuple["RoutingData", torch.Tensor, torch.Tensor]:
if triton_kernels_version == "3.8":
_, topk = topk_ids.shape
flat_e = topk_ids.reshape(-1)
valid = flat_e >= 0
flat_e = flat_e.to(torch.int64).clamp(min=0)
flat_w = topk_weights.reshape(-1).to(torch.float32) * valid.to(torch.float32)
nrows = flat_e.shape[0]
order = torch.argsort(flat_e, stable=True)
col_sum = torch.zeros(
num_local_experts, dtype=torch.int32, device=flat_e.device
)
col_sum.scatter_add_(0, flat_e, torch.ones_like(flat_e, dtype=torch.int32))
ragged = make_ragged_tensor_metadata(col_sum, nrows)
gather_tok = (order // topk).to(torch.int32)
gammas = flat_w[order]
routing = RoutingData(ragged, gammas, gather_tok, topk, nrows)
return routing, gather_tok, gather_tok
topk_ids = topk_ids.to(torch.int16)
topk_weights = topk_weights.to(torch.bfloat16)
@@ -774,7 +864,7 @@ def make_routing_data(
Bitmatrix(
bitmatrix, dtype=BIT, shape=bitmatrix_shape, shape_max=bitmatrix_shape_max
)
if not use_legacy_triton_kernels
if triton_kernels_version != "3.5.1"
else Bitmatrix(
bitmatrix,
shape=bitmatrix_shape,
@@ -786,7 +876,7 @@ def make_routing_data(
# matmul_ogs expects invalid topk_weights to be -1s
topk_weights = torch.where(topk_ids == -1, -1.0, topk_weights)
if use_legacy_triton_kernels:
if triton_kernels_version == "3.5.1":
from triton_kernels.routing import routing_from_bitmatrix
return routing_from_bitmatrix(
@@ -809,6 +899,10 @@ def routing_data_from_sparse_topk(
call already produced; when the topk ids are used as-is (no expert-map
remapping) that work is redundant.
"""
if triton_kernels_version == "3.8":
# 3.8 routing is rebuilt from raw ids; no SparseMatrix reuse.
return make_routing_data(sparse_topk.indx, sparse_topk.vals, n_expts)
dispatch_indx = sparse_topk.mask_metadata.row_sorted_indx
combine_indx = sparse_topk.mask_metadata.col_sorted_indx
ragged_batch_metadata = make_ragged_tensor_metadata(
@@ -1197,6 +1291,50 @@ class UnfusedOAITritonExperts(LoRAExpertsMixin, BaseOAITritonExperts):
topk = topk_ids.size(1)
if triton_kernels_version == "3.8":
# Activation and topk-reduce kept unfused from the matmuls (3.8 has
# no in-kernel scatter+reduce); combine via external index_add.
assert self._lora_context is None, (
"tk38 unfused MoE path does not support LoRA yet"
)
act_out_dim = self.adjust_N_for_activation(w1.shape[2], activation)
M, K = hidden_states.shape[-2:]
a_ragged = routing_data.ragged
gather_tok = routing_data.gather_tok
gammas = routing_data.gate_scal
inter = matmul(
hidden_states,
w1,
quant_config.w1_bias,
a_ragged,
None,
gather_tok,
None,
quant_config.w1_precision,
gammas=gammas if apply_router_weight_on_input else None,
)
act_out = torch.empty(
(routing_data.n_valid, act_out_dim),
dtype=hidden_states.dtype,
device=hidden_states.device,
)
self.activation(activation, act_out, inter)
down = matmul(
act_out,
w2,
quant_config.w2_bias,
a_ragged,
None,
None,
None,
quant_config.w2_precision,
gammas=None if apply_router_weight_on_input else gammas,
)
acc = torch.zeros((M, K), dtype=torch.float32, device=hidden_states.device)
acc.index_add_(0, gather_tok.to(torch.int64), down.to(torch.float32))
output.view(M, K).copy_(acc.to(output.dtype))
return
# type check, uint8 means mxfp4
assert hidden_states.dtype == torch.bfloat16
assert (
@@ -45,7 +45,7 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
)
from vllm.model_executor.layers.quantization.utils.w8a8_utils import all_close_1d
from vllm.platforms import current_platform
from vllm.utils.import_utils import has_triton_kernels
from vllm.utils.import_utils import get_triton_kernels_version
from vllm.utils.math_utils import round_up
if TYPE_CHECKING:
@@ -54,9 +54,14 @@ if TYPE_CHECKING:
logger = init_logger(__name__)
if has_triton_kernels():
triton_kernels_version = get_triton_kernels_version()
if triton_kernels_version is not None:
try:
from triton_kernels.matmul_ogs import PrecisionConfig
if triton_kernels_version == "3.8":
from triton_kernels.matmul import FlexCtx, PrecisionConfig
else:
from triton_kernels.matmul_ogs import FlexCtx, PrecisionConfig
except (ImportError, AttributeError) as e:
logger.error(
"Failed to import Triton kernels. Please make sure your triton "
@@ -65,6 +70,15 @@ if has_triton_kernels():
)
def _mx_scale_kwargs(scale):
"""PrecisionConfig weight-scale kwargs: 3.8 uses b_mx_scale/b_microblock_size,
3.5.1/3.6 use weight_scale.
"""
if triton_kernels_version == "3.8":
return {"b_mx_scale": scale, "b_microblock_size": 32}
return {"weight_scale": scale}
def _pack_deepgemm_mxfp4_scales(
w13_weight: torch.Tensor,
w2_weight: torch.Tensor,
@@ -1182,7 +1196,6 @@ def convert_gpt_oss_weight_to_mxfp4_moe_kernel_format(
elif mxfp4_backend == Mxfp4MoeBackend.AITER_MXFP4_FP8:
# W4A8: MXFP4 weights + static FP8 activations (triton kernel)
from triton_kernels.matmul_ogs import FlexCtx, PrecisionConfig
from triton_kernels.numerics import InFlexData
if w13_bias is not None:
@@ -1217,11 +1230,11 @@ def convert_gpt_oss_weight_to_mxfp4_moe_kernel_format(
# Create PrecisionConfig with both weight and activation info
w13_precision_config = PrecisionConfig(
weight_scale=w13_scale,
**_mx_scale_kwargs(w13_scale),
flex_ctx=FlexCtx(rhs_data=w13_flex, lhs_data=lhs_data13),
)
w2_precision_config = PrecisionConfig(
weight_scale=w2_scale,
**_mx_scale_kwargs(w2_scale),
flex_ctx=FlexCtx(rhs_data=w2_flex, lhs_data=lhs_data2),
)
@@ -1238,8 +1251,6 @@ def convert_gpt_oss_weight_to_mxfp4_moe_kernel_format(
)
elif mxfp4_backend in TRITON_BACKENDS:
from triton_kernels.matmul_ogs import FlexCtx, PrecisionConfig
if w13_bias is not None:
w13_bias = w13_bias.to(torch.float32)
if w2_bias is not None:
@@ -1255,10 +1266,10 @@ def convert_gpt_oss_weight_to_mxfp4_moe_kernel_format(
)
w13_precision_config = PrecisionConfig(
weight_scale=w13_scale, flex_ctx=FlexCtx(rhs_data=w13_flex)
**_mx_scale_kwargs(w13_scale), flex_ctx=FlexCtx(rhs_data=w13_flex)
)
w2_precision_config = PrecisionConfig(
weight_scale=w2_scale, flex_ctx=FlexCtx(rhs_data=w2_flex)
**_mx_scale_kwargs(w2_scale), flex_ctx=FlexCtx(rhs_data=w2_flex)
)
# The original mxfp4 block scales have been swizzled into the
@@ -1653,8 +1664,6 @@ def convert_weight_to_mxfp4_moe_kernel_format(
elif mxfp4_backend in TRITON_BACKENDS or (
mxfp4_backend == Mxfp4MoeBackend.AITER_MXFP4_BF16 and is_gfx1250
):
from triton_kernels.matmul_ogs import FlexCtx, PrecisionConfig
if mxfp4_backend == Mxfp4MoeBackend.AITER_TRITON_MXFP4_BF16:
# AITER moe_gemm_a16w4 needs gate/up interleaved
def interleave_gate_up(w: torch.Tensor) -> torch.Tensor:
@@ -1698,10 +1707,10 @@ def convert_weight_to_mxfp4_moe_kernel_format(
)
w13_precision_config = PrecisionConfig(
weight_scale=w13_scale, flex_ctx=FlexCtx(rhs_data=w13_flex)
**_mx_scale_kwargs(w13_scale), flex_ctx=FlexCtx(rhs_data=w13_flex)
)
w2_precision_config = PrecisionConfig(
weight_scale=w2_scale, flex_ctx=FlexCtx(rhs_data=w2_flex)
**_mx_scale_kwargs(w2_scale), flex_ctx=FlexCtx(rhs_data=w2_flex)
)
# The original mxfp4 block scales have been swizzled into the
@@ -18,7 +18,7 @@ from vllm.model_executor.layers.quantization.utils.mxfp8_utils import (
swizzle_mxfp8_scale,
)
from vllm.triton_utils import tl, triton
from vllm.utils.math_utils import round_up
from vllm.utils.math_utils import cdiv, round_up
from vllm.v1.worker.ubatching import (
dbo_current_ubatch_id,
dbo_enabled,
@@ -114,6 +114,7 @@ class DeepEPV2PrepareAndFinalize(mk.FusedMoEPrepareAndFinalizeModular):
num_topk: int,
use_fp8_dispatch: bool = False,
use_cudagraph: bool = False,
sp_size: int = 1,
):
super().__init__()
self.buffer = buffer
@@ -124,6 +125,7 @@ class DeepEPV2PrepareAndFinalize(mk.FusedMoEPrepareAndFinalizeModular):
self.num_topk = num_topk
self.use_fp8_dispatch = use_fp8_dispatch
self.use_cudagraph = use_cudagraph
self.sp_size = sp_size
# DBO microbatching: one handle slot per micro-batch.
self.handles: list[deep_ep.EPHandle | None] = [None, None]
@@ -192,12 +194,14 @@ class DeepEPV2PrepareAndFinalize(mk.FusedMoEPrepareAndFinalizeModular):
# high concurrency). Round up to a power of 2 instead: this bounds the
# set to ~log2(max_num_batched_tokens) values (compiled once, then
# cached) while staying small for decode (e.g. 1 token -> 1) and capped
# at the buffer's init capacity for prefill.
# at the buffer's init capacity for prefill. With sequence parallelism,
# each EP rank holds a ceil(n / sp_size) shard of its DP rank's batch.
num_max_tokens_per_rank = None
if not do_expand:
dp_meta = get_forward_context().dp_metadata
if dp_meta is not None:
n = int(dp_meta.num_tokens_across_dp_cpu.max())
n = cdiv(n, self.sp_size)
else:
n = tokens.shape[0]
num_max_tokens_per_rank = 1 << max(n - 1, 0).bit_length()
@@ -82,6 +82,15 @@ class MoriPrepareAndFinalize(mk.FusedMoEPrepareAndFinalizeModular):
elif quant_config.is_per_act_token:
quant_func = get_hip_quant(QuantType.per_Token)
a1, scale = quant_func(a1, quant_dtype=current_platform.fp8_dtype())
elif quant_config.is_per_tensor:
quant_func = get_hip_quant(QuantType.per_Tensor)
a1, scale = quant_func(
a1,
scale=quant_config.a1_scale,
quant_dtype=current_platform.fp8_dtype(),
)
# mori expects one scale slot per token; broadcast.
scale = scale.expand(a1.shape[0], 1).contiguous()
(
dispatch_a1,
@@ -7,7 +7,7 @@ import torch
from vllm.logger import init_logger
from vllm.platforms import current_platform
from vllm.triton_utils import tl, triton
from vllm.utils.import_utils import has_triton_kernels
from vllm.utils.import_utils import get_triton_kernels_version, has_triton_kernels
from vllm.utils.torch_utils import direct_register_custom_op, is_torch_equal_or_newer
logger = init_logger(__name__)
@@ -27,10 +27,23 @@ def should_use_cdna4_mx_scale_swizzle() -> bool:
return on_gfx950() and get_tensor_model_parallel_world_size() <= 2
def weight_mx_scale(precision_config):
"""mxfp4 weight scale from a triton_kernels PrecisionConfig: 3.8 exposes it
as b_mx_scale, 3.5.1/3.6 as weight_scale.
"""
if get_triton_kernels_version() == "3.8":
return precision_config.b_mx_scale
return precision_config.weight_scale
def _swizzle_mxfp4(quant_tensor, scale, num_warps=8):
"""Weight swizzle for mxfp4 moe, used for OAI mxfp4 kernel."""
assert has_triton_kernels()
import triton_kernels.matmul_ogs_details.opt_flags as opt_flags
if get_triton_kernels_version() == "3.8":
# 3.8: matmul_ogs_details -> matmul_details
import triton_kernels.matmul_details.opt_flags as opt_flags
else:
import triton_kernels.matmul_ogs_details.opt_flags as opt_flags
from triton_kernels.numerics import InFlexData
from triton_kernels.tensor import FP4, convert_layout, wrap_torch_tensor
from triton_kernels.tensor_details import layout
@@ -51,6 +64,17 @@ def _swizzle_mxfp4(quant_tensor, scale, num_warps=8):
)
value_layout = StridedLayout
scale_layout = StridedLayout
elif current_platform.is_rocm() and get_triton_kernels_version() == "3.8":
# StridedLayout(-2) is triton_kernels' default CDNA mxfp4 weight layout
# (make_default_matmul_mxfp4_w_layout); 3.8 wants layout instances.
from triton_kernels.tensor_details.layout import CDNA4MXScaleLayout
value_layout = StridedLayout(-2)
scale_layout = (
CDNA4MXScaleLayout()
if should_use_cdna4_mx_scale_swizzle()
else StridedLayout()
)
elif current_platform.is_rocm():
value_layout = StridedLayout
if should_use_cdna4_mx_scale_swizzle():
+20
View File
@@ -527,6 +527,26 @@ def has_triton_kernels() -> bool:
return is_available
@cache
def get_triton_kernels_version() -> str | None:
"""The triton_kernels MoE-API generation ("3.5.1"/"3.6"/"3.8"), or None.
Inferred by capability since the package exposes no usable version: 3.8
replaced ``matmul_ogs`` with ``matmul``, and 3.5.1 predates ``SparseMatrix``.
"""
if not has_triton_kernels():
return None
try:
import triton_kernels.matmul_ogs # noqa: F401
except ImportError:
return "3.8"
try:
from triton_kernels.tensor import SparseMatrix # noqa: F401
except ImportError:
return "3.5.1"
return "3.6"
@cache
def has_tilelang() -> bool:
"""Whether the optional `tilelang` package is available.
+26 -15
View File
@@ -989,6 +989,20 @@ class AiterFlashAttentionImpl(AttentionImpl):
"cross-attention."
)
def _get_kv_cache_descales(
self,
layer: AttentionLayer,
num_decodes: int,
) -> tuple[torch.Tensor | None, torch.Tensor | None]:
# AITER treats non-null descales as an instruction to dequantize.
if not is_quantized_kv_cache(self.kv_cache_dtype):
return None, None
descale_shape = (num_decodes, self.num_kv_heads)
return (
layer._k_scale.expand(descale_shape),
layer._v_scale.expand(descale_shape),
)
def extend_for_sliding_window(
self,
attn_metadata: AiterFlashAttentionMetadata,
@@ -1367,6 +1381,9 @@ class AiterFlashAttentionImpl(AttentionImpl):
or decode_max_query_len > 1
or self.sinks is not None
):
k_descale, v_descale = self._get_kv_cache_descales(
layer, num_decodes
)
assert not rocm_aiter_ops.is_shuffle_kv_cache_enabled(), (
"Shuffle KV cache layout is not supported with sliding "
"window, sinks, or speculative decoding (multi-token decode)."
@@ -1376,7 +1393,6 @@ class AiterFlashAttentionImpl(AttentionImpl):
flash_attn_with_kvcache,
)
descale_shape = (num_decodes, key_cache.shape[2])
decode_query = query[:num_decode_tokens].reshape(
num_decodes,
decode_max_query_len,
@@ -1393,8 +1409,8 @@ class AiterFlashAttentionImpl(AttentionImpl):
window_size=self.sliding_window,
softcap=self.logits_soft_cap,
q_descale=None,
k_descale=layer._k_scale.expand(descale_shape),
v_descale=layer._v_scale.expand(descale_shape),
k_descale=k_descale,
v_descale=v_descale,
page_table=attn_metadata.block_table[:num_decodes],
)
output[:num_decode_tokens].copy_(
@@ -1412,10 +1428,6 @@ class AiterFlashAttentionImpl(AttentionImpl):
unified_attention,
)
descale_shape = (
num_decodes,
key_cache.shape[2],
)
unified_attention(
q=query[:num_decode_tokens],
k=key_cache,
@@ -1434,8 +1446,8 @@ class AiterFlashAttentionImpl(AttentionImpl):
block_table=attn_metadata.block_table[:num_decodes],
softcap=self.logits_soft_cap,
q_descale=None,
k_descale=layer._k_scale.expand(descale_shape),
v_descale=layer._v_scale.expand(descale_shape),
k_descale=k_descale,
v_descale=v_descale,
sinks=self.sinks,
)
return
@@ -1449,6 +1461,9 @@ class AiterFlashAttentionImpl(AttentionImpl):
use_unified_attention = self.head_size < _MIN_HEAD_SIZE_FOR_LL4MI
if use_unified_attention:
k_descale, v_descale = self._get_kv_cache_descales(
layer, num_decodes
)
assert not rocm_aiter_ops.is_shuffle_kv_cache_enabled(), (
"unified_attention fallback with shuffle layout "
"is not supported yet."
@@ -1460,10 +1475,6 @@ class AiterFlashAttentionImpl(AttentionImpl):
decode_cu_seqlens_q = attn_metadata.query_start_loc[
: num_decodes + 1
]
descale_shape = (
num_decodes,
key_cache.shape[2],
)
unified_attention(
q=query[:num_decode_tokens],
k=key_cache,
@@ -1480,8 +1491,8 @@ class AiterFlashAttentionImpl(AttentionImpl):
block_table=attn_metadata.block_table[:num_decodes],
softcap=self.logits_soft_cap,
q_descale=None,
k_descale=layer._k_scale.expand(descale_shape),
v_descale=layer._v_scale.expand(descale_shape),
k_descale=k_descale,
v_descale=v_descale,
)
elif rocm_aiter_ops.is_shuffle_kv_cache_enabled():
_, num_heads, head_size = query.shape
+8 -1
View File
@@ -123,6 +123,13 @@ class Scheduler(SchedulerInterface):
# Scheduling constraints.
self.max_num_running_reqs = self.scheduler_config.max_num_seqs
# Admission into RUNNING. Separate from max_num_running_reqs, which
# is the model-runner slot count.
self.max_num_active_reqs = (
self.scheduler_config.max_num_active_seqs
if self.scheduler_config.max_num_active_seqs is not None
else self.max_num_running_reqs
)
self.max_num_scheduled_tokens = (
self.scheduler_config.max_num_scheduled_tokens
if self.scheduler_config.max_num_scheduled_tokens is not None
@@ -850,7 +857,7 @@ class Scheduler(SchedulerInterface):
# Paused streaming sessions (WAITING_FOR_STREAMING_REQ) are not
# in `running` but still hold a model-runner request slot.
num_running = len(self.running) + self.num_waiting_for_streaming_input
if num_running >= self.max_num_running_reqs:
if num_running >= self.max_num_active_reqs:
break
request_queue = self._select_waiting_queue_for_scheduling()
+4 -3
View File
@@ -369,9 +369,10 @@ class BaseConfigParser(Parser):
args.weight_byte_size = get_dtype_size(torch_dtype)
# FIXME: handle this better by parsing whether activations use
# bf16, fp32, etc...
args.activation_byte_size = 2
# Activations are produced in the model's compute dtype. Quantization
# overrides weight_byte_size below but leaves activations alone, so
# this stays keyed on the model dtype.
args.activation_byte_size = get_dtype_size(torch_dtype)
args.dp_size = vllm_config.parallel_config.data_parallel_size
args.tp_size = vllm_config.parallel_config.tensor_parallel_size