[MoE] Generalize masked activation for padded layouts (#51217)

Signed-off-by: mgoin <mgoin64@gmail.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Misha Goin
2026-09-01 11:34:13 -04:00
committed by GitHub
co-authored by OpenAI Codex
parent a232e29e9d
commit 82b7d49a6e
15 changed files with 1217 additions and 196 deletions
+260 -1
View File
@@ -1377,6 +1377,102 @@ def test_humming_activation_metadata_tracks_shared_apply(activation: MoEActivati
) == apply_moe_activation_supported(activation)
@pytest.mark.parametrize(
("env_override", "use_ep", "expected"),
[
(None, False, "indexed"),
(None, True, "grouped_contiguous"),
("auto", True, "grouped_contiguous"),
("indexed", True, "indexed"),
("grouped", False, "grouped_contiguous"),
],
)
def test_humming_selects_gemm_from_parallelism_and_override(
monkeypatch: pytest.MonkeyPatch,
env_override: str | None,
use_ep: bool,
expected: str,
):
from vllm.model_executor.layers.fused_moe.experts.fused_humming_moe import (
get_humming_moe_gemm_type,
)
if env_override is None:
monkeypatch.delenv("VLLM_HUMMING_MOE_GEMM_TYPE", raising=False)
else:
monkeypatch.setenv("VLLM_HUMMING_MOE_GEMM_TYPE", env_override)
moe_config = make_dummy_moe_config()
moe_config.moe_parallel_config.use_ep = use_ep
assert get_humming_moe_gemm_type(moe_config) == expected
@pytest.mark.parametrize(
("dp_token_counts", "topk_shape", "expected"),
[
pytest.param(None, (7, 3), 21, id="runtime-layout"),
pytest.param([3, 5], (48, 1), 48, id="expanded-dp-layout"),
],
)
def test_humming_global_valid_shape_m(
monkeypatch: pytest.MonkeyPatch,
dp_token_counts: list[int] | None,
topk_shape: tuple[int, int],
expected: int,
):
from types import SimpleNamespace
import vllm.model_executor.layers.fused_moe.experts.fused_humming_moe as humming
dp_metadata = (
None
if dp_token_counts is None
else SimpleNamespace(
num_tokens_across_dp_cpu=torch.tensor(dp_token_counts, dtype=torch.int32)
)
)
monkeypatch.setattr(
humming,
"get_forward_context",
lambda: SimpleNamespace(dp_metadata=dp_metadata),
)
experts = SimpleNamespace(moe_config=make_dummy_moe_config(experts_per_token=6))
topk_ids = torch.empty(topk_shape, dtype=torch.int64)
result = humming.HummingExpertsBase.get_global_valid_shape_m(experts, topk_ids)
assert result == expected
def test_humming_permute_scratch_is_keyed_by_runtime_topk(
monkeypatch: pytest.MonkeyPatch,
):
from types import SimpleNamespace
from unittest.mock import Mock
import vllm.model_executor.layers.fused_moe.experts.fused_humming_moe as humming
scratch_topk6 = Mock()
scratch_topk1 = Mock()
scratch_type = Mock(side_effect=[scratch_topk6, scratch_topk1])
monkeypatch.setattr(humming, "moe_permute_unpermute_supported", lambda: True)
monkeypatch.setattr(humming, "MoEPermuteScratch", scratch_type)
moe_config = make_dummy_moe_config(max_num_tokens=512, experts_per_token=6)
moe_config.moe_parallel_config.dp_size = 2
experts = SimpleNamespace(_permute_scratch={}, moe_config=moe_config)
assert humming.HummingExpertsBase._get_permute_scratch(experts, 6) is scratch_topk6
assert humming.HummingExpertsBase._get_permute_scratch(experts, 1) is scratch_topk1
assert humming.HummingExpertsBase._get_permute_scratch(experts, 6) is scratch_topk6
assert scratch_type.call_count == 2
first_call, second_call = scratch_type.call_args_list
assert first_call.kwargs["max_num_tokens"] == 1024
assert first_call.kwargs["topk"] == 6
assert second_call.kwargs["max_num_tokens"] == 6144
assert second_call.kwargs["topk"] == 1
def test_humming_delegates_to_instance_activation():
from types import SimpleNamespace
from unittest.mock import Mock
@@ -1408,10 +1504,173 @@ def test_humming_delegates_to_instance_activation():
activation=MoEActivation.SWIGLUOAI_UNINTERLEAVE,
input=input,
output=output,
valid_rows=None,
)
def test_humming_grouped_apply_forwards_valid_prefix(
monkeypatch: pytest.MonkeyPatch,
):
from types import MethodType, SimpleNamespace
from unittest.mock import Mock
import vllm.model_executor.layers.fused_moe.experts.fused_humming_moe as humming
from vllm.model_executor.layers.fused_moe.experts.fused_humming_moe import (
HummingExpertsBase,
HummingGroupedExperts,
)
activation_func = Mock()
buffers = {
"gate_up_output": torch.empty(6, 4),
"activation_output": torch.empty(6, 2),
"down_output": torch.empty(6, 4),
}
experts = SimpleNamespace(
activation=activation_func,
num_experts=2,
estimate_local_valid_shape_m=lambda _: 6,
prepare_buffers=lambda *_: buffers,
_get_permute_scratch=lambda _: None,
quantize_input=lambda _, *, inputs, input_scale=None, quanted_input: (
inputs,
input_scale,
),
humming_forward=Mock(),
compute_config_str="",
w13_tuning_config_str="",
w2_tuning_config_str="",
)
experts.apply_activation = MethodType(HummingExpertsBase.apply_activation, experts)
hidden_states = torch.empty(3, 4)
output = torch.empty_like(hidden_states)
topk_ids = torch.zeros((3, 2), dtype=torch.int64)
topk_weights = torch.ones_like(topk_ids, dtype=torch.float32)
expected_counts = torch.tensor([4], dtype=torch.int32)
expert_offsets = torch.tensor([0, 2, 4], dtype=torch.int64)
monkeypatch.setattr(
humming,
"moe_permute",
lambda **_: (
torch.empty(6, 4),
None,
expert_offsets,
torch.arange(6),
None,
),
)
monkeypatch.setattr(humming, "moe_unpermute", lambda **_: None)
HummingGroupedExperts.apply(
experts,
output=output,
hidden_states=hidden_states,
w1=torch.empty(0),
w2=torch.empty(0),
topk_weights=topk_weights,
topk_ids=topk_ids,
activation=MoEActivation.SITU,
global_num_experts=2,
expert_map=None,
a1q_scale=None,
a2_scale=None,
workspace13=torch.empty(0),
workspace2=torch.empty(0),
expert_tokens_meta=None,
apply_router_weight_on_input=False,
)
activation_func.assert_called_once()
call_kwargs = activation_func.call_args.kwargs
assert call_kwargs["activation"] == MoEActivation.SITU
assert call_kwargs["input"].shape == (6, 4)
assert call_kwargs["output"].shape == (6, 2)
torch.testing.assert_close(call_kwargs["valid_token_counts"], expected_counts)
def test_batched_marlin_activation_uses_expert_token_counts(
monkeypatch: pytest.MonkeyPatch,
):
from types import SimpleNamespace
from unittest.mock import Mock
import vllm.model_executor.layers.fused_moe.experts.marlin_moe as marlin
from vllm.model_executor.layers.fused_moe.experts.marlin_moe import (
BatchedMarlinExperts,
)
from vllm.model_executor.layers.fused_moe.modular_kernel import (
ExpertTokensMetadata,
)
activation_func = Mock()
experts = SimpleNamespace(
activation=activation_func,
w1_bias=None,
w2_bias=None,
w1_scale=None,
w2_scale=None,
quant_type_id=0,
a1_gscale=None,
a2_gscale=None,
w13_g_idx=None,
w2_g_idx=None,
w13_g_idx_sort_indices=None,
w2_g_idx_sort_indices=None,
w1_zp=None,
w2_zp=None,
input_dtype=None,
is_k_full=True,
activation_config=ApplyMoEActivationConfig(
clamp_limit=3.0, alpha=1.3, beta=0.5
),
)
hidden_states = torch.empty(2, 3, 4)
expert_num_tokens = torch.tensor([1, 2], dtype=torch.int32)
topk_ids = torch.zeros((2, 3), dtype=torch.int64)
expert_map = torch.arange(2, dtype=torch.int32)
def fake_batched_fused_marlin_moe(**kwargs):
kwargs["activation_func"](
kwargs["activation"],
torch.empty(6, 4),
torch.empty(6, 8),
topk_ids=topk_ids,
expert_map=expert_map,
)
return kwargs["output"]
monkeypatch.setattr(
marlin, "batched_fused_marlin_moe", fake_batched_fused_marlin_moe
)
output = torch.empty_like(hidden_states)
BatchedMarlinExperts.apply(
experts,
output=output,
hidden_states=hidden_states,
w1=torch.empty(0),
w2=torch.empty(0),
topk_weights=torch.ones_like(topk_ids, dtype=torch.float32),
topk_ids=topk_ids,
activation=MoEActivation.SWIGLUOAI_UNINTERLEAVE,
global_num_experts=2,
expert_map=expert_map,
a1q_scale=None,
a2_scale=None,
workspace13=torch.empty(0),
workspace2=torch.empty(0),
expert_tokens_meta=ExpertTokensMetadata(expert_num_tokens, None),
apply_router_weight_on_input=False,
)
activation_func.assert_called_once()
call = activation_func.call_args
assert call.args[0] == MoEActivation.SWIGLUOAI_UNINTERLEAVE
assert call.args[1].shape == (2, 3, 4)
assert call.args[2].shape == (2, 3, 8)
torch.testing.assert_close(call.kwargs["valid_token_counts"], expert_num_tokens)
@pytest.mark.parametrize(
"activation",
[