未验证 提交 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. */
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) {
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
......
......@@ -39,7 +39,6 @@ limitations under the License. */
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include <stdexcept>
......@@ -275,8 +274,10 @@ template <typename StrType>
inline std::string GetErrorSumaryString(StrType&& what, const char* file,
int line) {
std::ostringstream sout;
if (FLAGS_call_stack_level > 1) {
sout << "\n----------------------\nError Message "
"Summary:\n----------------------\n";
}
sout << string::Sprintf("%s (at %s:%d)", std::forward<StrType>(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__(...) \
#define __THROW_ERROR_INTERNAL__(__ERROR_SUMMARY) \
do { \
HANDLE_THE_ERROR \
throw ::paddle::platform::EnforceNotMet( \
::paddle::string::Sprintf(__VA_ARGS__), __FILE__, __LINE__); \
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(...) \
......@@ -356,16 +405,7 @@ struct EnforceNotMet : public std::exception {
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 \
} \
__THROW_ERROR_INTERNAL__(::paddle::platform::ErrorSummary(__VA_ARGS__)); \
} \
} while (0)
#endif
......@@ -387,9 +427,12 @@ struct EnforceNotMet : public std::exception {
#define PADDLE_ENFORCE_NOT_NULL(__VAL, ...) \
do { \
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.]", \
::paddle::platform::ErrorSummary(__VA_ARGS__).ToString()); \
__summary__.error_message()); \
__THROW_ERROR_INTERNAL__( \
::paddle::platform::ErrorSummary(__summary__.code(), __message__)); \
} \
} while (0)
......@@ -406,17 +449,20 @@ struct EnforceNotMet : public std::exception {
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; \
__THROW_ERROR_INTERNAL__( \
auto __message__ = ::paddle::string::Sprintf( \
"%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< \
__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)
......@@ -462,9 +508,7 @@ struct EnforceNotMet : public std::exception {
(([&]() -> std::add_lvalue_reference<decltype(*(__PTR))>::type { \
auto* __ptr = (__PTR); \
if (UNLIKELY(nullptr == __ptr)) { \
__THROW_ERROR_INTERNAL__( \
"%s\n [Hint: pointer " #__PTR " should not be null.]", \
paddle::platform::errors::NotFound( \
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" \
......@@ -474,8 +518,12 @@ struct EnforceNotMet : public std::exception {
typeid(std::add_lvalue_reference<decltype(*__ptr)>::type) \
.name()), \
__ROLE, __NAME, __OP_TYPE, __NAME, __ROLE, __OP_TYPE, __NAME, \
__NAME) \
.ToString()); \
__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; \
})())
......@@ -588,7 +636,7 @@ struct EOFException : public std::exception {
do { \
HANDLE_THE_ERROR \
throw ::paddle::memory::allocation::BadAlloc( \
::paddle::platform::ErrorSummary(__VA_ARGS__).ToString(), __FILE__, \
::paddle::platform::ErrorSummary(__VA_ARGS__).to_string(), __FILE__, \
__LINE__); \
END_HANDLE_THE_ERROR \
} while (0)
......@@ -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 {
......@@ -874,18 +898,9 @@ DEFINE_CUDA_STATUS_TYPE(ncclResult_t, ncclSuccess);
::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 \
} \
auto __summary__ = ::paddle::platform::errors::External( \
::paddle::platform::build_nvidia_error_msg(__cond__)); \
__THROW_ERROR_INTERNAL__(__summary__); \
} \
} while (0)
......
......@@ -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();
......
......@@ -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_;
......
......@@ -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<platform::EOFException> eof(*m, "EOFException");
static pybind11::exception<platform::EnforceNotMet> 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) {
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;
}
}
});
......
......@@ -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
......@@ -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])'
]
......
......@@ -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()
......@@ -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()
......@@ -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()
......@@ -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()
......@@ -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()
......@@ -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()
......@@ -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()
......@@ -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)
......
......@@ -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__':
......
......@@ -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
......
......@@ -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()
......@@ -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__":
......
......@@ -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)
......
......@@ -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()
......@@ -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()
......@@ -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()
......@@ -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()
......@@ -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()
......@@ -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),
......
......@@ -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__':
......
......@@ -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()
......@@ -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()
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册