[Bugfix] Fix DeepGEMM FP8 warmup coverage (#56452)

Signed-off-by: Ayushman Singh <40520701+ayush1399@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Ayushman Singh
2026-09-12 14:20:38 -04:00
committed by GitHub
co-authored by mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 29332cf936
commit 7bcca16729
4 changed files with 140 additions and 99 deletions
@@ -38,6 +38,22 @@ class FP8BlockParams(FP8Params):
input_scale_ub=getattr(layer, cls.INPUT_SCALE_UB, None),
)
@property
def block_scale_attr(self) -> str:
"""Fp8LinearMethod registers the block scale as ``weight_scale_inv``,
compressed-tensors as ``weight_scale``."""
return (
self.WEIGHT_SCALE
if self.weight_scale_inv is None
else self.WEIGHT_SCALE_INV
)
@property
def block_scale(self) -> torch.Tensor:
scale = getattr(self, self.block_scale_attr)
assert scale is not None
return scale
class Fp8BlockScaledMMLinearKernel(
MMLinearKernel[FP8ScaledMMLinearLayerConfig, FP8BlockParams], ABC
@@ -74,25 +90,13 @@ class Fp8BlockScaledMMLinearKernel(
def process_weights_after_loading(self, layer: torch.nn.Module):
params = self._get_layer_params(layer)
# Fp8LinearMethod registered weight scale
# buffer as weight_scale_inv unlike compressed tensors.
weight_scale = (
params.weight_scale
if params.weight_scale_inv is None
else params.weight_scale_inv
)
scale_attr_name = (
params.WEIGHT_SCALE
if params.weight_scale_inv is None
else params.WEIGHT_SCALE_INV
)
new_weight, new_weight_scale = process_fp8_weight_block_strategy(
params.weight,
weight_scale,
params.block_scale,
)
replace_parameter(layer, params.WEIGHT, new_weight.data)
replace_parameter(layer, scale_attr_name, new_weight_scale.data)
replace_parameter(layer, params.block_scale_attr, new_weight_scale.data)
def apply_weights(
self,
@@ -104,11 +108,7 @@ class Fp8BlockScaledMMLinearKernel(
out_dtype = self.config.out_dtype
params = self._get_layer_params(layer)
weight = params.weight
weight_scale = (
params.weight_scale
if params.weight_scale_inv is None
else params.weight_scale_inv
)
weight_scale = params.block_scale
input_scale = params.input_scale
scale_up = params.input_scale_ub
@@ -86,25 +86,28 @@ class DeepGemmFp8BlockScaledMMKernel(Fp8BlockScaledMMLinearKernel):
params = self._get_layer_params(layer)
assert layer.weight_block_size is not None
is_bmm = getattr(layer, "is_bmm", False)
if self.is_deep_gemm_supported:
weight_scale_invs = params.weight_scale_inv
scale_attr = (
params.WEIGHT_SCALE_INV
if weight_scale_invs is not None
else params.WEIGHT_SCALE
)
dg_weight, dg_weight_scale = deepgemm_post_process_fp8_weight_block(
wq=params.weight,
ws=weight_scale_invs
if weight_scale_invs is not None
else params.weight_scale,
ws=params.block_scale,
quant_block_shape=tuple(layer.weight_block_size),
use_e8m0=self.use_deep_gemm_e8m0,
is_bmm=getattr(layer, "is_bmm", False),
is_bmm=is_bmm,
bmm_batch_size=getattr(layer, "bmm_batch_size", 0),
)
replace_parameter(layer, params.WEIGHT, dg_weight)
replace_parameter(layer, scale_attr, dg_weight_scale)
replace_parameter(layer, params.block_scale_attr, dg_weight_scale)
# bmm layers bypass this kernel and run through fp8_einsum.
if not is_bmm:
layer.deep_gemm_warmup_provider = self
def get_deep_gemm_warmup_weights(
self, layer: torch.nn.Module
) -> tuple[torch.Tensor, torch.Tensor]:
params = self._get_layer_params(layer)
return params.weight, params.block_scale
def apply_block_scaled_mm(
self,
+17 -69
View File
@@ -22,9 +22,6 @@ from vllm.model_executor.layers.fused_moe.experts.deep_gemm_moe import DeepGemmE
from vllm.model_executor.layers.fused_moe.experts.triton_deep_gemm_moe import (
TritonOrDeepGemmExperts,
)
from vllm.model_executor.layers.linear import LinearBase
from vllm.model_executor.layers.quantization.fp8 import Fp8LinearMethod
from vllm.model_executor.layers.quantization.online.mxfp8 import Mxfp8OnlineLinearMethod
from vllm.tracing import instrument
from vllm.utils.deep_gemm import (
fp8_gemm_nt,
@@ -82,28 +79,6 @@ def _generate_optimal_warmup_m_values(
return sorted(m_values)
def _extract_data_from_linear_base_module(
m: torch.nn.Module,
) -> tuple[torch.Tensor, torch.Tensor, list[int]]:
"""
Extract weights, weight scales and quantization block sizes from the given
LinearBase module.
"""
assert isinstance(m, LinearBase)
assert isinstance(m.quant_method, Fp8LinearMethod)
assert m.quant_method.block_quant
assert m.quant_method.quant_config is not None
w = m.weight
ws = m.weight_scale_inv if hasattr(m, "weight_scale_inv") else m.weight_scale
quant_block_size = m.quant_method.quant_config.weight_block_size
assert isinstance(w, torch.Tensor)
assert isinstance(ws, torch.Tensor)
assert quant_block_size is not None
return (w, ws, quant_block_size)
def _extract_data_from_fused_moe_module(
m_: torch.nn.Module,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, int]:
@@ -133,45 +108,19 @@ def _extract_data_from_fused_moe_module(
return w13, w13_s, w2, w2_s, num_topk
def _is_deep_gemm_backed_kernel(fp8_linear: object) -> bool:
def _deep_gemm_linear_data(
module: torch.nn.Module,
) -> tuple[torch.Tensor, torch.Tensor] | None:
"""
Return True if the selected linear kernel dispatches to DeepGEMM, either
directly or as the fallback branch of a dynamic wrapper.
Return (weight, weight_scale) for a layer whose kernel stamped itself as
``deep_gemm_warmup_provider`` in ``process_weights_after_loading``, else None.
"""
if isinstance(fp8_linear, DeepGemmFp8BlockScaledMMKernel):
return True
return isinstance(
getattr(fp8_linear, "fallback", None), DeepGemmFp8BlockScaledMMKernel
)
def _fp8_linear_may_use_deep_gemm(module: torch.nn.Module) -> bool:
"""
Return True if the input module/layer could be processed with DeepGEMM.
"""
if not (
isinstance(module, LinearBase)
and isinstance(module.quant_method, Fp8LinearMethod)
and not isinstance(module.quant_method, Mxfp8OnlineLinearMethod)
and getattr(module.quant_method, "block_quant", False)
and not getattr(module.quant_method, "use_marlin", True)
):
return False
fp8_linear = getattr(module.quant_method, "fp8_linear", None)
if not _is_deep_gemm_backed_kernel(fp8_linear):
return False
block_size = get_mk_alignment_for_contiguous_layout()[0]
w, _, block_sizes = _extract_data_from_linear_base_module(module)
return (
block_sizes == get_mk_alignment_for_contiguous_layout()
and w.ndim == 2
and w.shape[0] % block_size == 0
and w.shape[1] % block_size == 0
)
provider = getattr(module, "deep_gemm_warmup_provider", None)
if not isinstance(provider, DeepGemmFp8BlockScaledMMKernel):
return None
if module.weight_block_size != get_mk_alignment_for_contiguous_layout():
return None
return provider.get_deep_gemm_warmup_weights(module)
def _fused_moe_grouped_gemm_may_use_deep_gemm(module: torch.nn.Module) -> bool:
@@ -361,11 +310,10 @@ def _deepgemm_grouped_fp8_gemm_nt_contiguous_warmup(
def deepgemm_fp8_gemm_nt_warmup(
model: torch.nn.Module, max_tokens: int, pbar: tqdm | None = None
):
dg_modules = [m for m in model.modules() if _fp8_linear_may_use_deep_gemm(m)]
for dgm in dg_modules:
w, ws, _ = _extract_data_from_linear_base_module(dgm)
_deepgemm_fp8_gemm_nt_warmup(w=w, ws=ws, max_tokens=max_tokens, pbar=pbar)
for m in model.modules():
if (data := _deep_gemm_linear_data(m)) is not None:
w, ws = data
_deepgemm_fp8_gemm_nt_warmup(w=w, ws=ws, max_tokens=max_tokens, pbar=pbar)
def deepgemm_grouped_fp8_gemm_nt_contiguous_warmup(
@@ -392,8 +340,8 @@ def _count_warmup_iterations(model: torch.nn.Module, max_tokens: int) -> int:
total = 0
for m in model.modules():
if _fp8_linear_may_use_deep_gemm(m):
w, _, _ = _extract_data_from_linear_base_module(m)
if (data := _deep_gemm_linear_data(m)) is not None:
w, _ = data
if w.size() not in seen_fp8_sizes:
total += len(_get_fp8_gemm_nt_m_values(w, max_tokens))
seen_fp8_sizes.add(w.size())