@@ -20,11 +20,12 @@ limitations under the License. */
2020#include " paddle/fluid/operators/amp/fp16_type_traits.h"
2121#include " paddle/fluid/operators/fused/fused_dropout_act_bias.h"
2222#include " paddle/fluid/operators/fused/fused_dropout_test.h"
23+ #include " paddle/fluid/operators/math/functors.h"
2324
2425namespace framework = paddle::framework;
2526namespace platform = paddle::platform;
2627namespace details = paddle::operators::details;
27- namespace operators = paddle::operators;
28+ namespace math = paddle::operators::math ;
2829
2930/* *
3031 * @brief the unittest of fused_dropout_act_bias
@@ -111,16 +112,12 @@ struct TestFusedDropoutActBias {
111112 }
112113
113114 {
114- out.Resize ({rows, cols});
115- out.mutable_data <T>(place);
116- mask.Resize ({rows, cols});
117- mask.mutable_data <uint8_t >(place);
118- dsrc.Resize ({rows, cols});
119- dsrc.mutable_data <T>(place);
115+ out.mutable_data <T>({rows, cols}, place);
116+ mask.mutable_data <uint8_t >({rows, cols}, place);
117+ dsrc.mutable_data <T>({rows, cols}, place);
120118
121119 if (has_bias) {
122- dbias.Resize ({cols});
123- dbias.mutable_data <T>(place);
120+ dbias.mutable_data <T>({cols}, place);
124121 }
125122 }
126123 }
@@ -133,7 +130,7 @@ struct TestFusedDropoutActBias {
133130 for (int i = 0 ; i < rows; i++) {
134131 for (int j = 0 ; j < cols; j++) {
135132 const T tmp = src_vec[i * cols + j] + bias_vec[j];
136- out1[i * cols + j] = act (& tmp);
133+ out1[i * cols + j] = act (tmp);
137134 }
138135 }
139136 // call dropout
@@ -143,7 +140,7 @@ struct TestFusedDropoutActBias {
143140 for (int i = 0 ; i < rows; i++) {
144141 for (int j = 0 ; j < cols; j++) {
145142 const T tmp = src_vec[i * cols + j];
146- out1[i * cols + j] = act (& tmp);
143+ out1[i * cols + j] = act (tmp);
147144 }
148145 }
149146
@@ -164,22 +161,22 @@ struct TestFusedDropoutActBias {
164161 GradFunctor act_grad;
165162 for (int i = 0 ; i < rows; i++) {
166163 for (int j = 0 ; j < cols; j++) {
164+ T args[2 ];
165+ args[0 ] = _out[i * cols + j];
167166 if (has_bias) {
168- T args[2 ];
169- args[0 ] = _out[i * cols + j];
170167 args[1 ] = src_vec[i * cols + j] + bias_vec[j];
171- T val = act_grad (args);
172- correct_dbias[j] += val;
173- correct_dsrc[i * cols + j] = val;
174168 } else {
175- T args[2 ];
176- args[0 ] = _out[i * cols + j];
177169 args[1 ] = src_vec[i * cols + j];
178- T val = act_grad (args);
179- correct_dsrc[i * cols + j] = val;
180170 }
171+ T val = args[0 ] * act_grad.UseOut (args[1 ]);
172+ correct_dsrc[i * cols + j] = val;
181173 }
182174 }
175+
176+ if (has_bias) {
177+ // reduce_sum: keep the same calculate order as the GPU
178+ ReduceSum<T>(correct_dsrc, &correct_dbias, rows, cols);
179+ }
183180 }
184181
185182 void FusedForward () {
@@ -273,47 +270,41 @@ static void BaseTest(const bool is_fp16 = false) {
273270 const int rows = 16 ;
274271 std::vector<int > cols_list = {16 , 17 };
275272 bool has_bias[2 ] = {true , false };
276- T default_diff = !is_fp16 ? static_cast <T>(1e-3 ) : default_diff =
277- static_cast <T>(1e-2 );
273+ T default_diff = !is_fp16 ? static_cast <T>(1e-5 ) : static_cast <T>(1e-1 );
278274 for (auto cols : {16 , 17 }) {
279275 for (auto has_bias : {true , false }) {
280276 TestFusedDropoutActBias<T, Functor, GradFunctor> test (rows, cols);
281277 test.has_bias = has_bias;
282278 test.Run ();
283279 test.CheckOut (default_diff);
284- if (!is_fp16) {
285- test.CheckGrad (default_diff);
286- }
280+ test.CheckGrad (default_diff);
287281 }
288282 }
289283}
290284
291285TEST (FusedDropout, GPUFusedDorpoutActBias) {
292- BaseTest<float , paddle::operators::ReluFunctor<float >,
293- paddle::operators::ReluGradFunctor<float >>();
294- BaseTest<float , operators::GeluFunctor<float >,
295- operators::GeluGradFunctor<float >>();
286+ BaseTest<float , math::ReluFunctor<float >, math::ReluGradFunctor<float >>();
287+ BaseTest<float , paddle::operators::GeluFunctor<float >,
288+ paddle::operators::GeluGradFunctor<float >>();
296289}
297290TEST (FusedDropout, GPUFusedDropoutActBiasDouble) {
298- BaseTest<double , operators::ReluFunctor<double >,
299- operators::ReluGradFunctor<double >>();
300- BaseTest<double , operators::GeluFunctor<double >,
301- operators::GeluGradFunctor<double >>();
291+ BaseTest<double , math::ReluFunctor<double >, math::ReluGradFunctor<double >>();
292+ BaseTest<double , paddle::operators::GeluFunctor<double >,
293+ paddle::operators::GeluGradFunctor<double >>();
302294}
303295
304296// test fp16, For inference, check_grad is not required. ref: test_dropout_op.py
305297TEST (FusedDropout, GPUFusedDropoutActBiasFp16) {
306298 using fp16 = platform::float16;
307- BaseTest<fp16, operators::ReluFunctor<fp16>,
308- operators::ReluGradFunctor<fp16>>(true );
299+ BaseTest<fp16, math::ReluFunctor<fp16>, math::ReluGradFunctor<fp16>>(true );
309300}
310301
311302TEST (FusedDropout, GPUFusedDropoutActBiasIsUpscaleInTrain) {
312303 const int rows = 16 ;
313304 const int cols = 16 ;
314305 for (auto is_upscale_in_train : {true , false }) {
315- TestFusedDropoutActBias<float , operators ::ReluFunctor<float >,
316- operators ::ReluGradFunctor<float >>
306+ TestFusedDropoutActBias<float , math ::ReluFunctor<float >,
307+ math ::ReluGradFunctor<float >>
317308 test (rows, cols, 0 , 1.0 , is_upscale_in_train, false );
318309 test.Run ();
319310 test.CheckOut (static_cast <float >(1e-5 ));
@@ -324,8 +315,8 @@ TEST(FusedDropout, GPUFusedDropoutActBiasIsUpscaleInTrain) {
324315TEST (FusedDropout, GPUFusedDropoutActBiasIsTest) {
325316 const int rows = 16 ;
326317 const int cols = 16 ;
327- TestFusedDropoutActBias<float , operators ::ReluFunctor<float >,
328- operators ::ReluGradFunctor<float >>
318+ TestFusedDropoutActBias<float , math ::ReluFunctor<float >,
319+ math ::ReluGradFunctor<float >>
329320 test (rows, cols, 0 , 0.35 , true , true );
330321 test.Run ();
331322 test.CheckOut (static_cast <float >(1e-5 ));
@@ -335,8 +326,8 @@ TEST(FusedDropout, GPUFusedDropoutActBiasIsTest) {
335326TEST (FusedDropout, GPUFusedDropoutActBiasSeed) {
336327 const int rows = 16 ;
337328 const int cols = 16 ;
338- TestFusedDropoutActBias<float , operators ::ReluFunctor<float >,
339- operators ::ReluGradFunctor<float >>
329+ TestFusedDropoutActBias<float , math ::ReluFunctor<float >,
330+ math ::ReluGradFunctor<float >>
340331 test (rows, cols, 125 , 0.0 , false , false );
341332 test.Run ();
342333 test.CheckOut (static_cast <float >(1e-5 ));
@@ -346,8 +337,8 @@ TEST(FusedDropout, GPUFusedDropoutActBiasSeed) {
346337TEST (FusedDropout, GPUFusedDropoutActBiasLargeShape) {
347338 const int rows = 256 ;
348339 const int cols = 4096 ;
349- TestFusedDropoutActBias<float , operators ::ReluFunctor<float >,
350- operators ::ReluGradFunctor<float >>
340+ TestFusedDropoutActBias<float , math ::ReluFunctor<float >,
341+ math ::ReluGradFunctor<float >>
351342 test (rows, cols);
352343 test.Run ();
353344 test.CheckOut (static_cast <float >(1e-5 ));
0 commit comments