| 76 | |
| 77 | template<typename T> |
| 78 | inline static Maybe<PyObject*> EagerLocalTensorToNumpy(PyObject* py_tensor) { |
| 79 | const auto& t = PyTensor_Unpack(py_tensor); |
| 80 | |
| 81 | std::shared_ptr<LocalTensor> tensor = JUST(t->AsLocalTensor()); |
| 82 | CHECK_OR_RETURN(JUST(tensor->device()) == JUST(Device::New("cpu"))); |
| 83 | CHECK_OR_RETURN(tensor->is_eager()) << "eager tensors supported only."; |
| 84 | // set base object attr |
| 85 | py::handle handle = py::handle(py_tensor); |
| 86 | |
| 87 | const size_t ndim = tensor->ndim(); |
| 88 | const auto shape = numpy::OFShapeToNumpyShape(tensor->shape()->dim_vec()); |
| 89 | // NumPy strides use bytes. OneFlow strides use element counts. |
| 90 | const auto stride = |
| 91 | numpy::OFStrideToNumpyStride(*JUST(tensor->stride()), tensor->dtype()->data_type()); |
| 92 | |
| 93 | void* data_ptr = JUST(GetTensorDataPtr(tensor)); |
| 94 | |
| 95 | return py::array(py::buffer_info(data_ptr, sizeof(T), py::format_descriptor<T>::format(), ndim, |
| 96 | shape, stride), |
| 97 | handle) |
| 98 | .release() |
| 99 | .ptr(); |
| 100 | } |
| 101 | |
| 102 | template<typename T> |
| 103 | struct TensorTypeToPyType final { |
nothing calls this directly
no test coverage detected