[BE] Make PyObjectSlot use a global PyInterpreter (#162659)

This pr gets rid of the pyobj_interpreter_ variable from PyObjectSlot and saves a word in the process

Gonna ask for review from @huydhn as there are some changes to CI.

Testing: imported internally and the failed android build seems to work now!

Pull Request resolved: https://github.com/pytorch/pytorch/pull/162659
Approved by: https://github.com/albanD, https://github.com/huydhn
This commit is contained in:
PaliC
2025-09-25 08:53:19 +00:00
committed by PyTorch MergeBot
parent 5f90e8c7ae
commit 29cbcbac42
10 changed files with 45 additions and 73 deletions

View File

@ -265,8 +265,7 @@ PyObject* THPVariable_Wrap(const at::TensorBase& var) {
}
std::optional<PyObject*> mb_obj =
var.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj(
/*ignore_hermetic_tls=*/false);
var.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj();
if (mb_obj.has_value()) {
auto obj = *mb_obj;
if (obj) {
@ -329,8 +328,8 @@ static bool isResurrectable(THPVariable* self) {
return false;
}
// Check if this is hermetic. If it is, no resurrection.
if (tensor.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj(
/*ignore_hermetic_tls=*/false) != (PyObject*)self) {
if (tensor.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj() !=
(PyObject*)self) {
return false;
}
return true;
@ -355,8 +354,7 @@ static bool THPVariable_tryResurrect(THPVariable* self) {
!tensor.unsafeGetTensorImpl()->pyobj_slot()->owns_pyobj());
c10::TensorImpl* tensor_impl = tensor.unsafeGetTensorImpl();
auto maybe_pyobj = tensor_impl->pyobj_slot()->check_pyobj(
/*ignore_hermetic_tls=*/false);
auto maybe_pyobj = tensor_impl->pyobj_slot()->check_pyobj();
TORCH_INTERNAL_ASSERT(
maybe_pyobj.has_value(),
@ -2223,8 +2221,8 @@ static int THPVariable_subclass_clear(THPVariable* self) {
// because Tensor asked us to (it's already destructing).
if (!self->cdata.unsafeIsBorrowed() &&
tensor.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj(
/*ignore_hermetic_tls=*/false) == (PyObject*)self) {
tensor.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj() ==
(PyObject*)self) {
// TODO: empirically, on OS X this assert appears to be untrue
// In test_py_tensors_multi_async_call - ProcessGroupRpcTestWithSpawn
// distributed/rpc/test_process_group_agent.py
@ -2410,8 +2408,7 @@ static PyObject* THPVariable_NewWithVar(
// This function overwrite the Tensor's pyobj field without extra checks
// Make sure it is not set otherwise we would leak memory
auto mb_obj = _var.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj(
/*ignore_hermetic_tls=*/false);
auto mb_obj = _var.unsafeGetTensorImpl()->pyobj_slot()->check_pyobj();
// Under some circumstances, we may attempt to create a new Python
// object for a variable that already has a Python object. The most common