[MoE Refactor] Standardize Humming MoE experts + utilities (#43373)

Signed-off-by: Bill Nell <bnell@redhat.com>
Co-authored-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
This commit is contained in:
bnellnm
2026-06-29 06:19:29 -07:00
committed by GitHub
co-authored by Robert Shaw
parent 59575da46d
commit bc8481af09
8 changed files with 1176 additions and 390 deletions
@@ -36,28 +36,42 @@ from vllm.model_executor.layers.fused_moe.utils import (
_resize_cache, _resize_cache,
swiglu_limit_func, swiglu_limit_func,
) )
from vllm.model_executor.layers.quantization.utils.quant_utils import QuantKey from vllm.model_executor.layers.quantization.utils.quant_utils import (
QuantKey,
kFp8DynamicTokenSym,
kFp8Static128BlockSym,
kFp8StaticChannelSym,
kInt4Static,
kInt8Static,
kMxfp4Dynamic,
kMxfp4Static,
kMxfp8Dynamic,
kMxfp8Static,
kNvfp4Static,
)
from vllm.platforms import current_platform from vllm.platforms import current_platform
from vllm.utils.humming import GemmType as HummingGemmType from vllm.utils.import_utils import has_humming
from vllm.utils.humming import HummingLayerMeta, HummingMethod, dtypes
from vllm.v1.worker.workspace import current_workspace_manager from vllm.v1.worker.workspace import current_workspace_manager
if TYPE_CHECKING: if TYPE_CHECKING:
from vllm.model_executor.layers.fused_moe import RoutedExperts from vllm.model_executor.layers.fused_moe import RoutedExperts
from vllm.utils.humming import GemmType as HummingGemmType
logger = init_logger(__name__) logger = init_logger(__name__)
def get_humming_moe_gemm_type() -> str: def get_humming_moe_gemm_type() -> str | None:
env_gemm_type: str = envs.VLLM_HUMMING_MOE_GEMM_TYPE or "" env_gemm_type: str | None = envs.VLLM_HUMMING_MOE_GEMM_TYPE
env_gemm_type = env_gemm_type.lower() gemm_type = None
if env_gemm_type == "indexed": if env_gemm_type is not None:
gemm_type = env_gemm_type env_gemm_type = env_gemm_type.lower()
elif env_gemm_type in ["grouped_contiguous", "grouped"]: if env_gemm_type == "indexed":
gemm_type = "grouped_contiguous" gemm_type = env_gemm_type
else: elif env_gemm_type in ["grouped_contiguous", "grouped"]:
gemm_type = "indexed" gemm_type = "grouped_contiguous"
else:
gemm_type = "indexed"
logger.info_once(f"Using {gemm_type} gemm for humming moe") # noqa logger.info_once(f"Using {gemm_type} gemm for humming moe") # noqa
return gemm_type return gemm_type
@@ -89,6 +103,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
self._permute_scratch: MoEPermuteScratch | None = None self._permute_scratch: MoEPermuteScratch | None = None
def init_humming_moe(self): def init_humming_moe(self):
from vllm.utils.humming import HummingMethod
self.compute_config = { self.compute_config = {
"use_batch_invariant": envs.VLLM_BATCH_INVARIANT, "use_batch_invariant": envs.VLLM_BATCH_INVARIANT,
"use_f16_accum": envs.VLLM_HUMMING_USE_F16_ACCUM, "use_f16_accum": envs.VLLM_HUMMING_USE_F16_ACCUM,
@@ -141,7 +157,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
return math.ceil(global_valid_shape_m * num_experts / global_num_experts) return math.ceil(global_valid_shape_m * num_experts / global_num_experts)
@staticmethod @staticmethod
def humming_gemm_type() -> HummingGemmType: def humming_gemm_type() -> "HummingGemmType":
raise NotImplementedError raise NotImplementedError
@classmethod @classmethod
@@ -153,12 +169,51 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
weight_key: QuantKey | None, weight_key: QuantKey | None,
activation_key: QuantKey | None, activation_key: QuantKey | None,
) -> bool: ) -> bool:
SUPPORTED_W_A = [
(kMxfp4Static, None),
(kMxfp4Static, kMxfp4Dynamic),
(kMxfp4Static, kMxfp8Dynamic),
(kMxfp4Static, kFp8DynamicTokenSym),
(kNvfp4Static, None),
(kNvfp4Static, kFp8DynamicTokenSym),
(kMxfp8Static, None),
(kMxfp8Static, kFp8DynamicTokenSym),
(kFp8StaticChannelSym, None),
(kFp8StaticChannelSym, kFp8DynamicTokenSym),
(kFp8Static128BlockSym, None),
(kFp8Static128BlockSym, kFp8DynamicTokenSym),
(kInt4Static, None),
(kInt4Static, kFp8DynamicTokenSym),
(kInt8Static, None),
(kInt8Static, kFp8DynamicTokenSym),
]
return (weight_key, activation_key) in SUPPORTED_W_A
@property
def expects_unquantized_inputs(self) -> bool:
"""
Humming kernels handle input quantization internally via
HummingMethod.may_quant_input() in the apply() method.
This property tells the prepare/finalize step to skip input
quantization (by setting defer_input_quant=True) and pass
unquantized inputs to the experts. This prevents double
quantization: once in prepare and once in Humming's apply().
Returns:
True to indicate that this expert expects unquantized inputs
and will handle quantization internally.
"""
return True return True
@staticmethod @staticmethod
def _supports_current_device() -> bool: def _supports_current_device() -> bool:
platform = current_platform platform = current_platform
return platform.is_cuda() and platform.has_device_capability((7, 5)) return (
has_humming()
and platform.is_cuda()
and platform.has_device_capability((7, 5))
)
@staticmethod @staticmethod
def _supports_no_act_and_mul() -> bool: def _supports_no_act_and_mul() -> bool:
@@ -182,10 +237,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
@staticmethod @staticmethod
def _supports_parallel_config(moe_parallel_config: FusedMoEParallelConfig) -> bool: def _supports_parallel_config(moe_parallel_config: FusedMoEParallelConfig) -> bool:
return not ( return True
moe_parallel_config.use_fi_nvl_two_sided_kernels
or moe_parallel_config.use_fi_nvl_one_sided_kernels
)
def moe_problem_size( def moe_problem_size(
self, self,
@@ -194,6 +246,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
w2: torch.Tensor, w2: torch.Tensor,
topk_ids: torch.Tensor, topk_ids: torch.Tensor,
) -> tuple[int, int, int, int, int]: ) -> tuple[int, int, int, int, int]:
from vllm.utils.humming import HummingLayerMeta
meta1: HummingLayerMeta = self.layer.humming_metas["w13"] meta1: HummingLayerMeta = self.layer.humming_metas["w13"]
meta2: HummingLayerMeta = self.layer.humming_metas["w2"] meta2: HummingLayerMeta = self.layer.humming_metas["w2"]
@@ -215,6 +269,9 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
return meta1.num_experts, num_tokens, meta1.shape_n // 2, meta1.shape_k, top_k return meta1.num_experts, num_tokens, meta1.shape_n // 2, meta1.shape_k, top_k
def get_buffer_metas(self, M: int, topk: int, activation: MoEActivation): 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 num_experts = self.num_experts
N = self.layer.intermediate_size_per_partition N = self.layer.intermediate_size_per_partition
K = self.layer.hidden_size K = self.layer.hidden_size
@@ -254,7 +311,9 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
torch_dtype_map = { torch_dtype_map = {
dtypes.float16: torch.float16, dtypes.float16: torch.float16,
dtypes.bfloat16: torch.bfloat16, dtypes.bfloat16: torch.bfloat16,
dtypes.float32: torch.float32,
dtypes.float8e4m3: torch.float8_e4m3fn, dtypes.float8e4m3: torch.float8_e4m3fn,
dtypes.float8e5m2: torch.float8_e5m2,
dtypes.int8: torch.int8, dtypes.int8: torch.int8,
dtypes.int4: torch.uint8, dtypes.int4: torch.uint8,
} }
@@ -289,7 +348,13 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
for key in buffer_metas: for key in buffer_metas:
meta = buffer_metas[key] meta = buffer_metas[key]
if "quanted" in key and a_dtype.num_bits == 4: if "quanted" in key and a_dtype.num_bits == 4:
meta["shape"] = meta["shape"][:-1] + (meta["shape"][-1] // 2,) last_dim = meta["shape"][-1]
if last_dim % 2 != 0:
raise ValueError(
f"Int4 packing requires last dimension to be even, "
f"got {last_dim} for buffer '{key}'"
)
meta["shape"] = meta["shape"][:-1] + (last_dim // 2,)
if num_bits == 16: if num_bits == 16:
required_buffers = ["gate_up_output", "activation_output", "down_output"] required_buffers = ["gate_up_output", "activation_output", "down_output"]
@@ -325,8 +390,13 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
output_key = "down_output" if self.is_batched() else "output" output_key = "down_output" if self.is_batched() else "output"
output_shape = buffer_metas[output_key]["shape"] output_shape = buffer_metas[output_key]["shape"]
elem_size = self.layer.params_dtype.itemsize
return (workspace1_nbytes // 2,), (workspace2_nbytes // 2,), output_shape return (
(workspace1_nbytes // elem_size,),
(workspace2_nbytes // elem_size,),
output_shape,
)
def workspace_shapes( def workspace_shapes(
self, self,
@@ -344,7 +414,7 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
def make_workspaces(self, M: int, topk: int, activation: MoEActivation): def make_workspaces(self, M: int, topk: int, activation: MoEActivation):
shapes = self._workspace_shapes(M, topk, activation) shapes = self._workspace_shapes(M, topk, activation)
workspace1_shape, workspace2_shape, output_shape = shapes workspace1_shape, workspace2_shape, output_shape = shapes
torch_dtype = self.layer.param_dtype torch_dtype = self.layer.params_dtype
workspace1, workspace2 = current_workspace_manager().get_simultaneous( workspace1, workspace2 = current_workspace_manager().get_simultaneous(
(workspace1_shape, torch_dtype), (workspace1_shape, torch_dtype),
(workspace2_shape, torch_dtype), (workspace2_shape, torch_dtype),
@@ -370,45 +440,8 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
return buffers return buffers
def apply( # Note: apply method is implemented by subclasses following the
self, # standard FusedMoEExpertsModular.apply signature
output: torch.Tensor,
hidden_states: torch.Tensor,
w1: torch.Tensor,
w2: torch.Tensor,
topk_weights: torch.Tensor,
topk_ids: torch.Tensor,
activation: MoEActivation,
global_num_experts: int,
expert_map: torch.Tensor | None,
a1q_scale: torch.Tensor | None,
a2_scale: torch.Tensor | None,
workspace13: torch.Tensor,
workspace2: torch.Tensor,
expert_tokens_meta: mk.ExpertTokensMetadata | None,
apply_router_weight_on_input: bool,
):
assert not apply_router_weight_on_input
self.main_apply(
hidden_states=hidden_states,
topk_weights=topk_weights,
topk_ids=topk_ids,
workspace1=workspace13,
workspace2=workspace2,
expert_tokens_meta=expert_tokens_meta,
)
def main_apply(
self,
hidden_states: torch.Tensor,
topk_weights: torch.Tensor,
topk_ids: torch.Tensor,
workspace1: torch.Tensor,
workspace2: torch.Tensor,
expert_tokens_meta: mk.ExpertTokensMetadata | None,
):
raise NotImplementedError
@staticmethod @staticmethod
def is_supported_config( def is_supported_config(
@@ -418,24 +451,27 @@ class HummingExpertsBase(mk.FusedMoEExpertsModular):
activation_key: QuantKey | None, activation_key: QuantKey | None,
activation_format: mk.FusedMoEActivationFormat, activation_format: mk.FusedMoEActivationFormat,
) -> tuple[bool, str | None]: ) -> tuple[bool, str | None]:
if activation_format == mk.FusedMoEActivationFormat.BatchedExperts: supported, reason = mk.FusedMoEExpertsModular.is_supported_config(
supported = cls.activation_format() == activation_format cls,
reason = "activation_format mismatched" moe_config,
elif activation_format == mk.FusedMoEActivationFormat.Standard: weight_key,
if cls.activation_format() != mk.FusedMoEActivationFormat.Standard: activation_key,
supported = False activation_format,
reason = "activation_format mismatched" )
else:
assert hasattr(cls, "humming_gemm_type")
gemm_type = cls.humming_gemm_type().value.lower()
preferred_gemm_type = get_humming_moe_gemm_type().lower()
supported = preferred_gemm_type == gemm_type
reason = "preferred gemm type mismatched"
else:
supported = False
reason = "unsupported activation_format"
return supported, None if supported else reason if supported:
assert hasattr(cls, "humming_gemm_type")
gemm_type = cls.humming_gemm_type().value.lower()
preferred_gemm_type = get_humming_moe_gemm_type()
if preferred_gemm_type is not None:
supported = preferred_gemm_type.lower() == gemm_type
if not supported:
reason = (
f"preferred gemm type {preferred_gemm_type} != "
f"supported gemm type {gemm_type}"
)
return supported, reason
def apply_activation( def apply_activation(
self, self,
@@ -459,7 +495,9 @@ class HummingIndexedExperts(HummingExpertsBase):
return mk.FusedMoEActivationFormat.Standard return mk.FusedMoEActivationFormat.Standard
@staticmethod @staticmethod
def humming_gemm_type() -> HummingGemmType: def humming_gemm_type() -> "HummingGemmType":
from vllm.utils.humming import GemmType as HummingGemmType
return HummingGemmType.INDEXED return HummingGemmType.INDEXED
def prepare_humming_moe_kwargs( def prepare_humming_moe_kwargs(
@@ -470,12 +508,18 @@ class HummingIndexedExperts(HummingExpertsBase):
) -> tuple[dict[str, Any], dict[str, Any]]: ) -> tuple[dict[str, Any], dict[str, Any]]:
valid_shape_m = self.estimate_local_valid_shape_m(topk_ids) valid_shape_m = self.estimate_local_valid_shape_m(topk_ids)
moe_block_size = None
for min_shape_m, max_shape_m, config in self.w13_tuning_config: for min_shape_m, max_shape_m, config in self.w13_tuning_config:
if valid_shape_m > min_shape_m and valid_shape_m <= max_shape_m: if valid_shape_m > min_shape_m and valid_shape_m <= max_shape_m:
moe_block_size = config["block_shape"][0] moe_block_size = config["block_shape"][0]
break break
else:
raise ValueError(f"cannot found moe_block_size for shape {valid_shape_m}") if moe_block_size is None:
logger.warning_once(
"No tuning config found for shape %s, using default block_size=64",
valid_shape_m,
)
moe_block_size = 64
sorted_ids, expert_ids, num_tokens_padded = moe_align_block_size( sorted_ids, expert_ids, num_tokens_padded = moe_align_block_size(
topk_ids=topk_ids, topk_ids=topk_ids,
@@ -501,27 +545,47 @@ class HummingIndexedExperts(HummingExpertsBase):
return moe_kwargs1, moe_kwargs2 return moe_kwargs1, moe_kwargs2
def main_apply( def apply(
self, self,
output: torch.Tensor,
hidden_states: torch.Tensor, hidden_states: torch.Tensor,
w1: torch.Tensor,
w2: torch.Tensor,
topk_weights: torch.Tensor, topk_weights: torch.Tensor,
topk_ids: torch.Tensor, topk_ids: torch.Tensor,
workspace1: torch.Tensor, activation: MoEActivation,
global_num_experts: int,
expert_map: torch.Tensor | None,
a1q_scale: torch.Tensor | None,
a2_scale: torch.Tensor | None,
workspace13: torch.Tensor,
workspace2: torch.Tensor, workspace2: torch.Tensor,
expert_tokens_meta: mk.ExpertTokensMetadata | None, expert_tokens_meta: mk.ExpertTokensMetadata | None,
): apply_router_weight_on_input: bool,
) -> None:
"""
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.
"""
from vllm.utils.humming import HummingMethod
assert not apply_router_weight_on_input
hidden_states = hidden_states.view(-1, hidden_states.size(-1)) hidden_states = hidden_states.view(-1, hidden_states.size(-1))
buffers = self.prepare_buffers( buffers = self.prepare_buffers(
workspace1, workspace13,
workspace2, workspace2,
topk_ids.size(0), topk_ids.size(0),
topk_ids.size(1), topk_ids.size(1),
self.layer.activation, activation,
) )
moe_kwargs1, moe_kwargs2 = self.prepare_humming_moe_kwargs( moe_kwargs1, moe_kwargs2 = self.prepare_humming_moe_kwargs(
topk_ids=topk_ids, topk_ids=topk_ids,
expert_map=self.layer.expert_map, expert_map=expert_map,
expert_tokens_meta=expert_tokens_meta, expert_tokens_meta=expert_tokens_meta,
) )
@@ -542,7 +606,7 @@ class HummingIndexedExperts(HummingExpertsBase):
) )
self.apply_activation( self.apply_activation(
activation=self.layer.activation, activation=activation,
input=buffers["gate_up_output"], input=buffers["gate_up_output"],
output=buffers["activation_output"], output=buffers["activation_output"],
) )
@@ -567,10 +631,13 @@ class HummingIndexedExperts(HummingExpertsBase):
inputs=buffers["down_output"].view(*topk_ids.shape, -1), inputs=buffers["down_output"].view(*topk_ids.shape, -1),
topk_weights=topk_weights, topk_weights=topk_weights,
topk_ids=topk_ids, topk_ids=topk_ids,
expert_map=self.layer.expert_map, expert_map=expert_map,
outputs=buffers["output"], outputs=buffers["output"],
) )
# Note: output is already written to buffers["output"]
# which aliases workspace13/output
class HummingGroupedExperts(HummingExpertsBase): class HummingGroupedExperts(HummingExpertsBase):
def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce:
@@ -581,35 +648,57 @@ class HummingGroupedExperts(HummingExpertsBase):
return mk.FusedMoEActivationFormat.Standard return mk.FusedMoEActivationFormat.Standard
@staticmethod @staticmethod
def humming_gemm_type() -> HummingGemmType: def humming_gemm_type() -> "HummingGemmType":
from vllm.utils.humming import GemmType as HummingGemmType
return HummingGemmType.GROUPED_CONTIGUOUS return HummingGemmType.GROUPED_CONTIGUOUS
def main_apply( def apply(
self, self,
output: torch.Tensor,
hidden_states: torch.Tensor, hidden_states: torch.Tensor,
w1: torch.Tensor,
w2: torch.Tensor,
topk_weights: torch.Tensor, topk_weights: torch.Tensor,
topk_ids: torch.Tensor, topk_ids: torch.Tensor,
workspace1: torch.Tensor, activation: MoEActivation,
global_num_experts: int,
expert_map: torch.Tensor | None,
a1q_scale: torch.Tensor | None,
a2_scale: torch.Tensor | None,
workspace13: torch.Tensor,
workspace2: torch.Tensor, workspace2: torch.Tensor,
expert_tokens_meta: mk.ExpertTokensMetadata | None, expert_tokens_meta: mk.ExpertTokensMetadata | None,
): apply_router_weight_on_input: bool,
) -> None:
"""
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.
"""
from vllm.utils.humming import HummingMethod
assert not apply_router_weight_on_input
valid_shape_m = self.estimate_local_valid_shape_m(topk_ids) valid_shape_m = self.estimate_local_valid_shape_m(topk_ids)
buffers = self.prepare_buffers( buffers = self.prepare_buffers(
workspace1, workspace13,
workspace2, workspace2,
topk_ids.size(0), topk_ids.size(0),
topk_ids.size(1), topk_ids.size(1),
self.layer.activation, activation,
) )
hidden_states, _, expert_first_token_offset, inv_perm, _ = moe_permute( hidden_states, _, expert_first_token_offset, inv_perm, _ = moe_permute(
hidden_states=hidden_states, hidden_states=hidden_states,
a1q_scale=None, a1q_scale=None,
topk_ids=topk_ids, topk_ids=topk_ids,
n_expert=self.global_num_experts, n_expert=global_num_experts,
n_local_expert=self.num_experts, n_local_expert=self.num_experts,
expert_map=self.layer.expert_map, expert_map=expert_map,
scratch=self._get_permute_scratch(), scratch=self._get_permute_scratch(),
) )
@@ -633,7 +722,7 @@ class HummingGroupedExperts(HummingExpertsBase):
) )
self.apply_activation( self.apply_activation(
activation=self.layer.activation, activation=activation,
input=buffers["gate_up_output"], input=buffers["gate_up_output"],
output=buffers["activation_output"], output=buffers["activation_output"],
) )
@@ -665,6 +754,9 @@ class HummingGroupedExperts(HummingExpertsBase):
expert_first_token_offset=expert_first_token_offset, expert_first_token_offset=expert_first_token_offset,
) )
# Note: output is already written to buffers["output"]
# which aliases workspace13/output
class BatchedHummingGroupedExperts(HummingExpertsBase): class BatchedHummingGroupedExperts(HummingExpertsBase):
def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce:
@@ -675,29 +767,51 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
return mk.FusedMoEActivationFormat.BatchedExperts return mk.FusedMoEActivationFormat.BatchedExperts
@staticmethod @staticmethod
def humming_gemm_type() -> HummingGemmType: def humming_gemm_type() -> "HummingGemmType":
from vllm.utils.humming import GemmType as HummingGemmType
return HummingGemmType.GROUPED_MASKED return HummingGemmType.GROUPED_MASKED
def main_apply( def apply(
self, self,
output: torch.Tensor,
hidden_states: torch.Tensor, hidden_states: torch.Tensor,
w1: torch.Tensor,
w2: torch.Tensor,
topk_weights: torch.Tensor, topk_weights: torch.Tensor,
topk_ids: torch.Tensor, topk_ids: torch.Tensor,
workspace1: torch.Tensor, activation: MoEActivation,
global_num_experts: int,
expert_map: torch.Tensor | None,
a1q_scale: torch.Tensor | None,
a2_scale: torch.Tensor | None,
workspace13: torch.Tensor,
workspace2: torch.Tensor, workspace2: torch.Tensor,
expert_tokens_meta: mk.ExpertTokensMetadata | None, expert_tokens_meta: mk.ExpertTokensMetadata | None,
): apply_router_weight_on_input: bool,
) -> None:
"""
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.
"""
from vllm.utils.humming import HummingMethod
assert not apply_router_weight_on_input
assert expert_tokens_meta is not None assert expert_tokens_meta is not None
hidden_states = hidden_states.view(-1, hidden_states.size(-1)) hidden_states = hidden_states.view(-1, hidden_states.size(-1))
valid_shape_m = self.estimate_local_valid_shape_m(topk_ids) valid_shape_m = self.estimate_local_valid_shape_m(topk_ids)
expert_num_tokens = expert_tokens_meta.expert_num_tokens expert_num_tokens = expert_tokens_meta.expert_num_tokens
buffers = self.prepare_buffers( buffers = self.prepare_buffers(
workspace1, workspace13,
workspace2, workspace2,
topk_ids.size(0), topk_ids.size(0),
topk_ids.size(1), topk_ids.size(1),
self.layer.activation, activation,
) )
inputs, input_scale = HummingMethod.may_quant_input( inputs, input_scale = HummingMethod.may_quant_input(
@@ -720,7 +834,7 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
) )
self.apply_activation( self.apply_activation(
activation=self.layer.activation, activation=activation,
input=buffers["gate_up_output"], input=buffers["gate_up_output"],
output=buffers["activation_output"], output=buffers["activation_output"],
) )
@@ -743,3 +857,6 @@ class BatchedHummingGroupedExperts(HummingExpertsBase):
tuning_config=self.w2_tuning_config_str, tuning_config=self.w2_tuning_config_str,
sublayer_name="w2", sublayer_name="w2",
) )
# Note: output is already written to buffers["down_output"]
# which aliases workspace13/output
@@ -711,10 +711,12 @@ def convert_gpt_oss_weight_to_mxfp4_moe_kernel_format(
if mxfp4_backend == Mxfp4MoeBackend.HUMMING: if mxfp4_backend == Mxfp4MoeBackend.HUMMING:
from vllm.model_executor.layers.quantization.utils.humming_utils import ( from vllm.model_executor.layers.quantization.utils.humming_utils import (
prepare_humming_moe_layer, convert_to_humming_moe_kernel_format,
) )
prepare_humming_moe_layer(layer, {"quant_method": "gpt_oss_mxfp4"}) convert_to_humming_moe_kernel_format(
layer, quant_config={"quant_method": "gpt_oss_mxfp4"}
)
return ( return (
layer.w13_weight, layer.w13_weight,
layer.w2_weight, layer.w2_weight,
@@ -1277,10 +1279,12 @@ def convert_weight_to_mxfp4_moe_kernel_format(
if mxfp4_backend == Mxfp4MoeBackend.HUMMING: if mxfp4_backend == Mxfp4MoeBackend.HUMMING:
from vllm.model_executor.layers.quantization.utils.humming_utils import ( from vllm.model_executor.layers.quantization.utils.humming_utils import (
prepare_humming_moe_layer, convert_to_humming_moe_kernel_format,
) )
prepare_humming_moe_layer(layer, {"quant_method": "mxfp4"}) convert_to_humming_moe_kernel_format(
layer, quant_config={"quant_method": "mxfp4"}
)
return ( return (
layer.w13_weight, layer.w13_weight,
layer.w2_weight, layer.w2_weight,
@@ -1569,7 +1573,7 @@ def make_mxfp4_moe_quant_config(
w2_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None,
a1_scale: torch.Tensor | None = None, a1_scale: torch.Tensor | None = None,
a2_scale: torch.Tensor | None = None, a2_scale: torch.Tensor | None = None,
layer: torch.nn.Module | None = None, layer: "RoutedExperts | None" = None,
) -> FusedMoEQuantConfig | None: ) -> FusedMoEQuantConfig | None:
"""Create a FusedMoEQuantConfig for the given MXFP4 backend.""" """Create a FusedMoEQuantConfig for the given MXFP4 backend."""
if mxfp4_backend == Mxfp4MoeBackend.DEEPGEMM_MXFP4: if mxfp4_backend == Mxfp4MoeBackend.DEEPGEMM_MXFP4:
@@ -1662,7 +1666,7 @@ def make_mxfp4_moe_quant_config(
get_humming_moe_quant_config, get_humming_moe_quant_config,
) )
assert isinstance(layer, RoutedExperts) assert layer is not None
return get_humming_moe_quant_config( return get_humming_moe_quant_config(
layer, layer,
gemm1_alpha=gemm1_alpha, gemm1_alpha=gemm1_alpha,
@@ -1703,6 +1707,7 @@ def make_mxfp4_moe_kernel(
assert prepare_finalize is not None assert prepare_finalize is not None
logger.info_once("Using %s", prepare_finalize.__class__.__name__) logger.info_once("Using %s", prepare_finalize.__class__.__name__)
logger.info_once("Using %s", experts_cls.__name__)
extra_kwargs = {} extra_kwargs = {}
if mxfp4_backend == Mxfp4MoeBackend.HUMMING: if mxfp4_backend == Mxfp4MoeBackend.HUMMING:
@@ -143,6 +143,7 @@ class CompressedTensorsW4A4Mxfp4MoEMethod(CompressedTensorsMoEMethod):
mxfp4_backend=self.mxfp4_backend, mxfp4_backend=self.mxfp4_backend,
w1_scale=layer.w13_weight_scale, w1_scale=layer.w13_weight_scale,
w2_scale=layer.w2_weight_scale, w2_scale=layer.w2_weight_scale,
layer=layer,
) )
def process_weights_after_loading(self, layer: RoutedExperts) -> None: def process_weights_after_loading(self, layer: RoutedExperts) -> None:
@@ -706,15 +706,15 @@ class Fp8MoEMethod(FusedMoEMethodBase):
layer.w2_weight.is_shuffled = True layer.w2_weight.is_shuffled = True
self.moe_quant_config = self.get_fused_moe_quant_config(layer) self.moe_quant_config = self.get_fused_moe_quant_config(layer)
if self.moe_quant_config: assert self.moe_quant_config is not None
assert self.experts_cls is not None assert self.experts_cls is not None
self.moe_kernel = make_fp8_moe_kernel( self.moe_kernel = make_fp8_moe_kernel(
moe_quant_config=self.moe_quant_config, moe_quant_config=self.moe_quant_config,
moe_config=self.moe, moe_config=self.moe,
fp8_backend=self.fp8_backend, fp8_backend=self.fp8_backend,
experts_cls=self.experts_cls, experts_cls=self.experts_cls,
routing_tables=layer._expert_routing_tables(), routing_tables=layer._expert_routing_tables(),
) )
def process_weights_after_loading(self, layer: RoutedExperts) -> None: def process_weights_after_loading(self, layer: RoutedExperts) -> None:
# Allow for accessing weights and scales in standard way. # Allow for accessing weights and scales in standard way.
@@ -30,6 +30,14 @@ from vllm.model_executor.layers.quantization.base_config import (
QuantizationConfig, QuantizationConfig,
QuantizeMethodBase, QuantizeMethodBase,
) )
from vllm.model_executor.layers.quantization.utils.humming_utils import (
convert_to_humming_moe_kernel_format,
get_humming_moe_quant_config,
input_schema_to_quant_key,
make_humming_moe_kernel,
select_humming_moe_experts,
weight_schema_to_quant_key,
)
from vllm.model_executor.model_loader.weight_utils import default_weight_loader from vllm.model_executor.model_loader.weight_utils import default_weight_loader
from vllm.model_executor.parameter import ( from vllm.model_executor.parameter import (
BasevLLMParameter, BasevLLMParameter,
@@ -106,7 +114,7 @@ def prepare_param(tensor, name, extra_attrs):
return param return param
def prepare_moe_param(tensor, name, extra_attrs): def prepare_moe_param(tensor: torch.Tensor, name: str, extra_attrs: dict[str, Any]):
param = torch.nn.Parameter(tensor, requires_grad=False) param = torch.nn.Parameter(tensor, requires_grad=False)
if "scale_type" in extra_attrs: if "scale_type" in extra_attrs:
extra_attrs["quant_method"] = extra_attrs["scale_type"] extra_attrs["quant_method"] = extra_attrs["scale_type"]
@@ -605,12 +613,27 @@ class HummingMoEMethod(FusedMoEMethodBase):
) -> None: ) -> None:
super().__init__(moe) super().__init__(moe)
self.quant_config = quant_config self.quant_config = quant_config
self.moe = moe
self.weight_schema = quant_config.weight_schema self.weight_schema = quant_config.weight_schema
self.input_schema = quant_config.input_schema self.input_schema = quant_config.input_schema
self.force_weight_schema = quant_config.force_weight_schema self.force_weight_schema = quant_config.force_weight_schema
self.force_input_schema = quant_config.force_input_schema self.force_input_schema = quant_config.force_input_schema
# Derive QuantKeys from humming schemas.
# Prefer force schemas (the final format after requant) over base.
weight_key = weight_schema_to_quant_key(
self.force_weight_schema or self.weight_schema
)
activation_key = input_schema_to_quant_key(
self.force_input_schema or self.input_schema
)
# Select Humming MoE experts
self.experts_cls = select_humming_moe_experts(
config=self.moe,
weight_key=weight_key,
activation_key=activation_key,
)
def prepare_weight_loader(self, layer, weight_loader): def prepare_weight_loader(self, layer, weight_loader):
def new_weight_loader( def new_weight_loader(
param: torch.nn.Parameter, param: torch.nn.Parameter,
@@ -647,7 +670,7 @@ class HummingMoEMethod(FusedMoEMethodBase):
sublayer_name = "w2" if shard_id == "w2" else "w13" sublayer_name = "w2" if shard_id == "w2" else "w13"
param = getattr(layer, sublayer_name + "_" + key) param = getattr(layer, sublayer_name + "_" + key)
part_subccess = param.weight_loader( part_success = param.weight_loader(
param=param, param=param,
loaded_weight=tensor.cpu(), loaded_weight=tensor.cpu(),
weight_name=shard_id + "_" + key, weight_name=shard_id + "_" + key,
@@ -655,7 +678,7 @@ class HummingMoEMethod(FusedMoEMethodBase):
expert_id=expert_id, expert_id=expert_id,
return_success=return_success, return_success=return_success,
) )
success = success and part_subccess success = success and part_success
return success if return_success else None return success if return_success else None
@@ -677,7 +700,7 @@ class HummingMoEMethod(FusedMoEMethodBase):
def create_weights( def create_weights(
self, self,
layer: torch.nn.Module, layer: RoutedExperts,
num_experts: int, num_experts: int,
hidden_size: int, hidden_size: int,
intermediate_size_per_partition: int, intermediate_size_per_partition: int,
@@ -734,160 +757,35 @@ class HummingMoEMethod(FusedMoEMethodBase):
locks = torch.zeros(1024, dtype=torch.int32) locks = torch.zeros(1024, dtype=torch.int32)
layer.register_buffer("locks", locks) layer.register_buffer("locks", locks)
def get_fused_moe_quant_config(self, layer: torch.nn.Module) -> FusedMoEQuantConfig: def get_fused_moe_quant_config(self, layer: RoutedExperts) -> FusedMoEQuantConfig:
from vllm.model_executor.layers.quantization.utils.humming_utils import (
get_humming_moe_quant_config,
)
return get_humming_moe_quant_config(layer) return get_humming_moe_quant_config(layer)
def process_weights_after_loading(self, layer: RoutedExperts) -> None: def process_weights_after_loading(self, layer: RoutedExperts) -> None:
if getattr(self, "processed", False): if getattr(self, "processed", False):
return return
self.processed = True self.processed = True
layer.weight_schemas = {}
layer.input_schemas = {}
for sublayer_name, configs in layer.sublayer_configs.items():
input_schema = self.input_schema
weight_schema = self.weight_schema
# convert from checkpoint format to humming format
if not isinstance(weight_schema, _hm.HummingWeightSchema):
tensors: dict[str, torch.Tensor] = dict(
(key.removeprefix(sublayer_name + "_"), value)
for key, value in layer.state_dict().items()
if key.startswith(sublayer_name + "_")
)
shape_k_stacks = [configs["shape_k"]] # Convert weights to Humming kernel format
shape_n_stacks = [configs["shape_n"]] convert_to_humming_moe_kernel_format(
if sublayer_name == "w13": layer=layer,
shape_n_stacks = [configs["shape_n"] // 2] * 2 sublayer_configs=layer.sublayer_configs,
weight_schema=self.weight_schema,
weight_schema, tensors = weight_schema.convert_humming( input_schema=self.input_schema,
tensors=tensors, force_weight_schema=self.force_weight_schema,
shape_n_stacks=shape_n_stacks,
shape_k_stacks=shape_k_stacks,
param_dtype=layer.param_dtype,
num_experts=layer.num_experts,
)
input_schema, _ = input_schema.convert_humming(
tensors=tensors,
shape_n_stacks=shape_n_stacks,
shape_k_stacks=shape_k_stacks,
param_dtype=layer.param_dtype,
num_experts=layer.num_experts,
)
for name, _ in list(layer.named_parameters()):
if not name.startswith(sublayer_name + "_"):
continue
delattr(layer, name)
for name, tensor in tensors.items():
name = f"{sublayer_name}_{name}"
param = torch.nn.Parameter(tensor, requires_grad=False)
setattr(layer, name, param)
layer.weight_schemas[sublayer_name] = weight_schema
layer.input_schemas[sublayer_name] = input_schema
# force requant (origin quant setting -> fp16/bf16 -> new_quant setting)
assert isinstance(weight_schema, _hm.HummingWeightSchema)
force_requant = self.force_weight_schema is not None
if force_requant and weight_schema != self.force_weight_schema:
tensors = dict(
(key.removeprefix(sublayer_name + "_"), value)
for key, value in layer.state_dict().items()
if key.startswith(sublayer_name + "_")
)
tensors = weight_schema.requant_tensors(
tensors=tensors,
target_weight_schema=self.force_weight_schema,
param_dtype=layer.param_dtype,
)
weight_schema = self.force_weight_schema
for name, _ in list(layer.named_parameters()):
if not name.startswith(sublayer_name + "_"):
continue
if name == sublayer_name + "_bias":
continue
delattr(layer, name)
for name, tensor in tensors.items():
name = f"{sublayer_name}_{name}"
param = torch.nn.Parameter(tensor, requires_grad=False)
setattr(layer, name, param)
del tensors
# prepare layer config from humming kernel
_hm.HummingMethod.prepare_layer_meta(
layer=layer,
shape_n=configs["shape_n"],
shape_k=configs["shape_k"],
pad_n_to_multiple=256,
pad_k_to_multiple=128,
input_schema=input_schema,
weight_schema=weight_schema,
has_bias=self.moe.has_bias,
num_experts=layer.num_experts,
torch_dtype=layer.param_dtype,
sublayer_name=sublayer_name,
)
# preprocess weight for inference
_hm.HummingMethod.transform_humming_layer(
layer, sublayer_name=sublayer_name
)
from vllm.model_executor.layers.fused_moe.experts.fused_humming_moe import (
HummingGroupedExperts,
HummingIndexedExperts,
get_humming_moe_gemm_type,
) )
# use moe modular # Build the MoE kernel
experts: HummingIndexedExperts | HummingGroupedExperts self.moe_quant_config = self.get_fused_moe_quant_config(layer)
layer._ensure_moe_quant_config_init()
assert self.moe_quant_config is not None assert self.moe_quant_config is not None
if get_humming_moe_gemm_type() == "indexed": assert self.experts_cls is not None
experts = HummingIndexedExperts(layer, self.moe, self.moe_quant_config) self.moe_kernel = make_humming_moe_kernel(
else: self.moe_quant_config,
experts = HummingGroupedExperts(layer, self.moe, self.moe_quant_config) self.moe,
self.experts = experts self.experts_cls,
layer=layer,
def select_gemm_impl( routing_tables=layer._expert_routing_tables(),
self,
prepare_finalize,
layer: torch.nn.Module,
):
from vllm.model_executor.layers.fused_moe import modular_kernel as mk
from vllm.model_executor.layers.fused_moe.experts.fused_humming_moe import (
BatchedHummingGroupedExperts,
HummingGroupedExperts,
HummingIndexedExperts,
get_humming_moe_gemm_type,
) )
activation_format = prepare_finalize.activation_format
assert self.moe_quant_config is not None
if activation_format == mk.FusedMoEActivationFormat.BatchedExperts:
return BatchedHummingGroupedExperts(
layer=layer,
moe_config=self.moe,
quant_config=self.moe_quant_config,
max_num_tokens=prepare_finalize.max_num_tokens_per_rank(),
num_dispatchers=prepare_finalize.num_dispatchers(),
)
elif get_humming_moe_gemm_type() == "indexed":
return HummingIndexedExperts(layer, self.moe, self.moe_quant_config)
else:
return HummingGroupedExperts(layer, self.moe, self.moe_quant_config)
def apply( def apply(
self, self,
layer: RoutedExperts, layer: RoutedExperts,
@@ -896,22 +794,36 @@ class HummingMoEMethod(FusedMoEMethodBase):
topk_ids: torch.Tensor, topk_ids: torch.Tensor,
shared_experts: SharedExperts | None, shared_experts: SharedExperts | None,
shared_experts_input: torch.Tensor | None, shared_experts_input: torch.Tensor | None,
) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: ) -> torch.Tensor:
workspace1, workspace2, output = self.experts.make_workspaces( """
M=topk_ids.size(0), Apply Humming-quantized MoE computation using the standard kernel flow.
topk=topk_ids.size(1),
activation=layer.activation,
)
assert workspace1.data_ptr() == output.data_ptr() This method uses FusedMoEKernel.apply() which orchestrates:
1. Preparation (quantization if needed - skipped for Humming via
expects_unquantized_inputs=True to prevent double quantization)
2. Expert computation (via experts.apply())
3. Finalization (weight application & reduction - no-op for Humming
since it's already done internally)
self.experts.main_apply( Humming handles all quantization, weight application, and reduction
internally in the experts.apply() method via HummingMethod calls.
Note: Although w1/w2 weights are passed to the kernel for interface
consistency, Humming's experts.apply() reads weights directly from
the layer object via HummingMethod.forward_layer() and ignores the
w1/w2 parameters.
"""
assert self.moe_kernel is not None
return self.moe_kernel.apply(
hidden_states=x, hidden_states=x,
topk_weights=topk_weights, w1=layer.w13_weight,
w2=layer.w2_weight,
topk_ids=topk_ids, topk_ids=topk_ids,
workspace1=workspace1, topk_weights=topk_weights,
workspace2=workspace2, activation=layer.activation,
expert_tokens_meta=None, global_num_experts=layer.global_num_experts,
expert_map=layer.expert_map,
apply_router_weight_on_input=False,
shared_experts=shared_experts,
shared_experts_input=shared_experts_input,
) )
return output
@@ -1280,6 +1280,7 @@ class QuarkOCP_MX_MoEMethod(QuarkMoEMethod):
gemm1_alpha=getattr(layer, "swiglu_alpha", None), gemm1_alpha=getattr(layer, "swiglu_alpha", None),
gemm1_beta=getattr(layer, "swiglu_beta", None), gemm1_beta=getattr(layer, "swiglu_beta", None),
swiglu_limit=getattr(layer, "swiglu_limit", None), swiglu_limit=getattr(layer, "swiglu_limit", None),
layer=layer,
) )
# Emulation and other schemes # Emulation and other schemes
File diff suppressed because it is too large Load Diff
+13
View File
@@ -15,6 +15,7 @@ _EXPORTS: dict[str, str] = {
"dtypes": "humming.dtypes", "dtypes": "humming.dtypes",
"DataType": "humming.dtypes:DataType", "DataType": "humming.dtypes:DataType",
"GemmType": "humming.config:GemmType", "GemmType": "humming.config:GemmType",
"WeightScaleType": "humming.config:WeightScaleType",
"HummingMethod": "humming.layer:HummingMethod", "HummingMethod": "humming.layer:HummingMethod",
"HummingLayerMeta": "humming.layer:HummingLayerMeta", "HummingLayerMeta": "humming.layer:HummingLayerMeta",
"BaseInputSchema": "humming.schema:BaseInputSchema", "BaseInputSchema": "humming.schema:BaseInputSchema",
@@ -22,6 +23,18 @@ _EXPORTS: dict[str, str] = {
"HummingInputSchema": "humming.schema:HummingInputSchema", "HummingInputSchema": "humming.schema:HummingInputSchema",
"HummingWeightSchema": "humming.schema:HummingWeightSchema", "HummingWeightSchema": "humming.schema:HummingWeightSchema",
"quantize_weight": "humming.utils.weight:quantize_weight", "quantize_weight": "humming.utils.weight:quantize_weight",
"AWQWeightSchema": "humming.schema:AWQWeightSchema",
"BitnetWeightSchema": "humming.schema:BitnetWeightSchema",
"ModeloptMxfp8WeightSchema": "humming.schema.modelopt:ModeloptMxfp8WeightSchema",
"ModeloptNvfp4InputSchema": "humming.schema.modelopt:ModeloptNvfp4InputSchema",
"ModeloptNvfp4WeightSchema": "humming.schema.modelopt:ModeloptNvfp4WeightSchema",
"CompressedTensorsInputSchema": "humming.schema:CompressedTensorsInputSchema",
"CompressedTensorsWeightSchema": "humming.schema:CompressedTensorsWeightSchema",
"Fp8InputSchema": "humming.schema:Fp8InputSchema",
"Fp8WeightSchema": "humming.schema.fp8:Fp8WeightSchema",
"Mxfp4WeightSchema": "humming.schema:Mxfp4WeightSchema",
"GptOssMxfp4WeightSchema": "humming.schema:GptOssMxfp4WeightSchema",
"GPTQWeightSchema": "humming.schema:GPTQWeightSchema",
} }