diff --git a/paddle/fluid/imperative/basic_engine.cc b/paddle/fluid/imperative/basic_engine.cc index a34ac72ec16428bdd4a9cfc54509db8b1b654f5e..376391c568b2627f7fa04ed58d66ec71e068dfc0 100644 --- a/paddle/fluid/imperative/basic_engine.cc +++ b/paddle/fluid/imperative/basic_engine.cc @@ -221,13 +221,16 @@ void BasicEngine::PrepareGradAccumulators( void BasicEngine::PrepareDeps() { PADDLE_ENFORCE_EQ( node_deps_.empty(), true, - platform::errors::AlreadyExists("Op deps must be initialized.")); - PADDLE_ENFORCE_EQ( - accumulators_.empty(), true, - platform::errors::AlreadyExists("Accumulators must be initialized.")); - PADDLE_ENFORCE_EQ( - accumulators_with_grad_node_.empty(), true, - platform::errors::AlreadyExists("Accumulators must be initialized.")); + platform::errors::AlreadyExists("Op deps are not empty before preparing " + "it for backward network execution.")); + PADDLE_ENFORCE_EQ(accumulators_.empty(), true, + platform::errors::AlreadyExists( + "Accumulators are not empty before preparing it for " + "backward network execution.")); + PADDLE_ENFORCE_EQ(accumulators_with_grad_node_.empty(), true, + platform::errors::AlreadyExists( + "Accumulators with grad_node as the key are not empty " + "before preparing it for backward network execution.")); std::queue q; std::unordered_set visited; diff --git a/paddle/fluid/imperative/variable_wrapper.h b/paddle/fluid/imperative/variable_wrapper.h index d4192de519a27989a3bc672d010f280b1ae343ff..1e900a34456eb0e4e4287771790ff1eb1b876ed7 100644 --- a/paddle/fluid/imperative/variable_wrapper.h +++ b/paddle/fluid/imperative/variable_wrapper.h @@ -258,11 +258,13 @@ class VariableWrapper { auto shared_node = grad_node_.lock(); if (shared_node != grad_node) { - PADDLE_ENFORCE_EQ( - !shared_node || !grad_node->InplaceGradNameMap().empty(), true, - platform::errors::PermissionDenied( - "Cannot set gradient op twice unless using Inplace Strategy.")); - if (shared_node) { + if (grad_node->InplaceGradNameMap().empty()) { + // grad_node doesn't have Inplace message + PADDLE_ENFORCE_EQ( + shared_node, nullptr, + platform::errors::PermissionDenied( + "Cannot set gradient op twice unless using Inplace Strategy.")); + } else if (shared_node) { VLOG(3) << "The gradient op of Var (" << Name() << ") has been set twice. Because Inplace Strategy is used."; }