[MoE] MXFP4 oracle: add CUTLASS W4A4 backend and BF16 activation fallback (#60079)

Signed-off-by: mgoin <mgoin64@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Misha Goin
2026-10-08 11:39:09 -07:00
committed by GitHub
co-authored by Claude
parent d8fa463e2b
commit 30c7405890
3 changed files with 214 additions and 14 deletions
+84
View File
@@ -37,9 +37,12 @@ from vllm.model_executor.layers.fused_moe.experts.cutlass_moe import (
run_cutlass_moe_fp4,
run_cutlass_moe_fp8,
run_cutlass_moe_mxfp4,
swizzle_mxfp4_scales,
)
from vllm.model_executor.layers.fused_moe.oracle import mxfp4 as mxfp4_oracle
from vllm.model_executor.layers.fused_moe.oracle import nvfp4 as nvfp4_oracle
from vllm.model_executor.layers.fused_moe.utils import moe_kernel_quantize_input
from vllm.model_executor.layers.quantization.utils.quant_utils import kMxfp4Dynamic
from vllm.platforms import current_platform
from vllm.utils.torch_utils import set_random_seed
@@ -294,6 +297,87 @@ def test_cutlass_fp4_moe_padded_routes_do_not_change_valid_output(quantization:
torch.testing.assert_close(actual[1:], torch.zeros_like(actual[1:]))
@torch.inference_mode()
def test_cutlass_mxfp4_oracle_matches_direct_kernel(monkeypatch, workspace_init):
"""The MXFP4 oracle selects CUTLASS for W4A4 and swizzles checkpoint
scales into the layout the kernel expects."""
if not CutlassExpertsMxfp4._supports_current_device():
pytest.skip("CUTLASS mxfp4 MoE is not supported on this GPU")
monkeypatch.setattr(mxfp4_oracle, "_user_moe_activation_override", lambda: None)
set_random_seed(7)
e, m, n, k, topk = 8, 33, 256, 512, 2
device = torch.device("cuda")
w1 = torch.randint(0, 256, (e, 2 * n, k // 2), device=device, dtype=torch.uint8)
w2 = torch.randint(0, 256, (e, k, n // 2), device=device, dtype=torch.uint8)
w1_scale = torch.randint(118, 124, (e, 2 * n, k // 32), device=device).byte()
w2_scale = torch.randint(118, 124, (e, k, n // 32), device=device).byte()
a = torch.randn((m, k), device=device, dtype=torch.bfloat16)
score = torch.randn((m, e), device=device, dtype=torch.bfloat16)
topk_weights, topk_ids, _ = fused_topk(a, score, topk, renormalize=False)
def swizzle(scale: torch.Tensor) -> torch.Tensor:
rows, cols = scale.shape[1:]
return torch.stack(
[swizzle_mxfp4_scales(s, rows, cols * 32).view(rows, cols) for s in scale]
)
expected = torch.empty_like(a)
run_cutlass_moe_mxfp4(
output=expected,
a=a,
w1_fp4=w1,
w1_blockscale=swizzle(w1_scale),
w2_fp4=w2,
w2_blockscale=swizzle(w2_scale),
topk_weights=topk_weights,
topk_ids=topk_ids,
activation=MoEActivation.SILU,
workspace13=torch.empty((m * topk, max(2 * n, k)), device=device).to(a),
workspace2=torch.empty((m * topk, n), device=device).to(a),
m=m,
n=n,
k=k,
e=e,
device=device,
apply_router_weight_on_input=False,
)
moe_config = make_dummy_moe_config(
num_experts=e, experts_per_token=topk, hidden_dim=k, intermediate_size=n
)
with set_current_vllm_config(vllm_config):
backend, experts_cls = mxfp4_oracle.select_mxfp4_moe_backend(
moe_config, activation_key=kMxfp4Dynamic
)
assert backend == mxfp4_oracle.Mxfp4MoeBackend.CUTLASS_MXFP4_MXFP4
w13, w2, w13_scale, w2_scale, _, _ = (
mxfp4_oracle.convert_weight_to_mxfp4_moe_kernel_format(
backend, torch.nn.Module(), w1, w2, w1_scale, w2_scale
)
)
kernel = mxfp4_oracle.make_mxfp4_moe_kernel(
mxfp4_oracle.make_mxfp4_moe_quant_config(backend, w13_scale, w2_scale),
moe_config,
experts_cls,
backend,
)
actual = kernel.apply(
a,
w13,
w2,
topk_weights,
topk_ids,
activation=MoEActivation.SILU,
global_num_experts=e,
expert_map=None,
apply_router_weight_on_input=False,
)
torch.testing.assert_close(actual, expected, atol=0, rtol=0)
@dataclasses.dataclass
class MOETensors:
a: torch.Tensor
+51 -1
View File
@@ -7,7 +7,11 @@ from dataclasses import dataclass, replace
import pytest
import torch
from tests.kernels.moe.utils import check_accuracy, check_deferred_moe_finalize
from tests.kernels.moe.utils import (
check_accuracy,
check_deferred_moe_finalize,
make_dummy_moe_config,
)
from vllm._aiter_ops import (
is_aiter_found,
is_aiter_found_and_supported,
@@ -2009,6 +2013,52 @@ def test_select_mxfp4_moe_backend_raises_with_unsupported_reasons(
mxfp4_oracle.select_mxfp4_moe_backend(moe_config)
@pytest.mark.parametrize(
"moe_backend,w4a4,supported,expected",
[
# The checkpoint's activation wins over BF16 backends listed first.
("auto", True, {"CUTLASS_MXFP4_MXFP4", "MARLIN"}, "CUTLASS_MXFP4_MXFP4"),
# No W4A4 kernel: run at BF16 rather than another quantized activation.
("auto", True, {"FLASHINFER_TRTLLM_MXFP4_MXFP8", "MARLIN"}, "MARLIN"),
("marlin", True, {"MARLIN"}, "MARLIN"),
# A BF16 checkpoint never gets its activations quantized.
("auto", False, {"CUTLASS_MXFP4_MXFP4", "MARLIN"}, "MARLIN"),
],
)
def test_select_mxfp4_moe_backend_respects_checkpoint_activation(
monkeypatch: pytest.MonkeyPatch,
moe_backend: str,
w4a4: bool,
supported: set[str],
expected: str,
):
"""Activations run at the checkpoint's precision when a backend supports
it and fall back to BF16 otherwise, never to a lower precision."""
import vllm.model_executor.layers.fused_moe.oracle.mxfp4 as mxfp4_oracle
from vllm.model_executor.layers.quantization.utils.quant_utils import (
kMxfp4Dynamic,
)
def backend_to_kernel_cls(backend):
class Experts:
@staticmethod
def is_supported_config(*args):
return backend.name in supported, None
return [Experts]
monkeypatch.setattr(mxfp4_oracle, "backend_to_kernel_cls", backend_to_kernel_cls)
monkeypatch.setattr(mxfp4_oracle, "_user_moe_activation_override", lambda: None)
moe_config = make_dummy_moe_config()
moe_config.moe_backend = moe_backend
backend, _ = mxfp4_oracle.select_mxfp4_moe_backend(
moe_config, activation_key=kMxfp4Dynamic if w4a4 else None
)
assert backend.name == expected
@pytest.mark.parametrize("backend_name", ["AITER_TRITON_MXFP4_BF16", "AITER_MXFP4_FP8"])
@pytest.mark.parametrize("use_ep", [False, True])
def test_aiter_mxfp4_monolithic_rejects_expert_parallel(