Tensor API Reference

Tensor is the fundamental data type in esp-dl, used for storing multi-type data such as int8, int16, float, etc., similar to the tensor in PyTorch. We have implemented some common tensor operations. Please refer to the following APIs for details.

Header File

Classes

class TensorBase

This class is designed according to PyTorch Tensor. TensorBase is required to ensure that the first address are aligned to 16 bytes and the memory size should be a multiple of 16 bytes.

TODO:: Implement more functions

Public Functions

TensorBase(std::vector<int> shape, const void *element, int exponent = 0, dtype_t dtype = DATA_TYPE_FLOAT, bool deep = true, uint32_t caps = MALLOC_CAP_DEFAULT)

Construct a TensorBase object.

参数
  • shape – Shape of tensor

  • element – Pointer of data

  • exponent – Exponent of tensor, default is 0

  • dtype – Data type of element, default is float

  • deep – True: malloc memory and copy data, false: use the pointer directly

  • caps – Bitwise OR of MALLOC_CAP_* flags indicating the type of memory to be returned

inline virtual ~TensorBase()

Destroy the TensorBase object.

bool assign(TensorBase *tensor)

Assign tensor to this tensor.

参数

tensor

返回

ture if assign successfully, otherwise false.

bool assign(std::vector<int> shape, const void *element, int exponent, dtype_t dtype)

Assign data to this tensor.

参数
  • shape

  • element

  • exponent

  • dtype

返回

ture if assign successfully, otherwise false.

inline int get_size()

Get the size of Tensor.

返回

the size of Tensor.

inline int get_aligned_size()

Get the aligned size of Tensor.

返回

the aligned size of Tensor.

inline size_t get_dtype_bytes()

Get the dtype size, in bytes.

返回

the size of dtype.

inline const char *get_dtype_string()

Get the dtype string of Tensor.

返回

the string of Tensor’s dtype.

inline int get_bytes()

Get the bytes of Tensor.

返回

the bytes of Tensor.

inline int get_aligned_bytes()

Get the bytes of Tensor.

返回

the bytes of Tensor.

inline virtual void *get_element_ptr()

Get data pointer. If cache(preload data pointer) is not null, return cache pointer, otherwise return data pointer.

返回

the pointer of Tensor’s data

template<typename T>
inline T *get_element_ptr()

Get data pointer by the specified template. If cache(preload data pointer) is not null, return cache pointer, otherwise return data pointer.

返回

the pointer of Tensor’s data

TensorBase &set_element_ptr(void *data)

Set the data pointer of Tensor.

参数

data – point to data memory

返回

TensorBase& self

inline std::vector<int> get_shape()

Get the shape of Tensor.

返回

std::vector<int> the shape of Tensor

TensorBase &set_shape(const std::vector<int> shape)

Set the shape of Tensor.

参数

shape – the shape of Tensor.

返回

Tensor.

inline int get_exponent()

Get the exponent of Tensor.

返回

int the exponent of Tensor

inline dtype_t get_dtype()

Get the data type of Tensor.

返回

dtype_t the data type of Tensor

inline uint32_t get_caps()

Get the memory flags of Tensor.

返回

uint32_t the memory flags of Tensor

TensorBase *reshape(std::vector<int> shape)

Change a new shape to the Tensor without changing its data.

参数

shape – the target shape

返回

TensorBase *self

template<typename T>
TensorBase *flip(const std::vector<int> &axes)

Flip the input Tensor along the specified axes.

参数

axes – the specified axes

返回

TensorBase& self

TensorBase *transpose(TensorBase *input, std::vector<int> perm = {})

Reverse or permute the axes of the input Tensor.

参数
  • input – the input Tensor

  • perm – the new arangement of the dims. if perm == {}, the dims arangement will be reversed.

返回

TensorBase *self

template<typename T>
TensorBase *transpose(T *input_element, std::vector<int> &input_shape, std::vector<int> &input_axis_offset, std::vector<int> &perm)

Reverse or permute the axes of the input Tensor.

参数
  • input_element – the input data pointer

  • input_shape – the input data shape

  • input_axis_offset – the input data axis offset

  • perm – the new arangement of the dims. if perm == {}, the dims arangement will be reversed.

返回

TensorBase *self

bool is_same_shape(TensorBase *tensor)

Check the shape is the same as the shape of input.

