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 82b297acd3dd6bc041f9017b080cb28ff1078599..54c6e39283ec59c99588b265a06f0566ea012d46 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 6db606edf6f4c353ec006f08709aaf818f45d018..a1df822265309da53b6150c167906668d4fcf5bc 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 27a8c6002e29d67f504bd1741998ec5e9350843e..64fb8b53b473cd5ca91469fccc1cd0b75f30dfdf 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 a4f2507728c6422afa5b4d67129147fe3d3e1faa..80b7c59df8fa0158d9ad665b40fd70447fe950e9 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);