[BugFix][TOPI] Fix resize accuracy issue with non-floor rounding#18838
Conversation
Summary of ChangesHello, 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 Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
`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".
12f8f3b to
c71cfb9
Compare
Summary
The int_div optimization in
topi.image.resizewas applied unconditionallyfor
nearest_neighbor+asymmetricmode, regardless of rounding method.This caused accuracy issues when
rounding_methodis not"floor"(e.g.,"round","round_prefer_ceil"), because integer division truncates towardzero rather than rounding.
Fix: Gate the int_div optimization on
rounding_method == "floor"orrounding_method == ""(the default, which gets resolved to"floor"fornon-align_corners modes).
_resize_2dinpython/tvm/topi/image/resize.pypython/tvm/topi/testing/resize_python.py