MCPcopy Create free account
hub / github.com/apache/arrow / NdarrayToTensor

Function NdarrayToTensor

python/pyarrow/src/arrow/python/numpy_convert.cc:200–231  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

198#undef TO_ARROW_TYPE_CASE
199
200Status NdarrayToTensor(MemoryPool* pool, PyObject* ao,
201 const std::vector<std::string>& dim_names,
202 std::shared_ptr<Tensor>* out) {
203 if (!PyArray_Check(ao)) {
204 return Status::TypeError("Did not pass ndarray object");
205 }
206
207 PyArrayObject* ndarray = reinterpret_cast<PyArrayObject*>(ao);
208
209 // TODO(wesm): What do we want to do with non-contiguous memory and negative strides?
210
211 int ndim = PyArray_NDIM(ndarray);
212
213 std::shared_ptr<Buffer> data = std::make_shared<NumPyBuffer>(ao);
214 std::vector<int64_t> shape(ndim);
215 std::vector<int64_t> strides(ndim);
216
217 npy_intp* array_strides = PyArray_STRIDES(ndarray);
218 npy_intp* array_shape = PyArray_SHAPE(ndarray);
219 for (int i = 0; i < ndim; ++i) {
220 if (array_strides[i] < 0) {
221 return Status::Invalid("Negative ndarray strides not supported");
222 }
223 shape[i] = array_shape[i];
224 strides[i] = array_strides[i];
225 }
226
227 ARROW_ASSIGN_OR_RAISE(
228 auto type, GetTensorType(reinterpret_cast<PyObject*>(PyArray_DESCR(ndarray))));
229 *out = std::make_shared<Tensor>(type, data, shape, strides, dim_names);
230 return Status::OK();
231}
232
233Status TensorToNdarray(const std::shared_ptr<Tensor>& tensor, PyObject* base,
234 PyObject** out) {

Callers 3

Calls 3

TypeErrorFunction · 0.50
InvalidFunction · 0.50
OKFunction · 0.50

Tested by

no test coverage detected