Class MSTensor
#include <types.h>
MSTensor定义了MindSpore中的张量。
构造函数
MSTensor()
explicit MSTensor(const std::shared_ptr<Impl> &impl)
MSTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape, const void *data, size_t data_len)
explicit MSTensor(std::nullptr_t)
注意:MSTensor构造时,若data指针通过malloc生成,用户在构造完成MSTensor后,需自行释放free,否则存在内存泄露。
析构函数
~MSTensor()
静态公有成员函数
函数 |
云侧推理是否支持 |
端侧推理是否支持 |
|---|---|---|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
✕ |
|
√ |
✕ |
|
MSTensor *StringsToTensor(const std::string &name, const std::vector<std::string> &str) |
√ |
√ |
std::vector<std::string> TensorToStrings(const MSTensor &tensor) |
√ |
√ |
√ |
√ |
CreateTensor
MSTensor *CreateTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape,
const void *data, size_t data_len, const std::string &device = "",
int device_id = -1) noexcept
创建一个MSTensor对象,其数据需复制后才能由Model访问,必须与DestroyTensorPtr成对使用。
参数
name: 名称。type:数据类型。shape:张量的形状。data:数据指针,指向一段已开辟的内存。data_len:数据长度,以字节为单位。device:设备类型,表明Tensor的内存存放的位置位于设备侧。device_id:设备编号。
返回值
MStensor指针。
CreateTensor
MSTensor *CreateTensor(const std::string &name, const MSTensor &tensor, const std::string &device = "",
int device_id = -1) noexcept
创建一个MSTensor对象,其数据需复制后才能由Model访问,必须与DestroyTensorPtr成对使用。
参数
name: 名称。tensor:用于作为拷贝的源MSTensor。device:设备类型,表明Tensor的内存存放的位置位于设备侧。device_id:设备编号。
返回值
MStensor指针。
CreateRefTensor
MSTensor *CreateRefTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape, void *data,
size_t data_len) noexcept
创建一个MSTensor对象,其数据可以直接由Model访问,必须与DestroyTensorPtr成对使用。
参数
name: 名称。type:数据类型。shape:张量的形状。data:数据指针,指向一段已开辟的内存。data_len:数据长度,以字节为单位。
返回值
MStensor指针。
CreateDeviceTensor
static inline MSTensor CreateDeviceTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape,
void *data, size_t data_len) noexcept
创建一个MSTensor对象,其device数据可以直接由Model访问,不需要与DestroyTensorPtr成对使用。
参数
name: 名称。type:数据类型。shape:张量的形状。data:数据指针,指向一段已开辟的device内存。data_len:数据长度,以字节为单位。
返回值
MStensor对象。
CreateTensorFromFile
static inline MSTensor *CreateTensorFromFile(const std::string &file, DataType type = DataType::kNumberTypeUInt8,
const std::vector<int64_t> &shape = {}) noexcept
创建一个MSTensor对象,其数据由文件路径file所指定,必须与DestroyTensorPtr成对使用。
参数
file: 文件路径,指向存放数据的二进制格式文件,可以是相对路径或者绝对路径。type:file文件保存的数据类型,也是创建后MSTensor对象的数据类型。shape:张量的形状,shape的乘积代表了file文件内数据的个数。
返回值
MStensor指针。
StringsToTensor
MSTensor *StringsToTensor(const std::string &name, const std::vector<std::string> &str)
创建一个字符串类型的MSTensor对象,其数据需复制后才能由Model访问,必须与DestroyTensorPtr成对使用。
参数
name: 名称。str:装有若干个字符串的vector容器。
返回值
MStensor指针。
TensorToStrings
std::vector<std::string> TensorToStrings(const MSTensor &tensor)
将字符串类型的MSTensor对象解析为字符串。
参数
tensor: 张量对象。
返回值
装有若干个字符串的
vector容器。
DestroyTensorPtr
void DestroyTensorPtr(MSTensor *tensor) noexcept
销毁一个由Clone、StringsToTensor、CreateRefTensor或CreateTensor所创建的对象,请勿用于销毁其他来源的MSTensor。
参数
tensor: 由Clone、StringsToTensor、CreateRefTensor或CreateTensor返回的指针。
公有成员函数
函数 |
云侧推理是否支持 |
端侧推理是否支持 |
|---|---|---|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
✕ |
|
√ |
✕ |
|
√ |
√ |
|
√ |
✕ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
|
√ |
√ |
Name
std::string Name() const
获取MSTensor的名字。
返回值
MSTensor的名字。
DataType
enum DataType DataType() const
获取MSTensor的数据类型。
返回值
MSTensor的数据类型。
Shape
const std::vector<int64_t> &Shape() const
获取MSTensor的Shape。
返回值
MSTensor的Shape。
ElementNum
int64_t ElementNum() const
获取MSTensor的元素个数。
返回值
MSTensor的元素个数。
Data
std::shared_ptr<const void> Data() const
获取指向MSTensor中的数据拷贝的智能指针。
返回值
指向
MSTensor中的数据拷贝的智能指针。
MutableData
void *MutableData()
获取MSTensor中的数据的指针。如果为空指针,为MSTensor的数据申请内存,并返回申请内存的地址,如果不为空,返回数据的指针。
返回值
指向
MSTensor中的数据的指针。
DataSize
size_t DataSize() const
获取MSTensor中的数据的以字节为单位的内存长度。
返回值
MSTensor中的数据的以字节为单位的内存长度。
GetDevice
int GetDevice() const
获取MSTensor所处的设备类型。
返回值
MSTensor所处的设备类型。
GetDeviceId
int GetDeviceId() const
获取MSTensor所处的设备编号。
返回值
MSTensor所处的设备编号。
IsConst
bool IsConst() const
判断MSTensor中的数据是否是常量数据。
返回值
MSTensor中的数据是否是常量数据。
IsDevice
bool IsDevice() const
判断MSTensor中是否在设备上。
返回值
MSTensor中是否在设备上。
Clone
MSTensor *Clone() const
拷贝一份自身的副本。
返回值
指向深拷贝副本的指针,必须与
DestroyTensorPtr成对使用。
operator==(std::nullptr_t)
bool operator==(std::nullptr_t) const
判断MSTensor是否合法。
返回值
MSTensor是否合法。
operator!=(std::nullptr_t)
bool operator!=(std::nullptr_t) const
判断MSTensor是否合法。
返回值
MSTensor是否合法。
operator==(const MSTensor &tensor)
bool operator==(const MSTensor &tensor) const
判断MSTensor是否与另一个MSTensor相等。
返回值
MSTensor是否与另一个MSTensor相等。
operator!=(const MSTensor &tensor)
bool operator!=(const MSTensor &tensor) const
判断MSTensor是否与另一个MSTensor不相等。
返回值
MSTensor是否与另一个MSTensor不相等。
SetShape
void SetShape(const std::vector<int64_t> &shape)
设置MSTensor的Shape,目前在Delegate机制使用。
SetDataType
void SetDataType(enum DataType data_type)
设置MSTensor的DataType,目前在Delegate机制使用。
SetTensorName
void SetTensorName(const std::string &name)
设置MSTensor的名字,目前在Delegate机制使用。
SetAllocator
void SetAllocator(std::shared_ptr<Allocator> allocator)
设置MSTensor数据所属的内存池。
参数
model: 指向Allocator的指针。
allocator
std::shared_ptr<Allocator> allocator() const
获取MSTensor数据所属的内存池。
返回值
指向Allocator的指针。
SetFormat
void SetFormat(mindspore::Format format)
设置MSTensor数据的format,目前在Delegate机制使用。
format
mindspore::Format format() const
获取MSTensor数据的format,目前在Delegate机制使用。
SetData
void SetData(void *data, bool own_data = true)
设置指向MSTensor数据的指针。
参数
data: 新的数据的地址。own_data: 是否在MSTensor析构时释放数据内存。如果为true,将在MSTensor析构时释放数据内存,如果重复调用SetData,将仅释放新的数据内存,老的数据内存需要用户释放;如果为false,需要用户释放数据内存。由于向前兼容,默认为true,建议用户设置为false。
SetDeviceData
void SetDeviceData(void *data)
设置数据的设备地址,由用户负责设备内存的申请和释放。仅适用于Ascend和GPU硬件后端。
GetDeviceData
void *GetDeviceData()
获取由SetDeviceData接口设置的MSTensor数据的设备地址。
QuantParams
std::vector<QuantParam> QuantParams() const
获取MSTensor的量化参数,目前在Delegate机制使用。
SetQuantParams
void SetQuantParams(std::vector<QuantParam> quant_params)
设置MSTensor的量化参数,目前在Delegate机制使用。
impl
const std::shared_ptr<Impl> impl()
获取实现类的指针。