Skip to content

flydsl moe: EP reduce path with masked gather + tuned-key fix - #3377

Merged
valarLip merged 15 commits into
mainfrom
dev/ep_reduce
Jun 5, 2026
Merged

valarLip merged 15 commits into
mainfrom
dev/ep_reduce

Conversation

@yadaish

@yadaish yadaish commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add an EP-aware stage-2 reduce path in FlyDSL MOE: compile_moe_reduction
    now fuses the validity gather valid = expert_mask[topk_ids[t,k]] != 0,
    so masked slots cost no atomics and no torch.sum epilogue. Gated by
    AITER_FLYDSL_FORCE_REDUCE=1 for debug; threaded through
    _flydsl_stage2_wrapper and flydsl_moe_stage2.
  • moe_sorting learns need_reduce / has_reduce_mask so moe_buf is
    sized correctly for the three downstream shapes (atomic accumulate vs
    per-topk slab vs caller-owned).
  • Fix tuned-config lookup miss in EP mode: callers append one fake-mask
    slot to topk_ids, inflating runtime topk by 1, but tuned CSVs are
    keyed on routed topk. get_2stage_cfgs now takes is_ep and does
    topk -= int(is_ep) before building the key, so the existing DSv3
    tuned rows (topk=8) are hit when the runtime sees topk=9.
  • Encode use_mask + dtype + topk + model_dim into the reduction kernel
    symbol via @flyc.kernel(name=module_name) so rocprofv3 traces can
    distinguish moe_reduction_kernel_masked_… from _plain_….
  • Add g1u1_a4w4_mxfp4 / g1u1_a8w4_mxfp4 EP cases to test_moe_ep.py
    and a test_ep.sh driver that covers smoke shapes plus the DSv3
    shape (model_dim=7168, inter_dim=2048, E=256, topk=8, ep=8).

yadaish and others added 3 commits May 27, 2026 15:38
moe_sorting gains need_reduce/has_reduce_mask to size moe_buf for the
stage2 reduce epilogue (per-topk slab vs atomic accumulate vs caller-
owned). Stage2 wrapper and moe_gemm_2stage updated to drive the
compile_moe_reduction path; test_moe_ep covers the new EP variants.

Co-Authored-By: Claude Opus 4 <[email protected]>
@yadaish
yadaish requested a review from a team May 27, 2026 16:47
@yadaish yadaish changed the title support ep reduce flydsl moe: EP reduce path with masked gather + tuned-key fix May 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 3377 --add-label <label>

@yadaish
yadaish requested review from Zzz9990, lalala-sh and valarLip May 28, 2026 02:06
)
X = target.view(token_num, topk, model_dim)
if use_mask:
em = expert_mask.to(torch.int32).contiguous()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dose here import extra copy? seems the agent write redundant input check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No copy here. Just one defense guard

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may use stride tensor here...

Comment thread aiter/fused_moe.py
raise e


def get_topk_valid_mask(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not called func?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be kept because it shows how to build valid mask.

tk = topk_ids.to(torch.int32).contiguous()
else:
# Placeholders; kernel ignores them when use_mask=False (compile-time).
em = torch.empty(0, device=arg_out.device, dtype=torch.int32)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the placeholder

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if so, one more interface will added. This part of code is rarely used.

yadaish added 4 commits May 28, 2026 03:30
The compile_moe_reduction kernel addressed X [tokens, topk, model_dim] via
fx.rocdl.make_buffer_tensor + i32 per-thread voffsets. For large batches the
per-token byte offset exceeds 2^32 (e.g. 131072*6*4096*2 = 6 GiB), wrapping
the i32 voffset and corrupting the reduction.

Fold the per-WG token offset into each buffer descriptor's 48-bit base
pointer via create_buffer_resource(base_byte_offset=fx.Int64(...)). In-kernel
voffsets stay i32-safe because they only span one token's slab
(topk*model_dim*elem_bytes).

The vector path now uses direct buffer_load/buffer_store against the
base-offset-shifted resources (the make_buffer_tensor helper has no base
hook). Behavior at typical sizes is unchanged; logits_diff at token=131072
drops from 0.337 to 0.0054, matching torch.sum.
# Conflicts:
#	aiter/ops/flydsl/kernels/moe_gemm_2stage.py
Adopt main's commit 1be4ee9 (#3373) plumbing in compile_moe_gemm1
and compile_moe_gemm2: switch kernel/launcher signatures to fx.Pointer,
add a local _ptr_buffer_resource helper, precompute w_nbytes/sw_nbytes,
and replace extract_base_index with ptrtoint + index_cast.

For compile_moe_reduction, preserve the EP-reduce semantics (X, Y,
expert_mask, topk_ids inputs and the fused valid = expert_mask[topk_ids
[t,k]] != 0 gather) and the i32 voffset overflow fix from 7e1935f,
while migrating its arguments to fx.Pointer. The per-token slab offset
is folded into the descriptor base address via a helper that does the
add in i64 before create_buffer_resource_from_addr. Add a new
compile-time num_experts kwarg so the expert_mask descriptor can be
sized exactly. Update _MoeGemm2ReduceWrapper and the moe_kernels reduce
call site to pass pointers via flyc.from_c_void_p / _ptr_view_safe.
valarLip
valarLip previously approved these changes Jun 2, 2026
yadaish and others added 2 commits June 2, 2026 08:37
Collapse the two boolean knobs on moe_sorting into a single `accumulate`
flag that mirrors how the stage2 path actually uses moe_buf, and pick it
automatically from the compiled FlyDSL stage2 kernel name.

Co-Authored-By: Claude Opus 4 <[email protected]>
yadaish and others added 5 commits June 2, 2026 14:07
…fp4 activation

In FlyDSL stage2 reduce mode (no expert_mask), moe_sorting returns a
zero-shape placeholder for moe_buf since the reduce path owns the final
write. fused_moe_2stages now allocates a real [token_num, model_dim]
output tensor before invoking stage2 when it detects the placeholder.

Also splits the legacy case generator in test_moe_2stage.py so the
a8w4 mxfp4 (per_1x32, fp8, fp4x2) triple iterates args.act instead of
hardcoding Swiglu, allowing -a silu to actually take effect for -q 7.

Co-Authored-By: Claude Opus 4 <[email protected]>
@valarLip
valarLip merged commit 7a8ff7d into main Jun 5, 2026
21 checks passed
@valarLip
valarLip deleted the dev/ep_reduce branch June 5, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants