From 5c7fce47c554d9a44a0b72814bb3515c9199f6a2 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Wed, 9 Nov 2022 10:02:23 +0800 Subject: [PATCH] refine python call error report (#47724) * refine python call error report --- paddle/fluid/eager/saved_tensors_hooks.cc | 12 ++++++++++++ paddle/fluid/pybind/eager_py_layer.cc | 2 ++ paddle/fluid/pybind/eager_utils.cc | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/eager/saved_tensors_hooks.cc b/paddle/fluid/eager/saved_tensors_hooks.cc index 6bd62c21611..1060e5d463d 100644 --- a/paddle/fluid/eager/saved_tensors_hooks.cc +++ b/paddle/fluid/eager/saved_tensors_hooks.cc @@ -36,6 +36,9 @@ void* PackHook::operator()(const paddle::experimental::Tensor& tensor) { auto args = PyTuple_New(1); PyTuple_SET_ITEM(args, 0, paddle::pybind::ToPyObject(tensor)); PyObject* ret = PyObject_Call(hook_, args, nullptr); + PADDLE_ENFORCE_NOT_NULL(ret, + paddle::platform::errors::External( + pybind11::detail::error_string().c_str())); Py_XDECREF(args); egr::Controller::Instance().SetHasGrad(grad_tmp); return reinterpret_cast(ret); @@ -49,6 +52,9 @@ void* PackHook::operator()(void* py_tensor) { Py_INCREF(reinterpret_cast(py_tensor)); PyTuple_SET_ITEM(args, 0, reinterpret_cast(py_tensor)); PyObject* ret = PyObject_Call(hook_, args, nullptr); + PADDLE_ENFORCE_NOT_NULL(ret, + paddle::platform::errors::External( + pybind11::detail::error_string().c_str())); Py_XDECREF(args); egr::Controller::Instance().SetHasGrad(grad_tmp); return reinterpret_cast(ret); @@ -69,6 +75,9 @@ paddle::experimental::Tensor UnPackHook::operator()(void* packed_value) { Py_INCREF(reinterpret_cast(packed_value)); PyTuple_SET_ITEM(args, 0, reinterpret_cast(packed_value)); PyObject* ret = PyObject_Call(hook_, args, nullptr); + PADDLE_ENFORCE_NOT_NULL(ret, + paddle::platform::errors::External( + pybind11::detail::error_string().c_str())); Py_XDECREF(args); egr::Controller::Instance().SetHasGrad(grad_tmp); @@ -91,6 +100,9 @@ void* UnPackHook::operator()(void* packed_value, void* other) { Py_INCREF(reinterpret_cast(packed_value)); PyTuple_SET_ITEM(args, 0, reinterpret_cast(packed_value)); PyObject* ret = PyObject_Call(hook_, args, nullptr); + PADDLE_ENFORCE_NOT_NULL(ret, + paddle::platform::errors::External( + pybind11::detail::error_string().c_str())); Py_XDECREF(args); egr::Controller::Instance().SetHasGrad(grad_tmp); diff --git a/paddle/fluid/pybind/eager_py_layer.cc b/paddle/fluid/pybind/eager_py_layer.cc index f39dc6d74f4..294da6956ab 100644 --- a/paddle/fluid/pybind/eager_py_layer.cc +++ b/paddle/fluid/pybind/eager_py_layer.cc @@ -139,6 +139,8 @@ PyObject* pylayer_method_apply(PyObject* cls, PyLayerObject* ctx = reinterpret_cast( PyObject_CallFunctionObjArgs(backward_function, nullptr)); if (!ctx) { + PADDLE_THROW(paddle::platform::errors::External( + pybind11::detail::error_string().c_str())); return nullptr; } VLOG(6) << "PyLayer construct PyLayerContext finish..."; diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 1237e4092f0..b4f65faf64a 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -1525,8 +1525,8 @@ paddle::experimental::Tensor PyTensorHook::operator()( } PADDLE_ENFORCE_NOT_NULL(res, - platform::errors::Unavailable( - "Hook function of Tensor return a nullptr.")); + paddle::platform::errors::External( + pybind11::detail::error_string().c_str())); if (res == Py_None) { return var; } -- GitLab