Repository navigation
Keep nn.LayerNorm inputs unquantized in the default PTQ presets - #2814
Merged
kevalmorabia97 merged 1 commit intoOct 15, 2026
Merged
kevalmorabia97 merged 1 commit into
kevalmorabia97 merged 1 commit into
Conversation
nn.LayerNorm became a quant module in 0.44 so that recipes can opt into its output quantizer for FP8 MHA. As a side effect, the bare "*input_quantizer" wildcard also enables its input quantizer, so every preset or recipe that imports default_disabled_quantizers and enables "*input_quantizer" quantizes the residual stream entering each LayerNorm. Disable the LayerNorm quantizers in default_disabled_quantizers, as for BatchNorm and Embedding; a recipe that wants them enables them after importing the unit. Signed-off-by: Hanjun Cho <gkswns0531@gmail.com>
Contributor
Collaborator
|
/ok to test 294e8e4 |
cjluo-nv
reviewed
Oct 15, 2026
cjluo-nv
left a comment
Collaborator
There was a problem hiding this comment.
Bot review (gpt-6.1-sol) — DM the bot to share feedback.
LGTM: the exclusion correctly follows preset enables and preserves the FP8 MHA override; human sign-off is needed for the justified test-file extension.
Needs action:
- Confirm the additive regression coverage in
tests/unit/torch/quantization/test_quant_layernorm.py; the existing identity test and assertions remain unchanged.
No action needed:
- The three preset regressions check that Linear inputs remain quantized while LayerNorm inputs are disabled.
- Tests were inspected, not executed.
kevalmorabia97
approved these changes
Oct 15, 2026
kevalmorabia97
enabled auto-merge (squash)
October 15, 2026 08:07
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2814 +/- ##
==========================================
+ Coverage 69.52% 78.32% +8.79%
==========================================
Files 649 649
Lines 72081 72286 +205
==========================================
+ Hits 50114 56616 +6502
+ Misses 21967 15670 -6297
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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 does this PR do?
Type of change: Bug fix
#1289 (in 0.44.0 through #1350) registered
nn.LayerNorminQuantModuleRegistryso that FP8 MHA recipes can quantize its output. As a side effect, presets and recipes that importconfigs/ptq/units/default_disabled_quantizersand enable a bare*input_quantizer(INT8_DEFAULT_CFG,FP8_DEFAULT_CFG,NVFP4_DEFAULT_CFG, ...) also quantize every LayerNorm input, i.e. the residual stream (49 extra NVFP4 quantizers in XLM-R large). In ONNX export this becomes a dynamic quantize in front ofLayerNormalizationthat no GEMM consumes. Weight-only and layer-scoped presets are not affected.This adds a
parent_class: 'nn.LayerNorm'exclusion todefault_disabled_quantizers, in the same form as the existingnn.BatchNorm*,nn.LeakyReLUandnn.Embeddingentries. LayerNorm quantization stays opt-in: the FP8 MHA override inexamples/torch_onnx/torch_quant_to_onnx.py, appended after the preset, enables the LayerNorm output quantizer and already disables the input quantizer, so its result is unchanged. As with the BatchNorm entries, annn.LayerNormsubclass registered as its own quant class is not covered.#2402 fixed the same regression for the diffusers NVFP4 presets with the same entry; those presets do not import this unit. Out of scope:
NVFP4_FP8_MHA_CONFIG,model_type/vit/ptq/fp8.yamland the diffusersfp8,int8andnvfp4_fp8_convpresets also skip this unit and still quantize LayerNorm inputs.Effect on
BAAI/bge-reranker-v2-m3(XLM-R large), NVFP4 W4A4 fake quant (ModelOpt 0.47.0, same as main here), 60 queries × 100 candidates each:Usage
No API change. A config that wants LayerNorm input quantization adds the entry after the default exclusions:
Testing
test_default_presets_keep_layernorm_inputs_unquantized(INT8_DEFAULT_CFG,FP8_DEFAULT_CFG,NVFP4_DEFAULT_CFG): fails on main, passes here.pytest tests/unit/torch/quantization tests/unit/recipe: 1912 passed, 1 skipped.pytest tests/examples/torch_onnx/test_torch_quant_to_onnx.py -k "vit_tiny or swin"(RTX PRO 6000, TensorRT 11.3): the 12 FP8, INT8, MXFP8 and auto cases pass. The 3 NVFP4 cases fail the same way on main, in the FP32 NVFP4 export that [6721556] Fix mixed-dtype NVFP4 ONNX precision conversion #2415 fixes, and pass with a local fix for it.pre-commit run --fileson the changed files: pass.bge-reranker-v2-m3withNVFP4_DEFAULT_CFGhas 0 LayerNorm input quantizers enabled, and its TensorRT engine matches fake quant (Recall@10 89.8% for both) with the [6721556] Fix mixed-dtype NVFP4 ONNX precision conversion #2415 export fix applied locally.Before your PR is "Ready for review"
Make sure you read and follow Contributor guidelines and your commits are signed (
git commit -s -S).Make sure you read and follow the Security Best Practices (e.g. avoiding hardcoded
trust_remote_code=True,torch.load(..., weights_only=False),pickle, etc.).mtq.quantizecalls with these presets no longer add LayerNorm input quantizers, as before 0.44. Saved checkpoints restore unchanged, because restore replays their stored config. Usage shows how to re-enable them.CONTRIBUTING.md: N/AAdditional Information
Found while quantizing
BAAI/bge-reranker-v2-m3to NVFP4; its TensorRT engine also needs the FP32 NVFP4 export fix in #2415.Summary by CodeRabbit
LayerNormmodules, avoiding added error in residual streams.LayerNorminput quantization can enable it after importing the relevant preset.