diff --git a/paddle/fluid/eager/backward.cc b/paddle/fluid/eager/backward.cc index a3c3c2718d99ec6f9b24745d34c59700e6759268..b70ec78c7598cc011970db1d4c81cc358caa4194 100644 --- a/paddle/fluid/eager/backward.cc +++ b/paddle/fluid/eager/backward.cc @@ -360,11 +360,22 @@ std::vector RunBackward( "Node's in-degree cannot be negative.", next_node->name())); - if (node_in_degree_map[next_node] == 0) { - if (dynamic_cast(next_node)) { - queue.push_front(std::move(next_node)); - } else { - queue.push_back(std::move(next_node)); + if (is_general_grad) { + if (node_in_degree_map[next_node] == 0 && + GeneralGrad::Instance().IsNeededNodes(next_node)) { + if (dynamic_cast(next_node)) { + queue.push_front(std::move(next_node)); + } else { + queue.push_back(std::move(next_node)); + } + } + } else { + if (node_in_degree_map[next_node] == 0) { + if (dynamic_cast(next_node)) { + queue.push_front(std::move(next_node)); + } else { + queue.push_back(std::move(next_node)); + } } } }