#include <torch/csrc/PyObjectConversion.h>
#include <c10/util/ScopeExit.h>
#include <torch/csrc/Device.h>
#include <torch/csrc/Dtype.h>
#include <torch/csrc/DynamicTypes.h>
#include <torch/csrc/Generator.h>
#include <torch/csrc/Layout.h>
#include <torch/csrc/MemoryFormat.h>
#include <torch/csrc/autograd/python_variable.h>
#include <torch/csrc/python_headers.h>
#include <torch/csrc/utils/object_ptr.h>
#include <torch/csrc/utils/python_numbers.h>
#include <torch/csrc/utils/python_strings.h>
#include <torch/csrc/utils/pythoncapi_compat.h>
#include <torch/csrc/utils/tensor_memoryformats.h>
#include <utility>
namespace torch::detail {
namespace {
template <typename F>
auto with_python_error_message(F&& fn) {
try {
return std::forward<F>(fn)();
} catch (python_error& error) {
auto restore = c10::make_scope_exit([&] { error.restore(); });
error.persist();
throw;
}
}
struct ConcretePyObjectConversion final : PyObjectConversionInterface {
bool is_tensor_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_is_tensor_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
return THPVariableClass &&
PyObject_TypeCheck(
obj, reinterpret_cast<PyTypeObject*>(THPVariableClass));
}
at::Tensor tensor_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_tensor_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPVariableClass &&
PyObject_TypeCheck(
obj, reinterpret_cast<PyTypeObject*>(THPVariableClass)),
"torch_tensor_from_pyobject: expected torch.Tensor, got ",
Py_TYPE(obj)->tp_name);
return THPVariable_Unpack(obj);
}
PyObject* tensor_to_pyobject(const at::Tensor& t, PyObject* py_type)
const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_tensor_to_pyobject requires the GIL to be held");
TORCH_CHECK(
py_type == nullptr || PyType_Check(py_type),
"torch_tensor_to_pyobject: py_type must be a Python type object, got ",
Py_TYPE(py_type)->tp_name);
PyObject* py = (py_type != nullptr)
? THPVariable_Wrap(t, reinterpret_cast<PyTypeObject*>(py_type))
: THPVariable_Wrap(t);
TORCH_CHECK(
py != nullptr,
"torch_tensor_to_pyobject: THPVariable_Wrap returned null");
return py;
}
at::ScalarType dtype_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_dtype_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPDtype_Check(obj),
"torch_dtype_from_pyobject: expected torch.dtype, got ",
Py_TYPE(obj)->tp_name);
return reinterpret_cast<THPDtype*>(obj)->scalar_type;
}
PyObject* dtype_to_pyobject(at::ScalarType dtype) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_dtype_to_pyobject requires the GIL to be held");
const auto raw = static_cast<int64_t>(dtype);
TORCH_CHECK(
raw >= 0 && raw < static_cast<int64_t>(at::ScalarType::NumOptions),
"torch_dtype_to_pyobject: invalid dtype code ",
raw);
PyObject* py = reinterpret_cast<PyObject*>(torch::getTHPDtype(dtype));
Py_INCREF(py);
return py;
}
at::Device device_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_device_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPDevice_Check(obj),
"torch_device_from_pyobject: expected torch.device, got ",
Py_TYPE(obj)->tp_name);
return reinterpret_cast<THPDevice*>(obj)->device;
}
PyObject* device_to_pyobject(const at::Device& device) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_device_to_pyobject requires the GIL to be held");
PyObject* py = THPDevice_New(device);
TORCH_CHECK(
py != nullptr, "torch_device_to_pyobject: THPDevice_New returned null");
return py;
}
at::Layout layout_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_layout_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPLayout_Check(obj),
"torch_layout_from_pyobject: expected torch.layout, got ",
Py_TYPE(obj)->tp_name);
return reinterpret_cast<THPLayout*>(obj)->layout;
}
PyObject* layout_to_pyobject(at::Layout layout) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_layout_to_pyobject requires the GIL to be held");
PyObject* py = reinterpret_cast<PyObject*>(torch::getTHPLayout(layout));
Py_INCREF(py);
return py;
}
at::MemoryFormat memory_format_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_memory_format_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPMemoryFormat_Check(obj),
"torch_memory_format_from_pyobject: expected torch.memory_format, got ",
Py_TYPE(obj)->tp_name);
return reinterpret_cast<THPMemoryFormat*>(obj)->memory_format;
}
PyObject* memory_format_to_pyobject(
at::MemoryFormat memory_format) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_memory_format_to_pyobject requires the GIL to be held");
PyObject* py = torch::utils::getTHPMemoryFormat(memory_format);
Py_INCREF(py);
return py;
}
bool bool_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_bool_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
PyBool_Check(obj),
"torch_bool_from_pyobject: expected bool, got ",
Py_TYPE(obj)->tp_name);
return THPUtils_unpackBool(obj);
}
PyObject* bool_to_pyobject(bool value) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_bool_to_pyobject requires the GIL to be held");
return PyBool_FromLong(value);
}
int64_t int64_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_int64_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPUtils_checkLong(obj),
"torch_int64_from_pyobject: expected int, got ",
Py_TYPE(obj)->tp_name);
return with_python_error_message([&] { return THPUtils_unpackLong(obj); });
}
PyObject* int64_to_pyobject(int64_t value) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_int64_to_pyobject requires the GIL to be held");
return with_python_error_message([&] {
PyObject* py = THPUtils_packInt64(value);
TORCH_CHECK_PYTHON(py != nullptr);
return py;
});
}
double double_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_double_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPUtils_checkDouble(obj),
"torch_double_from_pyobject: expected float, got ",
Py_TYPE(obj)->tp_name);
return with_python_error_message(
[&] { return THPUtils_unpackDouble(obj); });
}
PyObject* double_to_pyobject(double value) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_double_to_pyobject requires the GIL to be held");
return with_python_error_message([&] {
PyObject* py = PyFloat_FromDouble(value);
TORCH_CHECK_PYTHON(py != nullptr);
return py;
});
}
std::string string_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_string_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPUtils_checkString(obj),
"torch_string_from_pyobject: expected str or bytes, got ",
Py_TYPE(obj)->tp_name);
return with_python_error_message([&] {
if (PyUnicode_Check(obj)) {
Py_ssize_t size = 0;
const char* data = PyUnicode_AsUTF8AndSize(obj, &size);
TORCH_CHECK_PYTHON(data != nullptr);
return std::string(data, static_cast<size_t>(size));
}
return THPUtils_unpackString(obj);
});
}
PyObject* string_to_pyobject(std::string_view str) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_string_to_pyobject requires the GIL to be held");
TORCH_CHECK(
str.size() <= static_cast<size_t>(PY_SSIZE_T_MAX),
"torch_string_to_pyobject: string is too long");
return with_python_error_message([&] {
PyObject* py = THPUtils_packString(str);
TORCH_CHECK_PYTHON(py != nullptr);
return py;
});
}
bool is_none_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_is_none_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
return obj == Py_None;
}
PyObject* new_none_pyobject() const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_new_none_pyobject requires the GIL to be held");
Py_INCREF(Py_None);
return Py_None;
}
void decref_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_decref_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
Py_DECREF(obj);
}
static Py_ssize_t sequence_size(PyObject* obj, const char* shim) {
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
if (PyList_Check(obj)) {
return PyList_GET_SIZE(obj);
}
TORCH_CHECK(
PyTuple_Check(obj),
shim,
": expected list or tuple, got ",
Py_TYPE(obj)->tp_name);
return PyTuple_GET_SIZE(obj);
}
size_t sequence_size_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_sequence_size_pyobject requires the GIL to be held");
return static_cast<size_t>(
sequence_size(obj, "torch_sequence_size_pyobject"));
}
PyObject* sequence_get_item_pyobject(PyObject* obj, size_t index)
const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_sequence_get_item_pyobject requires the GIL to be held");
const auto size = static_cast<size_t>(
sequence_size(obj, "torch_sequence_get_item_pyobject"));
TORCH_CHECK(
index < size,
"torch_sequence_get_item_pyobject: index ",
index,
" is out of range for a sequence of size ",
size);
const auto i = static_cast<Py_ssize_t>(index);
if (PyList_Check(obj)) {
return with_python_error_message([&] {
PyObject* item = PyList_GetItemRef(obj, i);
TORCH_CHECK_PYTHON(item != nullptr);
return item;
});
}
PyObject* item = PyTuple_GET_ITEM(obj, i);
Py_INCREF(item);
return item;
}
PyObject* new_list_pyobject(PyObject* const* items, size_t length)
const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_new_list_pyobject requires the GIL to be held");
TORCH_CHECK(
length <= static_cast<size_t>(PY_SSIZE_T_MAX),
"torch_new_list_pyobject: too many items");
for (size_t i = 0; i < length; ++i) {
TORCH_CHECK(items[i] != nullptr, "torch_new_list_pyobject: null item");
}
THPObjectPtr list(with_python_error_message([&] {
PyObject* py = PyList_New(static_cast<Py_ssize_t>(length));
TORCH_CHECK_PYTHON(py != nullptr);
return py;
}));
for (size_t i = 0; i < length; ++i) {
Py_INCREF(items[i]);
PyList_SET_ITEM(list.get(), static_cast<Py_ssize_t>(i), items[i]);
}
return list.release();
}
at::Generator generator_from_pyobject(PyObject* obj) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_generator_from_pyobject requires the GIL to be held");
TORCH_CHECK(obj != nullptr, "py_obj must not be null");
TORCH_CHECK(
THPGeneratorClass &&
PyObject_TypeCheck(
obj, reinterpret_cast<PyTypeObject*>(THPGeneratorClass)),
"torch_generator_from_pyobject: expected torch.Generator, got ",
Py_TYPE(obj)->tp_name);
return reinterpret_cast<THPGenerator*>(obj)->cdata;
}
PyObject* generator_to_pyobject(
const at::Generator& generator) const override {
TORCH_CHECK(
PyGILState_Check(),
"torch_generator_to_pyobject requires the GIL to be held");
TORCH_CHECK(
generator.defined(),
"torch_generator_to_pyobject: undefined generator");
return with_python_error_message([&] {
PyObject* py = THPGenerator_Wrap(generator);
TORCH_CHECK_PYTHON(py != nullptr);
return py;
});
}
};
struct RegisterPyObjectConversion {
ConcretePyObjectConversion impl;
RegisterPyObjectConversion() {
setPyObjectConversionImpl(&impl);
}
RegisterPyObjectConversion(const RegisterPyObjectConversion&) = delete;
RegisterPyObjectConversion& operator=(const RegisterPyObjectConversion&) =
delete;
RegisterPyObjectConversion(RegisterPyObjectConversion&&) = delete;
RegisterPyObjectConversion& operator=(RegisterPyObjectConversion&&) = delete;
~RegisterPyObjectConversion() {
setPyObjectConversionImpl(nullptr);
}
};
const RegisterPyObjectConversion register_py_object_conversion;
}
}