forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy_convert.cc
More file actions
303 lines (260 loc) · 8.77 KB
/
Copy pathnumpy_convert.cc
File metadata and controls
303 lines (260 loc) · 8.77 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
293
294
295
296
297
298
299
300
301
302
303
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/python/numpy_interop.h"
#include "arrow/python/numpy_convert.h"
#include <cstdint>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include "arrow/buffer.h"
#include "arrow/tensor.h"
#include "arrow/type.h"
#include "arrow/python/common.h"
#include "arrow/python/type_traits.h"
namespace arrow {
namespace py {
bool is_contiguous(PyObject* array) {
if (PyArray_Check(array)) {
return (PyArray_FLAGS(reinterpret_cast<PyArrayObject*>(array)) &
(NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS)) != 0;
} else {
return false;
}
}
int cast_npy_type_compat(int type_num) {
// Both LONGLONG and INT64 can be observed in the wild, which is buggy. We set
// U/LONGLONG to U/INT64 so things work properly.
#if (NPY_INT64 == NPY_LONGLONG) && (NPY_SIZEOF_LONGLONG == 8)
if (type_num == NPY_LONGLONG) {
type_num = NPY_INT64;
}
if (type_num == NPY_ULONGLONG) {
type_num = NPY_UINT64;
}
#endif
return type_num;
}
NumPyBuffer::NumPyBuffer(PyObject* ao) : Buffer(nullptr, 0) {
arr_ = ao;
Py_INCREF(ao);
if (PyArray_Check(ao)) {
PyArrayObject* ndarray = reinterpret_cast<PyArrayObject*>(ao);
data_ = reinterpret_cast<const uint8_t*>(PyArray_DATA(ndarray));
size_ = PyArray_SIZE(ndarray) * PyArray_DESCR(ndarray)->elsize;
capacity_ = size_;
if (PyArray_FLAGS(ndarray) & NPY_ARRAY_WRITEABLE) {
is_mutable_ = true;
}
}
}
NumPyBuffer::~NumPyBuffer() {
PyAcquireGIL lock;
Py_XDECREF(arr_);
}
#define TO_ARROW_TYPE_CASE(NPY_NAME, FACTORY) \
case NPY_##NPY_NAME: \
*out = FACTORY(); \
break;
Status GetTensorType(PyObject* dtype, std::shared_ptr<DataType>* out) {
if (!PyArray_DescrCheck(dtype)) {
return Status::TypeError("Did not pass numpy.dtype object");
}
PyArray_Descr* descr = reinterpret_cast<PyArray_Descr*>(dtype);
int type_num = cast_npy_type_compat(descr->type_num);
switch (type_num) {
TO_ARROW_TYPE_CASE(BOOL, uint8);
TO_ARROW_TYPE_CASE(INT8, int8);
TO_ARROW_TYPE_CASE(INT16, int16);
TO_ARROW_TYPE_CASE(INT32, int32);
TO_ARROW_TYPE_CASE(INT64, int64);
#if (NPY_INT64 != NPY_LONGLONG)
TO_ARROW_TYPE_CASE(LONGLONG, int64);
#endif
TO_ARROW_TYPE_CASE(UINT8, uint8);
TO_ARROW_TYPE_CASE(UINT16, uint16);
TO_ARROW_TYPE_CASE(UINT32, uint32);
TO_ARROW_TYPE_CASE(UINT64, uint64);
#if (NPY_UINT64 != NPY_ULONGLONG)
TO_ARROW_CASE(ULONGLONG);
#endif
TO_ARROW_TYPE_CASE(FLOAT16, float16);
TO_ARROW_TYPE_CASE(FLOAT32, float32);
TO_ARROW_TYPE_CASE(FLOAT64, float64);
default: {
std::stringstream ss;
ss << "Unsupported numpy type " << descr->type_num << std::endl;
return Status::NotImplemented(ss.str());
}
}
return Status::OK();
}
Status GetNumPyType(const DataType& type, int* type_num) {
#define NUMPY_TYPE_CASE(ARROW_NAME, NPY_NAME) \
case Type::ARROW_NAME: \
*type_num = NPY_##NPY_NAME; \
break;
switch (type.id()) {
NUMPY_TYPE_CASE(UINT8, UINT8);
NUMPY_TYPE_CASE(INT8, INT8);
NUMPY_TYPE_CASE(UINT16, UINT16);
NUMPY_TYPE_CASE(INT16, INT16);
NUMPY_TYPE_CASE(UINT32, UINT32);
NUMPY_TYPE_CASE(INT32, INT32);
NUMPY_TYPE_CASE(UINT64, UINT64);
NUMPY_TYPE_CASE(INT64, INT64);
NUMPY_TYPE_CASE(HALF_FLOAT, FLOAT16);
NUMPY_TYPE_CASE(FLOAT, FLOAT32);
NUMPY_TYPE_CASE(DOUBLE, FLOAT64);
default: {
std::stringstream ss;
ss << "Unsupported tensor type: " << type.ToString() << std::endl;
return Status::NotImplemented(ss.str());
}
}
#undef NUMPY_TYPE_CASE
return Status::OK();
}
Status NumPyDtypeToArrow(PyObject* dtype, std::shared_ptr<DataType>* out) {
if (!PyArray_DescrCheck(dtype)) {
return Status::TypeError("Did not pass numpy.dtype object");
}
PyArray_Descr* descr = reinterpret_cast<PyArray_Descr*>(dtype);
return NumPyDtypeToArrow(descr, out);
}
Status NumPyDtypeToArrow(PyArray_Descr* descr, std::shared_ptr<DataType>* out) {
int type_num = cast_npy_type_compat(descr->type_num);
switch (type_num) {
TO_ARROW_TYPE_CASE(BOOL, boolean);
TO_ARROW_TYPE_CASE(INT8, int8);
TO_ARROW_TYPE_CASE(INT16, int16);
TO_ARROW_TYPE_CASE(INT32, int32);
TO_ARROW_TYPE_CASE(INT64, int64);
#if (NPY_INT64 != NPY_LONGLONG)
TO_ARROW_TYPE_CASE(LONGLONG, int64);
#endif
TO_ARROW_TYPE_CASE(UINT8, uint8);
TO_ARROW_TYPE_CASE(UINT16, uint16);
TO_ARROW_TYPE_CASE(UINT32, uint32);
TO_ARROW_TYPE_CASE(UINT64, uint64);
#if (NPY_UINT64 != NPY_ULONGLONG)
TO_ARROW_CASE(ULONGLONG);
#endif
TO_ARROW_TYPE_CASE(FLOAT16, float16);
TO_ARROW_TYPE_CASE(FLOAT32, float32);
TO_ARROW_TYPE_CASE(FLOAT64, float64);
TO_ARROW_TYPE_CASE(STRING, binary);
TO_ARROW_TYPE_CASE(UNICODE, utf8);
case NPY_DATETIME: {
auto date_dtype =
reinterpret_cast<PyArray_DatetimeDTypeMetaData*>(descr->c_metadata);
switch (date_dtype->meta.base) {
case NPY_FR_s:
*out = timestamp(TimeUnit::SECOND);
break;
case NPY_FR_ms:
*out = timestamp(TimeUnit::MILLI);
break;
case NPY_FR_us:
*out = timestamp(TimeUnit::MICRO);
break;
case NPY_FR_ns:
*out = timestamp(TimeUnit::NANO);
break;
case NPY_FR_D:
*out = date32();
break;
default:
return Status::NotImplemented("Unsupported datetime64 time unit");
}
} break;
default: {
std::stringstream ss;
ss << "Unsupported numpy type " << descr->type_num << std::endl;
return Status::NotImplemented(ss.str());
}
}
return Status::OK();
}
#undef TO_ARROW_TYPE_CASE
Status NdarrayToTensor(MemoryPool* pool, PyObject* ao, std::shared_ptr<Tensor>* out) {
PyAcquireGIL lock;
if (!PyArray_Check(ao)) {
return Status::TypeError("Did not pass ndarray object");
}
PyArrayObject* ndarray = reinterpret_cast<PyArrayObject*>(ao);
// TODO(wesm): What do we want to do with non-contiguous memory and negative strides?
int ndim = PyArray_NDIM(ndarray);
std::shared_ptr<Buffer> data = std::make_shared<NumPyBuffer>(ao);
std::vector<int64_t> shape(ndim);
std::vector<int64_t> strides(ndim);
npy_intp* array_strides = PyArray_STRIDES(ndarray);
npy_intp* array_shape = PyArray_SHAPE(ndarray);
for (int i = 0; i < ndim; ++i) {
if (array_strides[i] < 0) {
return Status::Invalid("Negative ndarray strides not supported");
}
shape[i] = array_shape[i];
strides[i] = array_strides[i];
}
std::shared_ptr<DataType> type;
RETURN_NOT_OK(
GetTensorType(reinterpret_cast<PyObject*>(PyArray_DESCR(ndarray)), &type));
*out = std::make_shared<Tensor>(type, data, shape, strides);
return Status::OK();
}
Status TensorToNdarray(const Tensor& tensor, PyObject* base, PyObject** out) {
PyAcquireGIL lock;
int type_num;
RETURN_NOT_OK(GetNumPyType(*tensor.type(), &type_num));
PyArray_Descr* dtype = PyArray_DescrNewFromType(type_num);
RETURN_IF_PYERROR();
std::vector<npy_intp> npy_shape(tensor.ndim());
std::vector<npy_intp> npy_strides(tensor.ndim());
for (int i = 0; i < tensor.ndim(); ++i) {
npy_shape[i] = tensor.shape()[i];
npy_strides[i] = tensor.strides()[i];
}
const void* immutable_data = nullptr;
if (tensor.data()) {
immutable_data = tensor.data()->data();
}
// Remove const =(
void* mutable_data = const_cast<void*>(immutable_data);
int array_flags = 0;
if (tensor.is_row_major()) {
array_flags |= NPY_ARRAY_C_CONTIGUOUS;
}
if (tensor.is_column_major()) {
array_flags |= NPY_ARRAY_F_CONTIGUOUS;
}
if (tensor.is_mutable()) {
array_flags |= NPY_ARRAY_WRITEABLE;
}
PyObject* result =
PyArray_NewFromDescr(&PyArray_Type, dtype, tensor.ndim(), npy_shape.data(),
npy_strides.data(), mutable_data, array_flags, nullptr);
RETURN_IF_PYERROR()
if (base != Py_None) {
PyArray_SetBaseObject(reinterpret_cast<PyArrayObject*>(result), base);
Py_XINCREF(base);
}
*out = result;
return Status::OK();
}
} // namespace py
} // namespace arrow