#pragma once
#include <torch/csrc/Layout.h>
#include <torch/csrc/python_headers.h>
#include <torch/csrc/utils/pythoncapi_compat.h>
#include <ATen/core/Tensor.h>
#include <ATen/core/jit_type_base.h>
#include <c10/util/irange.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <torch/csrc/Device.h>
#include <torch/csrc/Dtype.h>
#include <torch/csrc/DynamicTypes.h>
#include <torch/csrc/Generator.h>
#include <torch/csrc/MemoryFormat.h>
#include <torch/csrc/Stream.h>
#include <torch/csrc/utils/tensor_memoryformats.h>
namespace py = pybind11;
#define IS_PYBIND_2_13_PLUS PYBIND11_VERSION_HEX >= 0x020D0000
PYBIND11_DECLARE_HOLDER_TYPE(T, c10::intrusive_ptr<T>, true)
PYBIND11_DECLARE_HOLDER_TYPE(T, c10::SingletonOrSharedTypePtr<T>)
PYBIND11_DECLARE_HOLDER_TYPE(T, c10::SingletonTypePtr<T>, true)
namespace pybind11::detail {
template <>
struct TORCH_PYTHON_API type_caster<at::Tensor> {
public:
PYBIND11_TYPE_CASTER(at::Tensor, _("torch.Tensor"));
bool load(handle src, bool );
static handle cast(
const at::Tensor& src,
return_value_policy ,
handle );
};
template <>
struct type_caster<at::Storage> {
public:
PYBIND11_TYPE_CASTER(at::Storage, _("torch.StorageBase"));
bool load(handle src, bool ) {
PyObject* obj = src.ptr();
if (torch::isStorage(obj)) {
value = torch::createStorage(obj);
return true;
}
return false;
}
static handle cast(
const at::Storage& src,
return_value_policy ,
handle ) {
return handle(torch::createPyObject(src));
}
};
template <>
struct type_caster<at::Generator> {
public:
PYBIND11_TYPE_CASTER(at::Generator, _("torch.Generator"));
bool load(handle src, bool ) {
PyObject* obj = src.ptr();
if (THPGenerator_Check(obj)) {
value = reinterpret_cast<THPGenerator*>(obj)->cdata;
return true;
}
return false;
}
static handle cast(
const at::Generator& src,
return_value_policy ,
handle ) {
return handle(THPGenerator_Wrap(src));
}
};
template <>
struct TORCH_PYTHON_API type_caster<at::IntArrayRef> {
public:
PYBIND11_TYPE_CASTER(at::IntArrayRef, _("Tuple[int, ...]"));
bool load(handle src, bool );
static handle cast(
at::IntArrayRef src,
return_value_policy ,
handle );
private:
std::vector<int64_t> v_value;
};
template <>
struct TORCH_PYTHON_API type_caster<at::SymIntArrayRef> {
public:
PYBIND11_TYPE_CASTER(at::SymIntArrayRef, _("List[int]"));
bool load(handle src, bool );
static handle cast(
at::SymIntArrayRef src,
return_value_policy ,
handle );
private:
std::vector<c10::SymInt> v_value;
};
template <>
struct TORCH_PYTHON_API type_caster<at::ArrayRef<c10::SymNode>> {
public:
PYBIND11_TYPE_CASTER(at::ArrayRef<c10::SymNode>, _("List[SymNode]"));
bool load(handle src, bool );
static handle cast(
at::ArrayRef<c10::SymNode> src,
return_value_policy ,
handle );
private:
std::vector<c10::SymNode> v_value;
};
template <>
struct type_caster<at::MemoryFormat> {
public:
PYBIND11_TYPE_CASTER(at::MemoryFormat, _("torch.memory_format"));
bool load(handle src, bool ) {
PyObject* obj = src.ptr();
if (THPMemoryFormat_Check(obj)) {
value = reinterpret_cast<THPMemoryFormat*>(obj)->memory_format;
return true;
}
return false;
}
static handle cast(
at::MemoryFormat src,
return_value_policy ,
handle ) {
return handle(Py_NewRef(torch::utils::getTHPMemoryFormat(src)));
}
};
template <>
struct type_caster<at::Device> {
public:
PYBIND11_TYPE_CASTER(at::Device, _("torch.device"));
type_caster() : value(c10::kCPU) {}
bool load(handle src, bool ) {
PyObject* obj = src.ptr();
if (THPDevice_Check(obj)) {
value = reinterpret_cast<THPDevice*>(obj)->device;
return true;
}
return false;
}
static handle cast(
const at::Device& src,
return_value_policy ,
handle ) {
return handle(THPDevice_New(src));
}
};
template <>
struct type_caster<at::ScalarType> {
public:
PYBIND11_TYPE_CASTER(at::ScalarType, _("torch.dtype"));
type_caster() : value(at::kFloat) {}
bool load(handle src, bool ) {
PyObject* obj = src.ptr();
if (THPDtype_Check(obj)) {
value = reinterpret_cast<THPDtype*>(obj)->scalar_type;
return true;
}
return false;
}
static handle cast(
const at::ScalarType& src,
return_value_policy ,
handle ) {
return Py_NewRef(torch::getTHPDtype(src));
}
};
template <>
struct type_caster<c10::Stream> {
public:
PYBIND11_TYPE_CASTER(c10::Stream, _("torch.Stream"));
type_caster() : value(c10::Stream::DEFAULT, c10::Device(c10::kCPU, 0)) {}
bool load(handle src, bool ) {
PyObject* obj = src.ptr();
if (THPStream_Check(obj)) {
value = c10::Stream::unpack3(
((THPStream*)obj)->stream_id,
static_cast<c10::DeviceIndex>(((THPStream*)obj)->device_index),
static_cast<c10::DeviceType>(((THPStream*)obj)->device_type));
return true;
}
return false;
}
static handle cast(
const c10::Stream& src,
return_value_policy ,
handle ) {
return handle(THPStream_Wrap(src));
}
};
template <>
struct type_caster<c10::DispatchKey>
: public type_caster_base<c10::DispatchKey> {
using base = type_caster_base<c10::DispatchKey>;
c10::DispatchKey tmp{};
public:
bool load(handle src, bool convert) {
if (base::load(src, convert)) {
return true;
} else if (py::isinstance(
src, py::module_::import("builtins").attr("str"))) {
tmp = c10::parseDispatchKey(py::cast<std::string>(src));
value = &tmp;
return true;
}
return false;
}
static handle cast(
c10::DispatchKey src,
return_value_policy policy,
handle parent) {
return base::cast(src, policy, parent);
}
};
template <>
struct TORCH_PYTHON_API type_caster<c10::Scalar> {
public:
PYBIND11_TYPE_CASTER(
c10::Scalar,
_("Union[Number, torch.SymInt, torch.SymFloat, torch.SymBool]"));
bool load(py::handle src, bool );
static py::handle cast(
const c10::Scalar& si,
return_value_policy ,
handle );
};
template <>
struct TORCH_PYTHON_API type_caster<c10::SymInt> {
public:
PYBIND11_TYPE_CASTER(c10::SymInt, _("Union[int, torch.SymInt]"));
bool load(py::handle src, bool );
static py::handle cast(
const c10::SymInt& si,
return_value_policy ,
handle );
};
template <>
struct TORCH_PYTHON_API type_caster<c10::SymFloat> {
public:
PYBIND11_TYPE_CASTER(c10::SymFloat, _("float"));
bool load(py::handle src, bool );
static py::handle cast(
const c10::SymFloat& si,
return_value_policy ,
handle );
};
template <>
struct TORCH_PYTHON_API type_caster<c10::SymBool> {
public:
PYBIND11_TYPE_CASTER(c10::SymBool, _("Union[bool, torch.SymBool]"));
bool load(py::handle src, bool );
static py::handle cast(
const c10::SymBool& si,
return_value_policy ,
handle );
};
template <typename T>
struct type_caster<c10::complex<T>> {
public:
PYBIND11_TYPE_CASTER(c10::complex<T>, _("complex"));
bool load(handle src, bool ) {
PyObject* obj = src.ptr();
Py_complex py_complex = PyComplex_AsCComplex(obj);
if (py_complex.real == -1.0 && PyErr_Occurred()) {
return false;
}
value = c10::complex<double>(py_complex.real, py_complex.imag);
return true;
}
static handle cast(
const c10::complex<T>& complex,
return_value_policy ,
handle ) {
return handle(PyComplex_FromDoubles(complex.real(), complex.imag()));
}
};
template <>
struct type_caster<c10::Layout> {
public:
PYBIND11_TYPE_CASTER(c10::Layout, _("torch.layout"));
static handle cast(
c10::Layout layout,
return_value_policy ,
handle ) {
return handle(Py_NewRef(torch::getTHPLayout(layout)));
}
bool load(handle src, bool ) {
if (!THPLayout_Check(src.ptr())) {
return false;
}
const auto layout = reinterpret_cast<THPLayout*>(src.ptr());
value = layout->layout;
return true;
}
};
}
namespace torch::impl {
template <typename T>
inline void destroy_without_gil(T* ptr) {
if (Py_IsInitialized() && PyGILState_Check()) {
pybind11::gil_scoped_release nogil;
delete ptr;
} else {
delete ptr;
}
}
}