Skip to content

Commit 5be3e22

Browse files
authored
Adding Reverse op (onnx#1804)
* Adding Reverse Op * fixing merge conflicts
1 parent c05f2ae commit 5be3e22

File tree

15 files changed

+444
-1
lines changed

15 files changed

+444
-1
lines changed

docs/Changelog.md

+77
Original file line numberDiff line numberDiff line change
@@ -9649,6 +9649,83 @@ This version of the operator has been available since version 10 of the default
96499649
<dd>Constrain input 'X' and output 'Y' to all tensor types.</dd>
96509650
</dl>
96519651

9652+
### <a name="Reverse-10"></a>**Reverse-10**</a>
9653+
9654+
Reverse a tensor of arbitrary shape along the given 'axes' in its dimensions.
9655+
9656+
Example 1:
9657+
input = [
9658+
[0.0, 1.0, 2.0],
9659+
[3.0, 4.0, 5.0],
9660+
]
9661+
9662+
output = [
9663+
[5.0, 4.0, 3.0],
9664+
[2.0, 1.0, 0.0],
9665+
]
9666+
9667+
Example 2:
9668+
input = [
9669+
[0.0, 1.0, 2.0],
9670+
[3.0, 4.0, 5.0],
9671+
]
9672+
9673+
axes = [0]
9674+
9675+
output = [
9676+
[3.0, 4.0, 5.0],
9677+
[0.0, 1.0, 2.0],
9678+
]
9679+
9680+
Example 3:
9681+
input = [
9682+
[[0.0, 1.0, 2.0],
9683+
[3.0, 4.0, 5.0]],
9684+
[[6.0, 7.0, 8.0],
9685+
[9.0, 10.0, 11.0]],
9686+
]
9687+
9688+
axes = [1,-1]
9689+
9690+
output = [
9691+
[[5.0, 4.0, 3.0],
9692+
[2.0, 1.0, 0.0]],
9693+
[[11.0, 10.0, 9.0],
9694+
[8.0, 7.0, 6.0]],
9695+
]
9696+
9697+
#### Version
9698+
9699+
This version of the operator has been available since version 10 of the default ONNX operator set.
9700+
9701+
#### Attributes
9702+
9703+
<dl>
9704+
<dt><tt>axes</tt> : list of ints</dt>
9705+
<dd>A list of integers indicating the axes to reverse. Negative values mean counting dimensions from the back.By default, reverse over all axes of input dimensions</dd>
9706+
</dl>
9707+
9708+
#### Inputs
9709+
9710+
<dl>
9711+
<dt><tt>input</tt> : T</dt>
9712+
<dd>Tensor of rank r >= 1.</dd>
9713+
</dl>
9714+
9715+
#### Outputs
9716+
9717+
<dl>
9718+
<dt><tt>output</tt> : T</dt>
9719+
<dd>Tensor of rank r >= 1 (same rank as input).</dd>
9720+
</dl>
9721+
9722+
#### Type Constraints
9723+
9724+
<dl>
9725+
<dt><tt>T</tt> : tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(float16), tensor(float), tensor(double), tensor(string), tensor(bool), tensor(complex64), tensor(complex128)</dt>
9726+
<dd>Input and output types can be of any tensor type.</dd>
9727+
</dl>
9728+
96529729
### <a name="Slice-10"></a>**Slice-10**</a>
96539730

96549731
Produces a slice of the input tensor along multiple axes. Similar to numpy:

docs/Operators.md

+146
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
* <a href="#Relu">Relu</a>
9898
* <a href="#Reshape">Reshape</a>
9999
* <a href="#Resize">Resize</a>
100+
* <a href="#Reverse">Reverse</a>
100101
* <a href="#Scan">Scan</a>
101102
* <a href="#Scatter">Scatter</a>
102103
* <a href="#Selu">Selu</a>
@@ -10098,6 +10099,151 @@ expect(node, inputs=[data, scales], outputs=[output],
1009810099
</details>
1009910100

1010010101

10102+
### <a name="Reverse"></a><a name="reverse">**Reverse**</a>
10103+
10104+
Reverse a tensor of arbitrary shape along the given 'axes' in its dimensions.
10105+
10106+
Example 1:
10107+
input = [
10108+
[0.0, 1.0, 2.0],
10109+
[3.0, 4.0, 5.0],
10110+
]
10111+
10112+
output = [
10113+
[5.0, 4.0, 3.0],
10114+
[2.0, 1.0, 0.0],
10115+
]
10116+
10117+
Example 2:
10118+
input = [
10119+
[0.0, 1.0, 2.0],
10120+
[3.0, 4.0, 5.0],
10121+
]
10122+
10123+
axes = [0]
10124+
10125+
output = [
10126+
[3.0, 4.0, 5.0],
10127+
[0.0, 1.0, 2.0],
10128+
]
10129+
10130+
Example 3:
10131+
input = [
10132+
[[0.0, 1.0, 2.0],
10133+
[3.0, 4.0, 5.0]],
10134+
[[6.0, 7.0, 8.0],
10135+
[9.0, 10.0, 11.0]],
10136+
]
10137+
10138+
axes = [1,-1]
10139+
10140+
output = [
10141+
[[5.0, 4.0, 3.0],
10142+
[2.0, 1.0, 0.0]],
10143+
[[11.0, 10.0, 9.0],
10144+
[8.0, 7.0, 6.0]],
10145+
]
10146+
10147+
#### Version
10148+
10149+
This version of the operator has been available since version 10 of the default ONNX operator set.
10150+
10151+
#### Attributes
10152+
10153+
<dl>
10154+
<dt><tt>axes</tt> : list of ints</dt>
10155+
<dd>A list of integers indicating the axes to reverse. Negative values mean counting dimensions from the back.By default, reverse over all axes of input dimensions</dd>
10156+
</dl>
10157+
10158+
#### Inputs
10159+
10160+
<dl>
10161+
<dt><tt>input</tt> : T</dt>
10162+
<dd>Tensor of rank r >= 1.</dd>
10163+
</dl>
10164+
10165+
#### Outputs
10166+
10167+
<dl>
10168+
<dt><tt>output</tt> : T</dt>
10169+
<dd>Tensor of rank r >= 1 (same rank as input).</dd>
10170+
</dl>
10171+
10172+
#### Type Constraints
10173+
10174+
<dl>
10175+
<dt><tt>T</tt> : tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(float16), tensor(float), tensor(double), tensor(string), tensor(bool), tensor(complex64), tensor(complex128)</dt>
10176+
<dd>Input and output types can be of any tensor type.</dd>
10177+
</dl>
10178+
10179+
10180+
#### Examples
10181+
10182+
<details>
10183+
<summary>default</summary>
10184+
10185+
```python
10186+
input = np.arange(6.0).reshape(2, 3)
10187+
10188+
node = onnx.helper.make_node(
10189+
'Reverse',
10190+
inputs=['input'],
10191+
outputs=['output']
10192+
)
10193+
10194+
output = np.flip(np.flip(input, 0), 1)
10195+
10196+
expect(node, inputs=[input], outputs=[output],
10197+
name='test_reverse_default')
10198+
```
10199+
10200+
</details>
10201+
10202+
10203+
<details>
10204+
<summary>with_axes</summary>
10205+
10206+
```python
10207+
input = np.arange(6.0).reshape(2, 3)
10208+
10209+
node = onnx.helper.make_node(
10210+
'Reverse',
10211+
inputs=['input'],
10212+
outputs=['output'],
10213+
axes=[0]
10214+
)
10215+
10216+
output = np.flip(input, 0)
10217+
10218+
expect(node, inputs=[input], outputs=[output],
10219+
name='test_reverse_with_axes')
10220+
```
10221+
10222+
</details>
10223+
10224+
10225+
<details>
10226+
<summary>with_negative_axes</summary>
10227+
10228+
```python
10229+
input = np.arange(12.0).reshape(2, 2, 3)
10230+
10231+
node = onnx.helper.make_node(
10232+
'Reverse',
10233+
inputs=['input'],
10234+
outputs=['output'],
10235+
axes=[1, -1]
10236+
)
10237+
10238+
output = np.flip(np.flip(input, 1), -1)
10239+
10240+
expect(node, inputs=[input], outputs=[output],
10241+
name='test_reverse_with_negative_axes')
10242+
```
10243+
10244+
</details>
10245+
10246+
1010110247
### <a name="Scan"></a><a name="scan">**Scan**</a>
1010210248

1010310249
Scan can be used to iterate over one or more scan_input tensors,

docs/TestCoverage.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* [Overall Test Coverage](#overall-test-coverage)
66
# Node Test Coverage
77
## Summary
8-
Node tests have covered 114/121 (94.21%, 5 generators excluded) common operators.
8+
Node tests have covered 115/122 (94.26%, 5 generators excluded) common operators.
99

1010
Node tests have covered 0/4 (0.00%, 0 generators excluded) experimental operators.
1111

@@ -5391,6 +5391,69 @@ expect(node, inputs=[data, scales], outputs=[output],
53915391
</details>
53925392

53935393

5394+
### Reverse
5395+
There are 3 test cases, listed as following:
5396+
<details>
5397+
<summary>default</summary>
5398+
5399+
```python
5400+
input = np.arange(6.0).reshape(2, 3)
5401+
5402+
node = onnx.helper.make_node(
5403+
'Reverse',
5404+
inputs=['input'],
5405+
outputs=['output']
5406+
)
5407+
5408+
output = np.flip(np.flip(input, 0), 1)
5409+
5410+
expect(node, inputs=[input], outputs=[output],
5411+
name='test_reverse_default')
5412+
```
5413+
5414+
</details>
5415+
<details>
5416+
<summary>with_axes</summary>
5417+
5418+
```python
5419+
input = np.arange(6.0).reshape(2, 3)
5420+
5421+
node = onnx.helper.make_node(
5422+
'Reverse',
5423+
inputs=['input'],
5424+
outputs=['output'],
5425+
axes=[0]
5426+
)
5427+
5428+
output = np.flip(input, 0)
5429+
5430+
expect(node, inputs=[input], outputs=[output],
5431+
name='test_reverse_with_axes')
5432+
```
5433+
5434+
</details>
5435+
<details>
5436+
<summary>with_negative_axes</summary>
5437+
5438+
```python
5439+
input = np.arange(12.0).reshape(2, 2, 3)
5440+
5441+
node = onnx.helper.make_node(
5442+
'Reverse',
5443+
inputs=['input'],
5444+
outputs=['output'],
5445+
axes=[1, -1]
5446+
)
5447+
5448+
output = np.flip(np.flip(input, 1), -1)
5449+
5450+
expect(node, inputs=[input], outputs=[output],
5451+
name='test_reverse_with_negative_axes')
5452+
```
5453+
5454+
</details>
5455+
5456+
53945457
### Scan
53955458
There are 2 test cases, listed as following:
53965459
<details>
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
from __future__ import unicode_literals
5+
6+
import numpy as np # type: ignore
7+
8+
import onnx
9+
from ..base import Base
10+
from . import expect
11+
12+
13+
class Reverse(Base):
14+
15+
@staticmethod
16+
def export_default(): # type: () -> None
17+
input = np.arange(6.0).reshape(2, 3)
18+
19+
node = onnx.helper.make_node(
20+
'Reverse',
21+
inputs=['input'],
22+
outputs=['output']
23+
)
24+
25+
output = np.flip(np.flip(input, 0), 1)
26+
27+
expect(node, inputs=[input], outputs=[output],
28+
name='test_reverse_default')
29+
30+
@staticmethod
31+
def export_with_axes(): # type: () -> None
32+
input = np.arange(6.0).reshape(2, 3)
33+
34+
node = onnx.helper.make_node(
35+
'Reverse',
36+
inputs=['input'],
37+
outputs=['output'],
38+
axes=[0]
39+
)
40+
41+
output = np.flip(input, 0)
42+
43+
expect(node, inputs=[input], outputs=[output],
44+
name='test_reverse_with_axes')
45+
46+
@staticmethod
47+
def export_with_negative_axes(): # type: () -> None
48+
input = np.arange(12.0).reshape(2, 2, 3)
49+
50+
node = onnx.helper.make_node(
51+
'Reverse',
52+
inputs=['input'],
53+
outputs=['output'],
54+
axes=[1, -1]
55+
)
56+
57+
output = np.flip(np.flip(input, 1), -1)
58+
59+
expect(node, inputs=[input], outputs=[output],
60+
name='test_reverse_with_negative_axes')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
 backend-test:c
2+

3+
inputoutput"Reversetest_reverse_defaultZ
4+
input
5+
 
6+

7+
b
8+
output
9+
 
10+

11+
B
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)