tvm
Public Types | Public Member Functions | Static Public Member Functions | List of all members
tvm::runtime::Tensor Class Reference

Managed Tensor. The array is backed by reference counted blocks. More...

#include <tensor.h>

Inheritance diagram for tvm::runtime::Tensor:
Collaboration diagram for tvm::runtime::Tensor:

Public Types

using Container = ffi::TensorObj
 

Public Member Functions

 Tensor ()=default
 
 Tensor (ObjectPtr< ffi::TensorObj > data)
 constructor. More...
 
 Tensor (ffi::UnsafeInit tag)
 
 Tensor (ffi::Tensor &&other)
 
 Tensor (const ffi::Tensor &other)
 
ffi::Shape Shape () const
 
runtime::DataType DataType () const
 
void CopyFrom (const DLTensor *other)
 Copy data content from another array. More...
 
void CopyFrom (const Tensor &other)
 
void CopyFromBytes (const void *data, size_t nbytes)
 Copy data content from a byte buffer. More...
 
void CopyTo (DLTensor *other) const
 Copy data content into another array. More...
 
void CopyTo (const Tensor &other) const
 
void CopyToBytes (void *data, size_t nbytes) const
 Copy data content into another array. More...
 
Tensor CopyTo (const Device &dev, ffi::Optional< ffi::String > mem_scope=std::nullopt) const
 Copy the data to another device. More...
 
bool Load (dmlc::Stream *stream)
 Load Tensor from stream. More...
 
void Save (dmlc::Stream *stream) const
 Save Tensor to stream. More...
 
Tensor CreateView (ffi::Shape shape, DLDataType dtype, uint64_t relative_byte_offset=0) const
 Create a Tensor that shares the data memory with the current one. More...
 

Static Public Member Functions

static Tensor FromDLPack (DLManagedTensor *tensor)
 
static Tensor FromDLPackVersioned (DLManagedTensorVersioned *tensor)
 
static Tensor Empty (ffi::Shape shape, DLDataType dtype, Device dev, ffi::Optional< ffi::String > mem_scope=std::nullopt)
 Create an empty Tensor. More...
 
static void CopyFromTo (const DLTensor *from, DLTensor *to, TVMStreamHandle stream=nullptr)
 Function to copy data from one array to another. More...
 
static void CopyToBytes (const DLTensor *from, void *to, size_t nbytes, TVMStreamHandle stream=nullptr)
 Function to copy data from one array to a byte buffer. More...
 

Detailed Description

Managed Tensor. The array is backed by reference counted blocks.

Member Typedef Documentation

◆ Container

using tvm::runtime::Tensor::Container = ffi::TensorObj

Constructor & Destructor Documentation

◆ Tensor() [1/5]

tvm::runtime::Tensor::Tensor ( )
default

◆ Tensor() [2/5]

tvm::runtime::Tensor::Tensor ( ObjectPtr< ffi::TensorObj >  data)
inlineexplicit

constructor.

Parameters
dataObjectPtr to the data container.

◆ Tensor() [3/5]

tvm::runtime::Tensor::Tensor ( ffi::UnsafeInit  tag)
inlineexplicit

◆ Tensor() [4/5]

tvm::runtime::Tensor::Tensor ( ffi::Tensor &&  other)
inline

◆ Tensor() [5/5]

tvm::runtime::Tensor::Tensor ( const ffi::Tensor &  other)
inline

Member Function Documentation

◆ CopyFrom() [1/2]

void tvm::runtime::Tensor::CopyFrom ( const DLTensor *  other)
inline

Copy data content from another array.

Parameters
otherThe source array to be copied from.
Note
The copy may happen asynchronously if it involves a GPU context. TVMSynchronize is necessary.

◆ CopyFrom() [2/2]

void tvm::runtime::Tensor::CopyFrom ( const Tensor other)
inline

◆ CopyFromBytes()

void tvm::runtime::Tensor::CopyFromBytes ( const void *  data,
size_t  nbytes 
)

Copy data content from a byte buffer.

