diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 4066907fff28ed080ae01c6a9cd1b3dc937a55d9..9bc5cb6a7e00deb0cbbe1995463c3bd623873bfa 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 4e96ca9f5f9d0fde85b76a29705dd98f812a647c..70dd5b055a5037425f5bfe6d41018303abb6b3e0 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 a46db932f6d6f35537aaaabee9bf409610981caa..98dcbc263758720b09b057d131ab1cf1caff4073 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 71c8cc1e3122122276108fd4692569fce325676a..b4edd51568db58346af48e459036537d43fa7099 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 f54016d504f5e2d275a09eb3c4acff044ae6d373..c3b8b8c1c6d10c23b01db9127d4841f6f3443642 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