def numpy(self): """Convert this array to numpy array Returns ------- np_arr : numpy.ndarray The corresponding numpy array. """ t = DataType(self.dtype) shape, dtype = self.shape, self.dtype old_dtype = dtype if t.lanes > 1: shape = shape + (t.lanes, ) t.lanes = 1 dtype = str(t) if dtype == "int4": dtype = "int8" if dtype == "bfloat16": dtype = "uint16" np_arr = np.empty(shape, dtype=dtype) assert np_arr.flags["C_CONTIGUOUS"] data = np_arr.ctypes.data_as(ctypes.c_void_p) nbytes = ctypes.c_size_t(np_arr.size * np_arr.dtype.itemsize) check_call(_LIB.TVMArrayCopyToBytes(self.handle, data, nbytes)) if old_dtype == "int4": length = np_arr.size np_arr_ret = np.empty((length, ), dtype="int8") np_arr = np_arr.reshape((length, )) old_index = np.bitwise_and(np_arr, 0x0F) even_index = np.bitwise_and(np_arr >> 4, 0x0F) np_arr_ret[1::2] = old_index[0:length // 2] np_arr_ret[0::2] = even_index[0:length // 2] return np_arr_ret.reshape(shape) return np_arr
def asnumpy(self): """Convert this array to numpy array Returns ------- np_arr : numpy.ndarray The corresponding numpy array. """ t = DataType(self.dtype) shape, dtype = self.shape, self.dtype if t.lanes > 1: shape = shape + (t.lanes, ) t.lanes = 1 dtype = str(t) np_arr = np.empty(shape, dtype=dtype) assert np_arr.flags['C_CONTIGUOUS'] data = np_arr.ctypes.data_as(ctypes.c_void_p) nbytes = ctypes.c_size_t(np_arr.size * np_arr.dtype.itemsize) check_call(_LIB.TVMArrayCopyToBytes(self.handle, data, nbytes)) return np_arr