Skip to content

Commit 3da094f

Browse files
committed
rearrange test
1 parent 4bf168b commit 3da094f

File tree

2 files changed

+160
-101
lines changed

2 files changed

+160
-101
lines changed

paddle/fluid/operators/elementwise_add_op.cu

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ limitations under the License. */
1717
#include "paddle/fluid/platform/float16.h"
1818

1919
namespace ops = paddle::operators;
20-
namespace plat = padddle::platform;
20+
namespace plat = paddle::platform;
2121

2222
REGISTER_OP_CUDA_KERNEL(
2323
elementwise_add, ops::ElementwiseAddKernel<plat::CUDADeviceContext, float>,
2424
ops::ElementwiseAddKernel<plat::CUDADeviceContext, double>,
2525
ops::ElementwiseAddKernel<plat::CUDADeviceContext, int>,
26-
ops::ElementwiseAddKernel<plat::CUDADeviceContext, int64_t>
27-
ops::ElementwiseAddKernel<plat::CUDADeviceContext, plat::float16>);
26+
ops::ElementwiseAddKernel<plat::CUDADeviceContext, int64_t>,
27+
ops::ElementwiseAddKernel<plat::CUDADeviceContext, plat::float16>);
2828
REGISTER_OP_CUDA_KERNEL(
2929
elementwise_add_grad,
3030
ops::ElementwiseAddGradKernel<plat::CUDADeviceContext, float>,

python/paddle/fluid/tests/unittests/test_elementwise_add_op.py

Lines changed: 157 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ class TestElementwiseAddOp(OpTest):
2121
def setUp(self):
2222
self.op_type = "elementwise_add"
2323
self.dtype = np.float32
24-
init_dtype()
24+
self.axis = -1
25+
self.init_dtype()
26+
self.init_input_output()
27+
self.init_axis()
2528

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)
2829
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)
3132
}
32-
self.outputs = {'Out': np.add(x, y)}
33+
self.attrs = {'axis': self.axis}
34+
self.outputs = {'Out': self.out}
3335

3436
def test_check_output(self):
3537
self.check_output()
@@ -51,12 +53,20 @@ def test_check_grad_ingore_y(self):
5153
self.check_grad(
5254
['X'], 'Out', max_relative_error=0.005, no_grad_set=set('Y'))
5355

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):
5565
pass
5666

5767

5868
class TestFP16ElementwiseAddOp(TestElementwiseAddOp):
59-
def init_dtype():
69+
def init_dtype(self):
6070
self.dtype = np.float16
6171

6272
def test_check_output(self):
@@ -67,130 +77,179 @@ def test_check_output(self):
6777

6878

6979
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
7791

7892

7993
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
87105

88106

89107
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)
97119

98120

99121
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)
106126

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
111139

112140

113141
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)
120146

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
125159

126160

127161
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)
134166

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)
138173

139174

140175
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)
147180

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
152193

153194

154195
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
161203

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
166213

167214

168215
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)
175220

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
180233

181234

182235
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)
189240

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
194253

195254

196255
if __name__ == '__main__':

0 commit comments

Comments
 (0)