[Fast Start] Support fp4 (#55465)
Signed-off-by: Isotr0py <Isotr0py@outlook.com> Co-authored-by: Isotr0py <Isotr0py@outlook.com>
This commit is contained in:
@@ -40,6 +40,7 @@ from vllm.model_executor.layers.fused_moe.oracle.mxfp8 import (
|
||||
select_mxfp8_moe_backend,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.oracle.nvfp4 import (
|
||||
NvFp4MoeBackend,
|
||||
convert_to_nvfp4_moe_kernel_format,
|
||||
is_global_sf_supported_for_nvfp4_backend,
|
||||
make_nvfp4_moe_kernel,
|
||||
@@ -100,7 +101,11 @@ from vllm.model_executor.parameter import (
|
||||
ModelWeightParameter,
|
||||
PerTensorScaleParameter,
|
||||
)
|
||||
from vllm.model_executor.utils import replace_parameter, set_weight_attrs
|
||||
from vllm.model_executor.utils import (
|
||||
is_weights_pre_processed,
|
||||
replace_parameter,
|
||||
set_weight_attrs,
|
||||
)
|
||||
from vllm.utils.math_utils import cdiv
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -819,6 +824,8 @@ class ModelOptNvFp4FusedMoE(FusedMoEMethodBase):
|
||||
quant_config: NVFP4 Quant Config
|
||||
"""
|
||||
|
||||
supports_pre_processed_weights = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
quant_config: ModelOptNvFp4Config,
|
||||
@@ -969,6 +976,15 @@ class ModelOptNvFp4FusedMoE(FusedMoEMethodBase):
|
||||
"""
|
||||
Convert NVFP4 MoE weights into kernel format and setup the kernel.
|
||||
"""
|
||||
if is_weights_pre_processed():
|
||||
if self.nvfp4_backend != NvFp4MoeBackend.FLASHINFER_TRTLLM:
|
||||
raise RuntimeError(
|
||||
"pre-processed weights require FLASHINFER_TRTLLM backend, "
|
||||
f"moe backend, got {self.nvfp4_backend}"
|
||||
)
|
||||
self._restore_padded_moe_dims(layer)
|
||||
self._build_moe_kernel(layer)
|
||||
return
|
||||
|
||||
# Use a single gscale for w13.
|
||||
if self.moe.is_act_and_mul and not torch.allclose(
|
||||
@@ -1013,7 +1029,10 @@ class ModelOptNvFp4FusedMoE(FusedMoEMethodBase):
|
||||
replace_parameter(layer, "w2_weight_scale_2", w2_scale_2)
|
||||
replace_parameter(layer, "w2_input_scale", a2_scale)
|
||||
|
||||
# Setup modular kernel.
|
||||
self._build_moe_kernel(layer)
|
||||
|
||||
def _build_moe_kernel(self, layer: RoutedExperts) -> None:
|
||||
"""Build the modular MoE kernel from the (already in-format) weights."""
|
||||
self.moe_quant_config = self.get_fused_moe_quant_config(layer)
|
||||
assert self.experts_cls is not None
|
||||
self.moe_kernel = make_nvfp4_moe_kernel(
|
||||
@@ -1025,6 +1044,16 @@ class ModelOptNvFp4FusedMoE(FusedMoEMethodBase):
|
||||
)
|
||||
self.moe_kernel.fused_experts.process_weights_after_loading(layer)
|
||||
|
||||
def _restore_padded_moe_dims(self, layer: RoutedExperts) -> None:
|
||||
"""Recover the padded ``moe_config`` dims from the exported weights."""
|
||||
mc = layer.moe_config
|
||||
padded_hidden = layer.w2_weight.shape[1]
|
||||
if padded_hidden != mc.hidden_dim:
|
||||
if mc.hidden_dim_unpadded is None:
|
||||
mc.hidden_dim_unpadded = mc.hidden_dim
|
||||
mc.hidden_dim = padded_hidden
|
||||
mc.intermediate_size_per_partition = layer.w2_weight.shape[2] * 2
|
||||
|
||||
def get_fused_moe_quant_config(self, layer: RoutedExperts) -> FusedMoEQuantConfig:
|
||||
return make_nvfp4_moe_quant_config(
|
||||
backend=self.nvfp4_backend,
|
||||
@@ -2438,6 +2467,12 @@ class ModelOptLinearMethod(LinearMethodBase):
|
||||
# NVFP4 methods.
|
||||
self.kernel: Any = None
|
||||
|
||||
@property
|
||||
def supports_pre_processed_weights(self) -> bool: # type: ignore[override]
|
||||
# TODO(Isotr0py): support fp8/mxfp8 ModelOpt kernels transpose/repack.
|
||||
w = self.spec.weight
|
||||
return isinstance(w, QuantKey) and w.dtype == FP4_DTYPE
|
||||
|
||||
def create_weights(
|
||||
self,
|
||||
layer,
|
||||
@@ -2476,6 +2511,8 @@ class ModelOptLinearMethod(LinearMethodBase):
|
||||
expose_input_quant_key(layer, self.kernel)
|
||||
|
||||
def process_weights_after_loading(self, layer) -> None:
|
||||
if is_weights_pre_processed():
|
||||
return
|
||||
self.fmt.pre_process(layer)
|
||||
self.wkey.process(layer, WEIGHT)
|
||||
if self.akey:
|
||||
|
||||
@@ -36,7 +36,11 @@ from vllm.model_executor.layers.quantization.base_config import (
|
||||
QuantizeMethodBase,
|
||||
)
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import is_layer_skipped
|
||||
from vllm.model_executor.utils import replace_parameter, set_weight_attrs
|
||||
from vllm.model_executor.utils import (
|
||||
is_weights_pre_processed,
|
||||
replace_parameter,
|
||||
set_weight_attrs,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -474,6 +478,8 @@ class GptOssMxfp4MoEMethod(FusedMoEMethodBase):
|
||||
class Mxfp4MoEMethod(FusedMoEMethodBase):
|
||||
"""MXFP4 MoE quantization method."""
|
||||
|
||||
supports_pre_processed_weights = True
|
||||
|
||||
def __init__(self, moe: FusedMoEConfig):
|
||||
super().__init__(moe)
|
||||
|
||||
@@ -760,9 +766,11 @@ class Mxfp4MoEMethod(FusedMoEMethodBase):
|
||||
replace_parameter(layer, "w2_bias", w2_bias)
|
||||
|
||||
# Build quant config
|
||||
self.moe_quant_config = self.get_fused_moe_quant_config(layer)
|
||||
self._build_moe_kernel(layer)
|
||||
|
||||
# Build kernel (modular or monolithic)
|
||||
def _build_moe_kernel(self, layer: RoutedExperts) -> None:
|
||||
"""Build the modular MoE kernel from the (already in-format) weights."""
|
||||
self.moe_quant_config = self.get_fused_moe_quant_config(layer)
|
||||
if self.moe_quant_config is not None and self.experts_cls is not None:
|
||||
self.moe_kernel = make_mxfp4_moe_kernel(
|
||||
moe_quant_config=self.moe_quant_config,
|
||||
@@ -774,6 +782,18 @@ class Mxfp4MoEMethod(FusedMoEMethodBase):
|
||||
self.moe_kernel.fused_experts.process_weights_after_loading(layer)
|
||||
|
||||
def process_weights_after_loading(self, layer):
|
||||
if self.mxfp4_backend == Mxfp4MoeBackend.NONE:
|
||||
return
|
||||
|
||||
if is_weights_pre_processed():
|
||||
if self.mxfp4_backend != Mxfp4MoeBackend.FLASHINFER_TRTLLM_MXFP4_MXFP8:
|
||||
raise RuntimeError(
|
||||
"pre-processed weights require FLASHINFER_TRTLLM_MXFP4_MXFP8 "
|
||||
f"moe backend, got {self.mxfp4_backend}"
|
||||
)
|
||||
self._build_moe_kernel(layer)
|
||||
return
|
||||
|
||||
w13 = layer.w13_weight
|
||||
w2 = layer.w2_weight
|
||||
w13_scale = layer.w13_weight_scale
|
||||
@@ -781,9 +801,6 @@ class Mxfp4MoEMethod(FusedMoEMethodBase):
|
||||
w13_bias = getattr(layer, "w13_bias", None)
|
||||
w2_bias = getattr(layer, "w2_bias", None)
|
||||
|
||||
if self.mxfp4_backend == Mxfp4MoeBackend.NONE:
|
||||
return
|
||||
|
||||
self._setup_kernel(layer, w13, w2, w13_scale, w2_scale, w13_bias, w2_bias)
|
||||
|
||||
def get_fused_moe_quant_config(
|
||||
|
||||
@@ -98,6 +98,14 @@ def pad_nvfp4_weight_for_cutlass(
|
||||
return weight, weights_padding_bytes
|
||||
|
||||
|
||||
def nvfp4_weight_padding_bytes(layer: torch.nn.Module) -> int:
|
||||
"""
|
||||
Compute K-dim padding (packed bytes) of an NVFP4 weight,
|
||||
Aligned with `pad_nvfp4_weight_for_cutlass` above.
|
||||
"""
|
||||
return layer.weight.shape[1] - layer.input_size_per_partition // 2
|
||||
|
||||
|
||||
def pad_nvfp4_activation_for_cutlass(
|
||||
x_fp4: torch.Tensor,
|
||||
weights_padding_bytes: int,
|
||||
|
||||
Reference in New Issue
Block a user