[Quant][Kernel] Remove GPTQ Group/Dynamic Activation Ordering (#54809)
Signed-off-by: Roderick-Wu <roderickwu2003@gmail.com> Signed-off-by: Roderick Wu <roderickwu2003@gmail.com> Signed-off-by: mgoin <mgoin64@gmail.com> Co-authored-by: mgoin <mgoin64@gmail.com>
This commit is contained in:
@@ -81,10 +81,6 @@ def _fused_marlin_moe(
|
||||
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,
|
||||
g_idx2: torch.Tensor | None = None,
|
||||
sort_indices1: torch.Tensor | None = None,
|
||||
sort_indices2: torch.Tensor | None = None,
|
||||
w1_zeros: torch.Tensor | None = None,
|
||||
w2_zeros: torch.Tensor | None = None,
|
||||
workspace: torch.Tensor | None = None,
|
||||
@@ -92,7 +88,6 @@ def _fused_marlin_moe(
|
||||
intermediate_cache2: torch.Tensor | None = None,
|
||||
output: torch.Tensor | None = None,
|
||||
input_dtype: torch.dtype | None = None,
|
||||
is_k_full: bool = True,
|
||||
activation_config: ApplyMoEActivationConfig | None = None,
|
||||
) -> torch.Tensor:
|
||||
assert hidden_states.ndim == 2
|
||||
@@ -142,8 +137,6 @@ def _fused_marlin_moe(
|
||||
a_scales1,
|
||||
global_scale1,
|
||||
w1_zeros,
|
||||
g_idx1,
|
||||
sort_indices1,
|
||||
workspace,
|
||||
sorted_token_ids,
|
||||
expert_ids,
|
||||
@@ -156,7 +149,6 @@ def _fused_marlin_moe(
|
||||
size_m=M,
|
||||
size_n=w13_num_shards * N,
|
||||
size_k=K,
|
||||
is_k_full=is_k_full,
|
||||
use_atomic_add=False,
|
||||
use_fp32_reduce=True,
|
||||
is_zp_float=False,
|
||||
@@ -209,8 +201,6 @@ def _fused_marlin_moe(
|
||||
a_scales2,
|
||||
global_scale2,
|
||||
w2_zeros,
|
||||
g_idx2,
|
||||
sort_indices2,
|
||||
workspace,
|
||||
sorted_token_ids,
|
||||
expert_ids,
|
||||
@@ -223,7 +213,6 @@ def _fused_marlin_moe(
|
||||
size_m=M * num_topk,
|
||||
size_n=K,
|
||||
size_k=N,
|
||||
is_k_full=is_k_full,
|
||||
use_atomic_add=False,
|
||||
use_fp32_reduce=True,
|
||||
is_zp_float=False,
|
||||
@@ -253,16 +242,11 @@ def fused_marlin_moe(
|
||||
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,
|
||||
g_idx2: torch.Tensor | None = None,
|
||||
sort_indices1: torch.Tensor | None = None,
|
||||
sort_indices2: torch.Tensor | None = None,
|
||||
w1_zeros: torch.Tensor | None = None,
|
||||
w2_zeros: torch.Tensor | None = None,
|
||||
workspace: torch.Tensor | None = None,
|
||||
intermediate_cache13: torch.Tensor | None = None,
|
||||
intermediate_cache2: torch.Tensor | None = None,
|
||||
is_k_full: bool = True,
|
||||
output: torch.Tensor | None = None,
|
||||
input_dtype: torch.dtype | None = None,
|
||||
activation_config: ApplyMoEActivationConfig | None = None,
|
||||
@@ -277,12 +261,6 @@ def fused_marlin_moe(
|
||||
- w2 (torch.Tensor): The second set of expert weights.
|
||||
- w1_scale (torch.Tensor): Scale to be used for w1.
|
||||
- w2_scale (torch.Tensor): Scale to be used for w2.
|
||||
- g_idx1 (torch.Tensor|None): The first set of act_order indices.
|
||||
- g_idx2 (torch.Tensor|None): The second set of act_order indices.
|
||||
- sort_indices1 (torch.Tensor|None): The first act_order input
|
||||
permutation.
|
||||
- sort_indices2 (torch.Tensor|None): The second act_order input
|
||||
permutation.
|
||||
- topk_weights (torch.Tensor): Top-k weights.
|
||||
- topk_ids (torch.Tensor): Indices of topk-k elements.
|
||||
- w1_zeros (torch.Tensor|None): Optional zero points to be used for w1.
|
||||
@@ -372,10 +350,6 @@ def fused_marlin_moe(
|
||||
input_global_scale2=input_global_scale2,
|
||||
global_scale1=global_scale1,
|
||||
global_scale2=global_scale2,
|
||||
g_idx1=g_idx1,
|
||||
g_idx2=g_idx2,
|
||||
sort_indices1=sort_indices1,
|
||||
sort_indices2=sort_indices2,
|
||||
w1_zeros=w1_zeros,
|
||||
w2_zeros=w2_zeros,
|
||||
workspace=workspace,
|
||||
@@ -383,7 +357,6 @@ def fused_marlin_moe(
|
||||
intermediate_cache2=intermediate_cache2,
|
||||
output=None,
|
||||
input_dtype=input_dtype,
|
||||
is_k_full=is_k_full,
|
||||
).view(-1, topk, K)
|
||||
|
||||
if output is None:
|
||||
@@ -416,16 +389,11 @@ def batched_fused_marlin_moe(
|
||||
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,
|
||||
g_idx2: torch.Tensor | None = None,
|
||||
sort_indices1: torch.Tensor | None = None,
|
||||
sort_indices2: torch.Tensor | None = None,
|
||||
w1_zeros: torch.Tensor | None = None,
|
||||
w2_zeros: torch.Tensor | None = None,
|
||||
workspace: torch.Tensor | None = None,
|
||||
intermediate_cache13: torch.Tensor | None = None,
|
||||
intermediate_cache2: torch.Tensor | None = None,
|
||||
is_k_full: bool = True,
|
||||
output: torch.Tensor | None = None,
|
||||
input_dtype: torch.dtype | None = None,
|
||||
activation_func: Callable[..., None] | None = None,
|
||||
@@ -548,10 +516,6 @@ def batched_fused_marlin_moe(
|
||||
input_global_scale2=input_global_scale2,
|
||||
global_scale1=global_scale1,
|
||||
global_scale2=global_scale2,
|
||||
g_idx1=g_idx1,
|
||||
g_idx2=g_idx2,
|
||||
sort_indices1=sort_indices1,
|
||||
sort_indices2=sort_indices2,
|
||||
w1_zeros=w1_zeros,
|
||||
w2_zeros=w2_zeros,
|
||||
workspace=workspace,
|
||||
@@ -559,7 +523,6 @@ def batched_fused_marlin_moe(
|
||||
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,
|
||||
)
|
||||
|
||||
output = output.view(B, BATCH_TOKENS_MAX, K)
|
||||
@@ -574,11 +537,6 @@ class MarlinExpertsBase(mk.FusedMoEExpertsModular):
|
||||
quant_config: FusedMoEQuantConfig,
|
||||
max_num_tokens: int | None = None,
|
||||
num_dispatchers: int | None = None,
|
||||
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,
|
||||
is_k_full: bool = True,
|
||||
):
|
||||
# TODO (varun) : Enable activation quantization
|
||||
assert (
|
||||
@@ -588,11 +546,6 @@ class MarlinExpertsBase(mk.FusedMoEExpertsModular):
|
||||
or quant_config.use_int8_w8a16
|
||||
or quant_config.use_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
|
||||
self.w2_g_idx_sort_indices = w2_g_idx_sort_indices
|
||||
self.is_k_full = is_k_full
|
||||
self.input_dtype = get_marlin_input_dtype()
|
||||
|
||||
super().__init__(
|
||||
@@ -786,11 +739,6 @@ class MarlinExperts(LoRAExpertsMixin, MarlinExpertsBase):
|
||||
# output buffer allocation. Please refer to workspace_shapes().
|
||||
intermediate_cache13=workspace2,
|
||||
intermediate_cache2=workspace13,
|
||||
g_idx1=self.w13_g_idx,
|
||||
g_idx2=self.w2_g_idx,
|
||||
sort_indices1=self.w13_g_idx_sort_indices,
|
||||
sort_indices2=self.w2_g_idx_sort_indices,
|
||||
is_k_full=self.is_k_full,
|
||||
input_dtype=self.input_dtype,
|
||||
)
|
||||
return
|
||||
@@ -901,11 +849,6 @@ class MarlinExperts(LoRAExpertsMixin, MarlinExpertsBase):
|
||||
output=output,
|
||||
intermediate_cache13=workspace2,
|
||||
intermediate_cache2=workspace13,
|
||||
g_idx1=self.w13_g_idx,
|
||||
g_idx2=self.w2_g_idx,
|
||||
sort_indices1=self.w13_g_idx_sort_indices,
|
||||
sort_indices2=self.w2_g_idx_sort_indices,
|
||||
is_k_full=self.is_k_full,
|
||||
input_dtype=self.input_dtype,
|
||||
)
|
||||
|
||||
@@ -931,22 +874,12 @@ class BatchedMarlinExperts(MarlinExpertsBase):
|
||||
quant_config: FusedMoEQuantConfig,
|
||||
max_num_tokens: int,
|
||||
num_dispatchers: int,
|
||||
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,
|
||||
is_k_full: bool = True,
|
||||
):
|
||||
super().__init__(
|
||||
moe_config=moe_config,
|
||||
quant_config=quant_config,
|
||||
max_num_tokens=max_num_tokens,
|
||||
num_dispatchers=num_dispatchers,
|
||||
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,
|
||||
)
|
||||
|
||||
def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce:
|
||||
@@ -1038,14 +971,9 @@ class BatchedMarlinExperts(MarlinExpertsBase):
|
||||
output=output,
|
||||
intermediate_cache13=workspace13,
|
||||
intermediate_cache2=workspace2,
|
||||
g_idx1=self.w13_g_idx,
|
||||
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,
|
||||
activation_func=activation_func,
|
||||
activation_config=self.activation_config,
|
||||
)
|
||||
|
||||
@@ -22,7 +22,6 @@ from vllm.model_executor.layers.fused_moe.config import (
|
||||
from vllm.model_executor.layers.fused_moe.experts.marlin_moe import (
|
||||
BatchedMarlinExperts,
|
||||
MarlinExperts,
|
||||
MarlinExpertsBase,
|
||||
)
|
||||
from vllm.model_executor.layers.fused_moe.experts.triton_moe import (
|
||||
TritonWNA16Experts,
|
||||
@@ -362,11 +361,6 @@ def make_wna16_moe_kernel(
|
||||
moe_config: FusedMoEConfig,
|
||||
experts_cls: type[mk.FusedMoEExperts],
|
||||
backend: WNA16MoEBackend = WNA16MoEBackend.MARLIN,
|
||||
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 (
|
||||
@@ -413,15 +407,6 @@ def make_wna16_moe_kernel(
|
||||
logger.info_once("Using %s", experts_cls.__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 prepare_finalize.activation_format == mk.FusedMoEActivationFormat.BatchedExperts:
|
||||
max_num_tokens = prepare_finalize.max_num_tokens_per_rank()
|
||||
assert max_num_tokens is not None
|
||||
@@ -450,8 +435,6 @@ def _process_weights_flashinfer(
|
||||
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[
|
||||
@@ -459,10 +442,6 @@ def _process_weights_flashinfer(
|
||||
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
|
||||
@@ -475,7 +454,7 @@ def _process_weights_flashinfer(
|
||||
Steps
|
||||
-----
|
||||
1. Transform weights/scales via ``prepare_static_weights_for_trtllm_mxint4_moe``.
|
||||
2. Return transformed tensors, passing through g_idx/bias unchanged.
|
||||
2. Return transformed tensors and biases.
|
||||
"""
|
||||
from vllm.model_executor.layers.quantization.utils.flashinfer_mxint4_moe import (
|
||||
prepare_static_weights_for_trtllm_mxint4_moe,
|
||||
@@ -493,10 +472,6 @@ def _process_weights_flashinfer(
|
||||
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,
|
||||
@@ -540,13 +515,10 @@ def _process_weights_marlin(
|
||||
num_bits: int,
|
||||
pack_factor: int,
|
||||
group_size: int,
|
||||
actorder: str | None,
|
||||
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_qzeros: torch.Tensor | None = None,
|
||||
w2_qzeros: torch.Tensor | None = None,
|
||||
w13_bias: torch.Tensor | None = None,
|
||||
@@ -556,10 +528,6 @@ def _process_weights_marlin(
|
||||
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_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
|
||||
@@ -573,10 +541,9 @@ def _process_weights_marlin(
|
||||
Steps
|
||||
-----
|
||||
1. Optional FP8 preprocessing of packed weights / scales.
|
||||
2. Sort / reset g_idx tensors for act-order handling.
|
||||
3. Repack weights via ``gptq_marlin_moe_repack``.
|
||||
4. Permute scales (and optionally extract INT8 global scales).
|
||||
5. Permute bias tensors.
|
||||
2. Repack weights via ``gptq_marlin_moe_repack``.
|
||||
3. Permute scales (and optionally extract INT8 global scales).
|
||||
4. Permute bias tensors.
|
||||
"""
|
||||
is_a_8bit = input_dtype is not None and input_dtype.itemsize == 1
|
||||
|
||||
@@ -584,8 +551,6 @@ def _process_weights_marlin(
|
||||
marlin_w2_qweight: torch.Tensor
|
||||
marlin_w13_scales: torch.Tensor
|
||||
marlin_w2_scales: torch.Tensor
|
||||
w13_g_idx_sort_indices: torch.Tensor | None = None
|
||||
w2_g_idx_sort_indices: torch.Tensor | None = None
|
||||
w13_input_global_scale: torch.Tensor | None = None
|
||||
w2_input_global_scale: torch.Tensor | None = None
|
||||
w13_bias_out: torch.Tensor | None = None
|
||||
@@ -606,13 +571,9 @@ def _process_weights_marlin(
|
||||
|
||||
# --- 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)
|
||||
@@ -627,44 +588,9 @@ def _process_weights_marlin(
|
||||
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]
|
||||
w13_g_idx_sort_indices = torch.empty_like(w13_g_idx)
|
||||
w2_g_idx_sort_indices = torch.empty_like(w2_g_idx)
|
||||
w13_sorted_g_idx = torch.empty_like(w13_g_idx)
|
||||
w2_sorted_g_idx = torch.empty_like(w2_g_idx)
|
||||
for e in range(num_experts):
|
||||
w13_g_idx_sort_indices[e] = torch.argsort(w13_g_idx[e]).to(torch.int32)
|
||||
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
|
||||
w13_g_idx = torch.nn.Parameter(
|
||||
torch.empty((num_experts, 0), dtype=torch.int32, device=device),
|
||||
requires_grad=False,
|
||||
)
|
||||
w2_g_idx = torch.nn.Parameter(
|
||||
torch.empty((num_experts, 0), dtype=torch.int32, device=device),
|
||||
requires_grad=False,
|
||||
)
|
||||
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,
|
||||
)
|
||||
|
||||
# --- Repack weights ---
|
||||
marlin_w13_qweight = ops.gptq_marlin_moe_repack(
|
||||
marlin_w13_qweight,
|
||||
w13_g_idx_sort_indices,
|
||||
marlin_w13_qweight.shape[1] * pack_factor,
|
||||
marlin_w13_qweight.shape[2],
|
||||
num_bits,
|
||||
@@ -672,7 +598,6 @@ def _process_weights_marlin(
|
||||
)
|
||||
marlin_w2_qweight = ops.gptq_marlin_moe_repack(
|
||||
marlin_w2_qweight,
|
||||
w2_g_idx_sort_indices,
|
||||
marlin_w2_qweight.shape[1] * pack_factor,
|
||||
marlin_w2_qweight.shape[2],
|
||||
num_bits,
|
||||
@@ -734,10 +659,6 @@ def _process_weights_marlin(
|
||||
marlin_w2_qweight,
|
||||
marlin_w13_scales,
|
||||
marlin_w2_scales,
|
||||
w13_g_idx,
|
||||
w2_g_idx,
|
||||
w13_g_idx_sort_indices,
|
||||
w2_g_idx_sort_indices,
|
||||
w13_qzeros,
|
||||
w2_qzeros,
|
||||
w13_input_global_scale,
|
||||
@@ -766,10 +687,6 @@ def _process_awq_weights_marlin(
|
||||
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
|
||||
@@ -782,8 +699,6 @@ def _process_awq_weights_marlin(
|
||||
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
|
||||
@@ -823,18 +738,8 @@ def _process_awq_weights_marlin(
|
||||
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,
|
||||
)
|
||||
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] * pack_factor,
|
||||
num_bits=weight_bits,
|
||||
@@ -842,7 +747,6 @@ def _process_awq_weights_marlin(
|
||||
)
|
||||
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] * pack_factor,
|
||||
num_bits=weight_bits,
|
||||
@@ -898,10 +802,6 @@ def _process_awq_weights_marlin(
|
||||
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,
|
||||
@@ -917,8 +817,6 @@ def _process_weights_cpu(
|
||||
w2: torch.Tensor,
|
||||
w13_scale: torch.Tensor,
|
||||
w2_scale: 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,
|
||||
@@ -928,10 +826,6 @@ def _process_weights_cpu(
|
||||
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
|
||||
@@ -962,7 +856,7 @@ def _process_weights_cpu(
|
||||
if isinstance(quant_config, AutoGPTQConfig) and quant_config.desc_act:
|
||||
raise NotImplementedError(
|
||||
"CPU WNA16 MoE backend does not support GPTQ with "
|
||||
"desc_act=True. The fused MoE kernel has no g_idx "
|
||||
"desc_act=True. The fused MoE kernel has no activation "
|
||||
"reordering support."
|
||||
)
|
||||
cpu_quant_algo = ops.CPUQuantAlgo.GPTQ
|
||||
@@ -1009,10 +903,6 @@ def _process_weights_cpu(
|
||||
blocked_w2,
|
||||
blocked_s13,
|
||||
blocked_s2,
|
||||
w13_g_idx,
|
||||
w2_g_idx,
|
||||
None, # w13_g_idx_sort_indices (unused on CPU)
|
||||
None, # w2_g_idx_sort_indices (unused on CPU)
|
||||
blocked_z13,
|
||||
blocked_z2,
|
||||
None, # w13_input_global_scale
|
||||
@@ -1352,10 +1242,6 @@ def _process_weights_emulation_gptq(
|
||||
w2_bf16, # w2_qweight (now bf16, not int32)
|
||||
dummy, # w13_scales (unused; nulled out in Int4EmulationTritonExperts)
|
||||
dummy, # w2_scales (unused)
|
||||
None, # w13_g_idx
|
||||
None, # w2_g_idx
|
||||
None, # w13_g_idx_sort_indices
|
||||
None, # w2_g_idx_sort_indices
|
||||
None, # w13_qzeros
|
||||
None, # w2_qzeros
|
||||
None, # w13_input_global_scale
|
||||
@@ -1413,10 +1299,6 @@ def _process_weights_emulation_awq(
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
@@ -1429,8 +1311,6 @@ def convert_to_wna16_moe_kernel_format(
|
||||
w2: torch.Tensor,
|
||||
w13_scale: torch.Tensor,
|
||||
w2_scale: 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,
|
||||
@@ -1441,10 +1321,6 @@ def convert_to_wna16_moe_kernel_format(
|
||||
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
|
||||
@@ -1509,14 +1385,12 @@ def convert_to_wna16_moe_kernel_format(
|
||||
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 = (
|
||||
-1 if quant_config.group_size is None else quant_config.group_size
|
||||
)
|
||||
actorder = quant_config.actorder
|
||||
else:
|
||||
raise TypeError(
|
||||
"Marlin WNA16 MoE backend requires AutoGPTQConfig, AutoAWQConfig or "
|
||||
@@ -1543,22 +1417,16 @@ def convert_to_wna16_moe_kernel_format(
|
||||
w2_bias,
|
||||
)
|
||||
else:
|
||||
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,
|
||||
input_dtype,
|
||||
num_bits,
|
||||
pack_factor,
|
||||
group_size,
|
||||
actorder,
|
||||
w13,
|
||||
w2,
|
||||
w13_scale,
|
||||
w2_scale,
|
||||
w13_g_idx,
|
||||
w2_g_idx,
|
||||
w13_qzeros,
|
||||
w2_qzeros,
|
||||
w13_bias,
|
||||
@@ -1571,8 +1439,6 @@ def convert_to_wna16_moe_kernel_format(
|
||||
w2,
|
||||
w13_scale,
|
||||
w2_scale,
|
||||
w13_g_idx,
|
||||
w2_g_idx,
|
||||
w13_qzeros,
|
||||
w2_qzeros,
|
||||
w13_bias,
|
||||
@@ -1584,8 +1450,6 @@ def convert_to_wna16_moe_kernel_format(
|
||||
w2,
|
||||
w13_scale,
|
||||
w2_scale,
|
||||
w13_g_idx,
|
||||
w2_g_idx,
|
||||
w13_bias,
|
||||
w2_bias,
|
||||
)
|
||||
@@ -1608,16 +1472,11 @@ def convert_to_wna16_moe_kernel_format(
|
||||
w13_bias,
|
||||
w2_bias,
|
||||
)
|
||||
empty = torch.empty((0,), dtype=torch.int32, device=w13.device)
|
||||
return (
|
||||
w13_xpu,
|
||||
w2_xpu,
|
||||
w13_scale_xpu,
|
||||
w2_scale_xpu,
|
||||
empty, # w13_g_idx
|
||||
empty, # w2_g_idx
|
||||
empty, # w13_g_idx_sort_indices
|
||||
empty, # w2_g_idx_sort_indices
|
||||
None, # w13_qzeros — sym int4 on XPU has none; kernel does uint4b8→s4
|
||||
None, # w2_qzeros
|
||||
None, # w13_input_global_scale
|
||||
@@ -1712,10 +1571,6 @@ def convert_to_wna16_moe_kernel_format(
|
||||
w2_uint8,
|
||||
w13_scale,
|
||||
w2_scale,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
w13_qzeros,
|
||||
w2_qzeros,
|
||||
None,
|
||||
|
||||
@@ -168,12 +168,6 @@ class RoutedExperts(PluggableLayer):
|
||||
"global_num_experts": moe_config.num_experts,
|
||||
}
|
||||
|
||||
# need full intermediate size pre-sharding for WNA16 act order
|
||||
if self._needs_intermediate_size_param(self.quant_method):
|
||||
moe_quant_params["intermediate_size_full"] = (
|
||||
self.moe_config.intermediate_size
|
||||
)
|
||||
|
||||
self.quant_method.create_weights(layer=self, **moe_quant_params)
|
||||
|
||||
self.lora_base_layer_prefix = ""
|
||||
@@ -206,14 +200,6 @@ class RoutedExperts(PluggableLayer):
|
||||
assert isinstance(quant_method, FusedMoEMethodBase)
|
||||
return quant_method
|
||||
|
||||
# TODO(bnell): make this a method on quant_method
|
||||
def _needs_intermediate_size_param(self, quant_method: FusedMoEMethodBase) -> bool:
|
||||
return quant_method.__class__.__name__ in (
|
||||
"AutoGPTQMoEMethod",
|
||||
"CompressedTensorsWNA16MoEMethod",
|
||||
"CompressedTensorsW4A16FlydslMoEMethod",
|
||||
)
|
||||
|
||||
def _ensure_moe_quant_config_init(self):
|
||||
if self.quant_method.moe_quant_config is None:
|
||||
# Note: the moe_quant_config can't be constructed until after
|
||||
@@ -349,7 +335,6 @@ class RoutedExperts(PluggableLayer):
|
||||
shard_id: str,
|
||||
loaded_weight: torch.Tensor,
|
||||
tp_rank: int,
|
||||
load_full_w2: bool = False,
|
||||
):
|
||||
"""
|
||||
Load grouped weight scales for group quantization or model weights
|
||||
@@ -360,17 +345,13 @@ class RoutedExperts(PluggableLayer):
|
||||
shard_id: either w1, w2, or w3
|
||||
loaded_weight: checkpoint weight to load into the param
|
||||
tp_rank: tensor parallel rank
|
||||
load_full_w2: whether or not the w2 loaded should be sharded.
|
||||
"""
|
||||
if shard_id == "w2":
|
||||
# In the case where we have actorder/g_idx, we do not partition the
|
||||
# w2 scales, as indicated by `load_full` argument, for all tp cases
|
||||
self._load_w2(
|
||||
shard_dim=shard_dim,
|
||||
loaded_weight=loaded_weight,
|
||||
expert_data=expert_data,
|
||||
tp_rank=tp_rank,
|
||||
load_full=load_full_w2,
|
||||
)
|
||||
elif shard_id in ("w1", "w3"):
|
||||
self._load_w13(
|
||||
@@ -532,13 +513,11 @@ class RoutedExperts(PluggableLayer):
|
||||
shard_dim: int,
|
||||
loaded_weight: torch.Tensor,
|
||||
tp_rank: int,
|
||||
load_full: bool = False,
|
||||
):
|
||||
# Index the loaded weight for tp sharding.
|
||||
# down_proj: "RowParallel" so tp sharding on input_dim
|
||||
# Only narrow if the loaded_weight is not a scalar (0-dim tensor)
|
||||
# and we're not loading the full weight
|
||||
if not load_full and loaded_weight.ndim > 0:
|
||||
# Only narrow if the loaded_weight is not a scalar (0-dim tensor).
|
||||
if loaded_weight.ndim > 0:
|
||||
# Same padding fix as _load_w13: use unpadded per-rank size.
|
||||
tp_size = self.moe_config.moe_parallel_config.tp_size
|
||||
loaded_per_rank = loaded_weight.shape[shard_dim] // tp_size
|
||||
@@ -571,25 +550,6 @@ class RoutedExperts(PluggableLayer):
|
||||
# _to_scalar's reshape(()) would reject the size-2 weight_shape.
|
||||
param_data[expert_id] = loaded_weight
|
||||
|
||||
def _load_g_idx(
|
||||
self,
|
||||
shard_id: str,
|
||||
expert_data: torch.Tensor,
|
||||
shard_dim: int,
|
||||
loaded_weight: torch.Tensor,
|
||||
tp_rank: int,
|
||||
):
|
||||
if shard_id == "w2":
|
||||
self._load_w2(
|
||||
shard_dim=shard_dim,
|
||||
loaded_weight=loaded_weight,
|
||||
expert_data=expert_data,
|
||||
tp_rank=tp_rank,
|
||||
)
|
||||
else:
|
||||
assert shard_id in ("w1", "w3")
|
||||
expert_data.copy_(loaded_weight)
|
||||
|
||||
@overload
|
||||
def weight_loader(
|
||||
self,
|
||||
@@ -739,17 +699,6 @@ class RoutedExperts(PluggableLayer):
|
||||
)
|
||||
return True if return_success else None
|
||||
|
||||
# Case g_idx
|
||||
if "g_idx" in weight_name:
|
||||
self._load_g_idx(
|
||||
shard_dim=0,
|
||||
shard_id=shard_id,
|
||||
loaded_weight=loaded_weight,
|
||||
expert_data=expert_data,
|
||||
tp_rank=self.moe_config.tp_rank,
|
||||
)
|
||||
return True if return_success else None
|
||||
|
||||
# TODO @dsikka: ModelOpt should follow the proper MoE loading pattern
|
||||
if "ModelOpt" in quant_method_name:
|
||||
# Determine per-tensor weight scale patterns based on variant
|
||||
@@ -853,7 +802,6 @@ class RoutedExperts(PluggableLayer):
|
||||
loaded_weight=loaded_weight,
|
||||
expert_data=expert_data,
|
||||
tp_rank=self.moe_config.tp_rank,
|
||||
load_full_w2=getattr(param, "load_full_w2", False),
|
||||
)
|
||||
elif quant_method == FusedMoeWeightScaleSupported.TENSOR.value:
|
||||
self._load_per_tensor_weight_scale(
|
||||
|
||||
Reference in New Issue
Block a user