tvm.runtime.ndarray

Runtime NDArray API

class tvm.runtime.ndarray.NDArray

Lightweight NDArray class of TVM runtime.

Strictly this is only an Array Container (a buffer object) No arthimetic operations are defined. All operations are performed by TVM functions.

The goal is not to re-build yet another array library. Instead, this is a minimal data structure to demonstrate how can we use TVM in existing project which might have their own array containers.

property dtype

Type of this array

property device

Device of this array

same_as(other)

Check object identity equality

Parameters

other (object) – The other object to compare to

Returns

same – Whether other is same as self.

Return type

bool

copyfrom(source_array)

Perform a synchronous copy from the array.

Parameters

source_array (array_like) – The data source we should like to copy from.

Returns

arr – Reference to self.

Return type

NDArray

asnumpy()

Convert this array to numpy array. This API will be deprecated in TVM v0.8 release. Please use numpy instead.

numpy()

Convert this array to numpy array

Returns

np_arr – The corresponding numpy array.

Return type

numpy.ndarray

copyto(target, mem_scope=None)

Copy array to target

Parameters
  • target (NDArray) – The target array to be copied, must have same shape as this array.

  • mem_scope (Optional[str]) – The memory scope of the array.

tvm.runtime.ndarray.device(dev_type, dev_id=0)

Construct a TVM device with given device type and id.

Parameters
  • dev_type (int or str) – The device type mask or name of the device.

  • dev_id (int, optional) – The integer device id

Returns

dev – The corresponding device.

Return type

tvm.runtime.Device

Examples

Device can be used to create reflection of device by string representation of the device type.

assert tvm.device("cpu", 1) == tvm.cpu(1)
assert tvm.device("cuda", 0) == tvm.cuda(0)
tvm.runtime.ndarray.numpyasarray(np_data)

Return a TVMArray representation of a numpy array.

tvm.runtime.ndarray.empty(shape, dtype='float32', device=cpu(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 – The array tvm supported.

Return type

tvm.nd.NDArray

tvm.runtime.ndarray.from_dlpack(dltensor)

Produces an array from an object with __dlpack__ method or a DLPack tensor w/o memory copy. Retreives the underlying DLPack tensor’s pointer to create an array from the data. Removes the original DLPack tensor’s destructor as now the array is responsible for destruction.

Parameters

dltensor (object with __dlpack__ attribute or a DLPack capsule) –

Returns

arr – The array view of the tensor data.

Return type

tvm.nd.NDArray

tvm.runtime.ndarray.cpu(dev_id=0)

Construct a CPU device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.cuda(dev_id=0)

Construct a CUDA GPU device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.gpu(dev_id=0)

Construct a CUDA GPU device

deprecated:: 0.9.0 Use tvm.cuda() instead.

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.rocm(dev_id=0)

Construct a ROCM device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.opencl(dev_id=0)

Construct a OpenCL device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.metal(dev_id=0)

Construct a metal device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.vpi(dev_id=0)

Construct a VPI simulated device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.vulkan(dev_id=0)

Construct a Vulkan device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.ext_dev(dev_id=0)

Construct a extension device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

Note

This API is reserved for quick testing of new device by plugin device API as ext_dev.

tvm.runtime.ndarray.hexagon(dev_id=0)

Construct a Hexagon device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.webgpu(dev_id=0)

Construct a webgpu device.

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.cl(dev_id=0)

Construct a OpenCL device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.mtl(dev_id=0)

Construct a metal device

Parameters

dev_id (int, optional) – The integer device id

Returns

dev – The created device

Return type

Device

tvm.runtime.ndarray.array(arr, device=cpu(0), mem_scope=None)

Create an array from source arr.

Parameters
  • arr (numpy.ndarray) – The array to be copied from

  • device (Device, optional) – The device to create the array

  • mem_scope (Optional[str]) – The memory scope of the array

Returns

ret – The created array

Return type

NDArray