Skip to content

Cherry pick commit#7740

Merged
andife merged 2 commits intoonnx:rel-1.21.0from
Sunny-Anand:cherry_pick_commit
Mar 14, 2026
Merged

Cherry pick commit#7740
andife merged 2 commits intoonnx:rel-1.21.0from
Sunny-Anand:cherry_pick_commit

Conversation

titaiwangms and others added 2 commits March 13, 2026 21:43
…nnx#7738)

Fix onnx#7735 

This pull request addresses issues with the ONNX Slice operator when
handling empty dimensions, ensuring correct and safe shape inference in
these edge cases. The main focus is to prevent undefined behavior and
ensure that slicing on empty dimensions consistently produces outputs
with zero length, regardless of the step direction. Additional
regression tests are included to verify these fixes, and several test
data files are updated to reflect the new behavior.

**Slice operator improvements for empty dimensions:**

* Updated the `processSliceInputs` function in `defs.cc` to
short-circuit and produce a zero-length output when the input dimension
size is zero, avoiding invalid clamp bounds and potential undefined
behavior.
* Modified shape inference logic in `old.cc` to detect empty dimensions
and set the output dimension value to zero, both for standard and
expanded shape inference code paths.
[[1]](diffhunk://#diff-5e200991755406044e2dbb2449683bb5c6d77583d1459f0065a756813dd1b59bR2635-R2645)
[[2]](diffhunk://#diff-5e200991755406044e2dbb2449683bb5c6d77583d1459f0065a756813dd1b59bR6329-R6335)

**Testing and regression coverage:**

* Added two new tests in `shape_inference_test.py` to verify that
slicing on empty dimensions with both positive and negative steps
produces the correct output shape, preventing regression of issue onnx#7735.

**Test data updates:**

* Updated several `output_0.pb` binary test data files under
`onnx/backend/test/data/node/` (including attention, gelu,
hammingwindow, and lppool tests) to align with the corrected Slice
operator behavior on empty dimensions.
[[1]](diffhunk://#diff-875055c843db41ea7d579c8bd29201e6f60bb4175ac96f840bdd27d488866198L1-R7)
[[2]](diffhunk://#diff-d6fe5631baa8b6cb320c03143f797abe3dce4ddf5363fa9cfe71cc12bc31ac53L2-R3)
[[3]](diffhunk://#diff-3275d5e3517a0e95967de3c46a75b02104980f2566f1a0cbefa386ea8e02f312L1-R3)
[[4]](diffhunk://#diff-537fe44987ef880d522c9c88283070745874abc990951f49eff8dccc35630af1L2-R2)
[[5]](diffhunk://#diff-3d57e2f0cf63b32349f6619a2f61e90d4a4aa4fdac9a794af6bfb00c97fe9f8eL1-R2)

These changes collectively improve the robustness of ONNX shape
inference and operator correctness when handling tensors with empty
dimensions.

---------

Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
onnx#7739)

This pull request improves the handling of the `Slice` operator when
applied to empty or scalar-shaped tensors, ensuring correct shape
inference and preventing potential crashes. The changes include both a
bug fix in the shape inference logic and the addition of a targeted test
case.

**Bug fix for `Slice` shape inference:**

* Updated the `processSliceInputs` function in `defs.cc` to use the
actual input dimension size or value (instead of input rank) when
calculating slice bounds, ensuring correct handling for empty or scalar
dimensions.

**Testing improvements:**

* Added a new test `test_slice_scalar_shape_output` in
`shape_inference_test.py` to verify that slicing the shape of a scalar
tensor produces a zero-length output and does not crash.

---------

Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Sunny-Anand Sunny-Anand requested review from a team as code owners March 14, 2026 02:46
@github-project-automation github-project-automation Bot moved this to In progress in PR Tracker Mar 14, 2026
@Sunny-Anand Sunny-Anand added this to the 1.21 milestone Mar 14, 2026
@Sunny-Anand Sunny-Anand added the topic: documentation Issues related to ONNX documentation label Mar 14, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (rel-1.21.0@a51ac07). Learn more about missing BASE report.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@              Coverage Diff              @@
##             rel-1.21.0    #7740   +/-   ##
=============================================
  Coverage              ?   55.72%           
=============================================
  Files                 ?      515           
  Lines                 ?    32498           
  Branches              ?     2871           
=============================================
  Hits                  ?    18111           
  Misses                ?    13598           
  Partials              ?      789           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-project-automation github-project-automation Bot moved this from In progress to Reviewer approved in PR Tracker Mar 14, 2026
@andife andife merged commit 33afebf into onnx:rel-1.21.0 Mar 14, 2026
54 of 56 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewer approved to Done in PR Tracker Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: documentation Issues related to ONNX documentation

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants