Skip to content

Commit

Permalink
Fix eager try catch (#41438) (#41477)
Browse files Browse the repository at this point in the history
[Cherry-Pick]Fix eager try catch (#41438)
  • Loading branch information
0x45f committed Apr 7, 2022
1 parent e34042a commit b73a70d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion paddle/fluid/pybind/eager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ void AutoInitTensorByTensor(TensorObject* py_tensor_ptr,
* ** name: std::string)
* **/
int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
EAGER_TRY
// set a flag to record use kwargs or not
bool flag_kwargs = false;
if (kwargs) flag_kwargs = true;
Expand Down Expand Up @@ -703,7 +704,8 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
"make sure u call the existed constructor."));
}

return 1;
return -1;
EAGER_CATCH_AND_THROW_RETURN_NEG
}

static void TensorDealloc(TensorObject* self) {
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/pybind/eager_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int tensor_properties_set_name(TensorObject* self, PyObject* value,
EAGER_TRY
self->tensor.set_name(CastPyArg2AttrString(value, 0));
return 0;
EAGER_CATCH_AND_THROW_RETURN_ZERO
EAGER_CATCH_AND_THROW_RETURN_NEG
}

PyObject* tensor_properties_get_stop_gradient(TensorObject* self,
Expand Down Expand Up @@ -110,7 +110,7 @@ int tensor_properties_set_grad(TensorObject* self, PyObject* value,
"the grad inside autograd_meta"));
grad->copy_(src, self->tensor.inner_place(), true);
return 0;
EAGER_CATCH_AND_THROW_RETURN_ZERO
EAGER_CATCH_AND_THROW_RETURN_NEG
}

int tensor_properties_set_stop_gradient(TensorObject* self, PyObject* value,
Expand All @@ -122,7 +122,7 @@ int tensor_properties_set_stop_gradient(TensorObject* self, PyObject* value,
meta->SetGradNode(std::make_shared<egr::GradNodeAccumulation>(meta));
}
return 0;
EAGER_CATCH_AND_THROW_RETURN_ZERO
EAGER_CATCH_AND_THROW_RETURN_NEG
}

PyObject* tensor_properties_get_persistable(TensorObject* self, void* closure) {
Expand All @@ -138,7 +138,7 @@ int tensor_properties_set_persistable(TensorObject* self, PyObject* value,
auto meta = egr::EagerUtils::autograd_meta(&self->tensor);
meta->SetPersistable(CastPyArg2AttrBoolean(value, 0));
return 0;
EAGER_CATCH_AND_THROW_RETURN_ZERO
EAGER_CATCH_AND_THROW_RETURN_NEG
}

PyObject* tensor_properties_get_shape(TensorObject* self, void* closure) {
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/pybind/eager_py_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ int tensor_properties_set_container(PyLayerObject* self, PyObject* value,
Py_XDECREF(self->container);
self->container = value;
return 0;
EAGER_CATCH_AND_THROW_RETURN_ZERO
EAGER_CATCH_AND_THROW_RETURN_NEG
}

PyObject* tensor_properties_get_non_differentiable(PyLayerObject* self,
Expand All @@ -417,7 +417,7 @@ int tensor_properties_set_non_differentiable(PyLayerObject* self,
Py_XDECREF(self->non_differentiable);
self->non_differentiable = value;
return 0;
EAGER_CATCH_AND_THROW_RETURN_ZERO
EAGER_CATCH_AND_THROW_RETURN_NEG
}

PyObject* tensor_properties_get_dirty_tensors(PyLayerObject* self,
Expand All @@ -439,15 +439,15 @@ int tensor_properties_set_dirty_tensors(PyLayerObject* self, PyObject* value,
Py_XDECREF(self->dirty_tensors);
self->dirty_tensors = value;
return 0;
EAGER_CATCH_AND_THROW_RETURN_ZERO
EAGER_CATCH_AND_THROW_RETURN_NEG
}

int tensor_properties_set_materialize_grads(PyLayerObject* self,
PyObject* value, void* closure) {
EAGER_TRY
self->materialize_grads = CastPyArg2AttrBoolean(value, 0);
return 0;
EAGER_CATCH_AND_THROW_RETURN_ZERO
EAGER_CATCH_AND_THROW_RETURN_NEG
}

PyMethodDef pylayer_methods[] = {
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/pybind/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ limitations under the License. */
return nullptr; \
}

#define EAGER_CATCH_AND_THROW_RETURN_ZERO \
#define EAGER_CATCH_AND_THROW_RETURN_NEG \
} \
catch (...) { \
ThrowExceptionToPython(std::current_exception()); \
return 0; \
return -1; \
}

namespace paddle {
Expand Down

0 comments on commit b73a70d

Please sign in to comment.