Skip to content

Add v2 shared experts - #724

Merged
AkshitaB merged 13 commits into
akshitab/moe-v2-corefrom
akshitab/moe-v2-shared-experts
Jun 23, 2026
Merged

AkshitaB merged 13 commits into
akshitab/moe-v2-corefrom
akshitab/moe-v2-shared-experts

Conversation

@AkshitaB

Copy link
Copy Markdown
Contributor

Adds nn/moe/v2/shared_experts.py: SharedExperts and SharedExpertsConfig.

Shared experts act like a feed-forward applied to every token, but support more than one expert. The up/gate projections of all experts are column-packed into one weight so the first projection is a single GEMM (BS, D) @ (D, E*2H); the result is split per-expert into up/gate, run through a SwiGLU nonlinearity, and the per-expert down projection is a grouped (batched) GEMM (E, BS, H) x (E, H, D) -> (E, BS, D). A split forward1/forward2 variant is provided so the projection and the down-GEMM can overlap with expert-parallel communication.

Depends only on olmo_core.config; nvtx is an optional import with the olmo_core._nvtx no-op fallback. Includes CPU unit tests covering num_params, the forward output shape, the single-expert case, and that the split forward matches the fused one.

🤖 Generated with Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@AkshitaB AkshitaB changed the title Add MoE-v2 shared experts Add v2 shared experts Jun 12, 2026
@AkshitaB
AkshitaB force-pushed the akshitab/moe-v2-shared-experts branch from 4fe872d to 0fba859 Compare June 12, 2026 21:41
@farhatkevin

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d7c835ca06

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@AkshitaB
AkshitaB force-pushed the akshitab/moe-v2-shared-experts branch from d7c835c to e8d91a8 Compare June 23, 2026 09:45

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 13da5b8082

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

TianhuaTao and others added 13 commits June 23, 2026 04:11
Adds `nn/moe/v2/shared_experts.py`: `SharedExperts` and `SharedExpertsConfig`.
Shared experts act like a feed-forward applied to every token, but support more
than one expert by column-packing the up/gate projections into a single GEMM
and running the per-expert down projection as a grouped (batched) GEMM, with a
SwiGLU nonlinearity.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Guard the optional `nvtx` import with the `olmo_core._nvtx` no-op fallback.
- Use the builtin `tuple` annotation instead of `typing.Tuple`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Cover config num_params, the forward output shape (E, B, S, D), the
single-expert case, and that the split forward1/forward2 (used for EP overlap)
matches the fused forward.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
SharedExperts hardcodes SiLU; the standard FeedForward exposes a configurable
ActivationFunction, and the v1 MoE MLP / routed_experts each roll their own.
Flag the duplication for a future unification.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
w_up_gate and w_down were left as raw torch.empty storage with no
reset_parameters, so SharedExpertsConfig.build() produced uninitialized
weights -> NaNs/nondeterministic output in production (the tests masked it
by calling normal_() manually). Add reset_parameters() with
kaiming_uniform_(a=sqrt(5)) and call it from __init__, matching the v1 MoE
expert MLPs. Drop the manual test init and add a regression test asserting
build() yields finite weights/output.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Align SharedExperts.reset_parameters with the v2 router's reset_parameters
and the default std the model-level init (init_moe_v2) applies, instead of
the kaiming_uniform default. init_moe_v2 still overrides these at model
construction; this is just the standalone-build default.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…th-scaled std

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
num_params() listed a :param d_model: it doesn't take (copy-paste), and
build() had no docstring. Correct both to match the codebase convention.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…docstring

Mirror the note added to MoERouterConfigV2: the v2 configs store dimension
fields directly, unlike the rest of the codebase which passes them only to
build(); flag for future unification.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Existing tests only checked output shape and forward1/forward2 split
consistency (both could be wrong identically). Add a per-expert SwiGLU +
down-projection reference and assert the vectorized forward matches it.
Also drop a stale init comment.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Document that forward1/forward2 are numerically-equivalent halves of forward,
split so the EP path can run shared experts on a separate stream and overlap
each half with the routed experts' dispatch/combine all-to-all.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Replace the "unify the SwiGLU implementations" TODO with a note that the
SiLU gating is intentional (matching forward1/forward2 and the routed
kernels), and that unifying the codebase's SwiGLU sites is a general
cross-module refactor tracked separately rather than done in this precursor.
Also simplify _swiglu to a one-liner.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@AkshitaB
AkshitaB force-pushed the akshitab/moe-v2-shared-experts branch from f4aa997 to a68fc88 Compare June 23, 2026 11:11
@AkshitaB
AkshitaB merged commit 527a181 into akshitab/moe-v2-core Jun 23, 2026
23 checks passed
@AkshitaB
AkshitaB deleted the akshitab/moe-v2-shared-experts branch June 23, 2026 11: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