Skip to content

[Relax][ONNX] Support AffineGrid align_corners false#19854

Open
THINKER-ONLY wants to merge 1 commit into
apache:mainfrom
THINKER-ONLY:affine-grid-align-corners
Open

[Relax][ONNX] Support AffineGrid align_corners false#19854
THINKER-ONLY wants to merge 1 commit into
apache:mainfrom
THINKER-ONLY:affine-grid-align-corners

Conversation

@THINKER-ONLY

@THINKER-ONLY THINKER-ONLY commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Motivation

ONNX AffineGrid has an align_corners attribute, and the ONNX default is
align_corners=0. The Relax ONNX frontend previously rejected that default with
NotImplementedError, so an ONNX model that omitted the attribute could not be
imported.

Fixes #19690.

Changes

This PR threads align_corners through Relax image.affine_grid, legalization,
and the TOPI implementation. The Relax API keeps the existing
align_corners=True default to preserve current behavior, while the ONNX
frontend reads the ONNX attribute and uses ONNX's default value of 0.

The tests cover Relax struct-info inference, legalization, numerical execution,
and ONNX import/execution for both align_corners=0 and align_corners=1.

Testing

  • cmake --build build --parallel $(nproc)
  • pre-commit run --files include/tvm/relax/attrs/image.h python/tvm/relax/frontend/onnx/ onnx_frontend.py python/tvm/relax/op/image/image.py python/tvm/relax/op/op_attrs.py python/ tvm/relax/transform/legalize_ops/image.py python/tvm/topi/image/grid_sample.py python/tvm/ topi/testing/grid_sample_python.py src/relax/op/image/resize.cc src/relax/op/image/resize.h tests/python/relax/test_frontend_onnx.py tests/python/relax/test_op_image.py tests/python/ relax/test_transform_legalize_ops_image.py
  • pytest tests/python/relax/test_op_image.py tests/python/relax/ test_transform_legalize_ops_image.py -q
  • pytest tests/python/relax/test_frontend_onnx.py -k 'affine_grid or grid_sample' -q

Copilot AI review requested due to automatic review settings June 21, 2026 05:36

@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 adds support for the align_corners attribute in the affine_grid operator across TVM Relax, TOPI, and the ONNX frontend, including corresponding tests. The review feedback highlights critical backward-compatibility issues with older IR/models where affine_grid may not have attributes. Specifically, strictly requiring attrs to be non-null in C++ struct info inference and directly accessing call.attrs.align_corners in Python legalization without checking if call.attrs is None can lead to failures.

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 src/relax/op/image/resize.cc Outdated
Comment on lines +382 to +383
const auto* attrs = call->attrs.as<AffineGridAttrs>();
TVM_FFI_ICHECK(attrs) << "Invalid Call";

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.

high

To maintain backward compatibility with older IR/models where affine_grid did not have attributes, we should not strictly require attrs to be non-null. Since attrs is not used anywhere else in InferStructInfoAffineGrid, we can safely remove these lines.

topi.image.affine_grid,
call.args[0],
target_shape=target_shape,
align_corners=call.attrs.align_corners,

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.

high

If call.attrs is None (which is the case for older IR/models where affine_grid had no attributes), accessing call.attrs.align_corners will raise an AttributeError. To prevent this and ensure backward compatibility, we should default align_corners to True if call.attrs is None.

Suggested change
align_corners=call.attrs.align_corners,
align_corners=call.attrs.align_corners if call.attrs else True,

Copilot AI 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.

Pull request overview

Adds support for ONNX AffineGrid’s default align_corners=0 behavior in the Relax ONNX frontend by plumbing an align_corners attribute through the Relax image.affine_grid op, legalization, and TOPI implementation, while preserving existing Relax default behavior (align_corners=True).

Changes:

  • Introduce AffineGridAttrs(align_corners) for relax.image.affine_grid and thread it through Relax op construction and legalization.
  • Extend TOPI affine_grid (and its Python reference) to generate grids for both align_corners=True/False.
  • Update ONNX AffineGrid import to honor ONNX’s align_corners attribute default (0), and add tests for both alignment modes.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
include/tvm/relax/attrs/image.h Adds AffineGridAttrs with align_corners and reflection metadata.
src/relax/op/image/resize.h Updates C++ Relax op constructor signature to accept align_corners.
src/relax/op/image/resize.cc Registers AffineGridAttrs, attaches attrs to calls, and updates op registration to use attrs type.
python/tvm/relax/op/op_attrs.py Registers Python-side AffineGridAttrs object wrapper.
python/tvm/relax/op/image/image.py Extends Relax Python API for affine_grid(..., align_corners=...) (default True).
python/tvm/relax/transform/legalize_ops/image.py Passes align_corners through legalization to TOPI.
python/tvm/topi/image/grid_sample.py Extends TOPI affine_grid to support both align_corners modes.
python/tvm/topi/testing/grid_sample_python.py Extends NumPy reference affine_grid_python for both align_corners modes.
python/tvm/relax/frontend/onnx/onnx_frontend.py Uses ONNX align_corners attribute (default 0) and forwards it into Relax affine_grid.
tests/python/relax/test_op_image.py Adds attribute test + struct-info inference coverage + E2E numeric coverage for both alignments.
tests/python/relax/test_transform_legalize_ops_image.py Adds legalization test coverage for align_corners=False expected TIR.
tests/python/relax/test_frontend_onnx.py Parameterizes ONNX AffineGrid test to cover omitted attr (default), 0, and 1.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@THINKER-ONLY THINKER-ONLY force-pushed the affine-grid-align-corners branch from aa94487 to 548af1f Compare June 21, 2026 05:59
@THINKER-ONLY THINKER-ONLY force-pushed the affine-grid-align-corners branch from 548af1f to 06da467 Compare June 21, 2026 07:26
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.

[Bug][Relax][ONNX] AffineGrid with align_corners=0 (the ONNX default!) is unsupported

2 participants