Skip to content

Add fused MoE transformer block and model - #737

Merged
AkshitaB merged 5 commits into
akshitab/moe-v2-corefrom
akshitab/moe-v2-block-model
Jun 29, 2026
Merged

AkshitaB merged 5 commits into
akshitab/moe-v2-corefrom
akshitab/moe-v2-block-model

Conversation

@AkshitaB

Copy link
Copy Markdown
Contributor

Adds the fused Mixture-of-Experts transformer block and model and wires them into the transformer build dispatch.

What's added

  • MoEFusedV2TransformerBlock and MoEFusedV2Transformer — the fused MoE block and model.
  • no_ep.py — the no-expert-parallel forward dispatch (combined_forward_no_ep).
  • tbo_state.py — the two-batch-overlap pending-context dataclass.
  • fp8.py — the rowwise-FP8 block machinery (shared-expert prequant cache + forward helpers) added alongside the existing config.
  • Transformer integration — moe_fused_v2 entries on TransformerType / TransformerBlockType, MoEFusedV2TransformerConfig (builds the model), the block-build dispatch, and InitMethod.init_moe_v2 with the per-block init dispatch.

Notes

  • The fused block consumes a single layer_norm config and builds identical attention and feed-forward norms from it.
  • The expert-parallel dispatch families (sync / no-sync VDev / no-sync rowwise) are imported lazily inside their wrapper methods, so this change imports and runs with only the no-EP path present. Those families land in follow-up changes.

Testing

src/test/nn/moe/v2/block_no_ep_test.py:

  • test_v2_no_ep_module_names_importable (CPU)
  • test_v2_no_ep_forward_backward_smoke, test_v2_no_ep_apply_compile_forward_smoke (GPU) — forced-router no-EP block forward/backward and compile.
  • test_v2_transformer_config_builds_and_initializes (GPU) — end-to-end config build + weight init.

Existing tests in src/test/nn/moe/v2/ continue to pass; make checks is clean.

🤖 Generated with Claude Code

AkshitaB and others added 2 commits June 25, 2026 14:12
Adds the fused MoE block (MoEFusedV2TransformerBlock) and model
(MoEFusedV2Transformer) along with the no-expert-parallel forward
dispatch (no_ep.py), the two-batch-overlap pending-context dataclass
(tbo_state.py), and the rowwise-FP8 block machinery (shared-expert
prequant cache + forward helpers) re-added to fp8.py.

The expert-parallel dispatch families (sync / no-sync VDev / no-sync
rowwise) are imported lazily inside their wrapper methods so this drop
imports and runs with only the no-EP path present; those families land
in follow-up changes.

Co-authored-by: Tianhua Tao <[email protected]>

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Adds the moe_fused_v2 entries to TransformerType / TransformerBlockType,
the MoEFusedV2TransformerConfig (which builds MoEFusedV2Transformer), the
block-build dispatch, and InitMethod.init_moe_v2 plus the per-block init
dispatch in the base model. The fused block consumes a single layer_norm
config (it builds identical attention and feed-forward norms from it).

Adds no-EP block forward/backward and apply_compile smoke tests plus an
end-to-end config-build/init test (GPU), and a CPU importability check.

Co-authored-by: Tianhua Tao <[email protected]>

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>

@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: 8b09631aca

ℹ️ 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".

Comment thread src/olmo_core/nn/moe/v2/no_ep.py Resolved
Comment thread src/olmo_core/nn/moe/v2/block.py Resolved
Comment thread src/olmo_core/nn/moe/v2/model.py Resolved
Comment thread src/olmo_core/nn/moe/v2/model.py Resolved
Comment thread src/olmo_core/nn/transformer/config.py Resolved
- Use the optional-nvtx fallback in no_ep.py and model.py so importing the
  block/model doesn't require the profiling extra.
