Skip to content

fix(relax/onnx): honor Resize keep_aspect_ratio_policy - #20846

Open
173787247 wants to merge 1 commit into
apache:mainfrom
173787247:fix/onnx-resize-keep-aspect-ratio
Open

173787247 wants to merge 1 commit into
apache:mainfrom
173787247:fix/onnx-resize-keep-aspect-ratio

Conversation

@173787247

Copy link
Copy Markdown

Fixes #20842

Problem

Resize-18 keep_aspect_ratio_policy adjusts the requested sizes to preserve the input aspect ratio. The frontend read the other Resize attributes but not this one, so the raw sizes were used and the output was stretched.

Reported example: input spatial (2, 4), requested sizes spatial (1, 3):

  • not_larger: ONNX Runtime returns shape (1, 1, 1, 2) with values [1, 3]; TVM returned (1, 1, 1, 3) with [1, 2, 4].
  • not_smaller: ONNX Runtime keeps (1, 1, 2, 4) (the batch/channel scales participate, so the max is 1).
  • stretch (default): unchanged.

Change

  • Added _apply_keep_aspect_ratio_policy: when the policy is not stretch and sizes is given, compute the per-axis scales over the affected axes (axes attribute, or all axes by default), take the min/max, and round each output dimension half-up like the ONNX reference implementation.
  • Applied it before the sizes are sliced to spatial dimensions, in both the constant and ShapeExpr branches.
  • Added test_resize_keep_aspect_ratio_policy, checked against ONNX Runtime for not_larger, not_smaller and stretch.

Validation

pytest tests/python/relax/test_frontend_onnx.py -k resize passes (12 tests), and the reported input matches ONNX Runtime for all three policies.

Resize-18 can adjust the requested sizes to preserve the input aspect
ratio (not_larger / not_smaller).  The frontend read every other Resize
attribute but dropped this one, so an imported model used the raw sizes
and produced a stretched output.

Apply the policy when sizes are given: compute the scale over the
affected axes (all axes by default), take the min/max, and round each
output dimension half-up like the ONNX reference.  Add an ONNX Runtime
checked test covering not_larger, not_smaller and stretch.

This branch has not been deployed

No deployments
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] Resize ignores keep_aspect_ratio_policy and changes output shape

1 participant