[quantization] Add per-token NVFP4 quantization MoE support for ReLU2 (#56740)
Signed-off-by: Xuanteng Huang <xuantengh@nvidia.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: OpenAI Codex <noreply@openai.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
OpenAI Codex
parent
b803edbc9f
commit
f38c6778ce
@@ -297,3 +297,16 @@ def test_nemotron_fp4_moe_flashinfer_trtllm(monkeypatch: pytest.MonkeyPatch):
|
||||
hf_overrides=HF_OVERRIDE_TEXT,
|
||||
extra_args=["--moe-backend=flashinfer_trtllm"],
|
||||
)
|
||||
|
||||
|
||||
def test_nemotron_nvfp4_per_token_moe_flashinfer_trtllm(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
):
|
||||
can_initialize(
|
||||
"nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-NVFP4",
|
||||
hf_overrides=HF_OVERRIDE_TEXT,
|
||||
extra_args=[
|
||||
"--moe-backend=flashinfer_trtllm",
|
||||
"--quantization-config.moe.activation=nvfp4_per_token",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ Run `pytest tests/quantization/test_modelopt.py`.
|
||||
"""
|
||||
|
||||
import os
|
||||
from contextlib import nullcontext
|
||||
from typing import Any, NoReturn
|
||||
from unittest.mock import MagicMock, Mock, patch
|
||||
|
||||
@@ -17,7 +18,9 @@ from tests.quantization.utils import (
|
||||
load_model_without_vllm_runner,
|
||||
)
|
||||
from vllm.config import VllmConfig, set_current_vllm_config
|
||||
from vllm.config.load import LoadConfig
|
||||
from vllm.config.model import ModelConfig
|
||||
from vllm.config.quantization import QuantizationConfigArgs
|
||||
from vllm.forward_context import set_forward_context
|
||||
from vllm.model_executor.kernels.linear import (
|
||||
FlashInferCuteDslNvFp4W4A16LinearKernel,
|
||||
@@ -40,12 +43,14 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
kMxfp8Dynamic,
|
||||
kMxfp8Static,
|
||||
kNvfp4Dynamic,
|
||||
kNvfp4DynamicToken,
|
||||
kNvfp4Static,
|
||||
)
|
||||
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
||||
ParallelLMHead,
|
||||
VocabParallelEmbedding,
|
||||
)
|
||||
from vllm.model_executor.model_loader.weight_utils import get_quant_config
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
|
||||
@@ -113,11 +118,13 @@ def _mixed_precision_config(quantized_layers: dict) -> ModelOptMixedPrecisionCon
|
||||
)
|
||||
|
||||
|
||||
def test_modelopt_nvfp4_quantizes_parallel_lm_head():
|
||||
@pytest.mark.parametrize("moe_activation", [None, "nvfp4_per_token"])
|
||||
def test_modelopt_nvfp4_quantizes_parallel_lm_head(moe_activation):
|
||||
config = ModelOptNvFp4Config(
|
||||
is_checkpoint_nvfp4_serialized=True,
|
||||
kv_cache_quant_algo=None,
|
||||
exclude_modules=[],
|
||||
quantization_args=QuantizationConfigArgs(moe={"activation": moe_activation}),
|
||||
)
|
||||
|
||||
method = config.get_quant_method(_mock_lm_head(), prefix="lm_head")
|
||||
@@ -790,6 +797,74 @@ def test_modelopt_nvfp4_moe_dispatches_to_marlin_when_w4a16(
|
||||
assert kwargs["activation_key"] is kNvfp4Dynamic
|
||||
|
||||
|
||||
@pytest.mark.parametrize("quantization", ["modelopt_fp4", "modelopt_mixed"])
|
||||
@pytest.mark.parametrize("per_token", [False, True])
|
||||
def test_modelopt_nvfp4_moe_activation_override(quantization, per_token):
|
||||
"""Runtime overrides reach NVFP4 configs without changing HF metadata."""
|
||||
hf_config = {
|
||||
"quant_method": "modelopt",
|
||||
"quant_algo": "MIXED_PRECISION"
|
||||
if quantization == "modelopt_mixed"
|
||||
else "NVFP4",
|
||||
"quantized_layers": {"experts": {"quant_algo": "NVFP4"}},
|
||||
}
|
||||
args = (
|
||||
QuantizationConfigArgs(moe={"activation": "nvfp4_per_token"})
|
||||
if per_token
|
||||
else None
|
||||
)
|
||||
config = get_quant_config(
|
||||
Mock(
|
||||
quantization=quantization,
|
||||
quantization_config=args,
|
||||
hf_config=Mock(quantization_config=hf_config),
|
||||
),
|
||||
LoadConfig(),
|
||||
)
|
||||
if isinstance(config, ModelOptMixedPrecisionConfig):
|
||||
config = config.nvfp4_config
|
||||
assert isinstance(config, ModelOptNvFp4Config)
|
||||
assert config.moe_activation_override == (kNvfp4DynamicToken if per_token else None)
|
||||
assert "_online_quantization_args" not in hf_config
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"quant_method,activation,backend,error",
|
||||
[
|
||||
("NVFP4", kNvfp4DynamicToken, "FLASHINFER_TRTLLM", None),
|
||||
("W4A16_NVFP4", kNvfp4DynamicToken, "FLASHINFER_TRTLLM", "W4A4 checkpoint"),
|
||||
(
|
||||
"NVFP4",
|
||||
kMxfp8Dynamic,
|
||||
"FLASHINFER_TRTLLM",
|
||||
"Unsupported.*activation override",
|
||||
),
|
||||
("NVFP4", kNvfp4DynamicToken, "MARLIN", "requires the FlashInfer TRTLLM"),
|
||||
],
|
||||
)
|
||||
def test_modelopt_nvfp4_moe_validates_activation_override(
|
||||
quant_method, activation, backend, error
|
||||
):
|
||||
from vllm.model_executor.layers.fused_moe.oracle.nvfp4 import NvFp4MoeBackend
|
||||
from vllm.model_executor.layers.quantization.modelopt import ModelOptNvFp4FusedMoE
|
||||
|
||||
config = ModelOptNvFp4Config(
|
||||
quant_method=quant_method,
|
||||
quantization_args=QuantizationConfigArgs(moe={"activation": activation}),
|
||||
)
|
||||
expected = pytest.raises(ValueError, match=error) if error else nullcontext()
|
||||
with (
|
||||
patch(
|
||||
"vllm.model_executor.layers.quantization.modelopt.select_nvfp4_moe_backend",
|
||||
return_value=(NvFp4MoeBackend[backend], Mock()),
|
||||
) as select_backend,
|
||||
expected,
|
||||
):
|
||||
method = ModelOptNvFp4FusedMoE(config, Mock())
|
||||
assert method.per_token_activation
|
||||
assert select_backend.call_args.kwargs["activation_key"] == activation
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"per_layer_algo, expected_weight, expected_activation",
|
||||
[
|
||||
|
||||
@@ -103,6 +103,8 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
amax_for_tp_weight_quant,
|
||||
kMxfp8Dynamic,
|
||||
kMxfp8Static,
|
||||
kNvfp4DynamicToken,
|
||||
kNvfp4Static,
|
||||
weight_amax,
|
||||
)
|
||||
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
||||
@@ -211,15 +213,26 @@ def test_qianfan_online_fp8_keeps_vision_layers_unquantized(
|
||||
assert args.ignore == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("is_act_and_mul", "expected_hidden_alignment"),
|
||||
[(True, 256), (False, 512)],
|
||||
)
|
||||
def test_online_nvfp4_reuses_kernel_when_weights_are_reprocessed(
|
||||
monkeypatch,
|
||||
monkeypatch, is_act_and_mul, expected_hidden_alignment
|
||||
) -> None:
|
||||
method = object.__new__(Nvfp4OnlineMoEMethod)
|
||||
method.moe = SimpleNamespace(is_act_and_mul=True)
|
||||
method.nvfp4_backend = object()
|
||||
method.experts_cls = object
|
||||
method.moe_quant_config = None
|
||||
method.moe_kernel = None
|
||||
monkeypatch.setattr(current_platform, "is_device_capability_family", lambda _: True)
|
||||
select_backend = Mock(return_value=(object(), object))
|
||||
monkeypatch.setattr(
|
||||
"vllm.model_executor.layers.quantization.online.nvfp4.select_nvfp4_moe_backend",
|
||||
select_backend,
|
||||
)
|
||||
moe_config = SimpleNamespace(is_act_and_mul=is_act_and_mul)
|
||||
method = Nvfp4OnlineMoEMethod(moe=moe_config)
|
||||
select_backend.assert_called_once_with(
|
||||
config=moe_config,
|
||||
weight_key=kNvfp4Static,
|
||||
activation_key=kNvfp4DynamicToken,
|
||||
)
|
||||
|
||||
layer = Mock()
|
||||
converted_weights = [
|
||||
@@ -274,7 +287,12 @@ def test_online_nvfp4_reuses_kernel_when_weights_are_reprocessed(
|
||||
|
||||
assert method.moe_kernel is kernel
|
||||
assert convert_weights.call_count == 3
|
||||
assert all(
|
||||
call.kwargs["trtllm_hidden_alignment"] == expected_hidden_alignment
|
||||
for call in convert_weights.call_args_list
|
||||
)
|
||||
make_kernel.assert_called_once()
|
||||
assert make_kernel.call_args.kwargs["per_token_activation"] is True
|
||||
get_quant_config.assert_called_once()
|
||||
assert process_weights.call_count == 3
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
kInt4Static32,
|
||||
kInt8StaticChannelSym,
|
||||
kMxfp8Dynamic,
|
||||
kNvfp4DynamicToken,
|
||||
kNvfp4Static,
|
||||
)
|
||||
|
||||
# ---- QuantSpec ------------------------------------------------------------
|
||||
@@ -68,13 +70,17 @@ def test_args_linear_string_resolves_via_quant_key_names():
|
||||
assert args.moe is None
|
||||
|
||||
|
||||
def test_args_moe_string_resolves_via_online_shorthand():
|
||||
@pytest.mark.parametrize(
|
||||
"shorthand,weight",
|
||||
[("fp8_per_block", kFp8Static128BlockSym), ("nvfp4_per_token", kNvfp4Static)],
|
||||
)
|
||||
def test_args_moe_string_resolves_via_online_shorthand(shorthand, weight):
|
||||
# An online-shorthand name pulls the matching slot from _ONLINE_SHORTHANDS
|
||||
# (so `linear: "fp8_per_block"` and `moe: "fp8_per_block"` produce the
|
||||
# same per-layer-kind spec the `--quantization fp8_per_block` shorthand
|
||||
# would).
|
||||
args = quant_config_args(moe="fp8_per_block")
|
||||
assert args.moe == QuantSpec(weight=kFp8Static128BlockSym)
|
||||
args = quant_config_args(moe=shorthand)
|
||||
assert args.moe == QuantSpec(weight=weight)
|
||||
|
||||
|
||||
def test_args_string_shorthand_missing_slot_raises():
|
||||
@@ -84,9 +90,14 @@ def test_args_string_shorthand_missing_slot_raises():
|
||||
quant_config_args(linear="int8_per_channel_weight_only")
|
||||
|
||||
|
||||
def test_args_accepts_dict_form():
|
||||
args = quant_config_args(moe={"activation": "mxfp8"})
|
||||
assert args.moe == QuantSpec(weight=None, activation=kMxfp8Dynamic)
|
||||
@pytest.mark.parametrize(
|
||||
"activation,expected",
|
||||
[("mxfp8", kMxfp8Dynamic), ("nvfp4_per_token", kNvfp4DynamicToken)],
|
||||
)
|
||||
def test_args_accepts_dict_form(activation, expected):
|
||||
args = quant_config_args(moe={"activation": activation})
|
||||
assert args.moe == QuantSpec(weight=None, activation=expected)
|
||||
assert args.linear is None
|
||||
|
||||
|
||||
def test_targets_reject_non_string_keys():
|
||||
@@ -110,22 +121,34 @@ def test_resolve_colliding_shorthand_is_deferred(quantization: str):
|
||||
assert resolve_quantization_config(quantization, None) is None
|
||||
|
||||
|
||||
def test_resolve_int8_shorthand_leaves_linear_unset():
|
||||
# int8_per_channel_weight_only is MoE-only; linear stays None so that
|
||||
# OnlineQuantizationConfig leaves Linear layers in full precision.
|
||||
args = resolve_quantization_config("int8_per_channel_weight_only", None)
|
||||
@pytest.mark.parametrize(
|
||||
"shorthand,weight",
|
||||
[
|
||||
("int8_per_channel_weight_only", kInt8StaticChannelSym),
|
||||
("nvfp4_per_token", kNvfp4Static),
|
||||
],
|
||||
)
|
||||
def test_resolve_moe_only_shorthand_leaves_linear_unset(shorthand, weight):
|
||||
args = resolve_quantization_config(shorthand, None)
|
||||
assert args is not None
|
||||
assert args.linear is None
|
||||
assert args.moe == QuantSpec(weight=kInt8StaticChannelSym)
|
||||
assert args.moe == QuantSpec(weight=weight)
|
||||
|
||||
|
||||
def test_resolve_quantization_config_only():
|
||||
@pytest.mark.parametrize("quantization", [None, "modelopt_fp4"])
|
||||
@pytest.mark.parametrize(
|
||||
"activation,expected",
|
||||
[("mxfp8", kMxfp8Dynamic), ("nvfp4_per_token", kNvfp4DynamicToken)],
|
||||
)
|
||||
def test_resolve_activation_override(quantization, activation, expected):
|
||||
# When only `quantization_config` is given (e.g. for an already-quantized
|
||||
# checkpoint that needs an activation override), it's returned as-is.
|
||||
args = resolve_quantization_config(None, {"moe": {"activation": "mxfp8"}})
|
||||
args = resolve_quantization_config(
|
||||
quantization, {"moe": {"activation": activation}}
|
||||
)
|
||||
assert args is not None
|
||||
assert args.linear is None
|
||||
assert args.moe == QuantSpec(weight=None, activation=kMxfp8Dynamic)
|
||||
assert args.moe == QuantSpec(weight=None, activation=expected)
|
||||
|
||||
|
||||
def test_resolve_merges_explicit_over_shorthand():
|
||||
|
||||
Reference in New Issue
Block a user