#pragma once
#include <ATen/core/Generator.h>
#include <ATen/core/Tensor.h>
#include <c10/core/Device.h>
#include <c10/core/Layout.h>
#include <c10/core/MemoryFormat.h>
#include <c10/core/ScalarType.h>
#include <c10/util/python_stub.h>
#include <torch/csrc/Export.h>
#include <string>
#include <string_view>
// Indirection that lets the libtorch-only Python-interop stable shims call into
// code that only libtorch_python can provide (THPVariable_* &co) without
// libtorch (or the user extension) linking libtorch_python. Conversions
// between Python objects and libtorch types that need libtorch_python are
// added here as methods.
//
// This mirrors c10's PyInterpreterVTable: an abstract interface declared in the
// lower library, a no-op default that errors, and a concrete implementation
// registered by libtorch_python at load time. Unlike PyInterpreterVTable this
// is a single process-global (there is no tagged tensor to route through when
// converting a raw PyObject*), so it assumes a single Python interpreter.
//
// How the pieces fit together:
// 1. torch/csrc/PyObjectConversion.h (this file) - the vtable interface; in
// libtorch.
// 2. torch/csrc/PyObjectConversion.cpp - a NoopPyObjectConversion that errors
// by default; in libtorch. This fallback fires when libtorch_python was
// never loaded to register a real implementation.
// 3. torch/csrc/PyObjectConversionPythonImpl.cpp - the concrete impl using
// THPVariable_*; in libtorch_python, registers itself at load.
// 4. torch/csrc/shim_common.cpp - the stable C shims that call through the
// vtable; in libtorch.
namespace torch::detail {
struct TORCH_API PyObjectConversionInterface {
virtual ~PyObjectConversionInterface() = default;
// Whether obj is a Python torch.Tensor (or a subclass). A probe for callers
// that want to type-check before tensor_from_pyobject (which errors on
// non-tensors). The GIL must be held.
virtual bool is_tensor_pyobject(PyObject* obj) const = 0;
// Unpack a Python torch.Tensor (PyObject*) into an at::Tensor that shares the
// underlying TensorImpl. The GIL must be held.
virtual at::Tensor tensor_from_pyobject(PyObject* obj) const = 0;
// Wrap an at::Tensor as a new-reference Python torch.Tensor. py_type, if
// non-null, is the result's exact PyTypeObject* (e.g. torch.nn.Parameter);
// null means the default torch.Tensor type. The GIL must be held.
virtual PyObject* tensor_to_pyobject(const at::Tensor& t, PyObject* py_type)
const = 0;
// Read the ScalarType out of a Python torch.dtype (PyObject*). The GIL must
// be held.
virtual at::ScalarType dtype_from_pyobject(PyObject* obj) const = 0;
// Return a new-reference Python torch.dtype for the given ScalarType. The
// GIL must be held.
virtual PyObject* dtype_to_pyobject(at::ScalarType dtype) const = 0;
// Read the at::Device out of a Python torch.device (PyObject*). The GIL must
// be held.
virtual at::Device device_from_pyobject(PyObject* obj) const = 0;
// Return a new-reference Python torch.device for the given at::Device. The
// GIL must be held.
virtual PyObject* device_to_pyobject(const at::Device& device) const = 0;
// Read the at::Layout out of a Python torch.layout (PyObject*). The GIL must
// be held.
virtual at::Layout layout_from_pyobject(PyObject* obj) const = 0;
// Return a new-reference Python torch.layout for the given at::Layout. The
// GIL must be held.
virtual PyObject* layout_to_pyobject(at::Layout layout) const = 0;
// Read the at::MemoryFormat out of a Python torch.memory_format (PyObject*).
// The GIL must be held.
virtual at::MemoryFormat memory_format_from_pyobject(PyObject* obj) const = 0;
// Return a new-reference Python torch.memory_format for the given
// at::MemoryFormat. The GIL must be held.
virtual PyObject* memory_format_to_pyobject(
at::MemoryFormat memory_format) const = 0;
// Read a Python bool (True or False only). The GIL must be held.
virtual bool bool_from_pyobject(PyObject* obj) const = 0;
// Return a new-reference Python bool. The GIL must be held.
virtual PyObject* bool_to_pyobject(bool value) const = 0;
// Read a Python int (not bool) as int64_t, erroring if it is out of range.
// The GIL must be held.
virtual int64_t int64_from_pyobject(PyObject* obj) const = 0;
// Return a new-reference Python int. The GIL must be held.
virtual PyObject* int64_to_pyobject(int64_t value) const = 0;
// Read a Python float or int as double. The GIL must be held.
virtual double double_from_pyobject(PyObject* obj) const = 0;
// Return a new-reference Python float. The GIL must be held.
virtual PyObject* double_to_pyobject(double value) const = 0;
// Copy a Python str (as UTF-8) or bytes (as is) into a std::string. The GIL
// must be held.
virtual std::string string_from_pyobject(PyObject* obj) const = 0;
// Decode UTF-8 bytes into a new-reference Python str. The GIL must be held.
virtual PyObject* string_to_pyobject(std::string_view str) const = 0;
// Whether obj is Python None. The GIL must be held.
virtual bool is_none_pyobject(PyObject* obj) const = 0;
// Return a new reference to Python None. The GIL must be held.
virtual PyObject* new_none_pyobject() const = 0;
// Release one reference to obj. The GIL must be held.
virtual void decref_pyobject(PyObject* obj) const = 0;
// Read the length of a Python list or tuple. The GIL must be held.
virtual size_t sequence_size_pyobject(PyObject* obj) const = 0;
// Return a new reference to the item at index of a Python list or tuple,
// erroring if index is out of range. The GIL must be held.
virtual PyObject* sequence_get_item_pyobject(PyObject* obj, size_t index)
const = 0;
// Return a new-reference Python list of items[0, length). The items are
// borrowed; the list takes its own references. The GIL must be held.
virtual PyObject* new_list_pyobject(PyObject* const* items, size_t length)
const = 0;
// Read the at::Generator out of a Python torch.Generator (or a subclass); the
// result shares its GeneratorImpl. The GIL must be held.
virtual at::Generator generator_from_pyobject(PyObject* obj) const = 0;
// Return a new-reference Python torch.Generator sharing the GeneratorImpl of
// the given defined at::Generator. The GIL must be held.
virtual PyObject* generator_to_pyobject(
const at::Generator& generator) const = 0;
};
// Install the conversion implementation. Called once by libtorch_python when it
// is loaded. Passing nullptr restores the default no-op implementation.
TORCH_API void setPyObjectConversionImpl(
const PyObjectConversionInterface* impl);
// Return the registered implementation. Before libtorch_python registers one
// this is a no-op that raises a clear error when its methods are called.
TORCH_API const PyObjectConversionInterface& getPyObjectConversionImpl();
} // namespace torch::detail