Class TensorView#

Class Documentation#

class TensorView#

A non-owning view of a Tensor.

This class stores a light-weight non-owning view of a Tensor. This is useful for accessing tensor data without retaining a strong reference to the Tensor. Since the caller may not always be able to pass in a strong referenced tensor.

It is the user’s responsibility to ensure that the underlying tensor data outlives the TensorView. This responsibility extends to all data pointed to by the underlying DLTensor. This includes not only the tensor elements in data but also the memory for shape and strides.

When exposing a function that expects only expects a TensorView, we recommend using ffi::TensorView as the argument type instead of ffi::Tensor.

Public Functions

inline TensorView(const Tensor &tensor)#

Create a TensorView from a Tensor.

Parameters:

tensor – The input Tensor.

inline TensorView(const DLTensor *tensor)#

Create a TensorView from a DLTensor.

Parameters:

tensor – The input DLTensor.

TensorView(const TensorView &tensor) = default#

Copy constructor.

Parameters:

tensor – The input TensorView.

TensorView(TensorView &&tensor) = default#

Move constructor.

Parameters:

tensor – The input TensorView.

TensorView(Tensor &&tensor) = delete#
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 int64_t numel() const#

Get the number of elements in the Tensor.

Returns:

The number of elements in 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 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 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.