@@ -21,15 +21,17 @@ class TestElementwiseAddOp(OpTest):
21
21
def setUp (self ):
22
22
self .op_type = "elementwise_add"
23
23
self .dtype = np .float32
24
- init_dtype ()
24
+ self .axis = - 1
25
+ self .init_dtype ()
26
+ self .init_input_output ()
27
+ self .init_axis ()
25
28
26
- x = np .random .uniform (0.1 , 1 , [13 , 17 ]).astype (self .dtype )
27
- y = np .random .uniform (0.1 , 1 , [13 , 17 ]).astype (self .dtype )
28
29
self .inputs = {
29
- 'X' : OpTest .np_dtype_to_fluid_dtype (x ),
30
- 'Y' : OpTest .np_dtype_to_fluid_dtype (y )
30
+ 'X' : OpTest .np_dtype_to_fluid_dtype (self . x ),
31
+ 'Y' : OpTest .np_dtype_to_fluid_dtype (self . y )
31
32
}
32
- self .outputs = {'Out' : np .add (x , y )}
33
+ self .attrs = {'axis' : self .axis }
34
+ self .outputs = {'Out' : self .out }
33
35
34
36
def test_check_output (self ):
35
37
self .check_output ()
@@ -51,12 +53,20 @@ def test_check_grad_ingore_y(self):
51
53
self .check_grad (
52
54
['X' ], 'Out' , max_relative_error = 0.005 , no_grad_set = set ('Y' ))
53
55
54
- def init_dtype ():
56
+ def init_input_output (self ):
57
+ self .x = np .random .uniform (0.1 , 1 , [13 , 17 ]).astype (self .dtype )
58
+ self .y = np .random .uniform (0.1 , 1 , [13 , 17 ]).astype (self .dtype )
59
+ self .out = np .add (self .x , self .y )
60
+
61
+ def init_dtype (self ):
62
+ pass
63
+
64
+ def init_axis (self ):
55
65
pass
56
66
57
67
58
68
class TestFP16ElementwiseAddOp (TestElementwiseAddOp ):
59
- def init_dtype ():
69
+ def init_dtype (self ):
60
70
self .dtype = np .float16
61
71
62
72
def test_check_output (self ):
@@ -67,130 +77,179 @@ def test_check_output(self):
67
77
68
78
69
79
class TestElementwiseAddOp_scalar (TestElementwiseAddOp ):
70
- def setUp (self ):
71
- self .op_type = "elementwise_add"
72
- self .inputs = {
73
- 'X' : np .random .rand (2 , 3 , 4 ).astype (np .float32 ),
74
- 'Y' : np .random .rand (1 ).astype (np .float32 )
75
- }
76
- self .outputs = {'Out' : self .inputs ['X' ] + self .inputs ['Y' ]}
80
+ def init_input_output (self ):
81
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
82
+ self .y = np .random .rand (1 ).astype (self .dtype )
83
+ self .out = self .x + self .y
84
+
85
+
86
+ class TestFP16ElementwiseAddOp_scalar (TestFP16ElementwiseAddOp ):
87
+ def init_input_output (self ):
88
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
89
+ self .y = np .random .rand (1 ).astype (self .dtype )
90
+ self .out = self .x + self .y
77
91
78
92
79
93
class TestElementwiseAddOp_scalar2 (TestElementwiseAddOp ):
80
- def setUp (self ):
81
- self .op_type = "elementwise_add"
82
- self .inputs = {
83
- 'X' : np .random .rand (2 , 3 , 4 ).astype (np .float32 ),
84
- 'Y' : np .random .rand (1 , 1 ).astype (np .float32 )
85
- }
86
- self .outputs = {'Out' : self .inputs ['X' ] + self .inputs ['Y' ]}
94
+ def init_input_output (self ):
95
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
96
+ self .y = np .random .rand (1 , 1 ).astype (self .dtype )
97
+ self .out = self .x + self .y
98
+
99
+
100
+ class TestFP16ElementwiseAddOp_scalar2 (TestFP16ElementwiseAddOp ):
101
+ def init_input_output (self ):
102
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
103
+ self .y = np .random .rand (1 , 1 ).astype (self .dtype )
104
+ self .out = self .x + self .y
87
105
88
106
89
107
class TestElementwiseAddOp_Vector (TestElementwiseAddOp ):
90
- def setUp (self ):
91
- self .op_type = "elementwise_add"
92
- self .inputs = {
93
- 'X' : np .random .random ((32 , )).astype ("float32" ),
94
- 'Y' : np .random .random ((32 , )).astype ("float32" )
95
- }
96
- self .outputs = {'Out' : np .add (self .inputs ['X' ], self .inputs ['Y' ])}
108
+ def init_input_output (self ):
109
+ self .x = np .random .random ((32 , )).astype (self .dtype )
110
+ self .y = np .random .random ((32 , )).astype (self .dtype )
111
+ self .out = np .add (self .x , self .y )
112
+
113
+
114
+ class TestFP16ElementwiseAddOp_Vector (TestFP16ElementwiseAddOp ):
115
+ def init_input_output (self ):
116
+ self .x = np .random .random ((32 , )).astype (self .dtype )
117
+ self .y = np .random .random ((32 , )).astype (self .dtype )
118
+ self .out = np .add (self .x , self .y )
97
119
98
120
99
121
class TestElementwiseAddOp_broadcast_0 (TestElementwiseAddOp ):
100
- def setUp (self ):
101
- self .op_type = "elementwise_add"
102
- self .inputs = {
103
- 'X' : np .random .rand (2 , 3 , 4 ).astype (np .float32 ),
104
- 'Y' : np .random .rand (2 ).astype (np .float32 )
105
- }
122
+ def init_input_output (self ):
123
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
124
+ self .y = np .random .rand (2 ).astype (self .dtype )
125
+ self .out = self .x + self .y .reshape (2 , 1 , 1 )
106
126
107
- self .attrs = {'axis' : 0 }
108
- self .outputs = {
109
- 'Out' : self .inputs ['X' ] + self .inputs ['Y' ].reshape (2 , 1 , 1 )
110
- }
127
+ def init_axis (self ):
128
+ self .axis = 0
129
+
130
+
131
+ class TestFP16ElementwiseAddOp_broadcast_0 (TestFP16ElementwiseAddOp ):
132
+ def init_input_output (self ):
133
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
134
+ self .y = np .random .rand (2 ).astype (self .dtype )
135
+ self .out = self .x + self .y .reshape (2 , 1 , 1 )
136
+
137
+ def init_axis (self ):
138
+ self .axis = 0
111
139
112
140
113
141
class TestElementwiseAddOp_broadcast_1 (TestElementwiseAddOp ):
114
- def setUp (self ):
115
- self .op_type = "elementwise_add"
116
- self .inputs = {
117
- 'X' : np .random .rand (2 , 3 , 4 ).astype (np .float32 ),
118
- 'Y' : np .random .rand (3 ).astype (np .float32 )
119
- }
142
+ def init_input_output (self ):
143
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
144
+ self .y = np .random .rand (3 ).astype (self .dtype )
145
+ self .out = self .x + self .y .reshape (1 , 3 , 1 )
120
146
121
- self .attrs = {'axis' : 1 }
122
- self .outputs = {
123
- 'Out' : self .inputs ['X' ] + self .inputs ['Y' ].reshape (1 , 3 , 1 )
124
- }
147
+ def init_axis (self ):
148
+ self .axis = 1
149
+
150
+
151
+ class TestFP16ElementwiseAddOp_broadcast_1 (TestFP16ElementwiseAddOp ):
152
+ def init_input_output (self ):
153
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
154
+ self .y = np .random .rand (3 ).astype (self .dtype )
155
+ self .out = self .x + self .y .reshape (1 , 3 , 1 )
156
+
157
+ def init_axis (self ):
158
+ self .axis = 1
125
159
126
160
127
161
class TestElementwiseAddOp_broadcast_2 (TestElementwiseAddOp ):
128
- def setUp (self ):
129
- self .op_type = "elementwise_add"
130
- self .inputs = {
131
- 'X' : np .random .rand (2 , 3 , 4 ).astype (np .float32 ),
132
- 'Y' : np .random .rand (4 ).astype (np .float32 )
133
- }
162
+ def init_input_output (self ):
163
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
164
+ self .y = np .random .rand (4 ).astype (self .dtype )
165
+ self .out = self .x + self .y .reshape (1 , 1 , 4 )
134
166
135
- self .outputs = {
136
- 'Out' : self .inputs ['X' ] + self .inputs ['Y' ].reshape (1 , 1 , 4 )
137
- }
167
+
168
+ class TestFP16ElementwiseAddOp_broadcast_2 (TestFP16ElementwiseAddOp ):
169
+ def init_input_output (self ):
170
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
171
+ self .y = np .random .rand (4 ).astype (self .dtype )
172
+ self .out = self .x + self .y .reshape (1 , 1 , 4 )
138
173
139
174
140
175
class TestElementwiseAddOp_broadcast_3 (TestElementwiseAddOp ):
141
- def setUp (self ):
142
- self .op_type = "elementwise_add"
143
- self .inputs = {
144
- 'X' : np .random .rand (2 , 3 , 4 , 5 ).astype (np .float32 ),
145
- 'Y' : np .random .rand (3 , 4 ).astype (np .float32 )
146
- }
176
+ def init_input_output (self ):
177
+ self .x = np .random .rand (2 , 3 , 4 , 5 ).astype (self .dtype )
178
+ self .y = np .random .rand (3 , 4 ).astype (self .dtype )
179
+ self .out = self .x + self .y .reshape (1 , 3 , 4 , 1 )
147
180
148
- self .attrs = {'axis' : 1 }
149
- self .outputs = {
150
- 'Out' : self .inputs ['X' ] + self .inputs ['Y' ].reshape (1 , 3 , 4 , 1 )
151
- }
181
+ def init_axis (self ):
182
+ self .axis = 1
183
+
184
+
185
+ class TestFP16ElementwiseAddOp_broadcast_3 (TestFP16ElementwiseAddOp ):
186
+ def init_input_output (self ):
187
+ self .x = np .random .rand (2 , 3 , 4 , 5 ).astype (self .dtype )
188
+ self .y = np .random .rand (3 , 4 ).astype (self .dtype )
189
+ self .out = self .x + self .y .reshape (1 , 3 , 4 , 1 )
190
+
191
+ def init_axis (self ):
192
+ self .axis = 1
152
193
153
194
154
195
class TestElementwiseAddOp_broadcast_4 (TestElementwiseAddOp ):
155
- def setUp (self ):
156
- self .op_type = "elementwise_add"
157
- self .inputs = {
158
- 'X' : np .random .rand (2 , 3 , 4 , 5 ).astype (np .float32 ),
159
- 'Y' : np .random .rand (2 , 1 ).astype (np .float32 )
160
- }
196
+ def init_input_output (self ):
197
+ self .x = np .random .rand (2 , 3 , 4 , 5 ).astype (self .dtype )
198
+ self .y = np .random .rand (2 , 1 ).astype (self .dtype )
199
+ self .out = self .x + self .y .reshape (2 , 1 , 1 , 1 )
200
+
201
+ def init_axis (self ):
202
+ self .axis = 0
161
203
162
- self .attrs = {'axis' : 0 }
163
- self .outputs = {
164
- 'Out' : self .inputs ['X' ] + self .inputs ['Y' ].reshape (2 , 1 , 1 , 1 )
165
- }
204
+
205
+ class TestFP16ElementwiseAddOp_broadcast_4 (TestFP16ElementwiseAddOp ):
206
+ def init_input_output (self ):
207
+ self .x = np .random .rand (2 , 3 , 4 , 5 ).astype (self .dtype )
208
+ self .y = np .random .rand (2 , 1 ).astype (self .dtype )
209
+ self .out = self .x + self .y .reshape (2 , 1 , 1 , 1 )
210
+
211
+ def init_axis (self ):
212
+ self .axis = 0
166
213
167
214
168
215
class TestElementwiseAddOp_rowwise_add_0 (TestElementwiseAddOp ):
169
- def setUp (self ):
170
- self .op_type = "elementwise_add"
171
- self .inputs = {
172
- 'X' : np .random .rand (2 , 3 , 4 ).astype (np .float32 ),
173
- 'Y' : np .random .rand (3 , 4 ).astype (np .float32 )
174
- }
216
+ def init_input_output (self ):
217
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
218
+ self .y = np .random .rand (3 , 4 ).astype (self .dtype )
219
+ self .out = self .x + self .y .reshape (1 , 3 , 4 )
175
220
176
- self .attrs = {'axis' : 1 }
177
- self .outputs = {
178
- 'Out' : self .inputs ['X' ] + self .inputs ['Y' ].reshape (1 , 3 , 4 )
179
- }
221
+ def init_axis (self ):
222
+ self .axis = 1
223
+
224
+
225
+ class TestFP16ElementwiseAddOp_rowwise_add_0 (TestFP16ElementwiseAddOp ):
226
+ def init_input_output (self ):
227
+ self .x = np .random .rand (2 , 3 , 4 ).astype (self .dtype )
228
+ self .y = np .random .rand (3 , 4 ).astype (self .dtype )
229
+ self .out = self .x + self .y .reshape (1 , 3 , 4 )
230
+
231
+ def init_axis (self ):
232
+ self .axis = 1
180
233
181
234
182
235
class TestElementwiseAddOp_rowwise_add_1 (TestElementwiseAddOp ):
183
- def setUp (self ):
184
- self .op_type = "elementwise_add"
185
- self .inputs = {
186
- 'X' : np .random .rand (2 , 1 ).astype (np .float32 ),
187
- 'Y' : np .random .rand (1 ).astype (np .float32 )
188
- }
236
+ def init_input_output (self ):
237
+ self .x = np .random .rand (2 , 1 ).astype (self .dtype )
238
+ self .y = np .random .rand (1 ).astype (self .dtype )
239
+ self .out = self .x + self .y .reshape (1 , 1 )
189
240
190
- self .attrs = {'axis' : 1 }
191
- self .outputs = {
192
- 'Out' : self .inputs ['X' ] + self .inputs ['Y' ].reshape (1 , 1 )
193
- }
241
+ def init_axis (self ):
242
+ self .axis = 1
243
+
244
+
245
+ class TestFP16ElementwiseAddOp_rowwise_add_1 (TestFP16ElementwiseAddOp ):
246
+ def init_input_output (self ):
247
+ self .x = np .random .rand (2 , 1 ).astype (self .dtype )
248
+ self .y = np .random .rand (1 ).astype (self .dtype )
249
+ self .out = self .x + self .y .reshape (1 , 1 )
250
+
251
+ def init_axis (self ):
252
+ self .axis = 1
194
253
195
254
196
255
if __name__ == '__main__' :
0 commit comments