Class ZeroCopyTensor

Class Documentation

class paddle::ZeroCopyTensor

Represents an n-dimensional array of values. The ZeroCopyTensor is used to store the input or output of the network. Zero copy means that the tensor supports direct copy of host or device data to device, eliminating additional CPU copy. ZeroCopyTensor is only used in the AnalysisPredictor. It is obtained through PaddlePredictor::GetinputTensor() and PaddlePredictor::GetOutputTensor() interface.

Public Functions

void Reshape(const std::vector<int> &shape)

Reset the shape of the tensor. Generally it’s only used for the input tensor. Reshape must be called before calling mutable_data() or copy_from_cpu()

Parameters
  • shape: The shape to set.

template<typename T>
T *mutable_data(PaddlePlace place)

Get the memory pointer in CPU or GPU with specific data type. Please Reshape the tensor first before call this. It’s usually used to get input data pointer.

Parameters
  • place: The place of the tensor.

template<typename T>
T *data(PaddlePlace *place, int *size) const

Get the memory pointer directly. It’s usually used to get the output data pointer.

Return

The tensor data buffer pointer.

Parameters
  • [out] place: To get the device type of the tensor.

  • [out] size: To get the data size of the tensor.

template<typename T>
void copy_from_cpu(const T *data)

Copy the host memory to tensor data. It’s usually used to set the input tensor data.

Parameters
  • data: The pointer of the data, from which the tensor will copy.

template<typename T>
void copy_to_cpu(T *data)

Copy the tensor data to the host memory. It’s usually used to get the output tensor data.

Parameters
  • [out] data: The tensor will copy the data to the address.

std::vector<int> shape() const

Return the shape of the Tensor.

void SetLoD(const std::vector<std::vector<size_t>> &x)

Set lod info of the tensor. More about LOD can be seen here: https://www.paddlepaddle.org.cn/documentation/docs/zh/beginners_guide/basic_concept/lod_tensor.html#lodtensor.

Parameters
  • x: the lod info.

std::vector<std::vector<size_t>> lod() const

Return the lod info of the tensor.

const std::string &name() const

Return the name of the tensor.

void SetPlace(PaddlePlace place, int device = -1)
PaddleDType type() const

Return the data type of the tensor. It’s usually used to get the output tensor data type.

Return

The data type of the tensor.

Protected Functions

ZeroCopyTensor(void *scope)
void SetName(const std::string &name)
void *FindTensor() const