tvm.runtime.ndarray#

Runtime NDArray API

class tvm.nd.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

__init__(handle, is_view=False)#

Initialize the function with handle

Parameters#

handleTVMArrayHandle

the handle to the underlying C++ TVMArray

__setitem__(in_slice, value)#

Set ndarray value

_copyto(target_nd)#

Internal function that implements copy to target ndarray.

_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.

to_dlpack()#

Produce an array from a DLPack Tensor without copying memory

Returns#

dlpack : DLPack tensor view of the array data

property device#

Device of this array

property dtype#

Type of this array

property shape#

Shape of this array

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