From 00554b3f6b128287b686ea277d53d070e1b05eca Mon Sep 17 00:00:00 2001 From: pangyoki Date: Tue, 19 Jan 2021 15:26:19 +0800 Subject: [PATCH] fix error message of Inplace strategy (#30520) --- paddle/fluid/imperative/basic_engine.cc | 17 ++++++++++------- paddle/fluid/imperative/variable_wrapper.h | 12 +++++++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/paddle/fluid/imperative/basic_engine.cc b/paddle/fluid/imperative/basic_engine.cc index a34ac72ec16..376391c568b 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 d4192de519a..1e900a34456 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."; } -- GitLab