6 #include <torch/csrc/python_headers.h> 9 #include <torch/csrc/Dtype.h> 10 #include <torch/csrc/Layout.h> 11 #include <torch/csrc/autograd/python_variable.h> 12 #include <torch/csrc/autograd/variable.h> 13 #include <torch/csrc/utils/python_numbers.h> 15 namespace torch {
namespace autograd {
namespace utils {
17 inline PyObject* wrap(
bool value) {
25 inline PyObject* wrap(int64_t value) {
26 return THPUtils_packInt64(value);
29 inline PyObject* wrap(
double value) {
30 return PyFloat_FromDouble(value);
33 inline PyObject* wrap(std::complex<double> value) {
36 return PyComplex_FromDoubles(value.real(), value.imag());
39 inline PyObject* wrap(
void* value) {
40 return THPUtils_packInt64(reinterpret_cast<intptr_t>(value));
43 inline PyObject* wrap(
THPDtype *dtype) {
45 return (PyObject*)dtype;
48 inline PyObject* wrap(
THPLayout *layout) {
50 return (PyObject*)layout;
54 return THPVariable_Wrap(Variable(std::move(tensor)));
58 return wrap(scalar_to_tensor(scalar));
61 inline PyObject* wrap(std::tuple<at::Tensor, at::Tensor> tensors) {
64 PyTuple_SET_ITEM(r.get(), 0, wrap(std::get<0>(tensors)));
65 PyTuple_SET_ITEM(r.get(), 1, wrap(std::get<1>(tensors)));
69 inline PyObject* wrap(PyTypeObject *type, std::tuple<at::Tensor, at::Tensor> tensors) {
72 PyStructSequence_SET_ITEM(r.get(), 0, wrap(std::get<0>(tensors)));
73 PyStructSequence_SET_ITEM(r.get(), 1, wrap(std::get<1>(tensors)));
77 inline PyObject* wrap(std::tuple<at::Tensor, at::Tensor, at::Tensor> tensors) {
80 PyTuple_SET_ITEM(r.get(), 0, wrap(std::move(std::get<0>(tensors))));
81 PyTuple_SET_ITEM(r.get(), 1, wrap(std::move(std::get<1>(tensors))));
82 PyTuple_SET_ITEM(r.get(), 2, wrap(std::move(std::get<2>(tensors))));
86 inline PyObject* wrap(PyTypeObject *type, std::tuple<at::Tensor, at::Tensor, at::Tensor> tensors) {
89 PyStructSequence_SET_ITEM(r.get(), 0, wrap(std::get<0>(tensors)));
90 PyStructSequence_SET_ITEM(r.get(), 1, wrap(std::get<1>(tensors)));
91 PyStructSequence_SET_ITEM(r.get(), 2, wrap(std::get<2>(tensors)));
95 inline PyObject* wrap(std::tuple<at::Tensor, at::Tensor, at::Tensor, int64_t> tensors) {
98 PyTuple_SET_ITEM(r.get(), 0, wrap(std::move(std::get<0>(tensors))));
99 PyTuple_SET_ITEM(r.get(), 1, wrap(std::move(std::get<1>(tensors))));
100 PyTuple_SET_ITEM(r.get(), 2, wrap(std::move(std::get<2>(tensors))));
101 PyTuple_SET_ITEM(r.get(), 3, wrap(std::get<3>(tensors)));
105 inline PyObject* wrap(std::tuple<at::Tensor, at::Tensor, float, int64_t> tensors) {
108 PyTuple_SET_ITEM(r.get(), 0, wrap(std::move(std::get<0>(tensors))));
109 PyTuple_SET_ITEM(r.get(), 1, wrap(std::move(std::get<1>(tensors))));
110 PyTuple_SET_ITEM(r.get(), 2, wrap(std::move(std::get<2>(tensors))));
111 PyTuple_SET_ITEM(r.get(), 3, wrap(std::move(std::get<3>(tensors))));
115 inline PyObject* wrap(std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor> tensors) {
118 PyTuple_SET_ITEM(r.get(), 0, wrap(std::move(std::get<0>(tensors))));
119 PyTuple_SET_ITEM(r.get(), 1, wrap(std::move(std::get<1>(tensors))));
120 PyTuple_SET_ITEM(r.get(), 2, wrap(std::move(std::get<2>(tensors))));
121 PyTuple_SET_ITEM(r.get(), 3, wrap(std::move(std::get<3>(tensors))));
125 inline PyObject* wrap(std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor, at::Tensor> tensors) {
128 PyTuple_SET_ITEM(r.get(), 0, wrap(std::move(std::get<0>(tensors))));
129 PyTuple_SET_ITEM(r.get(), 1, wrap(std::move(std::get<1>(tensors))));
130 PyTuple_SET_ITEM(r.get(), 2, wrap(std::move(std::get<2>(tensors))));
131 PyTuple_SET_ITEM(r.get(), 3, wrap(std::move(std::get<3>(tensors))));
132 PyTuple_SET_ITEM(r.get(), 4, wrap(std::move(std::get<4>(tensors))));
139 for (
size_t i = 0; i < tl.
size(); ++i) {
140 PyTuple_SET_ITEM(r.get(), i, wrap(tl[i]));
Scalar represents a 0-dimensional tensor which contains a single element.
constexpr size_t size() const
size - Get the array size.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...