diff --git a/.jenkins/check/config/whitelizard.txt b/.jenkins/check/config/whitelizard.txt index 1ef6537bc0e..847b45bbd40 100644 --- a/.jenkins/check/config/whitelizard.txt +++ b/.jenkins/check/config/whitelizard.txt @@ -18,7 +18,7 @@ mindspore/mindspore/ccsrc/frontend/parallel/ops_info/gather_v2_p_info.cc:mindspo mindspore/mindspore/ccsrc/plugin/device/gpu/hal/device/gpu_kernel_runtime.cc:mindspore::device::gpu::GPUKernelRuntime::LaunchKernelDynamic mindspore/mindspore/ccsrc/pipeline/jit/init.cc:PYBIND11_MODULE mindspore/mindspore/ccsrc/pipeline/jit/parse/resolve.cc:mindspore::parse::ResolveObjectToNode -mindspore/mindspore/ccsrc/utils/python_utils.cc:mindspore::HandleExceptionRethrow +mindspore/mindspore/ccsrc/include/common/utils/python_utils.h:mindspore::HandleExceptionRethrow mindspore/mindspore/ccsrc/pipeline/jit/static_analysis/prim.cc:mindspore::abstract::ConvertAbstractToPython 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 diff --git a/mindspore/ccsrc/include/common/utils/python_utils.h b/mindspore/ccsrc/include/common/utils/python_utils.h index b655b870a35..c6582a4abd4 100644 --- a/mindspore/ccsrc/include/common/utils/python_utils.h +++ b/mindspore/ccsrc/include/common/utils/python_utils.h @@ -18,15 +18,100 @@ #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_PYTHON_UTILS_H_ #include - -#include "include/common/visible.h" -#include "utils/trace_base.h" +#include +#include "utils/info.h" namespace mindspore { -COMMON_EXPORT void HandleExceptionRethrow(const std::function &main_func, - const std::function &already_set_error_handler, - const std::function &other_error_handler, - const std::function &default_error_handler, - const DebugInfoPtr &debug_info = nullptr); +template +static void ThrowException(const std::function &other_error_handler, const DebugInfoPtr &debug_info, + const CatchedExceptionType &ex) { + if (other_error_handler) { + other_error_handler(); + } + if (debug_info == nullptr) { + throw ThrowExceptionType(ex.what()); + } else { + std::stringstream ss; + ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); + throw ThrowExceptionType(ss.str()); + } +} + +inline void HandleExceptionRethrow(const std::function &main_func, + const std::function &already_set_error_handler, + const std::function &other_error_handler, + const std::function &default_error_handler, + const DebugInfoPtr &debug_info = nullptr) { + try { + if (!main_func) { + MS_LOG(ERROR) << "The 'main_func' should not be empty."; + return; + } + main_func(); + } catch (const py::error_already_set &ex) { + if (already_set_error_handler) { + already_set_error_handler(); + } + // Re-throw this exception to Python interpreter to handle it + throw(py::error_already_set(ex)); + } catch (const py::type_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::value_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::index_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::key_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::attribute_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::name_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::assertion_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::base_exception &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::keyboard_interrupt &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::stop_iteration &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::overflow_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::zero_division_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::environment_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::io_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::os_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::memory_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::unbound_local_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::not_implemented_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::indentation_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const py::runtime_warning &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const std::runtime_error &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (const std::exception &ex) { + ThrowException(other_error_handler, debug_info, ex); + } catch (...) { + if (default_error_handler) { + default_error_handler(); + } + +#ifndef _MSC_VER + auto exception_type = abi::__cxa_current_exception_type(); + MS_EXCEPTION_IF_NULL(exception_type); + std::string ex_name(exception_type->name()); + MS_LOG(EXCEPTION) << "Error occurred. Exception name: " << ex_name; +#else + MS_LOG(EXCEPTION) << "Error occurred."; +#endif + } +} } // namespace mindspore #endif // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_PYTHON_UTILS_H_ diff --git a/mindspore/ccsrc/pybind_api/ir/log_adapter_py.h b/mindspore/ccsrc/pybind_api/ir/log_adapter_py.h index bfc3f15a1dc..30e94dfcec0 100644 --- a/mindspore/ccsrc/pybind_api/ir/log_adapter_py.h +++ b/mindspore/ccsrc/pybind_api/ir/log_adapter_py.h @@ -98,6 +98,9 @@ class PyExceptionInitializer { if (exception_type == RuntimeWarning) { throw py::runtime_warning(str); } + if (exception_type == RuntimeError) { + throw std::runtime_error(str); + } py::pybind11_fail(str); } }; diff --git a/mindspore/ccsrc/utils/python_utils.cc b/mindspore/ccsrc/utils/python_utils.cc deleted file mode 100644 index 572e2a18b07..00000000000 --- a/mindspore/ccsrc/utils/python_utils.cc +++ /dev/null @@ -1,307 +0,0 @@ -/** - * Copyright 2023 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "include/common/utils/python_utils.h" - -#include "include/common/utils/python_adapter.h" -#include "pybind_api/pybind_patch.h" - -namespace mindspore { -void HandleExceptionRethrow(const std::function &main_func, - const std::function &already_set_error_handler, - const std::function &other_error_handler, - const std::function &default_error_handler, const DebugInfoPtr &debug_info) { - try { - if (!main_func) { - MS_LOG(ERROR) << "The 'main_func' should not be empty."; - return; - } - main_func(); - } catch (const py::error_already_set &ex) { - if (already_set_error_handler) { - already_set_error_handler(); - } - // Re-throw this exception to Python interpreter to handle it - throw(py::error_already_set(ex)); - } catch (const py::type_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::type_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::type_error(ss.str()); - } - } catch (const py::value_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::value_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::value_error(ss.str()); - } - } catch (const py::index_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::index_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::index_error(ss.str()); - } - } catch (const py::key_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::key_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::key_error(ss.str()); - } - } catch (const py::attribute_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::attribute_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::attribute_error(ss.str()); - } - } catch (const py::name_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::name_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::name_error(ss.str()); - } - } catch (const py::assertion_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::assertion_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::assertion_error(ss.str()); - } - } catch (const py::base_exception &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::base_exception(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::base_exception(ss.str()); - } - } catch (const py::keyboard_interrupt &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::keyboard_interrupt(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::keyboard_interrupt(ss.str()); - } - } catch (const py::stop_iteration &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::stop_iteration(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::stop_iteration(ss.str()); - } - } catch (const py::overflow_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::overflow_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::overflow_error(ss.str()); - } - } catch (const py::zero_division_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::zero_division_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::zero_division_error(ss.str()); - } - } catch (const py::environment_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::environment_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::environment_error(ss.str()); - } - } catch (const py::io_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::io_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::io_error(ss.str()); - } - } catch (const py::os_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::os_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::os_error(ss.str()); - } - } catch (const py::memory_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::memory_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::memory_error(ss.str()); - } - } catch (const py::unbound_local_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::unbound_local_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::unbound_local_error(ss.str()); - } - } catch (const py::not_implemented_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::not_implemented_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::not_implemented_error(ss.str()); - } - } catch (const py::indentation_error &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::indentation_error(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::indentation_error(ss.str()); - } - } catch (const py::runtime_warning &ex) { - if (other_error_handler) { - other_error_handler(); - } - - if (debug_info == nullptr) { - throw py::runtime_warning(ex); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw py::runtime_warning(ss.str()); - } - } catch (const std::exception &ex) { - if (other_error_handler) { - other_error_handler(); - } - - // Re-throw this exception to Python interpreter to handle it. - if (debug_info == nullptr) { - throw std::runtime_error(ex.what()); - } else { - std::stringstream ss; - ss << ex.what() << ".\n\n" << trace::GetDebugInfo(debug_info); - throw std::runtime_error(ss.str()); - } - } catch (...) { - if (default_error_handler) { - default_error_handler(); - } - -#ifndef _MSC_VER - auto exception_type = abi::__cxa_current_exception_type(); - MS_EXCEPTION_IF_NULL(exception_type); - std::string ex_name(exception_type->name()); - MS_LOG(EXCEPTION) << "Error occurred. Exception name: " << ex_name; -#else - MS_LOG(EXCEPTION) << "Error occurred."; -#endif - } -} -} // namespace mindspore diff --git a/mindspore/core/utils/log_adapter.cc b/mindspore/core/utils/log_adapter.cc index 12020178fad..1c8db04f5e0 100644 --- a/mindspore/core/utils/log_adapter.cc +++ b/mindspore/core/utils/log_adapter.cc @@ -384,7 +384,11 @@ void LogWriter::operator^(const LogStream &stream) const { const auto &exception_handler = GetExceptionHandler(); if (exception_handler != nullptr) { exception_handler(exception_type_, oss.str()); + } else { + oss << "[Runtime error for null exception handler]"; + throw std::runtime_error(oss.str()); } + oss << "[Runtime error for unknown exception type:" << exception_type_ << "]"; throw std::runtime_error(oss.str()); }