Skip to content

Fix concat elimination crashing when a Concat input is a constant - #2838

Merged
gcunhase merged 5 commits into
NVIDIA:mainfrom
gcunhase:dev/gcunhasergio/fix_concat_elimination_constant_input
Oct 16, 2026
Merged

gcunhase merged 5 commits into
NVIDIA:mainfrom
gcunhase:dev/gcunhasergio/fix_concat_elimination_constant_input

Conversation

@gcunhase

@gcunhase gcunhase commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Type of change: Bug fix

get_concat_eliminated_tensors groups every input of a quantized Concat so the inputs share one
scaling factor. It also grouped constant (initializer) inputs. The calibrators only collect ranges
for activation tensors, so applying the group raised
KeyError: '<tensor>_ReduceMin' in _compute_data_minmax_calibrator.

A ViT hits this: constant folding leaves the class token (/Expand_output_0) as an initializer that
feeds Concat, and when the Concat output is quantized the default concat_elimination pass puts it
in a group. With --autotune the crash came after the whole search had finished.

The fix skips gs.Constant inputs when building the group. The constant keeps its own scale, so
TensorRT may keep one Q/DQ pair at that Concat that concat elimination would have removed.

Usage

python -m modelopt.onnx.quantization --onnx_path model.onnx --quantize_mode fp8 --autotune default

Testing

  • New unit test tests/unit/onnx/quantization/test_concat_elimination.py
  • Real model: ViT-tiny (timm), FP8 --autotune default, H100, TensorRT 10.16.2.11. Before: crash
    after 2.0 h. After: completes in 2.0 h (ModelOpt keeps the FP16 model, since FP8 measured 0.895x).
import timm
import torch

import modelopt.onnx.quantization as moq

model = timm.create_model("vit_tiny_patch16_224", pretrained=False).eval()
torch.onnx.export(
    model, torch.randn(1, 3, 224, 224), "vit_tiny.onnx", input_names=["input"], opset_version=17
)
moq.quantize("vit_tiny.onnx", quantize_mode="fp8", autotune=True)

Before your PR is "Ready for review"

  • Is this change backward compatible?: ✅
  • If you copied code from any other sources or added a new PIP dependency, did you follow guidance in CONTRIBUTING.md: N/A
  • Did you write any new necessary tests?: ✅
  • Did you update Changelog?: ✅
  • Did you get Claude approval on this PR?: ❌ (not run yet)

Additional Information

None.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • ONNX calibration no longer crashes when a concatenation includes a constant input.
    • Constant inputs are excluded from shared-scale grouping, while eligible activation inputs continue to be grouped.
  • Documentation
    • Updated the changelog with release highlights, including GGML imatrix calibration, a Nemotron 3.5 Super VL AutoQuantize recipe, and external-draft speculative decoding.

gcunhase and others added 5 commits October 16, 2026 09:22
get_concat_eliminated_tensors put every Concat input whose consumers are
quantized into the shared-scale group, including initializers. The
calibrators only collect ranges for activation tensors, so applying the
group raised KeyError ('/Expand_output_0_ReduceMin') for a ViT whose
constant-folded class token feeds a quantized Concat, and the run failed
after autotune had finished.

Skip gs.Constant inputs when building the group, and add a unit test that
fails without the change.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: gcunhase <[email protected]>
…lim.py"

This reverts commit 954b136.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: gcunhase <[email protected]>
…t_elim.py

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: gcunhase <[email protected]>
Move the test graph builder to _test_utils/onnx/lib_test_models.py,
name the unit test file test_concat_elimination.py, and note the fix
in the changelog.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: gcunhase <[email protected]>
@gcunhase
gcunhase requested review from a team as code owners October 16, 2026 16:12
@gcunhase
gcunhase requested a review from ajrasane October 16, 2026 16:12
@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/Model-Optimizer/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 9aa5b684-0d98-4d69-88c1-ecd6b8655a60
📥 Commits

Reviewing files that changed from the base of the PR and between 35c4142 and 732125c.

