[#16196][fix] Support MIXED_PRECISION experts-only NVFP4 DeepSeek-V4 checkpoints#16276
Open
waynehacking8 wants to merge 4 commits into
Open
[#16196][fix] Support MIXED_PRECISION experts-only NVFP4 DeepSeek-V4 checkpoints#16276waynehacking8 wants to merge 4 commits into
waynehacking8 wants to merge 4 commits into
Conversation
…ntized The ModelOpt experts-only NVFP4 DeepSeek-V4 checkpoints (e.g. nvidia/DeepSeek-V4-Pro-NVFP4) are MIXED_PRECISION with "*.attn.*" in exclude_modules, so the fused A projection module is built unquantized (BF16) while the checkpoint stores q_a_proj/kv_a_proj_with_mqa as block-scale FP8. The non-NVFP4 branch of the fused_a loader then dereferences module.weight_scale (AttributeError) and would copy raw FP8 bytes into the BF16 weight even if guarded. Guard on hasattr(module, "weight_scale") and dequantize with the existing weight_dequant kernel instead. Fixes the loader half of NVIDIA#16196. Signed-off-by: WEI CHENG CHIU <waynehacking8@gmail.com>
…P4 DeepSeek-V4 The ModelOpt experts-only NVFP4 repacks (nvidia/DeepSeek-V4-Pro-NVFP4) are MIXED_PRECISION: only the routed experts are re-quantized to NVFP4, and the excluded projections (attention, shared experts, MTP e_proj/h_proj) are still stored as the FP8 base model's block-scale tensors, not BF16. Construction previously hit the "MIXED_PRECISION is ambiguous" assert, and building the excluded modules unquantized would break the MLA fused-q-quant contract at runtime (q_b_proj BF16 while _is_fused_q_fp8_quant_enabled is True). - _mixed_precision_base_quant_config(): derive FP8_BLOCK_SCALES from the checkpoint's embedded base quantization_config (quant_method=fp8 + weight_block_size) and build attention, shared experts and MTP e_proj/ h_proj with it, matching the native FP8 checkpoint flow. - _get_decoder_layer_quant_config(): under MIXED_PRECISION resolve the layer algo from the per-layer quantized_layers table (routed experts, NVFP4). - _get_experts_quant_config(): also accept the checkpoint-native key form (layers.N.ffn.experts) emitted by these repacks. Verified on a synthetic tiny replica of the checkpoint (exact dtypes and tensor name set): construction passes, weights load to 100%, fused_a fp8 payload is bit-exact vs the checkpoint and q_b_proj/shared experts end up FP8_BLOCK_SCALES with weight_scale present. MTP spec-decoding is out of scope (MTP routed experts are stored at the base model's MXFP4). Addresses the construction half of NVIDIA#16196. Signed-off-by: WEI CHENG CHIU <waynehacking8@gmail.com>
Contributor
📝 WalkthroughWalkthroughDeepSeek V4 mixed-precision handling now derives FP8 block-scale configurations, dequantizes fused-A weights for unquantized destinations, and applies consistent quantization settings to attention, shared-expert, and MTP projections. ChangesDeepSeek V4 mixed-precision quantization
Estimated code review effort: 4 (Complex) | ~45 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Addresses the CodeRabbit docstring-coverage warning on NVIDIA#16276. Signed-off-by: WEI CHENG CHIU <waynehacking8@gmail.com>
…MXFP4) The ModelOpt experts-only NVFP4 repacks (e.g. nvidia/DeepSeek-V4-Pro-NVFP4) re-quantize only the dense routed experts to NVFP4 (U8) and leave the MTP routed experts at the base model's MXFP4 (I8). _set_deepseek_v4_routed_moe_ quant_config detected a single layout from layers.0 and applied it to every MoE layer including the MTP layer, so the MTP experts got NVFP4 and crashed in fused_moe load_quant_scales. Detect the MTP expert dtype separately and assign the MTP layer indices the correct (MXFP4) config. Validated end-to-end on real weights: nvidia/DeepSeek-V4-Pro-NVFP4 on 4x B300, TP4, moe_backend=TRTLLM, rc15.post1 (equivalent change), with the construction fix from 3972f5e: loads 100% + serves + generates, MTP=1 and MTP=3 (mtp_eagle_one_model builds one shared MTP layer). MTP accept_len ~2.86. Signed-off-by: d3nb <wanxiren@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by CodeRabbit
Description
nvidia/DeepSeek-V4-Pro-NVFP4(the ModelOpt experts-only NVFP4 repack) cannot be loaded (#16196). The checkpoint isMIXED_PRECISION: only the routed experts are re-quantized to NVFP4, and the modules inexclude_modules(attention, shared experts, MTPe_proj/h_proj) are still stored at the FP8 base model's precision (fp8_e4m3 weight + ue8m0 block scale) -- not BF16. Two failures follow on main:DeepseekV4DecoderLayer's"MIXED_PRECISION is ambiguous"assert.module.weight_scale(AttributeError, the reported crash), and at runtime the MLA fused-FP8-q-quant contract breaks (_q_branchassert:q_b_projBF16 while_is_fused_q_fp8_quant_enabled()is True).This PR resolves both:
_mixed_precision_base_quant_config(): underMIXED_PRECISION, derive anFP8_BLOCK_SCALESconfig from the checkpoint's embedded basequantization_config(quant_method=fp8+weight_block_size) and build attention, shared experts, and MTPe_proj/h_projwith it -- matching how the native FP8 checkpoint flow builds them._get_decoder_layer_quant_config(): underMIXED_PRECISION, resolve the decoder-layer algo from the per-layerquantized_layerstable (routed experts, NVFP4), removing the ambiguity assert failure while keepingis_nvfp4semantics for the MoE._get_experts_quant_config(): also accept the checkpoint-native key formlayers.N.ffn.expertsemitted by these repacks (quant_config_dictkeys are stored verbatim, so the internal-form lookup was silently missing and returning the global config).hasattr(module, "weight_scale")and dequantize with the existingweight_dequantkernel as a defensive fallback for any excluded-but-FP8-stored module that is still built unquantized._set_deepseek_v4_routed_moe_quant_config(): detect the MTP routed-expert layout separately (commit authored by @d3nb, taken from Detect MTP routed-expert layout separately (dense NVFP4 vs MTP MXFP4) waynehacking8/TensorRT-LLM#1). These repacks re-quantize only the dense routed experts to NVFP4 (U8) and leave the MTP routed experts at the base model's MXFP4 (I8); deriving a single layout fromlayers.0assigned NVFP4 to the MTP layers too and crashed infused_moeload_quant_scales. The MTP layer indices now get their own config (MXFP4 viaget_mxfp4_quant_algo,group_size32), falling back to the dense config when the layouts match or the probe tensor is absent.MTP speculative decoding is now in scope: the last commit is @d3nb's per-region layout detection, merged from waynehacking8#1 with his authorship intact.
Fixes #16196.
Test Coverage
moe_config.backend=TRTLLM): validated by the issue reporter @d3nb on the actual 851 GBnvidia/DeepSeek-V4-Pro-NVFP4: weights load to 100%, server starts, chat completions generate correctly;q_b_proj/shared experts end up FP8 withweight_scalepresent and the previous_q_branchassert no longer fires. Details: [DeepSeek-V4] Loading nvidia/DeepSeek-V4-Pro-NVFP4 crashes: 'Linear' object has no attribute 'weight_scale' in fused_a loader (MIXED_PRECISION attn excluded → built BF16 but stored as FP8) #16196 (comment) (and the follow-up validation comment).hf_quant_config.jsonshape): construction passes, all weight groups load, the fused-A fp8 payload is bit-exact vs the checkpoint and the fused scales match exactly; decoder layers reportis_nvfp4=Truewith experts packed NVFP4.weight_dequanton fp8_e4m3 weights with ue8m0 scales) matches a blockwise reference exactly, including non-multiple-of-128 edge blocks.mtp_eagle_one_modelbuilds one shared MTP layer); MTP accept_len ~2.86.PR Checklist