Skip to content

[Fix][TOPI] Avoid zero division in unit crop resize - #21101

Open
Nanmur wants to merge 1 commit into
apache:mainfrom
Nanmur:codex/fix-resize-crop-unit-extent
Open

Nanmur wants to merge 1 commit into
apache:mainfrom
Nanmur:codex/fix-resize-crop-unit-extent

Conversation

@Nanmur

@Nanmur Nanmur commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #20877.

Problem

In TOPI get_inx, tf_crop_and_resize already selects the ROI midpoint when the output extent is one. However, both arguments to te.if_then_else are constructed eagerly. Building the unselected branch divides by target_width - 1, which is zero for a static unit extent, and raises Divide by zero before the conditional can discard it.

Change

Use max(target_width - 1, 1) as the denominator of that branch. For output extents greater than one this is the original denominator; for a unit extent the existing conditional still selects the unchanged ROI-center expression. The denominator is also safe with symbolic extents.

This change is confined to tf_crop_and_resize; it does not change align_corners or address empty-output handling.

Regression coverage

  • Unit height, unit width, both unit axes, and a non-unit control.
  • Nearest and linear interpolation, using nonuniform input values.
  • Full, zero, cropped, and reversed ROIs.
  • Symbolic coordinate expressions specialized to output extents 1, 2, and 5.

Validation

  • Unmodified main TOPI resize module: 24 failed, 11 passed. All 24 failures reproduce the unit-extent divide-by-zero error.
  • Patched module: 35 passed (32 ONNX import/LLVM compilation/VM execution comparisons against ONNX Runtime, plus 3 symbolic-coordinate checks).
  • Ruff 0.12.3 check and format check passed; git diff --check passed.

The main TOPI resize Python module was loaded verbatim into an existing local TVM 0.26.dev0 LLVM runtime. The pre-/post-TIRx-extraction compatibility aliases are kept outside this PR. This is not a full build or full-suite result for current main; upstream CI is required.

For a current-main build:

python -m pytest tests/python/relax/test_frontend_onnx_resize_crop_unit.py -q

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][TOPI] tf_crop_and_resize builds an inactive divide-by-zero branch for output size one

1 participant