[MoE Refactor] WNA16 MoE backend selection into oracle module (#42553)
Signed-off-by: Bill Nell <bnell@redhat.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -869,19 +869,23 @@ def nvfp4_w4a16_moe_quant_config(
|
||||
def int4_w4a16_moe_quant_config(
|
||||
w1_scale: torch.Tensor,
|
||||
w2_scale: torch.Tensor,
|
||||
w1_zp: torch.Tensor | None,
|
||||
w2_zp: torch.Tensor | None,
|
||||
w1_zp: torch.Tensor | None = None,
|
||||
w2_zp: torch.Tensor | None = None,
|
||||
w1_bias: torch.Tensor | None = None,
|
||||
w2_bias: torch.Tensor | None = None,
|
||||
block_shape: list[int] | None = None,
|
||||
a1_gscale: torch.Tensor | None = None,
|
||||
a2_gscale: torch.Tensor | None = None,
|
||||
) -> FusedMoEQuantConfig:
|
||||
"""
|
||||
Construct a quant config for 16-bit float activations and int4 weights.
|
||||
"""
|
||||
group_shape = GroupShape(*block_shape) if block_shape is not None else None
|
||||
return FusedMoEQuantConfig(
|
||||
_a1=FusedMoEQuantDesc(shape=group_shape),
|
||||
_a2=FusedMoEQuantDesc(shape=group_shape),
|
||||
_w1=FusedMoEQuantDesc("int4", group_shape, w1_scale, None, w1_zp),
|
||||
_w2=FusedMoEQuantDesc("int4", group_shape, w2_scale, None, w2_zp),
|
||||
_a1=FusedMoEQuantDesc(shape=group_shape, alpha_or_gscale=a1_gscale),
|
||||
_a2=FusedMoEQuantDesc(shape=group_shape, alpha_or_gscale=a2_gscale),
|
||||
_w1=FusedMoEQuantDesc("int4", group_shape, w1_scale, None, w1_zp, w1_bias),
|
||||
_w2=FusedMoEQuantDesc("int4", group_shape, w2_scale, None, w2_zp, w2_bias),
|
||||
)
|
||||
|
||||
|
||||
@@ -922,19 +926,21 @@ def fp8_w8a16_moe_quant_config(
|
||||
def int8_w8a16_moe_quant_config(
|
||||
w1_scale: torch.Tensor,
|
||||
w2_scale: torch.Tensor,
|
||||
w1_zp: torch.Tensor | None,
|
||||
w2_zp: torch.Tensor | None,
|
||||
w1_zp: torch.Tensor | None = None,
|
||||
w2_zp: torch.Tensor | None = None,
|
||||
w1_bias: torch.Tensor | None = None,
|
||||
w2_bias: torch.Tensor | None = None,
|
||||
block_shape: list[int] | None = None,
|
||||
a1_gscale: torch.Tensor | None = None,
|
||||
a2_gscale: torch.Tensor | None = None,
|
||||
) -> FusedMoEQuantConfig:
|
||||
"""
|
||||
Construct a quant config for 16-bit float activations and int8 weights.
|
||||
"""
|
||||
group_shape = GroupShape(*block_shape) if block_shape is not None else None
|
||||
return FusedMoEQuantConfig(
|
||||
_a1=FusedMoEQuantDesc(shape=group_shape),
|
||||
_a2=FusedMoEQuantDesc(shape=group_shape),
|
||||
_a1=FusedMoEQuantDesc(shape=group_shape, alpha_or_gscale=a1_gscale),
|
||||
_a2=FusedMoEQuantDesc(shape=group_shape, alpha_or_gscale=a2_gscale),
|
||||
_w1=FusedMoEQuantDesc(torch.int8, group_shape, w1_scale, None, w1_zp, w1_bias),
|
||||
_w2=FusedMoEQuantDesc(torch.int8, group_shape, w2_scale, None, w2_zp, w2_bias),
|
||||
)
|
||||
@@ -965,47 +971,6 @@ def int4_w4afp8_moe_quant_config(
|
||||
)
|
||||
|
||||
|
||||
def awq_marlin_moe_quant_config(
|
||||
w1_scale: torch.Tensor,
|
||||
w2_scale: torch.Tensor,
|
||||
w1_zp: torch.Tensor | None,
|
||||
w2_zp: torch.Tensor | None,
|
||||
weight_bits: int,
|
||||
group_size: int,
|
||||
w1_bias: torch.Tensor | None = None,
|
||||
w2_bias: torch.Tensor | None = None,
|
||||
a1_gscale: torch.Tensor | None = None,
|
||||
a2_gscale: torch.Tensor | None = None,
|
||||
) -> FusedMoEQuantConfig:
|
||||
"""
|
||||
Construct a quant config for awq marlin quantization.
|
||||
|
||||
a1_gscale / a2_gscale are optional global scales applied to activation
|
||||
quantization scales when Marlin runs with 8-bit activations.
|
||||
"""
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape
|
||||
|
||||
w_shape = None if group_size == -1 else GroupShape(row=1, col=group_size)
|
||||
|
||||
# Activations are NOT quantized for AWQ (fp16/bf16)
|
||||
a_shape = w_shape # Same as weight shape for alignment
|
||||
|
||||
# Determine weight dtype
|
||||
if weight_bits == 4:
|
||||
weight_dtype = "int4"
|
||||
elif weight_bits == 8:
|
||||
weight_dtype = torch.int8
|
||||
else:
|
||||
raise ValueError(f"Unsupported weight_bits: {weight_bits}")
|
||||
|
||||
return FusedMoEQuantConfig(
|
||||
_a1=FusedMoEQuantDesc(dtype=None, shape=a_shape, alpha_or_gscale=a1_gscale),
|
||||
_a2=FusedMoEQuantDesc(dtype=None, shape=a_shape, alpha_or_gscale=a2_gscale),
|
||||
_w1=FusedMoEQuantDesc(weight_dtype, w_shape, w1_scale, None, w1_zp, w1_bias),
|
||||
_w2=FusedMoEQuantDesc(weight_dtype, w_shape, w2_scale, None, w2_zp, w2_bias),
|
||||
)
|
||||
|
||||
|
||||
def biased_moe_quant_config(
|
||||
w1_bias: torch.Tensor | None,
|
||||
w2_bias: torch.Tensor | None,
|
||||
|
||||
@@ -44,6 +44,7 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
kFp8StaticChannelSym,
|
||||
kFp8StaticTensorSym,
|
||||
kInt4Static,
|
||||
kInt4Static32,
|
||||
kInt8Static,
|
||||
kMxfp4Static,
|
||||
kMxfp8Static,
|
||||
@@ -566,8 +567,9 @@ class MarlinExpertsBase(mk.FusedMoEExpertsModular):
|
||||
quant_config.use_mxfp4_w4a16
|
||||
or quant_config.use_nvfp4_w4a16
|
||||
or quant_config.use_int4_w4a16
|
||||
or quant_config.use_int8_w8a16
|
||||
or quant_config.use_fp8_w8a16
|
||||
), "Supports only {mxfp,nvfp,int}4_w4a16 or fp8_w8a16"
|
||||
), "Supports only {mxfp,nvfp,int}4_w4a16, int8_w8a16 or fp8_w8a16"
|
||||
self.w13_g_idx = w13_g_idx
|
||||
self.w2_g_idx = w2_g_idx
|
||||
self.w13_g_idx_sort_indices = w13_g_idx_sort_indices
|
||||
@@ -608,6 +610,7 @@ class MarlinExpertsBase(mk.FusedMoEExpertsModular):
|
||||
kNvfp4Static,
|
||||
kInt4Static,
|
||||
kInt8Static,
|
||||
kInt4Static32,
|
||||
]
|
||||
return weight_key in SUPPORTED_W
|
||||
|
||||
@@ -640,6 +643,8 @@ class MarlinExpertsBase(mk.FusedMoEExpertsModular):
|
||||
if self.w1_zp is not None or self.w2_zp is not None:
|
||||
return scalar_types.uint4.id
|
||||
return scalar_types.uint4b8.id
|
||||
elif self.quant_config.use_int8_w8a16:
|
||||
return scalar_types.uint8b128.id
|
||||
elif self.quant_config.use_mxfp4_w4a16 or self.quant_config.use_nvfp4_w4a16:
|
||||
return scalar_types.float4_e2m1f.id
|
||||
elif (
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import torch
|
||||
|
||||
import vllm.model_executor.layers.fused_moe.modular_kernel as mk
|
||||
from vllm.model_executor.layers.fused_moe.activation import MoEActivation
|
||||
from vllm.model_executor.layers.fused_moe.config import (
|
||||
FusedMoEConfig,
|
||||
FusedMoEParallelConfig,
|
||||
FusedMoEQuantConfig,
|
||||
RoutingMethodType,
|
||||
)
|
||||
from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
QuantKey,
|
||||
kInt4Static32,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
|
||||
class TrtLlmMxint4ExpertsMonolithic(mk.FusedMoEExpertsMonolithic):
|
||||
"""
|
||||
FlashInfer TRT-LLM MxInt4 MoE kernel. Monolithic interface
|
||||
(fused router + experts).
|
||||
|
||||
Wraps flashinfer_trtllm_mxint4_moe().
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
moe_config: FusedMoEConfig,
|
||||
quant_config: FusedMoEQuantConfig,
|
||||
):
|
||||
super().__init__(moe_config, quant_config)
|
||||
self.topk = moe_config.experts_per_token
|
||||
self.intermediate_size_per_partition = (
|
||||
moe_config.intermediate_size_per_partition
|
||||
)
|
||||
self.local_num_experts = moe_config.num_local_experts
|
||||
self.ep_rank = moe_config.ep_rank
|
||||
self.routing_method = moe_config.routing_method
|
||||
|
||||
@staticmethod
|
||||
def _supports_current_device() -> bool:
|
||||
from vllm.model_executor.layers.quantization.utils.flashinfer_mxint4_moe import ( # noqa: E501
|
||||
is_flashinfer_mxint4_moe_available,
|
||||
)
|
||||
|
||||
p = current_platform
|
||||
return (
|
||||
p.is_cuda()
|
||||
and p.is_device_capability_family(100)
|
||||
and is_flashinfer_mxint4_moe_available()
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _supports_no_act_and_mul() -> bool:
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _supports_quant_scheme(
|
||||
weight_key: QuantKey | None,
|
||||
activation_key: QuantKey | None,
|
||||
) -> bool:
|
||||
return (weight_key, activation_key) == (kInt4Static32, None)
|
||||
|
||||
@staticmethod
|
||||
def _supports_activation(activation: MoEActivation) -> bool:
|
||||
# FlashInfer MxInt4 uses a fused SwiGLU activation.
|
||||
return activation == MoEActivation.SWIGLUOAI
|
||||
|
||||
@staticmethod
|
||||
def _supports_parallel_config(
|
||||
moe_parallel_config: FusedMoEParallelConfig,
|
||||
) -> bool:
|
||||
return (
|
||||
not moe_parallel_config.use_all2all_kernels
|
||||
or moe_parallel_config.use_ag_rs_all2all_kernels
|
||||
) and not moe_parallel_config.enable_eplb
|
||||
|
||||
@staticmethod
|
||||
def _supports_routing_method(
|
||||
routing_method: RoutingMethodType,
|
||||
weight_key: QuantKey | None,
|
||||
activation_key: QuantKey | None,
|
||||
) -> bool:
|
||||
return routing_method in [
|
||||
RoutingMethodType.Renormalize,
|
||||
RoutingMethodType.RenormalizeNaive,
|
||||
RoutingMethodType.DeepSeekV3,
|
||||
RoutingMethodType.Llama4,
|
||||
RoutingMethodType.Simulated,
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def _supports_router_logits_dtype(
|
||||
router_logits_dtype: torch.dtype | None,
|
||||
routing_method: RoutingMethodType,
|
||||
) -> bool:
|
||||
if router_logits_dtype == torch.float32:
|
||||
# DeepSeekV3 routing handles float32 logits internally.
|
||||
# Simulated routing generates synthetic decisions.
|
||||
return routing_method in (
|
||||
RoutingMethodType.DeepSeekV3,
|
||||
RoutingMethodType.Simulated,
|
||||
)
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def activation_format() -> mk.FusedMoEActivationFormat:
|
||||
return mk.FusedMoEActivationFormat.Standard
|
||||
|
||||
def supports_expert_map(self) -> bool:
|
||||
return False
|
||||
|
||||
@property
|
||||
def expects_unquantized_inputs(self) -> bool:
|
||||
# The kernel handles quantization internally.
|
||||
return True
|
||||
|
||||
def apply(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
w1: torch.Tensor,
|
||||
w2: torch.Tensor,
|
||||
router_logits: torch.Tensor,
|
||||
activation: MoEActivation,
|
||||
global_num_experts: int,
|
||||
expert_map: torch.Tensor | None,
|
||||
a1q_scale: torch.Tensor | None,
|
||||
apply_router_weight_on_input: bool,
|
||||
num_expert_group: int | None = None,
|
||||
e_score_correction_bias: torch.Tensor | None = None,
|
||||
routed_scaling_factor: float | None = None,
|
||||
topk_group: int | None = None,
|
||||
) -> torch.Tensor:
|
||||
from vllm.model_executor.layers.quantization.utils.flashinfer_mxint4_moe import ( # noqa: E501
|
||||
flashinfer_trtllm_mxint4_moe,
|
||||
)
|
||||
|
||||
assert self.w1_scale is not None
|
||||
assert self.w2_scale is not None
|
||||
return flashinfer_trtllm_mxint4_moe(
|
||||
x=hidden_states,
|
||||
router_logits=router_logits,
|
||||
w13_weight_packed=w1,
|
||||
w13_weight_scale=self.w1_scale,
|
||||
w2_weight_packed=w2,
|
||||
w2_weight_scale=self.w2_scale,
|
||||
global_num_experts=global_num_experts,
|
||||
top_k=self.topk,
|
||||
intermediate_size_per_partition=self.intermediate_size_per_partition,
|
||||
local_num_experts=self.local_num_experts,
|
||||
ep_rank=self.ep_rank,
|
||||
num_expert_group=num_expert_group,
|
||||
topk_group=topk_group,
|
||||
e_score_correction_bias=e_score_correction_bias,
|
||||
routing_method_type=self.routing_method,
|
||||
)
|
||||
@@ -2,9 +2,12 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import sys
|
||||
from enum import Enum
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
from compressed_tensors.quantization import (
|
||||
QuantizationArgs,
|
||||
)
|
||||
|
||||
import vllm._custom_ops as ops
|
||||
import vllm.model_executor.layers.fused_moe.modular_kernel as mk
|
||||
@@ -12,10 +15,16 @@ from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.fused_moe.config import (
|
||||
FusedMoEConfig,
|
||||
FusedMoEQuantConfig,
|
||||
int4_w4a16_moe_quant_config,
|
||||
int8_w8a16_moe_quant_config,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.experts.marlin_moe import (
|
||||
BatchedMarlinExperts,
|
||||
MarlinExperts,
|
||||
MarlinExpertsBase,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.experts.trtllm_mxint4_moe import (
|
||||
TrtLlmMxint4ExpertsMonolithic,
|
||||
)
|
||||
from vllm.model_executor.layers.quantization.base_config import QuantizationConfig
|
||||
from vllm.model_executor.layers.quantization.utils.marlin_utils import (
|
||||
@@ -29,16 +38,13 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.model_executor.layers.quantization.auto_gptq import AutoGPTQConfig
|
||||
from vllm.model_executor.layers.quantization.awq_marlin import AWQMarlinConfig
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class WNA16MoEBackend(Enum):
|
||||
MARLIN = "MARLIN"
|
||||
BATCHED_MARLIN = "BATCHED_MARLIN"
|
||||
FLASHINFER_TRTLLM = "FLASHINFER_TRTLLM"
|
||||
XPU = "XPU"
|
||||
|
||||
|
||||
@@ -47,26 +53,17 @@ def backend_to_kernel_cls(
|
||||
) -> list[type[mk.FusedMoEExperts]]:
|
||||
"""Return the experts class for the given backend, or None for NONE."""
|
||||
if backend == WNA16MoEBackend.MARLIN:
|
||||
from vllm.model_executor.layers.fused_moe.experts.marlin_moe import (
|
||||
MarlinExperts,
|
||||
)
|
||||
|
||||
return [MarlinExperts]
|
||||
|
||||
elif backend == WNA16MoEBackend.BATCHED_MARLIN:
|
||||
from vllm.model_executor.layers.fused_moe.experts.marlin_moe import (
|
||||
BatchedMarlinExperts,
|
||||
)
|
||||
|
||||
return [BatchedMarlinExperts]
|
||||
|
||||
elif backend == WNA16MoEBackend.FLASHINFER_TRTLLM:
|
||||
return [TrtLlmMxint4ExpertsMonolithic]
|
||||
elif backend == WNA16MoEBackend.XPU:
|
||||
from vllm.model_executor.layers.fused_moe.experts.xpu_moe import (
|
||||
XPUExpertsWNA16,
|
||||
)
|
||||
|
||||
return [XPUExpertsWNA16]
|
||||
|
||||
else:
|
||||
raise ValueError(f"Unknown WNA16 MoE backend: {backend.value}")
|
||||
|
||||
@@ -77,23 +74,25 @@ def _get_priority_backends() -> list[WNA16MoEBackend]:
|
||||
"""
|
||||
if current_platform.is_xpu():
|
||||
return [WNA16MoEBackend.XPU]
|
||||
return [
|
||||
|
||||
_AVAILABLE_BACKENDS = [
|
||||
WNA16MoEBackend.FLASHINFER_TRTLLM,
|
||||
WNA16MoEBackend.MARLIN,
|
||||
WNA16MoEBackend.BATCHED_MARLIN,
|
||||
]
|
||||
return _AVAILABLE_BACKENDS
|
||||
|
||||
|
||||
def select_wna16_moe_backend(
|
||||
config: FusedMoEConfig,
|
||||
weight_key: QuantKey,
|
||||
weight_bits: int,
|
||||
) -> tuple[WNA16MoEBackend, type[mk.FusedMoEExperts]]:
|
||||
"""Select the WNA16 MoE backend.
|
||||
|
||||
Args:
|
||||
config: the shared ``FusedMoEConfig`` for this layer.
|
||||
weight_bits: quantization bit-width (4 or 8). 8-bit weights are not
|
||||
supported by the modular Marlin kernel, so ``NONE`` is returned.
|
||||
weight_key: The QuantKey describing the weight quantization.
|
||||
Must have int4 or int8 type.
|
||||
|
||||
Returns:
|
||||
A tuple of (``WNA16MoEBackend``, experts class or ``None``).
|
||||
@@ -156,15 +155,55 @@ def select_wna16_moe_backend(
|
||||
)
|
||||
|
||||
|
||||
def make_wna16_moe_quant_config(
|
||||
w1_scale: torch.Tensor,
|
||||
w2_scale: torch.Tensor,
|
||||
group_size: int,
|
||||
num_bits: int,
|
||||
w1_zp: torch.Tensor | None = None,
|
||||
w2_zp: torch.Tensor | None = None,
|
||||
w1_bias: torch.Tensor | None = None,
|
||||
w2_bias: torch.Tensor | None = None,
|
||||
a1_gscale: torch.Tensor | None = None,
|
||||
a2_gscale: torch.Tensor | None = None,
|
||||
) -> FusedMoEQuantConfig:
|
||||
"""Create the FusedMoEQuantConfig for 4 or 8-bit WNA16 MoE."""
|
||||
if num_bits == 4:
|
||||
return int4_w4a16_moe_quant_config(
|
||||
w1_scale=w1_scale,
|
||||
w2_scale=w2_scale,
|
||||
w1_zp=w1_zp,
|
||||
w2_zp=w2_zp,
|
||||
w1_bias=w1_bias,
|
||||
w2_bias=w2_bias,
|
||||
block_shape=[0, group_size],
|
||||
a1_gscale=a1_gscale,
|
||||
a2_gscale=a2_gscale,
|
||||
)
|
||||
else:
|
||||
assert num_bits == 8
|
||||
return int8_w8a16_moe_quant_config(
|
||||
w1_scale=w1_scale,
|
||||
w2_scale=w2_scale,
|
||||
w1_zp=w1_zp,
|
||||
w2_zp=w2_zp,
|
||||
w1_bias=w1_bias,
|
||||
w2_bias=w2_bias,
|
||||
block_shape=[0, group_size],
|
||||
a1_gscale=a1_gscale,
|
||||
a2_gscale=a2_gscale,
|
||||
)
|
||||
|
||||
|
||||
def make_wna16_moe_kernel(
|
||||
moe_quant_config: FusedMoEQuantConfig,
|
||||
moe_config: FusedMoEConfig,
|
||||
experts_cls: type[mk.FusedMoEExperts] | None,
|
||||
is_k_full: bool,
|
||||
w13_g_idx: torch.Tensor | None,
|
||||
w2_g_idx: torch.Tensor | None,
|
||||
w13_g_idx_sort_indices: torch.Tensor | None,
|
||||
w2_g_idx_sort_indices: torch.Tensor | None,
|
||||
experts_cls: type[mk.FusedMoEExperts],
|
||||
is_k_full: bool = False,
|
||||
w13_g_idx: torch.Tensor | None = None,
|
||||
w2_g_idx: torch.Tensor | None = None,
|
||||
w13_g_idx_sort_indices: torch.Tensor | None = None,
|
||||
w2_g_idx_sort_indices: torch.Tensor | None = None,
|
||||
routing_tables: tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None = None,
|
||||
) -> mk.FusedMoEKernel:
|
||||
from vllm.model_executor.layers.fused_moe.all2all_utils import (
|
||||
@@ -174,16 +213,37 @@ def make_wna16_moe_kernel(
|
||||
XPUExpertsWNA16,
|
||||
)
|
||||
|
||||
assert experts_cls in (MarlinExperts, BatchedMarlinExperts, XPUExpertsWNA16)
|
||||
# Currently, we only support TrtLlmMxint4ExpertsMonolithic, MarlinExperts
|
||||
# and BatchedMarlinExperts
|
||||
assert experts_cls in (
|
||||
MarlinExperts,
|
||||
BatchedMarlinExperts,
|
||||
TrtLlmMxint4ExpertsMonolithic,
|
||||
XPUExpertsWNA16,
|
||||
)
|
||||
|
||||
is_monolithic = experts_cls.is_monolithic()
|
||||
|
||||
prepare_finalize = maybe_make_prepare_finalize(
|
||||
moe=moe_config,
|
||||
quant_config=moe_quant_config,
|
||||
routing_tables=routing_tables,
|
||||
allow_new_interface=True,
|
||||
use_monolithic=is_monolithic,
|
||||
)
|
||||
assert prepare_finalize is not None
|
||||
assert isinstance(prepare_finalize, mk.FusedMoEPrepareAndFinalizeModular)
|
||||
|
||||
logger.info_once("Using %s", prepare_finalize.__class__.__name__, scope="local")
|
||||
|
||||
extra_args: dict[str, Any] = {}
|
||||
if issubclass(experts_cls, MarlinExpertsBase):
|
||||
extra_args = {
|
||||
"w13_g_idx": w13_g_idx,
|
||||
"w2_g_idx": w2_g_idx,
|
||||
"w13_g_idx_sort_indices": w13_g_idx_sort_indices,
|
||||
"w2_g_idx_sort_indices": w2_g_idx_sort_indices,
|
||||
"is_k_full": is_k_full,
|
||||
}
|
||||
|
||||
if experts_cls is XPUExpertsWNA16:
|
||||
assert (
|
||||
@@ -199,30 +259,20 @@ def make_wna16_moe_kernel(
|
||||
elif (
|
||||
prepare_finalize.activation_format == mk.FusedMoEActivationFormat.BatchedExperts
|
||||
):
|
||||
assert experts_cls == BatchedMarlinExperts
|
||||
max_num_tokens = prepare_finalize.max_num_tokens_per_rank()
|
||||
assert max_num_tokens is not None
|
||||
experts = BatchedMarlinExperts(
|
||||
experts = experts_cls(
|
||||
max_num_tokens=max_num_tokens,
|
||||
num_dispatchers=prepare_finalize.num_dispatchers(),
|
||||
moe_config=moe_config,
|
||||
quant_config=moe_quant_config,
|
||||
w13_g_idx=w13_g_idx,
|
||||
w2_g_idx=w2_g_idx,
|
||||
w13_g_idx_sort_indices=w13_g_idx_sort_indices,
|
||||
w2_g_idx_sort_indices=w2_g_idx_sort_indices,
|
||||
is_k_full=is_k_full,
|
||||
**extra_args,
|
||||
)
|
||||
else:
|
||||
assert experts_cls == MarlinExperts
|
||||
experts = MarlinExperts(
|
||||
experts = experts_cls(
|
||||
moe_config=moe_config,
|
||||
quant_config=moe_quant_config,
|
||||
w13_g_idx=w13_g_idx,
|
||||
w2_g_idx=w2_g_idx,
|
||||
w13_g_idx_sort_indices=w13_g_idx_sort_indices,
|
||||
w2_g_idx_sort_indices=w2_g_idx_sort_indices,
|
||||
is_k_full=is_k_full,
|
||||
**extra_args,
|
||||
)
|
||||
|
||||
return mk.FusedMoEKernel(
|
||||
@@ -236,10 +286,74 @@ def make_wna16_moe_kernel(
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _process_weights_flashinfer(
|
||||
w13_qweight: torch.Tensor,
|
||||
w2_qweight: torch.Tensor,
|
||||
w13_scales: torch.Tensor,
|
||||
w2_scales: torch.Tensor,
|
||||
w13_g_idx: torch.Tensor,
|
||||
w2_g_idx: torch.Tensor,
|
||||
w13_bias: torch.Tensor | None = None,
|
||||
w2_bias: torch.Tensor | None = None,
|
||||
) -> tuple[
|
||||
torch.Tensor, # w13_qweight
|
||||
torch.Tensor, # w2_qweight
|
||||
torch.Tensor, # w13_scales
|
||||
torch.Tensor, # w2_scales
|
||||
torch.Tensor, # w13_g_idx
|
||||
torch.Tensor, # w2_g_idx
|
||||
torch.Tensor | None, # w13_g_idx_sort_indices
|
||||
torch.Tensor | None, # w2_g_idx_sort_indices
|
||||
torch.Tensor | None, # w13_qzeros
|
||||
torch.Tensor | None, # w2_qzeros
|
||||
torch.Tensor | None, # w13_input_global_scale
|
||||
torch.Tensor | None, # w2_input_global_scale
|
||||
torch.Tensor | None, # w13_bias
|
||||
torch.Tensor | None, # w2_bias
|
||||
]:
|
||||
"""Flashinfer (TRT-LLM MXINT4) weight post-processing.
|
||||
|
||||
Steps
|
||||
-----
|
||||
1. Transform weights/scales via ``prepare_static_weights_for_trtllm_mxint4_moe``.
|
||||
2. Return transformed tensors, passing through g_idx/bias unchanged.
|
||||
"""
|
||||
from vllm.model_executor.layers.quantization.utils.flashinfer_mxint4_moe import (
|
||||
prepare_static_weights_for_trtllm_mxint4_moe,
|
||||
)
|
||||
|
||||
dict_weights_mxint4 = prepare_static_weights_for_trtllm_mxint4_moe(
|
||||
w13_qweight,
|
||||
w13_scales,
|
||||
w2_qweight,
|
||||
w2_scales,
|
||||
)
|
||||
|
||||
return (
|
||||
dict_weights_mxint4["gemm1_weights"],
|
||||
dict_weights_mxint4["gemm2_weights"],
|
||||
dict_weights_mxint4["gemm1_scales"],
|
||||
dict_weights_mxint4["gemm2_scales"],
|
||||
w13_g_idx,
|
||||
w2_g_idx,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
w13_bias,
|
||||
w2_bias,
|
||||
)
|
||||
|
||||
|
||||
def _process_weights_marlin(
|
||||
layer: torch.nn.Module,
|
||||
quant_config: "AutoGPTQConfig",
|
||||
input_dtype: torch.dtype | None,
|
||||
num_bits: int,
|
||||
pack_factor: int,
|
||||
group_size: int,
|
||||
actorder: str | None,
|
||||
w13_qweight: torch.Tensor,
|
||||
w2_qweight: torch.Tensor,
|
||||
w13_scales: torch.Tensor,
|
||||
@@ -292,6 +406,7 @@ def _process_weights_marlin(
|
||||
|
||||
# --- FP8 weight / scale adjustment ---
|
||||
if input_dtype == torch.float8_e4m3fn:
|
||||
# NOTE: for non-zp quantization format only
|
||||
marlin_w13_qweight = ops.marlin_int4_fp8_preprocess(w13_qweight, inplace=False)
|
||||
marlin_w2_qweight = ops.marlin_int4_fp8_preprocess(w2_qweight, inplace=False)
|
||||
marlin_w13_scales = w13_scales.data * 512
|
||||
@@ -303,7 +418,7 @@ def _process_weights_marlin(
|
||||
marlin_w2_scales = w2_scales
|
||||
|
||||
# --- Process act_order (g_idx) ---
|
||||
if quant_config.desc_act:
|
||||
if actorder == "group":
|
||||
num_experts = w13_g_idx.shape[0]
|
||||
w13_g_idx_sort_indices = torch.empty_like(w13_g_idx)
|
||||
w2_g_idx_sort_indices = torch.empty_like(w2_g_idx)
|
||||
@@ -314,6 +429,8 @@ def _process_weights_marlin(
|
||||
w2_g_idx_sort_indices[e] = torch.argsort(w2_g_idx[e]).to(torch.int32)
|
||||
w13_sorted_g_idx[e] = w13_g_idx[e][w13_g_idx_sort_indices[e]]
|
||||
w2_sorted_g_idx[e] = w2_g_idx[e][w2_g_idx_sort_indices[e]]
|
||||
w13_g_idx = w13_sorted_g_idx
|
||||
w2_g_idx = w2_sorted_g_idx
|
||||
else:
|
||||
num_experts = w13_g_idx.shape[0]
|
||||
device = w13_g_idx.device
|
||||
@@ -338,17 +455,17 @@ def _process_weights_marlin(
|
||||
marlin_w13_qweight = ops.gptq_marlin_moe_repack(
|
||||
marlin_w13_qweight,
|
||||
w13_g_idx_sort_indices,
|
||||
marlin_w13_qweight.shape[1] * quant_config.pack_factor,
|
||||
marlin_w13_qweight.shape[1] * pack_factor,
|
||||
marlin_w13_qweight.shape[2],
|
||||
quant_config.quant_type.size_bits,
|
||||
num_bits,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
marlin_w2_qweight = ops.gptq_marlin_moe_repack(
|
||||
marlin_w2_qweight,
|
||||
w2_g_idx_sort_indices,
|
||||
marlin_w2_qweight.shape[1] * quant_config.pack_factor,
|
||||
marlin_w2_qweight.shape[1] * pack_factor,
|
||||
marlin_w2_qweight.shape[2],
|
||||
quant_config.quant_type.size_bits,
|
||||
num_bits,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
|
||||
@@ -357,19 +474,15 @@ def _process_weights_marlin(
|
||||
s=marlin_w13_scales,
|
||||
size_k=layer.intermediate_size_per_partition,
|
||||
size_n=marlin_w13_scales.shape[2],
|
||||
group_size=quant_config.group_size,
|
||||
group_size=group_size,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
group_size_or_pack_factor = group_size if group_size != -1 else pack_factor
|
||||
marlin_w2_scales = marlin_moe_permute_scales(
|
||||
s=marlin_w2_scales,
|
||||
size_k=marlin_w2_scales.shape[1]
|
||||
* (
|
||||
quant_config.group_size
|
||||
if quant_config.group_size != -1
|
||||
else quant_config.pack_factor
|
||||
),
|
||||
size_k=marlin_w2_scales.shape[1] * group_size_or_pack_factor,
|
||||
size_n=marlin_w2_scales.shape[2],
|
||||
group_size=quant_config.group_size,
|
||||
group_size=group_size,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
|
||||
@@ -409,7 +522,9 @@ def _process_weights_marlin(
|
||||
|
||||
def _process_awq_weights_marlin(
|
||||
layer: torch.nn.Module,
|
||||
quant_config: "AWQMarlinConfig",
|
||||
weight_bits: int,
|
||||
pack_factor: int,
|
||||
group_size: int,
|
||||
input_dtype: torch.dtype | None,
|
||||
w13_qweight: torch.Tensor,
|
||||
w2_qweight: torch.Tensor,
|
||||
@@ -475,16 +590,16 @@ def _process_awq_weights_marlin(
|
||||
w13_qweight,
|
||||
w13_g_idx_sort_indices,
|
||||
size_k=w13_qweight.shape[1],
|
||||
size_n=w13_qweight.shape[2] * quant_config.pack_factor,
|
||||
num_bits=quant_config.weight_bits,
|
||||
size_n=w13_qweight.shape[2] * pack_factor,
|
||||
num_bits=weight_bits,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
marlin_w2_qweight = ops.awq_marlin_moe_repack(
|
||||
w2_qweight,
|
||||
w2_g_idx_sort_indices,
|
||||
size_k=w2_qweight.shape[1],
|
||||
size_n=w2_qweight.shape[2] * quant_config.pack_factor,
|
||||
num_bits=quant_config.weight_bits,
|
||||
size_n=w2_qweight.shape[2] * pack_factor,
|
||||
num_bits=weight_bits,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
|
||||
@@ -492,7 +607,7 @@ def _process_awq_weights_marlin(
|
||||
s=w13_scales,
|
||||
size_k=layer.intermediate_size_per_partition,
|
||||
size_n=w13_scales.shape[2],
|
||||
group_size=quant_config.group_size,
|
||||
group_size=group_size,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
if input_dtype == torch.int8 and layer.num_groups_w13 > 1:
|
||||
@@ -504,7 +619,7 @@ def _process_awq_weights_marlin(
|
||||
s=w2_scales,
|
||||
size_k=layer.intermediate_size_per_partition,
|
||||
size_n=w2_scales.shape[2],
|
||||
group_size=quant_config.group_size,
|
||||
group_size=group_size,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
if input_dtype == torch.int8 and layer.num_groups_w2 > 1:
|
||||
@@ -515,15 +630,15 @@ def _process_awq_weights_marlin(
|
||||
marlin_w13_qzeros = moe_awq_to_marlin_zero_points(
|
||||
w13_qzeros,
|
||||
size_k=w13_qzeros.shape[1],
|
||||
size_n=w13_qzeros.shape[2] * quant_config.pack_factor,
|
||||
num_bits=quant_config.weight_bits,
|
||||
size_n=w13_qzeros.shape[2] * pack_factor,
|
||||
num_bits=weight_bits,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
marlin_w2_qzeros = moe_awq_to_marlin_zero_points(
|
||||
w2_qzeros,
|
||||
size_k=w2_qzeros.shape[1],
|
||||
size_n=w2_qzeros.shape[2] * quant_config.pack_factor,
|
||||
num_bits=quant_config.weight_bits,
|
||||
size_n=w2_qzeros.shape[2] * pack_factor,
|
||||
num_bits=weight_bits,
|
||||
is_a_8bit=is_a_8bit,
|
||||
)
|
||||
|
||||
@@ -616,7 +731,7 @@ def _process_weights_xpu(
|
||||
def convert_to_wna16_moe_kernel_format(
|
||||
backend: WNA16MoEBackend,
|
||||
layer: torch.nn.Module,
|
||||
quant_config: QuantizationConfig,
|
||||
quant_config: QuantizationConfig | QuantizationArgs | None,
|
||||
input_dtype: torch.dtype | None,
|
||||
w13: torch.Tensor,
|
||||
w2: torch.Tensor,
|
||||
@@ -669,9 +784,16 @@ def convert_to_wna16_moe_kernel_format(
|
||||
if isinstance(quant_config, AWQMarlinConfig):
|
||||
if w13_qzeros is None or w2_qzeros is None:
|
||||
raise ValueError("AWQ Marlin MoE requires zero-point tensors.")
|
||||
|
||||
weight_bits = quant_config.weight_bits
|
||||
pack_factor = quant_config.pack_factor
|
||||
group_size = quant_config.group_size
|
||||
|
||||
return _process_awq_weights_marlin(
|
||||
layer,
|
||||
quant_config,
|
||||
weight_bits,
|
||||
pack_factor,
|
||||
group_size,
|
||||
input_dtype,
|
||||
w13,
|
||||
w2,
|
||||
@@ -682,19 +804,30 @@ def convert_to_wna16_moe_kernel_format(
|
||||
w13_bias,
|
||||
w2_bias,
|
||||
)
|
||||
|
||||
if not isinstance(quant_config, AutoGPTQConfig):
|
||||
elif isinstance(quant_config, AutoGPTQConfig):
|
||||
num_bits = quant_config.quant_type.size_bits
|
||||
pack_factor = quant_config.pack_factor
|
||||
group_size = quant_config.group_size
|
||||
actorder = "group" if quant_config.desc_act else None
|
||||
elif isinstance(quant_config, QuantizationArgs):
|
||||
num_bits = quant_config.num_bits
|
||||
pack_factor = 32 // quant_config.num_bits
|
||||
group_size = quant_config.group_size
|
||||
actorder = quant_config.actorder
|
||||
else:
|
||||
raise TypeError(
|
||||
"Marlin WNA16 MoE backend requires AutoGPTQConfig or "
|
||||
"AWQMarlinConfig, got "
|
||||
f"{type(quant_config).__name__}."
|
||||
"Marlin WNA16 MoE backend requires AutoGPTQConfig, AWQMarlinConfig or "
|
||||
f"QuantizationArgs, got {type(quant_config).__name__}."
|
||||
)
|
||||
if w13_g_idx is None or w2_g_idx is None:
|
||||
raise ValueError("GPTQ Marlin MoE requires g_idx tensors.")
|
||||
return _process_weights_marlin(
|
||||
layer,
|
||||
quant_config,
|
||||
input_dtype,
|
||||
num_bits,
|
||||
pack_factor,
|
||||
group_size,
|
||||
actorder,
|
||||
w13,
|
||||
w2,
|
||||
w13_scale,
|
||||
@@ -706,7 +839,19 @@ def convert_to_wna16_moe_kernel_format(
|
||||
w13_bias,
|
||||
w2_bias,
|
||||
)
|
||||
elif backend == WNA16MoEBackend.FLASHINFER_TRTLLM:
|
||||
return _process_weights_flashinfer(
|
||||
w13,
|
||||
w2,
|
||||
w13_scale,
|
||||
w2_scale,
|
||||
w13_g_idx,
|
||||
w2_g_idx,
|
||||
w13_bias,
|
||||
w2_bias,
|
||||
)
|
||||
elif backend == WNA16MoEBackend.XPU:
|
||||
assert quant_config is not None
|
||||
(
|
||||
w13_xpu,
|
||||
w2_xpu,
|
||||
|
||||
Reference in New Issue
Block a user