未验证 提交 4e1f7692 编写于 作者: W wanghuancoder 提交者: GitHub

[Eager]release gil when run backward (#44433)

* release gil when run backward
上级 547075e9
...@@ -34,6 +34,7 @@ GradNodePyLayer::operator()( ...@@ -34,6 +34,7 @@ GradNodePyLayer::operator()(
kSlotSmallVectorSize>& grads, // NOLINT kSlotSmallVectorSize>& grads, // NOLINT
bool create_graph, bool create_graph,
bool is_new_grad) { bool is_new_grad) {
pybind11::gil_scoped_acquire gil;
VLOG(3) << "Running Eager Backward Node: " << name(); VLOG(3) << "Running Eager Backward Node: " << name();
paddle::small_vector<std::vector<paddle::experimental::Tensor>, paddle::small_vector<std::vector<paddle::experimental::Tensor>,
......
...@@ -119,9 +119,12 @@ static PyObject* eager_api_run_backward(PyObject* self, ...@@ -119,9 +119,12 @@ static PyObject* eager_api_run_backward(PyObject* self,
EAGER_TRY EAGER_TRY
auto tensors = CastPyArg2VectorOfTensor(PyTuple_GET_ITEM(args, 0), 0); auto tensors = CastPyArg2VectorOfTensor(PyTuple_GET_ITEM(args, 0), 0);
auto grad_tensors = CastPyArg2VectorOfTensor(PyTuple_GET_ITEM(args, 1), 1); auto grad_tensors = CastPyArg2VectorOfTensor(PyTuple_GET_ITEM(args, 1), 1);
egr::Backward(tensors, {
grad_tensors, eager_gil_scoped_release guard;
CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, 2), 2)); egr::Backward(tensors,
grad_tensors,
CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, 2), 2));
}
RETURN_PY_NONE RETURN_PY_NONE
EAGER_CATCH_AND_THROW_RETURN_NULL EAGER_CATCH_AND_THROW_RETURN_NULL
} }
...@@ -138,15 +141,18 @@ static PyObject* eager_api_run_partial_grad(PyObject* self, ...@@ -138,15 +141,18 @@ static PyObject* eager_api_run_partial_grad(PyObject* self,
auto only_inputs = CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, 5), 5); auto only_inputs = CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, 5), 5);
auto allow_unused = CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, 6), 6); auto allow_unused = CastPyArg2AttrBoolean(PyTuple_GET_ITEM(args, 6), 6);
auto no_grad_vars = CastPyArg2VectorOfTensor(PyTuple_GET_ITEM(args, 7), 7); auto no_grad_vars = CastPyArg2VectorOfTensor(PyTuple_GET_ITEM(args, 7), 7);
std::vector<paddle::experimental::Tensor> result;
std::vector<paddle::experimental::Tensor> result = egr::Grad(tensors, {
inputs, eager_gil_scoped_release guard;
grad_tensors, result = egr::Grad(tensors,
retain_graph, inputs,
create_graph, grad_tensors,
only_inputs, retain_graph,
allow_unused, create_graph,
no_grad_vars); only_inputs,
allow_unused,
no_grad_vars);
}
VLOG(1) << " in eager_api_run_partial_grad, after runing egr::Grad"; VLOG(1) << " in eager_api_run_partial_grad, after runing egr::Grad";
return ToPyObject(result, true /* return_py_none_if_not_initialize */); return ToPyObject(result, true /* return_py_none_if_not_initialize */);
EAGER_CATCH_AND_THROW_RETURN_NULL EAGER_CATCH_AND_THROW_RETURN_NULL
...@@ -179,18 +185,21 @@ static PyObject* eager_api_read_next_tensor_list(PyObject* self, ...@@ -179,18 +185,21 @@ static PyObject* eager_api_read_next_tensor_list(PyObject* self,
auto tensor_base_list = auto tensor_base_list =
CastPyArg2VectorOfTensorBase(PyTuple_GET_ITEM(args, 0), 0); CastPyArg2VectorOfTensorBase(PyTuple_GET_ITEM(args, 0), 0);
std::vector<paddle::experimental::Tensor> tensor_list; std::vector<paddle::experimental::Tensor> tensor_list;
tensor_list.reserve(tensor_base_list.size()); {
auto func = [](framework::Tensor& tensor_base) { eager_gil_scoped_release guard;
paddle::experimental::Tensor tensor( tensor_list.reserve(tensor_base_list.size());
egr::Controller::Instance().GenerateUniqueName()); auto func = [](framework::Tensor& tensor_base) {
auto autograd_meta = egr::EagerUtils::autograd_meta(&tensor); paddle::experimental::Tensor tensor(
autograd_meta->SetPersistable(false); egr::Controller::Instance().GenerateUniqueName());
autograd_meta->SetStopGradient(true); auto autograd_meta = egr::EagerUtils::autograd_meta(&tensor);
tensor.set_impl(std::make_shared<phi::DenseTensor>(tensor_base)); autograd_meta->SetPersistable(false);
return tensor; autograd_meta->SetStopGradient(true);
}; tensor.set_impl(std::make_shared<phi::DenseTensor>(tensor_base));
for (auto& tensor_base : tensor_base_list) { return tensor;
tensor_list.emplace_back(func(tensor_base)); };
for (auto& tensor_base : tensor_base_list) {
tensor_list.emplace_back(func(tensor_base));
}
} }
return ToPyObject(tensor_list); return ToPyObject(tensor_list);
EAGER_CATCH_AND_THROW_RETURN_NULL EAGER_CATCH_AND_THROW_RETURN_NULL
......
...@@ -253,5 +253,17 @@ std::vector<paddle::framework::Scope*> GetScopePtrListFromArgs( ...@@ -253,5 +253,17 @@ std::vector<paddle::framework::Scope*> GetScopePtrListFromArgs(
ssize_t arg_idx, ssize_t arg_idx,
bool dispensable); bool dispensable);
class eager_gil_scoped_release {
public:
eager_gil_scoped_release() { tstate = PyEval_SaveThread(); }
~eager_gil_scoped_release() {
if (!tstate) return;
PyEval_RestoreThread(tstate);
}
private:
PyThreadState* tstate{nullptr};
};
} // namespace pybind } // namespace pybind
} // namespace paddle } // namespace paddle
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册