[Quant] Serve 32-wide-K ue8m0 block-FP8 linears through the FlashInfer MXFP8 GEMMs (#40039)
This commit is contained in:
@@ -56,15 +56,19 @@ from sglang.srt.layers.quantization.base_config import (
|
||||
from sglang.srt.layers.quantization.fp8_utils import (
|
||||
_use_aiter_bpreshuffle_gfx95,
|
||||
apply_fp8_linear,
|
||||
block_fp8_scale_to_mxfp8_e8m0,
|
||||
can_auto_enable_marlin_fp8,
|
||||
can_serve_block_fp8_as_mxfp8,
|
||||
cutlass_fp8_supported,
|
||||
deepgemm_w8a8_block_fp8_linear_with_fallback,
|
||||
dispatch_block_fp8_mxfp8_linear,
|
||||
dispatch_w8a8_block_fp8_linear,
|
||||
dispatch_w8a8_mxfp8_linear,
|
||||
input_to_float8,
|
||||
mxfp8_group_quantize,
|
||||
normalize_e4m3fn_to_e4m3fnuz,
|
||||
requant_block_scale_ue8m0_for_deepgemm,
|
||||
resolve_block_fp8_mxfp8_backend,
|
||||
resolve_mxfp8_dense_gemm_backend,
|
||||
torch_w8a8_block_fp8_linear,
|
||||
unshuffle_aiter_fp8_weight,
|
||||
@@ -72,6 +76,7 @@ from sglang.srt.layers.quantization.fp8_utils import (
|
||||
)
|
||||
from sglang.srt.layers.quantization.kv_cache import BaseKVCacheMethod
|
||||
from sglang.srt.layers.quantization.marlin_utils_fp8 import prepare_fp8_layer_for_marlin
|
||||
from sglang.srt.layers.quantization.mxfp8_input import Mxfp8SwizzledInput
|
||||
from sglang.srt.layers.quantization.unquant import (
|
||||
UnquantizedFusedMoEMethod,
|
||||
UnquantizedLinearMethod,
|
||||
@@ -282,6 +287,7 @@ class Fp8Config(QuantizationConfig):
|
||||
self.packed_modules_mapping = packed_modules_mapping or {}
|
||||
self.use_mxfp8 = use_mxfp8
|
||||
self.kv_cache_quant_algo = kv_cache_quant_algo
|
||||
# "ue8m0" checkpoints quantize activations with power-of-two scales.
|
||||
self.scale_fmt = scale_fmt
|
||||
if weight_block_size is not None:
|
||||
if not is_checkpoint_fp8_serialized:
|
||||
@@ -514,7 +520,26 @@ class Fp8LinearMethod(LinearMethodBase):
|
||||
self.mxfp8_dense_backend = resolve_mxfp8_dense_gemm_backend()
|
||||
self.w8a8_mxfp8_linear = dispatch_w8a8_mxfp8_linear()
|
||||
else:
|
||||
self.w8a8_block_fp8_linear = dispatch_w8a8_block_fp8_linear()
|
||||
# Dispatch on the block size the weight will have after loading: an
|
||||
# MXFP8 checkpoint converted to block-fp8 ends up as [128, 128].
|
||||
effective_block_size = (
|
||||
[128, 128] if self.convert_mxfp8_to_block else self.weight_block_size
|
||||
)
|
||||
self.w8a8_block_fp8_linear = dispatch_w8a8_block_fp8_linear(
|
||||
weight_block_size=effective_block_size,
|
||||
act_scale_ue8m0=isinstance(self.quant_config, Fp8Config)
|
||||
and self.quant_config.scale_fmt == "ue8m0",
|
||||
)
|
||||
# Method-wide gate; a layer that cannot take the MXFP8 view stays on the
|
||||
# block kernel (see _prepare_block_fp8_as_mxfp8).
|
||||
self.block_fp8_as_mxfp8 = not self.use_mxfp8 and can_serve_block_fp8_as_mxfp8(
|
||||
self.weight_block_size, getattr(self.quant_config, "scale_fmt", None)
|
||||
)
|
||||
if self.block_fp8_as_mxfp8:
|
||||
self.mxfp8_dense_backend = resolve_block_fp8_mxfp8_backend()
|
||||
self.w8a8_mxfp8_linear = dispatch_block_fp8_mxfp8_linear(
|
||||
self.mxfp8_dense_backend
|
||||
)
|
||||
self.is_checkpoint_fp8_serialized = (
|
||||
self.quant_config.is_checkpoint_fp8_serialized
|
||||
)
|
||||
@@ -768,17 +793,19 @@ class Fp8LinearMethod(LinearMethodBase):
|
||||
|
||||
layer.weight.data = weight.data
|
||||
layer.weight_scale_inv.data = weight_scale.data
|
||||
if self.block_fp8_as_mxfp8:
|
||||
self._prepare_block_fp8_as_mxfp8(layer)
|
||||
|
||||
# The preshuffle rewrites the weight into a layout only
|
||||
# aiter_w8a8_block_fp8_linear can read, so it is correct exactly when
|
||||
# this quant method is what consumes the weight. A layer whose weight is
|
||||
# read directly by the model (DeepSeek-V4 wo_a, whose absorb GEMM takes
|
||||
# .weight/.weight_scale_inv and runs its own batched kernel) sets
|
||||
# skip_aiter_bpreshuffle and keeps the plain row-major layout.
|
||||
# keep_plain_weight_layout and keeps the plain row-major layout.
|
||||
if (
|
||||
_use_aiter_bpreshuffle_gfx95
|
||||
and self.w8a8_block_fp8_linear is aiter_w8a8_block_fp8_linear
|
||||
and not getattr(layer, "skip_aiter_bpreshuffle", False)
|
||||
and not getattr(layer, "keep_plain_weight_layout", False)
|
||||
):
|
||||
n, k = layer.weight.shape
|
||||
if not use_aiter_triton_gemm_w8a8_tuned_gfx950(n, k):
|
||||
@@ -817,8 +844,30 @@ class Fp8LinearMethod(LinearMethodBase):
|
||||
with torch.no_grad():
|
||||
layer.weight_scale_inv.set_(scale_reordered)
|
||||
|
||||
def _process_mxfp8_linear_weight_scale(self, layer: Module) -> None:
|
||||
if not self.use_mxfp8:
|
||||
def _prepare_block_fp8_as_mxfp8(self, layer: Module) -> None:
|
||||
layer.block_fp8_mxfp8_ready = False
|
||||
if getattr(layer, "keep_plain_weight_layout", False):
|
||||
# The model reads .weight / .weight_scale_inv directly.
|
||||
return
|
||||
n, k = layer.weight.shape
|
||||
if k % 32 != 0:
|
||||
return
|
||||
try:
|
||||
scale_u8 = block_fp8_scale_to_mxfp8_e8m0(
|
||||
layer.weight_scale_inv.data, (n, k), self.weight_block_size
|
||||
)
|
||||
except ValueError as e:
|
||||
logger.warning("Block-fp8 layer stays on the Triton kernel: %s", e)
|
||||
return
|
||||
# weight_scale_inv stays in place for the Triton fallback and raw readers;
|
||||
# the swizzled copy is stored separately.
|
||||
self._process_mxfp8_linear_weight_scale(layer, scale_u8=scale_u8)
|
||||
layer.block_fp8_mxfp8_ready = True
|
||||
|
||||
def _process_mxfp8_linear_weight_scale(
|
||||
self, layer: Module, scale_u8: Optional[torch.Tensor] = None
|
||||
) -> None:
|
||||
if not (self.use_mxfp8 or scale_u8 is not None):
|
||||
return
|
||||
|
||||
backend = self.mxfp8_dense_backend
|
||||
@@ -826,7 +875,8 @@ class Fp8LinearMethod(LinearMethodBase):
|
||||
from flashinfer import shuffle_matrix_a, shuffle_matrix_sf_a
|
||||
|
||||
weight = layer.weight.data
|
||||
scale_u8 = layer.weight_scale_inv.data
|
||||
if scale_u8 is None:
|
||||
scale_u8 = layer.weight_scale_inv.data
|
||||
n, k = weight.shape
|
||||
epilogue_tile_m = 128
|
||||
sf_cols = k // 32
|
||||
@@ -866,7 +916,8 @@ class Fp8LinearMethod(LinearMethodBase):
|
||||
elif backend.is_flashinfer_cutlass() or backend.is_flashinfer_cutedsl():
|
||||
from flashinfer import block_scale_interleave
|
||||
|
||||
scale_u8 = layer.weight_scale_inv.data
|
||||
if scale_u8 is None:
|
||||
scale_u8 = layer.weight_scale_inv.data
|
||||
# block_scale_interleave may pad and/or reshape scales,
|
||||
# so store swizzled scales separately to keep weight update working
|
||||
copy_or_rebind_param(
|
||||
@@ -880,7 +931,8 @@ class Fp8LinearMethod(LinearMethodBase):
|
||||
)
|
||||
|
||||
n, k = layer.weight.shape
|
||||
scale_u8 = layer.weight_scale_inv.data
|
||||
if scale_u8 is None:
|
||||
scale_u8 = layer.weight_scale_inv.data
|
||||
layer.weight_scale_inv_swizzled = None
|
||||
if n % 64 != 0 or k % 128 != 0:
|
||||
if not (get_platform().is_blackwell and is_flashinfer_available()):
|
||||
@@ -1084,7 +1136,22 @@ class Fp8LinearMethod(LinearMethodBase):
|
||||
bias=bias,
|
||||
)
|
||||
|
||||
if self.use_mxfp8:
|
||||
mxfp8_view = self.use_mxfp8 or (
|
||||
self.block_fp8_as_mxfp8 and layer.block_fp8_mxfp8_ready
|
||||
)
|
||||
if isinstance(x, Mxfp8SwizzledInput):
|
||||
if not mxfp8_view or not (
|
||||
self.mxfp8_dense_backend.is_flashinfer_cutlass()
|
||||
or self.mxfp8_dense_backend.is_flashinfer_cutedsl()
|
||||
):
|
||||
raise ValueError(
|
||||
"Mxfp8SwizzledInput needs a layer with an MXFP8 view on a "
|
||||
"FlashInfer CUTLASS / CuTe-DSL backend"
|
||||
)
|
||||
elif self.block_fp8_as_mxfp8 and isinstance(x, tuple):
|
||||
# A legacy (q, scale) block-fp8 pair keeps the block kernel.
|
||||
mxfp8_view = False
|
||||
if mxfp8_view:
|
||||
backend = self.mxfp8_dense_backend
|
||||
extra_kwargs = {}
|
||||
if backend.is_flashinfer_cutlass() or backend.is_flashinfer_cutedsl():
|
||||
|
||||
@@ -574,7 +574,10 @@ if get_platform().is_sm90 and is_flashinfer_available():
|
||||
from flashinfer.gemm import fp8_blockscale_gemm_sm90
|
||||
|
||||
|
||||
def dispatch_w8a8_block_fp8_linear() -> Callable:
|
||||
def dispatch_w8a8_block_fp8_linear(
|
||||
weight_block_size: Optional[List[int]] = None,
|
||||
act_scale_ue8m0: bool = False,
|
||||
) -> Callable:
|
||||
"""
|
||||
Dispatch to the appropriate FP8 block linear implementation.
|
||||
|
||||
@@ -582,6 +585,11 @@ def dispatch_w8a8_block_fp8_linear() -> Callable:
|
||||
1. The --fp8-gemm-backend server argument (preferred)
|
||||
2. Auto-detection based on hardware capabilities
|
||||
"""
|
||||
# Only Triton reads the block size at launch; DeepGEMM, the FlashInfer
|
||||
# groupwise kernels and CUTLASS take 128-wide K blocks only.
|
||||
if weight_block_size is not None and weight_block_size[1] != 128:
|
||||
return partial(triton_w8a8_block_fp8_linear, act_scale_ue8m0=act_scale_ue8m0)
|
||||
|
||||
backend = get_fp8_gemm_runner_backend()
|
||||
|
||||
# Handle explicit backend selection via --fp8-gemm-backend
|
||||
@@ -710,6 +718,68 @@ def _unsupported_mxfp8_linear(*args, **kwargs) -> torch.Tensor:
|
||||
)
|
||||
|
||||
|
||||
def resolve_block_fp8_mxfp8_backend() -> Mxfp8DenseGemmBackend:
|
||||
"""The FlashInfer MXFP8 backend a 32-wide-K ue8m0 block-fp8 weight can run on."""
|
||||
backend = get_fp8_gemm_runner_backend()
|
||||
# Explicit CUTLASS / CuTe-DSL only: they leave the weight untouched and store
|
||||
# the swizzled scale separately, so the block layout stays readable by Triton.
|
||||
if not (backend.is_flashinfer_cutedsl() or backend.is_flashinfer_cutlass()):
|
||||
return Mxfp8DenseGemmBackend.UNSUPPORTED
|
||||
if not (_is_cuda and get_platform().is_blackwell and is_flashinfer_available()):
|
||||
return Mxfp8DenseGemmBackend.UNSUPPORTED
|
||||
resolved = resolve_mxfp8_dense_gemm_backend()
|
||||
return resolved if resolved.is_flashinfer() else Mxfp8DenseGemmBackend.UNSUPPORTED
|
||||
|
||||
|
||||
def can_serve_block_fp8_as_mxfp8(
|
||||
weight_block_size: Optional[List[int]], scale_fmt: Optional[str]
|
||||
) -> bool:
|
||||
"""Whether a block-fp8 linear can run on the MXFP8 dense GEMMs instead of Triton:
|
||||
a 32-wide-K ue8m0 block weight is an MXFP8 operand (block_fp8_scale_to_mxfp8_e8m0)."""
|
||||
if weight_block_size is None or len(weight_block_size) != 2:
|
||||
return False
|
||||
if weight_block_size[1] != 32 or scale_fmt != "ue8m0":
|
||||
return False
|
||||
return not resolve_block_fp8_mxfp8_backend().is_unsupported()
|
||||
|
||||
|
||||
def dispatch_block_fp8_mxfp8_linear(backend: Mxfp8DenseGemmBackend) -> Callable:
|
||||
"""The MXFP8 linear for a block-fp8 weight served as MXFP8."""
|
||||
if backend.is_flashinfer_cutlass():
|
||||
return partial(flashinfer_mxfp8_blockscaled_linear, backend="cutlass")
|
||||
if backend.is_flashinfer_cutedsl():
|
||||
return partial(flashinfer_mxfp8_blockscaled_linear, backend="cute-dsl")
|
||||
return _unsupported_mxfp8_linear
|
||||
|
||||
|
||||
def block_fp8_scale_to_mxfp8_e8m0(
|
||||
weight_scale: torch.Tensor,
|
||||
weight_shape: Tuple[int, int],
|
||||
weight_block_size: List[int],
|
||||
) -> torch.Tensor:
|
||||
"""Expand fp32 power-of-two block scales [ceil(N / bn), K // 32] into the MXFP8
|
||||
per-row e8m0 layout [N, K // 32] (uint8 exponent bytes), bit-exact."""
|
||||
n, k = weight_shape
|
||||
block_n, block_k = weight_block_size
|
||||
if block_k != 32 or k % 32 != 0:
|
||||
raise ValueError(
|
||||
f"MXFP8 needs a 32-wide K block and K % 32 == 0, got {block_k=} {k=}"
|
||||
)
|
||||
scale = weight_scale.detach().float().contiguous()
|
||||
if tuple(scale.shape) != (ceil_div(n, block_n), k // 32):
|
||||
raise ValueError(
|
||||
f"unexpected block scale shape {tuple(scale.shape)} for weight {weight_shape}"
|
||||
)
|
||||
bits = scale.view(torch.int32)
|
||||
# A positive normal power of two has a zero mantissa; its exponent field is the e8m0 code.
|
||||
if not bool(torch.all((bits & 0x7FFFFF) == 0)) or not bool(torch.all(scale > 0)):
|
||||
raise ValueError(
|
||||
"block scales are not positive powers of two; cannot encode as e8m0"
|
||||
)
|
||||
e8m0 = (bits >> 23).to(torch.uint8)
|
||||
return e8m0.repeat_interleave(block_n, dim=0)[:n].contiguous()
|
||||
|
||||
|
||||
def dispatch_w8a8_mxfp8_linear() -> Callable:
|
||||
backend = resolve_mxfp8_dense_gemm_backend()
|
||||
if backend.is_deep_gemm():
|
||||
@@ -1334,6 +1404,7 @@ def triton_w8a8_block_fp8_linear(
|
||||
weight_scale: torch.Tensor,
|
||||
input_scale: Optional[torch.Tensor] = None,
|
||||
bias: Optional[torch.Tensor] = None,
|
||||
act_scale_ue8m0: bool = False,
|
||||
) -> torch.Tensor:
|
||||
if input_scale is not None:
|
||||
# Pre-quantized input: ``input`` is already fp8 and ``input_scale`` is
|
||||
@@ -1348,9 +1419,15 @@ def triton_w8a8_block_fp8_linear(
|
||||
input_2d = input.view(-1, input.shape[-1])
|
||||
output_dtype = input_2d.dtype
|
||||
output_shape = [*input.shape[:-1], weight.shape[0]]
|
||||
q_input, x_scale = per_token_group_quant_fp8(
|
||||
input_2d, block_size[1], column_major_scales=False
|
||||
)
|
||||
if act_scale_ue8m0:
|
||||
# Power-of-two scales in fp32 storage, as ue8m0 checkpoints quantize.
|
||||
q_input, x_scale = sglang_per_token_group_quant_fp8(
|
||||
input_2d, block_size[1], scale_ue8m0=True
|
||||
)
|
||||
else:
|
||||
q_input, x_scale = per_token_group_quant_fp8(
|
||||
input_2d, block_size[1], column_major_scales=False
|
||||
)
|
||||
|
||||
output = w8a8_block_fp8_matmul_triton(
|
||||
q_input, weight, x_scale, weight_scale, block_size, output_dtype=output_dtype
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
"""Explicit input layout for a linear consuming prequantized MXFP8 activations."""
|
||||
|
||||
from typing import NamedTuple
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
class Mxfp8SwizzledInput(NamedTuple):
|
||||
"""E4M3 activations and UE8M0 scales in FlashInfer's 128x4 layout.
|
||||
|
||||
A plain FP8 tuple may contain block-FP8 scales with a different layout.
|
||||
This marker lets a converted block-FP8 linear distinguish the two.
|
||||
"""
|
||||
|
||||
data: torch.Tensor
|
||||
scales: torch.Tensor
|
||||
@@ -777,7 +777,7 @@ class MqaAttentionBase(nn.Module):
|
||||
# that is aiter's B-preshuffle, which silently permutes the weight
|
||||
# in place (same shape, dtype and strides) and makes this GEMM
|
||||
# return noise.
|
||||
self.wo_a.skip_aiter_bpreshuffle = True
|
||||
self.wo_a.keep_plain_weight_layout = True
|
||||
self.wo_b = RowParallelLinear(
|
||||
self.n_groups * self.o_lora_rank,
|
||||
self.hidden_size,
|
||||
@@ -3559,7 +3559,7 @@ class DeepseekV4ForCausalLM(nn.Module):
|
||||
# ROCm: aiter's mxscale GEMM reads uint8 e8m0 block scales, and
|
||||
# requantizes the weight when the checkpoint's scales are not
|
||||
# already powers of two. It also needs the weight row-major, so
|
||||
# check the linear method honoured skip_aiter_bpreshuffle: a
|
||||
# check the linear method honoured keep_plain_weight_layout: a
|
||||
# preshuffled weight has the same shape, dtype and strides and
|
||||
# would only show up as garbage output.
|
||||
assert not getattr(attn.wo_a, "aiter_bpreshuffled", False), (
|
||||
|
||||
Reference in New Issue
Block a user