未验证 提交 813b2ade 编写于 作者: C Chen Weihang 提交者: GitHub

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
上级 a5c18204
...@@ -21,6 +21,18 @@ limitations under the License. */ ...@@ -21,6 +21,18 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace framework { 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, void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs,
platform::EnforceNotMet *exception) { platform::EnforceNotMet *exception) {
if (attrs.count("sub_block") != 0) { if (attrs.count("sub_block") != 0) {
...@@ -37,23 +49,37 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs, ...@@ -37,23 +49,37 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs,
std::ostringstream sout; std::ostringstream sout;
// Step 1. Construct python call stack string // Step 1. Construct python call stack string
if (callstack) { if (callstack) {
if (FLAGS_call_stack_level > 1) {
sout << "\n\n Compile Traceback (most recent call last):"; sout << "\n\n Compile Traceback (most recent call last):";
} else {
sout << "In user code:\n";
}
for (auto &line : *callstack) { for (auto &line : *callstack) {
sout << "\n " << line; sout << "\n " << line;
} }
} }
// Step 2. Construct final call stack & append error op name // 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]"; sout << " [operator < " << type << " > error]";
exception->err_str_ = sout.str(); exception->set_error_str(sout.str());
} }
void AppendErrorOpHint(const std::string &type, void AppendErrorOpHint(const std::string &type,
platform::EnforceNotMet *exception) { platform::EnforceNotMet *exception) {
std::ostringstream sout; std::ostringstream sout;
sout << exception->err_str_; sout << exception->what();
sout << " [operator < " << type << " > error]"; sout << " [operator < " << type << " > error]";
exception->err_str_ = sout.str(); exception->set_error_str(sout.str());
} }
} // namespace framework } // namespace framework
......
...@@ -39,7 +39,6 @@ limitations under the License. */ ...@@ -39,7 +39,6 @@ limitations under the License. */
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <iostream>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
...@@ -275,8 +274,10 @@ template <typename StrType> ...@@ -275,8 +274,10 @@ template <typename StrType>
inline std::string GetErrorSumaryString(StrType&& what, const char* file, inline std::string GetErrorSumaryString(StrType&& what, const char* file,
int line) { int line) {
std::ostringstream sout; std::ostringstream sout;
if (FLAGS_call_stack_level > 1) {
sout << "\n----------------------\nError Message " sout << "\n----------------------\nError Message "
"Summary:\n----------------------\n"; "Summary:\n----------------------\n";
}
sout << string::Sprintf("%s (at %s:%d)", std::forward<StrType>(what), file, sout << string::Sprintf("%s (at %s:%d)", std::forward<StrType>(what), file,
line) line)
<< std::endl; << std::endl;
...@@ -294,41 +295,89 @@ inline std::string GetTraceBackString(StrType&& what, const char* file, ...@@ -294,41 +295,89 @@ inline std::string GetTraceBackString(StrType&& what, const char* file,
} }
} }
inline bool is_error(bool stat) { return !stat; } inline std::string SimplifyErrorTypeFormat(const std::string& str) {
std::ostringstream sout;
inline void throw_on_error(bool stat, const std::string& msg) { size_t type_end_pos = str.find(":", 0);
throw std::runtime_error(msg); 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 // Note: This Macro can only be used within enforce.h
#define __THROW_ERROR_INTERNAL__(...) \ #define __THROW_ERROR_INTERNAL__(__ERROR_SUMMARY) \
do { \ do { \
HANDLE_THE_ERROR \ HANDLE_THE_ERROR \
throw ::paddle::platform::EnforceNotMet( \ throw ::paddle::platform::EnforceNotMet(__ERROR_SUMMARY, __FILE__, \
::paddle::string::Sprintf(__VA_ARGS__), __FILE__, __LINE__); \ __LINE__); \
END_HANDLE_THE_ERROR \ END_HANDLE_THE_ERROR \
} while (0) } while (0)
/** ENFORCE EXCEPTION AND MACROS **/ /** ENFORCE EXCEPTION AND MACROS **/
struct EnforceNotMet : public std::exception { struct EnforceNotMet : public std::exception {
public:
EnforceNotMet(std::exception_ptr e, const char* file, int line) { EnforceNotMet(std::exception_ptr e, const char* file, int line) {
try { try {
std::rethrow_exception(e); 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) { } catch (std::exception& e) {
err_str_ = GetTraceBackString(e.what(), file, line); err_str_ = GetTraceBackString(e.what(), file, line);
simple_err_str_ = SimplifyErrorTypeFormat(err_str_);
} }
} }
EnforceNotMet(const std::string& str, const char* file, int line) 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) EnforceNotMet(const ErrorSummary& error, const char* file, int line)
: err_str_(GetTraceBackString(error.ToString(), file, 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_; 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(...) \ #define PADDLE_THROW(...) \
...@@ -356,16 +405,7 @@ struct EnforceNotMet : public std::exception { ...@@ -356,16 +405,7 @@ struct EnforceNotMet : public std::exception {
do { \ do { \
auto __cond__ = (COND); \ auto __cond__ = (COND); \
if (UNLIKELY(::paddle::platform::is_error(__cond__))) { \ if (UNLIKELY(::paddle::platform::is_error(__cond__))) { \
try { \ __THROW_ERROR_INTERNAL__(::paddle::platform::ErrorSummary(__VA_ARGS__)); \
::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 \
} \
} \ } \
} while (0) } while (0)
#endif #endif
...@@ -387,9 +427,12 @@ struct EnforceNotMet : public std::exception { ...@@ -387,9 +427,12 @@ struct EnforceNotMet : public std::exception {
#define PADDLE_ENFORCE_NOT_NULL(__VAL, ...) \ #define PADDLE_ENFORCE_NOT_NULL(__VAL, ...) \
do { \ do { \
if (UNLIKELY(nullptr == (__VAL))) { \ if (UNLIKELY(nullptr == (__VAL))) { \
__THROW_ERROR_INTERNAL__( \ auto __summary__ = ::paddle::platform::ErrorSummary(__VA_ARGS__); \
auto __message__ = ::paddle::string::Sprintf( \
"%s\n [Hint: " #__VAL " should not be null.]", \ "%s\n [Hint: " #__VAL " should not be null.]", \
::paddle::platform::ErrorSummary(__VA_ARGS__).ToString()); \ __summary__.error_message()); \
__THROW_ERROR_INTERNAL__( \
::paddle::platform::ErrorSummary(__summary__.code(), __message__)); \
} \ } \
} while (0) } while (0)
...@@ -406,17 +449,20 @@ struct EnforceNotMet : public std::exception { ...@@ -406,17 +449,20 @@ struct EnforceNotMet : public std::exception {
bool __is_not_error = (static_cast<__COMMON_TYPE1__>(__val1))__CMP( \ bool __is_not_error = (static_cast<__COMMON_TYPE1__>(__val1))__CMP( \
static_cast<__COMMON_TYPE2__>(__val2)); \ static_cast<__COMMON_TYPE2__>(__val2)); \
if (UNLIKELY(!__is_not_error)) { \ if (UNLIKELY(!__is_not_error)) { \
auto __summary__ = ::paddle::platform::ErrorSummary(__VA_ARGS__); \
constexpr bool __kCanToString__ = \ constexpr bool __kCanToString__ = \
::paddle::platform::details::CanToString<__TYPE1__>::kValue && \ ::paddle::platform::details::CanToString<__TYPE1__>::kValue && \
::paddle::platform::details::CanToString<__TYPE2__>::kValue; \ ::paddle::platform::details::CanToString<__TYPE2__>::kValue; \
__THROW_ERROR_INTERNAL__( \ auto __message__ = ::paddle::string::Sprintf( \
"%s\n [Hint: Expected %s " #__CMP \ "%s\n [Hint: Expected %s " #__CMP \
" %s, but received %s " #__INV_CMP " %s.]", \ " %s, but received %s " #__INV_CMP " %s.]", \
::paddle::platform::ErrorSummary(__VA_ARGS__).ToString(), #__VAL1, \ __summary__.error_message(), #__VAL1, #__VAL2, \
#__VAL2, ::paddle::platform::details::BinaryCompareMessageConverter< \ ::paddle::platform::details::BinaryCompareMessageConverter< \
__kCanToString__>::Convert(#__VAL1, __val1), \ __kCanToString__>::Convert(#__VAL1, __val1), \
::paddle::platform::details::BinaryCompareMessageConverter< \ ::paddle::platform::details::BinaryCompareMessageConverter< \
__kCanToString__>::Convert(#__VAL2, __val2)); \ __kCanToString__>::Convert(#__VAL2, __val2)); \
__THROW_ERROR_INTERNAL__( \
::paddle::platform::ErrorSummary(__summary__.code(), __message__)); \
} \ } \
} while (0) } while (0)
...@@ -462,9 +508,7 @@ struct EnforceNotMet : public std::exception { ...@@ -462,9 +508,7 @@ struct EnforceNotMet : public std::exception {
(([&]() -> std::add_lvalue_reference<decltype(*(__PTR))>::type { \ (([&]() -> std::add_lvalue_reference<decltype(*(__PTR))>::type { \
auto* __ptr = (__PTR); \ auto* __ptr = (__PTR); \
if (UNLIKELY(nullptr == __ptr)) { \ if (UNLIKELY(nullptr == __ptr)) { \
__THROW_ERROR_INTERNAL__( \ auto __summary__ = paddle::platform::errors::NotFound( \
"%s\n [Hint: pointer " #__PTR " should not be null.]", \
paddle::platform::errors::NotFound( \
"Unable to get %s data of %s %s in operator %s. " \ "Unable to get %s data of %s %s in operator %s. " \
"Possible reasons are:\n" \ "Possible reasons are:\n" \
" 1. The %s is not the %s of operator %s;\n" \ " 1. The %s is not the %s of operator %s;\n" \
...@@ -474,8 +518,12 @@ struct EnforceNotMet : public std::exception { ...@@ -474,8 +518,12 @@ struct EnforceNotMet : public std::exception {
typeid(std::add_lvalue_reference<decltype(*__ptr)>::type) \ typeid(std::add_lvalue_reference<decltype(*__ptr)>::type) \
.name()), \ .name()), \
__ROLE, __NAME, __OP_TYPE, __NAME, __ROLE, __OP_TYPE, __NAME, \ __ROLE, __NAME, __OP_TYPE, __NAME, __ROLE, __OP_TYPE, __NAME, \
__NAME) \ __NAME); \
.ToString()); \ 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; \ return *__ptr; \
})()) })())
...@@ -588,7 +636,7 @@ struct EOFException : public std::exception { ...@@ -588,7 +636,7 @@ struct EOFException : public std::exception {
do { \ do { \
HANDLE_THE_ERROR \ HANDLE_THE_ERROR \
throw ::paddle::memory::allocation::BadAlloc( \ throw ::paddle::memory::allocation::BadAlloc( \
::paddle::platform::ErrorSummary(__VA_ARGS__).ToString(), __FILE__, \ ::paddle::platform::ErrorSummary(__VA_ARGS__).to_string(), __FILE__, \
__LINE__); \ __LINE__); \
END_HANDLE_THE_ERROR \ END_HANDLE_THE_ERROR \
} while (0) } while (0)
...@@ -687,10 +735,6 @@ inline std::string build_nvidia_error_msg(cudaError_t e) { ...@@ -687,10 +735,6 @@ inline std::string build_nvidia_error_msg(cudaError_t e) {
return sout.str(); return sout.str();
} }
inline void throw_on_error(cudaError_t e, const std::string& msg) {
throw std::runtime_error(msg);
}
/** curand ERROR **/ /** curand ERROR **/
inline bool is_error(curandStatus_t stat) { inline bool is_error(curandStatus_t stat) {
return stat != CURAND_STATUS_SUCCESS; return stat != CURAND_STATUS_SUCCESS;
...@@ -734,11 +778,6 @@ inline std::string build_nvidia_error_msg(curandStatus_t stat) { ...@@ -734,11 +778,6 @@ inline std::string build_nvidia_error_msg(curandStatus_t stat) {
return msg + curandGetErrorString(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 *****/ /***** CUDNN ERROR *****/
inline bool is_error(cudnnStatus_t stat) { inline bool is_error(cudnnStatus_t stat) {
return stat != CUDNN_STATUS_SUCCESS; return stat != CUDNN_STATUS_SUCCESS;
...@@ -749,10 +788,6 @@ inline std::string build_nvidia_error_msg(cudnnStatus_t stat) { ...@@ -749,10 +788,6 @@ inline std::string build_nvidia_error_msg(cudnnStatus_t stat) {
return msg + platform::dynload::cudnnGetErrorString(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 *****/ /***** CUBLAS ERROR *****/
inline bool is_error(cublasStatus_t stat) { inline bool is_error(cublasStatus_t stat) {
return stat != CUBLAS_STATUS_SUCCESS; return stat != CUBLAS_STATUS_SUCCESS;
...@@ -788,10 +823,6 @@ inline std::string build_nvidia_error_msg(cublasStatus_t stat) { ...@@ -788,10 +823,6 @@ inline std::string build_nvidia_error_msg(cublasStatus_t stat) {
return msg + cublasGetErrorString(stat) + " "; return msg + cublasGetErrorString(stat) + " ";
} }
inline void throw_on_error(cublasStatus_t stat, const std::string& msg) {
throw std::runtime_error(msg);
}
/***** CUSOLVER ERROR *****/ /***** CUSOLVER ERROR *****/
inline bool is_error(cusolverStatus_t stat) { inline bool is_error(cusolverStatus_t stat) {
return stat != CUSOLVER_STATUS_SUCCESS; return stat != CUSOLVER_STATUS_SUCCESS;
...@@ -817,15 +848,12 @@ inline const char* cusolverGetErrorString(cusolverStatus_t stat) { ...@@ -817,15 +848,12 @@ inline const char* cusolverGetErrorString(cusolverStatus_t stat) {
return "Unknown cusolver status"; return "Unknown cusolver status";
} }
} }
inline std::string build_nvidia_error_msg(cusolverStatus_t stat) { inline std::string build_nvidia_error_msg(cusolverStatus_t stat) {
std::string msg(" Cublas error, "); std::string msg(" Cublas error, ");
return msg + cusolverGetErrorString(stat) + " "; return msg + cusolverGetErrorString(stat) + " ";
} }
inline void throw_on_error(cusolverStatus_t stat, const std::string& msg) {
throw std::runtime_error(msg);
}
/****** NCCL ERROR ******/ /****** NCCL ERROR ******/
#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) #if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL)
inline bool is_error(ncclResult_t nccl_result) { inline bool is_error(ncclResult_t nccl_result) {
...@@ -836,10 +864,6 @@ inline std::string build_nvidia_error_msg(ncclResult_t nccl_result) { ...@@ -836,10 +864,6 @@ inline std::string build_nvidia_error_msg(ncclResult_t nccl_result) {
std::string msg(" Nccl error, "); std::string msg(" Nccl error, ");
return msg + platform::dynload::ncclGetErrorString(nccl_result) + " "; 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 #endif // not(__APPLE__) and PADDLE_WITH_NCCL
namespace details { namespace details {
...@@ -874,18 +898,9 @@ DEFINE_CUDA_STATUS_TYPE(ncclResult_t, ncclSuccess); ...@@ -874,18 +898,9 @@ DEFINE_CUDA_STATUS_TYPE(ncclResult_t, ncclSuccess);
::paddle::platform::details::CudaStatusType< \ ::paddle::platform::details::CudaStatusType< \
__CUDA_STATUS_TYPE__>::kSuccess; \ __CUDA_STATUS_TYPE__>::kSuccess; \
if (UNLIKELY(__cond__ != __success_type__)) { \ if (UNLIKELY(__cond__ != __success_type__)) { \
try { \ auto __summary__ = ::paddle::platform::errors::External( \
::paddle::platform::throw_on_error( \ ::paddle::platform::build_nvidia_error_msg(__cond__)); \
__cond__, \ __THROW_ERROR_INTERNAL__(__summary__); \
::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 \
} \
} \ } \
} while (0) } while (0)
......
...@@ -68,7 +68,7 @@ std::string error_name(Code code) { ...@@ -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())); std::string result(error_name(code()));
result += ": "; result += ": ";
result += error_message(); result += error_message();
......
...@@ -52,7 +52,7 @@ class ErrorSummary { ...@@ -52,7 +52,7 @@ class ErrorSummary {
const std::string& error_message() const { return msg_; } const std::string& error_message() const { return msg_; }
std::string ToString() const; std::string to_string() const;
private: private:
Code code_; Code code_;
......
...@@ -17,6 +17,21 @@ limitations under the License. */ ...@@ -17,6 +17,21 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace pybind { 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) { void BindException(pybind11::module* m) {
static pybind11::exception<platform::EOFException> eof(*m, "EOFException"); static pybind11::exception<platform::EOFException> eof(*m, "EOFException");
static pybind11::exception<platform::EnforceNotMet> exc(*m, "EnforceNotMet"); static pybind11::exception<platform::EnforceNotMet> exc(*m, "EnforceNotMet");
...@@ -26,7 +41,37 @@ void BindException(pybind11::module* m) { ...@@ -26,7 +41,37 @@ void BindException(pybind11::module* m) {
} catch (const platform::EOFException& e) { } catch (const platform::EOFException& e) {
eof(e.what()); eof(e.what());
} catch (const platform::EnforceNotMet& e) { } catch (const platform::EnforceNotMet& e) {
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()); exc(e.what());
break;
}
} }
}); });
......
...@@ -112,11 +112,11 @@ if __name__ == '__main__': ...@@ -112,11 +112,11 @@ if __name__ == '__main__':
print(type(e)) print(type(e))
# Note. Enforce in cuda kernel may not catch in paddle, and # Note. Enforce in cuda kernel may not catch in paddle, and
# Exception type will be RuntimeError # Exception type will be RuntimeError
assert type(e) == core.EnforceNotMet or type(e) == RuntimeError assert type(e) == OSError or type(e) == RuntimeError
try: try:
check(use_cuda=False) check(use_cuda=False)
assert False assert False
except Exception as e: except Exception as e:
print(e) print(e)
print(type(e)) print(type(e))
assert type(e) == core.EnforceNotMet assert type(e) == RuntimeError
...@@ -20,7 +20,6 @@ import unittest ...@@ -20,7 +20,6 @@ import unittest
import numpy as np import numpy as np
import paddle import paddle
import paddle.fluid as fluid 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 import error
from paddle.fluid.dygraph.dygraph_to_static.origin_info import unwrap from paddle.fluid.dygraph.dygraph_to_static.origin_info import unwrap
...@@ -76,9 +75,9 @@ class TestErrorInCompileTime(unittest.TestCase): ...@@ -76,9 +75,9 @@ class TestErrorInCompileTime(unittest.TestCase):
def set_message(self): def set_message(self):
self.expected_message = \ 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()', '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")', 'fluid.layers.fill_constant(shape=[1, 2], value=9, dtype="int")',
] ]
...@@ -130,13 +129,13 @@ class TestErrorInCompileTime2(TestErrorInCompileTime): ...@@ -130,13 +129,13 @@ class TestErrorInCompileTime2(TestErrorInCompileTime):
self.func = func_error_in_compile_time_2 self.func = func_error_in_compile_time_2
def set_exception_type(self): def set_exception_type(self):
self.exception_type = EnforceNotMet self.exception_type = ValueError
def set_message(self): def set_message(self):
self.expected_message = \ 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])' 'x = fluid.layers.reshape(x, shape=[1, 2])'
] ]
...@@ -146,12 +145,12 @@ class TestErrorInRuntime(TestErrorInCompileTime): ...@@ -146,12 +145,12 @@ class TestErrorInRuntime(TestErrorInCompileTime):
self.func = func_error_in_runtime self.func = func_error_in_runtime
def set_exception_type(self): def set_exception_type(self):
self.exception_type = EnforceNotMet self.exception_type = ValueError
def set_message(self): def set_message(self):
self.expected_message = \ 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])' 'x = fluid.layers.reshape(x, shape=[1, two])'
] ]
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from __future__ import print_function from __future__ import print_function
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.layers as layers import paddle.fluid.layers as layers
import unittest import unittest
...@@ -42,7 +43,7 @@ class TestAssertOp(unittest.TestCase): ...@@ -42,7 +43,7 @@ class TestAssertOp(unittest.TestCase):
shape=[1], dtype='bool', value=False) shape=[1], dtype='bool', value=False)
layers.Assert(condition) layers.Assert(condition)
with self.assertRaises(fluid.core.EnforceNotMet): with self.assertRaises(ValueError):
self.run_network(net_func) self.run_network(net_func)
def test_assert_cond_numel_error(self): def test_assert_cond_numel_error(self):
...@@ -51,7 +52,7 @@ class TestAssertOp(unittest.TestCase): ...@@ -51,7 +52,7 @@ class TestAssertOp(unittest.TestCase):
shape=[1, 2], dtype='bool', value=True) shape=[1, 2], dtype='bool', value=True)
layers.Assert(condition, []) layers.Assert(condition, [])
with self.assertRaises(fluid.core.EnforceNotMet): with self.assertRaises(ValueError):
self.run_network(net_func) self.run_network(net_func)
def test_assert_print_data(self): def test_assert_print_data(self):
...@@ -62,7 +63,7 @@ class TestAssertOp(unittest.TestCase): ...@@ -62,7 +63,7 @@ class TestAssertOp(unittest.TestCase):
layers.Assert(condition, [zero, one]) layers.Assert(condition, [zero, one])
print("test_assert_print_data") print("test_assert_print_data")
with self.assertRaises(fluid.core.EnforceNotMet): with self.assertRaises(ValueError):
self.run_network(net_func) self.run_network(net_func)
def test_assert_summary(self): def test_assert_summary(self):
...@@ -72,7 +73,7 @@ class TestAssertOp(unittest.TestCase): ...@@ -72,7 +73,7 @@ class TestAssertOp(unittest.TestCase):
layers.Assert(condition, (x, ), 5) layers.Assert(condition, (x, ), 5)
print("test_assert_summary") print("test_assert_summary")
with self.assertRaises(fluid.core.EnforceNotMet): with self.assertRaises(ValueError):
self.run_network(net_func) self.run_network(net_func)
def test_assert_summary_greater_than_size(self): def test_assert_summary_greater_than_size(self):
...@@ -82,9 +83,10 @@ class TestAssertOp(unittest.TestCase): ...@@ -82,9 +83,10 @@ class TestAssertOp(unittest.TestCase):
layers.Assert(condition, [x], 10, name="test") layers.Assert(condition, [x], 10, name="test")
print("test_assert_summary_greater_than_size") print("test_assert_summary_greater_than_size")
with self.assertRaises(fluid.core.EnforceNotMet): with self.assertRaises(ValueError):
self.run_network(net_func) self.run_network(net_func)
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
from __future__ import print_function from __future__ import print_function
import unittest import unittest
import paddle
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.fluid as fluid import paddle.fluid as fluid
...@@ -34,7 +35,7 @@ class TestCCommInitAllOp(unittest.TestCase): ...@@ -34,7 +35,7 @@ class TestCCommInitAllOp(unittest.TestCase):
program = fluid.Program() program = fluid.Program()
block = program.global_block() block = program.global_block()
block.append_op(type='c_comm_init_all', attrs={'ring_id': 0}) 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) self.exe.run(program)
def test_specifying_devices(self): def test_specifying_devices(self):
...@@ -47,4 +48,5 @@ class TestCCommInitAllOp(unittest.TestCase): ...@@ -47,4 +48,5 @@ class TestCCommInitAllOp(unittest.TestCase):
if __name__ == "__main__": if __name__ == "__main__":
paddle.enable_static()
unittest.main() unittest.main()
...@@ -118,7 +118,10 @@ class TestCholeskySingularAPI(unittest.TestCase): ...@@ -118,7 +118,10 @@ class TestCholeskySingularAPI(unittest.TestCase):
fetches = exe.run(fluid.default_main_program(), fetches = exe.run(fluid.default_main_program(),
feed={"input": input_np}, feed={"input": input_np},
fetch_list=[result]) 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") print("The mat is singular")
pass pass
...@@ -135,10 +138,14 @@ class TestCholeskySingularAPI(unittest.TestCase): ...@@ -135,10 +138,14 @@ class TestCholeskySingularAPI(unittest.TestCase):
input = fluid.dygraph.to_variable(input_np) input = fluid.dygraph.to_variable(input_np)
try: try:
result = paddle.cholesky(input) 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") print("The mat is singular")
pass pass
if __name__ == "__main__": if __name__ == "__main__":
paddle.enable_static()
unittest.main() unittest.main()
...@@ -14,9 +14,13 @@ ...@@ -14,9 +14,13 @@
from __future__ import print_function from __future__ import print_function
import numpy
import unittest
import paddle
import paddle.fluid as fluid
import paddle.compat as cpt import paddle.compat as cpt
import paddle.fluid.core as core import paddle.fluid.core as core
import unittest
class TestException(unittest.TestCase): class TestException(unittest.TestCase):
...@@ -24,7 +28,7 @@ class TestException(unittest.TestCase): ...@@ -24,7 +28,7 @@ class TestException(unittest.TestCase):
exception = None exception = None
try: try:
core.__unittest_throw_exception__() core.__unittest_throw_exception__()
except core.EnforceNotMet as ex: except RuntimeError as ex:
self.assertIn("This is a test of exception", self.assertIn("This is a test of exception",
cpt.get_exception_message(ex)) cpt.get_exception_message(ex))
exception = ex exception = ex
...@@ -32,5 +36,43 @@ class TestException(unittest.TestCase): ...@@ -32,5 +36,43 @@ class TestException(unittest.TestCase):
self.assertIsNotNone(exception) 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__": if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -87,7 +87,7 @@ class TestFillAnyLikeOpOverflow(TestFillAnyLikeOp): ...@@ -87,7 +87,7 @@ class TestFillAnyLikeOpOverflow(TestFillAnyLikeOp):
exception = None exception = None
try: try:
self.check_output(check_dygraph=False) self.check_output(check_dygraph=False)
except core.EnforceNotMet as ex: except ValueError as ex:
exception = ex exception = ex
self.assertIsNotNone(exception) self.assertIsNotNone(exception)
...@@ -98,4 +98,5 @@ class TestFillAnyLikeOpFloat16(TestFillAnyLikeOp): ...@@ -98,4 +98,5 @@ class TestFillAnyLikeOpFloat16(TestFillAnyLikeOp):
if __name__ == "__main__": if __name__ == "__main__":
paddle.enable_static()
unittest.main() unittest.main()
...@@ -77,7 +77,7 @@ class TestHistogramOpError(unittest.TestCase): ...@@ -77,7 +77,7 @@ class TestHistogramOpError(unittest.TestCase):
shape=[3, 4], dtype='float32', value=3.0) shape=[3, 4], dtype='float32', value=3.0)
paddle.histogram(input=input_value, bins=-1, min=1, max=5) 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) self.run_network(net_func)
def test_min_max_error(self): def test_min_max_error(self):
...@@ -88,7 +88,7 @@ class TestHistogramOpError(unittest.TestCase): ...@@ -88,7 +88,7 @@ class TestHistogramOpError(unittest.TestCase):
shape=[3, 4], dtype='float32', value=3.0) shape=[3, 4], dtype='float32', value=3.0)
paddle.histogram(input=input_value, bins=1, min=5, max=1) 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) self.run_network(net_func)
def test_min_max_range_error(self): def test_min_max_range_error(self):
...@@ -99,7 +99,7 @@ class TestHistogramOpError(unittest.TestCase): ...@@ -99,7 +99,7 @@ class TestHistogramOpError(unittest.TestCase):
shape=[3, 4], dtype='float32', value=3.0) shape=[3, 4], dtype='float32', value=3.0)
paddle.histogram(input=input_value, bins=1, min=-np.inf, max=5) 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) self.run_network(net_func)
def test_type_errors(self): def test_type_errors(self):
...@@ -138,4 +138,5 @@ class TestHistogramOp(OpTest): ...@@ -138,4 +138,5 @@ class TestHistogramOp(OpTest):
if __name__ == "__main__": if __name__ == "__main__":
paddle.enable_static()
unittest.main() unittest.main()
...@@ -333,7 +333,7 @@ class TestImperative(unittest.TestCase): ...@@ -333,7 +333,7 @@ class TestImperative(unittest.TestCase):
try: try:
new_variable.numpy() new_variable.numpy()
except Exception as e: except Exception as e:
assert type(e) == core.EnforceNotMet assert type(e) == ValueError
try: try:
new_variable.backward() new_variable.backward()
...@@ -689,4 +689,5 @@ class TestDygraphGuardWithError(unittest.TestCase): ...@@ -689,4 +689,5 @@ class TestDygraphGuardWithError(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -96,7 +96,7 @@ class TestDygraphDataLoaderProcess(unittest.TestCase): ...@@ -96,7 +96,7 @@ class TestDygraphDataLoaderProcess(unittest.TestCase):
exception = None exception = None
try: try:
_reader_process_loop(loader._batch_reader, loader._data_queue) _reader_process_loop(loader._batch_reader, loader._data_queue)
except core.EnforceNotMet as ex: except ValueError as ex:
exception = ex exception = ex
self.assertIsNotNone(exception) self.assertIsNotNone(exception)
......
...@@ -356,7 +356,7 @@ class TestRaiseNoDoubleGradOp(TestCase): ...@@ -356,7 +356,7 @@ class TestRaiseNoDoubleGradOp(TestCase):
loss.backward() loss.backward()
def test_raise(self): 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__': if __name__ == '__main__':
......
...@@ -50,8 +50,8 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase): ...@@ -50,8 +50,8 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase):
set_child_signal_handler(id(self), test_process.pid) set_child_signal_handler(id(self), test_process.pid)
time.sleep(5) time.sleep(5)
except core.EnforceNotMet as ex: except SystemError as ex:
self.assertIn("FatalError", cpt.get_exception_message(ex)) self.assertIn("Fatal", cpt.get_exception_message(ex))
exception = ex exception = ex
self.assertIsNotNone(exception) self.assertIsNotNone(exception)
...@@ -68,7 +68,7 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase): ...@@ -68,7 +68,7 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase):
set_child_signal_handler(id(self), test_process.pid) set_child_signal_handler(id(self), test_process.pid)
time.sleep(3) time.sleep(3)
except core.EnforceNotMet as ex: except SystemError as ex:
self.assertIn("Segmentation fault", cpt.get_exception_message(ex)) self.assertIn("Segmentation fault", cpt.get_exception_message(ex))
exception = ex exception = ex
...@@ -86,7 +86,7 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase): ...@@ -86,7 +86,7 @@ class TestDygraphDataLoaderSingalHandler(unittest.TestCase):
set_child_signal_handler(id(self), test_process.pid) set_child_signal_handler(id(self), test_process.pid)
time.sleep(3) time.sleep(3)
except core.EnforceNotMet as ex: except SystemError as ex:
self.assertIn("Bus error", cpt.get_exception_message(ex)) self.assertIn("Bus error", cpt.get_exception_message(ex))
exception = ex exception = ex
......
...@@ -153,7 +153,10 @@ class TestInverseSingularAPI(unittest.TestCase): ...@@ -153,7 +153,10 @@ class TestInverseSingularAPI(unittest.TestCase):
fetches = exe.run(fluid.default_main_program(), fetches = exe.run(fluid.default_main_program(),
feed={"input": input_np}, feed={"input": input_np},
fetch_list=[result]) 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") print("The mat is singular")
pass pass
...@@ -168,10 +171,14 @@ class TestInverseSingularAPI(unittest.TestCase): ...@@ -168,10 +171,14 @@ class TestInverseSingularAPI(unittest.TestCase):
input = fluid.dygraph.to_variable(input_np) input = fluid.dygraph.to_variable(input_np)
try: try:
result = paddle.inverse(input) 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") print("The mat is singular")
pass pass
if __name__ == "__main__": if __name__ == "__main__":
paddle.enable_static()
unittest.main() unittest.main()
...@@ -186,29 +186,28 @@ class TestMultinomialError(unittest.TestCase): ...@@ -186,29 +186,28 @@ class TestMultinomialError(unittest.TestCase):
x = paddle.rand([4]) x = paddle.rand([4])
paddle.multinomial(x, num_samples=-2) 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_replacement_False(self):
def test_samples_larger_than_categories(): def test_samples_larger_than_categories():
x = paddle.rand([4]) x = paddle.rand([4])
paddle.multinomial(x, num_samples=5, replacement=False) paddle.multinomial(x, num_samples=5, replacement=False)
self.assertRaises(core.EnforceNotMet, self.assertRaises(ValueError, test_samples_larger_than_categories)
test_samples_larger_than_categories)
def test_input_probs_dim(self): def test_input_probs_dim(self):
def test_dim_larger_than_2(): def test_dim_larger_than_2():
x = paddle.rand([2, 3, 3]) x = paddle.rand([2, 3, 3])
paddle.multinomial(x) 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(): def test_dim_less_than_1():
x_np = np.random.random([]) x_np = np.random.random([])
x = paddle.to_tensor(x_np) x = paddle.to_tensor(x_np)
paddle.multinomial(x) paddle.multinomial(x)
self.assertRaises(core.EnforceNotMet, test_dim_less_than_1) self.assertRaises(ValueError, test_dim_less_than_1)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -194,7 +194,7 @@ class TestMultiplyError(unittest.TestCase): ...@@ -194,7 +194,7 @@ class TestMultiplyError(unittest.TestCase):
with program_guard(Program(), Program()): with program_guard(Program(), Program()):
x = paddle.static.data(name='x', shape=[20, 50], dtype=np.float64) x = paddle.static.data(name='x', shape=[20, 50], dtype=np.float64)
y = paddle.static.data(name='y', shape=[20], 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) np.random.seed(7)
# test dynamic computation graph: dtype can not be int8 # test dynamic computation graph: dtype can not be int8
...@@ -203,21 +203,21 @@ class TestMultiplyError(unittest.TestCase): ...@@ -203,21 +203,21 @@ class TestMultiplyError(unittest.TestCase):
y_data = np.random.randn(200).astype(np.int8) y_data = np.random.randn(200).astype(np.int8)
x = paddle.to_tensor(x_data) x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_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 # test dynamic computation graph: inputs must be broadcastable
x_data = np.random.rand(200, 5) x_data = np.random.rand(200, 5)
y_data = np.random.rand(200) y_data = np.random.rand(200)
x = paddle.to_tensor(x_data) x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_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) # test dynamic computation graph: inputs must be broadcastable(python)
x_data = np.random.rand(200, 5) x_data = np.random.rand(200, 5)
y_data = np.random.rand(200) y_data = np.random.rand(200)
x = paddle.to_tensor(x_data) x = paddle.to_tensor(x_data)
y = paddle.to_tensor(y_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 # test dynamic computation graph: dtype must be same
x_data = np.random.randn(200).astype(np.int64) x_data = np.random.randn(200).astype(np.int64)
......
...@@ -98,7 +98,7 @@ class TestMultiprocessReaderException(unittest.TestCase): ...@@ -98,7 +98,7 @@ class TestMultiprocessReaderException(unittest.TestCase):
exe.run(feed=data, fetch_list=[image_p_1]) exe.run(feed=data, fetch_list=[image_p_1])
num += 1 num += 1
self.assertEquals(num, batch_num) self.assertEquals(num, batch_num)
except fluid.core.EnforceNotMet as ex: except SystemError as ex:
self.assertEquals(num, 0) self.assertEquals(num, 0)
raise ReaderException() raise ReaderException()
else: else:
...@@ -113,7 +113,7 @@ class TestMultiprocessReaderException(unittest.TestCase): ...@@ -113,7 +113,7 @@ class TestMultiprocessReaderException(unittest.TestCase):
reader.reset() reader.reset()
self.assertFalse(self.raise_exception) self.assertFalse(self.raise_exception)
self.assertEquals(num, batch_num) self.assertEquals(num, batch_num)
except fluid.core.EnforceNotMet as ex: except SystemError as ex:
self.assertTrue(self.raise_exception) self.assertTrue(self.raise_exception)
self.assertEquals(num, 0) self.assertEquals(num, 0)
raise ReaderException() raise ReaderException()
...@@ -152,4 +152,5 @@ class TestCase3(TestMultiprocessReaderException): ...@@ -152,4 +152,5 @@ class TestCase3(TestMultiprocessReaderException):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -18,6 +18,7 @@ import unittest ...@@ -18,6 +18,7 @@ import unittest
import numpy as np import numpy as np
import math import math
from op_test import OpTest from op_test import OpTest
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.nn.functional as functional import paddle.nn.functional as functional
...@@ -152,7 +153,7 @@ class TestOneHotOp_exception(unittest.TestCase): ...@@ -152,7 +153,7 @@ class TestOneHotOp_exception(unittest.TestCase):
fetch_list=[one_hot_out], fetch_list=[one_hot_out],
return_numpy=False) return_numpy=False)
self.assertRaises(core.EnforceNotMet, run) self.assertRaises(ValueError, run)
class TestOneHotOpApi(unittest.TestCase): class TestOneHotOpApi(unittest.TestCase):
...@@ -204,4 +205,5 @@ class BadInputTestOnehotV2(unittest.TestCase): ...@@ -204,4 +205,5 @@ class BadInputTestOnehotV2(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -18,6 +18,7 @@ import unittest ...@@ -18,6 +18,7 @@ import unittest
import numpy as np import numpy as np
import math import math
from op_test import OpTest from op_test import OpTest
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.fluid.framework as framework import paddle.fluid.framework as framework
...@@ -172,7 +173,7 @@ class TestOneHotOp_exception(unittest.TestCase): ...@@ -172,7 +173,7 @@ class TestOneHotOp_exception(unittest.TestCase):
fetch_list=[one_hot_out], fetch_list=[one_hot_out],
return_numpy=False) return_numpy=False)
self.assertRaises(core.EnforceNotMet, run) self.assertRaises(ValueError, run)
class TestOneHotOpError(unittest.TestCase): class TestOneHotOpError(unittest.TestCase):
...@@ -200,4 +201,5 @@ class TestOneHotOpError(unittest.TestCase): ...@@ -200,4 +201,5 @@ class TestOneHotOpError(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -18,6 +18,7 @@ import unittest ...@@ -18,6 +18,7 @@ import unittest
import numpy as np import numpy as np
import math import math
from op_test import OpTest from op_test import OpTest
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.fluid.framework as framework import paddle.fluid.framework as framework
...@@ -169,7 +170,7 @@ class TestOneHotOp_exception(unittest.TestCase): ...@@ -169,7 +170,7 @@ class TestOneHotOp_exception(unittest.TestCase):
fetch_list=[one_hot_out], fetch_list=[one_hot_out],
return_numpy=False) return_numpy=False)
self.assertRaises(core.EnforceNotMet, run) self.assertRaises(ValueError, run)
class TestOneHotOpApi(unittest.TestCase): class TestOneHotOpApi(unittest.TestCase):
...@@ -220,4 +221,5 @@ class BadInputTestOnehotV2(unittest.TestCase): ...@@ -220,4 +221,5 @@ class BadInputTestOnehotV2(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -16,6 +16,7 @@ from __future__ import print_function ...@@ -16,6 +16,7 @@ from __future__ import print_function
from functools import partial from functools import partial
import numpy import numpy
import unittest import unittest
import paddle
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.fluid as fluid import paddle.fluid as fluid
from simple_nets import init_data, simple_fc_net from simple_nets import init_data, simple_fc_net
...@@ -76,13 +77,14 @@ class TestFeedPersistableVar(unittest.TestCase): ...@@ -76,13 +77,14 @@ class TestFeedPersistableVar(unittest.TestCase):
self.feed_dict['learning_rate'] = numpy.array( self.feed_dict['learning_rate'] = numpy.array(
[1.0, 1.0]).astype("float32") [1.0, 1.0]).astype("float32")
run = partial(self.check_feed_persistable_var, self.feed_dict) 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['image'] = self.img[0, :]
self.feed_dict['label'] = self.label[0, :] self.feed_dict['label'] = self.label[0, :]
run = partial(self.check_feed_persistable_var, self.feed_dict) run = partial(self.check_feed_persistable_var, self.feed_dict)
self.assertRaises(core.EnforceNotMet, run) self.assertRaises(RuntimeError, run)
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -218,8 +218,8 @@ class TestPowerError(unittest.TestCase): ...@@ -218,8 +218,8 @@ class TestPowerError(unittest.TestCase):
np.random.randint(5, 10)) np.random.randint(5, 10))
x = (np.random.rand(*dims) * 10).astype(np.float64) x = (np.random.rand(*dims) * 10).astype(np.float64)
y = (np.random.rand(dims[-1] + 1) * 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(ValueError, _run_power, DYNAMIC, x, y)
self.assertRaises(fluid.core.EnforceNotMet, _run_power, STATIC, x, y) self.assertRaises(ValueError, _run_power, STATIC, x, y)
# test dynamic computation graph: inputs must be broadcastable # test dynamic computation graph: inputs must be broadcastable
dims = (np.random.randint(1, 10), np.random.randint(5, 10), dims = (np.random.randint(1, 10), np.random.randint(5, 10),
......
...@@ -136,8 +136,7 @@ class TestRetainGraph(unittest.TestCase): ...@@ -136,8 +136,7 @@ class TestRetainGraph(unittest.TestCase):
def test_retain(self): def test_retain(self):
self.run_retain(need_retain=True) self.run_retain(need_retain=True)
self.assertRaises( self.assertRaises(RuntimeError, self.run_retain, need_retain=False)
fluid.core.EnforceNotMet, self.run_retain, need_retain=False)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -17,6 +17,7 @@ from __future__ import print_function ...@@ -17,6 +17,7 @@ from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
from op_test import OpTest from op_test import OpTest
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid import core from paddle.fluid import core
...@@ -107,7 +108,7 @@ class TestCase4(unittest.TestCase): ...@@ -107,7 +108,7 @@ class TestCase4(unittest.TestCase):
x = np.random.random(size=(10, 1, 1, 1, 1, 1, 1)).astype('int64') x = np.random.random(size=(10, 1, 1, 1, 1, 1, 1)).astype('int64')
exe.run(train_program, feed={"label": x}) exe.run(train_program, feed={"label": x})
self.assertRaises(core.EnforceNotMet, _run_program) self.assertRaises(IndexError, _run_program)
class TestReverseLoDTensorArray(unittest.TestCase): class TestReverseLoDTensorArray(unittest.TestCase):
...@@ -182,4 +183,5 @@ class TestReverseLoDTensorArray(unittest.TestCase): ...@@ -182,4 +183,5 @@ class TestReverseLoDTensorArray(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -17,6 +17,7 @@ from __future__ import print_function ...@@ -17,6 +17,7 @@ from __future__ import print_function
import unittest import unittest
import numpy as np import numpy as np
from op_test import OpTest from op_test import OpTest
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
...@@ -36,12 +37,12 @@ class TestRunTimeException(unittest.TestCase): ...@@ -36,12 +37,12 @@ class TestRunTimeException(unittest.TestCase):
x = np.random.random(size=(10)).astype('int64') x = np.random.random(size=(10)).astype('int64')
exe.run(train_program, feed={"label": x}) exe.run(train_program, feed={"label": x})
self.assertRaises(core.EnforceNotMet, _run_program) self.assertRaises(ValueError, _run_program)
class TestCompileTimeException(unittest.TestCase): class TestCompileTimeException(unittest.TestCase):
def test_compile_time_exception(self): def test_compile_time_exception(self):
self.assertRaises(core.EnforceNotMet, self.build_model) self.assertRaises(ValueError, self.build_model)
def build_model(self): def build_model(self):
train_program = fluid.Program() train_program = fluid.Program()
...@@ -53,4 +54,5 @@ class TestCompileTimeException(unittest.TestCase): ...@@ -53,4 +54,5 @@ class TestCompileTimeException(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -356,7 +356,7 @@ class TestTensor(unittest.TestCase): ...@@ -356,7 +356,7 @@ class TestTensor(unittest.TestCase):
try: try:
error_array = ["1", "2"] error_array = ["1", "2"]
tensor.set(error_array, place) tensor.set(error_array, place)
except core.EnforceNotMet as ex: except ValueError as ex:
exception = ex exception = ex
self.assertIsNotNone(exception) self.assertIsNotNone(exception)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册