Cherry pick commit#7740
Merged
andife merged 2 commits intoonnx:rel-1.21.0from Mar 14, 2026
Merged
Conversation
…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>
Codecov Report✅ All modified and coverable lines are covered by tests. 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. |
justinchuby
approved these changes
Mar 14, 2026
andife
approved these changes
Mar 14, 2026
titaiwangms
approved these changes
Mar 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Fixes #
Fix Slice shape inference crashes with SIGABRT on empty dimensions by titaiwangms · Pull Request #7738 · onnx/onnx
Rename misleading input_rank param to input_dim_value in processSlice… by titaiwangms · Pull Request #7739 · onnx/onnx