Skip to content

[DeepSeek-V4] Implement MoE routing primitives#3871

Merged
copybara-service[bot] merged 1 commit into
mainfrom
dsv4-moe-routing-primitives
Jun 10, 2026
Merged

[DeepSeek-V4] Implement MoE routing primitives#3871
copybara-service[bot] merged 1 commit into
mainfrom
dsv4-moe-routing-primitives

Conversation

@parambole

@parambole parambole commented May 11, 2026

Copy link
Copy Markdown
Collaborator

Description

Implement Mixture of Experts (MoE) routing primitives and infrastructural updates required for DeepSeek-V4 integration into MaxText.

  • DeepSeek Routing Primitives: Integrated token-based hash routing logic and DeepSeek-specific Top-K routing directly into moe.py's get_topk function.
  • Safe Layer Parameters: Updated RoutedMoE and RoutedAndSharedMoE to accept layer_idx and input_ids via optional keyword arguments. This allows DeepSeek to pipe these variables down into the hashing logic, while safely defaulting to 0 and None so existing callers are entirely unimpacted.
  • Hardware Optimization Bypasses: Added boundary checks to bypass the TPU fused_moe_matmul (via the vllm_rpa block) specifically for hash-routed layers where it is unsupported.
  • Activation Functions: Registered the sqrtsoftplus custom activation function in linears.py required by the DeepSeek V4 MoE router.
  • Unit test suite (tests/unit/deepseek_v4_vs_reference_test.py): Validates MoE routing parity against PyTorch reference implementations.

Tests

Tested on CPU. All DeepSeek V4 and core MoE unit tests pass successfully.

pytest tests/unit/deepseek_v4_vs_reference_test.py tests/unit/moe_test.py
============================= test session starts ==============================
collecting ... 
collected 43 items
tests/unit/deepseek_v4_vs_reference_test.py ............                 [ 27%]
tests/unit/moe_test.py ...............................                   [100%]
================ 25 passed, 18 skipped, 123 warnings in 47.50s =================

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@codecov

codecov Bot commented May 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.53846% with 15 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/maxtext/layers/moe.py 64.86% 8 Missing and 5 partials ⚠️
src/maxtext/layers/linears.py 0.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@parambole
parambole force-pushed the dsv4-moe-routing-primitives branch from 37ee811 to 31329c5 Compare May 11, 2026 20:38
@parambole
parambole force-pushed the deepseek_v4_core_primitives branch from 1ab79e5 to c025463 Compare May 12, 2026 17:22
@parambole
parambole force-pushed the dsv4-moe-routing-primitives branch from 31329c5 to 22a57ff Compare May 12, 2026 17:23
@parambole
parambole force-pushed the deepseek_v4_core_primitives branch from c025463 to 68c44a6 Compare May 12, 2026 21:12
@parambole
parambole force-pushed the dsv4-moe-routing-primitives branch from 22a57ff to 32869e5 Compare May 12, 2026 21:12
@parambole
parambole force-pushed the deepseek_v4_core_primitives branch 2 times, most recently from 72a92a7 to e81f52d Compare May 14, 2026 17:45
@parambole
parambole force-pushed the dsv4-moe-routing-primitives branch from 32869e5 to c92f2e0 Compare May 14, 2026 17:51
@parambole parambole changed the title Implement custom MoE HashRouter, TopKRouter, and sqrtsoftplus Implement DeepSeek-V4 MoE routing primitives (HashRouter, TopKRouter, RoutedMoE) May 14, 2026
@parambole parambole changed the title Implement DeepSeek-V4 MoE routing primitives (HashRouter, TopKRouter, RoutedMoE) [DeepSeek-V4] Implement MoE routing primitives (HashRouter, TopKRouter, RoutedMoE) May 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Hi @parambole, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@parambole
parambole force-pushed the deepseek_v4_core_primitives branch 6 times, most recently from 0cac1e1 to fa01c9c Compare June 5, 2026 00:33
Base automatically changed from deepseek_v4_core_primitives to main June 5, 2026 01:06
@parambole
parambole force-pushed the dsv4-moe-routing-primitives branch from dc34263 to 41e3f67 Compare June 5, 2026 18:39

@dipakg-lang dipakg-lang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks like this change wrongly has attention related changes instead of MOE routing related ones?

@parambole
parambole changed the base branch from main to deepseek_v4_compressed_attention June 8, 2026 20:21
@parambole
parambole force-pushed the dsv4-moe-routing-primitives branch from 6c801b1 to cb89739 Compare June 8, 2026 21:09
@parambole

Copy link
Copy Markdown
Collaborator Author

Looks like this change wrongly has attention related changes instead of MOE routing related ones?

Ah! That was just an artifact of the base branch I selected. I recently changed the PR's base branch to deepseek_v4_compressed_attention, which should isolate the diff to show only the MoE routing changes going forward. Let me know if the diff still looks noisy!

@parambole
parambole force-pushed the dsv4-moe-routing-primitives branch from cb89739 to eb9de77 Compare June 8, 2026 21:48
@parambole parambole changed the title [DeepSeek-V4] Implement MoE routing primitives (HashRouter, TopKRouter, RoutedMoE) [DeepSeek-V4] Implement MoE routing primitives Jun 8, 2026
@parambole
parambole force-pushed the dsv4-moe-routing-primitives branch from eb9de77 to 0167a4e Compare June 9, 2026 00:26
Comment thread src/maxtext/layers/moe.py
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated

@RissyRan RissyRan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the change! Please note that we have DS v2, and a few if condition may break the functionality.

Comment thread src/maxtext/configs/types.py Outdated
Comment thread src/maxtext/configs/types.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated

@shuningjin shuningjin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for refactoring and reusing MoE logic! Further comments:

  • Perhaps we can use is_hash_routing instead layer_idx to indicate hash router for clarify. Decoder can use idx to determine.
  • Perhaps can use nnx.Variable instead of nnx.Param for register_buffer counterpart

Comment thread src/maxtext/configs/base.yml Outdated
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/configs/types.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py
Comment thread tests/unit/deepseek_v4_vs_reference_test.py Outdated

@RissyRan RissyRan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the change! Please add an assertion in types.py.

Comment thread src/maxtext/configs/base.yml Outdated
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py Outdated

@shuningjin shuningjin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great work!

Comment thread src/maxtext/layers/moe.py
Comment thread src/maxtext/layers/moe.py Outdated
Comment thread src/maxtext/layers/moe.py
@shuningjin

Copy link
Copy Markdown
Collaborator

Note sure why tests/integration/hlo_diff_test.py fails. Please double check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants