mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
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
40 lines
1.0 KiB
C++
40 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <c10/core/impl/PyInterpreter.h>
|
|
#include <c10/macros/Export.h>
|
|
#include <c10/util/Registry.h>
|
|
#include <memory>
|
|
|
|
namespace c10::impl {
|
|
|
|
// Minimal interface for PyInterpreter hooks
|
|
struct C10_API PyInterpreterHooksInterface {
|
|
virtual ~PyInterpreterHooksInterface() = default;
|
|
|
|
// Get the PyInterpreter instance
|
|
// Stub implementation throws error when Python is not available
|
|
virtual PyInterpreter* getPyInterpreter() const {
|
|
TORCH_CHECK(
|
|
false,
|
|
"PyTorch was compiled without Python support. "
|
|
"Cannot access Python interpreter from C++.");
|
|
}
|
|
};
|
|
|
|
struct C10_API PyInterpreterHooksArgs{};
|
|
|
|
C10_DECLARE_REGISTRY(
|
|
PyInterpreterHooksRegistry,
|
|
PyInterpreterHooksInterface,
|
|
PyInterpreterHooksArgs);
|
|
|
|
#define REGISTER_PYTHON_HOOKS(clsname) \
|
|
C10_REGISTER_CLASS(PyInterpreterHooksRegistry, clsname, clsname)
|
|
|
|
// Get the global PyInterpreter hooks instance
|
|
C10_API const PyInterpreterHooksInterface& getPyInterpreterHooks();
|
|
|
|
C10_API PyInterpreter* getGlobalPyInterpreter();
|
|
|
|
} // namespace c10::impl
|