Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/features/quantization/modelopt.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ following `quantization.quant_algo` values:
[Engine Arguments](../../configuration/engine_args.md) page and shown by
`vllm serve --help=KernelConfig`.

!!! note
For MXFP8 checkpoints on SM100, SM103, and SM107 GPUs, BF16 models can

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean "MXFP8 models"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to "models quantized to MXFP8 with BF16 activations" to distinguish the weight format from the activation dtype.

select FlashInfer's TRTLLM dense GEMM explicitly with
`--linear-backend flashinfer_trtllm`. Automatic MXFP8 kernel selection is
unchanged.

## Quantizing HuggingFace Models with PTQ

You can quantize HuggingFace models using the example scripts provided in the Model Optimizer repository. The primary script for LLM PTQ is typically found within the `examples/llm_ptq` directory.
Expand Down
123 changes: 123 additions & 0 deletions tests/kernels/quantization/test_flashinfer_mxfp8_trtllm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import pytest
import torch
import torch.nn.functional as F

from vllm.model_executor.kernels.linear import (
FlashInferTrtllmMxfp8LinearKernel,
Mxfp8LinearLayerConfig,
)
from vllm.platforms import current_platform
from vllm.utils import flashinfer as vllm_flashinfer
from vllm.utils.flashinfer import has_flashinfer

if not (
current_platform.is_cuda()
and any(current_platform.is_device_capability(cc) for cc in (100, 103, 107))
and has_flashinfer()
):
pytest.skip(
reason="FlashInfer TRTLLM MXFP8 requires SM100, SM103, or SM107",
allow_module_level=True,
)


def _make_layer(weight: torch.Tensor) -> torch.nn.Module:
from flashinfer import SfLayout, mxfp8_quantize

weight_mxfp8, weight_scale = mxfp8_quantize(
weight,
sf_swizzle_layout=SfLayout.layout_linear,
)
layer = torch.nn.Module()
layer.weight = torch.nn.Parameter(weight_mxfp8, requires_grad=False)
layer.weight_scale = torch.nn.Parameter(
weight_scale.view(weight.shape[0], weight.shape[1] // 32),
requires_grad=False,
)
return layer


@pytest.mark.parametrize("shape", [(1, 130, 256), (7, 256, 512), (128, 130, 768)])
@torch.inference_mode()
def test_flashinfer_trtllm_mxfp8_linear_numerics(
shape: tuple[int, int, int],
) -> None:
torch.manual_seed(0)
m, n, k = shape
x = torch.randn((m, k), dtype=torch.bfloat16, device="cuda")
weight = torch.randn((n, k), dtype=torch.bfloat16, device="cuda")
layer = _make_layer(weight)
kernel = FlashInferTrtllmMxfp8LinearKernel(Mxfp8LinearLayerConfig())
kernel.process_weights_after_loading(layer)

output = kernel.apply_weights(layer, x)
reference = torch.mm(x, weight.t())
similarity = F.cosine_similarity(
output.float().flatten(), reference.float().flatten(), dim=0
)

assert output.shape == (m, n)
assert output.is_contiguous()
assert similarity.item() > 0.98


@torch.inference_mode()
def test_flashinfer_trtllm_mxfp8_custom_ops() -> None:
x = torch.randn((7, 512), dtype=torch.bfloat16, device="cuda")
weight = torch.randn((256, 512), dtype=torch.bfloat16, device="cuda")
layer = _make_layer(weight)
kernel = FlashInferTrtllmMxfp8LinearKernel(Mxfp8LinearLayerConfig())
kernel.process_weights_after_loading(layer)

torch.library.opcheck(
torch.ops.vllm.flashinfer_mxfp8_quantize_8x4.default,
(x,),
)
x_mxfp8, x_scale = vllm_flashinfer.flashinfer_mxfp8_quantize_8x4(x)
# SchemaCheckMode compares inputs with allclose, which CUDA does not
# implement for float8. The numerical tests above guard input mutation.
torch.library.opcheck(
torch.ops.vllm.mm_mxfp8.default,
(
x_mxfp8,
layer.weight.t(),
x_scale,
layer.weight_scale,
torch.bfloat16,
"trtllm",
True,
),
test_utils=(
"test_autograd_registration",
"test_faketensor",
"test_aot_dispatch_dynamic",
),
)


@torch.inference_mode()
def test_flashinfer_trtllm_mxfp8_linear_cuda_graph() -> None:
torch.manual_seed(0)
m, n, k = 7, 130, 512
weight = torch.randn((n, k), dtype=torch.bfloat16, device="cuda")
layer = _make_layer(weight)
kernel = FlashInferTrtllmMxfp8LinearKernel(Mxfp8LinearLayerConfig())
kernel.process_weights_after_loading(layer)

static_x = torch.randn((m, k), dtype=torch.bfloat16, device="cuda")
kernel.apply_weights(layer, static_x)
torch.accelerator.synchronize()

graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph):
graph_output = kernel.apply_weights(layer, static_x)

