From 93e63fc0f62c36e08853db59d540ebfdf253ee98 Mon Sep 17 00:00:00 2001 From: Rodrigo Kumpera Date: Fri, 23 Jun 2023 19:13:46 +0000 Subject: [PATCH] [Core] Drop GIL in THPVariable_item around aten op (#104103) This can cause a deadlock by starving other python threads required for the kernel to make progress. Pull Request resolved: https://github.com/pytorch/pytorch/pull/104103 Approved by: https://github.com/albanD --- tools/autograd/templates/python_variable_methods.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/autograd/templates/python_variable_methods.cpp b/tools/autograd/templates/python_variable_methods.cpp index 1be745e5dd4..b1dd6beaea3 100644 --- a/tools/autograd/templates/python_variable_methods.cpp +++ b/tools/autograd/templates/python_variable_methods.cpp @@ -875,7 +875,11 @@ static PyObject * THPVariable_item(PyObject* self, PyObject* args) } jit::tracer::warn("Converting a tensor to a Python number", jit::tracer::WARN_PYTHON_DATAFLOW); auto& self_ = THPVariable_Unpack(self); - return py::cast(self_.item()).release().ptr(); + auto dispatch_item_ = [](const Tensor& self) -> at::Scalar { + pybind11::gil_scoped_release no_gil; + return self.item(); + }; + return py::cast(dispatch_item_(self_)).release().ptr(); END_HANDLE_TH_ERRORS }