mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Summary: This is a re-attempt to land the iwyu header changes, by taking the diff from [PR 100304](https://github.com/pytorch/pytorch/pull/100304), and adding the bare minimal changes to make the diff build corectly in the internal builds. X-link: https://github.com/facebookresearch/pytorch3d/pull/1541 X-link: https://github.com/fairinternal/pytorch3d/pull/44 - Re-work D45769819 to fix header inclusions in c10 Test Plan: ``` buck2 build --no-remote-cache mode/dev-nosan //caffe2/c10/... buck2 build --no-remote-cache mode/dev-nosan //deeplearning/fbgemm/fbgemm_gpu/... buck2 build mode/dev-nosan //vision/fair/pytorch3d/pytorch3d:_C ``` Reviewed By: malfet Differential Revision: D45920611 Pull Request resolved: https://github.com/pytorch/pytorch/pull/101846 Approved by: https://github.com/malfet, https://github.com/Skylion007
71 lines
2.1 KiB
C++
71 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include <c10/core/impl/PyInterpreter.h>
|
|
#include <c10/macros/Export.h>
|
|
#include <c10/util/python_stub.h>
|
|
|
|
namespace c10 {
|
|
|
|
// This is an safe owning holder for a PyObject, akin to pybind11's
|
|
// py::object, with two major differences:
|
|
//
|
|
// - It is in c10/core; i.e., you can use this type in contexts where
|
|
// you do not have a libpython dependency
|
|
//
|
|
// - It is multi-interpreter safe (ala torchdeploy); when you fetch
|
|
// the underlying PyObject* you are required to specify what the current
|
|
// interpreter context is and we will check that you match it.
|
|
//
|
|
// It is INVALID to store a reference to a Tensor object in this way;
|
|
// you should just use TensorImpl directly in that case!
|
|
struct C10_API SafePyObject {
|
|
// Steals a reference to data
|
|
SafePyObject(PyObject* data, c10::impl::PyInterpreter* pyinterpreter)
|
|
: data_(data), pyinterpreter_(pyinterpreter) {}
|
|
|
|
// In principle this could be copyable if we add an incref to PyInterpreter
|
|
// but for now it's easier to just disallow it.
|
|
SafePyObject(SafePyObject const&) = delete;
|
|
SafePyObject& operator=(SafePyObject const&) = delete;
|
|
|
|
~SafePyObject() {
|
|
(*pyinterpreter_)->decref(data_, /*is_tensor*/ false);
|
|
}
|
|
|
|
c10::impl::PyInterpreter& pyinterpreter() const {
|
|
return *pyinterpreter_;
|
|
}
|
|
PyObject* ptr(const c10::impl::PyInterpreter*) const;
|
|
|
|
private:
|
|
PyObject* data_;
|
|
c10::impl::PyInterpreter* pyinterpreter_;
|
|
};
|
|
|
|
// Like SafePyObject, but non-owning. Good for references to global PyObjects
|
|
// that will be leaked on interpreter exit. You get a copy constructor/assign
|
|
// this way.
|
|
struct C10_API SafePyHandle {
|
|
SafePyHandle() : data_(nullptr), pyinterpreter_(nullptr) {}
|
|
SafePyHandle(PyObject* data, c10::impl::PyInterpreter* pyinterpreter)
|
|
: data_(data), pyinterpreter_(pyinterpreter) {}
|
|
|
|
c10::impl::PyInterpreter& pyinterpreter() const {
|
|
return *pyinterpreter_;
|
|
}
|
|
PyObject* ptr(const c10::impl::PyInterpreter*) const;
|
|
void reset() {
|
|
data_ = nullptr;
|
|
pyinterpreter_ = nullptr;
|
|
}
|
|
operator bool() {
|
|
return data_;
|
|
}
|
|
|
|
private:
|
|
PyObject* data_;
|
|
c10::impl::PyInterpreter* pyinterpreter_;
|
|
};
|
|
|
|
} // namespace c10
|