From be11648af30db3599f930db58b57caf146a46a87 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Sat, 9 Apr 2022 11:11:37 +0800 Subject: [PATCH] fix pylayer mem leak, test=develop (#41559) --- paddle/fluid/eager/pylayer/py_layer_node.cc | 6 ++++++ paddle/fluid/pybind/eager_py_layer.cc | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/paddle/fluid/eager/pylayer/py_layer_node.cc b/paddle/fluid/eager/pylayer/py_layer_node.cc index 5008e958c5f..42036a28cfa 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 cade856b360..605056e7af2 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 } -- GitLab