Skip to content

[Relax][ONNX] Handle empty Slice with negative step#19537

Open
THINKER-ONLY wants to merge 1 commit into
apache:mainfrom
THINKER-ONLY:fix-onnx-slice-negative-empty
Open

[Relax][ONNX] Handle empty Slice with negative step#19537
THINKER-ONLY wants to merge 1 commit into
apache:mainfrom
THINKER-ONLY:fix-onnx-slice-negative-empty

Conversation

@THINKER-ONLY
Copy link
Copy Markdown
Contributor

This PR fixes #19532.

ONNX Slice can produce an empty tensor when a negative step moves away from the end bound. For example, starts=[0], ends=[5], and steps=[-1] should produce a zero-length result, matching ONNX Runtime.

Previously, the Relax ONNX frontend passed those bounds directly to relax.strided_slice. During legalization, TOPI rejected the range because the canonicalized end is greater than the begin for a negative stride.

This PR normalizes statically empty constant Slice ranges before emitting relax.strided_slice, so Relax can represent the same zero-length result. It also adds a regression test for the negative-step empty result case.

Testing:

  • pytest tests/python/relax/test_frontend_onnx.py::test_slice -q
  • pytest tests/python/relax/test_frontend_onnx.py -k slice -q
  • pytest tests/python/relax/test_op_index.py tests/python/relax/test_transform_legalize_ops_index_linear_algebra.py -k strided_slice -q
  • pytest tests/python/relax/test_frontend_onnx.py -k "not test_all_reduce_funcs_axes_input and not test_topk" -q
  • git diff --check
  • python -m py_compile python/tvm/relax/frontend/onnx/onnx_frontend.py tests/python/relax/test_frontend_onnx.py

Note: Full test_frontend_onnx.py -q currently has 14 failures in test_all_reduce_funcs_axes_input and test_topk; the same failures reproduce on the cached baseline source, so they are unrelated to this patch.

ONNX Slice can produce an empty tensor when a negative step moves away from the end bound.  For example, starts=[0], ends=[5], and steps=[-1] should produce a zero-length result instead of being rejected by Relax strided_slice legalization.

Normalize statically empty constant Slice ranges before emitting Relax strided_slice, and add a frontend regression test matching ONNX Runtime behavior.
Copilot AI review requested due to automatic review settings May 11, 2026 14:21
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

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 introduces logic to handle statically empty ONNX slice ranges in the Relax frontend. It adds helper functions to extract static dimensions and canonicalize slice indices according to ONNX rules, ensuring that empty slices are correctly normalized for Relax's strided_slice. A new test case was added to verify this behavior. I have no feedback to provide.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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 Slice crashes on negative-step empty result

2 participants