From fb4069ac8a5cd16445db28033b96a30345611137 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 6 Apr 2022 21:27:30 +0800 Subject: [PATCH] fix device_id bug for final_state op in multiprocess testcase (#41407) * support final_state in multiprocess * fix no place.device * set device_id in eager_gen --- .../final_state_generator/eager_gen.py | 11 +++++++++++ python/paddle/fluid/dygraph/math_op_patch.py | 5 ++++- python/paddle/fluid/tests/unittests/test_inplace.py | 4 +--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/eager/auto_code_generator/final_state_generator/eager_gen.py b/paddle/fluid/eager/auto_code_generator/final_state_generator/eager_gen.py index 19e42e1bdf640..3b4c8f962179e 100644 --- a/paddle/fluid/eager/auto_code_generator/final_state_generator/eager_gen.py +++ b/paddle/fluid/eager/auto_code_generator/final_state_generator/eager_gen.py @@ -194,6 +194,16 @@ class {} : public egr::GradNodeBase {{ // Get Input AutoGradMeta {} + // Set Device Id + auto place = egr::Controller::Instance().GetExpectedPlace(); + if (paddle::platform::is_gpu_place(place)) {{ +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) + phi::backends::gpu::SetDeviceId(place.device); +#else + PADDLE_THROW(paddle::platform::errors::PreconditionNotMet( + "PaddlePaddle should compile with GPU if use CUDAPlace.")); +#endif + }} // Forward API Call {} // Get Outputs @@ -284,6 +294,7 @@ class {} : public egr::GradNodeBase {{ #include "paddle/fluid/platform/profiler/event_tracing.h" #include "paddle/fluid/eager/amp_utils.h" #include "paddle/fluid/eager/eager_amp_auto_cast.h" +#include "paddle/phi/backends/gpu/gpu_info.h" {} {} diff --git a/python/paddle/fluid/dygraph/math_op_patch.py b/python/paddle/fluid/dygraph/math_op_patch.py index 98de5949ba422..8b80444fe9011 100644 --- a/python/paddle/fluid/dygraph/math_op_patch.py +++ b/python/paddle/fluid/dygraph/math_op_patch.py @@ -270,7 +270,10 @@ def __impl__(self, other_var): # 4. calculation axis = -1 - math_op = getattr(_C_ops, op_type) + if framework._in_eager_mode_ and op_type == 'elementwise_add': + math_op = getattr(_C_ops, 'final_state_add') + else: + math_op = getattr(_C_ops, op_type) return math_op(self, other_var, 'axis', axis) comment = OpProtoHolder.instance().get_op_proto(op_type).comment diff --git a/python/paddle/fluid/tests/unittests/test_inplace.py b/python/paddle/fluid/tests/unittests/test_inplace.py index ee0d5bcdde6f2..c54d3f02d43f0 100644 --- a/python/paddle/fluid/tests/unittests/test_inplace.py +++ b/python/paddle/fluid/tests/unittests/test_inplace.py @@ -103,9 +103,7 @@ def func_test_backward_success_2(self): var_b[1:2] = 3 # var_b is modified inplace before using it - var_c = paddle.add( - var_b, - var_b) # Here, the grad op of sum doesn't use the value of var_b + var_c = var_b + var_b # Here, the grad op of sum doesn't use the value of var_b loss = var_c.sum() var_b[1:2] = 3 # var_b is modified inplace after using it