[dev] [DeepSeek-v4] Part 2: Hash MoE and SwiGLU clamp - #4481
Conversation
Victarry
left a comment
There was a problem hiding this comment.
Generally looks good to me. Co-reviewd with AI, please take a look~
|
|
||
| activation_func_clamp_value: Optional[float] = None | ||
| """Clamp the output of the linear_fc1 in the activation function. Only used when activation_func |
There was a problem hiding this comment.
[SUGGESTION] The docstring now claims activation_func_clamp_value works for quick_gelu or swiglu, but in this PR clamp is only wired through the weighted SwiGLU path (weighted_bias_swiglu_impl -> WeightedSwiGLUFunction). The dense path bias_swiglu_impl (used by non-MoE / non-token-weighted SwiGLU MLP) does not accept clamp_value and silently ignores it.
So a user setting activation_func_clamp_value on a model with dense SwiGLU MLP layers will see zero effect, with no warning.
Suggestion: either
- extend
bias_swiglu_impl/BiasSwiGLUFunction/SwiGLUFunctionto also accept and respectclamp_value, or - narrow the docstring to "weighted SwiGLU (MoE) only" and add a runtime check that warns or asserts when
activation_func_clamp_value > 0is set on a dense-SwiGLU configuration.
There was a problem hiding this comment.
fixed the docstring
|
/ok to test df71a39 |
|
/ok to test c51d461 |
|
LGTM |
|
/ok to test 608a1b2 |
|
/ok to test dfadf2e |
|
/ok to test d6a9445 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/25151919314 |
### PR Category <!-- One of [ Train | Inference | Compress | Serve | RL | Core | Hardware | CICD | Tools | Others ] --> [Train] Most of codes are copied from Megatron-LM Dev branch. The dev branch is different with main branch or release version. Megatron LM PR: DeepSeek-V4: NVIDIA#4458 NVIDIA#4481 NVIDIA#4518 mHC: NVIDIA#2943 ### PR Types <!-- One of [ User Experience | New Features | Bug Fixes | Improvements | Performance | Breaking Change| Deprecations | Test Case | Docs | Others ] --> [New features] ### PR Description <!-- Describe what you’ve done --> Add DeepSeek V4 model into FlagScale and Megatron-FL Supported: 1. CSA and HCA 2. Hash Router 3. mHC 4. Engram(optional) Unsupported: 1. Sqrtsoftpuls router score function. ✅ 2. mHC recompute. ✅ 3. Overlap_grad_reduce and overlap_param_gather when Zero 1. ✅ 4. Any infra optimizations. ### NOTE: This is only a draft pr, please reivew to give more suggestions. such as: 1. File structure. - All modules are moved into Megatron-FL ### Next plan: 1. Distributed training. ✅ 3. Muon optimizer with Zero 1 adaptation. 🚧 4. Low precision is out of scope of this pr, limited by resource. 5. Maybe context parallel for sparse attention. 6. Welcome to give more suggestions. --------- Co-authored-by: Hongxiao Bai <hongxiaob@nvidia.com> Co-authored-by: Yuzhong Wang <yuzhongw@nvidia.com>
### PR Category <!-- One of [ Train | Inference | Compress | Serve | RL | Core | Hardware | CICD | Tools | Others ] --> [Train] Most of codes are copied from Megatron-LM Dev branch. The dev branch is different with main branch or release version. Megatron LM PR: DeepSeek-V4: NVIDIA/Megatron-LM#4458 NVIDIA/Megatron-LM#4481 NVIDIA/Megatron-LM#4518 mHC: NVIDIA/Megatron-LM#2943 ### PR Types <!-- One of [ User Experience | New Features | Bug Fixes | Improvements | Performance | Breaking Change| Deprecations | Test Case | Docs | Others ] --> [New features] ### PR Description <!-- Describe what you’ve done --> Add DeepSeek V4 model into FlagScale and Megatron-FL Supported: 1. CSA and HCA 2. Hash Router 3. mHC 4. Engram(optional) Unsupported: 1. Sqrtsoftpuls router score function. ✅ 2. mHC recompute. ✅ 3. Overlap_grad_reduce and overlap_param_gather when Zero 1. ✅ 4. Any infra optimizations. ### NOTE: This is only a draft pr, please reivew to give more suggestions. such as: 1. File structure. - **All modules are moved to Megatron-FL. Only model_builder is left in Flagscale.** - Delete Engram related CI or not? ### Next plan: 1. Distributed training. ✅ 3. Muon optimizer with Zero 1 adaptation. 😢 4. Low precision is out of scope of this pr, limited by resource. 5. Maybe context parallel for sparse attention. 6. Welcome to give more suggestions. --------- Co-authored-by: zhaoyingli <86812880+zhaoyinglia@users.noreply.github.com>
Map the canonical padded vocabulary size into TransformerConfig for hash routing and hide the redundant generated CLI field. Reconstructed from NVIDIA#4481 and the frozen feature set in NVIDIA#5795. Signed-off-by: Deyu Fu <Deyu.Foo@gmail.com>
…layers Co-authored-by: dingzicha1997<dingzilin@huawei.com> # message auto-generated for no-merge-commit merge: !4898 merge codex/hash-layer-init into master fix: use deterministic placeholder initialization for DeepSeek4 hash layers Created-by: dingzicha1997 Commit-by: dingzicha1997 Merged-by: ascend-robot Description: ## What this PR does / why we need it? This PR updates the initialization logic of the DeepSeek4 hash-layer `tid2eid` table. The previous implementation generated one `torch.randperm(moe_router_topk)` for each token. This only permuted expert IDs within `[0, moe_router_topk)` and consumed a large amount of RNG state during model construction. Following NVIDIA/Megatron-LM#4481, this PR: - replaces the random initialization with deterministic round-robin initialization across all experts; - keeps `tid2eid` as a non-trainable parameter; - preserves the `int64` dtype required by the existing `torch.gather` and `torch.scatter` routing path; - documents that this initialization is only a placeholder for constructing a runnable model from scratch. DeepSeek4 checkpoints provide a pre-trained `tid2eid` table. No public recipe for initializing this table for real training is currently available, so the placeholder initialization in this PR must not be treated as a real training initialization strategy. Reference: NVIDIA/Megatron-LM#4481 The remaining changes in `router.py` are formatting and static-check fixes produced while making the file pass the repository pre-commit checks. They do not change the existing routing behavior. ## Does this PR introduce any user-facing change? No CLI or configuration interface is changed. For models loaded from a valid DeepSeek4 checkpoint, the checkpoint-provided `tid2eid` table overwrites the placeholder value, so checkpoint loading behavior is unchanged. For hash-layer models constructed without a checkpoint, the initial routing table changes from random permutations limited to the first `moe_router_topk` experts to deterministic round-robin assignments across all experts. The placeholder initialization is not suitable for real-world training. ## How was this patch tested? - Ran the complete pre-commit checks for: - `mindspeed_llm/core/transformer/moe/router.py` - Verified: - `tid2eid` shape and deterministic round-robin values; - expert indices remain within the configured expert range; - `tid2eid` remains an `int64` non-trainable parameter; - compatibility with the existing `torch.gather` and `torch.scatter` routing path; - non-hash layers do not create `tid2eid`. - Ran the DeepSeek4 checkpoint-loading ST case for 15 iterations. Training completed successfully and the checkpoint loaded without state-dict errors. - The existing DeepSeek4 ST baseline JSON is intentionally not modified by this PR. See merge request: Ascend/MindSpeed-LLM!4898
…layers Co-authored-by: dingzicha1997<dingzilin@huawei.com> # message auto-generated for no-merge-commit merge: !4911 merge codex/hash-layer-init-26.1.0 into 26.1.0 fix: use deterministic placeholder initialization for DeepSeek4 hash layers Created-by: dingzicha1997 Commit-by: dingzicha1997 Merged-by: ascend-robot Description: ## What this PR does / why we need it? This PR updates the initialization logic of the DeepSeek4 hash-layer `tid2eid` table. The previous implementation generated one `torch.randperm(moe_router_topk)` for each token. This only permuted expert IDs within `[0, moe_router_topk)` and consumed a large amount of RNG state during model construction. Following NVIDIA/Megatron-LM#4481, this PR: - replaces the random initialization with deterministic round-robin initialization across all experts; - keeps `tid2eid` as a non-trainable parameter; - preserves the `int64` dtype required by the existing `torch.gather` and `torch.scatter` routing path; - documents that this initialization is only a placeholder for constructing a runnable model from scratch. DeepSeek4 checkpoints provide a pre-trained `tid2eid` table. No public recipe for initializing this table for real training is currently available, so the placeholder initialization in this PR must not be treated as a real training initialization strategy. Reference: NVIDIA/Megatron-LM#4481 The remaining changes in `router.py` are formatting and static-check fixes produced while making the file pass the repository pre-commit checks. They do not change the existing routing behavior. ## Does this PR introduce any user-facing change? No CLI or configuration interface is changed. For models loaded from a valid DeepSeek4 checkpoint, the checkpoint-provided `tid2eid` table overwrites the placeholder value, so checkpoint loading behavior is unchanged. For hash-layer models constructed without a checkpoint, the initial routing table changes from random permutations limited to the first `moe_router_topk` experts to deterministic round-robin assignments across all experts. The placeholder initialization is not suitable for real-world training. ## How was this patch tested? - Ran the complete pre-commit checks for: - `mindspeed_llm/core/transformer/moe/router.py` - Verified: - `tid2eid` shape and deterministic round-robin values; - expert indices remain within the configured expert range; - `tid2eid` remains an `int64` non-trainable parameter; - compatibility with the existing `torch.gather` and `torch.scatter` routing path; - non-hash layers do not create `tid2eid`. - Ran the DeepSeek4 checkpoint-loading ST case for 15 iterations. Training completed successfully and the checkpoint loaded without state-dict errors. - The existing DeepSeek4 ST baseline JSON is intentionally not modified by this PR. See merge request: Ascend/MindSpeed-LLM!4911
What does this PR do ?
We will create several PRs to functionally support DeepSeek-v4 training. This is the second one.
Add DeepSeek-v4 Hash MoE and SwiGLU clamp.
--moe-n-hash-layers.--activation-func-clamp-value.Issue tracking
For PRs from open-source community contributors:
Linked issue:
Contribution process
Pre-checks
Code review
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.
Step 1: Mark PR as "Ready for Review"
.github/CODEOWNERS.Final Review might get declined if these requirements are not fulfilled.
Step 2: Final Review
For PRs that change
megatron/core, once all expert reviewers have approved, theFinal Reviewlabel is applied automatically and final reviewers are assigned.For PRs outside
megatron/core, this step is skipped.Step 3: Approved
Once all required reviewers have approved, the
Approvedlabel is applied automatically.Merge
Any member of mcore-engineers will be able to merge your PR.
For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.