Class MSTensor

Class Documentation

class MSTensor

The MSTensor class defines a tensor in MindSpore.

Public Functions

inline std::string Name() const

Obtains the name of the MSTensor.

Returns

The name of the MSTensor.

enum DataType DataType() const

Obtains the data type of the MSTensor.

Returns

The data type of the MSTensor.

const std::vector<int64_t> &Shape() const

Obtains the shape of the MSTensor.

Returns

The shape of the MSTensor.

int64_t ElementNum() const

Obtains the number of elements of the MSTensor.

Returns

The number of elements of the MSTensor.

std::shared_ptr<const void> Data() const

Obtains a shared pointer to the copy of data of the MSTensor. The data can be read on host.

Returns

A shared pointer to the copy of data of the MSTensor.

void *MutableData()

Obtains the pointer to the data of the MSTensor. If the MSTensor is a device tensor, the data cannot be accessed directly on host.

Returns

A pointer to the data of the MSTensor.

size_t DataSize() const

Obtains the length of the data of the MSTensor, in bytes.

Returns

The length of the data of the MSTensor, in bytes.

bool IsConst() const

Get whether the MSTensor data is const data.

Returns

Const flag of MSTensor

bool IsDevice() const

Gets the boolean value that indicates whether the memory of MSTensor is on device.

Returns

The boolean value that indicates whether the memory of MSTensor is on device.

MSTensor *Clone() const

Gets a deep copy of the MSTensor, must be used in pair with DestroyTensorPtr.

Returns

A pointer points to a deep copy of the MSTensor.

bool operator==(std::nullptr_t) const

Gets the boolean value that indicates whether the MSTensor is valid.

Returns

The boolean value that indicates whether the MSTensor is valid.

bool operator!=(std::nullptr_t) const

Gets the boolean value that indicates whether the MSTensor is valid.

Returns

The boolean value that indicates whether the MSTensor is valid.

bool operator==(const MSTensor &tensor) const

Get the boolean value that indicates whether the MSTensor equals tensor.

Parameters

another[in] MSTensor.

Returns

The boolean value that indicates whether the MSTensor equals tensor.

void SetShape(const std::vector<int64_t> &shape)

Set the shape of for the MSTensor. Only valid for Lite.

Parameters

Shape[in] of the MSTensor, a vector of int64_t.

void SetDataType(enum DataType data_type)

Set the data type for the MSTensor. Only valid for Lite.

Parameters

The[in] data type of the MSTensor.

inline void SetTensorName(const std::string &name)

Set the name for the MSTensor. Only valid for Lite.

Parameters

The[in] name of the MSTensor.

void SetAllocator(std::shared_ptr<Allocator> allocator)

Set the Allocator for the MSTensor. Only valid for Lite.

Parameters

A[in] pointer to Allocator.

std::shared_ptr<Allocator> allocator() const

Obtain the Allocator of the MSTensor. Only valid for Lite.

Returns

A pointer to Allocator.

void SetFormat(mindspore::Format format)

Set the format for the MSTensor. Only valid for Lite.

Parameters

The[in] format of the MSTensor.

mindspore::Format format() const

Obtain the format of the MSTensor. Only valid for Lite.

Returns

The format of the MSTensor.

void SetData(void *data)

Set the data for the MSTensor. Only valid for Lite.

Parameters

A[in] pointer to the data of the MSTensor.

std::vector<QuantParam> QuantParams() const

Get the quantization parameters of the MSTensor. Only valid for Lite.

Returns

The quantization parameters of the MSTensor.

void SetQuantParams(std::vector<QuantParam> quant_params)

Set the quantization parameters for the MSTensor. Only valid for Lite.

Parameters

The[in] quantization parameters of the MSTensor.

Public Static Functions

static inline MSTensor *CreateTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape, const void *data, size_t data_len) noexcept

Creates a MSTensor object, whose data need to be copied before accessed by Model, must be used in pairs with DestroyTensorPtr.

Parameters
  • name[in] The name of the MSTensor.

  • type[in] The data type of the MSTensor.

  • shape[in] The shape of the MSTensor.

  • data[in] The data pointer that points to allocated memory.

  • data_len[in] The length of the memory, in bytes.

Returns

A pointer of MSTensor.

static inline MSTensor *CreateRefTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape, const void *data, size_t data_len) noexcept

Creates a MSTensor object, whose data can be directly accessed by Model, must be used in pairs with DestroyTensorPtr.

Parameters
  • name[in] The name of the MSTensor.

  • type[in] The data type of the MSTensor.

  • shape[in] The shape of the MSTensor.

  • data[in] The data pointer that points to allocated memory.

  • data_len[in] The length of the memory, in bytes.

Returns

A pointer of MSTensor.

static inline MSTensor *CreateDevTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape, const void *data, size_t data_len) noexcept

Creates a MSTensor object, whose device data can be directly accessed by Model, must be used in pairs with DestroyTensorPtr.

Parameters
  • name[in] The name of the MSTensor.

  • type[in] The data type of the MSTensor.

  • shape[in] The shape of the MSTensor.

  • data[in] The data pointer that points to device memory.

  • data_len[in] The length of the memory, in bytes.

Returns

A pointer of MSTensor.

static inline MSTensor *CreateImageTensor(const std::string &image_file) noexcept

Creates a MSTensor object from local image file, must be used in pairs with DestroyTensorPtr.

Parameters

image_file[in] Path of image file.

Returns

A pointer of MSTensor.

static inline MSTensor *StringsToTensor(const std::string &name, const std::vector<std::string> &str)

Create a string type MSTensor object whose data can be accessed by Model only after being copied, must be used in pair with DestroyTensorPtr.

Parameters
  • name[in] The name of the MSTensor.

  • str[in] A vector container containing several strings.

Returns

A pointer of MSTensor.

static inline std::vector<std::string> TensorToStrings(const MSTensor &tensor)

Parse the string type MSTensor object into strings.

Parameters

tensor[in] A MSTensor object.

Returns

A vector container containing several strings.

static void DestroyTensorPtr(MSTensor *tensor) noexcept

Destroy an object created by Clone, StringsToTensor, CreateRefTensor, CreateDevTensor or CreateTensor. Do not use it to destroy MSTensor from other sources.

Parameters

tensor[in] A MSTensor object.