Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决 grad_fn next_functions api 接口导致内存异常的问题 - #55627

Merged
merged 17 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions paddle/fluid/pybind/eager_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,16 @@ PyObject* tensor_properties_get_grad_fn(TensorObject* self, void* closure) {

if (meta) {
// Get the GradNode from meta
auto grad_node = meta->GradNode(); // Convert GradNode to a Python object
// The conversion will depend on the structure of GradNode.

if (!grad_node) {
auto grad_node_ptr = meta->GetMutableGradNode();
if (!grad_node_ptr) {
Py_INCREF(Py_None);
return Py_None;
}

PyObject* py_grad_node = ToPyObject(grad_node);
PyObject* py_grad_node = ToPyObject(grad_node_ptr);

return py_grad_node;

} else {
// If meta does not exist, return an appropriate Python object (e.g., None
// or a special value).
Expand Down
5 changes: 2 additions & 3 deletions paddle/fluid/pybind/eager_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,9 @@ paddle::optional<paddle::Tensor> GetOptionalTensorFromArgs(
}
}

PyObject* ToPyObject(egr::GradNodeBase* grad_node) {
PyObject* ToPyObject(std::shared_ptr<egr::GradNodeBase> grad_node) {
py::object py_obj = py::cast(grad_node, py::return_value_policy::reference);
py::handle py_handle = py::handle(py_obj);
PyObject* py_grad_node = py_handle.ptr();
PyObject* py_grad_node = py_obj.release().ptr();
Py_INCREF(py_grad_node);
return py_grad_node;
}
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/pybind/eager_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ PyObject* ToPyObject(
const std::unordered_map<std::string, std::vector<std::string>>& value);
PyObject* ToPyObject(const paddle::framework::Vocab& value);

PyObject* ToPyObject(egr::GradNodeBase* grad_node);
PyObject* ToPyObject(std::shared_ptr<egr::GradNodeBase> grad_node);

class PyTensorHook : public egr::TensorHook {
public:
Expand Down
24 changes: 18 additions & 6 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -778,12 +778,24 @@ PYBIND11_MODULE(libpaddle, m) {
}
});

py::class_<egr::GradNodeBase>(m, "GradNodeBase")
.def("name", &egr::GradNodeBase::name)
.def_property_readonly("next_functions",
&egr::GradNodeBase::NextFunctions)
.def("input_meta", &egr::GradNodeBase::InputMeta)
.def("output_meta", &egr::GradNodeBase::OutputMeta);
py::class_<egr::GradNodeBase, std::shared_ptr<egr::GradNodeBase>>(
m, "GradNodeBase")
.def("name",
[](const std::shared_ptr<egr::GradNodeBase> &self) {
return self->name();
})
.def_property_readonly(
"next_functions",
[](const std::shared_ptr<egr::GradNodeBase> &self) {
return self->NextFunctions();
})
.def("input_meta",
[](const std::shared_ptr<egr::GradNodeBase> &self) {
return self->InputMeta();
})
.def("output_meta", [](const std::shared_ptr<egr::GradNodeBase> &self) {
return self->OutputMeta();
});

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
m.def("cudnn_version", &platform::DnnVersion);
Expand Down
5 changes: 5 additions & 0 deletions test/legacy_test/test_grad_fn_and_next_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ def check_func(self, grad_fn, grad_fn_json) -> None:
grad_fn_json (dict): grad_node_json of node
"""
self.assertEqual(grad_fn.name(), grad_fn_json["func_name"])
# Recursively test other nodes
if hasattr(grad_fn, 'next_functions') and grad_fn.next_functions[0]:
next_funcs_json = grad_fn_json["next_funcs"]
for u in grad_fn.next_functions:
self.check_func(u, next_funcs_json[u.name()])


if __name__ == "__main__":
Expand Down