Class Tensor#

Inheritance Relationships#

Base Type#

Class Documentation#

class Tensor : public tvm::ffi::ObjectRef#

Managed Tensor (n-dimensional array). The tensor is backed by reference counted blocks.

Note

This class can be subclassed to implement downstream customized Tensor types that are backed by the same TensorObj storage type.

Public Functions

Tensor() = default#

Default constructor.

inline explicit Tensor(::tvm::ffi::ObjectPtr<TensorObj> n)#

Constructor from a ObjectPtr<TensorObj>.

Parameters:

n – The ObjectPtr<TensorObj>.

inline explicit Tensor(::tvm::ffi::UnsafeInit tag)#

Constructor from a UnsafeInit tag.

Parameters:

tag – The UnsafeInit tag.

inline void *data_ptr() const#

Get the data pointer of the Tensor.

Returns:

The data pointer of the Tensor.

inline DLDevice device() const#

Get the device of the Tensor.

Returns:

The device of the Tensor.

inline int32_t ndim() const#

Get the number of dimensions in the Tensor.

Returns:

The number of dimensions in the Tensor.

inline DLDataType dtype() const#

Get the data type of the Tensor.

Returns:

The data type of the Tensor.

inline ShapeView shape() const#

Get the shape of the Tensor.

Returns:

The shape of the Tensor.

inline ShapeView strides() const#

Get the strides of the Tensor.

Returns:

The strides of the Tensor.

inline int64_t size(int64_t idx) const#

Get the size of the idx-th dimension. If the idx is negative, it gets the size of last idx-th dimension.

Parameters:

idx – The index of the size.

Returns:

The size of the idx-th dimension.

inline int64_t stride(int64_t idx) const#

Get the stride of the idx-th dimension. If the idx is negative, it gets the stride of last idx-th dimension.

Parameters:

idx – The index of the stride.

Returns:

The stride of the idx-th dimension.

inline int64_t numel() const#

Get the number of elements in the Tensor.

Returns:

The number of elements in the Tensor.

inline uint64_t byte_offset() const#

Get the byte offset of the Tensor.

Returns:

The byte offset of the Tensor.

inline bool IsContiguous() const#

Check if the Tensor is contiguous.

Returns:

True if the Tensor is contiguous, false otherwise.

inline bool IsAligned(size_t alignment) const#

Check if the Tensor data is aligned to the given alignment.

Parameters:

alignment – The alignment to check.

Returns:

True if the Tensor data is aligned to the given alignment, false otherwise.

inline DLManagedTensor *ToDLPack() const#

Convert the Tensor to a DLPack managed tensor.

Returns:

The converted DLPack managed tensor.

inline DLManagedTensorVersioned *ToDLPackVersioned() const#

Convert the Tensor to a DLPack managed tensor.

Returns:

The converted DLPack managed tensor.

inline const DLTensor *GetDLTensorPtr() const#

Get the underlying DLTensor pointer.

Returns:

The underlying DLTensor pointer.

inline int32_t dim()#

This functions redirects to ndim().

Returns:

The number of dimensions in the Tensor.

inline ShapeView sizes() const#

This functions redirects to shape().

Returns:

The shape of the Tensor.

inline bool is_contiguous() const#

This functions redirects to IsContiguous().

Returns:

True if the Tensor is contiguous, false otherwise.

Public Static Functions

template<typename TNDAlloc, typename ...ExtraArgs>
static inline Tensor FromNDAlloc(TNDAlloc alloc, ffi::ShapeView shape, DLDataType dtype, DLDevice device, ExtraArgs&&... extra_args)#

Create a Tensor from a NDAllocator.

Parameters:
  • alloc – The NDAllocator.

  • shape – The shape of the Tensor.

  • dtype – The data type of the Tensor.

  • device – The device of the Tensor.

  • extra_args – Extra arguments to be forwarded to TNDAlloc.

Template Parameters:
  • TNDAlloc – The type of the NDAllocator, impelments Alloc and Free.

  • ExtraArgs – Extra arguments to be passed to Alloc.

Returns:

The created Tensor.

static inline Tensor FromEnvAlloc(int (*env_alloc)(DLTensor*, TVMFFIObjectHandle*), ffi::ShapeView shape, DLDataType dtype, DLDevice device)#

Create a Tensor from the TVMFFIEnvTensorAlloc API.

This function can be used together with TVMFFIEnvSetDLPackManagedTensorAllocator in the extra/c_env_api.h to create a Tensor from the thread-local environment allocator. We explicitly pass TVMFFIEnvTensorAlloc to maintain explicit dependency on extra/c_env_api.h

ffi::Tensor tensor = ffi::Tensor::FromEnvAlloc(
  TVMFFIEnvTensorAlloc, shape, dtype, device
);

Parameters:
  • env_alloc – TVMFFIEnvTensorAlloc function pointer.

  • shape – The shape of the Tensor.

  • dtype – The data type of the Tensor.

  • device – The device of the Tensor.

Returns:

The created Tensor.

static inline Tensor FromDLPack(DLManagedTensor *tensor, size_t require_alignment = 0, bool require_contiguous = false)#

Create a Tensor from a DLPack managed tensor, pre v1.0 API.

Note

This function will not run any checks on flags.

Parameters:
  • tensor – The input DLPack managed tensor.

  • require_alignment – The minimum alignment requored of the data + byte_offset.

  • require_contiguous – Boolean flag indicating if we need to check for contiguity.

Returns:

The created Tensor.

static inline Tensor FromDLPackVersioned(DLManagedTensorVersioned *tensor, size_t require_alignment = 0, bool require_contiguous = false)#

Create a Tensor from a DLPack managed tensor, post v1.0 API.

Parameters:
  • tensor – The input DLPack managed tensor.

  • require_alignment – The minimum alignment requored of the data + byte_offset.

  • require_contiguous – Boolean flag indicating if we need to check for contiguity.

Returns:

The created Tensor.

Protected Functions

inline const TensorObj *get() const#

Get const internal container pointer.

Returns:

a const container pointer.

inline TensorObj *get_mutable() const#

Get mutable internal container pointer.

Returns:

a mutable container pointer.