From a708c4212dd148a85aafa04b6c707ba00e15545b Mon Sep 17 00:00:00 2001 From: chenfei Date: Wed, 14 Dec 2022 15:13:19 +0800 Subject: [PATCH] fix bug of value error cannot raise --- .jenkins/check/config/whitelizard.txt | 2 +- mindspore/ccsrc/pipeline/jit/debug/trace.cc | 2 +- mindspore/ccsrc/pipeline/jit/pipeline.cc | 1 + .../{log_adapter_py.cc => log_adapter_py.h} | 8 +++- mindspore/core/utils/log_adapter.cc | 46 ++++++++++++++----- mindspore/core/utils/log_adapter.h | 27 ++++++++--- 6 files changed, 65 insertions(+), 21 deletions(-) rename mindspore/ccsrc/pybind_api/ir/{log_adapter_py.cc => log_adapter_py.h} (90%) diff --git a/.jenkins/check/config/whitelizard.txt b/.jenkins/check/config/whitelizard.txt index a513b1557e1..b1c9ca04095 100644 --- a/.jenkins/check/config/whitelizard.txt +++ b/.jenkins/check/config/whitelizard.txt @@ -19,7 +19,7 @@ mindspore/mindspore/ccsrc/pipeline/jit/init.cc:PYBIND11_MODULE mindspore/mindspore/ccsrc/pipeline/jit/parse/resolve.cc:mindspore::parse::ResolveObjectToNode mindspore/mindspore/ccsrc/pipeline/jit/pipeline.cc:mindspore::pipeline::GraphExecutorPy::Compile mindspore/mindspore/ccsrc/pipeline/jit/static_analysis/prim.cc:mindspore::abstract::ConvertAbstractToPython -mindspore/mindspore/ccsrc/pybind_api/ir/log_adapter_py.cc:mindspore::PyExceptionInitializer::HandleExceptionPy +mindspore/mindspore/ccsrc/pybind_api/ir/log_adapter_py.h:mindspore::PyExceptionInitializer::HandleExceptionPy mindspore/mindspore/ccsrc/plugin/device/gpu/kernel/math/unary_op_gpu_kernel.h:mindspore::kernel::UnaryOpGpuKernel::Launch mindspore/mindspore/ccsrc/plugin/device/ascend/optimizer/ir_fission/dynamic_rnn_grad_fission_v2.cc:mindspore::opt::AddLSTMInputGradNode mindspore/mindspore/ccsrc/plugin/device/ascend/kernel/aicpu/aicpu_ops/drop_out_gen_mask_kernels.cc:aicpu::ARMDropOutGenMaskKernel diff --git a/mindspore/ccsrc/pipeline/jit/debug/trace.cc b/mindspore/ccsrc/pipeline/jit/debug/trace.cc index c88c1959c89..d6243fd028f 100644 --- a/mindspore/ccsrc/pipeline/jit/debug/trace.cc +++ b/mindspore/ccsrc/pipeline/jit/debug/trace.cc @@ -505,7 +505,7 @@ void GetTraceStackInfo(std::ostringstream &oss, bool add_title) { // Register trace provider to LogWriter. struct TraceProviderRegister { - TraceProviderRegister() noexcept { LogWriter::set_trace_provider(GetTraceStackInfo); } + TraceProviderRegister() noexcept { LogWriter::SetTraceProvider(GetTraceStackInfo); } ~TraceProviderRegister() = default; } trace_provider_register; diff --git a/mindspore/ccsrc/pipeline/jit/pipeline.cc b/mindspore/ccsrc/pipeline/jit/pipeline.cc index f7b08bea0f1..3399b1b2289 100644 --- a/mindspore/ccsrc/pipeline/jit/pipeline.cc +++ b/mindspore/ccsrc/pipeline/jit/pipeline.cc @@ -74,6 +74,7 @@ #include "kernel/akg/akg_kernel_build_manager.h" #include "kernel/graph_kernel_info.h" #include "include/backend/data_queue/data_queue_mgr.h" +#include "pybind_api/ir/log_adapter_py.h" #ifndef ENABLE_SECURITY #include "debug/data_dump/dump_json_parser.h" #endif diff --git a/mindspore/ccsrc/pybind_api/ir/log_adapter_py.cc b/mindspore/ccsrc/pybind_api/ir/log_adapter_py.h similarity index 90% rename from mindspore/ccsrc/pybind_api/ir/log_adapter_py.cc rename to mindspore/ccsrc/pybind_api/ir/log_adapter_py.h index 234227c3be8..d907834d511 100644 --- a/mindspore/ccsrc/pybind_api/ir/log_adapter_py.cc +++ b/mindspore/ccsrc/pybind_api/ir/log_adapter_py.h @@ -13,6 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef MINDSPORE_CCSRC_PYBINDAPI_IR_LOGADAPTERPY_H_ +#define MINDSPORE_CCSRC_PYBINDAPI_IR_LOGADAPTERPY_H_ #include "utils/log_adapter.h" @@ -24,7 +26,10 @@ namespace py = pybind11; namespace mindspore { class PyExceptionInitializer { public: - PyExceptionInitializer() { mindspore::LogWriter::set_exception_handler(HandleExceptionPy); } + PyExceptionInitializer() { + MS_LOG(INFO) << "Set exception handler"; + mindspore::LogWriter::SetExceptionHandler(HandleExceptionPy); + } ~PyExceptionInitializer() = default; @@ -96,3 +101,4 @@ class PyExceptionInitializer { static PyExceptionInitializer py_exception_initializer; } // namespace mindspore +#endif // MINDSPORE_CCSRC_PYBINDAPI_IR_LOGADAPTERPY_H_ diff --git a/mindspore/core/utils/log_adapter.cc b/mindspore/core/utils/log_adapter.cc index 9397a484ee3..75aeab10b90 100644 --- a/mindspore/core/utils/log_adapter.cc +++ b/mindspore/core/utils/log_adapter.cc @@ -167,20 +167,40 @@ static int GetSlogLevel(MsLogLevel level) { } #endif -void LogWriter::set_exception_handler(const ExceptionHandler &exception_handler) { - exception_handler_ = exception_handler; +LogWriter::ExceptionHandler &LogWriter::exception_handler() { + static LogWriter::ExceptionHandler g_exception_handler = nullptr; + return g_exception_handler; } -void LogWriter::set_trace_provider(const TraceProvider &trace_provider) { - if (trace_provider_ == nullptr) { - trace_provider_ = trace_provider; - } else { +LogWriter::TraceProvider &LogWriter::trace_provider() { + static LogWriter::TraceProvider g_trace_provider = nullptr; + return g_trace_provider; +} + +const LogWriter::ExceptionHandler &LogWriter::GetExceptionHandler() { + const auto &exception_handler_tmp = exception_handler(); + return exception_handler_tmp; +} + +void LogWriter::SetExceptionHandler(const LogWriter::ExceptionHandler &new_exception_handler) { + auto &exception_handler_tmp = exception_handler(); + exception_handler_tmp = new_exception_handler; +} + +const LogWriter::TraceProvider &LogWriter::GetTraceProvider() { + const auto &trace_provider_tmp = trace_provider(); + return trace_provider_tmp; +} + +void LogWriter::SetTraceProvider(const LogWriter::TraceProvider &new_trace_provider) { + auto &trace_provider_tmp = trace_provider(); + if (trace_provider_tmp != nullptr) { MS_LOG(INFO) << "trace provider has been set, skip."; + return; } + trace_provider_tmp = new_trace_provider; } -LogWriter::TraceProvider LogWriter::trace_provider() { return trace_provider_; } - static inline std::string GetEnv(const std::string &envvar) { const char *value = std::getenv(envvar.c_str()); @@ -390,16 +410,18 @@ void LogWriter::operator^(const LogStream &stream) const { if (this_thread_max_log_level >= MsLogLevel::kException) { RemoveLabelBeforeOutputLog(msg); } - if (trace_provider_ != nullptr) { - trace_provider_(oss, true); + const auto &trace_provider = GetTraceProvider(); + if (trace_provider != nullptr) { + trace_provider(oss, true); } running = false; } DisplayDevExceptionMessage(oss, dmsg, location_); - if (exception_handler_ != nullptr) { - exception_handler_(exception_type_, oss.str()); + const auto &exception_handler = GetExceptionHandler(); + if (exception_handler != nullptr) { + exception_handler(exception_type_, oss.str()); } throw std::runtime_error(oss.str()); } diff --git a/mindspore/core/utils/log_adapter.h b/mindspore/core/utils/log_adapter.h index b7c91909497..c3fc3aee329 100644 --- a/mindspore/core/utils/log_adapter.h +++ b/mindspore/core/utils/log_adapter.h @@ -268,21 +268,36 @@ class MS_CORE_API LogWriter { void operator^(const LogStream &stream) const NO_RETURN; #endif - static void set_exception_handler(const ExceptionHandler &exception_handler); - static void set_trace_provider(const TraceProvider &trace_provider); - static TraceProvider trace_provider(); + /// \brief Get the function pointer of converting exception types in c++. + /// + /// \return A pointer of the function. + static const ExceptionHandler &GetExceptionHandler(); + + /// \brief Set the function pointer of converting exception types in c++. + /// + /// \param[in] A function pointer of converting exception types in c++. + static void SetExceptionHandler(const ExceptionHandler &new_exception_handler); + + /// \brief Get the function pointer of printing trace stacks. + /// + /// \return A pointer of the function. + static const TraceProvider &GetTraceProvider(); + + /// \brief Set the function pointer of printing trace stacks. + /// + /// \param[in] A function pointer of printing trace stacks. + static void SetTraceProvider(const TraceProvider &new_trace_provider); private: void OutputLog(const std::ostringstream &msg) const; void RemoveLabelBeforeOutputLog(const std::ostringstream &msg) const; + static ExceptionHandler &exception_handler(); + static TraceProvider &trace_provider(); LocationInfo location_; MsLogLevel log_level_; SubModuleId submodule_; ExceptionType exception_type_; - - inline static ExceptionHandler exception_handler_ = nullptr; - inline static TraceProvider trace_provider_ = nullptr; }; #define MSLOG_IF(level, condition, excp_type) \