forked from Tensor-Array/Tensor-Array-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor_bind.cc
More file actions
292 lines (267 loc) · 7.52 KB
/
tensor_bind.cc
File metadata and controls
292 lines (267 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <tensor-array/core/tensor.hh>
#include <tensor-array/core/data_type_wrapper.hh>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
using namespace tensor_array::value;
using namespace tensor_array::datatype;
using namespace tensor_array::wrapper;
template <typename T>
TensorBase convert_numpy_to_tensor_base(pybind11::array_t<T> py_buf)
{
pybind11::buffer_info info = py_buf.request();
std::vector<unsigned int> shape_vec(info.ndim);
std::transform
(
info.shape.cbegin(),
info.shape.cend(),
shape_vec.begin(),
[](pybind11::size_t dim)
{
return static_cast<unsigned int>(dim);
}
);
return TensorBase(warp_type(warp_type(typeid(T))), shape_vec, info.ptr);
}
pybind11::dtype get_py_type(const std::type_info& info)
{
if (info == typeid(std::int8_t))
return pybind11::dtype::of<std::int8_t>();
if (info == typeid(std::int16_t))
return pybind11::dtype::of<std::int16_t>();
if (info == typeid(std::int32_t))
return pybind11::dtype::of<std::int32_t>();
if (info == typeid(std::int64_t))
return pybind11::dtype::of<std::int64_t>();
if (info == typeid(std::uint8_t))
return pybind11::dtype::of<std::uint8_t>();
if (info == typeid(std::uint16_t))
return pybind11::dtype::of<std::uint16_t>();
if (info == typeid(std::uint32_t))
return pybind11::dtype::of<std::uint32_t>();
if (info == typeid(std::uint64_t))
return pybind11::dtype::of<std::uint64_t>();
if (info == typeid(bool))
return pybind11::dtype::of<bool>();
if (info == typeid(float))
return pybind11::dtype::of<float>();
if (info == typeid(double))
return pybind11::dtype::of<double>();
throw std::runtime_error("no dtype");
}
pybind11::array convert_tensor_to_numpy(const Tensor& self)
{
const TensorBase& base_tensor = self.get_buffer().change_device({tensor_array::devices::CPU, 0});
std::vector<pybind11::size_t> shape_vec(base_tensor.shape().size());
std::transform
(
base_tensor.shape().begin(),
base_tensor.shape().end(),
shape_vec.begin(),
[](unsigned int dim)
{
return static_cast<pybind11::size_t>(dim);
}
);
auto ty0 = pybind11::detail::get_type_info(base_tensor.type());
pybind11::dtype ty1 = get_py_type(base_tensor.type());
return pybind11::array(ty1, shape_vec, base_tensor.data());
}
Tensor python_tuple_slice(const Tensor& self, pybind11::tuple tuple_slice)
{
std::vector<Tensor::Slice> t_slices;
for (size_t i = 0; i < tuple_slice.size(); i++)
{
ssize_t start, stop, step;
ssize_t length;
pybind11::slice py_slice = tuple_slice[i].cast<pybind11::slice>();
if (!py_slice.compute(self.get_buffer().shape().begin()[i], &start, &stop, &step, &length))
throw std::runtime_error("Invalid slice");
t_slices.insert
(
t_slices.begin() + i,
Tensor::Slice
{
static_cast<int>(start),
static_cast<int>(stop),
static_cast<int>(step)
}
);
}
return self[initializer_wrapper<Tensor::Slice>(t_slices.begin().operator->(), t_slices.end().operator->())];
}
Tensor python_slice(const Tensor& self, pybind11::slice py_slice)
{
std::vector<Tensor::Slice> t_slices;
ssize_t start, stop, step;
ssize_t length;
if (!py_slice.compute(self.get_buffer().shape().begin()[0], &start, &stop, &step, &length))
throw std::runtime_error("Invalid slice");
return self
[
{
Tensor::Slice
{
static_cast<int>(start),
static_cast<int>(stop),
static_cast<int>(step)
}
}
];
}
Tensor python_index(const Tensor& self, unsigned int i)
{
return self[i];
}
std::size_t python_len(const Tensor& self)
{
std::initializer_list<unsigned int> shape_list = self.get_buffer().shape();
return shape_list.size() != 0 ? shape_list.begin()[0]: 1U;
}
pybind11::str tensor_to_string(const Tensor& self)
{
return pybind11::repr(convert_tensor_to_numpy(self));
}
Tensor tensor_cast_1(const Tensor& self, DataType dtype)
{
return self.tensor_cast(warp_type(dtype));
}
pybind11::tuple tensor_shape(const Tensor& self)
{
return pybind11::cast(std::vector<unsigned int>(self.get_buffer().shape()));
}
DataType tensor_type(const Tensor& self)
{
return warp_type(self.get_buffer().type());
}
Tensor tensor_copying(const Tensor& self)
{
return Tensor(self);
}
Tensor py_zeros(pybind11::tuple shape_tuple, DataType dtype)
{
std::vector<unsigned int> shape_vec;
for (auto& it: shape_tuple)
shape_vec.push_back(it.cast<unsigned int>());
return TensorBase(warp_type(dtype), shape_vec);
}
Tensor py_rand(pybind11::tuple shape_tuple, unsigned int seed = std::rand())
{
std::vector<unsigned int> shape_vec;
for (auto& it: shape_tuple)
shape_vec.push_back(it.cast<unsigned int>());
return tensor_rand(shape_vec, seed);
}
PYBIND11_MODULE(tensor2, m)
{
pybind11::enum_<DataType>(m, "DataType")
.value("BOOL", BOOL_DTYPE)
.value("S_INT_8", S_INT_8)
.value("S_INT_16", S_INT_16)
.value("S_INT_32", S_INT_32)
.value("S_INT_64", S_INT_64)
.value("FLOAT", FLOAT_DTYPE)
.value("DOUBLE", DOUBLE_DTYPE)
.value("HALF", HALF_DTYPE)
.value("BFLOAT16", BF16_DTYPE)
.value("U_INT_8", U_INT_8)
.value("U_INT_16", U_INT_16)
.value("U_INT_32", U_INT_32)
.value("U_INT_64", U_INT_64)
.export_values();
m.def
(
"zeros",
&py_zeros,
pybind11::arg("shape"),
pybind11::arg("dtype") = S_INT_32
);
m.def(
"rand",
&py_rand,
pybind11::arg("shape"),
pybind11::arg("seed") = std::rand()
);
m.def(
"add",
&tensor_array::value::add,
pybind11::arg("value_1"),
pybind11::arg("value_2")
);
m.def(
"multiply",
&tensor_array::value::multiply,
pybind11::arg("value_1"),
pybind11::arg("value_2")
);
m.def(
"divide",
&tensor_array::value::divide,
pybind11::arg("value_1"),
pybind11::arg("value_2")
);
m.def(
"power",
&tensor_array::value::power,
pybind11::arg("value_1"),
pybind11::arg("value_2")
);
m.def(
"matmul",
&tensor_array::value::matmul,
pybind11::arg("value_1"),
pybind11::arg("value_2")
);
m.def(
"condition",
&tensor_array::value::condition,
pybind11::arg("condition_value"),
pybind11::arg("value_if_true"),
pybind11::arg("value_if_false")
);
pybind11::class_<Tensor>(m, "Tensor")
.def(pybind11::init())
.def(pybind11::init(&tensor_copying))
.def(pybind11::init(&convert_numpy_to_tensor_base<int>))
.def(pybind11::init(&convert_numpy_to_tensor_base<float>))
.def(pybind11::self + pybind11::self)
.def(pybind11::self - pybind11::self)
.def(pybind11::self * pybind11::self)
.def(pybind11::self / pybind11::self)
.def(pybind11::self += pybind11::self)
.def(pybind11::self -= pybind11::self)
.def(pybind11::self *= pybind11::self)
.def(pybind11::self /= pybind11::self)
.def(pybind11::self == pybind11::self)
.def(pybind11::self != pybind11::self)
.def(pybind11::self >= pybind11::self)
.def(pybind11::self <= pybind11::self)
.def(pybind11::self > pybind11::self)
.def(pybind11::self < pybind11::self)
.def(+pybind11::self)
.def(-pybind11::self)
.def(hash(pybind11::self))
.def("transpose", &Tensor::transpose)
.def("calc_grad", &Tensor::calc_grad)
.def("get_grad", &Tensor::get_grad)
.def("sin", &Tensor::sin)
.def("cos", &Tensor::cos)
.def("tan", &Tensor::tan)
.def("sinh", &Tensor::sinh)
.def("cosh", &Tensor::cosh)
.def("tanh", &Tensor::tanh)
.def("log", &Tensor::log)
.def("clone", &Tensor::clone)
.def("cast", &tensor_cast_1)
.def("numpy", &convert_tensor_to_numpy)
.def("shape", &tensor_shape)
.def("dtype", &tensor_type)
.def("__getitem__", &python_index)
.def("__getitem__", &python_slice)
.def("__getitem__", &python_tuple_slice)
.def("__len__", &python_len)
.def("__matmul__", &matmul)
.def("__repr__", &tensor_to_string)
.def("__copy__", &tensor_copying);
}