[MXFP4] Support for linear layers + compressed-tensors integration (#41664)

This commit is contained in:
Dipika Sikka
2026-05-12 07:49:33 -04:00
committed by GitHub
parent 4df1be9547
commit a7b801e26d
11 changed files with 358 additions and 26 deletions
+41 -2
View File
@@ -20,6 +20,7 @@ import torch
import vllm.envs as envs
from vllm.logger import init_logger
from vllm.platforms import current_platform
from vllm.utils.math_utils import cdiv
logger = init_logger(__name__)
@@ -480,6 +481,8 @@ if has_flashinfer():
dtype: torch.dtype,
use_8x4_sf_layout: bool,
backend: str,
block_size: int = 16,
use_nvfp4: bool = True,
) -> torch.Tensor:
from flashinfer import mm_fp4 as flashinfer_mm_fp4_
@@ -490,8 +493,9 @@ if has_flashinfer():
B_scale,
g_scale,
dtype,
block_size=16,
block_size=block_size,
use_8x4_sf_layout=use_8x4_sf_layout,
use_nvfp4=use_nvfp4,
backend=backend,
)
@@ -507,9 +511,36 @@ if has_flashinfer():
dtype: torch.dtype,
use_8x4_sf_layout: bool,
backend: str,
block_size: int = 16,
use_nvfp4: bool = True,
) -> torch.Tensor:
return torch.empty(A.shape[0], B.shape[1], dtype=dtype, device=A.device)
@torch.library.custom_op(
"vllm::flashinfer_mxfp4_quantize",
mutates_args=[],
device_types="cuda",
)
def flashinfer_mxfp4_quantize(
a: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
from flashinfer import mxfp4_quantize as _mxfp4_quantize
return _mxfp4_quantize(a)
@torch.library.register_fake("vllm::flashinfer_mxfp4_quantize")
def flashinfer_mxfp4_quantize_fake(
a: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
m, k = a.shape
sf_vec_size = 32
padded_m = cdiv(m, 128) * 128
sf_cols = cdiv(k // sf_vec_size, 4) * 4
return (
torch.empty(m, k // 2, dtype=torch.uint8, device=a.device),
torch.empty(padded_m, sf_cols, dtype=torch.uint8, device=a.device),
)
@torch.library.custom_op(
"vllm::bmm_fp8",
mutates_args=[],
@@ -658,15 +689,20 @@ def flashinfer_scaled_fp4_mm(
b: torch.Tensor,
block_scale_a: torch.Tensor,
block_scale_b: torch.Tensor,
alpha: torch.Tensor,
alpha: torch.Tensor | None,
out_dtype: torch.dtype,
backend: str,
block_size: int = 16,
use_nvfp4: bool = True,
) -> torch.Tensor:
assert a.ndim == 2 and b.ndim == 2
assert block_scale_a.ndim == 2 and block_scale_b.ndim == 2
assert a.stride(-1) == 1 and b.stride(-1) == 1
assert a.shape[1] == b.shape[1]
if alpha is None:
alpha = torch.ones(1, dtype=torch.float32, device=a.device)
if backend in ("cutlass", "cudnn"):
block_scale_a = block_scale_a.view(torch.uint8)
block_scale_b = block_scale_b.view(torch.uint8)
@@ -682,6 +718,8 @@ def flashinfer_scaled_fp4_mm(
out_dtype,
use_8x4_sf_layout=use_8x4_sf_layout,
backend=backend,
block_size=block_size,
use_nvfp4=use_nvfp4,
)
@@ -904,6 +942,7 @@ __all__ = [
"supports_trtllm_attention",
"can_use_trtllm_attention",
"use_trtllm_attention",
"flashinfer_mxfp4_quantize",
"flashinfer_scaled_fp4_mm",
"flashinfer_scaled_fp4_mm_out",
"flashinfer_scaled_fp8_mm",