- Only import the rowwise EP metric helpers when the rowwise all-to-all path
  is configured, so compute_auxiliary_metrics()/reset_metrics() don't fail on
  the no-EP path (that helper module isn't present here).
- Look up per-block kwargs with int(block_key) to match the base transformer,
  so context-parallel / RoPE per-block buffers aren't dropped.
- Make MoEFusedV2Transformer.init_weights() accept world_mesh=None, so the
  standalone config.build(...).init_weights(device=...) path works without PP/EP.
- Forward tie_word_embeddings through MoEFusedV2TransformerConfig.build() and
  re-tie after to_empty() during init_weights().

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>

@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: e33584c906

ℹ️ 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".

Comment thread src/olmo_core/nn/moe/v2/model.py Resolved
Comment on lines +1159 to +1160
if loss_reduction == "mean":
return (lhs + rhs) * 0.5

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Weight TBO mean losses by valid-token counts

When TBO is enabled with loss_reduction="mean", each half has already been reduced by LMHead over its own non-ignored labels, so averaging the two scalar means gives equal weight to both halves. For padded batches or any uneven ignore_index distribution across the split, this reports a different loss than the non-TBO path and changes gradients; combine unreduced/summed losses with the corresponding valid-token counts instead.

Useful? React with 👍 / 👎.

**kwargs,
)

h = self.forward_embed(input_ids)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve caller-provided input embeddings

Calls that pass input_embeddings through this Transformer API (for example MultimodalLM, which splices image features into token embeddings) have that kwarg swallowed by _prepare_inputs, and this line re-embeds input_ids instead. In those multimodal runs the image features are silently dropped; mirror the base Transformer.forward() handling or reject input_embeddings explicitly.

Useful? React with 👍 / 👎.

- Document MoEFusedV2TransformerBlockConfig (class + per-field attribute
  docstrings for the live fields; group the deferred expert-parallel knobs
  under an explanatory comment).
- Add a class docstring to MoEFusedV2TransformerBlock and SyncedTboPendingContext.
- Correct the MoEFusedV2Transformer docstring to reference the fused block and
  its config instead of the v1 MoE block.
- Move the misplaced init_weights() docstring ahead of the local imports so it
  is an actual docstring.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>

@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: 22aa4eb787

ℹ️ 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".

reduce_grads_in_fp32: bool = True,
bucket_cap_mb: Optional[int] = None,
):
from olmo_core.nn.parallel.distributed import MultiGroupDistributedDataParallel

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Import a DDP wrapper that exists

Calling apply_dp() currently fails before wrapping anything because olmo_core.nn.parallel.distributed is not present in this tree (checked repo-wide with rg, and there is no src/olmo_core/nn/parallel package). Since apply_ddp() is hard-disabled above, any MoE-v2 run that tries to use the provided data-parallel path will hit ModuleNotFoundError here instead of starting training.

Useful? React with 👍 / 👎.



@dataclass
class MoEFusedV2TransformerConfig(TransformerConfig):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Default fused configs to the fused type

MoEFusedV2TransformerConfig inherits name=TransformerType.default, but build() below only accepts TransformerType.moe_fused_v2, so constructing the advertised specialized config directly fails with NotImplementedError unless every caller redundantly passes name=...; the new block config has the same inherited-default problem for TransformerBlockType. Please override these subclass defaults to their fused enum values so the public config classes build the objects they name.

Useful? React with 👍 / 👎.

Comment on lines +277 to +280
if self == InitMethod.llama:
std = std / (2 * num_blocks) ** 0.5
elif self == InitMethod.llama_depth:
std = std / (2 * (block_idx + 1)) ** 0.5

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor fan-in initialization for fused experts

For init_method=fan_in, neither branch here runs, so the fused MoE routers and expert matrices all keep the caller's default std (usually 0.02). The existing MoE initializer handles fan_in by using d_model**-0.5 for routers/up/gate and hidden_size**-0.5 for down projections; because MoE-v2 configs inherit the same InitMethod without validation, fan-in configs silently get the wrong variance. Add a fan-in path with separate stds for the fused up/gate and down tensors.

Useful? React with 👍 / 👎.

Add a TODO(ep-config) on the expert-parallel knob group in
MoEFusedV2TransformerBlockConfig: the flat ep_no_sync_* flag bag conflates mode
selection with per-mode and rowwise-only tuning, so the eventual refactor should
follow the codebase's Registrable per-variant config pattern (one config per EP
mode). To be done when the EP families land.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
@AkshitaB
AkshitaB merged commit ce19d8e into akshitab/moe-v2-core Jun 29, 2026
46 of 47 checks passed
@AkshitaB
AkshitaB deleted the akshitab/moe-v2-block-model branch June 29, 2026 21:25
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.

1 participant