📒 Files selected for processing: 4
  • CHANGELOG.rst
  • modelopt/onnx/quantization/qdq_graph.py
  • tests/_test_utils/onnx/lib_test_models.py
  • tests/unit/onnx/quantization/test_concat_elimination.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

📝 Walkthrough

The Concat quantization grouping logic now skips constant inputs. Added tests cover both constant and non-constant Concat inputs. The changelog records the calibration crash fix.

Changes: Concat quantization grouping

Layer / File(s) Summary
Skip constants during Concat grouping
modelopt/onnx/quantization/qdq_graph.py, tests/_test_utils/onnx/lib_test_models.py, tests/unit/onnx/quantization/test_concat_elimination.py, CHANGELOG.rst
The Concat input scan skips constants when building quantized tensor groups. Tests cover grouping with constant and non-constant inputs. The changelog records the calibration fix.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 73212

No actionable merge-blocking risk is identified; the change is ready for normal merge checks.

Pre-merge checks: Passed 5 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 5 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the primary change: preventing concat elimination from crashing when a Concat input is a constant.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Security Anti-Patterns Passed No specified security anti-pattern was introduced. The authoritative PR diff changes one modelopt Python file and test helpers only; added code contains no torch.load(..., weights_only=False), numpy.l…
Full details: Security Anti-Patterns

Explanation

No specified security anti-pattern was introduced. The authoritative PR diff changes one modelopt Python file and test helpers only; added code contains no torch.load(..., weights_only=False), numpy.load(..., allow_pickle=True), hardcoded trust_remote_code=True, external-input eval/exec, or # nosec. The PR changes no pyproject.toml or requirements files. SECURITY.md confirms these are the relevant prohibited patterns.

Full details: Docstring Coverage

Explanation

Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@cjluo-nv cjluo-nv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bot review (bedrock-claude-opus-5-5) — DM the bot to share feedback.

Approving: this is a small, correct fix. It stops initializer Concat inputs from joining a concat-elimination scale group, and new tests cover both the constant and the activation case.

No action needed.

No action needed:

  • The isinstance(input_tensor, gs.Constant) check is narrower than the existing is_const_input helper on purpose. That helper also flags Identity/Constant-node outputs, which are runtime tensors the calibrator can still measure. So the narrower check doesn't change grouping for anything except initializers.
  • If a Constant-op output (not an initializer) feeds a Concat, it would still be grouped. That's outside this bug's scope (ViT's class token is folded into an initializer), but worth remembering if a similar KeyError shows up again.
  • The new test file's header matches LICENSE_HEADER. The fixture in lib_test_models.py adds new code only and doesn't change existing models.
  • I read the tests but didn't run them.

@gcunhase
gcunhase enabled auto-merge (squash) October 16, 2026 16:19
@codecov

codecov Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.65%. Comparing base (98c10fc) to head (732125c).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2838      +/-   ##
==========================================
+ Coverage   69.57%   78.65%   +9.07%     
==========================================
  Files         656      656              
  Lines       73018    73090      +72     
==========================================
+ Hits        50805    57490    +6685     
+ Misses      22213    15600    -6613     
Flag Coverage Δ
examples-diffusers 20.19% <0.00%> (+0.03%) ⬆️
examples-gpt-oss 13.56% <0.00%> (+0.01%) ⬆️
examples-hf_ptq 23.35% <0.00%> (+0.03%) ⬆️
examples-llm_distill 13.64% <0.00%> (+0.01%) ⬆️
examples-llm_eval 17.34% <0.00%> (+0.03%) ⬆️
examples-llm_qat 17.59% <0.00%> (+0.03%) ⬆️
examples-llm_sparsity 15.85% <0.00%> (+<0.01%) ⬆️
examples-megatron_bridge 26.62% <0.00%> (-0.15%) ⬇️
examples-specdec_bench 13.34% <0.00%> (+0.01%) ⬆️
examples-speculative_decoding 17.58% <0.00%> (-0.02%) ⬇️
examples-torch_onnx 21.56% <0.00%> (+<0.01%) ⬆️
examples-torch_trt 15.31% <0.00%> (+0.01%) ⬆️
examples-vllm_serve 15.05% <0.00%> (+0.01%) ⬆️
gpu 59.57% <50.00%> (+37.63%) ⬆️
regression 15.17% <0.00%> (+0.03%) ⬆️
unit 60.04% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gcunhase
gcunhase merged commit 97a4106 into NVIDIA:main Oct 16, 2026
63 of 65 checks passed
@rohansjoshi rohansjoshi added the cherry-pick-done Added by bot once PR is cherry-picked to the release branch label Oct 16, 2026
rohansjoshi added a commit that referenced this pull request Oct 17, 2026
#2769 #2770 #2719 #2796 #2801 #2794 #2787 #2682 #2793 #2791 #2792 #2766 #2823 #2777 #2773 #2813 #2832 #2795 #2684 #2822 #2838 #2807 #2772 #2837 #2820 (#2843)

## Cherry-picked PRs

- #2647
- #2700
- #2718
- #2578
- #2580
- #2711
- #2731
- #2726
- #2697
- #2769
- #2770
- #2719
- #2796
- #2801
- #2794
- #2787
- #2682
- #2793
- #2791
- #2792
- #2766
- #2823
- #2777
- #2773
- #2813
- #2832
- #2795
- #2684
- #2822
- #2838
- #2807
- #2772
- #2837
- #2820

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Expanded quantized model export support for MXFP4, MXFP8, FP8, and
additional block-quantization formats.
* Added quantization support for GPT-OSS and Step-family models, plus
export support for native FP8 experts.
* Added chat JSONL data preparation for Megatron workflows and an option
to freeze the student MTP head during distillation.
* Added model recipes for Kimi, Gemma, DiffusionGemma, and GLM variants.
* **Bug Fixes**
* Improved ONNX quantization and export compatibility, distributed
calibration consistency, and handling of packed attention caches.
  * Improved safe model loading and low-precision quantization accuracy.
* **Documentation**
* Updated the 0.48.0 changelog and guidance for data preparation and
quantization recipes.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Also includes a partial backport of #2656 (only the
`_validate_fold_weight` hook and the `mtq.fold_weight` pre-check), which
#2820 depends on.

---------

Signed-off-by: Shengliang Xu <[email protected]>
Signed-off-by: Rohan Joshi <[email protected]>
Signed-off-by: Juhi Mittal <[email protected]>
Signed-off-by: Fridah-nv <[email protected]>
Signed-off-by: Chenjie Luo <[email protected]>
Signed-off-by: yingguo-trt <[email protected]>
Signed-off-by: weimingc <[email protected]>
Signed-off-by: Yue <[email protected]>
Signed-off-by: joe-nv <[email protected]>
Signed-off-by: ajrasane <[email protected]>
Signed-off-by: Ajinkya Rasane <[email protected]>
Signed-off-by: ColinM-sys <[email protected]>
Signed-off-by: Keval Morabia <[email protected]>
Signed-off-by: Meng Xin <[email protected]>
Signed-off-by: gcunhase <[email protected]>
Signed-off-by: Chad Voegele <[email protected]>
Co-authored-by: Shengliang Xu <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Juhi Mittal <[email protected]>
Co-authored-by: Frida Hou <[email protected]>
Co-authored-by: Chenjie Luo <[email protected]>
Co-authored-by: yingguo-trt <[email protected]>
Co-authored-by: Wei-Ming Chen <[email protected]>
Co-authored-by: yueshen2016 <[email protected]>
Co-authored-by: joe <[email protected]>
Co-authored-by: Ajinkya Rasane <[email protected]>
Co-authored-by: Colin McDonough <[email protected]>
Co-authored-by: Keval Morabia <[email protected]>
Co-authored-by: mxinO <[email protected]>
Co-authored-by: Gwena Cunha <[email protected]>
Co-authored-by: Chad Voegele <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-pick-0.48.0 cherry-pick-done Added by bot once PR is cherry-picked to the release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants