From c1235c935fe25c9e52818de8cbc2d3ce10cd3a11 Mon Sep 17 00:00:00 2001 From: peizhilin Date: Wed, 9 Jan 2019 17:34:15 +0800 Subject: [PATCH] add the enable_debug flag test=develop --- paddle/fluid/framework/operator.cc | 22 ++++++++++++++++------ paddle/fluid/framework/operator.h | 4 ++-- paddle/fluid/platform/debug_support.cc | 9 +++++++++ paddle/fluid/platform/enforce.h | 5 +++++ python/paddle/fluid/framework.py | 12 +++++++++--- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 4066907fff..9bc5cb6a7e 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -32,6 +32,12 @@ DEFINE_bool(check_nan_inf, false, "Checking whether operator produce NAN/INF or not. It will be " "extremely slow so please use this flag wisely."); +DEFINE_bool( + enable_debug, false, + "The enable_debug indicate whether to give more detail information when, " + "use the paddlepaddle. However it may deduce the performance since it has" + "to record the information during runtime."); + namespace paddle { namespace framework { @@ -157,7 +163,7 @@ RuntimeContext::RuntimeContext(const VariableNameMap& innames, } } -void OperatorBase::PreHook() { +void OperatorBase::PreHook(const Scope& scope, const platform::Place& place) { auto attrName = OpProtoAndCheckerMaker::OpCreationCallstackAttrName(); if (HasAttr(attrName)) { auto& callstack = Attr>(attrName); @@ -166,8 +172,10 @@ void OperatorBase::PreHook() { } void OperatorBase::Run(const Scope& scope, const platform::Place& place) { - VLOG(4) << "Call the prehook ... "; - PreHook(); + if (FLAGS_enable_debug) { + VLOG(4) << "Call the prehook ... "; + PreHook(scope, place); + } VLOG(4) << place << " " << DebugStringEx(&scope); if (platform::is_gpu_place(place)) { @@ -191,11 +199,13 @@ void OperatorBase::Run(const Scope& scope, const platform::Place& place) { } VLOG(3) << place << " " << DebugStringEx(&scope); - VLOG(4) << "Call the posthook ... "; - PostHook(); + if (FLAGS_enable_debug) { + VLOG(4) << "Call the posthook ... "; + PostHook(scope, place); + } } -void OperatorBase::PostHook() { +void OperatorBase::PostHook(const Scope& scope, const platform::Place& place) { // do nothing here } diff --git a/paddle/fluid/framework/operator.h b/paddle/fluid/framework/operator.h index 4e96ca9f5f..70dd5b055a 100644 --- a/paddle/fluid/framework/operator.h +++ b/paddle/fluid/framework/operator.h @@ -161,8 +161,8 @@ class OperatorBase { const RuntimeContext& ctx) const {} // Add the hooks - virtual void PreHook(); - virtual void PostHook(); + virtual void PreHook(const Scope& scope, const platform::Place& place); + virtual void PostHook(const Scope& scope, const platform::Place& place); protected: std::string type_; diff --git a/paddle/fluid/platform/debug_support.cc b/paddle/fluid/platform/debug_support.cc index a46db932f6..98dcbc2637 100644 --- a/paddle/fluid/platform/debug_support.cc +++ b/paddle/fluid/platform/debug_support.cc @@ -27,6 +27,15 @@ std::string PythonDebugSupport::Format() const { for (auto &line : info) { sout << line; } + } else { +#ifdef _WIN32 + sout << "please set FLAGS_enable_debug=True to get more details regard to " + "this failure.\n"; +#else // _WIN32 + sout << "please export FLAGS_enable_debug=True to get more details regard " + "to " + "this failure.\n"; +#endif // _WIN32 } return sout.str(); } diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index 71c8cc1e31..b4edd51568 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -265,6 +265,10 @@ inline void throw_on_error(T e) { #define __THROW_ON_ERROR_ONE_ARG(COND, ARG) \ ::paddle::platform::throw_on_error(COND, ::paddle::string::Sprintf(ARG)); +#ifdef _WIN32 +#define __PADDLE_THROW_ON_ERROR(COND, ...) \ + __THROW_ON_ERROR_ONE_ARG(COND, __VA_ARGS__) +#else // _WIN32 #define __PADDLE_THROW_ON_ERROR(COND, ...) \ __PADDLE_THROW_ERROR_I( \ __VA_ARGS__, ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \ @@ -276,6 +280,7 @@ inline void throw_on_error(T e) { ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \ ::paddle::platform::throw_on_error(COND, __VA_ARGS__), \ __THROW_ON_ERROR_ONE_ARG(COND, __VA_ARGS__)) +#endif // _WIN32 #define __PADDLE_UNARY_COMPARE(COND, ...) \ do { \ diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index f54016d504..c3b8b8c1c6 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -627,9 +627,15 @@ class Operator(object): if role_var_name in op_attrs and len(op_attrs[role_var_name]) == 0: del op_attrs[role_var_name] - callstack_var_name = op_maker.kOpCreationCallstackAttrName() - op_attrs[callstack_var_name] = list( - reversed(traceback.format_stack()))[1:] + if 'FLAGS_enable_debug' in os.environ and os.environ[ + 'FLAGS_enable_debug']: + callstack_var_name = op_maker.kOpCreationCallstackAttrName() + op_attrs[callstack_var_name] = list( + reversed(traceback.format_stack()))[1:] + op_attrs[callstack_var_name].insert( + 0, + 'Invoke operator ' + ('' + if type is None else type) + ' error.\n') if len(self.desc.type()) != 0: return -- GitLab