diff --git a/paddle/fluid/imperative/basic_engine.cc b/paddle/fluid/imperative/basic_engine.cc index 94d66c5d080c2763b8928cf6d729251895df393f..9b3c2e510ab1ec0a7d57c972963a041a060c6fce 100644 --- a/paddle/fluid/imperative/basic_engine.cc +++ b/paddle/fluid/imperative/basic_engine.cc @@ -215,13 +215,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 5bbabaf3d22d03a0487edfdf09a5470e272a036a..8c7c473c26b5d90b4c972bcdbae407ddf10c73e4 100644 --- a/paddle/fluid/imperative/variable_wrapper.h +++ b/paddle/fluid/imperative/variable_wrapper.h @@ -257,11 +257,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."; }