[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
This commit is contained in:
Rodrigo Kumpera 2023-06-23 19:13:46 +00:00 committed by PyTorch MergeBot
parent b5d1b42f99
commit 93e63fc0f6

View file

@ -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
}