From f1e3d79518da12de460d3443cf0e9d4327e5fc41 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Wed, 3 Aug 2022 11:09:09 +0800 Subject: [PATCH] fix pylayer forward memleak (#44826) --- paddle/fluid/eager/pylayer/py_layer_node.h | 1 + paddle/fluid/pybind/eager_py_layer.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/paddle/fluid/eager/pylayer/py_layer_node.h b/paddle/fluid/eager/pylayer/py_layer_node.h index 57ee7016d8..f1166c362e 100644 --- a/paddle/fluid/eager/pylayer/py_layer_node.h +++ b/paddle/fluid/eager/pylayer/py_layer_node.h @@ -31,6 +31,7 @@ class GradNodePyLayer : public GradNodeBase { size_t bwd_out_slot_num) : GradNodeBase(bwd_in_slot_num, bwd_out_slot_num) { ctx_ = ctx; + Py_INCREF(ctx_); } ~GradNodePyLayer() override { Py_XDECREF(ctx_); }; diff --git a/paddle/fluid/pybind/eager_py_layer.cc b/paddle/fluid/pybind/eager_py_layer.cc index 25eb696085..970530353c 100644 --- a/paddle/fluid/pybind/eager_py_layer.cc +++ b/paddle/fluid/pybind/eager_py_layer.cc @@ -383,6 +383,7 @@ PyObject* pylayer_method_apply(PyObject* cls, Py_XDECREF(kwargs_value_list); Py_XDECREF(backward_function); Py_XDECREF(forward_fn); + Py_XDECREF(ctx); return outputs; EAGER_CATCH_AND_THROW_RETURN_NULL -- GitLab