diff --git a/paddle/fluid/eager/pylayer/py_layer_node.cc b/paddle/fluid/eager/pylayer/py_layer_node.cc index 5008e958c5f11a90df20a37134f5e658c48947ae..42036a28cfa15eabc1d4cef28e70ba1f6fb9a4f8 100644 --- a/paddle/fluid/eager/pylayer/py_layer_node.cc +++ b/paddle/fluid/eager/pylayer/py_layer_node.cc @@ -154,6 +154,12 @@ operator()( } } + Py_XDECREF(backward_fn); + Py_XDECREF(backward_args); + if (!PyTuple_Check(outputs)) { + Py_XDECREF(outputs_tuple); + } + return grad_out; } } // namespace egr diff --git a/paddle/fluid/pybind/eager_py_layer.cc b/paddle/fluid/pybind/eager_py_layer.cc index cade856b3607a536b8d9cf468283e61ee7d78abf..605056e7af2b5e7ae759b6b8ceba77c5b4aaf2dc 100644 --- a/paddle/fluid/pybind/eager_py_layer.cc +++ b/paddle/fluid/pybind/eager_py_layer.cc @@ -231,6 +231,10 @@ PyObject* pylayer_method_apply(PyObject* cls, PyObject* args, auto outputs = PyObject_Call(forward_fn, forward_args, kwargs); egr::Controller::Instance().SetHasGrad(trace_backward); if (!outputs) { + Py_XDECREF(forward_args); + Py_XDECREF(kwargs_value_list); + Py_XDECREF(backward_function); + Py_XDECREF(forward_fn); return nullptr; } @@ -367,6 +371,14 @@ PyObject* pylayer_method_apply(PyObject* cls, PyObject* args, VLOG(6) << "PyLayer construct backward node finish..."; } + if (!PyTuple_Check(outputs)) { + Py_XDECREF(outputs_tuple); + } + Py_XDECREF(forward_args); + Py_XDECREF(kwargs_value_list); + Py_XDECREF(backward_function); + Py_XDECREF(forward_fn); + return outputs; EAGER_CATCH_AND_THROW_RETURN_NULL }