[Kernel] Extend Marlin thread-tile padding to MoE (WNA16 + FP8/MXFP8) (#45703)
Signed-off-by: mgoin <mgoin64@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,7 @@ from vllm.model_executor.layers.fused_moe.experts.trtllm_mxint4_moe import (
|
||||
from vllm.model_executor.layers.quantization.base_config import QuantizationConfig
|
||||
from vllm.model_executor.layers.quantization.utils.marlin_utils import (
|
||||
marlin_act_int8_process_scales,
|
||||
marlin_moe_padded_intermediate,
|
||||
marlin_moe_permute_scales,
|
||||
marlin_permute_bias,
|
||||
moe_awq_to_marlin_zero_points,
|
||||
@@ -361,6 +362,34 @@ def _process_weights_flashinfer(
|
||||
)
|
||||
|
||||
|
||||
def _pad_w13_shard_cols(x: torch.Tensor, unit: int, padded_unit: int) -> torch.Tensor:
|
||||
"""Zero-pad each of the two gate/up shards of a ``(E, rows, 2 * unit)``
|
||||
tensor along its last dim, from ``unit`` to ``padded_unit`` columns."""
|
||||
if padded_unit == unit:
|
||||
return x
|
||||
e, rows, _ = x.shape
|
||||
x = x.view(e, rows, 2, unit)
|
||||
x = torch.nn.functional.pad(x, (0, padded_unit - unit))
|
||||
return x.reshape(e, rows, 2 * padded_unit).contiguous()
|
||||
|
||||
|
||||
def _pad_rows(x: torch.Tensor, padded_rows: int) -> torch.Tensor:
|
||||
"""Zero-pad a ``(E, rows, cols)`` tensor to ``padded_rows`` rows."""
|
||||
if padded_rows == x.size(1):
|
||||
return x
|
||||
return torch.nn.functional.pad(x, (0, 0, 0, padded_rows - x.size(1)))
|
||||
|
||||
|
||||
def _pad_w13_bias(bias: torch.Tensor, n: int, padded_n: int) -> torch.Tensor:
|
||||
"""Zero-pad each gate/up shard of a ``(E, 2 * n)`` bias to ``padded_n``."""
|
||||
if padded_n == n:
|
||||
return bias
|
||||
e = bias.size(0)
|
||||
bias = bias.view(e, 2, n)
|
||||
bias = torch.nn.functional.pad(bias, (0, padded_n - n))
|
||||
return bias.reshape(e, 2 * padded_n).contiguous()
|
||||
|
||||
|
||||
def _process_weights_marlin(
|
||||
layer: torch.nn.Module,
|
||||
input_dtype: torch.dtype | None,
|
||||
@@ -431,6 +460,29 @@ def _process_weights_marlin(
|
||||
marlin_w13_scales = w13_scales
|
||||
marlin_w2_scales = w2_scales
|
||||
|
||||
# --- Pad the intermediate size to a valid Marlin thread tile ---
|
||||
# GPTQ packs along K: w13's N is in the (shard) columns, w2's N in the rows.
|
||||
# Act-order keeps the strict shape and is never padded.
|
||||
N = layer.intermediate_size_per_partition
|
||||
padded_N = marlin_moe_padded_intermediate(N, group_size)
|
||||
if padded_N != N:
|
||||
assert actorder != "group", (
|
||||
"Marlin MoE thread-tile padding is unsupported with act-order"
|
||||
)
|
||||
marlin_w13_qweight = _pad_w13_shard_cols(marlin_w13_qweight, N, padded_N)
|
||||
marlin_w2_qweight = _pad_rows(marlin_w2_qweight, padded_N // pack_factor)
|
||||
marlin_w13_scales = _pad_w13_shard_cols(marlin_w13_scales, N, padded_N)
|
||||
if group_size > 0:
|
||||
marlin_w2_scales = _pad_rows(marlin_w2_scales, padded_N // group_size)
|
||||
if w13_qzeros is not None:
|
||||
w13_qzeros = _pad_w13_shard_cols(
|
||||
w13_qzeros, N // pack_factor, padded_N // pack_factor
|
||||
)
|
||||
if w2_qzeros is not None and group_size > 0:
|
||||
w2_qzeros = _pad_rows(w2_qzeros, padded_N // group_size)
|
||||
if w13_bias is not None:
|
||||
w13_bias = _pad_w13_bias(w13_bias, N, padded_N)
|
||||
|
||||
# --- Process act_order (g_idx) ---
|
||||
if actorder == "group":
|
||||
num_experts = w13_g_idx.shape[0]
|
||||
@@ -608,6 +660,25 @@ def _process_awq_weights_marlin(
|
||||
w13_scales = w13_scales.data * 512
|
||||
w2_scales = w2_scales.data * 512
|
||||
|
||||
# --- Pad the intermediate size to a valid Marlin thread tile ---
|
||||
# AWQ packs along N: w13's N is in the (shard) columns, w2's N in the rows.
|
||||
N = layer.intermediate_size_per_partition
|
||||
padded_N = marlin_moe_padded_intermediate(N, group_size)
|
||||
if padded_N != N:
|
||||
w13_qweight = _pad_w13_shard_cols(
|
||||
w13_qweight, N // pack_factor, padded_N // pack_factor
|
||||
)
|
||||
w2_qweight = _pad_rows(w2_qweight, padded_N)
|
||||
w13_scales = _pad_w13_shard_cols(w13_scales, N, padded_N)
|
||||
w13_qzeros = _pad_w13_shard_cols(
|
||||
w13_qzeros, N // pack_factor, padded_N // pack_factor
|
||||
)
|
||||
if group_size > 0:
|
||||
w2_scales = _pad_rows(w2_scales, padded_N // group_size)
|
||||
w2_qzeros = _pad_rows(w2_qzeros, padded_N // group_size)
|
||||
if w13_bias is not None:
|
||||
w13_bias = _pad_w13_bias(w13_bias, N, padded_N)
|
||||
|
||||
w13_g_idx_sort_indices = torch.nn.Parameter(
|
||||
torch.empty((num_experts, 0), dtype=torch.int32, device=device),
|
||||
requires_grad=False,
|
||||
|
||||
Reference in New Issue
Block a user