From 40b3e752fde87dc44558a398ff06ceaabf19d909 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Tue, 19 Jan 2021 21:27:27 +0800 Subject: [PATCH] [Cherry-pick] PR 30520. fix error message of Inplace strategy (#30520) (#30568) Cherry pick PR #30520 . Fix error message of Inplace strategy. --- 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 94d66c5d080..9b3c2e510ab 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 5bbabaf3d22..8c7c473c26b 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."; } -- GitLab