From 813b2ade342a991f3ed08ff31533a4e05b534a23 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Tue, 27 Oct 2020 11:07:44 +0800 Subject: [PATCH] Enrich the python error types of paddle & polish format (#28124) * add multiple exception type * define all exception & polish compile pystack * mapping paddle error to python exception * polish static mode error format * fix failed unittests * fix dytostatic test_error * fix check_nan_inf failed * add unittest for coverage * revert some code try to solve compile error * refactor enforce & error change * polish code & add unittest --- paddle/fluid/framework/op_call_stack.cc | 36 ++- paddle/fluid/platform/enforce.h | 289 +++++++++--------- paddle/fluid/platform/errors.cc | 2 +- paddle/fluid/platform/errors.h | 2 +- paddle/fluid/pybind/exception.cc | 47 ++- .../tests/unittests/check_nan_inf_base.py | 4 +- .../unittests/dygraph_to_static/test_error.py | 13 +- .../fluid/tests/unittests/test_assert_op.py | 12 +- .../unittests/test_c_comm_init_all_op.py | 4 +- .../fluid/tests/unittests/test_cholesky_op.py | 11 +- .../fluid/tests/unittests/test_exception.py | 46 ++- .../tests/unittests/test_fill_any_like_op.py | 3 +- .../tests/unittests/test_histogram_op.py | 7 +- .../tests/unittests/test_imperative_basic.py | 3 +- .../test_imperative_data_loader_process.py | 2 +- .../unittests/test_imperative_double_grad.py | 2 +- .../test_imperative_signal_handler.py | 8 +- .../fluid/tests/unittests/test_inverse_op.py | 11 +- .../tests/unittests/test_multinomial_op.py | 9 +- .../fluid/tests/unittests/test_multiply.py | 8 +- .../test_multiprocess_reader_exception.py | 5 +- .../unittests/test_nn_functional_hot_op.py | 4 +- .../fluid/tests/unittests/test_one_hot_op.py | 4 +- .../tests/unittests/test_one_hot_v2_op.py | 4 +- ..._parallel_executor_feed_persistable_var.py | 6 +- .../paddle/fluid/tests/unittests/test_pow.py | 4 +- .../tests/unittests/test_retain_graph.py | 3 +- .../fluid/tests/unittests/test_reverse_op.py | 4 +- .../test_runtime_and_compiletime_exception.py | 6 +- .../fluid/tests/unittests/test_tensor.py | 2 +- 30 files changed, 360 insertions(+), 201 deletions(-) diff --git a/paddle/fluid/framework/op_call_stack.cc b/paddle/fluid/framework/op_call_stack.cc index 80db35e0c39..380ba74a1cb 100644 --- a/paddle/fluid/framework/op_call_stack.cc +++ b/paddle/fluid/framework/op_call_stack.cc @@ -21,6 +21,18 @@ limitations under the License. */ namespace paddle { namespace framework { +std::string InsertIndentationIntoEachLine(const std::string &str) { + std::ostringstream sout; + size_t start_pos = 0; + size_t end_pos = 0; + while ((end_pos = str.find("\n", start_pos)) != std::string::npos) { + sout << " " << str.substr(start_pos, end_pos + 1); + start_pos = end_pos + 1; + } + sout << " " << str.substr(start_pos, end_pos); + return sout.str(); +} + void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs, platform::EnforceNotMet *exception) { if (attrs.count("sub_block") != 0) { @@ -37,23 +49,37 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs, std::ostringstream sout; // Step 1. Construct python call stack string if (callstack) { - sout << "\n\n Compile Traceback (most recent call last):"; + if (FLAGS_call_stack_level > 1) { + sout << "\n\n Compile Traceback (most recent call last):"; + } else { + sout << "In user code:\n"; + } for (auto &line : *callstack) { sout << "\n " << line; } } // Step 2. Construct final call stack & append error op name - sout << exception->err_str_; + if (FLAGS_call_stack_level > 1) { + sout << exception->what(); + } else { + // If callstack exists, use err_str_ instead sub_err_str_ + if (callstack) { + sout << "\n\n"; + sout << InsertIndentationIntoEachLine(exception->error_str()); + } else { + sout << exception->simple_error_str(); + } + } sout << " [operator < " << type << " > error]"; - exception->err_str_ = sout.str(); + exception->set_error_str(sout.str()); } void AppendErrorOpHint(const std::string &type, platform::EnforceNotMet *exception) { std::ostringstream sout; - sout << exception->err_str_; + sout << exception->what(); sout << " [operator < " << type << " > error]"; - exception->err_str_ = sout.str(); + exception->set_error_str(sout.str()); } } // namespace framework diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index 165321d9c87..6a272498170 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -39,7 +39,6 @@ limitations under the License. */ #include #include -#include #include #include #include @@ -275,8 +274,10 @@ template inline std::string GetErrorSumaryString(StrType&& what, const char* file, int line) { std::ostringstream sout; - sout << "\n----------------------\nError Message " - "Summary:\n----------------------\n"; + if (FLAGS_call_stack_level > 1) { + sout << "\n----------------------\nError Message " + "Summary:\n----------------------\n"; + } sout << string::Sprintf("%s (at %s:%d)", std::forward(what), file, line) << std::endl; @@ -294,41 +295,89 @@ inline std::string GetTraceBackString(StrType&& what, const char* file, } } -inline bool is_error(bool stat) { return !stat; } - -inline void throw_on_error(bool stat, const std::string& msg) { - throw std::runtime_error(msg); +inline std::string SimplifyErrorTypeFormat(const std::string& str) { + std::ostringstream sout; + size_t type_end_pos = str.find(":", 0); + if (type_end_pos == std::string::npos) { + sout << str; + } else { + // Remove "Error:", add "()"" + sout << "(" << str.substr(0, type_end_pos - 5) << ")" + << str.substr(type_end_pos + 1); + } + return sout.str(); } +inline bool is_error(bool stat) { return !stat; } + // Note: This Macro can only be used within enforce.h -#define __THROW_ERROR_INTERNAL__(...) \ - do { \ - HANDLE_THE_ERROR \ - throw ::paddle::platform::EnforceNotMet( \ - ::paddle::string::Sprintf(__VA_ARGS__), __FILE__, __LINE__); \ - END_HANDLE_THE_ERROR \ +#define __THROW_ERROR_INTERNAL__(__ERROR_SUMMARY) \ + do { \ + HANDLE_THE_ERROR \ + throw ::paddle::platform::EnforceNotMet(__ERROR_SUMMARY, __FILE__, \ + __LINE__); \ + END_HANDLE_THE_ERROR \ } while (0) /** ENFORCE EXCEPTION AND MACROS **/ struct EnforceNotMet : public std::exception { + public: EnforceNotMet(std::exception_ptr e, const char* file, int line) { try { std::rethrow_exception(e); + } catch (platform::EnforceNotMet& e) { + code_ = e.code(); + err_str_ = GetTraceBackString(e.what(), file, line); + simple_err_str_ = SimplifyErrorTypeFormat(err_str_); } catch (std::exception& e) { err_str_ = GetTraceBackString(e.what(), file, line); + simple_err_str_ = SimplifyErrorTypeFormat(err_str_); } } EnforceNotMet(const std::string& str, const char* file, int line) - : err_str_(GetTraceBackString(str, file, line)) {} + : err_str_(GetTraceBackString(str, file, line)) { + simple_err_str_ = SimplifyErrorTypeFormat(err_str_); + } - EnforceNotMet(const platform::ErrorSummary& error, const char* file, int line) - : err_str_(GetTraceBackString(error.ToString(), file, line)) {} + EnforceNotMet(const ErrorSummary& error, const char* file, int line) + : code_(error.code()), + err_str_(GetTraceBackString(error.to_string(), file, line)) { + simple_err_str_ = SimplifyErrorTypeFormat(err_str_); + } - const char* what() const noexcept override { return err_str_.c_str(); } + const char* what() const noexcept override { + if (FLAGS_call_stack_level > 1) { + return err_str_.c_str(); + } else { + return simple_err_str_.c_str(); + } + } + + error::Code code() const { return code_; } + + const std::string& error_str() const { return err_str_; } + + const std::string& simple_error_str() const { return simple_err_str_; } + + void set_error_str(std::string str) { + if (FLAGS_call_stack_level > 1) { + err_str_ = str; + } else { + simple_err_str_ = str; + } + } + private: + // Used to determine the final type of exception thrown + error::Code code_ = error::LEGACY; + // Complete error message + // e.g. InvalidArgumentError: *** std::string err_str_; + // Simple errror message used when no C++ stack and python compile stack + // e.g. (InvalidArgument) *** + std::string simple_err_str_; }; #define PADDLE_THROW(...) \ @@ -352,21 +401,12 @@ struct EnforceNotMet : public std::exception { } \ } while (0) #else -#define PADDLE_ENFORCE(COND, ...) \ - do { \ - auto __cond__ = (COND); \ - if (UNLIKELY(::paddle::platform::is_error(__cond__))) { \ - try { \ - ::paddle::platform::throw_on_error( \ - __cond__, \ - ::paddle::platform::ErrorSummary(__VA_ARGS__).ToString()); \ - } catch (...) { \ - HANDLE_THE_ERROR \ - throw ::paddle::platform::EnforceNotMet(std::current_exception(), \ - __FILE__, __LINE__); \ - END_HANDLE_THE_ERROR \ - } \ - } \ +#define PADDLE_ENFORCE(COND, ...) \ + do { \ + auto __cond__ = (COND); \ + if (UNLIKELY(::paddle::platform::is_error(__cond__))) { \ + __THROW_ERROR_INTERNAL__(::paddle::platform::ErrorSummary(__VA_ARGS__)); \ + } \ } while (0) #endif @@ -384,40 +424,46 @@ struct EnforceNotMet : public std::exception { * PADDLE_ENFORCE(a, b, "some simple enforce failed between %d numbers", 2) */ -#define PADDLE_ENFORCE_NOT_NULL(__VAL, ...) \ - do { \ - if (UNLIKELY(nullptr == (__VAL))) { \ - __THROW_ERROR_INTERNAL__( \ - "%s\n [Hint: " #__VAL " should not be null.]", \ - ::paddle::platform::ErrorSummary(__VA_ARGS__).ToString()); \ - } \ +#define PADDLE_ENFORCE_NOT_NULL(__VAL, ...) \ + do { \ + if (UNLIKELY(nullptr == (__VAL))) { \ + auto __summary__ = ::paddle::platform::ErrorSummary(__VA_ARGS__); \ + auto __message__ = ::paddle::string::Sprintf( \ + "%s\n [Hint: " #__VAL " should not be null.]", \ + __summary__.error_message()); \ + __THROW_ERROR_INTERNAL__( \ + ::paddle::platform::ErrorSummary(__summary__.code(), __message__)); \ + } \ } while (0) -#define __PADDLE_BINARY_COMPARE(__VAL1, __VAL2, __CMP, __INV_CMP, ...) \ - do { \ - auto __val1 = (__VAL1); \ - auto __val2 = (__VAL2); \ - using __TYPE1__ = decltype(__val1); \ - using __TYPE2__ = decltype(__val2); \ - using __COMMON_TYPE1__ = \ - ::paddle::platform::details::CommonType1<__TYPE1__, __TYPE2__>; \ - using __COMMON_TYPE2__ = \ - ::paddle::platform::details::CommonType2<__TYPE1__, __TYPE2__>; \ - bool __is_not_error = (static_cast<__COMMON_TYPE1__>(__val1))__CMP( \ - static_cast<__COMMON_TYPE2__>(__val2)); \ - if (UNLIKELY(!__is_not_error)) { \ - constexpr bool __kCanToString__ = \ - ::paddle::platform::details::CanToString<__TYPE1__>::kValue && \ - ::paddle::platform::details::CanToString<__TYPE2__>::kValue; \ - __THROW_ERROR_INTERNAL__( \ - "%s\n [Hint: Expected %s " #__CMP \ - " %s, but received %s " #__INV_CMP " %s.]", \ - ::paddle::platform::ErrorSummary(__VA_ARGS__).ToString(), #__VAL1, \ - #__VAL2, ::paddle::platform::details::BinaryCompareMessageConverter< \ - __kCanToString__>::Convert(#__VAL1, __val1), \ - ::paddle::platform::details::BinaryCompareMessageConverter< \ - __kCanToString__>::Convert(#__VAL2, __val2)); \ - } \ +#define __PADDLE_BINARY_COMPARE(__VAL1, __VAL2, __CMP, __INV_CMP, ...) \ + do { \ + auto __val1 = (__VAL1); \ + auto __val2 = (__VAL2); \ + using __TYPE1__ = decltype(__val1); \ + using __TYPE2__ = decltype(__val2); \ + using __COMMON_TYPE1__ = \ + ::paddle::platform::details::CommonType1<__TYPE1__, __TYPE2__>; \ + using __COMMON_TYPE2__ = \ + ::paddle::platform::details::CommonType2<__TYPE1__, __TYPE2__>; \ + bool __is_not_error = (static_cast<__COMMON_TYPE1__>(__val1))__CMP( \ + static_cast<__COMMON_TYPE2__>(__val2)); \ + if (UNLIKELY(!__is_not_error)) { \ + auto __summary__ = ::paddle::platform::ErrorSummary(__VA_ARGS__); \ + constexpr bool __kCanToString__ = \ + ::paddle::platform::details::CanToString<__TYPE1__>::kValue && \ + ::paddle::platform::details::CanToString<__TYPE2__>::kValue; \ + auto __message__ = ::paddle::string::Sprintf( \ + "%s\n [Hint: Expected %s " #__CMP \ + " %s, but received %s " #__INV_CMP " %s.]", \ + __summary__.error_message(), #__VAL1, #__VAL2, \ + ::paddle::platform::details::BinaryCompareMessageConverter< \ + __kCanToString__>::Convert(#__VAL1, __val1), \ + ::paddle::platform::details::BinaryCompareMessageConverter< \ + __kCanToString__>::Convert(#__VAL2, __val2)); \ + __THROW_ERROR_INTERNAL__( \ + ::paddle::platform::ErrorSummary(__summary__.code(), __message__)); \ + } \ } while (0) #define PADDLE_ENFORCE_EQ(__VAL0, __VAL1, ...) \ @@ -458,26 +504,28 @@ struct EnforceNotMet : public std::exception { * Examples: * GET_DATA_SAFELY(ctx.Input("X"), "Input", "X", "Mul"); */ -#define GET_DATA_SAFELY(__PTR, __ROLE, __NAME, __OP_TYPE) \ - (([&]() -> std::add_lvalue_reference::type { \ - auto* __ptr = (__PTR); \ - if (UNLIKELY(nullptr == __ptr)) { \ - __THROW_ERROR_INTERNAL__( \ - "%s\n [Hint: pointer " #__PTR " should not be null.]", \ - paddle::platform::errors::NotFound( \ - "Unable to get %s data of %s %s in operator %s. " \ - "Possible reasons are:\n" \ - " 1. The %s is not the %s of operator %s;\n" \ - " 2. The %s has no corresponding variable passed in;\n" \ - " 3. The %s corresponding variable is not initialized.", \ - paddle::platform::demangle( \ - typeid(std::add_lvalue_reference::type) \ - .name()), \ - __ROLE, __NAME, __OP_TYPE, __NAME, __ROLE, __OP_TYPE, __NAME, \ - __NAME) \ - .ToString()); \ - } \ - return *__ptr; \ +#define GET_DATA_SAFELY(__PTR, __ROLE, __NAME, __OP_TYPE) \ + (([&]() -> std::add_lvalue_reference::type { \ + auto* __ptr = (__PTR); \ + if (UNLIKELY(nullptr == __ptr)) { \ + auto __summary__ = paddle::platform::errors::NotFound( \ + "Unable to get %s data of %s %s in operator %s. " \ + "Possible reasons are:\n" \ + " 1. The %s is not the %s of operator %s;\n" \ + " 2. The %s has no corresponding variable passed in;\n" \ + " 3. The %s corresponding variable is not initialized.", \ + paddle::platform::demangle( \ + typeid(std::add_lvalue_reference::type) \ + .name()), \ + __ROLE, __NAME, __OP_TYPE, __NAME, __ROLE, __OP_TYPE, __NAME, \ + __NAME); \ + auto __message__ = ::paddle::string::Sprintf( \ + "%s\n [Hint: pointer " #__PTR " should not be null.]", \ + __summary__.error_message()); \ + __THROW_ERROR_INTERNAL__( \ + ::paddle::platform::ErrorSummary(__summary__.code(), __message__)); \ + } \ + return *__ptr; \ })()) /* @@ -584,13 +632,13 @@ struct EOFException : public std::exception { END_HANDLE_THE_ERROR \ } while (0) -#define PADDLE_THROW_BAD_ALLOC(...) \ - do { \ - HANDLE_THE_ERROR \ - throw ::paddle::memory::allocation::BadAlloc( \ - ::paddle::platform::ErrorSummary(__VA_ARGS__).ToString(), __FILE__, \ - __LINE__); \ - END_HANDLE_THE_ERROR \ +#define PADDLE_THROW_BAD_ALLOC(...) \ + do { \ + HANDLE_THE_ERROR \ + throw ::paddle::memory::allocation::BadAlloc( \ + ::paddle::platform::ErrorSummary(__VA_ARGS__).to_string(), __FILE__, \ + __LINE__); \ + END_HANDLE_THE_ERROR \ } while (0) /** CUDA PADDLE ENFORCE FUNCTIONS AND MACROS **/ @@ -687,10 +735,6 @@ inline std::string build_nvidia_error_msg(cudaError_t e) { return sout.str(); } -inline void throw_on_error(cudaError_t e, const std::string& msg) { - throw std::runtime_error(msg); -} - /** curand ERROR **/ inline bool is_error(curandStatus_t stat) { return stat != CURAND_STATUS_SUCCESS; @@ -734,11 +778,6 @@ inline std::string build_nvidia_error_msg(curandStatus_t stat) { return msg + curandGetErrorString(stat) + " "; } -inline void throw_on_error(curandStatus_t stat, const std::string& msg) { - throw thrust::system_error(cudaErrorLaunchFailure, thrust::cuda_category(), - msg); -} - /***** CUDNN ERROR *****/ inline bool is_error(cudnnStatus_t stat) { return stat != CUDNN_STATUS_SUCCESS; @@ -749,10 +788,6 @@ inline std::string build_nvidia_error_msg(cudnnStatus_t stat) { return msg + platform::dynload::cudnnGetErrorString(stat) + " "; } -inline void throw_on_error(cudnnStatus_t stat, const std::string& msg) { - throw std::runtime_error(msg); -} - /***** CUBLAS ERROR *****/ inline bool is_error(cublasStatus_t stat) { return stat != CUBLAS_STATUS_SUCCESS; @@ -788,10 +823,6 @@ inline std::string build_nvidia_error_msg(cublasStatus_t stat) { return msg + cublasGetErrorString(stat) + " "; } -inline void throw_on_error(cublasStatus_t stat, const std::string& msg) { - throw std::runtime_error(msg); -} - /***** CUSOLVER ERROR *****/ inline bool is_error(cusolverStatus_t stat) { return stat != CUSOLVER_STATUS_SUCCESS; @@ -817,15 +848,12 @@ inline const char* cusolverGetErrorString(cusolverStatus_t stat) { return "Unknown cusolver status"; } } + inline std::string build_nvidia_error_msg(cusolverStatus_t stat) { std::string msg(" Cublas error, "); return msg + cusolverGetErrorString(stat) + " "; } -inline void throw_on_error(cusolverStatus_t stat, const std::string& msg) { - throw std::runtime_error(msg); -} - /****** NCCL ERROR ******/ #if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) inline bool is_error(ncclResult_t nccl_result) { @@ -836,10 +864,6 @@ inline std::string build_nvidia_error_msg(ncclResult_t nccl_result) { std::string msg(" Nccl error, "); return msg + platform::dynload::ncclGetErrorString(nccl_result) + " "; } - -inline void throw_on_error(ncclResult_t nccl_result, const std::string& msg) { - throw std::runtime_error(msg); -} #endif // not(__APPLE__) and PADDLE_WITH_NCCL namespace details { @@ -866,27 +890,18 @@ DEFINE_CUDA_STATUS_TYPE(ncclResult_t, ncclSuccess); } // namespace details -#define PADDLE_ENFORCE_CUDA_SUCCESS(COND) \ - do { \ - auto __cond__ = (COND); \ - using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ - constexpr auto __success_type__ = \ - ::paddle::platform::details::CudaStatusType< \ - __CUDA_STATUS_TYPE__>::kSuccess; \ - if (UNLIKELY(__cond__ != __success_type__)) { \ - try { \ - ::paddle::platform::throw_on_error( \ - __cond__, \ - ::paddle::platform::errors::External( \ - ::paddle::platform::build_nvidia_error_msg(__cond__)) \ - .ToString()); \ - } catch (...) { \ - HANDLE_THE_ERROR \ - throw ::paddle::platform::EnforceNotMet(std::current_exception(), \ - __FILE__, __LINE__); \ - END_HANDLE_THE_ERROR \ - } \ - } \ +#define PADDLE_ENFORCE_CUDA_SUCCESS(COND) \ + do { \ + auto __cond__ = (COND); \ + using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ + constexpr auto __success_type__ = \ + ::paddle::platform::details::CudaStatusType< \ + __CUDA_STATUS_TYPE__>::kSuccess; \ + if (UNLIKELY(__cond__ != __success_type__)) { \ + auto __summary__ = ::paddle::platform::errors::External( \ + ::paddle::platform::build_nvidia_error_msg(__cond__)); \ + __THROW_ERROR_INTERNAL__(__summary__); \ + } \ } while (0) #undef DEFINE_CUDA_STATUS_TYPE diff --git a/paddle/fluid/platform/errors.cc b/paddle/fluid/platform/errors.cc index 85beefa59de..94a182f9656 100644 --- a/paddle/fluid/platform/errors.cc +++ b/paddle/fluid/platform/errors.cc @@ -68,7 +68,7 @@ std::string error_name(Code code) { } } -std::string ErrorSummary::ToString() const { +std::string ErrorSummary::to_string() const { std::string result(error_name(code())); result += ": "; result += error_message(); diff --git a/paddle/fluid/platform/errors.h b/paddle/fluid/platform/errors.h index a2f2e7c130c..6bcd5cf39f2 100644 --- a/paddle/fluid/platform/errors.h +++ b/paddle/fluid/platform/errors.h @@ -52,7 +52,7 @@ class ErrorSummary { const std::string& error_message() const { return msg_; } - std::string ToString() const; + std::string to_string() const; private: Code code_; diff --git a/paddle/fluid/pybind/exception.cc b/paddle/fluid/pybind/exception.cc index 776d4808062..3d07985ff65 100644 --- a/paddle/fluid/pybind/exception.cc +++ b/paddle/fluid/pybind/exception.cc @@ -17,6 +17,21 @@ limitations under the License. */ namespace paddle { namespace pybind { +/* Paddle Exception mapping rules: + * - InvalidArgumentError -> ValueError + * - NotFoundError -> RuntimeError + * - OutOfRangeError -> IndexError + * - AlreadyExistsError -> RuntimeError + * - ResourceExhaustedError -> MemoryError + * - PreconditionNotMetError -> RuntimeError + * - PermissionDeniedError -> RuntimeError + * - ExecutionTimeoutError -> RuntimeError + * - UnimplementedError -> NotImplementedError + * - UnavailableError -> RuntimeError + * - FatalError -> SystemError + * - ExternalError -> OSError + */ + void BindException(pybind11::module* m) { static pybind11::exception eof(*m, "EOFException"); static pybind11::exception exc(*m, "EnforceNotMet"); @@ -26,7 +41,37 @@ void BindException(pybind11::module* m) { } catch (const platform::EOFException& e) { eof(e.what()); } catch (const platform::EnforceNotMet& e) { - exc(e.what()); + switch (e.code()) { + case paddle::platform::error::INVALID_ARGUMENT: + PyErr_SetString(PyExc_ValueError, e.what()); + break; + case paddle::platform::error::NOT_FOUND: + case paddle::platform::error::ALREADY_EXISTS: + case paddle::platform::error::PRECONDITION_NOT_MET: + case paddle::platform::error::PERMISSION_DENIED: + case paddle::platform::error::EXECUTION_TIMEOUT: + case paddle::platform::error::UNAVAILABLE: + PyErr_SetString(PyExc_RuntimeError, e.what()); + break; + case paddle::platform::error::OUT_OF_RANGE: + PyErr_SetString(PyExc_IndexError, e.what()); + break; + case paddle::platform::error::RESOURCE_EXHAUSTED: + PyErr_SetString(PyExc_MemoryError, e.what()); + break; + case paddle::platform::error::UNIMPLEMENTED: + PyErr_SetString(PyExc_NotImplementedError, e.what()); + break; + case paddle::platform::error::FATAL: + PyErr_SetString(PyExc_SystemError, e.what()); + break; + case paddle::platform::error::EXTERNAL: + PyErr_SetString(PyExc_OSError, e.what()); + break; + default: + exc(e.what()); + break; + } } }); diff --git a/python/paddle/fluid/tests/unittests/check_nan_inf_base.py b/python/paddle/fluid/tests/unittests/check_nan_inf_base.py index c682c795019..1c5db616306 100644 --- a/python/paddle/fluid/tests/unittests/check_nan_inf_base.py +++ b/python/paddle/fluid/tests/unittests/check_nan_inf_base.py @@ -112,11 +112,11 @@ if __name__ == '__main__': print(type(e)) # Note. Enforce in cuda kernel may not catch in paddle, and # Exception type will be RuntimeError - assert type(e) == core.EnforceNotMet or type(e) == RuntimeError + assert type(e) == OSError or type(e) == RuntimeError try: check(use_cuda=False) assert False except Exception as e: print(e) print(type(e)) - assert type(e) == core.EnforceNotMet + assert type(e) == RuntimeError diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py index 2998ba85757..82f4bd763a2 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py @@ -20,7 +20,6 @@ import unittest import numpy as np import paddle import paddle.fluid as fluid -from paddle.fluid.core import EnforceNotMet from paddle.fluid.dygraph.dygraph_to_static import error from paddle.fluid.dygraph.dygraph_to_static.origin_info import unwrap @@ -76,9 +75,9 @@ class TestErrorInCompileTime(unittest.TestCase): def set_message(self): self.expected_message = \ - ['File "{}", line 36, in func_error_in_compile_time'.format(self.filepath), + ['File "{}", line 35, in func_error_in_compile_time'.format(self.filepath), 'inner_func()', - 'File "{}", line 29, in inner_func'.format(self.filepath), + 'File "{}", line 28, in inner_func'.format(self.filepath), 'fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")', ] @@ -130,13 +129,13 @@ class TestErrorInCompileTime2(TestErrorInCompileTime): self.func = func_error_in_compile_time_2 def set_exception_type(self): - self.exception_type = EnforceNotMet + self.exception_type = ValueError def set_message(self): self.expected_message = \ [ - 'File "{}", line 47, in func_error_in_compile_time_2'.format(self.filepath), + 'File "{}", line 46, in func_error_in_compile_time_2'.format(self.filepath), 'x = fluid.layers.reshape(x, shape=[1, 2])' ] @@ -146,12 +145,12 @@ class TestErrorInRuntime(TestErrorInCompileTime): self.func = func_error_in_runtime def set_exception_type(self): - self.exception_type = EnforceNotMet + self.exception_type = ValueError def set_message(self): self.expected_message = \ [ - 'File "{}", line 55, in func_error_in_runtime'.format(self.filepath), + 'File "{}", line 54, in func_error_in_runtime'.format(self.filepath), 'x = fluid.layers.reshape(x, shape=[1, two])' ] diff --git a/python/paddle/fluid/tests/unittests/test_assert_op.py b/python/paddle/fluid/tests/unittests/test_assert_op.py index 47dbb1092c5..f7ab991de56 100644 --- a/python/paddle/fluid/tests/unittests/test_assert_op.py +++ b/python/paddle/fluid/tests/unittests/test_assert_op.py @@ -14,6 +14,7 @@ from __future__ import print_function +import paddle import paddle.fluid as fluid import paddle.fluid.layers as layers import unittest @@ -42,7 +43,7 @@ class TestAssertOp(unittest.TestCase): shape=[1], dtype='bool', value=False) layers.Assert(condition) - with self.assertRaises(fluid.core.EnforceNotMet): + with self.assertRaises(ValueError): self.run_network(net_func) def test_assert_cond_numel_error(self): @@ -51,7 +52,7 @@ class TestAssertOp(unittest.TestCase): shape=[1, 2], dtype='bool', value=True) layers.Assert(condition, []) - with self.assertRaises(fluid.core.EnforceNotMet): + with self.assertRaises(ValueError): self.run_network(net_func) def test_assert_print_data(self): @@ -62,7 +63,7 @@ class TestAssertOp(unittest.TestCase): layers.Assert(condition, [zero, one]) print("test_assert_print_data") - with self.assertRaises(fluid.core.EnforceNotMet): + with self.assertRaises(ValueError): self.run_network(net_func) def test_assert_summary(self): @@ -72,7 +73,7 @@ class TestAssertOp(unittest.TestCase): layers.Assert(condition, (x, ), 5) print("test_assert_summary") - with self.assertRaises(fluid.core.EnforceNotMet): + with self.assertRaises(ValueError): self.run_network(net_func) def test_assert_summary_greater_than_size(self): @@ -82,9 +83,10 @@ class TestAssertOp(unittest.TestCase): layers.Assert(condition, [x], 10, name="test") print("test_assert_summary_greater_than_size") - with self.assertRaises(fluid.core.EnforceNotMet): + with self.assertRaises(ValueError): self.run_network(net_func) if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_c_comm_init_all_op.py b/python/paddle/fluid/tests/unittests/test_c_comm_init_all_op.py index 042f03e19ab..a7f4a15381b 100644 --- a/python/paddle/fluid/tests/unittests/test_c_comm_init_all_op.py +++ b/python/paddle/fluid/tests/unittests/test_c_comm_init_all_op.py @@ -15,6 +15,7 @@ from __future__ import print_function import unittest +import paddle import paddle.fluid.core as core import paddle.fluid as fluid @@ -34,7 +35,7 @@ class TestCCommInitAllOp(unittest.TestCase): program = fluid.Program() block = program.global_block() block.append_op(type='c_comm_init_all', attrs={'ring_id': 0}) - with self.assertRaises(core.EnforceNotMet): + with self.assertRaises(ValueError): self.exe.run(program) def test_specifying_devices(self): @@ -47,4 +48,5 @@ class TestCCommInitAllOp(unittest.TestCase): if __name__ == "__main__": + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_cholesky_op.py b/python/paddle/fluid/tests/unittests/test_cholesky_op.py index 2fcec657c14..93f62b20f29 100644 --- a/python/paddle/fluid/tests/unittests/test_cholesky_op.py +++ b/python/paddle/fluid/tests/unittests/test_cholesky_op.py @@ -118,7 +118,10 @@ class TestCholeskySingularAPI(unittest.TestCase): fetches = exe.run(fluid.default_main_program(), feed={"input": input_np}, fetch_list=[result]) - except fluid.core.EnforceNotMet as ex: + except RuntimeError as ex: + print("The mat is singular") + pass + except ValueError as ex: print("The mat is singular") pass @@ -135,10 +138,14 @@ class TestCholeskySingularAPI(unittest.TestCase): input = fluid.dygraph.to_variable(input_np) try: result = paddle.cholesky(input) - except fluid.core.EnforceNotMet as ex: + except RuntimeError as ex: + print("The mat is singular") + pass + except ValueError as ex: print("The mat is singular") pass if __name__ == "__main__": + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_exception.py b/python/paddle/fluid/tests/unittests/test_exception.py index 7dd6047968f..adc7386bdeb 100644 --- a/python/paddle/fluid/tests/unittests/test_exception.py +++ b/python/paddle/fluid/tests/unittests/test_exception.py @@ -14,9 +14,13 @@ from __future__ import print_function +import numpy +import unittest + +import paddle +import paddle.fluid as fluid import paddle.compat as cpt import paddle.fluid.core as core -import unittest class TestException(unittest.TestCase): @@ -24,7 +28,7 @@ class TestException(unittest.TestCase): exception = None try: core.__unittest_throw_exception__() - except core.EnforceNotMet as ex: + except RuntimeError as ex: self.assertIn("This is a test of exception", cpt.get_exception_message(ex)) exception = ex @@ -32,5 +36,43 @@ class TestException(unittest.TestCase): self.assertIsNotNone(exception) +class TestExceptionNoCStack(unittest.TestCase): + def setUp(self): + paddle.enable_static() + # test no C++ stack format + fluid.set_flags({'FLAGS_call_stack_level': 1}) + + def test_exception_in_static_mode(self): + x = fluid.layers.data(name='X', shape=[-1, 13], dtype='float32') + y = fluid.layers.data(name='Y', shape=[-1, 1], dtype='float32') + predict = fluid.layers.fc(input=x, size=1, act=None) + loss = fluid.layers.square_error_cost(input=predict, label=y) + avg_loss = fluid.layers.mean(loss) + + fluid.optimizer.SGD(learning_rate=0.01).minimize(avg_loss) + + place = fluid.CPUPlace() + exe = fluid.Executor(place) + exe.run(fluid.default_startup_program()) + + x = numpy.random.random(size=(8, 12)).astype('float32') + y = numpy.random.random(size=(8, 1)).astype('float32') + + with self.assertRaises(ValueError): + exe.run(fluid.default_main_program(), + feed={'X': x, + 'Y': y}, + fetch_list=[avg_loss.name]) + + def test_exception_in_dynamic_mode(self): + place = fluid.CPUPlace() + with fluid.dygraph.guard(place): + x = numpy.random.random(size=(10, 2)).astype('float32') + linear = fluid.dygraph.Linear(1, 10) + data = fluid.dygraph.to_variable(x) + with self.assertRaises(ValueError): + res = linear(data) + + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py b/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py index b18b5456c12..5bc2d1cda18 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_any_like_op.py @@ -87,7 +87,7 @@ class TestFillAnyLikeOpOverflow(TestFillAnyLikeOp): exception = None try: self.check_output(check_dygraph=False) - except core.EnforceNotMet as ex: + except ValueError as ex: exception = ex self.assertIsNotNone(exception) @@ -98,4 +98,5 @@ class TestFillAnyLikeOpFloat16(TestFillAnyLikeOp): if __name__ == "__main__": + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_histogram_op.py b/python/paddle/fluid/tests/unittests/test_histogram_op.py index f540b885e12..7da9dbd62e9 100644 --- a/python/paddle/fluid/tests/unittests/test_histogram_op.py +++ b/python/paddle/fluid/tests/unittests/test_histogram_op.py @@ -77,7 +77,7 @@ class TestHistogramOpError(unittest.TestCase): shape=[3, 4], dtype='float32', value=3.0) paddle.histogram(input=input_value, bins=-1, min=1, max=5) - with self.assertRaises(fluid.core.EnforceNotMet): + with self.assertRaises(IndexError): self.run_network(net_func) def test_min_max_error(self): @@ -88,7 +88,7 @@ class TestHistogramOpError(unittest.TestCase): shape=[3, 4], dtype='float32', value=3.0) paddle.histogram(input=input_value, bins=1, min=5, max=1) - with self.assertRaises(fluid.core.EnforceNotMet): + with self.assertRaises(ValueError): self.run_network(net_func) def test_min_max_range_error(self): @@ -99,7 +99,7 @@ class TestHistogramOpError(unittest.TestCase): shape=[3, 4], dtype='float32', value=3.0) paddle.histogram(input=input_value, bins=1, min=-np.inf, max=5) - with self.assertRaises(fluid.core.EnforceNotMet): + with self.assertRaises(ValueError): self.run_network(net_func) def test_type_errors(self): @@ -138,4 +138,5 @@ class TestHistogramOp(OpTest): if __name__ == "__main__": + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_basic.py b/python/paddle/fluid/tests/unittests/test_imperative_basic.py index 7378975aa37..8892c08a470 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_basic.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_basic.py @@ -333,7 +333,7 @@ class TestImperative(unittest.TestCase): try: new_variable.numpy() except Exception as e: - assert type(e) == core.EnforceNotMet + assert type(e) == ValueError try: new_variable.backward() @@ -689,4 +689,5 @@ class TestDygraphGuardWithError(unittest.TestCase): if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_process.py b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_process.py index 9b2d71c9f90..2a3a1e8b0a3 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_process.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_process.py @@ -96,7 +96,7 @@ class TestDygraphDataLoaderProcess(unittest.TestCase): exception = None try: _reader_process_loop(loader._batch_reader, loader._data_queue) - except core.EnforceNotMet as ex: + except ValueError as ex: exception = ex self.assertIsNotNone(exception) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py b/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py index 600ee6d10e5..8f3116f6535 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py @@ -356,7 +356,7 @@ class TestRaiseNoDoubleGradOp(TestCase): loss.backward() def test_raise(self): - self.assertRaises(fluid.core.EnforceNotMet, self.raise_no_grad_op) + self.assertRaises(RuntimeError, self.raise_no_grad_op) if __name__ == '__main__': diff --git a/python/paddle/fluid/tests/unittests/test_imperative_signal_handler.py b/python/paddle/fluid/tests/unittests/test_imperative_signal_handler.py index a0da4b0efee..d783a2cc752 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_signal_handler.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_signal_handler.py @@ -50,8 +50,8 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase): set_child_signal_handler(id(self), test_process.pid) time.sleep(5) - except core.EnforceNotMet as ex: - self.assertIn("FatalError", cpt.get_exception_message(ex)) + except SystemError as ex: + self.assertIn("Fatal", cpt.get_exception_message(ex)) exception = ex self.assertIsNotNone(exception) @@ -68,7 +68,7 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase): set_child_signal_handler(id(self), test_process.pid) time.sleep(3) - except core.EnforceNotMet as ex: + except SystemError as ex: self.assertIn("Segmentation fault", cpt.get_exception_message(ex)) exception = ex @@ -86,7 +86,7 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase): set_child_signal_handler(id(self), test_process.pid) time.sleep(3) - except core.EnforceNotMet as ex: + except SystemError as ex: self.assertIn("Bus error", cpt.get_exception_message(ex)) exception = ex diff --git a/python/paddle/fluid/tests/unittests/test_inverse_op.py b/python/paddle/fluid/tests/unittests/test_inverse_op.py index fd540dcd741..85c4c6000a6 100644 --- a/python/paddle/fluid/tests/unittests/test_inverse_op.py +++ b/python/paddle/fluid/tests/unittests/test_inverse_op.py @@ -153,7 +153,10 @@ class TestInverseSingularAPI(unittest.TestCase): fetches = exe.run(fluid.default_main_program(), feed={"input": input_np}, fetch_list=[result]) - except fluid.core.EnforceNotMet as ex: + except RuntimeError as ex: + print("The mat is singular") + pass + except ValueError as ex: print("The mat is singular") pass @@ -168,10 +171,14 @@ class TestInverseSingularAPI(unittest.TestCase): input = fluid.dygraph.to_variable(input_np) try: result = paddle.inverse(input) - except fluid.core.EnforceNotMet as ex: + except RuntimeError as ex: + print("The mat is singular") + pass + except ValueError as ex: print("The mat is singular") pass if __name__ == "__main__": + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_multinomial_op.py b/python/paddle/fluid/tests/unittests/test_multinomial_op.py index db4978930e0..b22f6b80df7 100644 --- a/python/paddle/fluid/tests/unittests/test_multinomial_op.py +++ b/python/paddle/fluid/tests/unittests/test_multinomial_op.py @@ -186,29 +186,28 @@ class TestMultinomialError(unittest.TestCase): x = paddle.rand([4]) paddle.multinomial(x, num_samples=-2) - self.assertRaises(core.EnforceNotMet, test_num_sample_less_than_0) + self.assertRaises(ValueError, test_num_sample_less_than_0) def test_replacement_False(self): def test_samples_larger_than_categories(): x = paddle.rand([4]) paddle.multinomial(x, num_samples=5, replacement=False) - self.assertRaises(core.EnforceNotMet, - test_samples_larger_than_categories) + self.assertRaises(ValueError, test_samples_larger_than_categories) def test_input_probs_dim(self): def test_dim_larger_than_2(): x = paddle.rand([2, 3, 3]) paddle.multinomial(x) - self.assertRaises(core.EnforceNotMet, test_dim_larger_than_2) + self.assertRaises(ValueError, test_dim_larger_than_2) def test_dim_less_than_1(): x_np = np.random.random([]) x = paddle.to_tensor(x_np) paddle.multinomial(x) - self.assertRaises(core.EnforceNotMet, test_dim_less_than_1) + self.assertRaises(ValueError, test_dim_less_than_1) if __name__ == "__main__": diff --git a/python/paddle/fluid/tests/unittests/test_multiply.py b/python/paddle/fluid/tests/unittests/test_multiply.py index abd0c15dc72..09a2007c1ad 100755 --- a/python/paddle/fluid/tests/unittests/test_multiply.py +++ b/python/paddle/fluid/tests/unittests/test_multiply.py @@ -194,7 +194,7 @@ class TestMultiplyError(unittest.TestCase): with program_guard(Program(), Program()): x = paddle.static.data(name='x', shape=[20, 50], dtype=np.float64) y = paddle.static.data(name='y', shape=[20], dtype=np.float64) - self.assertRaises(fluid.core.EnforceNotMet, tensor.multiply, x, y) + self.assertRaises(ValueError, tensor.multiply, x, y) np.random.seed(7) # test dynamic computation graph: dtype can not be int8 @@ -203,21 +203,21 @@ class TestMultiplyError(unittest.TestCase): y_data = np.random.randn(200).astype(np.int8) x = paddle.to_tensor(x_data) y = paddle.to_tensor(y_data) - self.assertRaises(fluid.core.EnforceNotMet, paddle.multiply, x, y) + self.assertRaises(RuntimeError, paddle.multiply, x, y) # test dynamic computation graph: inputs must be broadcastable x_data = np.random.rand(200, 5) y_data = np.random.rand(200) x = paddle.to_tensor(x_data) y = paddle.to_tensor(y_data) - self.assertRaises(fluid.core.EnforceNotMet, paddle.multiply, x, y) + self.assertRaises(ValueError, paddle.multiply, x, y) # test dynamic computation graph: inputs must be broadcastable(python) x_data = np.random.rand(200, 5) y_data = np.random.rand(200) x = paddle.to_tensor(x_data) y = paddle.to_tensor(y_data) - self.assertRaises(fluid.core.EnforceNotMet, paddle.multiply, x, y) + self.assertRaises(ValueError, paddle.multiply, x, y) # test dynamic computation graph: dtype must be same x_data = np.random.randn(200).astype(np.int64) diff --git a/python/paddle/fluid/tests/unittests/test_multiprocess_reader_exception.py b/python/paddle/fluid/tests/unittests/test_multiprocess_reader_exception.py index 9634f5af30a..95e2462a2e2 100644 --- a/python/paddle/fluid/tests/unittests/test_multiprocess_reader_exception.py +++ b/python/paddle/fluid/tests/unittests/test_multiprocess_reader_exception.py @@ -98,7 +98,7 @@ class TestMultiprocessReaderException(unittest.TestCase): exe.run(feed=data, fetch_list=[image_p_1]) num += 1 self.assertEquals(num, batch_num) - except fluid.core.EnforceNotMet as ex: + except SystemError as ex: self.assertEquals(num, 0) raise ReaderException() else: @@ -113,7 +113,7 @@ class TestMultiprocessReaderException(unittest.TestCase): reader.reset() self.assertFalse(self.raise_exception) self.assertEquals(num, batch_num) - except fluid.core.EnforceNotMet as ex: + except SystemError as ex: self.assertTrue(self.raise_exception) self.assertEquals(num, 0) raise ReaderException() @@ -152,4 +152,5 @@ class TestCase3(TestMultiprocessReaderException): if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_nn_functional_hot_op.py b/python/paddle/fluid/tests/unittests/test_nn_functional_hot_op.py index 339f689998f..9b7ba5c4b05 100644 --- a/python/paddle/fluid/tests/unittests/test_nn_functional_hot_op.py +++ b/python/paddle/fluid/tests/unittests/test_nn_functional_hot_op.py @@ -18,6 +18,7 @@ import unittest import numpy as np import math from op_test import OpTest +import paddle import paddle.fluid as fluid import paddle.fluid.core as core import paddle.nn.functional as functional @@ -152,7 +153,7 @@ class TestOneHotOp_exception(unittest.TestCase): fetch_list=[one_hot_out], return_numpy=False) - self.assertRaises(core.EnforceNotMet, run) + self.assertRaises(ValueError, run) class TestOneHotOpApi(unittest.TestCase): @@ -204,4 +205,5 @@ class BadInputTestOnehotV2(unittest.TestCase): if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_one_hot_op.py b/python/paddle/fluid/tests/unittests/test_one_hot_op.py index 34a1087c9ff..e1da94c1219 100644 --- a/python/paddle/fluid/tests/unittests/test_one_hot_op.py +++ b/python/paddle/fluid/tests/unittests/test_one_hot_op.py @@ -18,6 +18,7 @@ import unittest import numpy as np import math from op_test import OpTest +import paddle import paddle.fluid as fluid import paddle.fluid.core as core import paddle.fluid.framework as framework @@ -172,7 +173,7 @@ class TestOneHotOp_exception(unittest.TestCase): fetch_list=[one_hot_out], return_numpy=False) - self.assertRaises(core.EnforceNotMet, run) + self.assertRaises(ValueError, run) class TestOneHotOpError(unittest.TestCase): @@ -200,4 +201,5 @@ class TestOneHotOpError(unittest.TestCase): if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_one_hot_v2_op.py b/python/paddle/fluid/tests/unittests/test_one_hot_v2_op.py index 9b6c2b1fd87..66de1b30979 100644 --- a/python/paddle/fluid/tests/unittests/test_one_hot_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_one_hot_v2_op.py @@ -18,6 +18,7 @@ import unittest import numpy as np import math from op_test import OpTest +import paddle import paddle.fluid as fluid import paddle.fluid.core as core import paddle.fluid.framework as framework @@ -169,7 +170,7 @@ class TestOneHotOp_exception(unittest.TestCase): fetch_list=[one_hot_out], return_numpy=False) - self.assertRaises(core.EnforceNotMet, run) + self.assertRaises(ValueError, run) class TestOneHotOpApi(unittest.TestCase): @@ -220,4 +221,5 @@ class BadInputTestOnehotV2(unittest.TestCase): if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_parallel_executor_feed_persistable_var.py b/python/paddle/fluid/tests/unittests/test_parallel_executor_feed_persistable_var.py index 831e2e76108..2597df7faff 100644 --- a/python/paddle/fluid/tests/unittests/test_parallel_executor_feed_persistable_var.py +++ b/python/paddle/fluid/tests/unittests/test_parallel_executor_feed_persistable_var.py @@ -16,6 +16,7 @@ from __future__ import print_function from functools import partial import numpy import unittest +import paddle import paddle.fluid.core as core import paddle.fluid as fluid from simple_nets import init_data, simple_fc_net @@ -76,13 +77,14 @@ class TestFeedPersistableVar(unittest.TestCase): self.feed_dict['learning_rate'] = numpy.array( [1.0, 1.0]).astype("float32") run = partial(self.check_feed_persistable_var, self.feed_dict) - self.assertRaises(core.EnforceNotMet, run) + self.assertRaises(RuntimeError, run) self.feed_dict['image'] = self.img[0, :] self.feed_dict['label'] = self.label[0, :] run = partial(self.check_feed_persistable_var, self.feed_dict) - self.assertRaises(core.EnforceNotMet, run) + self.assertRaises(RuntimeError, run) if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_pow.py b/python/paddle/fluid/tests/unittests/test_pow.py index 0764cb580e4..a468b24a79a 100755 --- a/python/paddle/fluid/tests/unittests/test_pow.py +++ b/python/paddle/fluid/tests/unittests/test_pow.py @@ -218,8 +218,8 @@ class TestPowerError(unittest.TestCase): np.random.randint(5, 10)) x = (np.random.rand(*dims) * 10).astype(np.float64) y = (np.random.rand(dims[-1] + 1) * 10).astype(np.float64) - self.assertRaises(fluid.core.EnforceNotMet, _run_power, DYNAMIC, x, y) - self.assertRaises(fluid.core.EnforceNotMet, _run_power, STATIC, x, y) + self.assertRaises(ValueError, _run_power, DYNAMIC, x, y) + self.assertRaises(ValueError, _run_power, STATIC, x, y) # test dynamic computation graph: inputs must be broadcastable dims = (np.random.randint(1, 10), np.random.randint(5, 10), diff --git a/python/paddle/fluid/tests/unittests/test_retain_graph.py b/python/paddle/fluid/tests/unittests/test_retain_graph.py index de94e0b0fcd..79664fe4b12 100644 --- a/python/paddle/fluid/tests/unittests/test_retain_graph.py +++ b/python/paddle/fluid/tests/unittests/test_retain_graph.py @@ -136,8 +136,7 @@ class TestRetainGraph(unittest.TestCase): def test_retain(self): self.run_retain(need_retain=True) - self.assertRaises( - fluid.core.EnforceNotMet, self.run_retain, need_retain=False) + self.assertRaises(RuntimeError, self.run_retain, need_retain=False) if __name__ == '__main__': diff --git a/python/paddle/fluid/tests/unittests/test_reverse_op.py b/python/paddle/fluid/tests/unittests/test_reverse_op.py index 5aaf0b85d50..9b739eff97c 100644 --- a/python/paddle/fluid/tests/unittests/test_reverse_op.py +++ b/python/paddle/fluid/tests/unittests/test_reverse_op.py @@ -17,6 +17,7 @@ from __future__ import print_function import unittest import numpy as np from op_test import OpTest +import paddle import paddle.fluid as fluid from paddle.fluid import core @@ -107,7 +108,7 @@ class TestCase4(unittest.TestCase): x = np.random.random(size=(10, 1, 1, 1, 1, 1, 1)).astype('int64') exe.run(train_program, feed={"label": x}) - self.assertRaises(core.EnforceNotMet, _run_program) + self.assertRaises(IndexError, _run_program) class TestReverseLoDTensorArray(unittest.TestCase): @@ -182,4 +183,5 @@ class TestReverseLoDTensorArray(unittest.TestCase): if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_runtime_and_compiletime_exception.py b/python/paddle/fluid/tests/unittests/test_runtime_and_compiletime_exception.py index 83c6ad84122..21fdeeeb3e6 100644 --- a/python/paddle/fluid/tests/unittests/test_runtime_and_compiletime_exception.py +++ b/python/paddle/fluid/tests/unittests/test_runtime_and_compiletime_exception.py @@ -17,6 +17,7 @@ from __future__ import print_function import unittest import numpy as np from op_test import OpTest +import paddle import paddle.fluid as fluid import paddle.fluid.core as core @@ -36,12 +37,12 @@ class TestRunTimeException(unittest.TestCase): x = np.random.random(size=(10)).astype('int64') exe.run(train_program, feed={"label": x}) - self.assertRaises(core.EnforceNotMet, _run_program) + self.assertRaises(ValueError, _run_program) class TestCompileTimeException(unittest.TestCase): def test_compile_time_exception(self): - self.assertRaises(core.EnforceNotMet, self.build_model) + self.assertRaises(ValueError, self.build_model) def build_model(self): train_program = fluid.Program() @@ -53,4 +54,5 @@ class TestCompileTimeException(unittest.TestCase): if __name__ == '__main__': + paddle.enable_static() unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_tensor.py b/python/paddle/fluid/tests/unittests/test_tensor.py index 03dffe4e5a2..f8f3eea78a6 100644 --- a/python/paddle/fluid/tests/unittests/test_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_tensor.py @@ -356,7 +356,7 @@ class TestTensor(unittest.TestCase): try: error_array = ["1", "2"] tensor.set(error_array, place) - except core.EnforceNotMet as ex: + except ValueError as ex: exception = ex self.assertIsNotNone(exception) -- GitLab