Megatron-LM icon indicating copy to clipboard operation
Megatron-LM copied to clipboard

Keep DeepSeek V4 CSA compressor and indexer in high precision under FP8 training

Open ssam18 opened this issue 5 months ago • 3 comments

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.

ssam18 avatar Jun 11 '26 21:06 ssam18

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.

copy-pr-bot[bot] avatar Jun 11 '26 21:06 copy-pr-bot[bot]

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

hxbai avatar Jun 12 '26 01:06 hxbai

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

ssam18 avatar Jun 12 '26 01:06 ssam18

/ok to test https://github.com/NVIDIA/Megatron-LM/pull/5308/commits/927157e31c1fab34a79cfb5c76781bb717b7acc4

hxbai avatar Jun 13 '26 04:06 hxbai

@ssam18 could you solve the linting issue? Thanks!

hxbai avatar Jun 16 '26 07:06 hxbai

/ok to test d266e28337f8668b883f36a238782e8066655763

hxbai avatar Jun 17 '26 02:06 hxbai

@ssam18 could you help fix the test failure? Thanks!

hxbai avatar Jun 24 '26 03:06 hxbai

/ok to test https://github.com/NVIDIA/Megatron-LM/pull/5308/commits/2640b0e5d6a7136eab7a8268a5ba6536b2292443

hxbai avatar Jul 05 '26 14:07 hxbai

/claude strict-review

yaox12 avatar Jul 06 '26 03:07 yaox12

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

  1. [CRITICAL] Compressor THD path not FP8-disabled (csa.py:1049-1050). _forward_sbhd wraps the compressor GEMMs in get_fp8_disabled_context, but _forward_thd — the packed-sequence path used in real DeepSeek V4 training — does not. Under --fp8 the compressor runs in FP8 there, silently diverging from the reference checkpoint and from the SBHD path.

  2. [CRITICAL] CP indexer linear_weights_proj not FP8-disabled (csa.py:2400). The non-CP forward_before_topk wraps this call (line 1358); the _forward_thd_cp path does not. Same divergence under CP + FP8.

  3. [CRITICAL] Fused mHC path dtype mismatch (hyper_connection.py:329-339). mapping_proj.weight is now kept in FP32, and the native path upcasts activations to match. The use_fused_mhc=True path passes the FP32 weight with a BF16 activation into torch.matmul unchanged — a dtype-mismatch error (or untested cuTile numerics). Enabling mHC-in-FP32 + use_fused_mhc is broken.

  4. [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.

claude[bot] avatar Jul 06 '26 03:07 claude[bot]

/ok to test https://github.com/NVIDIA/Megatron-LM/pull/5308/commits/22363a1b21c6dc41329726222221567d3cd33b23

hxbai avatar Jul 07 '26 05:07 hxbai

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28914952456

svcnvidia-nemo-ci avatar Jul 08 '26 03:07 svcnvidia-nemo-ci

/ok to test https://github.com/NVIDIA/Megatron-LM/pull/5308/commits/94aa5a3ae460792405f649b81de224f851e642de

hxbai avatar Jul 08 '26 04:07 hxbai

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28991531867

svcnvidia-nemo-ci avatar Jul 09 '26 03:07 svcnvidia-nemo-ci