diff --git a/paddle/fluid/imperative/basic_engine.cc b/paddle/fluid/imperative/basic_engine.cc index df8c9f0bccb300fdfd9243aa21417c921c38141f..94d66c5d080c2763b8928cf6d729251895df393f 100644 --- a/paddle/fluid/imperative/basic_engine.cc +++ b/paddle/fluid/imperative/basic_engine.cc @@ -328,9 +328,13 @@ void BasicEngine::Execute() { "Cannot find gradient of variable %s", var->Name())); } - // leaf_accumulators_ : hooks and accumulate-grad for leaf tensor + // leaf_accumulators_ : hooks and accumulate-grad for leaf tensor, + // it should be orderly and not reapeated. if (var->IsLeafGrad()) { - leaf_accumulators_.insert(iter->second.get()); + if (std::find(leaf_accumulators_.begin(), leaf_accumulators_.end(), + iter->second.get()) == leaf_accumulators_.end()) { + leaf_accumulators_.push_back(iter->second.get()); + } if (iter->second->HasInnerVar()) { var = iter->second->InnerVar(); diff --git a/paddle/fluid/imperative/basic_engine.h b/paddle/fluid/imperative/basic_engine.h index 87c4ea380f3c09d29f779691e6c1b143a9614d49..a2ad8b5f8aa61e438e5afdc72681a96ee21c996b 100644 --- a/paddle/fluid/imperative/basic_engine.h +++ b/paddle/fluid/imperative/basic_engine.h @@ -69,7 +69,9 @@ class BasicEngine : public Engine { std::vector>> need_accu_var_list_; // leaf_accumulators_ is only for leaf tensor(hooks/accumulate grad) - std::unordered_set leaf_accumulators_; + // It should be orderly and not repeated, because multiple cards must ensure + // that the order of vars is the same. + std::vector leaf_accumulators_; bool retain_graph_; };