Skip to content

Commit a0722c3

Browse files
Copilotjustinchuby
andauthored
Fix Shape operator specification: correct range bounds and document start > end behavior (onnx#7132)
The Shape operator specification incorrectly stated that axes are clamped to the range `[0, r-1]`, but since the `end` parameter is exclusive in slicing operations, the correct range should be `[0, r]`. Additionally, the spec was missing documentation for the `start > end` case. ## Changes Made ### Documentation Fixes - **Corrected range specification**: Changed from `[0, r-1]` to `[0, r]` in all Shape operator documentation - **Added start > end behavior**: Added "If start > end, the result will be an empty shape." to clarify edge case behavior ### Affected Versions Updated documentation for all Shape operator versions that support `start` and `end` attributes: - `Shape_ver24_doc` (current version) - `Shape_ver23_doc` - `Shape_ver19_doc` - `Shape_ver15_doc` Note: Older versions (ver13, ver1) don't have `start`/`end` attributes so no changes were needed. ### Test Coverage - Added test case `test_shape("_start_greater_than_end", x, start=2, end=1)` to validate the documented behavior - Verified that the reference implementation correctly returns an empty array when `start > end` ## Implementation Verification The underlying C++ implementation was already correct - it properly clamps both `start` and `end` to `[0, rank]` and handles the `start > end` case by returning an empty shape: ```cpp start = (start < 0) ? 0 : (start > rank) ? rank : start; end = (end < 0) ? 0 : (end > rank) ? rank : end; output_length->set_dim_value((end - start) < 0 ? 0 : (end - start)); ``` This fix ensures the documentation accurately reflects the actual behavior, making it consistent with how ONNX Runtime handles these edge cases. Fixes onnx#6862. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com> Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com> Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
1 parent a0e90be commit a0722c3

File tree

9 files changed

+27
-20
lines changed

9 files changed

+27
-20
lines changed

docs/Changelog.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19866,11 +19866,11 @@ This version of the operator has been available since version 15 of the default
1986619866
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
1986719867
If the end axis is omitted, the axes upto the last one will be included.
1986819868
Negative axes indicate counting back from the last axis.
19869-
Note that axes will be clamped to the range [0, r-1], where r is the
19869+
Note that axes will be clamped to the range [0, r], where r is the
1987019870
rank of the input tensor if they are out-of-range (after adding r in the case of
1987119871
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
1987219872
value of r, and specifying any start value < -r is equivalent to specifying a start
19873-
value of 0.
19873+
value of 0. If start > end, the result will be an empty shape.
1987419874

1987519875
Examples:
1987619876

@@ -23815,11 +23815,11 @@ This version of the operator has been available since version 19 of the default
2381523815
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
2381623816
If the end axis is omitted, the axes upto the last one will be included.
2381723817
Negative axes indicate counting back from the last axis.
23818-
Note that axes will be clamped to the range [0, r-1], where r is the
23818+
Note that axes will be clamped to the range [0, r], where r is the
2381923819
rank of the input tensor if they are out-of-range (after adding r in the case of
2382023820
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
2382123821
value of r, and specifying any start value < -r is equivalent to specifying a start
23822-
value of 0.
23822+
value of 0. If start > end, the result will be an empty shape.
2382323823

2382423824
Examples:
2382523825

@@ -25660,11 +25660,11 @@ This version of the operator has been available since version 21 of the default
2566025660
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
2566125661
If the end axis is omitted, the axes upto the last one will be included.
2566225662
Negative axes indicate counting back from the last axis.
25663-
Note that axes will be clamped to the range [0, r-1], where r is the
25663+
Note that axes will be clamped to the range [0, r], where r is the
2566425664
rank of the input tensor if they are out-of-range (after adding r in the case of
2566525665
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
2566625666
value of r, and specifying any start value < -r is equivalent to specifying a start
25667-
value of 0.
25667+
value of 0. If start > end, the result will be an empty shape.
2566825668

2566925669
Examples:
2567025670

@@ -29557,11 +29557,11 @@ This version of the operator has been available since version 23 of the default
2955729557
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
2955829558
If the end axis is omitted, the axes upto the last one will be included.
2955929559
Negative axes indicate counting back from the last axis.
29560-
Note that axes will be clamped to the range [0, r-1], where r is the
29560+
Note that axes will be clamped to the range [0, r], where r is the
2956129561
rank of the input tensor if they are out-of-range (after adding r in the case of
2956229562
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
2956329563
value of r, and specifying any start value < -r is equivalent to specifying a start
29564-
value of 0.
29564+
value of 0. If start > end, the result will be an empty shape.
2956529565

2956629566
Examples:
2956729567

@@ -30811,11 +30811,11 @@ This version of the operator has been available since version 24 of the default
3081130811
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
3081230812
If the end axis is omitted, the axes upto the last one will be included.
3081330813
Negative axes indicate counting back from the last axis.
30814-
Note that axes will be clamped to the range [0, r-1], where r is the
30814+
Note that axes will be clamped to the range [0, r], where r is the
3081530815
rank of the input tensor if they are out-of-range (after adding r in the case of
3081630816
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
3081730817
value of r, and specifying any start value < -r is equivalent to specifying a start
30818-
value of 0.
30818+
value of 0. If start > end, the result will be an empty shape.
3081930819

3082030820
Examples:
3082130821

docs/Operators.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32527,11 +32527,11 @@ expect(
3252732527
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
3252832528
If the end axis is omitted, the axes upto the last one will be included.
3252932529
Negative axes indicate counting back from the last axis.
32530-
Note that axes will be clamped to the range [0, r-1], where r is the
32530+
Note that axes will be clamped to the range [0, r], where r is the
3253132531
rank of the input tensor if they are out-of-range (after adding r in the case of
3253232532
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
3253332533
value of r, and specifying any start value < -r is equivalent to specifying a start
32534-
value of 0.
32534+
value of 0. If start > end, the result will be an empty shape.
3253532535

3253632536
Examples:
3253732537

@@ -32632,6 +32632,8 @@ test_shape("_start_1_end_2", x, start=1, end=2)
3263232632
test_shape("_clip_start", x, start=-10)
3263332633

3263432634
test_shape("_clip_end", x, end=10)
32635+
32636+
test_shape("_start_greater_than_end", x, start=2, end=1)
3263532637
```
3263632638

3263732639
</details>

docs/TestCoverage.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23046,6 +23046,8 @@ test_shape("_start_1_end_2", x, start=1, end=2)
2304623046
test_shape("_clip_start", x, start=-10)
2304723047

2304823048
test_shape("_clip_end", x, end=10)
23049+
23050+
test_shape("_start_greater_than_end", x, start=2, end=1)
2304923051
```
2305023052

2305123053
</details>

onnx/backend/test/case/node/shape.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ def export() -> None:
5656
test_shape("_clip_start", x, start=-10)
5757

5858
test_shape("_clip_end", x, end=10)
59+
60+
test_shape("_start_greater_than_end", x, start=2, end=1)
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BxJ�x��?h��>��z?�j@$ �?�.z��8s?b��hdӽ�9�>(�>�%�?^�B?�0�= B�>]ת>�=�?R�iJ�>�Z�/d#��S'?�K]?��=��C@�(��Hm;= �?�2�?��?��>���>�Ec������!��� >*z�?��?�Oƾmǚ��6��&õ�gڿ��?�x�FKྙ[��� G?4�ο��Y�L=e��> �����k��QN�>.:�=�ݚ>�b"�6���
Binary file not shown.

onnx/defs/tensor/defs.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,11 @@ If start axis is omitted, the slice starts from axis 0.
402402
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
403403
If the end axis is omitted, the axes upto the last one will be included.
404404
Negative axes indicate counting back from the last axis.
405-
Note that axes will be clamped to the range [0, r-1], where r is the
405+
Note that axes will be clamped to the range [0, r], where r is the
406406
rank of the input tensor if they are out-of-range (after adding r in the case of
407407
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
408408
value of r, and specifying any start value < -r is equivalent to specifying a start
409-
value of 0.
409+
value of 0. If start > end, the result will be an empty shape.
410410

411411
Examples:
412412

onnx/defs/tensor/old.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,11 +1752,11 @@ If start axis is omitted, the slice starts from axis 0.
17521752
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
17531753
If the end axis is omitted, the axes upto the last one will be included.
17541754
Negative axes indicate counting back from the last axis.
1755-
Note that axes will be clamped to the range [0, r-1], where r is the
1755+
Note that axes will be clamped to the range [0, r], where r is the
17561756
rank of the input tensor if they are out-of-range (after adding r in the case of
17571757
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
17581758
value of r, and specifying any start value < -r is equivalent to specifying a start
1759-
value of 0.
1759+
value of 0. If start > end, the result will be an empty shape.
17601760

17611761
Examples:
17621762

@@ -7553,11 +7553,11 @@ If start axis is omitted, the slice starts from axis 0.
75537553
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
75547554
If the end axis is omitted, the axes upto the last one will be included.
75557555
Negative axes indicate counting back from the last axis.
7556-
Note that axes will be clamped to the range [0, r-1], where r is the
7556+
Note that axes will be clamped to the range [0, r], where r is the
75577557
rank of the input tensor if they are out-of-range (after adding r in the case of
75587558
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
75597559
value of r, and specifying any start value < -r is equivalent to specifying a start
7560-
value of 0.
7560+
value of 0. If start > end, the result will be an empty shape.
75617561

75627562
Examples:
75637563

@@ -7656,11 +7656,11 @@ If start axis is omitted, the slice starts from axis 0.
76567656
The end axis, if specified, is exclusive (and the returned value will not include the size of that axis).
76577657
If the end axis is omitted, the axes upto the last one will be included.
76587658
Negative axes indicate counting back from the last axis.
7659-
Note that axes will be clamped to the range [0, r-1], where r is the
7659+
Note that axes will be clamped to the range [0, r], where r is the
76607660
rank of the input tensor if they are out-of-range (after adding r in the case of
76617661
negative axis). Thus, specifying any end value > r is equivalent to specifying an end
76627662
value of r, and specifying any start value < -r is equivalent to specifying a start
7663-
value of 0.
7663+
value of 0. If start > end, the result will be an empty shape.
76647664

76657665
Examples:
76667666

0 commit comments

Comments
 (0)