Skip to content

Commit c8a8b7c

Browse files
hariharans29ebarsoum
authored andcommitted
Support Range operator in ONNX (#2242)
Range operators was reviewed in #2087
1 parent 44b0d6d commit c8a8b7c

File tree

30 files changed

+544
-23
lines changed

30 files changed

+544
-23
lines changed

docs/Changelog.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10841,6 +10841,67 @@ This version of the operator has been available since version 11 of the default
1084110841
<dd>tensor of bool, which should be a scalar.</dd>
1084210842
</dl>
1084310843

10844+
### <a name="Range-11"></a>**Range-11**</a>
10845+
10846+
Generate a tensor containing a sequence of numbers that begin at `start` and extends by increments of `delta`
10847+
up to `limit` (exclusive).
10848+
10849+
The number of elements in the output of range is computed as below-
10850+
10851+
`number_of_elements = max( ceil( (limit - start) / delta ) , 0 )`
10852+
10853+
The pseudocode determining the contents of the output is shown below-
10854+
10855+
`for(int i=0; i<number_of_elements; ++i)`
10856+
10857+
`{`
10858+
10859+
` output[i] = start + (i * delta); `
10860+
10861+
`}`
10862+
10863+
`Example 1`
10864+
Inputs: start = 3, limit = 9, delta = 3
10865+
Output: [3, 6]
10866+
10867+
`Example 2`
10868+
Inputs: start = 10, limit = 4, delta = -2
10869+
Output: [10, 8, 6]
10870+
10871+
10872+
#### Version
10873+
10874+
This version of the operator has been available since version 11 of the default ONNX operator set.
10875+
10876+
#### Inputs
10877+
10878+
<dl>
10879+
<dt><tt>start</tt> : T</dt>
10880+
<dd>Scalar. First entry for the range of output values.</dd>
10881+
<dt><tt>limit</tt> : T</dt>
10882+
<dd>Scalar. Exclusive upper limit for the range of output values.</dd>
10883+
<dt><tt>delta</tt> : T</dt>
10884+
<dd>Scalar. Value to step by.</dd>
10885+
</dl>
10886+
10887+
#### Outputs
10888+
10889+
<dl>
10890+
<dt><tt>output</tt> : T</dt>
10891+
<dd>A 1-D tensor with same type as the inputs containing generated range of values.</dd>
10892+
</dl>
10893+
10894+
#### Type Constraints
10895+
10896+
<dl>
10897+
<dt><tt>T</tt> : tensor(float), tensor(double), tensor(int16), tensor(int32), tensor(int64)</dt>
10898+
<dd>Constrain input types to common numeric type tensors.</dd>
10899+
</dl>
10900+
10901+
#### Function
10902+
10903+
The Function can be represented as a function.
10904+
1084410905
### <a name="Resize-11"></a>**Resize-11**</a>
1084510906

1084610907
Resize the input tensor. In general, it calculates every value in the output tensor as a weighted average of neighborhood (a.k.a. sampling locations) in the input tensor.

docs/Operators.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
**Operators with function registered:**
151151
* <a href="#DynamicQuantizeLinear">DynamicQuantizeLinear</a>
152152
* <a href="#MeanVarianceNormalization">MeanVarianceNormalization</a>
153+
* <a href="#Range">Range</a>
153154

154155
## ai.onnx (default)
155156
### <a name="Abs"></a><a name="abs">**Abs**</a>
@@ -10350,6 +10351,114 @@ This version of the operator has been available since version 1 of the default O
1035010351
</dl>
1035110352

1035210353

10354+
### <a name="Range"></a><a name="range">**Range**</a>
10355+
10356+
Generate a tensor containing a sequence of numbers that begin at `start` and extends by increments of `delta`
10357+
up to `limit` (exclusive).
10358+
10359+
The number of elements in the output of range is computed as below-
10360+
10361+
`number_of_elements = max( ceil( (limit - start) / delta ) , 0 )`
10362+
10363+
The pseudocode determining the contents of the output is shown below-
10364+
10365+
`for(int i=0; i<number_of_elements; ++i)`
10366+
10367+
`{`
10368+
10369+
` output[i] = start + (i * delta); `
10370+
10371+
`}`
10372+
10373+
`Example 1`
10374+
Inputs: start = 3, limit = 9, delta = 3
10375+
Output: [3, 6]
10376+
10377+
`Example 2`
10378+
Inputs: start = 10, limit = 4, delta = -2
10379+
Output: [10, 8, 6]
10380+
10381+
10382+
#### Version
10383+
10384+
This version of the operator has been available since version 11 of the default ONNX operator set.
10385+
10386+
#### Inputs
10387+
10388+
<dl>
10389+
<dt><tt>start</tt> : T</dt>
10390+
<dd>Scalar. First entry for the range of output values.</dd>
10391+
<dt><tt>limit</tt> : T</dt>
10392+
<dd>Scalar. Exclusive upper limit for the range of output values.</dd>
10393+
<dt><tt>delta</tt> : T</dt>
10394+
<dd>Scalar. Value to step by.</dd>
10395+
</dl>
10396+
10397+
#### Outputs
10398+
10399+
<dl>
10400+
<dt><tt>output</tt> : T</dt>
10401+
<dd>A 1-D tensor with same type as the inputs containing generated range of values.</dd>
10402+
</dl>
10403+
10404+
#### Type Constraints
10405+
10406+
<dl>
10407+
<dt><tt>T</tt> : tensor(float), tensor(double), tensor(int16), tensor(int32), tensor(int64)</dt>
10408+
<dd>Constrain input types to common numeric type tensors.</dd>
10409+
</dl>
10410+
10411+
#### Function
10412+
10413+
The Function can be represented as a function.
10414+
10415+
10416+
#### Examples
10417+
10418+
<details>
10419+
<summary>range_float_type_positive_delta</summary>
10420+
10421+
```python
10422+
node = onnx.helper.make_node(
10423+
'Range',
10424+
inputs=['start', 'limit', 'delta'],
10425+
outputs=['output'],
10426+
)
10427+
10428+
start = np.float32(1)
10429+
limit = np.float32(5)
10430+
delta = np.float32(2)
10431+
10432+
output = np.arange(start, limit, delta, dtype=np.float32) # expected output [1.0, 3.0]
10433+
expect(node, inputs=[start, limit, delta], outputs=[output],
10434+
name='test_range_float_type_positive_delta')
10435+
```
10436+
10437+
</details>
10438+
10439+
10440+
<details>
10441+
<summary>range_int32_type_negative_delta</summary>
10442+
10443+
```python
10444+
node = onnx.helper.make_node(
10445+
'Range',
10446+
inputs=['start', 'limit', 'delta'],
10447+
outputs=['output'],
10448+
)
10449+
10450+
start = np.int32(10)
10451+
limit = np.int32(6)
10452+
delta = np.int32(-3)
10453+
10454+
output = np.arange(start, limit, delta, dtype=np.int32) # expected output [10, 7]
10455+
expect(node, inputs=[start, limit, delta], outputs=[output],
10456+
name='test_range_int32_type_negative_delta')
10457+
```
10458+
10459+
</details>
10460+
10461+
1035310462
### <a name="Reciprocal"></a><a name="reciprocal">**Reciprocal**</a>
1035410463

1035510464
Reciprocal takes one input data (Tensor<T>) and produces one output data

docs/TestCoverage.md

Lines changed: 45 additions & 1 deletion
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 132/139 (94.96%, 5 generators excluded) common operators.
8+
Node tests have covered 133/140 (95.00%, 5 generators excluded) common operators.
99

1010
Node tests have covered 0/0 (N/A) experimental operators.
1111

@@ -5450,6 +5450,50 @@ expect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='te
54505450
</details>
54515451

54525452

5453+
### Range
5454+
There are 2 test cases, listed as following:
5455+
<details>
5456+
<summary>range_float_type_positive_delta</summary>
5457+
5458+
```python
5459+
node = onnx.helper.make_node(
5460+
'Range',
5461+
inputs=['start', 'limit', 'delta'],
5462+
outputs=['output'],
5463+
)
5464+
5465+
start = np.float32(1)
5466+
limit = np.float32(5)
5467+
delta = np.float32(2)
5468+
5469+
output = np.arange(start, limit, delta, dtype=np.float32) # expected output [1.0, 3.0]
5470+
expect(node, inputs=[start, limit, delta], outputs=[output],
5471+
name='test_range_float_type_positive_delta')
5472+
```
5473+
5474+
</details>
5475+
<details>
5476+
<summary>range_int32_type_negative_delta</summary>
5477+
5478+
```python
5479+
node = onnx.helper.make_node(
5480+
'Range',
5481+
inputs=['start', 'limit', 'delta'],
5482+
outputs=['output'],
5483+
)
5484+
5485+
start = np.int32(10)
5486+
limit = np.int32(6)
5487+
delta = np.int32(-3)
5488+
5489+
output = np.arange(start, limit, delta, dtype=np.int32) # expected output [10, 7]
5490+
expect(node, inputs=[start, limit, delta], outputs=[output],
5491+
name='test_range_int32_type_negative_delta')
5492+
```
5493+
5494+
</details>
5495+
5496+
54535497
### Reciprocal
54545498
There are 1 test cases, listed as following:
54555499
<details>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 Range(Base):
14+
15+
@staticmethod
16+
def export_range_float_type_positive_delta(): # type: () -> None
17+
node = onnx.helper.make_node(
18+
'Range',
19+
inputs=['start', 'limit', 'delta'],
20+
outputs=['output'],
21+
)
22+
23+
start = np.float32(1)
24+
limit = np.float32(5)
25+
delta = np.float32(2)
26+
27+
output = np.arange(start, limit, delta, dtype=np.float32) # expected output [1.0, 3.0]
28+
expect(node, inputs=[start, limit, delta], outputs=[output],
29+
name='test_range_float_type_positive_delta')
30+
31+
@staticmethod
32+
def export_range_int32_type_negative_delta(): # type: () -> None
33+
node = onnx.helper.make_node(
34+
'Range',
35+
inputs=['start', 'limit', 'delta'],
36+
outputs=['output'],
37+
)
38+
39+
start = np.int32(10)
40+
limit = np.int32(6)
41+
delta = np.int32(-3)
42+
43+
output = np.arange(start, limit, delta, dtype=np.int32) # expected output [10, 7]
44+
expect(node, inputs=[start, limit, delta], outputs=[output],
45+
name='test_range_int32_type_negative_delta')
Binary file not shown.
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)