Skip to content

Commit ab8aaf9

Browse files
authored
Add scan test case (#1586)
* add scan test case * add scan-output to scan test case * run document generator to update doc * Update Changelog.md * Update Changelog.md * Update Operators.md * Undo changes in min/max value produced by doc generator * Updated test coverage * merge master and regenerate test coverage
1 parent c95357e commit ab8aaf9

8 files changed

Lines changed: 171 additions & 4 deletions

File tree

‎docs/Operators.md‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9726,6 +9726,61 @@ This version of the operator has been available since version 8 of the default O
97269726
</dl>
97279727

97289728

9729+
#### Examples
9730+
9731+
<details>
9732+
<summary>scan</summary>
9733+
9734+
```python
9735+
# Given an input sequence [x1, ..., xN], sum up its elements using a scan
9736+
# returning the final state (x1+x2+...+xN) as well the scan_output
9737+
# [x1, x1+x2, ..., x1+x2+...+xN]
9738+
#
9739+
# create graph to represent scan body
9740+
sum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])
9741+
next = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])
9742+
sum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])
9743+
scan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])
9744+
add_node = onnx.helper.make_node(
9745+
'Add',
9746+
inputs=['sum_in', 'next'],
9747+
outputs=['sum_out']
9748+
)
9749+
id_node = onnx.helper.make_node(
9750+
'Identity',
9751+
inputs=['sum_out'],
9752+
outputs=['scan_out']
9753+
)
9754+
scan_body = onnx.helper.make_graph(
9755+
[add_node, id_node],
9756+
'scan_body',
9757+
[sum_in, next],
9758+
[sum_out, scan_out]
9759+
)
9760+
# create scan op node
9761+
no_sequence_lens = '' # optional input, not supplied
9762+
node = onnx.helper.make_node(
9763+
'Scan',
9764+
inputs=[no_sequence_lens, 'initial', 'x'],
9765+
outputs=['y', 'z'],
9766+
num_scan_inputs=1,
9767+
body=scan_body
9768+
)
9769+
# create inputs for batch-size 1, sequence-length 3, inner dimension 2
9770+
initial = np.array([0, 0]).astype(np.float32).reshape((1, 2))
9771+
x = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((1, 3, 2))
9772+
# final state computed = [1 + 3 + 5, 2 + 4 + 6]
9773+
y = np.array([9, 12]).astype(np.float32).reshape((1, 2))
9774+
# scan-output computed
9775+
z = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((1, 3, 2))
9776+
9777+
expect(node, inputs=[initial, x], outputs=[y, z],
9778+
name='test_scan_sum')
9779+
```
9780+
9781+
</details>
9782+
9783+
97299784
### <a name="Selu"></a><a name="selu">**Selu**</a>
97309785

97319786
Selu takes one input data (Tensor<T>) and produces one output data

‎docs/TestCoverage.md‎

Lines changed: 56 additions & 4 deletions
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 101/109 (92.66%, 5 generators excluded) common operators.
8+
Node tests have covered 102/109 (93.58%, 5 generators excluded) common operators.
99

1010
Node tests have covered 2/12 (16.67%, 0 generators excluded) experimental operators.
1111

@@ -5088,6 +5088,61 @@ for test_name, shape in test_cases.items():
50885088
</details>
50895089

50905090

5091+
### Scan
5092+
There are 1 test cases, listed as following:
5093+
<details>
5094+
<summary>scan</summary>
5095+
5096+
```python
5097+
# Given an input sequence [x1, ..., xN], sum up its elements using a scan
5098+
# returning the final state (x1+x2+...+xN) as well the scan_output
5099+
# [x1, x1+x2, ..., x1+x2+...+xN]
5100+
#
5101+
# create graph to represent scan body
5102+
sum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])
5103+
next = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])
5104+
sum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])
5105+
scan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])
5106+
add_node = onnx.helper.make_node(
5107+
'Add',
5108+
inputs=['sum_in', 'next'],
5109+
outputs=['sum_out']
5110+
)
5111+
id_node = onnx.helper.make_node(
5112+
'Identity',
5113+
inputs=['sum_out'],
5114+
outputs=['scan_out']
5115+
)
5116+
scan_body = onnx.helper.make_graph(
5117+
[add_node, id_node],
5118+
'scan_body',
5119+
[sum_in, next],
5120+
[sum_out, scan_out]
5121+
)
5122+
# create scan op node
5123+
no_sequence_lens = '' # optional input, not supplied
5124+
node = onnx.helper.make_node(
5125+
'Scan',
5126+
inputs=[no_sequence_lens, 'initial', 'x'],
5127+
outputs=['y', 'z'],
5128+
num_scan_inputs=1,
5129+
body=scan_body
5130+
)
5131+
# create inputs for batch-size 1, sequence-length 3, inner dimension 2
5132+
initial = np.array([0, 0]).astype(np.float32).reshape((1, 2))
5133+
x = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((1, 3, 2))
5134+
# final state computed = [1 + 3 + 5, 2 + 4 + 6]
5135+
y = np.array([9, 12]).astype(np.float32).reshape((1, 2))
5136+
# scan-output computed
5137+
z = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((1, 3, 2))
5138+
5139+
expect(node, inputs=[initial, x], outputs=[y, z],
5140+
name='test_scan_sum')
5141+
```
5142+
5143+
</details>
5144+
5145+
50915146
### Selu
50925147
There are 2 test cases, listed as following:
50935148
<details>
@@ -6116,9 +6171,6 @@ expect(node, inputs=[x, y], outputs=[z],
61166171
### RandomUniformLike (random generator operator)
61176172

61186173

6119-
### Scan (call for test cases)
6120-
6121-
61226174
### SpaceToDepth (call for test cases)
61236175

61246176

Lines changed: 60 additions & 0 deletions
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 Scan(Base):
14+
15+
@staticmethod
16+
def export(): # type: () -> None
17+
# Given an input sequence [x1, ..., xN], sum up its elements using a scan
18+
# returning the final state (x1+x2+...+xN) as well the scan_output
19+
# [x1, x1+x2, ..., x1+x2+...+xN]
20+
#
21+
# create graph to represent scan body
22+
sum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])
23+
next = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])
24+
sum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])
25+
scan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])
26+
add_node = onnx.helper.make_node(
27+
'Add',
28+
inputs=['sum_in', 'next'],
29+
outputs=['sum_out']
30+
)
31+
id_node = onnx.helper.make_node(
32+
'Identity',
33+
inputs=['sum_out'],
34+
outputs=['scan_out']
35+
)
36+
scan_body = onnx.helper.make_graph(
37+
[add_node, id_node],
38+
'scan_body',
39+
[sum_in, next],
40+
[sum_out, scan_out]
41+
)
42+
# create scan op node
43+
no_sequence_lens = '' # optional input, not supplied
44+
node = onnx.helper.make_node(
45+
'Scan',
46+
inputs=[no_sequence_lens, 'initial', 'x'],
47+
outputs=['y', 'z'],
48+
num_scan_inputs=1,
49+
body=scan_body
50+
)
51+
# create inputs for batch-size 1, sequence-length 3, inner dimension 2
52+
initial = np.array([0, 0]).astype(np.float32).reshape((1, 2))
53+
x = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((1, 3, 2))
54+
# final state computed = [1 + 3 + 5, 2 + 4 + 6]
55+
y = np.array([9, 12]).astype(np.float32).reshape((1, 2))
56+
# scan-output computed
57+
z = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((1, 3, 2))
58+
59+
expect(node, inputs=[initial, x], outputs=[y, z],
60+
name='test_scan_sum')
365 Bytes
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)