Refactor AWQ Marlin MoE onto modular WNA16 oracle (#42483)

Signed-off-by: Siddharth Bedekar <bedeksid@gmail.com>
Signed-off-by: Siddharth Bedekar <104613085+bedeks@users.noreply.github.com>
Co-authored-by: Robert Shaw <robertgshaw2-redhat@users.noreply.github.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Siddharth Bedekar
2026-05-18 08:02:43 -07:00
committed by GitHub
co-authored by Robert Shaw OpenAI Codex gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent f5d3dc7115
commit 78e7a7b9b0
6 changed files with 405 additions and 206 deletions
@@ -957,9 +957,14 @@ def awq_marlin_moe_quant_config(
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
@@ -977,8 +982,8 @@ def awq_marlin_moe_quant_config(
raise ValueError(f"Unsupported weight_bits: {weight_bits}")
return FusedMoEQuantConfig(
_a1=FusedMoEQuantDesc(dtype=None, shape=a_shape),
_a2=FusedMoEQuantDesc(dtype=None, shape=a_shape),
_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),
)
@@ -401,6 +401,8 @@ def batched_fused_marlin_moe(
global_num_experts: int = -1,
activation: MoEActivation = MoEActivation.SILU,
expert_map: torch.Tensor | None = None,
input_global_scale1: torch.Tensor | None = None,
input_global_scale2: torch.Tensor | None = None,
global_scale1: torch.Tensor | None = None,
global_scale2: torch.Tensor | None = None,
g_idx1: torch.Tensor | None = None,
@@ -414,6 +416,7 @@ def batched_fused_marlin_moe(
intermediate_cache2: torch.Tensor | None = None,
is_k_full: bool = True,
output: torch.Tensor | None = None,
input_dtype: torch.dtype | None = None,
inplace: bool = False,
clamp_limit: float | None = None,
) -> torch.Tensor:
@@ -489,7 +492,15 @@ def batched_fused_marlin_moe(
topk = 1
# TODO(varun) : Choose a decent block size like in fused_marlin_moe
# Tune block_size_m based on expert capacity to reduce padding overhead.
block_size_m = 64
for b_m in [8, 16, 32, 48, 64]:
if BATCH_TOKENS_MAX / b_m < 0.9:
block_size_m = b_m
break
if input_dtype is not None and input_dtype.itemsize == 1:
block_size_m = max(block_size_m, 16)
sorted_token_ids, expert_ids, num_tokens_post_padded = batched_moe_align_block_size(
max_tokens_per_batch=BATCH_TOKENS_MAX,
@@ -525,6 +536,8 @@ def batched_fused_marlin_moe(
sorted_token_ids=sorted_token_ids,
expert_ids=expert_ids,
num_tokens_post_padded=num_tokens_post_padded,
input_global_scale1=input_global_scale1,
input_global_scale2=input_global_scale2,
global_scale1=global_scale1,
global_scale2=global_scale2,
g_idx1=g_idx1,
@@ -537,6 +550,7 @@ def batched_fused_marlin_moe(
intermediate_cache13=intermediate_cache13,
intermediate_cache2=intermediate_cache2,
output=output.view(-1, K) if output is not None else output,
input_dtype=input_dtype,
is_k_full=is_k_full,
clamp_limit=clamp_limit,
)
@@ -635,6 +649,8 @@ class MarlinExpertsBase(mk.FusedMoEExpertsModular):
@property
def quant_type_id(self) -> int:
if self.quant_config.use_int4_w4a16:
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_mxfp4_w4a16 or self.quant_config.use_nvfp4_w4a16:
return scalar_types.float4_e2m1f.id
@@ -754,6 +770,10 @@ class MarlinExperts(LoRAExpertsMixin, MarlinExpertsBase):
topk_ids=topk_ids,
global_scale1=self.g1_alphas,
global_scale2=self.g2_alphas,
input_global_scale1=self.a1_gscale,
input_global_scale2=self.a2_gscale,
w1_zeros=self.w1_zp,
w2_zeros=self.w2_zp,
quant_type_id=self.quant_type_id,
apply_router_weight_on_input=apply_router_weight_on_input,
global_num_experts=global_num_experts,
@@ -853,6 +873,10 @@ class MarlinExperts(LoRAExpertsMixin, MarlinExpertsBase):
topk_ids=topk_ids,
global_scale1=self.g1_alphas,
global_scale2=self.g2_alphas,
input_global_scale1=self.a1_gscale,
input_global_scale2=self.a2_gscale,
w1_zeros=self.w1_zp,
w2_zeros=self.w2_zp,
quant_type_id=self.quant_type_id,
apply_router_weight_on_input=apply_router_weight_on_input,
global_num_experts=global_num_experts,
@@ -967,6 +991,8 @@ class BatchedMarlinExperts(MarlinExpertsBase):
global_num_experts=global_num_experts,
activation=activation,
expert_map=expert_map,
input_global_scale1=self.a1_gscale,
input_global_scale2=self.a2_gscale,
output=output,
intermediate_cache13=workspace13,
intermediate_cache2=workspace2,
@@ -974,6 +1000,9 @@ class BatchedMarlinExperts(MarlinExpertsBase):
g_idx2=self.w2_g_idx,
sort_indices1=self.w13_g_idx_sort_indices,
sort_indices2=self.w2_g_idx_sort_indices,
w1_zeros=self.w1_zp,
w2_zeros=self.w2_zp,
input_dtype=self.input_dtype,
is_k_full=self.is_k_full,
clamp_limit=self.gemm1_clamp_limit,
)
@@ -21,6 +21,7 @@ from vllm.model_executor.layers.quantization.utils.marlin_utils import (
marlin_act_int8_process_scales,
marlin_moe_permute_scales,
marlin_permute_bias,
moe_awq_to_marlin_zero_points,
)
from vllm.model_executor.layers.quantization.utils.quant_utils import (
QuantKey,
@@ -28,6 +29,7 @@ from vllm.model_executor.layers.quantization.utils.quant_utils import (
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__)
@@ -147,7 +149,6 @@ def make_wna16_moe_kernel(
moe_quant_config: FusedMoEQuantConfig,
moe_config: FusedMoEConfig,
experts_cls: type[mk.FusedMoEExperts] | None,
layer: torch.nn.Module,
is_k_full: bool,
w13_g_idx: torch.Tensor | None,
w2_g_idx: torch.Tensor | None,
@@ -220,6 +221,8 @@ def _process_weights_marlin(
w2_scales: torch.Tensor,
w13_g_idx: torch.Tensor,
w2_g_idx: torch.Tensor,
w13_qzeros: torch.Tensor | None = None,
w2_qzeros: torch.Tensor | None = None,
w13_bias: torch.Tensor | None = None,
w2_bias: torch.Tensor | None = None,
) -> tuple[
@@ -231,6 +234,8 @@ def _process_weights_marlin(
torch.Tensor, # w2_g_idx
torch.Tensor, # w13_g_idx_sort_indices
torch.Tensor, # 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
@@ -368,6 +373,151 @@ def _process_weights_marlin(
w2_g_idx,
w13_g_idx_sort_indices,
w2_g_idx_sort_indices,
w13_qzeros,
w2_qzeros,
w13_input_global_scale,
w2_input_global_scale,
w13_bias_out,
w2_bias_out,
)
def _process_awq_weights_marlin(
layer: torch.nn.Module,
quant_config: "AWQMarlinConfig",
input_dtype: torch.dtype | None,
w13_qweight: torch.Tensor,
w2_qweight: torch.Tensor,
w13_scales: torch.Tensor,
w2_scales: torch.Tensor,
w13_qzeros: torch.Tensor,
w2_qzeros: 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 | None, # 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, # 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
]:
"""AWQ-specific Marlin weight post-processing.
AWQ checkpoints use a different packing order than GPTQ, so they need
AWQ-specific weight repacking and zero-point conversion before Marlin runs.
"""
num_experts = w13_qweight.shape[0]
device = w13_qweight.device
is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1
w13_input_global_scale: torch.Tensor | None = None
w2_input_global_scale: torch.Tensor | None = None
w13_bias_out: torch.Tensor | None = None
w2_bias_out: torch.Tensor | None = None
if input_dtype == torch.float8_e4m3fn:
ops.marlin_int4_fp8_preprocess(
w13_qweight.view(-1, w13_qweight.size(2)),
w13_qzeros.view(-1, w13_qzeros.size(2)),
inplace=True,
)
ops.marlin_int4_fp8_preprocess(
w2_qweight.view(-1, w2_qweight.size(2)),
w2_qzeros.view(-1, w2_qzeros.size(2)),
inplace=True,
)
w13_scales = w13_scales.data * 512
w2_scales = w2_scales.data * 512
w13_g_idx_sort_indices = torch.nn.Parameter(
torch.empty((num_experts, 0), dtype=torch.int32, device=device),
requires_grad=False,
)
w2_g_idx_sort_indices = torch.nn.Parameter(
torch.empty((num_experts, 0), dtype=torch.int32, device=device),
requires_grad=False,
)
marlin_w13_qweight = ops.awq_marlin_moe_repack(
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,
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,
is_a_8bit=is_a_8bit,
)
marlin_w13_scales = marlin_moe_permute_scales(
s=w13_scales,
size_k=layer.intermediate_size_per_partition,
size_n=w13_scales.shape[2],
group_size=quant_config.group_size,
is_a_8bit=is_a_8bit,
)
if input_dtype == torch.int8 and layer.num_groups_w13 > 1:
marlin_w13_scales, w13_input_global_scale = marlin_act_int8_process_scales(
marlin_w13_scales
)
marlin_w2_scales = marlin_moe_permute_scales(
s=w2_scales,
size_k=layer.intermediate_size_per_partition,
size_n=w2_scales.shape[2],
group_size=quant_config.group_size,
is_a_8bit=is_a_8bit,
)
if input_dtype == torch.int8 and layer.num_groups_w2 > 1:
marlin_w2_scales, w2_input_global_scale = marlin_act_int8_process_scales(
marlin_w2_scales
)
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,
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,
is_a_8bit=is_a_8bit,
)
if w13_bias is not None:
w13_bias_out = marlin_permute_bias(w13_bias)
if w2_bias is not None:
w2_bias_out = marlin_permute_bias(w2_bias)
return (
marlin_w13_qweight,
marlin_w2_qweight,
marlin_w13_scales,
marlin_w2_scales,
None,
None,
w13_g_idx_sort_indices,
w2_g_idx_sort_indices,
marlin_w13_qzeros,
marlin_w2_qzeros,
w13_input_global_scale,
w2_input_global_scale,
w13_bias_out,
@@ -384,8 +534,10 @@ def convert_to_wna16_moe_kernel_format(
w2: torch.Tensor,
w13_scale: torch.Tensor,
w2_scale: torch.Tensor,
w13_g_idx: torch.Tensor,
w2_g_idx: torch.Tensor,
w13_g_idx: torch.Tensor | None = None,
w2_g_idx: torch.Tensor | None = None,
w13_qzeros: torch.Tensor | None = None,
w2_qzeros: torch.Tensor | None = None,
w13_bias: torch.Tensor | None = None,
w2_bias: torch.Tensor | None = None,
) -> tuple[
@@ -397,6 +549,8 @@ def convert_to_wna16_moe_kernel_format(
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, # 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
@@ -420,12 +574,35 @@ def convert_to_wna16_moe_kernel_format(
from vllm.model_executor.layers.quantization.auto_gptq import (
AutoGPTQConfig,
)
from vllm.model_executor.layers.quantization.awq_marlin import (
AWQMarlinConfig,
)
if isinstance(quant_config, AWQMarlinConfig):
if w13_qzeros is None or w2_qzeros is None:
raise ValueError("AWQ Marlin MoE requires zero-point tensors.")
return _process_awq_weights_marlin(
layer,
quant_config,
input_dtype,
w13,
w2,
w13_scale,
w2_scale,
w13_qzeros,
w2_qzeros,
w13_bias,
w2_bias,
)
if not isinstance(quant_config, AutoGPTQConfig):
raise TypeError(
"Marlin WNA16 MoE backend requires AutoGPTQConfig, got "
"Marlin WNA16 MoE backend requires AutoGPTQConfig or "
"AWQMarlinConfig, got "
f"{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,
@@ -436,6 +613,8 @@ def convert_to_wna16_moe_kernel_format(
w2_scale,
w13_g_idx,
w2_g_idx,
w13_qzeros,
w2_qzeros,
w13_bias,
w2_bias,
)