Repository navigation
flydsl moe: EP reduce path with masked gather + tuned-key fix - #3377
Merged
Merged
Conversation
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]>
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
lalala-sh
reviewed
May 28, 2026
| ) | ||
| X = target.view(token_num, topk, model_dim) | ||
| if use_mask: | ||
| em = expert_mask.to(torch.int32).contiguous() |
Contributor
There was a problem hiding this comment.
dose here import extra copy? seems the agent write redundant input check
Contributor
Author
There was a problem hiding this comment.
No copy here. Just one defense guard
Collaborator
There was a problem hiding this comment.
we may use stride tensor here...
| raise e | ||
|
|
||
|
|
||
| def get_topk_valid_mask( |
Contributor
Author
There was a problem hiding this comment.
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) |
Contributor
There was a problem hiding this comment.
Can we remove the placeholder
Contributor
Author
There was a problem hiding this comment.
if so, one more interface will added. This part of code is rarely used.
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.
This was referenced May 30, 2026
valarLip
previously approved these changes
Jun 2, 2026
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]>
…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
approved these changes
Jun 5, 2026
1 task done
This was referenced Jun 9, 2026
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compile_moe_reductionnow 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=1for debug; threaded through_flydsl_stage2_wrapperandflydsl_moe_stage2.moe_sortinglearnsneed_reduce/has_reduce_masksomoe_bufissized correctly for the three downstream shapes (atomic accumulate vs
per-topk slab vs caller-owned).
slot to
topk_ids, inflating runtimetopkby 1, but tuned CSVs arekeyed on routed topk.
get_2stage_cfgsnow takesis_epand doestopk -= int(is_ep)before building the key, so the existing DSv3tuned rows (
topk=8) are hit when the runtime seestopk=9.use_mask+ dtype + topk + model_dim into the reduction kernelsymbol via
@flyc.kernel(name=module_name)so rocprofv3 traces candistinguish
moe_reduction_kernel_masked_…from_plain_….g1u1_a4w4_mxfp4/g1u1_a8w4_mxfp4EP cases totest_moe_ep.pyand a
test_ep.shdriver that covers smoke shapes plus the DSv3shape (
model_dim=7168, inter_dim=2048, E=256, topk=8, ep=8).