Skip to content

Mario/overlap moe kernel integration - #3651

Merged
MarioSieg merged 37 commits into
mainfrom
mario/overlap-moe-kernel-integration
Oct 20, 2026
Merged

MarioSieg merged 37 commits into
mainfrom
mario/overlap-moe-kernel-integration

Conversation

@MarioSieg

@MarioSieg MarioSieg commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Integration of our closed mega-moe based on Sonic Moe's abstractions


Note

High Risk
Introduces a new distributed training path with persistent cross-rank kernels, strict NCCL/SM and buffer sizing constraints, and a proprietary deep_gemm dependency; misconfiguration can deadlock or fail at runtime.

Overview
Adds Mega MoE as a new expert-parallel MoE dispatch path (model.moe.dispatch.type = "mega_moe") that fuses token dispatch, SwiGLU expert GEMMs, and combine into prime-mega-moe deep_gemm kernels, overlapping EP communication with compute. Config covers precision (bf16 / mxfp8 / fp8 / fp8_bwd), max_tokens_per_rank (auto-derived from the micro batch in RL/SFT), and num_reserved_sms with a startup check that NCCL_MAX_CTAS ≤ reserved SMs to avoid deadlocks with FSDP NCCL.

Trainer wiring uses FusedTokenDispatcher plus MegaMoEExpertCompute (custom autograd ops, symmetric buffers, optional MXFP8 casting); model.moe.compute is ignored when Mega MoE is selected. GroupedExperts now accepts fused compute that takes routing tensors directly. Docs, a multi-GPU benchmark vs the bf16 Torch dispatcher path, activation-checkpointing registration, SFT timing sync, and a unit test that Mega MoE requires ep > 1 are included.

Reviewed by Cursor Bugbot for commit e433a59. Bugbot is set up for automated code reviews on this repo. Configure here.

@garrett361

Copy link
Copy Markdown
Contributor

nice, benchmark numbers?

@MarioSieg

MarioSieg commented Sep 24, 2026 •

Copy link
Copy Markdown
Member Author

nice, benchmark numbers?

mega-moe is 1.55× faster (35.4% lower fwd+bwd time), while using slightly less peak memory.

Note: we are currently investgating an MFU issue unrelated to this kernels so the speedup might change.
That's why this PR is a draft until we get 100% right bench numbers .- speedup might be a bit higher or lower.

MarioSieg and others added 3 commits September 24, 2026 20:17
…o/overlap-moe-kernel-integration

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Mega MoE is now a `MegaMoEExpertCompute` installed on `GroupedExperts.compute`
by `configure_moe_runtime`, like the other expert compute backends, instead of
a token dispatcher that injected a `fused` callback into `GroupedExperts.forward`.

Because the kernel fuses dispatch and combine, it consumes undispatched tokens
and their routing: `FusedDispatchExpertCompute` describes such a compute, and
`FusedTokenDispatcher` hands the tokens to it without communicating.
`GroupedExperts.forward(x, *routing)` forwards either `num_tokens_per_expert`
or `top_scores, selected_experts_indices` to its compute.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@MarioSieg
MarioSieg changed the base branch from main to feat/sonicmoe-bf16-ep September 24, 2026 21:40
@S1ro1
S1ro1 force-pushed the feat/sonicmoe-bf16-ep branch 2 times, most recently from 1f1ff90 to 0609006 Compare September 29, 2026 14:53
Base automatically changed from feat/sonicmoe-bf16-ep to main September 30, 2026 20:42
MarioSieg and others added 2 commits October 2, 2026 06:39
…e extra

Mega MoE kernels come from prime-mega-moe, a closed-source DeepGEMM fork. Its wheel is
built by hand and attached to the prime-kernels release; prime-rl pins the URL and never
builds it from source.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
SonicMoE landed on main squashed (#3535) with an ExpertCompute.validate hook and
GroupedExperts.set_compute. Mega MoE adopts both: MegaMoEExpertCompute.validate carries the
expert-structure checks and configure_moe_runtime installs it through set_compute.

uv.lock was regenerated with the previous deep_gemm wheel URL because the prime-mega-moe
wheel is not uploaded yet; re-run uv lock once it is.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ck compiles with fullgraph

The fused forward was a custom op wrapped in a Python autograd.Function that saved
x.to(bfloat16).contiguous() for backward. When the block compiles with fullgraph under
activation checkpointing, dynamo rejects that saved tensor because it aliases the
autograd.Function's input inside the checkpoint subgraph.

Make the backward a custom op too and register autograd on prime_rl::mega_moe_forward,
saving the op's own inputs. Both ops take the caller's dtypes, run the kernels in bf16,
and return gradients in the inputs' dtypes, so the expert dW dtype is unchanged.
@MarioSieg
MarioSieg marked this pull request as ready for review October 6, 2026 16:27

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Main moved the DeepSeek V4 clamped SwiGLU to `activations.ClampedSilu`;
the helper still imported a `ClampedSwiglu` from `deepseek_v4.moe` that no
longer exists, so constructing `MegaMoEExpertCompute` failed for every model.
`ClampedSwiglu` in `activations.py` is a different function (sigmoid * 1.702,
`up + 1`) that the kernel does not compute, so it stays rejected.
garrett361
garrett361 previously approved these changes Oct 7, 2026

@garrett361 garrett361 left a comment

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.

Looks really good, just had organization comments and questions, mainly.

Comment thread benchmarks/mega_moe/bench_mega_moe.py Resolved
Comment thread docs/scaling.md Resolved
Comment thread docs/scaling.md Resolved
Comment thread docs/scaling.md Resolved
Comment thread src/prime_rl/trainer/sft/train.py Resolved
Comment thread src/prime_rl/trainer/moe_runtime.py Resolved

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

…nel-integration

# Conflicts:
#	packages/prime-rl-configs/src/prime_rl/configs/trainer.py

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

)
self.validate(experts)
hidden = experts.down_proj.shape[1]
reserve_sms_for_comm(num_reserved_sms)

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.

This one @MarioSieg ?

Comment thread docs/scaling.md Resolved

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.

@MarioSieg response to this?

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread benchmarks/mega_moe/bench_mega_moe.py Resolved
garrett361
garrett361 previously approved these changes Oct 19, 2026

@garrett361 garrett361 left a comment

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.

Looks good, thanks for all the iteration on this! I'd still like to clean up some of the internals of mega_moe.py and the kernel names at some point (still requires a number of fairly technical and non-obvious mappings to follow), but let's get this in and clean up later.

@MarioSieg

Copy link
Copy Markdown
Member Author

Looks good, thanks for all the iteration on this! I'd still like to clean up some of the internals of mega_moe.py and the kernel names at some point (still requires a number of fairly technical and non-obvious mappings to follow), but let's get this in and clean up later.

LMK what you would cleanup / refractor I can finish it today and we can merge tomorrow then.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fecda7f. Configure here.

Comment thread src/prime_rl/trainer/rl/train.py Resolved
Comment thread src/prime_rl/trainer/parallel_dims.py Resolved
@MarioSieg
MarioSieg force-pushed the mario/overlap-moe-kernel-integration branch from a5c0048 to af43116 Compare October 20, 2026 11:01
@MarioSieg
MarioSieg merged commit a74bcb8 into main Oct 20, 2026
19 of 20 checks passed
@MarioSieg
MarioSieg deleted the mario/overlap-moe-kernel-integration branch October 20, 2026 11:21
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