Parameters
dataThe source bytes to be copied from.
nbytesThe size of the buffer in bytes Must be equal to the size of the Tensor.
Note
The copy always triggers a TVMSynchronize.

◆ CopyFromTo()

static void tvm::runtime::Tensor::CopyFromTo ( const DLTensor *  from,
DLTensor *  to,
TVMStreamHandle  stream = nullptr 
)
static

Function to copy data from one array to another.

Parameters
fromThe source array.
toThe target array.
streamThe stream used in copy.

◆ CopyTo() [1/3]

Tensor tvm::runtime::Tensor::CopyTo ( const Device dev,
ffi::Optional< ffi::String >  mem_scope = std::nullopt 
) const

Copy the data to another device.

Parameters
devThe target device.
mem_scopeThe memory scope of the target array.
Returns
The array under another device.
Note
The copy always triggers a TVMSynchronize.

◆ CopyTo() [2/3]

void tvm::runtime::Tensor::CopyTo ( const Tensor other) const
inline

◆ CopyTo() [3/3]

void tvm::runtime::Tensor::CopyTo ( DLTensor *  other) const
inline

Copy data content into another array.

Parameters
otherThe source array to be copied from.
Note
The copy may happen asynchronously if it involves a GPU context. TVMSynchronize is necessary.

◆ CopyToBytes() [1/2]

static void tvm::runtime::Tensor::CopyToBytes ( const DLTensor *  from,
void *  to,
size_t  nbytes,
TVMStreamHandle  stream = nullptr 
)
static

Function to copy data from one array to a byte buffer.

Parameters
fromThe source array.
toThe target byte buffer.
nbytesThe size of the data buffer.
streamThe stream used in copy.

◆ CopyToBytes() [2/2]

void tvm::runtime::Tensor::CopyToBytes ( void *  data,
size_t  nbytes 
) const

Copy data content into another array.

Parameters
dataThe source bytes to be copied from.
nbytesThe size of the data buffer. Must be equal to the size of the Tensor.
Note
The copy always triggers a TVMSynchronize.

◆ CreateView()

Tensor tvm::runtime::Tensor::CreateView ( ffi::Shape  shape,
DLDataType  dtype,
uint64_t  relative_byte_offset = 0 
) const

Create a Tensor that shares the data memory with the current one.

Parameters
shapeThe shape of the new array.
dtypeThe data type of the new array.
relative_byte_offsetThe offset of the output Tensor, relative to the current byte offset.

By default, the offset of the view is the same as the offset of the current array.

Note
The new array must not allow access of addresses which would be out of bounds in the current array. If the new array is larger than the current array, or if the relative_byte_offset would place the end of the new array outside the bounds of the current array, this function will raise an exception.

◆ DataType()

runtime::DataType tvm::runtime::Tensor::DataType ( ) const
inline

◆ Empty()

static Tensor tvm::runtime::Tensor::Empty ( ffi::Shape  shape,
DLDataType  dtype,
Device  dev,
ffi::Optional< ffi::String >  mem_scope = std::nullopt 
)
static

Create an empty Tensor.

Parameters
shapeThe shape of the new array.
dtypeThe data type of the new array.
devThe device of the array.
mem_scopeThe memory scope of the array.
Returns
The created Array

◆ FromDLPack()

static Tensor tvm::runtime::Tensor::FromDLPack ( DLManagedTensor *  tensor)
inlinestatic

◆ FromDLPackVersioned()

static Tensor tvm::runtime::Tensor::FromDLPackVersioned ( DLManagedTensorVersioned *  tensor)
inlinestatic

◆ Load()

bool tvm::runtime::Tensor::Load ( dmlc::Stream *  stream)
inline

Load Tensor from stream.

Parameters
streamThe input data stream
Returns
Whether load is successful

◆ Save()

void tvm::runtime::Tensor::Save ( dmlc::Stream *  stream) const
inline

Save Tensor to stream.

Parameters
streamThe output data stream

◆ Shape()

ffi::Shape tvm::runtime::Tensor::Shape ( ) const
inline

The documentation for this class was generated from the following file: