Skip to content

[BugFix][TOPI] Fix resize accuracy issue with non-floor rounding#18838

Merged
tqchen merged 3 commits into
apache:mainfrom
tqchen:reland-resize-fix
Feb 27, 2026
Merged

[BugFix][TOPI] Fix resize accuracy issue with non-floor rounding#18838
tqchen merged 3 commits into
apache:mainfrom
tqchen:reland-resize-fix

Conversation

@tqchen

@tqchen tqchen commented Feb 27, 2026

Copy link
Copy Markdown
Member

Summary

The int_div optimization in topi.image.resize was applied unconditionally
for nearest_neighbor + asymmetric mode, regardless of rounding method.
This caused accuracy issues when rounding_method is not "floor" (e.g.,
"round", "round_prefer_ceil"), because integer division truncates toward
zero rather than rounding.

Fix: Gate the int_div optimization on rounding_method == "floor" or
rounding_method == "" (the default, which gets resolved to "floor" for
non-align_corners modes).

  • Updates _resize_2d in python/tvm/topi/image/resize.py
  • Updates reference implementation in python/tvm/topi/testing/resize_python.py
  • Updates legalize test expected output to reflect the new behavior

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request reintroduces a fix for resize operations within TOPI, specifically enhancing the nearest neighbor interpolation method. It introduces a new rounding_method parameter across several resize functions, allowing users to specify different rounding behaviors for index calculations. This change provides more granular control over how pixels are mapped during resizing, addressing potential inaccuracies and improving the flexibility of the resize implementation.

Highlights

  • Enhanced Nearest Neighbor Rounding: Introduced a rounding_method parameter to control how indices are rounded during nearest neighbor interpolation, allowing for more precise control over resizing behavior.
  • Flexible Rounding Strategies: Implemented various rounding strategies within the get_index function, including 'floor', 'round', 'round_prefer_floor', 'round_prefer_ceil', and 'ceil'.
  • Conditional Integer Division Conversion: Updated the can_convert_multiply_to_intdiv logic to conditionally apply integer division based on the specified rounding_method for 'asymmetric' nearest neighbor resizing.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • python/tvm/topi/image/resize.py
    • Applied conditional logic for can_convert_multiply_to_intdiv based on the rounding_method when method is 'nearest_neighbor' and coordinate_transformation_mode is 'asymmetric'.
  • python/tvm/topi/testing/resize_python.py
    • Modified get_index to accept a rounding_method parameter and implemented a dispatch mechanism for different rounding strategies (floor, round, round_prefer_floor, round_prefer_ceil, ceil).
    • Updated resize3d_nearest, resize3d_ncdhw, resize1d_python, resize2d_python, and resize3d_python to propagate the new rounding_method parameter.
Activity
  • No human activity (comments, reviews) has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@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 relands a fix for image resizing, specifically addressing rounding methods for nearest neighbor scaling. The changes introduce a rounding_method parameter across various resize functions and their testing counterparts. The core logic is updated in get_index in the test utilities and a corresponding optimization constraint is added in _resize_2d. The changes appear correct and improve consistency. I have one suggestion to improve code clarity in the test implementation.

Comment thread python/tvm/topi/testing/resize_python.py
`can_convert_multiply_to_intdiv` was enabled unconditionally for
`nearest_neighbor` + `asymmetric` mode, even when `rounding_method` is
not `floor`.  This converted the mapped coordinate via integer division
(equivalent to floor) regardless of the requested rounding, producing
wrong results whenever the target size is an exact integer multiple of
the source size.

Fix: guard the `can_convert_multiply_to_intdiv` optimisation so it only
activates when `rounding_method` is `"floor"` or `""` (which defaults to
`"floor"` for asymmetric mode).

Also update the Python reference implementation (`resize_python.py`) to
accept and thread a `rounding_method` parameter through all public entry
points (`get_index`, `resize3d_nearest`, `resize3d_ncdhw`, `resize1d_python`,
`resize2d_python`, `resize3d_python`), so the reference matches the
corrected TVM behaviour.

Add `tests/python/te/test_topi_resize2d.py` with regression tests that
exercise the buggy path (non-floor rounding + asymmetric + integer
scale factor) as well as sanity checks for floor rounding and
non-integer scale factors.

Fixes: apache#16137
The rounding_method default in Relax is "round", not "".
With the int_div optimization now gated on floor/default rounding,
the expected output for the legalize test changes from T.Div to
T.Cast+T.round since "round" != "floor".
@tqchen tqchen changed the title [TOPI] Reland resize fix [BugFix][TOPI] Fix resize accuracy issue with non-floor rounding Feb 27, 2026

@MasterJH5574 MasterJH5574 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.

LGTM.

@tqchen tqchen merged commit c1d3243 into apache:main Feb 27, 2026
11 of 12 checks passed
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