[AMD] feat(moe): fold padded-topk_ids fill into fused shared-experts append+remap (#31370)
This commit is contained in:
@@ -1329,8 +1329,11 @@ def _fused_append_remap_shared_experts_deepep_kernel(
|
||||
shared_id_base, # runtime scalar: ep_rank * num_local_experts + num_local_routed
|
||||
num_local_routed, # runtime scalar: routed experts per rank (for gap-insertion)
|
||||
scale_factor, # runtime scalar: shared-expert weight
|
||||
num_token_non_padded_ptr, # 1-elem int tensor; only read when HAS_PADDING
|
||||
pad_fill_id, # runtime scalar: routed-id fill for padded rows
|
||||
K: tl.constexpr,
|
||||
S: tl.constexpr,
|
||||
HAS_PADDING: tl.constexpr,
|
||||
):
|
||||
"""Append shared experts AND apply the DeepEP interleaved remap in one pass.
|
||||
|
||||
@@ -1356,6 +1359,14 @@ def _fused_append_remap_shared_experts_deepep_kernel(
|
||||
# precede it. Matches `routed + routed // num_local_routed` exactly.
|
||||
ids = ids + ids // num_local_routed
|
||||
|
||||
if HAS_PADDING:
|
||||
# Fold the padded-topk_ids fill (previously a separate _fill_padded_rows
|
||||
# launch): rows >= num_token_non_padded get pad_fill_id in every routed
|
||||
# slot. Matches the old fill(topk_ids=0) -> remap(0)=0 when pad_fill_id==0.
|
||||
n_valid = tl.load(num_token_non_padded_ptr)
|
||||
if pid >= n_valid:
|
||||
ids = tl.full((K,), pad_fill_id, dtype=ids.dtype)
|
||||
|
||||
tl.store(out_ids_ptr + out_ids_row_ptr + offs_k, ids)
|
||||
tl.store(out_weights_ptr + out_ids_row_ptr + offs_k, ws)
|
||||
|
||||
@@ -1374,6 +1385,8 @@ def fused_append_remap_shared_experts_deepep(
|
||||
scale_factor,
|
||||
shared_id_base,
|
||||
num_local_routed,
|
||||
num_token_non_padded=None,
|
||||
pad_fill_id=0,
|
||||
):
|
||||
"""Fused append + DeepEP remap (see kernel docstring).
|
||||
|
||||
@@ -1391,6 +1404,9 @@ def fused_append_remap_shared_experts_deepep(
|
||||
(m, k + s), dtype=topk_weights.dtype, device=topk_weights.device
|
||||
)
|
||||
|
||||
has_padding = num_token_non_padded is not None
|
||||
# Placeholder pointer when no padding (never dereferenced: HAS_PADDING False).
|
||||
ntnp_ptr = num_token_non_padded if has_padding else topk_ids
|
||||
_fused_append_remap_shared_experts_deepep_kernel[(m,)](
|
||||
topk_ids,
|
||||
topk_weights,
|
||||
@@ -1399,8 +1415,11 @@ def fused_append_remap_shared_experts_deepep(
|
||||
shared_id_base,
|
||||
num_local_routed,
|
||||
scale_factor,
|
||||
ntnp_ptr,
|
||||
pad_fill_id,
|
||||
K=k,
|
||||
S=s,
|
||||
HAS_PADDING=has_padding,
|
||||
num_warps=1,
|
||||
)
|
||||
return out_ids, out_weights
|
||||
|
||||
@@ -1933,6 +1933,7 @@ def _post_process_topk_ids(
|
||||
)
|
||||
capture_routed_experts_if_allowed(topk_config, layer_id, topk_ids)
|
||||
recorder_topk_ids = None
|
||||
_fold_pad_into_append = False
|
||||
if _is_cuda:
|
||||
# LP path: solve LP outside torch.compile (the solver contains an
|
||||
# EP all-reduce that can't run inside compiled regions).
|
||||
@@ -1979,7 +1980,19 @@ def _post_process_topk_ids(
|
||||
# contribution to the hidden state is still zero regardless of the id.
|
||||
# Regression: skipping this mask when EPLB is disabled caused garbage
|
||||
# MoE routing for models like DeepSeek-R1-MXFP4 (accuracy ~0.09 vs 0.94+).
|
||||
_mask_topk_ids_padded_region(topk_ids, num_token_non_padded, fill_value=0)
|
||||
#
|
||||
# Fold: when the fused append+remap kernel runs below (aiter per-rank
|
||||
# shared-slot path, EPLB off) it folds this padded fill itself
|
||||
# (pad_fill_id=0 -> remap(0)=0, bit-identical), so skip the separate
|
||||
# _fill_padded_rows launch here.
|
||||
_fold_pad_into_append = (
|
||||
num_fused_shared_experts > 0
|
||||
and _use_aiter
|
||||
and use_per_rank_shared_slots
|
||||
and not _eplb_remap_enabled()
|
||||
)
|
||||
if not _fold_pad_into_append:
|
||||
_mask_topk_ids_padded_region(topk_ids, num_token_non_padded, fill_value=0)
|
||||
# The logical->physical remap is only meaningful when a real
|
||||
# expert-location mapping exists. With a trivial placement and EPLB off
|
||||
# the map is identity so the remap can be skipped safely.
|
||||
@@ -2034,6 +2047,9 @@ def _post_process_topk_ids(
|
||||
1.0, # shared-expert weight on the aiter path
|
||||
shared_id_base,
|
||||
num_local_routed,
|
||||
num_token_non_padded=(
|
||||
num_token_non_padded if _fold_pad_into_append else None
|
||||
),
|
||||
)
|
||||
elif _aiter_append:
|
||||
M, N = router_logits.shape
|
||||
|
||||
Reference in New Issue
Block a user