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 copyfrom(self, source_array): """Perform an synchronize copy from the array. Parameters ---------- source_array : array_like The data source we should like to copy from. Returns ------- arr : NDArray Reference to self. """ if isinstance(source_array, NDArrayBase): source_array.copyto(self) return self if not isinstance(source_array, np.ndarray): try: source_array = np.array(source_array, dtype=self.dtype) except: raise TypeError( "array must be an array_like data," + "type %s is not supported" % str(type(source_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) if source_array.shape != shape: raise ValueError( "array shape do not match the shape of NDArray {0} vs {1}".format( source_array.shape, shape ) ) numpy_str_map = DataType.NUMPY2STR np_dtype_str = ( numpy_str_map[source_array.dtype] if source_array.dtype in numpy_str_map else str(source_array.dtype) ) if (not source_array.flags["C_CONTIGUOUS"]) or ( dtype == "bfloat16" or dtype != np_dtype_str ): source_array = np.ascontiguousarray( source_array, dtype="uint16" if dtype == "bfloat16" else dtype ) assert source_array.flags["C_CONTIGUOUS"] data = source_array.ctypes.data_as(ctypes.c_void_p) nbytes = ctypes.c_size_t(source_array.size * source_array.dtype.itemsize) check_call(_LIB.TVMArrayCopyFromBytes(self.handle, data, nbytes)) return self
def empty(shape, dtype="float32", device=device(1, 0), mem_scope=None): """Create an empty array given shape and device Parameters ---------- shape : Union[tvm.runtime.ShapeTuple, Sequence[typing.SupportsInt]] The shape of the array. dtype : type or str The data type of the array. device : Device The device of the array. mem_scope : Optional[str] The memory scope of the array. Returns ------- arr : tvm.nd.NDArray The array tvm supported. """ if not isinstance(shape, tvm.runtime.ShapeTuple): shape = tvm.runtime.ShapeTuple([int(dim) for dim in shape]) dtype = DataType(dtype) arr = _ffi_api.TVMArrayAllocWithScope(shape, dtype, device, mem_scope) return arr
def empty(shape, dtype="float32", ctx=context(1, 0)): """Create an empty array given shape and device Parameters ---------- shape : tuple of int The shape of the array dtype : type or str The data type of the array. ctx : TVMContext The context of the array Returns ------- arr : tvm.nd.NDArray The array tvm supported. """ shape = c_array(tvm_shape_index_t, shape) ndim = ctypes.c_int(len(shape)) handle = TVMArrayHandle() dtype = DataType(dtype) check_call( _LIB.TVMArrayAlloc(shape, ndim, ctypes.c_int(dtype.type_code), ctypes.c_int(dtype.bits), ctypes.c_int(dtype.lanes), ctx.device_type, ctx.device_id, ctypes.byref(handle))) return _make_array(handle, False, False)
def empty(shape, dtype="float32", device=device(1, 0), mem_scope=None): """Create an empty array given shape and device Parameters ---------- shape : tuple of int The shape of the array. dtype : type or str The data type of the array. device : Device The device of the array. mem_scope : Optional[str] The memory scope of the array. Returns ------- arr : tvm.nd.NDArray The array tvm supported. """ shape_imm = [] for s in shape: if isinstance(s, tvm.tir.IntImm): shape_imm.append(s.value) else: shape_imm.append(int(s)) arr = np.array(shape_imm, "int64") ptr = arr.ctypes.data_as(ctypes.POINTER(ctypes.c_int64)) shape_ptr = ctypes.cast(ptr, ctypes.c_void_p) ndim = len(shape_imm) dtype = DataType(dtype) arr = _ffi_api.TVMArrayAllocWithScope(shape_ptr, ndim, dtype, device, mem_scope) return arr
def copyfrom(self, source_array): """Peform an synchronize copy from the array. Parameters ---------- source_array : array_like The data source we should like to copy from. Returns ------- arr : NDArray Reference to self. """ if isinstance(source_array, NDArrayBase): source_array.copyto(self) return self if not isinstance(source_array, np.ndarray): try: source_array = np.array(source_array, dtype=self.dtype) except: raise TypeError('array must be an array_like data,' + 'type %s is not supported' % str(type(source_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) if source_array.shape != shape: raise ValueError( "array shape do not match the shape of NDArray {0} vs {1}". format(source_array.shape, shape)) source_array = np.ascontiguousarray(source_array, dtype=dtype) assert source_array.flags['C_CONTIGUOUS'] data = source_array.ctypes.data_as(ctypes.c_void_p) nbytes = ctypes.c_size_t(source_array.size * source_array.dtype.itemsize) check_call(_LIB.TVMArrayCopyFromBytes(self.handle, data, nbytes)) return self
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
def numpyasarray(np_data): """Return a TVMArray representation of a numpy array.""" data = np_data assert data.flags["C_CONTIGUOUS"] arr = TVMArray() shape = c_array(tvm_shape_index_t, data.shape) arr.data = data.ctypes.data_as(ctypes.c_void_p) arr.shape = shape arr.strides = None arr.dtype = DataType(np.dtype(data.dtype).name) arr.ndim = data.ndim # CPU device arr.device = device(1, 0) return arr, shape