#include <torch/csrc/Exceptions.h>
#include <torch/csrc/FakeTensorMode.h>
#include <torch/csrc/PyInterpreter.h>
#include <array>
#include <mutex>
#include <new>
#include <type_traits>
namespace torch::fake_tensor {
static py::handle get_cpp_fake_tensor_mode_class() {
#if IS_PYBIND_2_13_PLUS
PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object>
storage;
return storage
.call_once_and_store_result([]() -> py::object {
return py::module::import("torch._subclasses.fake_tensor")
.attr("CppFakeTensorMode");
})
.get_stored();
#else
static py::handle cls =
py::object(py::module::import("torch._subclasses.fake_tensor")
.attr("CppFakeTensorMode"))
.release();
return cls;
#endif
}
struct PyCppFakeTensorMode {
PyObject_HEAD
std::shared_ptr<c10::FakeTensorMode> mode;
PyObject* weakreflist;
};
static PyObject* cpp_fake_tensor_mode_new(
PyTypeObject* type,
PyObject*,
PyObject*) {
auto* self = reinterpret_cast<PyCppFakeTensorMode*>(type->tp_alloc(type, 0));
if (self == nullptr) {
return nullptr;
}
new (&self->mode) std::shared_ptr<c10::FakeTensorMode>();
self->weakreflist = nullptr;
return reinterpret_cast<PyObject*>(self);
}
static void cpp_fake_tensor_mode_dealloc(PyObject* obj) {
auto* self = reinterpret_cast<PyCppFakeTensorMode*>(obj);
if (self->weakreflist != nullptr) {
PyObject_ClearWeakRefs(obj);
}
self->mode.~shared_ptr();
Py_TYPE(obj)->tp_free(obj);
}
std::shared_ptr<c10::FakeTensorMode> getCppFakeTensorMode(py::handle py_mode) {
TORCH_CHECK_TYPE(
Py_TYPE(py_mode.ptr()) ==
reinterpret_cast<PyTypeObject*>(
get_cpp_fake_tensor_mode_class().ptr()),
"Expected a CppFakeTensorMode (not a subclass, nor a bare ",
"_CppFakeTensorMode), got ",
Py_TYPE(py_mode.ptr())->tp_name);
auto& mode = reinterpret_cast<PyCppFakeTensorMode*>(py_mode.ptr())->mode;
TORCH_CHECK(mode != nullptr, "CppFakeTensorMode is not initialized");
return mode;
}
static int cpp_fake_tensor_mode_init(
PyObject* obj,
PyObject* args,
PyObject* kwargs) {
HANDLE_TH_ERRORS
PyObject* converter;
PyObject* shape_env;
PyObject* creation_traceback = Py_None;
static const char* names[] = {
"converter", "shape_env", "creation_traceback", nullptr};
if (!PyArg_ParseTupleAndKeywords(
args,
kwargs,
"OO|O:_CppFakeTensorMode",
const_cast<char**>(names),
&converter,
&shape_env,
&creation_traceback)) {
return -1;
}
TORCH_CHECK(converter != Py_None, "Expected a FakeTensorConverter");
TORCH_CHECK_TYPE(
Py_TYPE(obj) ==
reinterpret_cast<PyTypeObject*>(
get_cpp_fake_tensor_mode_class().ptr()),
"Expected a CppFakeTensorMode (not a subclass, nor a bare ",
"_CppFakeTensorMode), got ",
Py_TYPE(obj)->tp_name);
auto* self = reinterpret_cast<PyCppFakeTensorMode*>(obj);
TORCH_CHECK(
self->mode == nullptr, "CppFakeTensorMode is already initialized");
std::shared_ptr<c10::SafePyObject> shape_env_obj;
if (shape_env != Py_None) {
shape_env_obj = std::make_shared<c10::SafePyObject>(
Py_NewRef(shape_env), getPyInterpreter());
}
auto config = py::module::import("torch._functorch.config");
auto mode = std::make_shared<c10::FakeTensorMode>(
std::move(shape_env_obj),
std::make_shared<c10::SafePyObject>(
Py_NewRef(converter), getPyInterpreter()),
config.attr("fake_tensor_allow_meta").cast<bool>(),
config.attr("fake_tensor_allow_unsafe_data_ptr_access").cast<bool>(),
config.attr("fake_tensor_propagate_real_tensors").cast<bool>());
mode->avoid_device_init_ = py::module::import("torch._subclasses.fake_tensor")
.attr("_avoid_device_init")()
.cast<bool>();
mode->prefer_device_type_ = config.attr("fake_tensor_prefer_device_type")
.cast<std::optional<std::string>>();
if (creation_traceback != Py_None) {
mode->creation_traceback_ = std::make_shared<c10::SafePyObject>(
Py_NewRef(creation_traceback), getPyInterpreter());
}
auto weakref =
py::reinterpret_steal<py::object>(PyWeakref_NewRef(obj, nullptr));
if (!weakref) {
throw py::error_already_set();
}
mode->fake_mode_pyobj_ =
std::make_shared<c10::SafePyObject>(weakref.ptr(), getPyInterpreter());
weakref.release();
TORCH_CHECK(
self->mode == nullptr, "CppFakeTensorMode is already initialized");
self->mode = std::move(mode);
return 0;
END_HANDLE_TH_ERRORS_RET(-1)
}
template <auto member>
static PyObject* get_mode_field(PyObject* self, void*) {
HANDLE_TH_ERRORS
return py::cast(getCppFakeTensorMode(self).get()->*member).release().ptr();
END_HANDLE_TH_ERRORS
}
template <auto member>
static int set_mode_field(PyObject* self, PyObject* value, void*) {
HANDLE_TH_ERRORS
if (value == nullptr) {
PyErr_SetString(
PyExc_AttributeError, "Cannot delete a CppFakeTensorMode field");
return -1;
}
auto mode = getCppFakeTensorMode(self);
using T = std::remove_reference_t<decltype(mode.get()->*member)>;
try {
mode.get()->*member = py::cast<T>(py::handle(value));
} catch (const py::cast_error& e) {
PyErr_SetString(PyExc_TypeError, e.what());
return -1;
}
return 0;
END_HANDLE_TH_ERRORS_RET(-1)
}
template <auto member>
static PyObject* get_mode_pyobject(PyObject* self, void*) {
HANDLE_TH_ERRORS
auto mode = getCppFakeTensorMode(self);
const auto& value = mode.get()->*member;
if constexpr (member == &c10::FakeTensorMode::fake_tensor_converter_) {
TORCH_CHECK(value, "This C++ FakeTensorMode has no FakeTensorConverter");
}
return Py_NewRef(value ? value->ptr(getPyInterpreter()) : Py_None);
END_HANDLE_TH_ERRORS
}
static PyObject* clear_non_cpu_constants(PyObject* self, PyObject* unused) {
HANDLE_TH_ERRORS
getCppFakeTensorMode(self)->clear_non_cpu_constants();
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static std::array<PyMethodDef, 2> mode_methods = {{
{"clear_non_cpu_constants", clear_non_cpu_constants, METH_NOARGS, nullptr},
{nullptr},
}};
static PyGetSetDef mode_properties[] = {
{"avoid_device_init",
get_mode_field<&c10::FakeTensorMode::avoid_device_init_>,
nullptr,
nullptr,
nullptr},
{"_creation_traceback",
get_mode_pyobject<&c10::FakeTensorMode::creation_traceback_>,
nullptr,
nullptr,
nullptr},
{"static_shapes",
get_mode_field<&c10::FakeTensorMode::static_shapes_>,
set_mode_field<&c10::FakeTensorMode::static_shapes_>,
nullptr,
nullptr},
{"allow_meta",
get_mode_field<&c10::FakeTensorMode::allow_meta_>,
set_mode_field<&c10::FakeTensorMode::allow_meta_>,
nullptr,
nullptr},
{"_allow_unsafe_data_ptr_access",
get_mode_field<&c10::FakeTensorMode::allow_unsafe_data_ptr_access_>,
set_mode_field<&c10::FakeTensorMode::allow_unsafe_data_ptr_access_>,
nullptr,
nullptr},
{"allow_non_fake_inputs",
get_mode_field<&c10::FakeTensorMode::allow_non_fake_inputs_>,
set_mode_field<&c10::FakeTensorMode::allow_non_fake_inputs_>,
nullptr,
nullptr},
{"allow_fallback_kernels",
get_mode_field<&c10::FakeTensorMode::allow_fallback_kernels_>,
set_mode_field<&c10::FakeTensorMode::allow_fallback_kernels_>,
nullptr,
nullptr},
{"propagate_real_tensors",
get_mode_field<&c10::FakeTensorMode::propagate_real_tensors_>,
set_mode_field<&c10::FakeTensorMode::propagate_real_tensors_>,
nullptr,
nullptr},
{"epoch",
get_mode_field<&c10::FakeTensorMode::epoch_>,
set_mode_field<&c10::FakeTensorMode::epoch_>,
nullptr,
nullptr},
{"allow_scalar_outputs",
get_mode_field<&c10::FakeTensorMode::allow_scalar_outputs_>,
set_mode_field<&c10::FakeTensorMode::allow_scalar_outputs_>,
nullptr,
nullptr},
{"fake_tensor_converter",
get_mode_pyobject<&c10::FakeTensorMode::fake_tensor_converter_>,
nullptr,
nullptr,
nullptr},
{"shape_env",
get_mode_pyobject<&c10::FakeTensorMode::shape_env_>,
nullptr,
nullptr,
nullptr},
{nullptr}};
PyTypeObject PyCppFakeTensorModeType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"torch._C._CppFakeTensorMode",
sizeof(PyCppFakeTensorMode),
0,
cpp_fake_tensor_mode_dealloc,
0,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
nullptr,
nullptr,
nullptr,
nullptr,
offsetof(PyCppFakeTensorMode, weakreflist),
nullptr,
nullptr,
mode_methods.data(),
nullptr,
mode_properties,
nullptr,
nullptr,
nullptr,
nullptr,
0,
cpp_fake_tensor_mode_init,
nullptr,
cpp_fake_tensor_mode_new,
};
py::object getCppFakeTensorModePyObj(
const std::shared_ptr<c10::FakeTensorMode>& mode) {
if (mode == nullptr) {
return py::none();
}
std::shared_ptr<c10::SafePyObject> cached;
{
std::lock_guard<std::mutex> lock(mode->fake_mode_pyobj_mutex_);
cached = mode->fake_mode_pyobj_;
}
if (cached != nullptr) {
PyObject* py_mode = nullptr;
if (PyWeakref_GetRef(cached->ptr(getPyInterpreter()), &py_mode) < 0) {
throw py::error_already_set();
}
if (py_mode != nullptr) {
return py::reinterpret_steal<py::object>(py_mode);
}
}
auto py_mode = py::reinterpret_steal<py::object>(cpp_fake_tensor_mode_new(
reinterpret_cast<PyTypeObject*>(get_cpp_fake_tensor_mode_class().ptr()),
nullptr,
nullptr));
if (!py_mode) {
throw py::error_already_set();
}
reinterpret_cast<PyCppFakeTensorMode*>(py_mode.ptr())->mode = mode;
PyObject* weakref = PyWeakref_NewRef(py_mode.ptr(), nullptr);
if (weakref == nullptr) {
throw py::error_already_set();
}
auto fresh = std::make_shared<c10::SafePyObject>(weakref, getPyInterpreter());
std::lock_guard<std::mutex> lock(mode->fake_mode_pyobj_mutex_);
if (mode->fake_mode_pyobj_ != cached) {
PyObject* installed = nullptr;
if (PyWeakref_GetRef(
mode->fake_mode_pyobj_->ptr(getPyInterpreter()), &installed) < 0) {
throw py::error_already_set();
}
if (installed != nullptr) {
return py::reinterpret_steal<py::object>(installed);
}
}
mode->fake_mode_pyobj_.swap(fresh);
return py_mode;
}
}