new_x = torch.randn_like(static_x)
static_x.copy_(new_x)
graph.replay()
eager_output = kernel.apply_weights(layer, new_x)

torch.testing.assert_close(graph_output, eager_output, rtol=0, atol=0)
252 changes: 252 additions & 0 deletions tests/kernels/quantization/test_mxfp8_trtllm_linear.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a lot of these mocked tests are overkill

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this part

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduced the mocked CPU suite from 252 lines to one backend-selection test. Numerics, padding, custom-op checks, and CUDA Graph coverage remain in the real GPU tests.

Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import sys
import types
from unittest.mock import patch

import pytest
import torch

from vllm.model_executor.kernels.linear import (
_POSSIBLE_MXFP8_KERNELS,
FlashInferTrtllmMxfp8LinearKernel,
Mxfp8LinearLayerConfig,
init_mxfp8_linear_kernel,
)
from vllm.platforms import PlatformEnum

pytestmark = pytest.mark.cpu_test


def _kernel() -> FlashInferTrtllmMxfp8LinearKernel:
kernel = object.__new__(FlashInferTrtllmMxfp8LinearKernel)
kernel.config = Mxfp8LinearLayerConfig()
return kernel


@pytest.mark.parametrize(
("compute_capability", "expected"),
[(100, True), (103, True), (107, True), (101, False), (120, False)],
)
def test_mxfp8_trtllm_supports_exact_blackwell_capabilities(
compute_capability: int, expected: bool
) -> None:
with (
patch(
"vllm.model_executor.kernels.linear.mxfp8.flashinfer.current_platform.is_cuda",
return_value=True,
),
patch(
"vllm.model_executor.kernels.linear.mxfp8.flashinfer.has_flashinfer",
return_value=True,
),
):
supported, _ = FlashInferTrtllmMxfp8LinearKernel.is_supported(
compute_capability
)

assert supported is expected


@patch("vllm.model_executor.kernels.linear.current_platform")
def test_mxfp8_trtllm_backend_selects_trtllm_kernel(platform_mock) -> None:
platform_mock._enum = PlatformEnum.CUDA

with (
patch(
"vllm.model_executor.kernels.linear._get_linear_backend",
return_value="flashinfer_trtllm",
),
patch.object(
FlashInferTrtllmMxfp8LinearKernel,
"is_supported",
return_value=(True, None),
),
):
kernel = init_mxfp8_linear_kernel()

assert isinstance(kernel, FlashInferTrtllmMxfp8LinearKernel)


@patch("vllm.model_executor.kernels.linear.current_platform")
def test_mxfp8_trtllm_backend_is_not_considered_by_auto(platform_mock) -> None:
platform_mock._enum = PlatformEnum.CUDA

with (
patch(
"vllm.model_executor.kernels.linear._get_linear_backend",
return_value="auto",
),
patch.dict(
_POSSIBLE_MXFP8_KERNELS,
{PlatformEnum.CUDA: [FlashInferTrtllmMxfp8LinearKernel]},
),
patch.object(
FlashInferTrtllmMxfp8LinearKernel,
"is_supported",
return_value=(True, None),
),
pytest.raises(
ValueError, match="Failed to find a kernel that can implement the MXFP8"
),
):
init_mxfp8_linear_kernel()


def test_mxfp8_trtllm_prepares_padded_weight_and_scale(monkeypatch) -> None:
calls: dict[str, tuple[tuple[int, ...], int, int | None]] = {}

def shuffle_matrix_a(weight: torch.Tensor, tile_m: int) -> torch.Tensor:
calls["weight"] = (tuple(weight.shape), tile_m, None)
return weight

def shuffle_matrix_sf_a(
scale: torch.Tensor, tile_m: int, *, num_elts_per_sf: int
) -> torch.Tensor:
calls["scale"] = (tuple(scale.shape), tile_m, num_elts_per_sf)
return scale

flashinfer = types.ModuleType("flashinfer")
flashinfer.__dict__["shuffle_matrix_a"] = shuffle_matrix_a
flashinfer.__dict__["shuffle_matrix_sf_a"] = shuffle_matrix_sf_a
monkeypatch.setitem(sys.modules, "flashinfer", flashinfer)

layer = torch.nn.Module()
layer.weight = torch.nn.Parameter(
torch.zeros((130, 512), dtype=torch.float8_e4m3fn), requires_grad=False
)
layer.weight_scale = torch.nn.Parameter(
torch.zeros((130, 16), dtype=torch.uint8), requires_grad=False
)

_kernel().process_weights_after_loading(layer)

assert calls == {
"weight": ((256, 512), 128, None),
"scale": ((256, 16), 128, 32),
}
assert layer.weight.shape == (256, 512)
assert layer.weight_scale.shape == (4096,)
assert layer._mxfp8_trtllm_output_size == 130

