未验证 提交 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) {
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) { 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
......
此差异已折叠。
...@@ -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) {
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;
}
} }
}); });
......
...@@ -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.
先完成此消息的编辑!
想要评论请 注册