参数

tensor – Input tensor pointer

返回

  • true: same shape

  • false: not

bool equal(TensorBase *tensor, float epsilon = 1e-6, bool verbose = false)

Compare the shape and data of two Tensor.

参数
  • tensor – Input tensor

  • epsilon – The max error of two element

  • verbose – If true, print the detail of results

返回

true if two tensor is equal otherwise false

TensorBase *slice(const std::vector<int> &start, const std::vector<int> &end, const std::vector<int> &axes = {}, const std::vector<int> &step = {})

Produces a slice of the this tensor along multiple axes.

警告

The length of start, end and step must be same as the shape of input tensor

参数
  • start – Starting indicesd

  • end – Ending indices

  • axes – Axes that starts and ends apply to.

  • step – Slice step, step = 1 if step is not specified

返回

Output tensor pointer, created by this slice function

template<typename T>
TensorBase *pad(T *input_element, const std::vector<int> &input_shape, const std::vector<int> &pads, const padding_mode_t mode, TensorBase *const_value = nullptr)

Pad input tensor.

参数
  • input_element – Data pointer of input tensor

  • input_shape – Shape of input tensor

  • pads – The number of padding elements to add, pads format should be: [x1_begin, x2_begin, …, x1_end, x2_end,…]

  • mode – Supported modes: constant(default), reflect, edge

  • const_value – (Optional) A scalar value to be used if the mode chosen is constant

返回

Output tensor pointer

TensorBase *pad(TensorBase *input, const std::vector<int> &pads, const padding_mode_t mode, TensorBase *const_value = nullptr)

Pad input tensor.

参数
  • input – Input tensor pointer

  • pads – Padding elements to add, pads format should be: [x1_begin, x2_begin, …, x1_end, x2_end,…]

  • mode – Supported modes: constant(default), reflect, edge

  • const_value – (Optional) A scalar value to be used if the mode chosen is constant

返回

Output tensor pointer

template<typename T>
bool compare_elements(const T *gt_elements, float epsilon = 1e-6, bool verbose = false)

Compare the elements of two Tensor.

参数
  • gt_elements – The ground truth elements

  • epsilon – The max error of two element

  • verbose – If true, print the detail of results

返回

true if all elements are equal otherwise false

int get_element_index(const std::vector<int> &axis_index)

Get the index of element.

参数

axis_index – The coordinates of element

返回

int the index of element

std::vector<int> get_element_coordinates(int index)

Get the coordinates of element.

参数

index – The index of element

返回

The coordinates of element

template<typename T>
T get_element(int index)

Get a element of Tensor by index.

参数

index – The index of element

返回

The element of tensor

template<typename T>
T get_element(const std::vector<int> &axis_index)

Get a element of Tensor.

参数

axis_index – The index of element

返回

The element of tensor

size_t set_preload_addr(void *addr, size_t size)

Set preload address of Tensor.

参数
  • addr – The address of preload data

  • size – Size of preload data

返回

The size of preload data

inline virtual void preload()

Preload the data of Tensor.

void reset_bias_layout(quant_type_t op_quant_type, bool is_depthwise)

Reset the layout of Tensor.

警告

Only available for Convolution. Don’t use it unless you know exactly what it does.

参数
  • op_quant_type – The quant type of operation

  • is_depthwise – Whether is depthwise convolution

virtual void print(bool print_data = false)

print the information of TensorBase

参数

print_data – Whether print the data

Public Members

int size

size of element including padding

std::vector<int> shape

shape of Tensor

dtype_t dtype

data type of element

int exponent

exponent of element

bool auto_free

free element when object destroy

std::vector<int> axis_offset

element offset of each axis

void *data

data pointer

void *cache

cache pointer, used for preload and do not need to free

uint32_t caps

flags indicating the type of memory

Public Static Functions

static void slice(TensorBase *input, TensorBase *output, const std::vector<int> &start, const std::vector<int> &end, const std::vector<int> &axes = {}, const std::vector<int> &step = {})

Produces a slice along multiple axes.

警告

The length of start, end and step must be same as the shape of input tensor

参数
  • input – Input Tensor

  • output – Output Tensor

  • start – Starting indicesd

  • end – Ending indices

  • axes – Axes that starts and ends apply to.

  • step – Slice step, step = 1 if step is not specified

返回

Void