_kernel().process_weights_after_loading(layer)
assert layer.weight.shape == (256, 512)
assert layer._mxfp8_trtllm_output_size == 130

# Layerwise reload restores checkpoint-format tensors but keeps ordinary
# Python attributes on the module. The backend must prepare them again.
layer.weight = torch.nn.Parameter(
torch.zeros((130, 512), dtype=torch.float8_e4m3fn), requires_grad=False
)
layer.weight_scale = torch.nn.Parameter(
torch.zeros((130, 16), dtype=torch.uint8), requires_grad=False
)

_kernel().process_weights_after_loading(layer)
assert layer.weight.shape == (256, 512)
assert layer.weight_scale.shape == (4096,)
assert layer._mxfp8_trtllm_output_size == 130


def test_mxfp8_trtllm_rejects_unsupported_k(monkeypatch) -> None:
flashinfer = types.ModuleType("flashinfer")
flashinfer.__dict__["shuffle_matrix_a"] = lambda weight, tile_m: weight
flashinfer.__dict__["shuffle_matrix_sf_a"] = lambda scale, tile_m, **kwargs: scale
monkeypatch.setitem(sys.modules, "flashinfer", flashinfer)

layer = torch.nn.Module()
layer.weight = torch.nn.Parameter(
torch.zeros((128, 288), dtype=torch.float8_e4m3fn), requires_grad=False
)
layer.weight_scale = torch.nn.Parameter(
torch.zeros((128, 9), dtype=torch.uint8), requires_grad=False
)

with pytest.raises(ValueError, match="K to be divisible by 256"):
_kernel().process_weights_after_loading(layer)


def test_mxfp8_trtllm_uses_8x4_quantization_and_slices_output(monkeypatch) -> None:
calls: dict[str, object] = {}

def quantize_8x4(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
calls["quant_shape"] = tuple(x.shape)
return (
torch.empty_like(x, dtype=torch.float8_e4m3fn),
torch.empty((128,), dtype=torch.uint8),
)

def mm_mxfp8(
a: torch.Tensor,
b: torch.Tensor,
a_scale: torch.Tensor,
b_scale: torch.Tensor,
*,
out_dtype: torch.dtype,
backend: str,
use_8x4_sf_layout: bool,
) -> torch.Tensor:
calls["mm"] = {
"a": tuple(a.shape),
"b": tuple(b.shape),
"a_scale": tuple(a_scale.shape),
"b_scale": tuple(b_scale.shape),
"out_dtype": out_dtype,
"backend": backend,
"use_8x4_sf_layout": use_8x4_sf_layout,
}
return torch.ones((a.shape[0], b.shape[1]), dtype=out_dtype)

monkeypatch.setattr(
"vllm.model_executor.kernels.linear.mxfp8.flashinfer.vllm_flashinfer.flashinfer_mxfp8_quantize_8x4",
quantize_8x4,
raising=False,
)
monkeypatch.setattr(
"vllm.model_executor.kernels.linear.mxfp8.flashinfer.vllm_flashinfer.mm_mxfp8",
mm_mxfp8,
raising=False,
)

layer = torch.nn.Module()
layer.weight = torch.nn.Parameter(
torch.zeros((256, 512), dtype=torch.float8_e4m3fn), requires_grad=False
)
layer.weight_scale = torch.nn.Parameter(
torch.zeros((4096,), dtype=torch.uint8), requires_grad=False
)
layer._mxfp8_trtllm_output_size = 130
bias = torch.arange(130, dtype=torch.bfloat16)

x = torch.zeros((2, 3, 512), dtype=torch.bfloat16)
output = _kernel().apply_weights(layer, x)
output_with_bias = _kernel().apply_weights(layer, x, bias)

assert output.shape == (2, 3, 130)
assert output.is_contiguous()
assert calls["quant_shape"] == (6, 512)
assert calls["mm"] == {
"a": (6, 512),
"b": (512, 256),
"a_scale": (128,),
"b_scale": (4096,),
"out_dtype": torch.bfloat16,
"backend": "trtllm",
"use_8x4_sf_layout": True,
}
torch.testing.assert_close(output_with_bias[0, 0], bias + 1)


def test_mxfp8_trtllm_rejects_float16_input() -> None:
layer = torch.nn.Module()
layer.weight = torch.nn.Parameter(
torch.zeros((256, 512), dtype=torch.float8_e4m3fn), requires_grad=False
)
layer.weight_scale = torch.nn.Parameter(
torch.zeros((4096,), dtype=torch.uint8), requires_grad=False
)
layer._mxfp8_trtllm_output_size = 256

with pytest.raises(ValueError, match="requires bfloat16 output"):
_kernel().apply_weights(layer, torch.zeros((1, 512), dtype=torch.float16))
Loading
Loading