mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 05:34:18 +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
33 lines
852 B
C++
33 lines
852 B
C++
#include <c10/core/impl/PyInterpreterHooks.h>
|
|
|
|
namespace c10::impl {
|
|
|
|
// Define the registry
|
|
C10_DEFINE_REGISTRY(
|
|
PyInterpreterHooksRegistry,
|
|
PyInterpreterHooksInterface,
|
|
PyInterpreterHooksArgs)
|
|
|
|
const PyInterpreterHooksInterface& getPyInterpreterHooks() {
|
|
auto create_impl = [] {
|
|
#if !defined C10_MOBILE
|
|
auto hooks = PyInterpreterHooksRegistry()->Create(
|
|
"PyInterpreterHooks", PyInterpreterHooksArgs{});
|
|
if (hooks) {
|
|
return hooks;
|
|
}
|
|
#endif
|
|
// Return stub implementation that will throw errors when methods are called
|
|
return std::make_unique<PyInterpreterHooksInterface>();
|
|
};
|
|
static auto hooks = create_impl();
|
|
return *hooks;
|
|
}
|
|
|
|
// Main function to get global PyInterpreter
|
|
PyInterpreter* getGlobalPyInterpreter() {
|
|
return getPyInterpreterHooks().getPyInterpreter();
|
|
}
|
|
|
|
} // namespace c10::impl
|