Fix non-scalar input amax in preprocess_linear_fusion for MoE export#1264
Open
AEON-7 wants to merge 1 commit intoNVIDIA:mainfrom
Open
Fix non-scalar input amax in preprocess_linear_fusion for MoE export#1264AEON-7 wants to merge 1 commit intoNVIDIA:mainfrom
AEON-7 wants to merge 1 commit intoNVIDIA:mainfrom
Conversation
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
preprocess_linear_fusion unconditionally asserts `modules[0].input_quantizer.amax.numel() == 1`, which breaks for NVFP4 quantization when the model has per-expert-decomposed MoE linears (gate_proj/up_proj pairs per expert). NVFP4's per-channel input quantizer produces a vector amax, not a scalar, so the assertion trips immediately on the first expert during `export_hf_checkpoint()`. Root cause: the function was written assuming fused linears have per-tensor scalar input amax. That's true for dense FP8/INT8 paths but false for NVFP4's per-channel activation statistics, which modelopt's own NVFP4_AWQ_FULL_CFG produces. This change: - Keeps the existing scalar-amax path (dense + FP8/INT8 unchanged) - Adds a non-scalar path using elementwise max (`.amax(dim=0)`) across the stacked per-channel amax tensors of the modules being fused Numerical correctness for the MoE case: the modules being fused here (e.g. gate_proj and up_proj of one expert) consume the *same* input tensor by construction, so their per-channel input amax tensors are identical. Elementwise max is therefore a no-op, and is the correct unification rule if they ever differ due to floating-point accumulation. Validated end-to-end on SuperGemma4 26B (128-expert MoE) with NVFP4_AWQ_FULL_CFG; export now completes and the serialized checkpoint loads + generates correctly. Before: export failed with `AssertionError: Only support scalar input quant amax` after 2h 24min of successful calibration. Signed-off-by: AEON-7 <m2vgz48wpp@privaterelay.appleid.com>
3fcc5a7 to
6929ecb
Compare
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.
What
modelopt/torch/export/quant_utils.py::preprocess_linear_fusionunconditionally asserts:This breaks NVFP4 quantization for models whose MoE experts are decomposed into per-expert
gate_proj/up_proj/down_projnn.Linearmodules (the standard pattern for HuggingFace-compatible export). NVFP4's activation quantizer is per-channel, soinput_quantizer.amaxis a vector — not a scalar — and the assertion trips immediately on the first expert duringexport_hf_checkpoint().How to reproduce
Any MoE model where the modelopt plugin decomposes fused-expert storage (e.g. Gemma 4's
[E, 2I, H]+[E, H, I]) into per-expertnn.Linearmodules, quantized withNVFP4_AWQ_FULL_CFG. On SuperGemma4 26B (128 experts, 30 layers) the assertion fires during therequantize_resmooth_fused_llm_layerspass, immediately after 2h 24min of successful calibration — calibration state is in-memory only, so all work is lost.The fix
Branch on
amax.numel() == 1:torch.max(torch.stack(...))— behaviour identical to today for dense FP8/INT8.torch.stack(...).amax(dim=0)— elementwise max across the stacked per-channel amax tensors.Why this is numerically safe
The modules being fused here (e.g.
gate_projandup_projof a single expert) consume the same input tensor by construction —modelopt.torch.export.unified_export_hf._fuse_shared_input_modulesgroups them precisely because they share an input. Their per-channel input amax tensors are therefore identical (up to float accumulation noise), and elementwise max is a no-op. If they ever differ for numerical reasons, elementwise max is the correct unification rule — exactly analogous to the scalar max the existing code uses.The scalar path is untouched, so dense models and FP8/INT8 MoE paths are unchanged.
Validation
End-to-end on SuperGemma4 26B (Gemma 4 MoE, 128 experts, per-expert-decomposed plugin) with
NVFP4_AWQ_FULL_CFG:AssertionError: Only support scalar input quant amaxon first expert during export, after 2h 24min of successful calibration.preprocess_linear_fusioncompletes; export produces a valid NVFP4 checkpoint that loads + generates coherent output.The resulting quantized model ships at AEON-7/supergemma4-26b-abliterated-multimodal-nvfp4.
Follow-up
A companion fix for
NVFP4QTensor.get_activation_scaling_factor(handling zero-amax channels from MoE routing sparsity) is coming in a separate PR — it's an orthogonal bug on the same export path.Summary by CodeRabbit