tvm.runtime.ndarray#

Runtime NDArray API

class tvm.runtime.ndarray.NDArray(handle, is_view=False)[源代码]#

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.

__dlpack__(stream=None)[源代码]#

Export the array for consumption by from_dlpack() as a DLPack capsule.

Parameters#

streamint, optional

A Python integer representing a pointer to a stream. Stream is provided by the consumer to the producer to instruct the producer to ensure that operations can safely be performed on the array.

Returns#

capsulePyCapsule

A DLPack capsule for the array, containing a DLPackManagedTensor.

__dlpack_device__()[源代码]#

Return a tuple of device_type, device_id in DLPack convention

__setitem__(in_slice, value)[源代码]#

Set ndarray value

_create_view(shape, dtype=None, relative_byte_offset=0)[源代码]#

Create a view into an existing array.

The view shares the same allocation and datatype as the existing array, but can have a different array shape. This is useful for runtimes that support non-flat memory, where both the physical shape of an allocation and the logical shape of the tensor it represents may need to be independently specified.

Warning: This function should not be used outside of low-level manipulations, as it breaks non-aliasing assumptions made by TVM. This function may also be removed/replaced in the future.

Parameters#

shape: Union[tvm.runtime.ShapeTuple, Sequence[typing.SupportsInt]]

The shape of the view.

dtype: Optional[str]

The datatype of the view. If None (default), the view will be the same data type as the current array.

relative_byte_offset: int

The location of the view, relative to the location of the current array.

Note: While the DLTensor.byte_offset field of the returned view is usually the same as relative_byte_offset, this is not guaranteed. The DLTensor.byte_offset field is relative to the start of the backing allocation, while the relative_byte_offset is relative to the start of self.

参数:
  • dtype (str | None)

  • relative_byte_offset (int)

asnumpy()[源代码]#

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

copyfrom(source_array)[源代码]#

Perform a synchronous copy from the array.

Parameters#

source_arrayarray_like

The data source we should like to copy from.

Returns#

arrNDArray

Reference to self.

copyto(target, mem_scope=None)[源代码]#

Copy array to target

Parameters#

targetNDArray

The target array to be copied, must have same shape as this array.

mem_scopeOptional[str]

The memory scope of the array.

numpy()[源代码]#

Convert this array to numpy array

Returns#

np_arrnumpy.ndarray

The corresponding numpy array.

same_as(other)[源代码]#

Check object identity equality

Parameters#

otherobject

The other object to compare to

Returns#

samebool

Whether other is same as self.

property device#

Device of this array

property dtype#

Type of this array

tvm.runtime.ndarray.array(arr, device=cpu(0), mem_scope=None)[源代码]#

Create an array from source arr.

Parameters#

arrnumpy.ndarray

The array to be copied from

deviceDevice, optional

The device to create the array

mem_scopeOptional[str]

The memory scope of the array

Returns#

retNDArray

The created array

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

Construct a OpenCL device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.cpu(dev_id=0)[源代码]#

Construct a CPU device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.cuda(dev_id=0)[源代码]#

Construct a CUDA GPU device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

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_idint, optional

The integer device id

Returns#

dev: tvm.runtime.Device

The corresponding 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.empty(shape, dtype='float32', device=cpu(0), mem_scope=None)[源代码]#

Create an empty array given shape and device

Parameters#

shapeUnion[tvm.runtime.ShapeTuple, Sequence[typing.SupportsInt]]

The shape of the array.

dtypetype or str

The data type of the array.

deviceDevice

The device of the array.

mem_scopeOptional[str]

The memory scope of the array.

Returns#

arrtvm.nd.NDArray

The array tvm supported.

tvm.runtime.ndarray.ext_dev(dev_id=0)[源代码]#

Construct a extension device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

Note#

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

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: tvm.nd.NDArray

The array view of the tensor data.

tvm.runtime.ndarray.gpu(dev_id=0)[源代码]#

Construct a CUDA GPU device

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

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.hexagon(dev_id=0)[源代码]#

Construct a Hexagon device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.metal(dev_id=0)[源代码]#

Construct a metal device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

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

Construct a metal device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.numpyasarray(np_data)[源代码]#

Return a TVMArray representation of a numpy array.

tvm.runtime.ndarray.opencl(dev_id=0)[源代码]#

Construct a OpenCL device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.rocm(dev_id=0)[源代码]#

Construct a ROCM device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.vpi(dev_id=0)[源代码]#

Construct a VPI simulated device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.vulkan(dev_id=0)[源代码]#

Construct a Vulkan device

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device

tvm.runtime.ndarray.webgpu(dev_id=0)[源代码]#

Construct a webgpu device.

Parameters#

dev_idint, optional

The integer device id

Returns#

devDevice

The created device