From 8be37f57bb4a6868da4f7036b7cd2c540e333fbb Mon Sep 17 00:00:00 2001 From: pangyoki Date: Tue, 5 Apr 2022 03:05:07 +0000 Subject: [PATCH 1/3] support final_state in multiprocess --- paddle/fluid/imperative/tracer.cc | 8 ++++++++ python/paddle/fluid/dygraph/math_op_patch.py | 5 ++++- python/paddle/fluid/tests/unittests/test_inplace.py | 4 +--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/imperative/tracer.cc b/paddle/fluid/imperative/tracer.cc index 3d4cfa2df3179..0b3fbed38dc16 100644 --- a/paddle/fluid/imperative/tracer.cc +++ b/paddle/fluid/imperative/tracer.cc @@ -385,6 +385,14 @@ void Tracer::TraceOp(const std::string& type, const NameTensorMap& ins, void Tracer::SetExpectedPlace(platform::Place place) { expected_place_ = place; + if (platform::is_gpu_place(place)) { +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) + platform::SetDeviceId(place.device); +#else + PADDLE_THROW(platform::errors::PreconditionNotMet( + "PaddlePaddle should compile with GPU if use CUDAPlace.")); +#endif + } } bool Tracer::ComputeRequiredGrad(const NameVarBaseMap& ins, 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 From a0835a151d774011bfd9f60d548257c1e3cdc4c3 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Tue, 5 Apr 2022 08:34:00 +0000 Subject: [PATCH 2/3] fix no place.device --- paddle/fluid/imperative/tracer.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/imperative/tracer.cc b/paddle/fluid/imperative/tracer.cc index 0b3fbed38dc16..b3285310d3c0c 100644 --- a/paddle/fluid/imperative/tracer.cc +++ b/paddle/fluid/imperative/tracer.cc @@ -387,7 +387,9 @@ void Tracer::SetExpectedPlace(platform::Place place) { expected_place_ = place; if (platform::is_gpu_place(place)) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - platform::SetDeviceId(place.device); + if (place.device) { + platform::SetDeviceId(place.device); + } #else PADDLE_THROW(platform::errors::PreconditionNotMet( "PaddlePaddle should compile with GPU if use CUDAPlace.")); From 3537f12018861d22826512a926369fdccc27e734 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Wed, 6 Apr 2022 06:30:12 +0000 Subject: [PATCH 3/3] set device_id in eager_gen --- .../final_state_generator/eager_gen.py | 11 +++++++++++ paddle/fluid/imperative/tracer.cc | 10 ---------- 2 files changed, 11 insertions(+), 10 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 b2db256f6026a..bb08b1ee438fc 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 @@ -195,6 +195,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 @@ -285,6 +295,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/paddle/fluid/imperative/tracer.cc b/paddle/fluid/imperative/tracer.cc index b3285310d3c0c..3d4cfa2df3179 100644 --- a/paddle/fluid/imperative/tracer.cc +++ b/paddle/fluid/imperative/tracer.cc @@ -385,16 +385,6 @@ void Tracer::TraceOp(const std::string& type, const NameTensorMap& ins, void Tracer::SetExpectedPlace(platform::Place place) { expected_place_ = place; - if (platform::is_gpu_place(place)) { -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (place.device) { - platform::SetDeviceId(place.device); - } -#else - PADDLE_THROW(platform::errors::PreconditionNotMet( - "PaddlePaddle should compile with GPU if use CUDAPlace.")); -#endif - } } bool Tracer::ComputeRequiredGrad(const NameVarBaseMap& ins,