diff --git a/paddle/fluid/eager/pylayer/py_layer_node.h b/paddle/fluid/eager/pylayer/py_layer_node.h index 57ee7016d8588e3820de43b68dd4ef25d613d213..f1166c362e147b3d53ef7c5ef536d4d30da5e4d8 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 25eb696085c09f93e294f1ea5be99a16712ecd27..970530353c7927854f075c84c2891189ed6b33d6 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