From 583a00257d4c5d1a54063d956057df1df6822b06 Mon Sep 17 00:00:00 2001 From: akii96 Date: Wed, 19 Aug 2026 19:14:52 +0300 Subject: [PATCH] [ROCm] [Bugfix] Fix Triton fused shared expert alignment (#51632) Signed-off-by: Aakif Nawaz --- tests/kernels/moe/test_moe.py | 44 +++++++++++++++++++ .../layers/fused_moe/experts/triton_moe.py | 8 +++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/tests/kernels/moe/test_moe.py b/tests/kernels/moe/test_moe.py index 17b8cae318..ac73ca9416 100644 --- a/tests/kernels/moe/test_moe.py +++ b/tests/kernels/moe/test_moe.py @@ -459,6 +459,50 @@ def test_fused_moe( ) +def test_fused_shared_expert_alignment(workspace_init): + set_random_seed(7) + m, n, k = 4, 64, 128 + routed_experts = 8 + physical_experts = routed_experts + 1 + dtype = torch.bfloat16 + + a = torch.randn((m, k), device=DEVICE_TYPE, dtype=dtype) / 10 + w1 = torch.randn((physical_experts, 2 * n, k), device=DEVICE_TYPE, dtype=dtype) / 10 + w2 = torch.randn((physical_experts, k, n), device=DEVICE_TYPE, dtype=dtype) / 10 + topk_ids = torch.tensor( + [[0, 8], [1, 8], [2, 8], [3, 8]], device=DEVICE_TYPE, dtype=torch.int32 + ) + topk_weights = torch.tensor( + [[0.5, 1.0]] * m, device=DEVICE_TYPE, dtype=torch.float32 + ) + + moe_config = make_dummy_moe_config( + num_experts=physical_experts, + experts_per_token=2, + hidden_dim=k, + intermediate_size=n, + in_dtype=dtype, + max_num_tokens=m, + ) + modular_moe = modular_triton_fused_moe(moe_config, FUSED_MOE_UNQUANTIZED_CONFIG) + + with set_current_vllm_config(vllm_config): + expected = torch_experts(a, w1, w2, topk_weights, topk_ids) + actual = modular_moe.apply( + hidden_states=a, + w1=w1, + w2=w2, + topk_weights=topk_weights, + topk_ids=topk_ids, + activation=MoEActivation.SILU, + global_num_experts=routed_experts, + expert_map=None, + apply_router_weight_on_input=False, + ) + + torch.testing.assert_close(actual, expected, atol=2e-2, rtol=0) + + def test_fused_moe_int64_overflow(workspace_init): """Regression test for int32 overflow in stride*offset products. diff --git a/vllm/model_executor/layers/fused_moe/experts/triton_moe.py b/vllm/model_executor/layers/fused_moe/experts/triton_moe.py index d0ece26cfa..9887423e79 100644 --- a/vllm/model_executor/layers/fused_moe/experts/triton_moe.py +++ b/vllm/model_executor/layers/fused_moe/experts/triton_moe.py @@ -315,13 +315,15 @@ class TritonExperts(LoRAExpertsMixin, mk.FusedMoEExpertsModular): ) intermediate_cache3 = _resize_cache(workspace2, (num_tokens, top_k_num, K)) + # Include fused shared-expert rows while preserving EP remapping. + num_align_experts = w1.shape[0] if expert_map is None else global_num_experts sorted_token_ids, expert_ids, num_tokens_post_padded = ( _prepare_expert_assignment( topk_ids, config, num_tokens, top_k_num, - global_num_experts, + num_align_experts, expert_map, use_int8_w8a16=self.quant_config.use_int8_w8a16, use_int4_w4a16=self.quant_config.use_int4_w4a16, @@ -688,8 +690,10 @@ class TritonWNA16Experts(TritonExperts): ) intermediate_cache3 = _resize_cache(workspace2, (num_tokens, top_k_num, K)) + # Include fused shared-expert rows while preserving EP remapping. + num_align_experts = w1.shape[0] if expert_map is None else global_num_experts sorted_token_ids, expert_ids, num_tokens_post_padded = moe_align_block_size( - topk_ids, config["BLOCK_SIZE_M"], global_num_experts, expert_map + topk_ids, config["BLOCK_SIZE_M"], num_align_experts, expert_map ) invoke_fused_moe_wna16_triton_kernel(