2025-05-01 07:59:28 -07:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-10-22 12:22:39 +08:00
|
|
|
import itertools
|
2025-09-07 04:39:34 +08:00
|
|
|
|
2025-05-01 07:59:28 -07:00
|
|
|
import pytest
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
|
|
import vllm.envs as envs
|
2025-09-07 04:39:34 +08:00
|
|
|
from tests.kernels.quantization.nvfp4_utils import quant_nvfp4_tensor
|
2025-12-09 16:39:26 -06:00
|
|
|
from vllm._aiter_ops import IS_AITER_FOUND
|
2025-08-29 03:36:50 +08:00
|
|
|
from vllm._custom_ops import cutlass_scaled_fp4_mm, scaled_fp4_quant
|
|
|
|
|
from vllm.compilation.activation_quant_fusion import (
|
|
|
|
|
FUSED_OPS,
|
|
|
|
|
SILU_MUL_OP,
|
|
|
|
|
ActivationQuantFusionPass,
|
|
|
|
|
)
|
|
|
|
|
from vllm.compilation.fusion import QUANT_OPS
|
2025-07-11 00:56:28 -04:00
|
|
|
from vllm.compilation.noop_elimination import NoOpEliminationPass
|
2025-09-22 15:30:05 -04:00
|
|
|
from vllm.compilation.post_cleanup import PostCleanupPass
|
2025-10-22 12:22:39 +08:00
|
|
|
from vllm.config import (
|
|
|
|
|
CompilationConfig,
|
|
|
|
|
CompilationMode,
|
|
|
|
|
PassConfig,
|
|
|
|
|
VllmConfig,
|
|
|
|
|
set_current_vllm_config,
|
|
|
|
|
)
|
2025-05-01 07:59:28 -07:00
|
|
|
from vllm.model_executor.layers.activation import SiluAndMul
|
2026-01-20 14:48:20 +08:00
|
|
|
from vllm.model_executor.layers.quantization.kernels.scaled_mm.cutlass import (
|
|
|
|
|
CutlassFP8ScaledMMLinearKernel,
|
|
|
|
|
)
|
|
|
|
|
from vllm.model_executor.layers.quantization.kernels.scaled_mm.flashinfer import (
|
|
|
|
|
FlashInferFP8ScaledMMLinearKernel,
|
|
|
|
|
)
|
|
|
|
|
from vllm.model_executor.layers.quantization.kernels.scaled_mm.pytorch import (
|
|
|
|
|
PerTensorTorchFP8ScaledMMLinearKernel,
|
|
|
|
|
)
|
|
|
|
|
from vllm.model_executor.layers.quantization.kernels.scaled_mm.rocm import (
|
|
|
|
|
ROCmFP8ScaledMMLinearKernel,
|
|
|
|
|
)
|
|
|
|
|
from vllm.model_executor.layers.quantization.kernels.scaled_mm.ScaledMMLinearKernel import ( # noqa: E501
|
|
|
|
|
FP8ScaledMMLinearKernel,
|
|
|
|
|
)
|
2025-12-09 16:39:26 -06:00
|
|
|
from vllm.model_executor.layers.quantization.utils.fp8_utils import W8A8BlockFp8LinearOp
|
2025-07-11 00:56:28 -04:00
|
|
|
from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
2025-08-29 03:36:50 +08:00
|
|
|
GroupShape,
|
|
|
|
|
kFp8StaticTensorSym,
|
2026-01-21 08:22:33 -05:00
|
|
|
kNvfp4Dynamic,
|
2025-08-29 03:36:50 +08:00
|
|
|
)
|
2025-07-11 00:56:28 -04:00
|
|
|
from vllm.platforms import current_platform
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2026-01-20 14:48:20 +08:00
|
|
|
from ..utils import TestFP8Layer
|
2025-05-01 07:59:28 -07:00
|
|
|
from .backend import TestBackend
|
|
|
|
|
|
2025-08-29 03:36:50 +08:00
|
|
|
FP8_DTYPE = current_platform.fp8_dtype()
|
|
|
|
|
FP4_DTYPE = torch.uint8
|
2025-05-01 07:59:28 -07:00
|
|
|
|
|
|
|
|
|
2025-08-29 03:36:50 +08:00
|
|
|
def is_nvfp4_supported():
|
|
|
|
|
return current_platform.has_device_capability(100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSiluMulFp8QuantModel(torch.nn.Module):
|
2026-01-20 14:48:20 +08:00
|
|
|
quant_key = kFp8StaticTensorSym
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self, hidden_size: int, force_kernel: FP8ScaledMMLinearKernel, **kwargs
|
|
|
|
|
):
|
2025-08-29 03:36:50 +08:00
|
|
|
super().__init__()
|
2025-05-01 07:59:28 -07:00
|
|
|
self.silu_and_mul = SiluAndMul()
|
|
|
|
|
|
2026-01-20 14:48:20 +08:00
|
|
|
self.fp8_linear = TestFP8Layer(
|
|
|
|
|
weight_shape=(hidden_size, hidden_size),
|
|
|
|
|
activation_quant_key=self.quant_key,
|
|
|
|
|
weight_quant_key=self.quant_key,
|
|
|
|
|
force_kernel=force_kernel,
|
|
|
|
|
)
|
2025-07-11 00:56:28 -04:00
|
|
|
|
2025-10-22 12:22:39 +08:00
|
|
|
self.enable_silu_mul_custom_op = self.silu_and_mul.enabled()
|
2026-01-20 14:48:20 +08:00
|
|
|
self.enable_quant_fp8_custom_op = self.fp8_linear.is_quant_fp8_enabled()
|
2025-07-11 00:56:28 -04:00
|
|
|
|
2025-05-01 07:59:28 -07:00
|
|
|
def forward(self, x):
|
|
|
|
|
y = self.silu_and_mul(x)
|
2026-01-20 14:48:20 +08:00
|
|
|
x2 = self.fp8_linear(y)
|
2025-05-01 07:59:28 -07:00
|
|
|
return x2
|
|
|
|
|
|
2025-08-29 03:36:50 +08:00
|
|
|
def ops_in_model_before(self):
|
2025-10-22 12:22:39 +08:00
|
|
|
return [
|
|
|
|
|
SILU_MUL_OP if self.enable_silu_mul_custom_op else torch.ops.aten.mul,
|
|
|
|
|
(
|
|
|
|
|
QUANT_OPS[kFp8StaticTensorSym]
|
|
|
|
|
if self.enable_quant_fp8_custom_op
|
|
|
|
|
else torch.ops.aten.reciprocal
|
|
|
|
|
),
|
|
|
|
|
]
|
2025-08-29 03:36:50 +08:00
|
|
|
|
|
|
|
|
def ops_in_model_after(self):
|
|
|
|
|
return [FUSED_OPS[kFp8StaticTensorSym]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSiluMulNvfp4QuantModel(torch.nn.Module):
|
2025-09-07 04:39:34 +08:00
|
|
|
def __init__(self, hidden_size: int, x: torch.Tensor, **kwargs):
|
2025-08-29 03:36:50 +08:00
|
|
|
super().__init__()
|
2025-09-22 15:30:05 -04:00
|
|
|
from vllm.compilation.activation_quant_fusion import (
|
|
|
|
|
silu_and_mul_nvfp4_quant_supported,
|
|
|
|
|
)
|
2025-10-05 15:06:22 +01:00
|
|
|
|
2025-09-22 15:30:05 -04:00
|
|
|
assert silu_and_mul_nvfp4_quant_supported
|
|
|
|
|
|
2025-08-29 03:36:50 +08:00
|
|
|
self.silu_and_mul = SiluAndMul()
|
2025-10-22 12:22:39 +08:00
|
|
|
self.enable_silu_mul_custom_op = self.silu_and_mul.enabled()
|
2025-09-07 04:39:34 +08:00
|
|
|
|
|
|
|
|
# create nvfp4 weight
|
|
|
|
|
w = torch.rand((hidden_size, hidden_size))
|
|
|
|
|
self.w, self.w_block_scale, self.w_global_scale = quant_nvfp4_tensor(w)
|
|
|
|
|
|
|
|
|
|
# get global scale offline
|
|
|
|
|
_, _, self.y_global_scale = quant_nvfp4_tensor(self.silu_and_mul(x))
|
|
|
|
|
|
|
|
|
|
self.alpha = 1.0 / (self.w_global_scale * self.y_global_scale)
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-08-29 03:36:50 +08:00
|
|
|
def forward(self, x):
|
|
|
|
|
y = self.silu_and_mul(x)
|
2025-09-07 04:39:34 +08:00
|
|
|
y_quant, y_block_scale = scaled_fp4_quant(y, self.y_global_scale)
|
2025-08-29 03:36:50 +08:00
|
|
|
out = cutlass_scaled_fp4_mm(
|
|
|
|
|
a=y_quant,
|
|
|
|
|
b=self.w,
|
|
|
|
|
block_scale_a=y_block_scale,
|
2025-09-07 04:39:34 +08:00
|
|
|
block_scale_b=self.w_block_scale,
|
|
|
|
|
alpha=self.alpha,
|
2025-08-29 03:36:50 +08:00
|
|
|
out_dtype=y.dtype,
|
|
|
|
|
)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
def ops_in_model_before(self):
|
2025-10-22 12:22:39 +08:00
|
|
|
return [
|
|
|
|
|
SILU_MUL_OP if self.enable_silu_mul_custom_op else torch.ops.aten.mul,
|
2026-01-21 08:22:33 -05:00
|
|
|
QUANT_OPS[kNvfp4Dynamic],
|
2025-10-22 12:22:39 +08:00
|
|
|
]
|
2025-08-29 03:36:50 +08:00
|
|
|
|
|
|
|
|
def ops_in_model_after(self):
|
2026-01-21 08:22:33 -05:00
|
|
|
return [FUSED_OPS[kNvfp4Dynamic]]
|
2025-08-29 03:36:50 +08:00
|
|
|
|
|
|
|
|
|
2025-12-09 16:39:26 -06:00
|
|
|
class TestSiluMulGroupFp8QuantModel(torch.nn.Module):
|
|
|
|
|
def __init__(self, hidden_size: int, **kwargs):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.silu_and_mul = SiluAndMul()
|
|
|
|
|
self.w8a8_block_fp8_linear = W8A8BlockFp8LinearOp(
|
|
|
|
|
weight_group_shape=GroupShape(128, 128),
|
|
|
|
|
act_quant_group_shape=GroupShape(1, 128),
|
|
|
|
|
cutlass_block_fp8_supported=False,
|
|
|
|
|
use_aiter_and_is_supported=True,
|
|
|
|
|
)
|
|
|
|
|
self.w = torch.rand(hidden_size, hidden_size).to(dtype=FP8_DTYPE).t()
|
|
|
|
|
|
|
|
|
|
scale_hidden_size = (hidden_size + 128 - 1) // 128
|
|
|
|
|
self.wscale = torch.rand(
|
|
|
|
|
(scale_hidden_size, scale_hidden_size), dtype=torch.float32
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.enable_silu_mul_custom_op = self.silu_and_mul.enabled()
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
|
y = self.silu_and_mul(x)
|
|
|
|
|
x2 = self.w8a8_block_fp8_linear.apply(y, self.w, self.wscale)
|
|
|
|
|
return x2
|
|
|
|
|
|
|
|
|
|
def ops_in_model_before(self):
|
|
|
|
|
return [
|
|
|
|
|
SILU_MUL_OP if self.enable_silu_mul_custom_op else torch.ops.aten.mul,
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def ops_in_model_after(self):
|
|
|
|
|
return [torch.ops.vllm.rocm_aiter_act_mul_and_fp8_group_quant]
|
|
|
|
|
|
|
|
|
|
|
2026-01-20 14:48:20 +08:00
|
|
|
ROCM_KERNELS = [ROCmFP8ScaledMMLinearKernel, PerTensorTorchFP8ScaledMMLinearKernel]
|
|
|
|
|
CUDA_KERNELS = [
|
|
|
|
|
FlashInferFP8ScaledMMLinearKernel,
|
|
|
|
|
CutlassFP8ScaledMMLinearKernel,
|
|
|
|
|
PerTensorTorchFP8ScaledMMLinearKernel,
|
|
|
|
|
]
|
|
|
|
|
TEST_KERNELS = ROCM_KERNELS if current_platform.is_rocm() else CUDA_KERNELS
|
|
|
|
|
|
|
|
|
|
|
2025-09-18 07:37:23 +08:00
|
|
|
@pytest.mark.parametrize("num_tokens", [32, 64])
|
|
|
|
|
@pytest.mark.parametrize("hidden_size", [128, 256])
|
|
|
|
|
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16])
|
2025-10-22 12:22:39 +08:00
|
|
|
@pytest.mark.parametrize("enable_silu_mul_custom_op", [True, False])
|
2025-08-29 03:36:50 +08:00
|
|
|
@pytest.mark.parametrize(
|
2026-01-20 14:48:20 +08:00
|
|
|
"model_class, enable_quant_fp8_custom_op, force_kernel",
|
|
|
|
|
list(itertools.product([TestSiluMulFp8QuantModel], [True, False], TEST_KERNELS))
|
2025-12-09 16:39:26 -06:00
|
|
|
+ [
|
2026-01-20 14:48:20 +08:00
|
|
|
(TestSiluMulNvfp4QuantModel, False, None),
|
|
|
|
|
(TestSiluMulGroupFp8QuantModel, False, None),
|
2025-12-09 16:39:26 -06:00
|
|
|
],
|
2025-09-07 04:39:34 +08:00
|
|
|
)
|
2025-05-14 00:13:56 -05:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
|
envs.VLLM_TARGET_DEVICE not in ["cuda", "rocm"], reason="Only test on CUDA and ROCm"
|
|
|
|
|
)
|
2025-09-18 07:37:23 +08:00
|
|
|
def test_fusion_silu_and_mul_quant(
|
2025-10-22 12:22:39 +08:00
|
|
|
num_tokens: int,
|
|
|
|
|
hidden_size: int,
|
|
|
|
|
dtype: torch.dtype,
|
2025-12-09 16:39:26 -06:00
|
|
|
model_class: type[
|
|
|
|
|
TestSiluMulFp8QuantModel
|
|
|
|
|
| TestSiluMulNvfp4QuantModel
|
|
|
|
|
| TestSiluMulGroupFp8QuantModel
|
|
|
|
|
],
|
2025-10-22 12:22:39 +08:00
|
|
|
enable_silu_mul_custom_op: bool,
|
|
|
|
|
enable_quant_fp8_custom_op: bool,
|
2026-01-20 14:48:20 +08:00
|
|
|
force_kernel: FP8ScaledMMLinearKernel | None,
|
2025-09-04 21:25:40 +08:00
|
|
|
):
|
2025-10-22 12:22:39 +08:00
|
|
|
if model_class is TestSiluMulNvfp4QuantModel and not is_nvfp4_supported():
|
|
|
|
|
pytest.skip("NVFP4 is not supported on this GPU.")
|
2025-12-09 16:39:26 -06:00
|
|
|
if model_class is TestSiluMulGroupFp8QuantModel and not IS_AITER_FOUND:
|
|
|
|
|
pytest.skip("AITER is not supported on this GPU.")
|
2025-08-29 03:36:50 +08:00
|
|
|
|
2025-05-01 07:59:28 -07:00
|
|
|
torch.set_default_device("cuda")
|
2025-09-18 07:37:23 +08:00
|
|
|
torch.set_default_dtype(dtype)
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-09-07 04:39:34 +08:00
|
|
|
x = torch.rand(num_tokens, hidden_size * 2)
|
|
|
|
|
|
2025-05-01 07:59:28 -07:00
|
|
|
# Reshape pass is needed for the fusion pass to work
|
2026-01-22 22:52:26 -05:00
|
|
|
custom_ops = ["none"]
|
2025-10-22 12:22:39 +08:00
|
|
|
if enable_silu_mul_custom_op:
|
|
|
|
|
custom_ops.append("+silu_and_mul")
|
|
|
|
|
if enable_quant_fp8_custom_op:
|
|
|
|
|
custom_ops.append("+quant_fp8")
|
|
|
|
|
config = VllmConfig(
|
|
|
|
|
compilation_config=CompilationConfig(
|
|
|
|
|
mode=CompilationMode.VLLM_COMPILE,
|
|
|
|
|
custom_ops=custom_ops,
|
2026-01-22 22:52:26 -05:00
|
|
|
backend="eager", # avoid compilation for SiluAndMul and QuantFP8
|
2025-12-02 22:38:55 -05:00
|
|
|
pass_config=PassConfig(fuse_act_quant=True, eliminate_noops=True),
|
2025-10-22 12:22:39 +08:00
|
|
|
),
|
2025-10-05 15:06:22 +01:00
|
|
|
)
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-10-22 12:22:39 +08:00
|
|
|
with set_current_vllm_config(config):
|
2025-12-09 16:39:26 -06:00
|
|
|
fusion_passes = [ActivationQuantFusionPass(config)]
|
|
|
|
|
if IS_AITER_FOUND:
|
|
|
|
|
from vllm.compilation.rocm_aiter_fusion import (
|
|
|
|
|
RocmAiterSiluMulFp8GroupQuantFusionPass,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
fusion_passes += [RocmAiterSiluMulFp8GroupQuantFusionPass(config)]
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-12-09 16:39:26 -06:00
|
|
|
passes = [NoOpEliminationPass(config), *fusion_passes, PostCleanupPass(config)]
|
2025-10-22 12:22:39 +08:00
|
|
|
backend = TestBackend(*passes)
|
2026-01-20 14:48:20 +08:00
|
|
|
model = model_class(hidden_size=hidden_size, force_kernel=force_kernel, x=x)
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-10-22 12:22:39 +08:00
|
|
|
# First dimension dynamic
|
|
|
|
|
torch._dynamo.mark_dynamic(x, 0)
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-10-22 12:22:39 +08:00
|
|
|
result = model(x)
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-10-22 12:22:39 +08:00
|
|
|
model2 = torch.compile(model, backend=backend)
|
|
|
|
|
result2 = model2(x)
|
2025-09-07 04:39:34 +08:00
|
|
|
|
2025-10-22 12:22:39 +08:00
|
|
|
# Check that it gives the same answer
|
|
|
|
|
if model_class == TestSiluMulFp8QuantModel:
|
|
|
|
|
atol, rtol = 1e-3, 1e-3
|
|
|
|
|
elif model_class == TestSiluMulNvfp4QuantModel:
|
|
|
|
|
atol, rtol = 1e-1, 1e-1
|
2025-12-09 16:39:26 -06:00
|
|
|
elif model_class == TestSiluMulGroupFp8QuantModel:
|
|
|
|
|
atol, rtol = 5e-2, 5e-2
|
2025-10-22 12:22:39 +08:00
|
|
|
|
|
|
|
|
torch.testing.assert_close(
|
|
|
|
|
result[0].to(dtype=dtype), result2[0].to(dtype=dtype), atol=atol, rtol=rtol
|
|
|
|
|
)
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-12-09 16:39:26 -06:00
|
|
|
assert sum([p.matched_count for p in fusion_passes]) == 1
|
2025-09-22 15:30:05 -04:00
|
|
|
|
2025-10-22 12:22:39 +08:00
|
|
|
# In pre-nodes, quant op should be present and fused kernels should not
|
|
|
|
|
backend.check_before_ops(model.ops_in_model_before())
|
2025-05-01 07:59:28 -07:00
|
|
|
|
2025-10-22 12:22:39 +08:00
|
|
|
# In post-nodes, fused kernels should be present and quant op should not
|
|
|
|
|
backend.check_after_ops(model.ops_in_model_after())
|