[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
+145 -3
View File
@@ -8,6 +8,7 @@ import torch
from tests.kernels.moe.utils import (
batched_moe,
make_dummy_moe_config,
make_quantized_test_activations,
make_test_weights,
naive_batched_moe,
@@ -16,7 +17,13 @@ from tests.kernels.quant_utils import native_batched_masked_quant_matmul
from tests.kernels.utils import torch_experts
from vllm.config import VllmConfig, set_current_vllm_config
from vllm.model_executor.layers.fused_moe import fused_topk
from vllm.model_executor.layers.fused_moe.activation import (
ApplyMoEActivationConfig,
MoEActivation,
apply_moe_activation,
)
from vllm.model_executor.layers.fused_moe.experts.fused_batched_moe import (
BatchedTritonExperts,
invoke_moe_batched_triton_kernel,
)
from vllm.platforms import current_platform
@@ -496,11 +503,146 @@ def test_batched_mm_td_zero_expert_tokens():
# BatchedTritonExperts device enablement (XPU)
def test_batched_triton_experts_supports_current_device():
from vllm.model_executor.layers.fused_moe.experts.fused_batched_moe import (
BatchedTritonExperts,
assert BatchedTritonExperts._supports_current_device()
@pytest.mark.parametrize(
"activation",
[
MoEActivation.SITU,
MoEActivation.SWIGLUOAI_UNINTERLEAVE,
MoEActivation.SWIGLUSTEP,
],
)
def test_batched_triton_supports_masked_activations(
monkeypatch, activation: MoEActivation
):
monkeypatch.setattr(current_platform, "is_xpu", lambda: False)
assert BatchedTritonExperts._supports_activation(activation)
monkeypatch.setattr(current_platform, "is_xpu", lambda: True)
assert not BatchedTritonExperts._supports_activation(activation)
def _torch_experts_with_shared_activation(
a: torch.Tensor,
w1: torch.Tensor,
w2: torch.Tensor,
topk_weights: torch.Tensor,
topk_ids: torch.Tensor,
activation: MoEActivation,
activation_config: ApplyMoEActivationConfig,
) -> torch.Tensor:
num_tokens, hidden_dim = a.shape
topk = topk_ids.shape[1]
routed_input = a[:, None, :].expand(-1, topk, -1).reshape(-1, hidden_dim)
routed_experts = topk_ids.reshape(-1)
routed_output = torch.zeros_like(routed_input)
for expert in range(w1.shape[0]):
expert_mask = routed_experts == expert
if not expert_mask.any():
continue
projected = routed_input[expert_mask] @ w1[expert].T
activated = torch.empty(
projected.shape[0], w2.shape[2], dtype=a.dtype, device=a.device
)
apply_moe_activation(
activation,
activated,
projected,
activation_config=activation_config,
)
routed_output[expert_mask] = activated @ w2[expert].T
return (
(
routed_output.view(num_tokens, topk, hidden_dim).float()
* topk_weights[..., None]
)
.sum(dim=1)
.to(a.dtype)
)
assert BatchedTritonExperts._supports_current_device()
_MASKED_BATCHED_ACTIVATION_CASES = [
pytest.param(
MoEActivation.SITU,
ApplyMoEActivationConfig(
activation_situ_beta=1.5,
activation_situ_linear_beta=2.0,
),
id="situ",
),
pytest.param(
MoEActivation.SWIGLUOAI_UNINTERLEAVE,
ApplyMoEActivationConfig(clamp_limit=3.0, alpha=1.3, beta=0.5),
id="swigluoai-uninterleave",
),
pytest.param(
MoEActivation.SWIGLUSTEP,
ApplyMoEActivationConfig(),
id="swiglustep",
),
]
@pytest.mark.parametrize(
("activation", "activation_config"), _MASKED_BATCHED_ACTIVATION_CASES
)
@torch.inference_mode()
def test_batched_experts_masked_activation_end_to_end(
activation: MoEActivation,
activation_config: ApplyMoEActivationConfig,
):
if current_platform.is_xpu():
pytest.skip("Masked activations are not enabled on XPU")
from vllm.v1.worker.workspace import init_workspace_manager
set_random_seed(7)
device = current_platform.device_type
init_workspace_manager(torch.device(f"{device}:0"))
m, n, k, e, topk = 16, 128, 128, 8, 2
a = torch.randn((m, k), device=device, dtype=torch.bfloat16) / 10
score = torch.randn((m, e), device=device, dtype=torch.bfloat16)
w1 = torch.randn((e, 2 * n, k), device=device, dtype=torch.bfloat16) / 15
w2 = torch.randn((e, k, n), device=device, dtype=torch.bfloat16) / 15
moe_config = make_dummy_moe_config(
num_experts=e,
experts_per_token=topk,
hidden_dim=k,
intermediate_size=n,
in_dtype=a.dtype,
activation=activation,
)
moe_config.swiglu_limit = activation_config.clamp_limit
moe_config.swiglu_alpha = activation_config.alpha
moe_config.swiglu_beta = activation_config.beta
moe_config.activation_situ_beta = activation_config.activation_situ_beta
moe_config.activation_situ_linear_beta = (
activation_config.activation_situ_linear_beta
)
routing_weights = torch.softmax(score.float(), dim=-1)
topk_weights, topk_ids = torch.topk(routing_weights, topk, dim=-1)
topk_ids = topk_ids.to(torch.int32)
with set_current_vllm_config(vllm_config):
baseline_output = _torch_experts_with_shared_activation(
a,
w1,
w2,
topk_weights,
topk_ids,
activation,
activation_config,
)
triton_output = batched_moe(
a, w1, w2, topk_weights, topk_ids, moe_config=moe_config
)
torch.testing.assert_close(triton_output, baseline_output, atol=3e-2, rtol=2e-2)
@pytest.mark.parametrize("m,n,k,e,topk", [(32, 512, 512, 8, 2), (45, 1024, 128, 8, 1)])