Skip to content

Commit ea1aa13

Browse files
liqunfulinkerzhang
authored andcommitted
add tests for reduce ops (onnx#675)
* add tests for reduce ops * add tests for reduce ops, fix formatting * add tests for reduce ops, fix formatting * update reduce_l2 for comments * append docs and a few fixes * fixe formatting * more fixes * more update * fix build * remove extra white spaces * fix reduce_sum * fix reduce_sum test failure * remove test pb files as they need to be updated * verifying tests * formatting * formatting * formatting * update according to reviewer's comments * update changelog and operators doc after merging from master * remove white spaces * remove unneeded test data * remove spaces that causing build failure * extra spaces * generate doc * regenerate doc * regenerate docs
1 parent 5dd68e6 commit ea1aa13

File tree

172 files changed

+2448
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+2448
-0
lines changed

docs/Operators.md

+963
Large diffs are not rendered by default.
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 ReduceL1(Base):
14+
15+
@staticmethod
16+
def export_do_not_keepdims():
17+
shape = [3, 2, 2]
18+
axes = [2]
19+
keepdims = 0
20+
21+
node = onnx.helper.make_node(
22+
'ReduceL1',
23+
inputs=['data'],
24+
outputs=['reduced'],
25+
axes=axes,
26+
keepdims=keepdims
27+
)
28+
29+
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
30+
#print(data)
31+
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
32+
33+
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
34+
#print(reduced)
35+
#[[3., 7.], [11., 15.], [19., 23.]]
36+
37+
expect(node, inputs=[data], outputs=[reduced],
38+
name='test_reduce_l1_do_not_keepdims_example')
39+
40+
np.random.seed(0)
41+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
42+
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
43+
44+
expect(node, inputs=[data], outputs=[reduced],
45+
name='test_reduce_l1_do_not_keepdims_random')
46+
47+
@staticmethod
48+
def export_keepdims():
49+
shape = [3, 2, 2]
50+
axes = [2]
51+
keepdims = 1
52+
53+
node = onnx.helper.make_node(
54+
'ReduceL1',
55+
inputs=['data'],
56+
outputs=['reduced'],
57+
axes=axes,
58+
keepdims=keepdims
59+
)
60+
61+
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
62+
#print(data)
63+
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
64+
65+
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
66+
#print(reduced)
67+
#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]
68+
69+
expect(node, inputs=[data], outputs=[reduced],
70+
name='test_reduce_l1_keep_dims_example')
71+
72+
np.random.seed(0)
73+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
74+
reduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)
75+
76+
expect(node, inputs=[data], outputs=[reduced],
77+
name='test_reduce_l1_keep_dims_random')
78+
79+
@staticmethod
80+
def export_default_axes_keepdims():
81+
shape = [3, 2, 2]
82+
axes = None
83+
keepdims = 1
84+
85+
node = onnx.helper.make_node(
86+
'ReduceL1',
87+
inputs=['data'],
88+
outputs=['reduced'],
89+
keepdims=keepdims
90+
)
91+
92+
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
93+
#print(data)
94+
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
95+
96+
reduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)
97+
#print(reduced)
98+
#[[[78.]]]
99+
100+
expect(node, inputs=[data], outputs=[reduced],
101+
name='test_reduce_l1_default_axes_keepdims_example')
102+
103+
np.random.seed(0)
104+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
105+
reduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)
106+
107+
expect(node, inputs=[data], outputs=[reduced],
108+
name='test_reduce_l1_default_axes_keepdims_random')
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 ReduceL2(Base):
14+
15+
@staticmethod
16+
def export_do_not_keepdims():
17+
shape = [3, 2, 2]
18+
axes = [2]
19+
keepdims = 0
20+
21+
node = onnx.helper.make_node(
22+
'ReduceL2',
23+
inputs=['data'],
24+
outputs=['reduced'],
25+
axes=axes,
26+
keepdims=keepdims
27+
)
28+
29+
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
30+
#print(data)
31+
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
32+
33+
reduced = np.sqrt(np.sum(
34+
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
35+
#print(reduced)
36+
#[[2.23606798, 5.],
37+
# [7.81024968, 10.63014581],
38+
# [13.45362405, 16.2788206]]
39+
40+
expect(node, inputs=[data], outputs=[reduced],
41+
name='test_reduce_l2_do_not_keepdims_example')
42+
43+
np.random.seed(0)
44+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
45+
reduced = np.sqrt(np.sum(
46+
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
47+
48+
expect(node, inputs=[data], outputs=[reduced],
49+
name='test_reduce_l2_do_not_keepdims_random')
50+
51+
@staticmethod
52+
def export_keepdims():
53+
shape = [3, 2, 2]
54+
axes = [2]
55+
keepdims = 1
56+
57+
node = onnx.helper.make_node(
58+
'ReduceL2',
59+
inputs=['data'],
60+
outputs=['reduced'],
61+
axes=axes,
62+
keepdims=keepdims
63+
)
64+
65+
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
66+
#print(data)
67+
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
68+
69+
reduced = np.sqrt(np.sum(
70+
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
71+
#print(reduced)
72+
#[[[2.23606798], [5.]]
73+
# [[7.81024968], [10.63014581]]
74+
# [[13.45362405], [16.2788206 ]]]
75+
76+
expect(node, inputs=[data], outputs=[reduced],
77+
name='test_reduce_l2_keep_dims_example')
78+
79+
np.random.seed(0)
80+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
81+
reduced = np.sqrt(np.sum(
82+
a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))
83+
84+
expect(node, inputs=[data], outputs=[reduced], name='test_reduce_l2_keep_dims_random')
85+
86+
@staticmethod
87+
def export_default_axes_keepdims():
88+
shape = [3, 2, 2]
89+
axes = None
90+
keepdims = 1
91+
92+
node = onnx.helper.make_node(
93+
'ReduceL2',
94+
inputs=['data'],
95+
outputs=['reduced'],
96+
keepdims=keepdims
97+
)
98+
99+
data = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)
100+
#print(data)
101+
#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]
102+
103+
reduced = np.sqrt(np.sum(
104+
a=np.square(data), axis=axes, keepdims=keepdims == 1))
105+
#print(reduced)
106+
#[[[25.49509757]]]
107+
108+
expect(node, inputs=[data], outputs=[reduced],
109+
name='test_reduce_l2_default_axes_keepdims_example')
110+
111+
np.random.seed(0)
112+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
113+
reduced = np.sqrt(np.sum(
114+
a=np.square(data), axis=axes, keepdims=keepdims == 1))
115+
116+
expect(node, inputs=[data], outputs=[reduced],
117+
name='test_reduce_l2_default_axes_keepdims_random')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 ReduceLogSumExp(Base):
14+
15+
@staticmethod
16+
def export_do_not_keepdims():
17+
shape = [3, 2, 2]
18+
axes = [1]
19+
keepdims = 0
20+
node = onnx.helper.make_node(
21+
'ReduceLogSumExp',
22+
inputs=['data'],
23+
outputs=['reduced'],
24+
axes=axes,
25+
keepdims=keepdims
26+
)
27+
28+
data = np.array(
29+
[[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],
30+
dtype=np.float32)
31+
reduced = np.log(np.sum(
32+
np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))
33+
# print(reduced)
34+
#[[20., 2.31326175]
35+
# [40.00004578, 2.31326175]
36+
# [60.00671387, 2.31326175]]
37+
38+
expect(node, inputs=[data], outputs=[reduced],
39+
name='test_reduce_log_sum_exp_do_not_keepdims_example')
40+
41+
np.random.seed(0)
42+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
43+
reduced = np.log(np.sum(
44+
np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))
45+
46+
expect(node, inputs=[data], outputs=[reduced],
47+
name='test_reduce_log_sum_exp_do_not_keepdims_random')
48+
49+
@staticmethod
50+
def export_keepdims():
51+
shape = [3, 2, 2]
52+
axes = [1]
53+
keepdims = 1
54+
node = onnx.helper.make_node(
55+
'ReduceLogSumExp',
56+
inputs=['data'],
57+
outputs=['reduced'],
58+
axes=axes,
59+
keepdims=keepdims
60+
)
61+
62+
data = np.array(
63+
[[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],
64+
dtype=np.float32)
65+
reduced = np.log(np.sum(np.exp(data),
66+
axis=tuple(axes),
67+
keepdims=keepdims == 1))
68+
# print(reduced)
69+
# [[[20., 2.31326175]]
70+
# [[40.00004578, 2.31326175]]
71+
# [[60.00671387, 2.31326175]]]
72+
73+
expect(node, inputs=[data], outputs=[reduced],
74+
name='test_reduce_log_sum_exp_keepdims_example')
75+
76+
np.random.seed(0)
77+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
78+
reduced = np.log(np.sum(np.exp(data),
79+
axis=tuple(axes),
80+
keepdims=keepdims == 1))
81+
82+
expect(node, inputs=[data], outputs=[reduced],
83+
name='test_reduce_log_sum_exp_keepdims_random')
84+
85+
@staticmethod
86+
def export_default_axes_keepdims():
87+
shape = [3, 2, 2]
88+
axes = None
89+
keepdims = 1
90+
91+
node = onnx.helper.make_node(
92+
'ReduceLogSumExp',
93+
inputs=['data'],
94+
outputs=['reduced'],
95+
keepdims=keepdims
96+
)
97+
98+
data = np.array(
99+
[[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],
100+
dtype=np.float32)
101+
reduced = np.log(np.sum(np.exp(data),
102+
axis=axes,
103+
keepdims=keepdims == 1))
104+
# print(reduced)
105+
# [[[60.00671387]]]
106+
107+
expect(node, inputs=[data], outputs=[reduced],
108+
name='test_reduce_log_sum_exp_default_axes_keepdims_example')
109+
110+
np.random.seed(0)
111+
data = np.random.uniform(-10, 10, shape).astype(np.float32)
112+
reduced = np.log(np.sum(np.exp(data),
113+
axis=axes,
114+
keepdims=keepdims == 1))
115+
expect(node, inputs=[data], outputs=[reduced],
116+
name='test_reduce_log_sum_exp_default_axes_keepdims_random')

0 commit comments

Comments
 (0)