[Refactor] refactor humming linear and moe backends to use explicit layer configs (#49610)
Signed-off-by: Jinzhen Lin <jinzhen.ljz@antgroup.com> Co-authored-by: OpenAI Codex <noreply@openai.com>
This commit is contained in:
co-authored by
OpenAI Codex
parent
e229fdbc87
commit
fcde8e1460
@@ -4,7 +4,7 @@
|
||||
|
||||
import json
|
||||
import math
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
import torch
|
||||
|
||||
@@ -62,8 +62,15 @@ from vllm.utils.import_utils import has_humming
|
||||
from vllm.v1.worker.workspace import current_workspace_manager
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.model_executor.layers.fused_moe import RoutedExperts
|
||||
from vllm.utils.humming import GemmType as HummingGemmType
|
||||
from vllm.model_executor.layers.quantization.utils.humming_utils import (
|
||||
HummingMoEQuantConfig,
|
||||
)
|
||||
from vllm.utils.humming import (
|
||||
GemmType as HummingGemmType,
|
||||
)
|
||||
from vllm.utils.humming import (
|
||||
LayerConfig as HummingLayerConfig,
|
||||
)
|
||||
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -88,15 +95,20 @@ def get_humming_moe_gemm_type() -> str:
|
||||
class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
def __init__(
|
||||
self,
|
||||
layer: "RoutedExperts",
|
||||
moe_config: FusedMoEConfig,
|
||||
quant_config: FusedMoEQuantConfig,
|
||||
max_num_tokens: int | None = None,
|
||||
num_dispatchers: int | None = None,
|
||||
):
|
||||
self.layer = layer
|
||||
self.num_experts = self.layer.local_num_experts
|
||||
self.global_num_experts = self.layer.global_num_experts
|
||||
humming_quant_config = cast("HummingMoEQuantConfig", quant_config)
|
||||
self.humming_configs: dict[str, HummingLayerConfig] = {
|
||||
"w13": humming_quant_config.w1_humming_config,
|
||||
"w2": humming_quant_config.w2_humming_config,
|
||||
}
|
||||
self.locks = torch.zeros(1024, dtype=torch.int32, device=moe_config.device)
|
||||
self.num_experts = moe_config.num_local_experts
|
||||
self.global_num_experts = moe_config.num_experts
|
||||
self.quant_config = quant_config
|
||||
self.init_humming_moe()
|
||||
|
||||
if self.is_batched():
|
||||
@@ -111,31 +123,73 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
self._permute_scratch: MoEPermuteScratch | None = None
|
||||
|
||||
def init_humming_moe(self):
|
||||
from vllm.utils.humming import HummingMethod
|
||||
from vllm.utils.humming import get_heuristics_config
|
||||
|
||||
self.compute_config = {
|
||||
"use_batch_invariant": envs.VLLM_BATCH_INVARIANT,
|
||||
"use_f16_accum": envs.VLLM_HUMMING_USE_F16_ACCUM,
|
||||
"gemm_type": self.humming_gemm_type().value,
|
||||
}
|
||||
self.w13_tuning_config = HummingMethod.get_default_tuning_configs(
|
||||
layer=self.layer,
|
||||
self.w13_tuning_config = get_heuristics_config(
|
||||
layer_config=self.humming_configs["w13"],
|
||||
use_f16_accum=envs.VLLM_HUMMING_USE_F16_ACCUM,
|
||||
use_batch_invariant=envs.VLLM_BATCH_INVARIANT,
|
||||
gemm_type=self.humming_gemm_type(),
|
||||
sublayer_name="w13",
|
||||
)
|
||||
self.w2_tuning_config = HummingMethod.get_default_tuning_configs(
|
||||
layer=self.layer,
|
||||
self.w2_tuning_config = get_heuristics_config(
|
||||
layer_config=self.humming_configs["w2"],
|
||||
use_f16_accum=envs.VLLM_HUMMING_USE_F16_ACCUM,
|
||||
use_batch_invariant=envs.VLLM_BATCH_INVARIANT,
|
||||
gemm_type=self.humming_gemm_type(),
|
||||
sublayer_name="w2",
|
||||
)
|
||||
self.compute_config_str = json.dumps(self.compute_config)
|
||||
self.w13_tuning_config_str = json.dumps(self.w13_tuning_config)
|
||||
self.w2_tuning_config_str = json.dumps(self.w2_tuning_config)
|
||||
|
||||
def quantize_input(
|
||||
self,
|
||||
sublayer_name: str,
|
||||
inputs: torch.Tensor,
|
||||
quanted_input: torch.Tensor | None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor | None]:
|
||||
from vllm.utils.humming import may_quant_input
|
||||
|
||||
return may_quant_input(
|
||||
self.humming_configs[sublayer_name],
|
||||
inputs=inputs,
|
||||
quanted_input=quanted_input,
|
||||
)
|
||||
|
||||
def humming_forward(
|
||||
self,
|
||||
sublayer_name: str,
|
||||
inputs: torch.Tensor,
|
||||
weight: torch.Tensor,
|
||||
input_scale: torch.Tensor | None,
|
||||
outputs: torch.Tensor,
|
||||
**kwargs: Any,
|
||||
) -> torch.Tensor:
|
||||
from vllm.utils.humming import humming_forward
|
||||
|
||||
is_w13 = sublayer_name == "w13"
|
||||
return humming_forward(
|
||||
self.humming_configs[sublayer_name],
|
||||
inputs=inputs,
|
||||
weight=weight,
|
||||
weight_scale=(
|
||||
self.quant_config.w1_scale if is_w13 else self.quant_config.w2_scale
|
||||
),
|
||||
zero_point=(self.quant_config.w1_zp if is_w13 else self.quant_config.w2_zp),
|
||||
bias=(self.quant_config.w1_bias if is_w13 else self.quant_config.w2_bias),
|
||||
weight_scale_2=(
|
||||
self.quant_config.g1_alphas if is_w13 else self.quant_config.g2_alphas
|
||||
),
|
||||
input_scale=input_scale,
|
||||
outputs=outputs,
|
||||
locks=self.locks,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def _get_permute_scratch(self) -> MoEPermuteScratch | None:
|
||||
if self._permute_scratch is None and moe_permute_unpermute_supported():
|
||||
self._permute_scratch = MoEPermuteScratch(
|
||||
@@ -218,8 +272,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
@property
|
||||
def expects_unquantized_inputs(self) -> bool:
|
||||
"""
|
||||
Humming kernels handle input quantization internally via
|
||||
HummingMethod.may_quant_input() in the apply() method.
|
||||
Humming kernels handle input quantization internally in apply().
|
||||
|
||||
This property tells the prepare/finalize step to skip input
|
||||
quantization (by setting defer_input_quant=True) and pass
|
||||
@@ -255,6 +308,10 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
def _supports_parallel_config(moe_parallel_config: FusedMoEParallelConfig) -> bool:
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def _supports_batch_invariance() -> bool:
|
||||
return True
|
||||
|
||||
def moe_problem_size(
|
||||
self,
|
||||
a1: torch.Tensor,
|
||||
@@ -262,10 +319,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
w2: torch.Tensor,
|
||||
topk_ids: torch.Tensor,
|
||||
) -> tuple[int, int, int, int, int]:
|
||||
from vllm.utils.humming import HummingLayerMeta
|
||||
|
||||
meta1: HummingLayerMeta = self.layer.humming_metas["w13"]
|
||||
meta2: HummingLayerMeta = self.layer.humming_metas["w2"]
|
||||
meta1 = self.humming_configs["w13"]
|
||||
meta2 = self.humming_configs["w2"]
|
||||
|
||||
assert meta1.num_experts == meta2.num_experts
|
||||
|
||||
@@ -282,25 +337,25 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
assert a1.size(0) == num_experts
|
||||
num_tokens = a1.size(1)
|
||||
|
||||
return (
|
||||
meta1.num_experts,
|
||||
num_tokens,
|
||||
# Logical intermediate width for both gated and non-gated activations
|
||||
self.layer.intermediate_size_per_partition,
|
||||
meta1.shape_k,
|
||||
top_k,
|
||||
)
|
||||
intermediate_dim = meta2.shape_k - meta2.pad_shape_k
|
||||
hidden_dim = meta1.shape_k - meta1.pad_shape_k
|
||||
return meta1.num_experts, num_tokens, intermediate_dim, hidden_dim, top_k
|
||||
|
||||
def get_buffer_metas(self, M: int, topk: int, activation: MoEActivation):
|
||||
from vllm.utils.humming import GemmType as HummingGemmType
|
||||
from vllm.utils.humming import dtypes
|
||||
|
||||
num_experts = self.num_experts
|
||||
N = self.layer.intermediate_size_per_partition
|
||||
K = self.layer.hidden_size
|
||||
w13_config = self.humming_configs["w13"]
|
||||
w2_config = self.humming_configs["w2"]
|
||||
gate_up_dim = w13_config.shape_n - w13_config.pad_shape_n
|
||||
intermediate_dim = w2_config.shape_k - w2_config.pad_shape_k
|
||||
K = w13_config.shape_k - w13_config.pad_shape_k
|
||||
assert isinstance(num_experts, int)
|
||||
assert isinstance(N, int)
|
||||
assert isinstance(gate_up_dim, int)
|
||||
assert isinstance(intermediate_dim, int)
|
||||
assert isinstance(K, int)
|
||||
assert intermediate_dim == self.adjust_N_for_activation(gate_up_dim, activation)
|
||||
|
||||
# hidden_states
|
||||
# (-> quanted_gate_up_input) (if not BF16/FP16 activation)
|
||||
@@ -310,7 +365,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
# -> down_output
|
||||
# (-> output) (if not is_batched)
|
||||
# Neighboring nodes are required to utilize distinct workspaces.
|
||||
# The output must be derived from workspace1.
|
||||
# The final output buffer is supplied by the modular kernel and may
|
||||
# alias workspace1.
|
||||
|
||||
output_shape: tuple[int, ...]
|
||||
if self.is_batched():
|
||||
@@ -327,10 +383,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
real_shape_m = M * topk
|
||||
output_shape = (M, K)
|
||||
|
||||
gate_up_size = N * (2 if activation.is_gated else 1)
|
||||
down_input_size = N
|
||||
a_dtype = self.layer.humming_metas["w13"].a_dtype
|
||||
c_dtype = self.layer.humming_metas["w13"].c_dtype
|
||||
a_dtype = self.humming_configs["w13"].a_dtype
|
||||
c_dtype = self.humming_configs["w13"].c_dtype
|
||||
num_bits = a_dtype.num_bits
|
||||
torch_dtype_map = {
|
||||
dtypes.float16: torch.float16,
|
||||
@@ -348,15 +402,15 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
"dtype": torch_dtype_map[a_dtype],
|
||||
},
|
||||
"gate_up_output": {
|
||||
"shape": (real_shape_m, gate_up_size),
|
||||
"shape": (real_shape_m, gate_up_dim),
|
||||
"dtype": torch_dtype_map[c_dtype],
|
||||
},
|
||||
"activation_output": {
|
||||
"shape": (real_shape_m, down_input_size),
|
||||
"shape": (real_shape_m, intermediate_dim),
|
||||
"dtype": torch_dtype_map[c_dtype],
|
||||
},
|
||||
"quanted_down_input": {
|
||||
"shape": (real_shape_m, down_input_size),
|
||||
"shape": (real_shape_m, intermediate_dim),
|
||||
"dtype": torch_dtype_map[a_dtype],
|
||||
},
|
||||
"down_output": {
|
||||
@@ -414,7 +468,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
|
||||
output_key = "down_output" if self.is_batched() else "output"
|
||||
output_shape = buffer_metas[output_key]["shape"]
|
||||
elem_size = self.layer.params_dtype.itemsize
|
||||
elem_size = self.moe_config.in_dtype.itemsize
|
||||
|
||||
return (
|
||||
(workspace1_nbytes // elem_size,),
|
||||
@@ -438,7 +492,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
|
||||
def make_workspaces(self, M: int, topk: int, activation: MoEActivation):
|
||||
shapes = self._workspace_shapes(M, topk, activation)
|
||||
workspace1_shape, workspace2_shape, output_shape = shapes
|
||||
torch_dtype = self.layer.params_dtype
|
||||
torch_dtype = self.moe_config.in_dtype
|
||||
workspace1, workspace2 = current_workspace_manager().get_simultaneous(
|
||||
(workspace1_shape, torch_dtype),
|
||||
(workspace2_shape, torch_dtype),
|
||||
@@ -600,12 +654,11 @@ class HummingIndexedExperts(HummingExpertsBase):
|
||||
"""
|
||||
Standard apply implementation for Humming indexed experts.
|
||||
|
||||
Note: Humming kernels handle weights and quantization internally through
|
||||
the layer object, so w1, w2, a1q_scale, a2_scale parameters are not used.
|
||||
The output is written into workspace13 via the buffer management.
|
||||
Humming performs activation quantization internally and consumes the
|
||||
weights supplied by the modular kernel interface.
|
||||
Results are written to the output buffer supplied by the modular
|
||||
kernel, which may alias workspace13.
|
||||
"""
|
||||
from vllm.utils.humming import HummingMethod
|
||||
|
||||
assert not apply_router_weight_on_input
|
||||
|
||||
hidden_states = hidden_states.view(-1, hidden_states.size(-1))
|
||||
@@ -616,6 +669,7 @@ class HummingIndexedExperts(HummingExpertsBase):
|
||||
topk_ids.size(1),
|
||||
activation,
|
||||
)
|
||||
buffers["output"] = output
|
||||
|
||||
moe_kwargs1, moe_kwargs2 = self.prepare_humming_moe_kwargs(
|
||||
topk_ids=topk_ids,
|
||||
@@ -623,19 +677,18 @@ class HummingIndexedExperts(HummingExpertsBase):
|
||||
expert_tokens_meta=expert_tokens_meta,
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w13",
|
||||
inputs=hidden_states,
|
||||
quanted_input=buffers.get("quanted_gate_up_input", None),
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w13",
|
||||
inputs=inputs,
|
||||
weight=w1,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["gate_up_output"],
|
||||
sublayer_name="w13",
|
||||
**moe_kwargs1,
|
||||
)
|
||||
|
||||
@@ -645,19 +698,18 @@ class HummingIndexedExperts(HummingExpertsBase):
|
||||
output=buffers["activation_output"],
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w2",
|
||||
inputs=buffers["activation_output"],
|
||||
quanted_input=buffers.get("quanted_down_input", None),
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w2",
|
||||
inputs=inputs,
|
||||
weight=w2,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["down_output"].view(-1, hidden_states.size(-1)),
|
||||
sublayer_name="w2",
|
||||
**moe_kwargs2,
|
||||
)
|
||||
|
||||
@@ -705,12 +757,11 @@ class HummingGroupedExperts(HummingExpertsBase):
|
||||
"""
|
||||
Standard apply implementation for Humming grouped experts.
|
||||
|
||||
Note: Humming kernels handle weights and quantization internally through
|
||||
the layer object, so w1, w2, a1q_scale, a2_scale parameters are not used.
|
||||
The output is written into workspace13 via the buffer management.
|
||||
Humming performs activation quantization internally and consumes the
|
||||
weights supplied by the modular kernel interface.
|
||||
Results are written to the output buffer supplied by the modular
|
||||
kernel, which may alias workspace13.
|
||||
"""
|
||||
from vllm.utils.humming import HummingMethod
|
||||
|
||||
assert not apply_router_weight_on_input
|
||||
|
||||
valid_shape_m = self.estimate_local_valid_shape_m(topk_ids)
|
||||
@@ -722,6 +773,7 @@ class HummingGroupedExperts(HummingExpertsBase):
|
||||
topk_ids.size(1),
|
||||
activation,
|
||||
)
|
||||
buffers["output"] = output
|
||||
|
||||
hidden_states, _, expert_first_token_offset, inv_perm, _ = moe_permute(
|
||||
hidden_states=hidden_states,
|
||||
@@ -733,23 +785,22 @@ class HummingGroupedExperts(HummingExpertsBase):
|
||||
scratch=self._get_permute_scratch(),
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w13",
|
||||
inputs=hidden_states,
|
||||
quanted_input=buffers.get("quanted_gate_up_input", None),
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w13",
|
||||
inputs=inputs,
|
||||
weight=w1,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["gate_up_output"],
|
||||
valid_shape_m=valid_shape_m,
|
||||
expert_layout=expert_first_token_offset,
|
||||
compute_config=self.compute_config_str,
|
||||
tuning_config=self.w13_tuning_config_str,
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
self.apply_activation(
|
||||
@@ -758,23 +809,22 @@ class HummingGroupedExperts(HummingExpertsBase):
|
||||
output=buffers["activation_output"],
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w2",
|
||||
inputs=buffers["activation_output"],
|
||||
quanted_input=buffers.get("quanted_down_input", None),
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w2",
|
||||
inputs=inputs,
|
||||
weight=w2,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["down_output"],
|
||||
valid_shape_m=valid_shape_m,
|
||||
expert_layout=expert_first_token_offset,
|
||||
compute_config=self.compute_config_str,
|
||||
tuning_config=self.w2_tuning_config_str,
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
moe_unpermute(
|
||||
@@ -821,12 +871,11 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
|
||||
"""
|
||||
Standard apply implementation for Humming batched grouped experts.
|
||||
|
||||
Note: Humming kernels handle weights and quantization internally through
|
||||
the layer object, so w1, w2, a1q_scale, a2_scale parameters are not used.
|
||||
The output is written into workspace13 via the buffer management.
|
||||
Humming performs activation quantization internally and consumes the
|
||||
weights supplied by the modular kernel interface.
|
||||
Results are written to the output buffer supplied by the modular
|
||||
kernel, which may alias workspace13.
|
||||
"""
|
||||
from vllm.utils.humming import HummingMethod
|
||||
|
||||
assert not apply_router_weight_on_input
|
||||
assert expert_tokens_meta is not None
|
||||
|
||||
@@ -841,24 +890,24 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
|
||||
topk_ids.size(1),
|
||||
activation,
|
||||
)
|
||||
buffers["down_output"] = output
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w13",
|
||||
inputs=hidden_states,
|
||||
quanted_input=buffers.get("quanted_gate_up_input", None),
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w13",
|
||||
inputs=inputs,
|
||||
weight=w1,
|
||||
input_scale=input_scale,
|
||||
outputs=buffers["gate_up_output"],
|
||||
valid_shape_m=valid_shape_m,
|
||||
expert_layout=expert_num_tokens,
|
||||
compute_config=self.compute_config_str,
|
||||
tuning_config=self.w13_tuning_config_str,
|
||||
sublayer_name="w13",
|
||||
)
|
||||
|
||||
self.apply_activation(
|
||||
@@ -867,21 +916,20 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
|
||||
output=buffers["activation_output"],
|
||||
)
|
||||
|
||||
inputs, input_scale = HummingMethod.may_quant_input(
|
||||
layer=self.layer,
|
||||
inputs, input_scale = self.quantize_input(
|
||||
"w2",
|
||||
inputs=buffers["activation_output"],
|
||||
quanted_input=buffers.get("quanted_down_input", None),
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
HummingMethod.forward_layer(
|
||||
layer=self.layer,
|
||||
self.humming_forward(
|
||||
"w2",
|
||||
inputs=inputs,
|
||||
weight=w2,
|
||||
input_scale=input_scale,
|
||||
outputs=output.view(-1, hidden_states.size(-1)),
|
||||
valid_shape_m=valid_shape_m,
|
||||
expert_layout=expert_num_tokens,
|
||||
compute_config=self.compute_config_str,
|
||||
tuning_config=self.w2_tuning_config_str,
|
||||
sublayer_name="w2",
|
||||
)
|
||||
|
||||
@@ -619,7 +619,12 @@ def make_fp8_moe_quant_config(
|
||||
)
|
||||
|
||||
assert isinstance(layer, RoutedExperts)
|
||||
return get_humming_moe_quant_config(layer)
|
||||
return get_humming_moe_quant_config(
|
||||
layer,
|
||||
gemm1_alpha=gemm1_alpha,
|
||||
gemm1_beta=gemm1_beta,
|
||||
gemm1_clamp_limit=swiglu_limit,
|
||||
)
|
||||
|
||||
# Flashinfer CUTLASS or HPC per-tensor uses single dq scale
|
||||
# (alpha = w_scale * a_scale) and inverse a2 scale.
|
||||
@@ -694,7 +699,6 @@ def make_fp8_moe_kernel(
|
||||
experts_cls: type[mk.FusedMoEExperts],
|
||||
fp8_backend: Fp8MoeBackend,
|
||||
routing_tables: tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None = None,
|
||||
layer: torch.nn.Module | None = None,
|
||||
) -> mk.FusedMoEKernel:
|
||||
# Create Prepare/Finalize.
|
||||
prepare_finalize = maybe_make_prepare_finalize(
|
||||
@@ -708,11 +712,6 @@ def make_fp8_moe_kernel(
|
||||
|
||||
logger.info_once("Using %s", prepare_finalize.__class__.__name__)
|
||||
|
||||
extra_kwargs = {}
|
||||
if fp8_backend == Fp8MoeBackend.HUMMING:
|
||||
assert layer is not None
|
||||
extra_kwargs = {"layer": layer}
|
||||
|
||||
# Create Experts.
|
||||
if prepare_finalize.activation_format == mk.FusedMoEActivationFormat.BatchedExperts:
|
||||
max_num_tokens = prepare_finalize.max_num_tokens_per_rank()
|
||||
@@ -722,13 +721,11 @@ def make_fp8_moe_kernel(
|
||||
quant_config=moe_quant_config,
|
||||
max_num_tokens=max_num_tokens,
|
||||
num_dispatchers=prepare_finalize.num_dispatchers(),
|
||||
**extra_kwargs,
|
||||
)
|
||||
else:
|
||||
experts = experts_cls(
|
||||
moe_config=moe_config,
|
||||
quant_config=moe_quant_config,
|
||||
**extra_kwargs,
|
||||
)
|
||||
|
||||
kernel = mk.FusedMoEKernel(
|
||||
|
||||
@@ -195,7 +195,12 @@ def make_int8_moe_quant_config(
|
||||
)
|
||||
|
||||
assert isinstance(layer, RoutedExperts)
|
||||
return get_humming_moe_quant_config(layer)
|
||||
return get_humming_moe_quant_config(
|
||||
layer,
|
||||
gemm1_alpha=getattr(layer, "swiglu_alpha", None),
|
||||
gemm1_beta=getattr(layer, "swiglu_beta", None),
|
||||
gemm1_clamp_limit=getattr(layer, "swiglu_limit", None),
|
||||
)
|
||||
|
||||
if scales_absent and not per_act_token_quant:
|
||||
return int8_w8a16_moe_quant_config(
|
||||
@@ -251,9 +256,9 @@ def convert_to_int8_moe_kernel_format(
|
||||
)
|
||||
|
||||
assert layer is not None
|
||||
# Humming reads canonical CT scales (w*_weight_scale) from the layer.
|
||||
# Online int8 produces per-channel (E, N) w*_scale; expose them as the
|
||||
# (E, N, 1) w*_weight_scale humming's loader expects.
|
||||
# The Humming conversion expects canonical CT scale parameter names.
|
||||
# Online int8 produces per-channel (E, N) w*_scale; expose them as
|
||||
# (E, N, 1) w*_weight_scale tensors before conversion.
|
||||
for sub in ("w13", "w2"):
|
||||
if hasattr(layer, f"{sub}_weight_scale"):
|
||||
continue
|
||||
@@ -279,7 +284,6 @@ def make_int8_moe_kernel(
|
||||
moe_config: FusedMoEConfig,
|
||||
experts_cls: type[mk.FusedMoEExperts],
|
||||
routing_tables: tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None = None,
|
||||
layer: torch.nn.Module | None = None,
|
||||
) -> mk.FusedMoEKernel:
|
||||
# Create Prepare/Finalize.
|
||||
prepare_finalize = maybe_make_prepare_finalize(
|
||||
@@ -293,11 +297,6 @@ def make_int8_moe_kernel(
|
||||
|
||||
logger.info_once("Using %s", prepare_finalize.__class__.__name__)
|
||||
|
||||
extra_kwargs = {}
|
||||
if int8_backend == Int8MoeBackend.HUMMING:
|
||||
assert layer is not None
|
||||
extra_kwargs = {"layer": layer}
|
||||
|
||||
# Create Experts.
|
||||
if prepare_finalize.activation_format == mk.FusedMoEActivationFormat.BatchedExperts:
|
||||
max_num_tokens = prepare_finalize.max_num_tokens_per_rank()
|
||||
@@ -307,13 +306,11 @@ def make_int8_moe_kernel(
|
||||
quant_config=moe_quant_config,
|
||||
max_num_tokens=max_num_tokens,
|
||||
num_dispatchers=prepare_finalize.num_dispatchers(),
|
||||
**extra_kwargs,
|
||||
)
|
||||
else:
|
||||
experts = experts_cls(
|
||||
moe_config=moe_config,
|
||||
quant_config=moe_quant_config,
|
||||
**extra_kwargs,
|
||||
)
|
||||
|
||||
kernel = mk.FusedMoEKernel(
|
||||
|
||||
@@ -358,7 +358,6 @@ def make_wna16_moe_kernel(
|
||||
moe_config: FusedMoEConfig,
|
||||
experts_cls: type[mk.FusedMoEExperts],
|
||||
backend: WNA16MoEBackend = WNA16MoEBackend.MARLIN,
|
||||
layer: torch.nn.Module | None = None,
|
||||
is_k_full: bool = False,
|
||||
w13_g_idx: torch.Tensor | None = None,
|
||||
w2_g_idx: torch.Tensor | None = None,
|
||||
@@ -410,10 +409,7 @@ def make_wna16_moe_kernel(
|
||||
logger.info_once("Using %s", experts_cls.__name__, scope="local")
|
||||
|
||||
extra_args: dict[str, Any] = {}
|
||||
if backend == WNA16MoEBackend.HUMMING:
|
||||
assert layer is not None
|
||||
extra_args = {"layer": layer}
|
||||
elif issubclass(experts_cls, MarlinExpertsBase):
|
||||
if issubclass(experts_cls, MarlinExpertsBase):
|
||||
extra_args = {
|
||||
"w13_g_idx": w13_g_idx,
|
||||
"w2_g_idx": w2_g_idx,
|
||||
|
||||
@@ -1788,7 +1788,6 @@ def make_mxfp4_moe_kernel(
|
||||
experts_cls: type[mk.FusedMoEExperts],
|
||||
mxfp4_backend: Mxfp4MoeBackend,
|
||||
routing_tables: tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None = None,
|
||||
layer: RoutedExperts | None = None,
|
||||
) -> mk.FusedMoEKernel:
|
||||
"""Create a FusedMoEKernel for the given MXFP4 backend."""
|
||||
is_monolithic = issubclass(experts_cls, mk.FusedMoEExpertsMonolithic)
|
||||
@@ -1805,11 +1804,6 @@ def make_mxfp4_moe_kernel(
|
||||
logger.info_once("Using %s", prepare_finalize.__class__.__name__)
|
||||
logger.info_once("Using %s", experts_cls.__name__)
|
||||
|
||||
extra_kwargs = {}
|
||||
if mxfp4_backend == Mxfp4MoeBackend.HUMMING:
|
||||
assert layer is not None
|
||||
extra_kwargs["layer"] = layer
|
||||
|
||||
# Create Experts.
|
||||
if prepare_finalize.activation_format == mk.FusedMoEActivationFormat.BatchedExperts:
|
||||
max_num_tokens = prepare_finalize.max_num_tokens_per_rank()
|
||||
@@ -1819,13 +1813,11 @@ def make_mxfp4_moe_kernel(
|
||||
quant_config=moe_quant_config,
|
||||
max_num_tokens=max_num_tokens,
|
||||
num_dispatchers=prepare_finalize.num_dispatchers(),
|
||||
**extra_kwargs,
|
||||
)
|
||||
else:
|
||||
experts = experts_cls(
|
||||
moe_config=moe_config,
|
||||
quant_config=moe_quant_config,
|
||||
**extra_kwargs,
|
||||
)
|
||||
|
||||
kernel = mk.FusedMoEKernel(
|
||||
|
||||
@@ -398,10 +398,10 @@ def convert_to_nvfp4_moe_kernel_format(
|
||||
a2_scale = None
|
||||
w13 = layer.w13_weight
|
||||
w13_scale = layer.w13_weight_scale
|
||||
w13_scale_2 = getattr(layer, "w13_global_scale", None)
|
||||
w13_scale_2 = getattr(layer, "w13_weight_scale_2", None)
|
||||
w2 = layer.w2_weight
|
||||
w2_scale = layer.w2_weight_scale
|
||||
w2_scale_2 = getattr(layer, "w2_global_scale", None)
|
||||
w2_scale_2 = getattr(layer, "w2_weight_scale_2", None)
|
||||
elif nvfp4_backend == NvFp4MoeBackend.MARLIN:
|
||||
a13_scale = None
|
||||
a2_scale = None
|
||||
@@ -485,7 +485,12 @@ def make_nvfp4_moe_quant_config(
|
||||
)
|
||||
|
||||
assert isinstance(layer, RoutedExperts)
|
||||
return get_humming_moe_quant_config(layer)
|
||||
return get_humming_moe_quant_config(
|
||||
layer,
|
||||
gemm1_alpha=getattr(layer, "swiglu_alpha", None),
|
||||
gemm1_beta=getattr(layer, "swiglu_beta", None),
|
||||
gemm1_clamp_limit=swiglu_limit,
|
||||
)
|
||||
elif backend == NvFp4MoeBackend.MARLIN:
|
||||
return nvfp4_w4a16_moe_quant_config(
|
||||
g1_alphas=w13_scale_2,
|
||||
@@ -544,7 +549,6 @@ def make_nvfp4_moe_kernel(
|
||||
experts_cls: type[mk.FusedMoEExperts],
|
||||
backend: NvFp4MoeBackend,
|
||||
routing_tables: tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None = None,
|
||||
layer: torch.nn.Module | None = None,
|
||||
per_token_activation: bool = False,
|
||||
) -> mk.FusedMoEKernel:
|
||||
# Create Prepare/Finalize.
|
||||
@@ -560,9 +564,6 @@ def make_nvfp4_moe_kernel(
|
||||
logger.info_once("Using %s", prepare_finalize.__class__.__name__)
|
||||
|
||||
extra_kwargs = {}
|
||||
if backend == NvFp4MoeBackend.HUMMING:
|
||||
assert layer is not None
|
||||
extra_kwargs = {"layer": layer}
|
||||
if backend == NvFp4MoeBackend.FLASHINFER_TRTLLM and per_token_activation:
|
||||
extra_kwargs["per_token_activation"] = True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user