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

Function NumpyToTensor

oneflow/extension/python/py_compute.cpp:95–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

93}
94
95void NumpyToTensor(PyObject* arg, user_op::Tensor* tensor) {
96 PyObject* ro_array = PyArray_FromAny(arg, nullptr, 0, 0, NPY_ARRAY_CARRAY_RO, nullptr);
97 // PyArray_FromAny has increased the reference count
98 Py_DECREF(ro_array);
99 PyArrayObject* array = reinterpret_cast<PyArrayObject*>(ro_array);
100
101 DataType of_data_type = CHECK_JUST(numpy::GetOFDataTypeFromNpArray(array));
102 CHECK_EQ(of_data_type, tensor->data_type())
103 << "Numpy to OneFlow data type " << DataType_Name(of_data_type)
104 << " is not equal to OneFlow tensor data type " << DataType_Name(tensor->data_type());
105
106 int64_t array_elem_cnt = 1;
107 FOR_RANGE(int, i, 0, PyArray_NDIM(array)) { array_elem_cnt *= PyArray_SHAPE(array)[i]; }
108 CHECK_EQ(array_elem_cnt, tensor->shape_view().elem_cnt())
109 << "Numpy array element count " << array_elem_cnt
110 << " is not equal to OneFlow tensor element count " << tensor->shape_view().elem_cnt();
111
112 void* array_data_ptr = PyArray_DATA(array);
113 MemToTensor(array_data_ptr, array_elem_cnt, tensor);
114}
115
116void MakePyInputs(const UserOpDef& op_def, user_op::KernelComputeContext* ctx,
117 PyObject** py_inputs) {

Callers 2

FOR_RANGEFunction · 0.85
GetPyOutputsFunction · 0.85

Calls 5

GetOFDataTypeFromNpArrayFunction · 0.85
MemToTensorFunction · 0.85
data_typeMethod · 0.45
elem_cntMethod · 0.45
shape_viewMethod · 0.45

Tested by

no test coverage detected