| 93 | } |
| 94 | |
| 95 | void 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 | |
| 116 | void MakePyInputs(const UserOpDef& op_def, user_op::KernelComputeContext* ctx, |
| 117 | PyObject** py_inputs) { |
no test coverage detected