[AMD] feat(moe): fold padded-topk_ids fill into fused shared-experts append+remap (#31370)

This commit is contained in:
karverma-amd
2026-08-18 02:32:57 -07:00
committed by GitHub
parent 70ee6b1714
commit 24d625698d
3 changed files with 83 additions and 1 deletions
@@ -173,6 +173,53 @@ class TestFusedAppendRemapPerRankSharedSlots(CustomTestCase):
)
self.assertTrue(torch.all(got_w[:, -s:] == 1.0))
def test_pad_fold_matches_separate_fill(self):
"""HAS_PADDING fold == separate padded-fill(0) then append+remap.
The fusion folds the padded-topk_ids fill into this kernel: rows
>= num_token_non_padded get pad_fill_id in every routed slot. With
pad_fill_id=0 this is bit-identical to the previous path that filled the
padded region with 0 (topk_ids=0 -> remap 0 + 0//nlr = 0) via a separate
_fill_padded_rows launch before append+remap ran.
"""
for m, k, npr, ep_size, ep_rank, s in self.CASES:
for n_valid in (0, max(m // 2, 1), m):
with self.subTest(m=m, k=k, ep_rank=ep_rank, s=s, n_valid=n_valid):
shared_id_base, num_local_routed = self._shared_id_base(
npr, ep_size, ep_rank, s
)
topk_ids, topk_weights = self._make_inputs(m, k, npr)
# Baseline: pre-fill padded rows to 0, no fold.
base_ids = topk_ids.clone()
base_ids[n_valid:] = 0
exp_ids, exp_w = fused_append_remap_shared_experts_deepep(
base_ids,
topk_weights.clone(),
s,
1.0,
shared_id_base,
num_local_routed,
)
# Fused: fold the fill (no pre-fill), pad_fill_id=0.
ntnp = torch.tensor(
[n_valid], dtype=torch.int32, device=topk_ids.device
)
got_ids, got_w = fused_append_remap_shared_experts_deepep(
topk_ids.clone(),
topk_weights.clone(),
s,
1.0,
shared_id_base,
num_local_routed,
num_token_non_padded=ntnp,
pad_fill_id=0,
)
self.assertTrue(torch.equal(got_ids, exp_ids))
self.assertTrue(torch.allclose(got_w, exp_w))
def test_no_shared_experts_is_noop(self):
"""s == 0 returns the inputs untouched (no kernel launch)."""
topk_ids, topk_weights = self._make_inputs(4, 8, 256)