Skip to content

Add DeepSeek-V4 HyperHead, align Sinkhorn implementation, and add parity tests - #4337

Merged
copybara-service[bot] merged 1 commit into
mainfrom
dsv4-bug-fixes
Jul 18, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
dsv4-bug-fixes

Conversation

@snehalv2002

@snehalv2002 snehalv2002 commented Jul 2, 2026 •

Copy link
Copy Markdown
Collaborator

Description

This PR introduces necessary bug fixes to DeepSeek4 in the implementation and checkpoint mapping to pass a logit parity test against the HuggingFace implementation.

Bug Fixes & Architectural Additions for DeepSeek4:

  • HyperConnection (mHC) Head & Stability Fixes: Added the DeepSeek4HyperHead module in layers/mhc.py and routed DEEPSEEK4 decoder blocks to use it in layers/decoders.py. Also fixed the sinkhorn function by adding an epsilon (1e-5) for numerical stability during normalization and adjusting the loop iterations.
  • Compressed Attention Causal Mask Fix: Fixed an issue in layers/attention_compressed.py where decoding with seq_len == 1 or compressed_len == 0 returned None for the causal mask. It now properly returns a tensor of zeros to prevent shape mismatch errors downstream.
  • MoE Block Routing: Updated layers/moe.py to ensure DEEPSEEK4 uses the appropriate top-K weight scaling and SwiGLU MLP activation bounds clipping, matching the DeepSeek standard.
  • Checkpoint Conversion Mapping: Implemented HuggingFace parameter mapping and hook functions (DEEPSEEKV4_MAXTEXT_TO_HF_PARAM_MAPPING and DEEPSEEKV4_MAXTEXT_TO_HF_PARAM_HOOK_FN) in utils/param_mapping.py to support correct weight loading/saving for the mHC and attention compressor blocks.
  • Configuration & Testing Updates:
    • Added a new deepseek4-tiny.yml configuration for debugging.
    • Updated the deepseek4-284b.yml config with necessary routing biases, top-k probabilities, and RoPE positional embeddings.
    • Added comprehensive unit testing in tests/unit/deepseek_v4_vs_reference_test.py.

Tests

  • Added new reference tests via tests/unit/deepseek_v4_vs_reference_test.py. We test the conversion mapping for each unique layer type (sliding window + hash routing, CSA/Indexer + hash routing, HCA + topk routing, CSA/Indexer + topk routing) used to do checkpoint conversion as part of unit tests by generating reference logits from the pytorch implementation, converting weights to maxtext implementation, generating maxtext logits, and verifying parity of the logits.
  • 509930698 This comment defines the relevant test scripts and test outputs we used to verify the bug fixes. We verified parity between the output of the reference HuggingFace implementation provided by DeepSeek against our own MaxText implementation.

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 Jul 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 29.65116% with 121 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...xtext/checkpoint_conversion/utils/param_mapping.py 3.25% 119 Missing ⚠️
src/maxtext/layers/moe.py 60.00% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@parambole parambole 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.

nit: Could you please make the PR title more descriptive ?

@parambole parambole 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.

LGTM

@snehalv2002 snehalv2002 changed the title bug fixes for deepseek4 Add DeepSeek-V4 HyperHead, align Sinkhorn implementation, and add parity tests Jul 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

🤖 I'm sorry @RissyRan, but I was unable to process your request. Please see the logs for more details.

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

LGTM, just a few minor comments. Thanks!

Comment thread src/maxtext/layers/mhc.py Resolved
@RissyRan

Copy link
Copy Markdown
Collaborator

🤖 I'm sorry @RissyRan, but I was unable to process your request. Please see the logs for more details.

I was able to run it yesterday for other PRs. Potentially the changes are large and it met "message": "Reached max session turns for this session. Increase the number of turns by specifying maxSessionTurns in settings.json.",

@snehalv2002
snehalv2002 force-pushed the dsv4-bug-fixes branch 7 times, most recently from 7f4b99f to aee748b Compare July 16, 2026 23:15
@snehalv2002 snehalv2002 reopened this Jul 17, 2026
@snehalv2002

Copy link
Copy Markdown
Collaborator Author

@googlebot I signed it!

…e.py, and match maxtext sinkhorn implementation to HF reference.
@copybara-service
copybara-service Bot merged commit 995eea2 into main Jul 18, 2026
49 of 50 checks passed
@copybara-service
copybara-service Bot deleted the dsv4-bug-fixes branch July 18, 2026 01:23
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.

4 participants