Files
pytorch/torch/csrc/Storage.h
PaliC 1b99c1859c [BE] Make PyObjectSlot use a global PyInterpreter and remove (#158427)
This PR is a bit more involved but effectively works to drastically simplify PyObjectSlot and PyInterpreter.
1) For PyObjectSlot we now use a global pyinterpreter since there only is one. From here we change all of the call sites to rely on this assumption.
2) We also remove the "tags" of the PyInterpreter by deprecating `PyInterpreterStatus`.

For the reviewer, sadly it seems like `functorch/csrc/dim/dim.cpp` needed to get linted, so there is an unreadable amount of changes there. Fortunately, the only actual change in the file is as follows which just removes `getPyInterpreter()` from  the `check_pyobj` call.

```
 mpy::handle handle_from_tensor(Arena& A, TensorRef t) {
-    // fast case: tensor is live in python
-    std::optional<PyObject*> mb_obj =
-        t->unsafeGetTensorImpl()->pyobj_slot()->check_pyobj(getPyInterpreter(), /*ignore_hermetic_tls=*/false);
-    if (mb_obj.has_value() && !t->unsafeGetTensorImpl()->pyobj_slot()->owns_pyobj()) {
-        return *mb_obj;
-    }
-    return A.autorelease(mpy::object::checked_steal(THPVariable_Wrap(*t)));
-}
-}
+  // fast case: tensor is live in python
+  std::optional<PyObject*> mb_obj =
+      t->unsafeGetTensorImpl()->pyobj_slot()->check_pyobj(
+          /*ignore_hermetic_tls=*/false);
+  if (mb_obj.has_value() &&
+      !t->unsafeGetTensorImpl()->pyobj_slot()->owns_pyobj()) {
+    return *mb_obj;
+  }
+  return A.autorelease(mpy::object::checked_steal(THPVariable_Wrap(*t)));
+}
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/158427
Approved by: https://github.com/albanD
2025-07-30 17:29:43 +00:00

60 lines
1.5 KiB
C

#ifndef THP_STORAGE_INC
#define THP_STORAGE_INC
#include <Python.h>
#include <c10/core/Storage.h>
#include <torch/csrc/Exceptions.h>
#include <torch/csrc/Export.h>
#include <torch/csrc/Types.h>
#define THPStorageStr "torch.UntypedStorage"
struct THPStorage {
PyObject_HEAD
c10::MaybeOwned<c10::Storage> cdata;
bool is_hermetic;
};
TORCH_PYTHON_API PyObject* THPStorage_Wrap(c10::Storage storage);
TORCH_PYTHON_API PyObject* THPStorage_NewWithStorage(
PyTypeObject* type,
c10::Storage _storage,
bool allow_preexisting_pyobj = false);
TORCH_PYTHON_API extern PyTypeObject* THPStorageClass;
inline bool THPStorage_CheckTypeExact(PyTypeObject* tp) {
return tp == THPStorageClass;
}
inline bool THPStorage_CheckExact(PyObject* obj) {
return THPStorage_CheckTypeExact(Py_TYPE(obj));
}
inline bool THPStorage_Check(PyObject* obj) {
if (!THPStorageClass)
return false;
const auto result = PyObject_IsInstance(obj, (PyObject*)THPStorageClass);
if (result == -1)
throw python_error();
return result;
}
bool THPStorage_init(PyObject* module);
void THPStorage_postInit(PyObject* module);
void THPStorage_assertNotNull(THPStorage* storage);
TORCH_PYTHON_API void THPStorage_assertNotNull(PyObject* obj);
TORCH_PYTHON_API extern PyTypeObject THPStorageType;
inline const c10::Storage& THPStorage_Unpack(THPStorage* storage) {
return *storage->cdata;
}
inline const c10::Storage& THPStorage_Unpack(PyObject* obj) {
return THPStorage_Unpack(reinterpret_cast<THPStorage*>(obj));
}
#endif