|
| 1 | +#include <Python.h> |
| 2 | + |
| 3 | +#include <torch/csrc/stable/ops.h> |
| 4 | +#include <torch/csrc/stable/pyobject.h> |
| 5 | +#include <torch/csrc/stable/tensor.h> |
| 6 | + |
| 7 | +// Importable (abi3) module functions that exercise the use case for the |
| 8 | +// PyObject<->Tensor stable shims: a raw PyObject arrives straight from Python |
| 9 | +// (no dispatcher boxing), so the GIL is naturally held. This is how a consumer |
| 10 | +// (e.g. a python-registered custom op) would actually call tensor_from_pyobject |
| 11 | +// / tensor_to_pyobject. The module links only libtorch; the conversion is |
| 12 | +// serviced by |
| 13 | +// the vtable that libtorch_python registers. |
| 14 | + |
| 15 | +using torch::stable::Tensor; |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +// PyObject -> stable Tensor -> PyObject; the result shares storage with input. |
| 20 | +PyObject* pyobject_roundtrip(PyObject* /*self*/, PyObject* obj) { |
| 21 | + try { |
| 22 | + Tensor t = torch::stable::tensor_from_pyobject(obj); |
| 23 | + return static_cast<PyObject*>(torch::stable::tensor_to_pyobject(t)); |
| 24 | + } catch (const std::exception& e) { |
| 25 | + if (!PyErr_Occurred()) { |
| 26 | + PyErr_SetString(PyExc_RuntimeError, e.what()); |
| 27 | + } |
| 28 | + return nullptr; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// args = (tensor, py_type); forces the result's exact Python type via |
| 33 | +// tensor_to_pyobject's py_type argument (e.g. torch.nn.Parameter). |
| 34 | +PyObject* pyobject_to_type(PyObject* /*self*/, PyObject* args) { |
| 35 | + PyObject* obj = nullptr; |
| 36 | + PyObject* py_type = nullptr; |
| 37 | + if (!PyArg_ParseTuple(args, "OO", &obj, &py_type)) { |
| 38 | + return nullptr; |
| 39 | + } |
| 40 | + try { |
| 41 | + // Clone so the result wraps a fresh TensorImpl: tensor_to_pyobject with an explicit |
| 42 | + // py_type fails if the TensorImpl already has a Python object of a different |
| 43 | + // (non-subclass) type, which is the case for `obj` itself. |
| 44 | + Tensor t = torch::stable::clone(torch::stable::tensor_from_pyobject(obj)); |
| 45 | + return static_cast<PyObject*>(torch::stable::tensor_to_pyobject(t, py_type)); |
| 46 | + } catch (const std::exception& e) { |
| 47 | + if (!PyErr_Occurred()) { |
| 48 | + PyErr_SetString(PyExc_RuntimeError, e.what()); |
| 49 | + } |
| 50 | + return nullptr; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Real work through the stable ABI on a tensor obtained from tensor_from_pyobject. |
| 55 | +PyObject* pyobject_sum(PyObject* /*self*/, PyObject* obj) { |
| 56 | + try { |
| 57 | + Tensor t = torch::stable::tensor_from_pyobject(obj); |
| 58 | + return static_cast<PyObject*>( |
| 59 | + torch::stable::tensor_to_pyobject(torch::stable::sum(t))); |
| 60 | + } catch (const std::exception& e) { |
| 61 | + if (!PyErr_Occurred()) { |
| 62 | + PyErr_SetString(PyExc_RuntimeError, e.what()); |
| 63 | + } |
| 64 | + return nullptr; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +PyMethodDef methods[] = { |
| 69 | + {"pyobject_roundtrip", pyobject_roundtrip, METH_O, nullptr}, |
| 70 | + {"pyobject_to_type", pyobject_to_type, METH_VARARGS, nullptr}, |
| 71 | + {"pyobject_sum", pyobject_sum, METH_O, nullptr}, |
| 72 | + {nullptr, nullptr, 0, nullptr}}; |
| 73 | + |
| 74 | +PyModuleDef moduledef = { |
| 75 | + PyModuleDef_HEAD_INIT, |
| 76 | + "_interop", |
| 77 | + nullptr, |
| 78 | + -1, |
| 79 | + methods, |
| 80 | + nullptr, |
| 81 | + nullptr, |
| 82 | + nullptr, |
| 83 | + nullptr}; |
| 84 | + |
| 85 | +} // namespace |
| 86 | + |
| 87 | +// PyMODINIT_FUNC (not a bare extern "C") so the init symbol is dllexported on |
| 88 | +// Windows; without it the .pyd omits PyInit__interop and the import fails. |
| 89 | +PyMODINIT_FUNC PyInit__interop(void) { |
| 90 | + return PyModule_Create(&moduledef); |
| 91 | +} |
0 commit comments