Skip to content

[Relax][Frontend][ONNX] Fix Resize coordinate error with non-integer scales - #19698

Open
cchung100m wants to merge 14 commits into
apache:mainfrom
cchung100m:issue-19570
Open

[Relax][Frontend][ONNX] Fix Resize coordinate error with non-integer scales#19698
cchung100m wants to merge 14 commits into
apache:mainfrom
cchung100m:issue-19570

Conversation

@cchung100m

Copy link
Copy Markdown
Contributor

Hi Committers,

This PR fixes issues #19570. Any suggestions would be appreciated if you are available.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request preserves original spatial scales during ONNX frontend coordinate transformations to avoid lossy ratios, propagating scale overrides through the 1D, 2D, and 3D image resize operations in TOPI. Feedback on these changes highlights a critical typo (sacle_x_override) in resize.py that will cause a runtime NameError, the fact that the newly defined original_spatial_scales in onnx_frontend.py is never actually used or passed to the resize operators, and a minor PEP 8 style violation regarding spaces around keyword argument assignments.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/tvm/topi/image/resize.py Outdated
Comment thread python/tvm/relax/frontend/onnx/onnx_frontend.py Outdated
Comment thread python/tvm/topi/image/resize.py Outdated
@cchung100m
cchung100m force-pushed the issue-19570 branch 3 times, most recently from dcd1223 to ff51a8a Compare June 10, 2026 16:24
@cchung100m

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request preserves the original spatial scales in the ONNX frontend resize operations to prevent lossy coordinate transformations with non-integer scales. It updates TOPI resize functions to accept scale overrides and adds corresponding unit tests. The review feedback suggests fixing a typo in a comment and using topi_mode instead of relax_mode in the 3D resize implementation for consistency.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/tvm/relax/frontend/onnx/onnx_frontend.py Outdated
Comment thread python/tvm/relax/frontend/onnx/onnx_frontend.py Outdated
@cchung100m
cchung100m marked this pull request as ready for review June 11, 2026 17:00
@cchung100m

Copy link
Copy Markdown
Contributor Author

cc @tlopex @guan404ming

@tlopex tlopex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for working on this fix. Preserving the original ONNX scale is the right approach, but I found two remaining correctness gaps.

  1. The dynamic-ROI path still drops original_spatial_scales.

When roi_dynamic_vec is present, _impl_v18 calls _emit_resize_topi_dynamic_roi without forwarding the original scales. The nested resize1d/2d/3d TOPI calls therefore continue to derive the coordinate scale from input_size / output_size.

For example, a dynamic ROI input with a 3x3 tensor and scale 2.5 will still use 3/7 instead of 1/2.5. Could we pass the original spatial scales through this helper for all supported ranks and add a regression test?

  1. The 2D asymmetric-nearest integer-division optimization ignores the scale override.

For method="nearest_neighbor", coordinate_transformation_mode="asymmetric", and rounding_method="floor", _resize_2d may set use_int_div=True based only on the input and output sizes. get_inx then uses integer division and does not use scale_x_override.

A concrete example is input size 3 with scale 2.1. The output size is floor(3 * 2.1) = 6, so the optimization uses x // 2, while ONNX requires floor(x / 2.1). These produce different pixel mappings. I think the integer division optimization should be disabled when an explicit scale override is provided, unless the override is proven to match the derived ratio exactly

@cchung100m
cchung100m marked this pull request as draft July 18, 2026 08:19
@cchung100m
cchung100m force-pushed the issue-19570 branch 9 times, most recently from 8232129 to 07a5f7f Compare July 23, 2026 12:35
@cchung100m
cchung100m marked this pull request as ready for review July 23, 2026 14:12
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@cchung100m

Copy link
Copy Markdown
Contributor Author

Hi @tlopex

Thank you for the detailed review. I have implemented regression tests as requested.

@cchung100m
cchung100m requested a review from tlopex August 4, 2026 16:55
@tlopex

tlopex commented Aug 4, 2026

Copy link
Copy Markdown
Member

@cchung100m Hey, the updated test does not exercise the integer-division optimization because providing scales makes scale_h and scale_w non-None, so the optimized path is skipped. Please use sizes or add a TOPI test without scales.

@cchung100m
cchung100m marked this pull request as draft August 13, 2026 13:11
@cchung100m
cchung100m force-pushed the issue-19570 branch 2 times, most recently from d18d5a2 to 04f7738 Compare August 13, 2026 14:23
@cchung100m cchung100m changed the title [Relax][ONNX] Fix Resize coordinate error with non-integer scales [Relax][Frontend][ONNX] Fix Resize coordinate error with non-integer scales Aug 13, 2026
@cchung100m
cchung100m marked this pull request as ready for review August 14, 2026 15:03
@cchung100m

Copy link
Copy Markdown
Contributor Author

Hi @tlopex
Thanks for the feedback. I've addressed the issue "use sizes or add a TOPI test without scales" by implementing three direct TOPI-level tests that call topi.image.resize2d directly. These tests:

  1. Verify the int-div optimization fires when scales=None and the size ratio is an integer
  2. Verify the optimization is correctly disabled when an explicit (non-integer) scale override is provided.
  3. Verify the optimization is behavior-preserving for integer scale overrides

@cchung100m

Copy link
Copy Markdown
Contributor Author

@tvm-bot rerun

@github-actions

Copy link
Copy Markdown
Contributor

Failed to re-run CI in https://github.com/apache/tvm/actions/runs/32032583932

Details
Traceback (most recent call last):
  File "/home/runner/work/tvm/tvm/ci/scripts/github/github_tvmbot.py", line 588, in comment_failure
    raise item
  File "/home/runner/work/tvm/tvm/ci/scripts/github/github_tvmbot.py", line 694, in run
    pr.rerun_jenkins_ci()
  File "/home/runner/work/tvm/tvm/ci/scripts/github/github_tvmbot.py", line 547, in rerun_jenkins_ci
    post(url, auth=("tvm-bot", TVM_BOT_JENKINS_TOKEN))
  File "/home/runner/work/tvm/tvm/ci/scripts/jenkins/git_utils.py", line 54, in post
    with request.urlopen(req, data) as response:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/urllib/request.py", line 215, in urlopen
    return opener.open(url, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/urllib/request.py", line 521, in open
    response = meth(req, response)
               ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/urllib/request.py", line 630, in http_response
    response = self.parent.error(
               ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/urllib/request.py", line 559, in error
    return self._call_chain(*args)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/urllib/request.py", line 492, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "/usr/lib/python3.12/urllib/request.py", line 639, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 502: Bad Gateway

with response

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>

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.

2 participants