Keep DeepSeek V4 CSA compressor and indexer in high precision under FP8 training
Fixes #5267.
When training DeepSeek V4 Flash with --fp8-param-gather, the CSA compressor and indexer weights were quantized to FP8 because every layer is built inside fp8_model_init, while the official DeepSeek checkpoint keeps them in BF16. On top of that, Float16Module downcasts the ape and attn_sink parameters to BF16 even though they are intentionally created in FP32. This PR builds the compressor and indexer linears outside the FP8 context and runs their GEMMs in BF16, using a new get_fp8_disabled_context() helper. It also adds a mark_keep_in_fp32() marker that Float16Module respects, so ape and attn_sink stay in FP32 through the BF16 conversion. The module name is now threaded down to the CSA linears as well, so they can be targeted by --te-precision-config-file if needed. Unit tests cover the FP32 preservation; the FP8 parameter behavior needs an FP8-capable GPU and should be covered by the FP8 functional tests in CI.
This pull request requires additional validation before any workflows can run on NVIDIA's runners.
Pull request vetters can view their responsibilities here.
Contributors can view more details about this message here.
@ssam18 Thank you very much for your contribution!
I left one comment.
Besides, could you also help wrap the parameters of mHC with fp32, including the ones in HyperConnectionModule (mapping_proj, alpha_pre, alpha_post, alpha_res, and bias) and the ones in the final contraction (hc_head_fn, hc_head_base, and hc_head_scale, in transformer_block, mtp, and hybrid_block)? Thanks!
@ssam18 Thank you very much for your contribution!
I left one comment.
Besides, could you also help wrap the parameters of mHC with fp32, including the ones in HyperConnectionModule (mapping_proj, alpha_pre, alpha_post, alpha_res, and bias) and the ones in the final contraction (hc_head_fn, hc_head_base, and hc_head_scale, in transformer_block, mtp, and hybrid_block)? Thanks!
Thank you @hxbai. I fixed it in d956562b7. linear_wq_b is now built inside the FP8 context and runs in FP8 like the other quantized GEMMs. linear_weights_proj and the indexer's compressor remain in BF16. Let me know if those should also be FP8.
/ok to test https://github.com/NVIDIA/Megatron-LM/pull/5308/commits/927157e31c1fab34a79cfb5c76781bb717b7acc4
@ssam18 could you solve the linting issue? Thanks!
/ok to test d266e28337f8668b883f36a238782e8066655763
@ssam18 could you help fix the test failure? Thanks!
/ok to test https://github.com/NVIDIA/Megatron-LM/pull/5308/commits/2640b0e5d6a7136eab7a8268a5ba6536b2292443
/claude strict-review
Strict Review Summary
Findings by severity — CRITICAL: 3, IMPORTANT: 1, SUGGESTION: 0
The core mechanisms of this PR are sound and well-tested: get_fp8_disabled_context, the mark_keep_in_fp32/convert_module_to_dtype_except_fp32_marked markers, the Float16Module integration, and the Float16OptimizerWithFloat16Params.sharded_state_dict fix for interleaved fp32/float16 params (correct and covered by a targeted unit test). The threading of name down to the CSA linears is correct, and all target linear classes accept the name kwarg.
However, the high-precision fix is applied inconsistently across forward paths, which undermines the stated goal on exactly the layouts DeepSeek V4 runs in.
Most impactful findings
-
[CRITICAL] Compressor THD path not FP8-disabled (
csa.py:1049-1050)._forward_sbhdwraps the compressor GEMMs inget_fp8_disabled_context, but_forward_thd— the packed-sequence path used in real DeepSeek V4 training — does not. Under--fp8the compressor runs in FP8 there, silently diverging from the reference checkpoint and from the SBHD path. -
[CRITICAL] CP indexer
linear_weights_projnot FP8-disabled (csa.py:2400). The non-CPforward_before_topkwraps this call (line 1358); the_forward_thd_cppath does not. Same divergence under CP + FP8. -
[CRITICAL] Fused mHC path dtype mismatch (
hyper_connection.py:329-339).mapping_proj.weightis now kept in FP32, and the native path upcasts activations to match. Theuse_fused_mhc=Truepath passes the FP32 weight with a BF16 activation intotorch.matmulunchanged — a dtype-mismatch error (or untested cuTile numerics). Enabling mHC-in-FP32 +use_fused_mhcis broken. -
[IMPORTANT] mHC numerics change for all BF16 runs (
hyper_connection.py:270-271). Marking the mHC params FP32 + upcasting changes mHC mapping numerics for every mHC run, not just FP8 — will shift golden values. Please regenerate affected golden values and note the change in the PR description.
Risk assessment
Medium-high. The optimizer and marker infrastructure are solid, but the FP8-disable wrapping must cover the THD and CP forward paths or the fix is effectively a no-op for DeepSeek V4's real training configuration, and the fused-mHC interaction needs to be fixed or explicitly guarded. Recommend addressing the three CRITICAL findings before merge.
/ok to test https://github.com/NVIDIA/Megatron-LM/pull/5308/commits/22363a1b21c6dc41329726222221567d3cd33b23
🔄 Merge queue validation started!
You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28914952456
/ok to test https://github.com/NVIDIA/Megatron-LM/pull/5308/commits/94aa5a3ae460792405f649b81de224f851e642de
🔄 Merge queue validation started!
You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28991531867