From 968bf46e7f1a39a02a02cb23ba2a50456a421917 Mon Sep 17 00:00:00 2001 From: Jiabin Yang <360788950@qq.com> Date: Wed, 20 Apr 2022 15:43:39 +0800 Subject: [PATCH] Fixed performance issue regarding BackwardRun using add_final_state_dygraph (#41912) (#41991) Co-authored-by: Zhanlue Yang --- .../final_state_generator/eager_gen.py | 3 --- paddle/fluid/eager/backward.cc | 3 ++- paddle/fluid/eager/grad_tensor_holder.cc | 11 ++++++++--- paddle/fluid/eager/grad_tensor_holder.h | 3 ++- 4 files changed, 12 insertions(+), 8 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 82b297acd3d..54c6e39283e 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 @@ -1471,9 +1471,6 @@ class DygraphNodeGenerator(DygraphFunctionGeneratorBase): grad_node_name = GetGradNodeName(forward_api_name) - if len(grad_node_creation_str) == 0: - grad_node_creation_str = f"if(create_graph) VLOG(3) << \"Higher order grad node for {grad_node_name} has not been implemented yet.\";" - self.node_definition_str = GRAD_FUNCTION_TEMPLATE.format( grad_node_name, fill_zero_str, get_grad_in_args_str, grad_node_name, grad_function_call_str, get_outputs_str, inputs_autograd_meta_str, diff --git a/paddle/fluid/eager/backward.cc b/paddle/fluid/eager/backward.cc index 6db606edf6f..a1df8222653 100644 --- a/paddle/fluid/eager/backward.cc +++ b/paddle/fluid/eager/backward.cc @@ -766,7 +766,8 @@ std::vector RunBackward( << ", rank: " << edge_rank.second; node_input_buffers_dict[next_node]->add( - edge_rank.first, edge_rank.second, grad_output_tensor); + edge_rank.first, edge_rank.second, grad_output_tensor, + create_graph); // Update queue node_in_degree_map[next_node]--; diff --git a/paddle/fluid/eager/grad_tensor_holder.cc b/paddle/fluid/eager/grad_tensor_holder.cc index 27a8c6002e2..64fb8b53b47 100644 --- a/paddle/fluid/eager/grad_tensor_holder.cc +++ b/paddle/fluid/eager/grad_tensor_holder.cc @@ -72,7 +72,8 @@ void GradTensorHolder::CopyValueFromTensor( } void GradTensorHolder::add(size_t slot_id, size_t rank, - const paddle::experimental::Tensor& t) { + const paddle::experimental::Tensor& t, + bool create_graph) { // TODO(jiabin): We need to deal with empty input_buffer with slot size not // empty; PADDLE_ENFORCE(slot_id < buffer_.size(), @@ -113,8 +114,12 @@ void GradTensorHolder::add(size_t slot_id, size_t rank, if (t.is_dense_tensor()) { if (buffer_tensor.is_dense_tensor()) { - buffer_tensor = add_final_state_dygraph_function(t, buffer_tensor); - + if (create_graph) { + buffer_tensor = add_final_state_dygraph_function(t, buffer_tensor); + } else { + paddle::imperative::TensorAdd( + t, &buffer_tensor); + } } else { // TODO(jiabin): Support Other TensorBase later // TODO(zhanlve): Replace SelectedRowsAddTensor with diff --git a/paddle/fluid/eager/grad_tensor_holder.h b/paddle/fluid/eager/grad_tensor_holder.h index a4f2507728c..80b7c59df8f 100644 --- a/paddle/fluid/eager/grad_tensor_holder.h +++ b/paddle/fluid/eager/grad_tensor_holder.h @@ -45,7 +45,8 @@ class GradTensorHolder { GradTensorHolder& operator=(const GradTensorHolder& other) = default; // Create new tensor and copy tensor->impl - void add(size_t slot_id, size_t rank, const paddle::experimental::Tensor& t); + void add(size_t slot_id, size_t rank, const paddle::experimental::Tensor& t, + bool create_graph = false); void CopyValueFromTensor(size_t slot_id, size_t rank, const paddle::experimental::Tensor& t, bool fill_one = false); -- GitLab