Skip to content

[OMNIML-5613] Quantize ResNet residual adds in torch ONNX example - #2024

Open
ajrasane wants to merge 11 commits into
mainfrom
ajrasane/quantize-resnet-residual-adds
Open

[OMNIML-5613] Quantize ResNet residual adds in torch ONNX example#2024
ajrasane wants to merge 11 commits into
mainfrom
ajrasane/quantize-resnet-residual-adds

Conversation

@ajrasane

@ajrasane ajrasane commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Type of change: Bug fix

Adds recipe-backed residual quantization for timm ResNet models in the torch ONNX example:

  • Adds FP8 and INT8 PTQ recipes that enable a shortcut quantizer immediately before each residual Add.
  • Adds an AutoQuantize recipe that searches FP8 and INT8 at eight effective bits while keeping residual shortcuts in FP8.
  • Inserts shortcut quantizers after ModelOpt module conversion so the recipes configure and calibrate them in the normal quantization flow.
  • Verifies all 16 ResNet-50 residual additions have shortcut Q/DQ immediately before the Add.

Usage

python examples/torch_onnx/torch_quant_to_onnx.py \
    --timm_model_name=resnet50 \
    --quantize_mode=<fp8|int8|auto> \
    --onnx_save_path=resnet50.onnx

Testing

  • All configured pre-commit hooks passed.
  • Recipe loader unit tests: 206 passed.
  • Exported ResNet-50 in FP8, INT8, and AutoQuantize modes.
  • ONNX checker passed for all three exports.
  • Verified 16 residual additions and 16 shortcut Q/DQ pairs in each exported graph.
  • TensorRT engine builds passed for FP8 and INT8 on Ada. AutoQuantize mixes INT8 and FP8, which TensorRT supports only on Blackwell and newer platforms.

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 the changelog?: ✅

🤖 Generated by Codex (AI agent).

ajrasane added 2 commits July 28, 2026 17:24
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The PR adds timm ResNet quantization plugins and PTQ recipes, integrates recipe selection into the Torch ONNX example, extends AutoQuantize matching and cost handling, and updates ONNX FP8/INT8 FP16 transformations with structural validation.

Changes

timm ResNet quantization and export

Layer / File(s) Summary
timm ResNet quantization plugin
modelopt/torch/quantization/plugins/*
Supported ResNet modules are rewritten with quantization-aware marker classes, residual and output hooks, and AutoQuantize grouping/scoring rules.
ResNet recipes and example wiring
modelopt_recipes/timm/resnet/ptq/*, examples/torch_onnx/torch_quant_to_onnx.py, modelopt_recipes/README.md, examples/torch_onnx/README.md, CHANGELOG.rst
PTQ recipes for FP8, INT8, MXFP8, NVFP4, and AWQ-lite are added; the ONNX example selects model-specific recipes and defaults; documentation describes the new recipe layout and quantizer placement.
AutoQuantize matching and cost reconstruction
modelopt/torch/quantization/algorithms.py, modelopt/torch/quantization/conversion.py, modelopt/torch/opt/dynamic.py
Quantizer attributes, parent classes, selected-format compression, and replay matching are persisted and evaluated for quantizable modules and registered subclasses.
ONNX conversion and graph validation
modelopt/onnx/*, modelopt/torch/_deploy/utils/torch_onnx.py, tests/examples/torch_onnx/*, tests/unit/onnx/*
FP8 RGB input-stem handling and INT8 FP16 Q/DQ conversion are added, with tests covering graph structure, dtype checks, opsets, and invalid scales.

Estimated code review effort: 4 (Complex) | ~60 minutes

Suggested reviewers: vishalpandya1990, cjluo-nv

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.32% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
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 forbidden patterns found in the full PR diff; changed Python files had no unsafe torch/numpy loads, trust_remote_code, builtin eval/exec, or nosec, and no dependency manifests changed.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main ResNet ONNX quantization change, even though the PR also includes supporting recipes, docs, and tests.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ajrasane/quantize-resnet-residual-adds

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

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.20%. Comparing base (a23390d) to head (4959049).

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2024       +/-   ##
===========================================
+ Coverage   60.23%   77.20%   +16.96%     
===========================================
  Files         519      520        +1     
  Lines       59399    59556      +157     
===========================================
+ Hits        35778    45979    +10201     
+ Misses      23621    13577    -10044     
Flag Coverage Δ
examples 40.18% <ø> (+9.07%) ⬆️
unit 55.16% <ø> (ø)

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.

@ajrasane
ajrasane marked this pull request as ready for review July 28, 2026 18:03
@ajrasane
ajrasane requested review from a team as code owners July 28, 2026 18:03
@ajrasane ajrasane self-assigned this Jul 28, 2026
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>

@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) — DM the bot to share feedback.

Small, focused change (+90/-2) that adds per-block residual-add quantizers to the timm ResNet path of the torch→ONNX example, with CHANGELOG + README updates and an ONNX-level assertion in the existing example test. No licensing concerns; no prompt-injection content in the PR metadata. A few things worth addressing before merge:

  1. Duplicated calibration logic / extra data pass. _add_resnet_residual_quantizers re-implements the enable_calib → forward-loop → load_calib_amax dance that _calibrate_uncalibrated_quantizers (same file) already performs, and adds a second full pass over the calibration set for ResNet (the first happens inside quantize_model for the FP8 Conv overrides). If the residual quantizer were attached as input_quantizer on the activation module and created before quantize_model, both the calibration and the dead-quantizer guard would come for free from the existing helpers. Even more idiomatic: modelopt already supports this via QuantModuleRegistry.register({nn.ReLU: "nn.ReLU"})(QuantInputBase) (exactly what modelopt/torch/quantization/nn/modules/quant_activations.py does for nn.LeakyReLU) plus a {"parent_class": "nn.ReLU", "quantizer_name": "*input_quantizer"} config entry, which gets calibrated by mtq.quantize's forward loop and is visible to mtq.print_quant_summary / modelopt state. Please either reuse one of these paths or note in the PR/comment why the manual hook is needed.

  2. Residual quantizers bypass the file's own amax<=0/NaN guard, and load_calib_amax() is strict. _disable_dead_quantizers only inspects input_quantizer/output_quantizer/weight_quantizer, and it runs inside quantize_model — i.e. before these quantizers exist. A residual quantizer that calibrates to amax == 0 (or NaN) will therefore reach the FP8 exporter, which is precisely the scale = 448 / amax division the guard exists to prevent. Also, unlike _calibrate_uncalibrated_quantizers, this code calls load_calib_amax() without strict=False, so any block that didn't see data raises.

  3. auto mode picks the residual format from the search space, not the search result. After mtq.auto_quantize, each block's actual format is known (e.g. block.conv3.input_quantizer._num_bits); deriving num_bits from the union of requested formats can give an FP8 residual Q/DQ next to an INT8-quantized block (or the reverse), which is what the rest of this file goes to some length to avoid for TRT.

Minor: the two new functions are the only helpers in this file without docstrings; and assert len(residual_adds) == 16 asserts that every Add in the exported graph is a residual add, which will break confusingly if the exporter ever emits an unrelated Add — consider filtering to the 16 residual adds (e.g. by producer/consumer pattern) before the count assertion.

Comment thread examples/torch_onnx/torch_quant_to_onnx.py Outdated
Comment thread examples/torch_onnx/torch_quant_to_onnx.py Outdated
Comment thread examples/torch_onnx/torch_quant_to_onnx.py Outdated
Comment thread tests/examples/torch_onnx/test_torch_quant_to_onnx.py
Comment thread examples/torch_onnx/torch_quant_to_onnx.py Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.

Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.

👉 Steps to fix this

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/torch_onnx/torch_quant_to_onnx.py`:
- Around line 223-239: Update _add_resnet_residual_quantizers and the
surrounding auto-quantization flow so residual quantizers are installed and
configured before mtq.auto_quantize() runs. For auto mode, include these
residual quantizer modules in every candidate format configuration used by the
search, ensuring their forced INT8/FP8 precision is scored and counted toward
the effective-bits constraint while preserving the existing non-auto behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 9cd948d5-f05e-4d18-9714-517039e16f2d

📥 Commits

Reviewing files that changed from the base of the PR and between 87c9f8c and c15755c.

📒 Files selected for processing (4)
  • CHANGELOG.rst
  • examples/torch_onnx/README.md
  • examples/torch_onnx/torch_quant_to_onnx.py
  • tests/examples/torch_onnx/test_torch_quant_to_onnx.py

Comment thread examples/torch_onnx/torch_quant_to_onnx.py Outdated
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
examples/torch_onnx/torch_quant_to_onnx.py (1)

636-642: ⚠️ Potential issue | 🟠 Major

Install residual quantizers before the quantization/search pass.

At Line 636, residual quantizers are added only after quantized_model has been created. In auto mode, they are therefore absent from candidate scoring and effective-bits constraints; the later heuristic can also choose a format different from the per-block format selected by AutoQuantize. The standard path additionally requires a second calibration pass and runs dead-quantizer cleanup before these modules exist.

Move installation/configuration before quantization, or explicitly integrate these quantizers into AutoQuantize and rerun cleanup after calibration.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/torch_onnx/torch_quant_to_onnx.py` around lines 636 - 642, Move the
`_add_resnet_residual_quantizers` installation and configuration before the
quantization/search pass creates `quantized_model`, so residual quantizers
participate in AutoQuantize candidate scoring and effective-bits constraints.
Ensure calibration and dead-quantizer cleanup operate on these modules, and
remove the current post-quantization-only installation path.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@examples/torch_onnx/torch_quant_to_onnx.py`:
- Around line 636-642: Move the `_add_resnet_residual_quantizers` installation
and configuration before the quantization/search pass creates `quantized_model`,
so residual quantizers participate in AutoQuantize candidate scoring and
effective-bits constraints. Ensure calibration and dead-quantizer cleanup
operate on these modules, and remove the current post-quantization-only
installation path.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 0ba0d581-0d68-4e4d-993c-47c5819b72dd

📥 Commits

Reviewing files that changed from the base of the PR and between c15755c and d2a78de.

📒 Files selected for processing (4)
  • CHANGELOG.rst
  • examples/torch_onnx/README.md
  • examples/torch_onnx/torch_quant_to_onnx.py
  • tests/examples/torch_onnx/test_torch_quant_to_onnx.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • examples/torch_onnx/README.md
  • CHANGELOG.rst
  • tests/examples/torch_onnx/test_torch_quant_to_onnx.py

Co-Authored-By: Codex <noreply@openai.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
@ajrasane
ajrasane requested a review from a team as a code owner July 29, 2026 16:04
@ajrasane
ajrasane requested a review from cjluo-nv July 29, 2026 16:04
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://NVIDIA.github.io/Model-Optimizer/pr-preview/pr-2024/

Built to branch gh-pages at 2026-07-30 00:56 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.

Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.

👉 Steps to fix this

Actionable comments posted: 4

🧹 Nitpick comments (5)
tests/unit/onnx/test_fold_casts.py (1)

86-97: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Redundant dtype plumbing in _initializer_q_model.

input_dtype and output_dtype are computed identically, and outputs[0] is built with FLOAT16 at Line 86 only to be overwritten at Line 97. Collapse into a single variable used at construction time.

♻️ Proposed simplification
-    outputs = [helper.make_tensor_value_info("y", TensorProto.FLOAT16, [None, 4])]
+    float_dtype = TensorProto.FLOAT16 if opset >= 19 else TensorProto.FLOAT
+    outputs = [helper.make_tensor_value_info("y", float_dtype, [None, 4])]
     if shared:
         nodes.append(helper.make_node("Identity", ["w"], ["w_out"], "identity"))
         outputs.append(helper.make_tensor_value_info("w_out", TensorProto.FLOAT, [4, 4]))
@@
-    input_dtype = TensorProto.FLOAT16 if opset >= 19 else TensorProto.FLOAT
-    output_dtype = TensorProto.FLOAT16 if opset >= 19 else TensorProto.FLOAT
-    outputs[0].type.tensor_type.elem_type = output_dtype
     return helper.make_model(
         helper.make_graph(
             nodes,
             "g",
-            [helper.make_tensor_value_info("x", input_dtype, [None, 4])],
+            [helper.make_tensor_value_info("x", float_dtype, [None, 4])],
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/onnx/test_fold_casts.py` around lines 86 - 97, In
`_initializer_q_model`, replace the redundant `input_dtype` and `output_dtype`
calculations with one shared dtype variable derived from `opset`, and use it
when constructing the primary `y` output instead of creating it as `FLOAT16` and
mutating it afterward. Remove the subsequent
`outputs[0].type.tensor_type.elem_type` assignment while preserving the existing
opset-dependent dtype.
examples/torch_onnx/torch_quant_to_onnx.py (1)

232-259: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low value

_calibrate_new_quantizers mutates _if_quant state without honoring pre-existing disabled-quant quantizers.

enabled_quantizers is filtered by is_enabled (i.e. _disabled), not by _if_quant. Any quantizer that was enabled but intentionally had disable_quant() applied earlier gets enable_quant() in the finally block, silently turning quantization back on. Filtering on module._if_quant instead would make the save/restore symmetric.

♻️ Suggested tweak
-    enabled_quantizers = [
-        module
-        for module in model.modules()
-        if isinstance(module, TensorQuantizer) and module.is_enabled
-    ]
+    enabled_quantizers = [
+        module
+        for module in model.modules()
+        if isinstance(module, TensorQuantizer) and module.is_enabled and module._if_quant
+    ]
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/torch_onnx/torch_quant_to_onnx.py` around lines 232 - 259, Update
_calibrate_new_quantizers to track quantizers based on their pre-existing
_if_quant state rather than is_enabled when building enabled_quantizers. Restore
quantization only for quantizers whose _if_quant state was originally active,
preserving intentionally disabled quantizers through the finally block.
tests/examples/torch_onnx/test_torch_quant_to_onnx.py (2)

35-62: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Unguarded producers[...] lookups will raise KeyError instead of a useful failure.

If an Add input is a graph input or an initializer (no producer node), Line 54/56 raises KeyError rather than an assertion explaining what the graph looks like. Same for the next(...) lookups at Lines 88 and 95, which raise StopIteration if the exporter ever emits MatMul/ReduceMean instead of Gemm/GlobalAveragePool. Prefer producers.get(...) plus explicit asserts so failures are diagnosable.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/examples/torch_onnx/test_torch_quant_to_onnx.py` around lines 35 - 62,
Harden _assert_residual_adds_are_quantized against missing graph producers by
replacing direct producers[...] accesses with producers.get(...) and explicit
assertions that identify the missing input or node. Apply the same pattern to
the MatMul/ReduceMean lookup paths around the next(...) calls, asserting the
expected producer exists before dereferencing it while preserving the current
validation behavior.

64-116: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Hard-coded node counts (54/52, 53, 12/4/4) need a comment explaining their derivation.

These numbers encode the exact ResNet-50 Q/DQ topology, but nothing in the test says where they come from, so a future exporter change produces an unexplainable assert 53 == 52. A one-line comment per assertion (e.g. "53 = 53 Conv weights, fc weight quantizer disabled") would make the failures actionable. As per path instructions, checked-in tests should "document expected behavior".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/examples/torch_onnx/test_torch_quant_to_onnx.py` around lines 64 - 116,
Add concise comments immediately before each hard-coded topology-count assertion
in the quantization test, explaining how the expected values derive from the
ResNet-50 Q/DQ structure and mode-specific behavior. Cover the activation
quantizer counts, DQ fanout counts, and int8 weight quantizer count, including
details such as the number of convolution weights and disabled fully connected
weight quantization; leave the assertions unchanged.

Source: Path instructions

modelopt/torch/_deploy/utils/torch_onnx.py (1)

48-48: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Importing a private helper across modules.

_convert_q_data_initializers_to_fp16 is underscore-private to modelopt/onnx/utils.py but is consumed here. Since it's now part of the export pipeline contract, consider promoting it to a public name (and adding it to that module's __all__) so the dependency is explicit.

Note the sequencing is load-bearing: this call raises if any Q scale is still FP32 after Line 670, which is why the stricter fold guard in modelopt/onnx/utils.py matters (flagged there).

Also applies to: 669-673

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@modelopt/torch/_deploy/utils/torch_onnx.py` at line 48, Promote
_convert_q_data_initializers_to_fp16 in modelopt/onnx/utils.py to a public
helper name, add that name to the module’s __all__, and update the import and
call sites in the ONNX export flow around _convert_q_data_initializers_to_fp16
accordingly. Preserve the existing sequencing and validation behavior after the
fold guard.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/torch_onnx/torch_quant_to_onnx.py`:
- Around line 363-392: Update _finalize_resnet_quantizers so the int8/fp8 path
only accesses quantizer attributes when they exist, using safe getattr checks
for block.conv1, model.fc, model.global_pool, and related quantizers. Preserve
enabling/disabling behavior for present quantizers, and avoid AttributeError
when _prepare_resnet_quantizers skipped setup or a block variant lacks these
attributes.

In `@modelopt/onnx/export/fp8_exporter.py`:
- Line 34: Update the weight-channel threshold logic near the FP8
minimum-channel constant and the relevant convolution export check to account
for the convolution’s group count, matching the torch-side gate. Base the
decision on the effective total input channels rather than per-group
weight_input.values.shape[1], so grouped and depthwise convolutions consistently
enable or skip activation and weight Q/DQ paths.

In `@modelopt/onnx/utils.py`:
- Around line 1471-1481: The Cast-folding condition in the loop over
onnx_model.graph.node should not reject Cast-to-FLOAT nodes solely because
tensor_types lacks node.input[0]. Treat missing input type metadata as eligible
for folding, or resolve the producer output type before skipping; preserve the
existing FLOAT16 exclusion when the type is explicitly known.
- Around line 1524-1534: Replace the hard ValueError in the Q-consumer
validation with graceful skipping of that initializer, and emit a warning
identifying q_node.name and the observed scale dtype. Ensure
get_onnx_bytes_and_metadata continues exporting when a Q scale is a graph input
or remains non-FP16, while preserving FP16 conversion for valid Q scales.

---

Nitpick comments:
In `@examples/torch_onnx/torch_quant_to_onnx.py`:
- Around line 232-259: Update _calibrate_new_quantizers to track quantizers
based on their pre-existing _if_quant state rather than is_enabled when building
enabled_quantizers. Restore quantization only for quantizers whose _if_quant
state was originally active, preserving intentionally disabled quantizers
through the finally block.

In `@modelopt/torch/_deploy/utils/torch_onnx.py`:
- Line 48: Promote _convert_q_data_initializers_to_fp16 in
modelopt/onnx/utils.py to a public helper name, add that name to the module’s
__all__, and update the import and call sites in the ONNX export flow around
_convert_q_data_initializers_to_fp16 accordingly. Preserve the existing
sequencing and validation behavior after the fold guard.

In `@tests/examples/torch_onnx/test_torch_quant_to_onnx.py`:
- Around line 35-62: Harden _assert_residual_adds_are_quantized against missing
graph producers by replacing direct producers[...] accesses with
producers.get(...) and explicit assertions that identify the missing input or
node. Apply the same pattern to the MatMul/ReduceMean lookup paths around the
next(...) calls, asserting the expected producer exists before dereferencing it
while preserving the current validation behavior.
- Around line 64-116: Add concise comments immediately before each hard-coded
topology-count assertion in the quantization test, explaining how the expected
values derive from the ResNet-50 Q/DQ structure and mode-specific behavior.
Cover the activation quantizer counts, DQ fanout counts, and int8 weight
quantizer count, including details such as the number of convolution weights and
disabled fully connected weight quantization; leave the assertions unchanged.

In `@tests/unit/onnx/test_fold_casts.py`:
- Around line 86-97: In `_initializer_q_model`, replace the redundant
`input_dtype` and `output_dtype` calculations with one shared dtype variable
derived from `opset`, and use it when constructing the primary `y` output
instead of creating it as `FLOAT16` and mutating it afterward. Remove the
subsequent `outputs[0].type.tensor_type.elem_type` assignment while preserving
the existing opset-dependent dtype.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: b4d1c1cd-df7c-44b1-be7d-a25ddb7f9ddb

📥 Commits

Reviewing files that changed from the base of the PR and between d2a78de and 77f2433.

📒 Files selected for processing (8)
  • CHANGELOG.rst
  • examples/torch_onnx/torch_quant_to_onnx.py
  • modelopt/onnx/export/fp8_exporter.py
  • modelopt/onnx/utils.py
  • modelopt/torch/_deploy/utils/torch_onnx.py
  • tests/examples/torch_onnx/test_torch_quant_to_onnx.py
  • tests/unit/onnx/quantization/test_fp8_mha_exporter.py
  • tests/unit/onnx/test_fold_casts.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.rst

Comment thread examples/torch_onnx/torch_quant_to_onnx.py Outdated
Comment thread modelopt/onnx/export/fp8_exporter.py Outdated
# when using 1/448 as the Q scale (single fixed value — softmax range is data-independent).
_FP8_E4M3_MAX = 448.0
_FP8_E4M3_SOFTMAX_SCALE = 1.0 / _FP8_E4M3_MAX
_FP8_MIN_CONV_CHANNELS = 16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Are there grouped/depthwise convs in the FP8 export path's expected model set?
rg -nP -C3 '\bgroup\b|groups' modelopt/onnx/export/fp8_exporter.py

Repository: NVIDIA/Model-Optimizer

Length of output: 160


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the relevant files and inspect the exporter + torch-side gate.
git ls-files 'modelopt/onnx/export/fp8_exporter.py' 'examples/torch_onnx/torch_quant_to_onnx.py'
wc -l modelopt/onnx/export/fp8_exporter.py examples/torch_onnx/torch_quant_to_onnx.py
ast-grep outline modelopt/onnx/export/fp8_exporter.py --view expanded
ast-grep outline examples/torch_onnx/torch_quant_to_onnx.py --view expanded

Repository: NVIDIA/Model-Optimizer

Length of output: 2528


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Read the relevant implementation slices.
sed -n '150,230p' modelopt/onnx/export/fp8_exporter.py
printf '\n----\n'
sed -n '190,225p' examples/torch_onnx/torch_quant_to_onnx.py

Repository: NVIDIA/Model-Optimizer

Length of output: 4975


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Find every place the FP8 conv channel threshold is applied.
rg -n -C3 '_FP8_MIN_CONV_CHANNELS|shape\[:2\]|in_channels|out_channels|groups' modelopt/onnx/export/fp8_exporter.py

Repository: NVIDIA/Model-Optimizer

Length of output: 686


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the surrounding exporter logic to see how the skipped weight path is used.
sed -n '1,120p' modelopt/onnx/export/fp8_exporter.py
printf '\n----\n'
sed -n '120,190p' modelopt/onnx/export/fp8_exporter.py
printf '\n----\n'
sed -n '440,500p' modelopt/onnx/export/fp8_exporter.py

Repository: NVIDIA/Model-Optimizer

Length of output: 11674


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Check whether grouped/depthwise convolutions are represented in tests/examples around FP8 export.
rg -n -C2 'depthwise|groups *= *[1-9]|grouped conv|grouped convolution|Conv2d\(.*groups' modelopt examples tests

Repository: NVIDIA/Model-Optimizer

Length of output: 22860


Grouped convs need a group-aware channel check modelopt/onnx/export/fp8_exporter.py:193-196weight_input.values.shape[1] is per-group, so grouped/depthwise convs can be treated as “small channel” even when the module’s in_channels/out_channels are large. That can leave activation Q/DQ enabled while the weight DQ is skipped. Use group in the threshold check so the weight path matches the torch-side gate.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@modelopt/onnx/export/fp8_exporter.py` at line 34, Update the weight-channel
threshold logic near the FP8 minimum-channel constant and the relevant
convolution export check to account for the convolution’s group count, matching
the torch-side gate. Base the decision on the effective total input channels
rather than per-group weight_input.values.shape[1], so grouped and depthwise
convolutions consistently enable or skip activation and weight Q/DQ paths.

Comment thread modelopt/onnx/utils.py Outdated
Comment thread modelopt/onnx/utils.py Outdated
Co-Authored-By: Codex <noreply@openai.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.

Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.

👉 Steps to fix this

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@modelopt/onnx/export/fp8_exporter.py`:
- Around line 196-197: Update the input-stem skip around the weight
dequantization pass so it applies only when the export is identified as a
ResNet, rather than using a three-channel input as the model-family
discriminator. Propagate and check an explicit ResNet/export marker alongside
node.inputs[0].name and weight_input.values.shape, while preserving the skip for
ResNet input stems and normal FP8 DQ restoration for non-ResNet models.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 22b23dca-030c-4c44-891d-67056a837b68

📥 Commits

Reviewing files that changed from the base of the PR and between 77f2433 and 7cf4bf5.

📒 Files selected for processing (4)
  • examples/torch_onnx/torch_quant_to_onnx.py
  • modelopt/onnx/export/fp8_exporter.py
  • tests/examples/torch_onnx/test_torch_quant_to_onnx.py
  • tests/unit/onnx/quantization/test_fp8_mha_exporter.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/examples/torch_onnx/test_torch_quant_to_onnx.py

Comment thread modelopt/onnx/export/fp8_exporter.py Outdated
Comment on lines +196 to +197
if node.inputs[0].name in graph_inputs and weight_input.values.shape[1] == 3:
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Keep the input-stem skip ResNet-specific.

This skips FP8 weight DQ for any direct RGB Conv, while the Torch-side exclusion only applies to timm.models.resnet.ResNet. A non-ResNet model with a direct three-channel input can therefore retain FP16 weights even though this exporter normally restores Conv weight FP8 DQ. Propagate an explicit ResNet/export marker into this pass, or otherwise avoid using RGB topology as the model-family discriminator.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@modelopt/onnx/export/fp8_exporter.py` around lines 196 - 197, Update the
input-stem skip around the weight dequantization pass so it applies only when
the export is identified as a ResNet, rather than using a three-channel input as
the model-family discriminator. Propagate and check an explicit ResNet/export
marker alongside node.inputs[0].name and weight_input.values.shape, while
preserving the skip for ResNet input stems and normal FP8 DQ restoration for
non-ResNet models.

ajrasane and others added 3 commits July 29, 2026 17:09
Co-Authored-By: Codex <noreply@openai.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Co-Authored-By: Codex <noreply@openai.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Co-Authored-By: Codex <noreply@openai.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
@ajrasane
ajrasane requested review from a team as code owners July 30, 2026 00:52

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

CodeRabbit couldn't request changes on this pull request because it doesn't have sufficient GitHub permissions.

Please grant CodeRabbit Pull requests: Read and write permission and re-run the review.

👉 Steps to fix this

Actionable comments posted: 1

🧹 Nitpick comments (1)
modelopt/torch/quantization/plugins/timm.py (1)

16-25: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add __all__ for this module's public API.

Only is_resnet_quantization_supported is a non-underscore (public) symbol here; everything else is intentionally private. Declaring __all__ = ["is_resnet_quantization_supported"] makes that contract explicit and keeps from .timm import * in plugins/__init__.py predictable if more public helpers are added later.

As per coding guidelines, "Define each module's public API with __all__ = [...]."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@modelopt/torch/quantization/plugins/timm.py` around lines 16 - 25, Add a
module-level __all__ declaration in timm.py containing only
is_resnet_quantization_supported, preserving the intended public API for
wildcard imports.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@modelopt_recipes/README.md`:
- Around line 79-84: Update the “Choosing where to look” guidance in README.md
to add a timm-specific lookup step before the general fallback, directing
readers to timm/<architecture>/ for architecture-specific recipes. Preserve the
existing huggingface and general guidance while ensuring timm recipes are
included in the selection flow.

---

Nitpick comments:
In `@modelopt/torch/quantization/plugins/timm.py`:
- Around line 16-25: Add a module-level __all__ declaration in timm.py
containing only is_resnet_quantization_supported, preserving the intended public
API for wildcard imports.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 62647b7a-3e4f-40ce-bedb-56b81fdb85f9

📥 Commits

Reviewing files that changed from the base of the PR and between 7cf4bf5 and edc43d4.

📒 Files selected for processing (22)
  • CHANGELOG.rst
  • examples/torch_onnx/README.md
  • examples/torch_onnx/torch_quant_to_onnx.py
  • modelopt/torch/opt/dynamic.py
  • modelopt/torch/quantization/algorithms.py
  • modelopt/torch/quantization/conversion.py
  • modelopt/torch/quantization/plugins/__init__.py
  • modelopt/torch/quantization/plugins/timm.py
  • modelopt_recipes/README.md
  • modelopt_recipes/timm/resnet/ptq/README.md
  • modelopt_recipes/timm/resnet/ptq/fp8.yaml
  • modelopt_recipes/timm/resnet/ptq/int8.yaml
  • modelopt_recipes/timm/resnet/ptq/mxfp8.yaml
  • modelopt_recipes/timm/resnet/ptq/nvfp4.yaml
  • modelopt_recipes/timm/resnet/ptq/nvfp4_awq_lite.yaml
  • modelopt_recipes/timm/resnet/ptq/static_fp8.quant_cfg.yaml
  • modelopt_recipes/timm/resnet/ptq/static_int8.quant_cfg.yaml
  • tests/unit/torch/nas/test_registry.py
  • tests/unit/torch/quantization/plugins/test_timm.py
  • tests/unit/torch/quantization/test_autoquant.py
  • tests/unit/torch/quantization/test_config_validation.py
  • tests/unit/torch/quantization/test_quantize_cpu.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • examples/torch_onnx/README.md

Comment thread modelopt_recipes/README.md Outdated
Comment on lines +79 to +84
## `timm/` — architecture-specific recipes

Recipes under `timm/<architecture>/` capture quantization choices required by
vision architectures and their deployment backends. See
[`timm/resnet/ptq/`](timm/resnet/ptq/) for ResNet recipes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Include timm recipes in the selection guidance.

The new section is discoverable, but the preceding “Choosing where to look” guidance still directs readers from huggingface/ directly to general/, skipping timm/<architecture>/. Add a timm-specific lookup step before the general fallback.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@modelopt_recipes/README.md` around lines 79 - 84, Update the “Choosing where
to look” guidance in README.md to add a timm-specific lookup step before the
general fallback, directing readers to timm/<architecture>/ for
architecture-specific recipes. Preserve the existing huggingface and general
guidance while ensuring timm recipes are included in the selection flow.

ajrasane and others added 2 commits July 30, 2026 20:34
Keep the change focused on shortcut QDQ placement with FP8, INT8, and AutoQuantize recipes.

Co-Authored-By: Codex <noreply@openai.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Resolve the torch ONNX example conflicts while preserving the residual-only recipe scope.

Co-Authored-By: Codex <noreply@openai.com>
Signed-off-by: ajrasane <131806219+ajrasane@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants