Skip to content

Conversation

@gcunhase
Copy link
Contributor

@gcunhase gcunhase commented Jan 9, 2026

What does this PR do?

Type of change: Bug fix

Overview: This PR fixes an input type mismatch in Resize layers when being converted to FP16.

Usage

$ python -m modelopt.onnx.autocast --onnx_path=$MODEL_NAME.onnx

Testing

Added unittest.

Before your PR is "Ready for review"

  • Make sure you read and follow Contributor guidelines and your commits are signed.
  • Is this change backward compatible?: Yes
  • Did you write any new necessary tests?: Yes
  • Did you add or update any necessary documentation?: No
  • Did you update Changelog?: No

Additional Information

This issue is also fixed by using the standalone type inference logic from #719.

Summary by CodeRabbit

Release Notes

  • Improvements

    • Enhanced the graph sanitization process to automatically duplicate shared constants during optimization, ensuring improved model handling and consistency.
  • Tests

    • Added test coverage for mixed precision conversion of Conv-Resize model architectures.

✏️ Tip: You can customize this high-level summary in your review settings.

@gcunhase gcunhase requested a review from a team as a code owner January 9, 2026 20:29
@gcunhase gcunhase requested a review from galagam January 9, 2026 20:29
@codecov
Copy link

codecov bot commented Jan 9, 2026

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 74.23%. Comparing base (18d9b1e) to head (7e9c6f4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
modelopt/onnx/quantization/fp8.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #757   +/-   ##
=======================================
  Coverage   74.22%   74.23%           
=======================================
  Files         192      192           
  Lines       19027    19033    +6     
=======================================
+ Hits        14123    14129    +6     
  Misses       4904     4904           

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Contributor

@galagam galagam left a comment

Choose a reason for hiding this comment

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

LGTM

@gcunhase gcunhase force-pushed the dev/gcunhasergio/5763448_autocast_resize_type_mismatch branch from 96abee2 to e8bc39e Compare January 13, 2026 00:30
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 13, 2026

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The changes introduce a new constant duplication feature to GraphSanitizer that is invoked during graph sanitization, along with test coverage for Conv-Resize model handling in mixed-precision conversion workflows.

Changes

Cohort / File(s) Summary
GraphSanitizer Enhancement
modelopt/onnx/autocast/graphsanitizer.py
Added duplicate_shared_constants() method that duplicates shared constants via utility function and logs the operation. Method is invoked early in sanitize() right after graph name initialization.
Test Model Builder
tests/_test_utils/onnx/lib_test_models.py
Added build_conv_resize_model() function that constructs ONNX graph with Conv followed by Resize operations, including initializers for weights and resize parameters. Note: Function appears defined twice with identical implementation.
Test Case Addition
tests/unit/onnx/autocast/test_autocast.py
Added test_conv_resize_conversion() test that validates mixed-precision conversion of Conv-Resize models, following pattern of existing conv_isinf test. Import extended to include new test model builder.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 55.56% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main bug fix: addressing a Resize input type mismatch error during model autocast conversion to mixed precision (FP16).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@gcunhase gcunhase changed the title [5763448] Fix Resize input type mismatch error [5763448][ONNX][Autocast] Fix Resize input type mismatch error Jan 13, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
tests/_test_utils/onnx/lib_test_models.py (1)

958-972: Consider adding a brief comment explaining the shared constant pattern.

The intentional reuse of resize_roi_scales for both roi and scales inputs is the core of this test. A brief comment would help future maintainers understand this is deliberate, not accidental.

📝 Suggested clarifying comment
         helper.make_node(
             op_type="Resize",
+            # Note: resize_roi_scales is intentionally used for both roi and scales inputs
+            # to test the shared constant duplication fix (PR #757)
             inputs=[
                 "conv1_conv/Conv2D:0",
                 "resize_roi_scales",
                 "resize_roi_scales",
                 "resize_sizes",
             ],
             outputs=["output_0"],
             name="resize1_resize/Resize",
             coordinate_transformation_mode="asymmetric",
             cubic_coeff_a=-0.75,
             mode="nearest",
             nearest_mode="floor",
         ),
tests/unit/onnx/autocast/test_autocast.py (1)

193-210: Test validates the fix but could be more explicit about what it's testing.

The test successfully verifies that a Conv-Resize model can be converted to mixed precision without errors. However:

  1. The comment on line 205 mentions "QDQ node placements" but the test doesn't check QDQ nodes—consider updating the comment to match the actual assertion.

  2. Since this PR specifically fixes the Resize input type mismatch, consider adding an assertion that verifies the Resize node's data input is FP16 while its roi, scales, and sizes inputs remain their original types (the core bug was that shared constants caused type conflicts).

💡 Suggested improvements
     # Output model should be produced in the same tmp_path
     output_onnx_path = onnx_path.replace(".onnx", ".fp16.onnx")
     onnx.save(converted_model, output_onnx_path)

-    # Load the output model and check QDQ node placements
+    # Load the output model and verify conversion
     graph = gs.import_onnx(converted_model)

     # Check that Conv is converted
     conv_nodes = [n for n in graph.nodes if "Conv" in n.op]
     assert assert_input_precision(conv_nodes)

+    # Check that Resize node has correct input types:
+    # - Data input (index 0) should be FP16
+    # - roi/scales/sizes inputs should remain their original types
+    resize_nodes = [n for n in graph.nodes if n.op == "Resize"]
+    assert len(resize_nodes) == 1
+    resize_node = resize_nodes[0]
+    assert resize_node.inputs[0].dtype == np.float16, "Resize data input should be FP16"
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6ae96b5 and e8bc39e.

📒 Files selected for processing (3)
  • modelopt/onnx/autocast/graphsanitizer.py
  • tests/_test_utils/onnx/lib_test_models.py
  • tests/unit/onnx/autocast/test_autocast.py
🧰 Additional context used
🧬 Code graph analysis (2)
tests/unit/onnx/autocast/test_autocast.py (2)
tests/_test_utils/onnx/lib_test_models.py (1)
  • build_conv_resize_model (929-1009)
modelopt/onnx/autocast/convert.py (1)
  • convert_to_mixed_precision (46-189)
tests/_test_utils/onnx/lib_test_models.py (1)
modelopt/onnx/utils.py (2)
  • infer_shapes (731-744)
  • check_model (555-567)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: linux
  • GitHub Check: wait-checks / wait
  • GitHub Check: wait-checks / wait
  • GitHub Check: code-quality
  • GitHub Check: build-docs
🔇 Additional comments (4)
modelopt/onnx/autocast/graphsanitizer.py (2)

70-70: Placement of duplicate_shared_constants() looks appropriate.

Calling this early in sanitize(), after ensuring the graph name but before naming nodes, is a sensible ordering. This ensures shared constants are duplicated before subsequent transformations that might depend on unique tensor names.


258-262: API contract verified — no issues found.

The utility function onnx_utils.duplicate_shared_constants exists in modelopt/onnx/utils.py at line 524 with the expected signature: def duplicate_shared_constants(onnx_model: onnx.ModelProto) -> tuple[onnx.ModelProto, bool]:. The import statement is correct, and the tuple unpacking in the method correctly handles the (model, bool) return type. The code is implemented correctly.

tests/_test_utils/onnx/lib_test_models.py (1)

929-1009: Test model correctly reproduces the shared constant scenario.

The build_conv_resize_model() function intentionally uses resize_roi_scales for both the roi and scales inputs of the Resize node (inputs at indices 1 and 2). This shared constant pattern is exactly what triggers the type mismatch error during FP16 conversion, making this an effective regression test for the bug fix.

The model structure and validation follow the established patterns in this file.

tests/unit/onnx/autocast/test_autocast.py (1)

23-23: LGTM!

Import correctly extended to include build_conv_resize_model.

@gcunhase gcunhase force-pushed the dev/gcunhasergio/5763448_autocast_resize_type_mismatch branch from 80325df to aa68114 Compare January 13, 2026 17:53
@gcunhase gcunhase enabled auto-merge (squash) January 13, 2026 18:12
@gcunhase gcunhase requested a review from a team as a code owner January 14, 2026 00:36
@gcunhase gcunhase requested a review from ajrasane January 14, 2026 00:36
Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
@gcunhase gcunhase force-pushed the dev/gcunhasergio/5763448_autocast_resize_type_mismatch branch from 2fdc09f to 7e9c6f4 Compare January 14, 2026 00:37
@kevalmorabia97 kevalmorabia97 merged commit 951c6aa into NVIDIA:main Jan 14, 2026
32 of 36 checks passed
kevalmorabia97 pushed a commit that referenced this pull request Jan 14, 2026
## What does this PR do?

**Type of change:** Bug fix

**Overview:** This PR fixes an input type mismatch in Resize layers when
being converted to FP16.

## Usage

```python
$ python -m modelopt.onnx.autocast --onnx_path=$MODEL_NAME.onnx
```

## Testing
Added unittest.

## Before your PR is "*Ready for review*"
<!-- If you haven't finished some of the above items you can still open
`Draft` PR. -->

- **Make sure you read and follow [Contributor
guidelines](https://github.com/NVIDIA/Model-Optimizer/blob/main/CONTRIBUTING.md)**
and your commits are signed.
- **Is this change backward compatible?**: Yes
- **Did you write any new necessary tests?**: Yes
- **Did you add or update any necessary documentation?**: No
- **Did you update
[Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?**:
No

## Additional Information
This issue is also fixed by using the standalone type inference logic
from #719.

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

## Summary by CodeRabbit

## Release Notes

* **Improvements**
* Enhanced the graph sanitization process to automatically duplicate
shared constants during optimization, ensuring improved model handling
and consistency.

* **Tests**
* Added test coverage for mixed precision conversion of Conv-Resize
model architectures.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>

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

---------

Signed-off-by: gcunhase <4861122+gcunhase@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants