MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / EagerLocalTensorToNumpy

Function EagerLocalTensorToNumpy

oneflow/api/python/utils/tensor_utils.h:78–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

76
77template<typename T>
78inline 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
102template<typename T>
103struct TensorTypeToPyType final {

Callers

nothing calls this directly

Calls 15

handleClass · 0.85
OFShapeToNumpyShapeFunction · 0.85
OFStrideToNumpyStrideFunction · 0.85
GetTensorDataPtrFunction · 0.85
ndimMethod · 0.80
NewFunction · 0.50
AsLocalTensorMethod · 0.45
deviceMethod · 0.45
is_eagerMethod · 0.45
shapeMethod · 0.45
strideMethod · 0.45
data_typeMethod · 0.45

Tested by

no test coverage detected