Skip to content

LatentMoE implementation - #799

Merged
jacob-morrison merged 12 commits into
akshitab/moe-v2-corefrom
akshitab/latent-moe-original
Jul 30, 2026
Merged

jacob-morrison merged 12 commits into
akshitab/moe-v2-corefrom
akshitab/latent-moe-original

Conversation

@AkshitaB

@AkshitaB AkshitaB commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds LatentMoE support to the original MoE and MoE v2 implementations.

Instead of running routed experts on the model’s hidden representation, LatentMoE wraps the routed expert path with two shared linear projections:

  1. Project routed token payloads into a smaller latent representation: d → ℓ
  2. Perform expert dispatch, expert computation, and combine in latent space
  3. Project the combined routed output back into model space: ℓ → d

The router continues to compute gating decisions from the original model-space representation. Only the routed payload and routed expert computation use
the latent dimension.

Shared experts, transformer norms, and residual operations remain in model space.

Configuration

Original MoE

For the original MoE implementation, add latent_moe to MoEConfig:

  from olmo_core.nn.moe import LatentMoEConfig, MoEConfig

  moe = MoEConfig(
      ...,
      latent_moe=LatentMoEConfig(
          latent_dim=1024,
      ),
  )

The router remains model-width, while the routed experts are built automatically with latent_dim.

MoE v2

For MoE v2, configure the router with the model dimension and the routed experts with the latent dimension:

  from olmo_core.nn.moe import LatentMoEConfig
  from olmo_core.nn.moe.v2.routed_experts import RoutedExpertsConfig
  from olmo_core.nn.moe.v2.router import MoERouterConfigV2

  d_model = 4096
  latent_dim = 1024

  block = OLMoDDPTransformerBlockConfig(
      ...,
      latent_moe=LatentMoEConfig(
          latent_dim=latent_dim,
      ),
      routed_experts_router=MoERouterConfigV2(
          d_model=d_model,
          ...,
      ),
      routed_experts=RoutedExpertsConfig(
          d_model=latent_dim,
          ...,
      ),
  )

The following dimensional constraints are enforced:

  • 0 < latent_dim < d_model
  • routed_experts_router.d_model == d_model
  • routed_experts.d_model == latent_dim
  • Shared expert and shared router dimensions remain equal to d_model

Leave latent_moe=None to use the standard MoE architecture.

Optional pre-up-projection normalization

Normalization before the ℓ → d projection is disabled by default.

Enable it with:

  latent_moe=LatentMoEConfig(
      latent_dim=1024,
      up_proj_input_norm_enabled=True,
  )

When enabled without an explicit configuration, a bias-free RMSNorm is used.

A different normalization can be configured explicitly:

  from olmo_core.nn.layer_norm import LayerNormConfig, LayerNormType

  latent_moe=LatentMoEConfig(
      latent_dim=1024,
      up_proj_input_norm_enabled=True,
      up_proj_input_norm=LayerNormConfig(
          name=LayerNormType.rms,
          eps=1e-6,
          bias=False,
      ),
  )

Projection biases are disabled by default and can be enabled with bias=True.

Supported execution paths

LatentMoE is integrated with:

  • Original MoE and dropless MoE
  • MoE v2 without expert parallelism
  • Synchronous and asynchronous expert parallelism
  • Rowwise, rowwise-wave, and DeepEP v2 backends
  • Hybrid MoE combined-forward paths
  • Tensor, expert, context, data, and pipeline parallel execution

Routing remains model-width in every path, while routed communication and expert computation use latent_dim.

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

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/olmo_core/nn/moe/v2/ep_deepep_v2.py Resolved

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

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/olmo_core/nn/moe/moe.py Resolved

@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: 35524df607

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/olmo_core/nn/moe/moe.py Resolved
Comment thread src/olmo_core/nn/transformer/init.py Resolved

@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: 50ea3068f7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/olmo_core/nn/moe/moe.py Resolved
@AkshitaB
AkshitaB requested a review from jacob-morrison July 29, 2026 01:37

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

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

return routed_output
if self.latent_up_proj_input_norm is not None:
routed_output = self.latent_up_proj_input_norm(routed_output)
return self.latent_up_proj(routed_output)

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 Cast DDP latent outputs before up-projection

When a LatentMoE OLMoDDPTransformerBlock is built with float32 routed experts/projections but a routed backend returns BF16 expert output, this call feeds a BF16 activation directly into an FP32 nn.Linear, which raises a matmul dtype mismatch before the routed branch can be merged. The non-DDP MoE path already casts the routed output to latent_up_proj.weight.dtype; this helper needs the same cast before applying the up projection.

Useful? React with 👍 / 👎.

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.

my codex independently agreed with this, so this seems reasonable to me!

Comment thread src/olmo_core/nn/moe/moe.py Resolved
@AkshitaB
AkshitaB requested a review from jacob-morrison July 29, 2026 07:07

@jacob-morrison jacob-morrison 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.

LGTM! One comment just for type safety, but otherwise seems good

return routed_output
if self.latent_up_proj_input_norm is not None:
routed_output = self.latent_up_proj_input_norm(routed_output)
return self.latent_up_proj(routed_output)

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.

my codex independently agreed with this, so this seems reasonable to me!

@jacob-morrison
jacob-morrison merged commit b835a88 into akshitab/moe-v2-core Jul 30, 2026
31 of 47 checks passed
@jacob-morrison
jacob-morrison deleted the akshitab/latent-moe-original branch July 30, 2026 04:03
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.

2 participants