mindspore¶
接口汇总¶
类¶
类名 | 描述 |
---|---|
Context | 保存执行中的环境变量。 |
DeviceInfoContext | 不同硬件设备的环境信息。 |
CPUDeviceInfo | 模型运行在CPU上的配置,仅MindSpore Lite支持。 |
GPUDeviceInfo | 模型运行在GPU上的配置。 |
KirinNPUDeviceInfo | 模型运行在NPU上的配置,仅MindSpore Lite支持。 |
Ascend910DeviceInfo | 模型运行在Ascend910上的配置,MindSpore Lite不支持。 |
Ascend310DeviceInfo | 模型运行在Ascend310上的配置。 |
Serialization | 汇总了模型文件读写的方法。 |
Buffer | Buff数据类。 |
Model | MindSpore中的模型,便于计算图管理。 |
MSTensor | MindSpore中的张量。 |
QuantParam | MSTensor中的一组量化参数。 |
MSKernelCallBack | MindSpore回调函数包装器,仅MindSpore Lite支持。 |
MSCallBackParam | MindSpore回调函数的参数,仅MindSpore Lite支持。 |
Delegate | MindSpore Lite接入第三方AI框架的代理,仅MindSpore Lite支持。 |
SchemaVersion | MindSpore Lite 执行推理时模型文件的版本,仅MindSpore Lite支持。 |
KernelIter | MindSpore Lite 算子列表的迭代器,仅MindSpore Lite支持。 |
DelegateModel | MindSpore Lite Delegate机制封装的模型,仅MindSpore Lite支持。 |
TrainCfg | MindSpore Lite训练配置类,仅MindSpore Lite支持。 |
MixPrecisionCfg | MindSpore Lite训练混合精度配置类,仅MindSpore Lite支持。 |
AccuracyMetrics | MindSpore Lite训练精度类,仅MindSpore Lite支持。 |
Metrics | MindSpore Lite训练指标类,仅MindSpore Lite支持。 |
TrainCallBack | MindSpore Lite训练回调类,仅MindSpore Lite支持。 |
TrainCallBackData | 定义了训练回调的一组参数,仅MindSpore Lite支持。 |
CkptSaver | MindSpore Lite训练模型文件保存类,仅MindSpore Lite支持。 |
LossMonitor | MindSpore Lite训练学习率调度类,仅MindSpore Lite支持。 |
LRScheduler | MindSpore Lite训练配置类,仅MindSpore Lite支持。 |
StepLRLambda | MindSpore Lite训练学习率的一组参数,仅MindSpore Lite支持。 |
MultiplicativeLRLambda | 每个epoch将学习率乘以一个因子,仅MindSpore Lite支持。 |
TimeMonitor | MindSpore Lite训练时间监测类,仅MindSpore Lite支持。 |
TrainAccuracy | MindSpore Lite训练学习率调度类,仅MindSpore Lite支持。 |
CharVersion | 获取当前版本号,仅MindSpore Lite支持。 |
Version | 获取当前版本号,仅MindSpore Lite支持。 |
Allocator | 内存管理基类。 |
Status | 返回状态类。 |
Graph | 图类。 |
CellBase | 容器基类。 |
Cell | 容器类。 |
GraphCell | 图容器类。 |
RunnerConfig | 模型并发推理配置参数。 |
ModelParallelRunner | 模型并发推理类。 |
枚举¶
接口名 | 描述 |
---|---|
mindspore::DataType | MindSpore MSTensor保存的数据支持的类型。 |
mindspore::Format | MindSpore MSTensor保存的数据支持的排列格式。 |
全局方法¶
方法名 | 描述 |
---|---|
StringToChar | 将std::string转换成std::vector<char>。 |
CharToString | 将std::vector<char>转换成std::string。 |
PairStringToChar | 将std::pair<std::string, int32_t>转换成std::pair<std::vector<char>, int32_t>。 |
PairCharToString | 将std::pair<std::vector<char>, int32_t>转换成std::pair<std::string, int32_t>。 |
VectorStringToChar | 将std::vector<std::string>转换成std::vector<std::vector<char>>。 |
VectorCharToString | 将std::vector<std::vector<char>>转换成std::vector<std::string>。 |
SetStringToChar | 将std::set<std::string>转换成std::set<std::vector<char>>。 |
SetCharToString | 将std::set<std::vector<char>>转换成std::set<std::string>。 |
MapStringToChar | 将std::map<std::string, int32_t>转换成std::map<std::vector<char>, int32_t>。 |
MapCharToString | 将std::map<std::vector<char>, int32_t>转换成std::map<std::string, int32_t>。 |
UnorderedMapStringToChar | 将std::unordered_map<std::string, std::string>转换成std::map<std::vector<char>, std::vector<char>>。 |
UnorderedMapCharToString | 将std::map<std::vector<char>, std::vector<char>>转换成std::unordered_map<std::string, std::string>。 |
ClassIndexStringToChar | 将std::vector<std::pair<std::string, std::vector<int32_t>>>转换成std::vector<std::pair<std::vector<char>, std::vector<int32_t>>>。 |
ClassIndexCharToString | 将std::vector<std::pair<std::vector<char>, std::vector<int32_t>>>转换成std::vector<std::pair<std::string, std::vector<int32_t>>>。 |
PairStringInt64ToPairCharInt64 | 将std::vector<std::pair<std::string, int64_t>>转换成std::vector<std::pair<std::vector<char>, int64_t>>。 |
PadInfoStringToChar | 将std::map<std::string, T>转换成std::map<std::vector<char>, T>。 |
PadInfoCharToString | 将std::map<std::vector<char>, T>转换成std::map<std::string, T>。 |
TensorMapCharToString | 将std::map<std::vector<char>, T>转换成std::unordered_map<std::string, T>。 |
Context¶
#include <context.h>
Context类用于保存执行中的环境变量。
构造函数和析构函数¶
Context();
~Context() = default;
公有成员函数¶
SetThreadNum¶
void SetThreadNum(int32_t thread_num);
设置运行时的线程数,该选项仅MindSpore Lite有效。
参数
thread_num
: 运行时的线程数。
SetThreadAffinity¶
void SetThreadAffinity(int mode);
设置运行时的CPU绑核策略,该选项仅MindSpore Lite有效。
参数
mode
: 绑核的模式,有效值为0-2,0为默认不绑核,1为绑大核,2为绑小核。
GetThreadAffinityMode¶
int GetThreadAffinityMode() const;
获取当前CPU绑核策略,该选项仅MindSpore Lite有效。
返回值
当前CPU绑核策略,有效值为0-2,0为默认不绑核,1为绑大核,2为绑小核。
SetThreadAffinity¶
void SetThreadAffinity(const std::vector<int> &core_list);
设置运行时的CPU绑核列表,该选项仅MindSpore Lite有效。如果SetThreadAffinity和SetThreadAffinity同时设置,core_list生效,mode不生效。
参数
core_list
: CPU绑核的列表。
GetThreadAffinityCoreList¶
std::vector<int32_t> GetThreadAffinityCoreList() const;
获取当前CPU绑核列表,该选项仅MindSpore Lite有效。
返回值
当前CPU绑核列表。
SetEnableParallel¶
void SetEnableParallel(bool is_parallel);
设置运行时是否支持并行,该选项仅MindSpore Lite有效。
参数
is_parallel
: bool量,为true则支持并行。
GetEnableParallel¶
bool GetEnableParallel() const;
获取当前是否支持并行,该选项仅MindSpore Lite有效。
返回值
返回值为为true,代表支持并行。
SetDelegate¶
void SetDelegate(const std::shared_ptr<Delegate> &delegate);
设置Delegate,Delegate定义了用于支持第三方AI框架接入的代理,该选项仅MindSpore Lite有效。
参数
delegate
: Delegate指针。
GetDelegate¶
std::shared_ptr<Delegate> GetDelegate() const;
获取当前Delegate,该选项仅MindSpore Lite有效。
返回值
当前Delegate的指针。
MutableDeviceInfo¶
std::vector<std::shared_ptr<DeviceInfoContext>> &MutableDeviceInfo();
修改该context下的DeviceInfoContext数组,仅MindSpore Lite支持数组中有多个成员是异构场景。
返回值
存储DeviceInfoContext的vector的引用。
DeviceInfoContext¶
#include <context.h>
DeviceInfoContext类定义不同硬件设备的环境信息。
构造函数和析构函数¶
DeviceInfoContext();
virtual ~DeviceInfoContext() = default;
公有成员函数¶
GetDeviceType¶
virtual enum DeviceType GetDeviceType() const = 0;
获取该DeviceInfoContext的类型。
返回值
该DeviceInfoContext的类型。
enum DeviceType { kCPU = 0, kGPU, kKirinNPU, kAscend910, kAscend310, // add new type here kInvalidDeviceType = 100, };
Cast¶
template <class T> std::shared_ptr<T> Cast();
在打开-fno-rtti
编译选项的情况下提供类似RTTI的功能,将DeviceInfoContext转换为T
类型的指针,若转换失败返回nullptr
。
返回值
转换后
T
类型的指针,若转换失败则为nullptr
。
SetAllocator¶
void SetAllocator(const std::shared_ptr<Allocator> &allocator);
设置内存管理器。
参数
allocator
: 内存管理器。
CPUDeviceInfo¶
#include <context.h>
派生自DeviceInfoContext,模型运行在CPU上的配置,仅MindSpore Lite支持该选项。
公有成员函数¶
函数 | 说明 |
---|---|
enum DeviceType GetDeviceType() const |
- 返回值: DeviceType::kCPU |
void SetEnableFP16(bool is_fp16) |
用于指定是否以FP16精度进行推理 - is_fp16 : 是否以FP16精度进行推理 |
bool GetEnableFP16() const |
- 返回值: 已配置的精度模式 |
GPUDeviceInfo¶
#include <context.h>
派生自DeviceInfoContext,模型运行在GPU上的配置,仅MindSpore Lite支持该选项。
公有成员函数¶
函数 | 说明 |
---|---|
enum DeviceType GetDeviceType() const |
- 返回值: DeviceType::kGPU |
void SetDeviceID(uint32_t device_id) |
用于指定设备ID - device_id : 设备ID |
uint32_t GetDeviceID() const |
- 返回值: 已配置的设备ID |
void SetPrecisionMode(const std::string &precision_mode) |
用于指定推理时算子精度 - precision_mode : 可选值origin (以模型中指定精度进行推理), fp16 (以FP16精度进行推理),默认值: origin |
std::string GetPrecisionMode() const |
- 返回值: 已配置的精度模式 |
int GetRankID() const |
- 返回值: 当前运行的RANK ID |
void SetEnableFP16(bool is_fp16) |
用于指定是否以FP16精度进行推理 - is_fp16 : 是否以FP16精度进行推理 |
bool GetEnableFP16() const |
- 返回值: 已配置的精度模式 |
void SetGLContext(void *gl_context) |
用于指定OpenGL EGLContext - *gl_context : 指向OpenGL EGLContext的指针 |
void *GetGLContext() const |
- 返回值: 已配置的指向OpenGL EGLContext的指针 |
void SetGLDisplay(void *gl_display) |
用于指定OpenGL EGLDisplay - *gl_display : 指向OpenGL EGLDisplay的指针 |
void *GetGLDisplay() const |
- 返回值: 已配置的指向OpenGL EGLDisplay的指针 |
KirinNPUDeviceInfo¶
#include <context.h>
派生自DeviceInfoContext,模型运行在NPU上的配置,仅MindSpore Lite支持该选项。
公有成员函数¶
函数 | 说明 |
---|---|
enum DeviceType GetDeviceType() const |
- 返回值: DeviceType::kGPU |
void SetFrequency(int frequency) |
用于指定NPU频率 - frequency : 设置为1(低功耗)、2(均衡)、3(高性能)、4(极致性能),默认为3 |
int GetFrequency() const |
- 返回值: 已配置的NPU频率模式 |
Ascend910DeviceInfo¶
#include <context.h>
派生自DeviceInfoContext,模型运行在Ascend910上的配置,MindSpore Lite不支持该选项。
公有成员函数¶
函数 | 说明 |
---|---|
void SetDeviceID(uint32_t device_id) |
用于指定设备ID - device_id : 设备ID |
uint32_t GetDeviceID() const |
- 返回值: 已配置的设备ID |
Ascend310DeviceInfo¶
#include <context.h>
派生自DeviceInfoContext,模型运行在Ascend310上的配置。
公有成员函数¶
函数 | 说明 |
---|---|
void SetDeviceID(uint32_t device_id) |
用于指定设备ID - device_id : 设备ID |
uint32_t GetDeviceID() const |
- 返回值: 已配置的设备ID |
void SetInsertOpConfigPath(const std::string &cfg_path) |
模型插入AIPP算子 - cfg_path : AIPP配置文件路径 |
std::string GetInsertOpConfigPath() |
- 返回值: 已配置的AIPP |
void SetInputFormat(const std::string &format) |
指定模型输入formatt - format : 可选有"NCHW" ,"NHWC" 等 |
std::string GetInputFormat() |
- 返回值: 已配置模型输入format |
void SetInputShape(const std::string &shape) |
指定模型输入shape - shape : 如"input_op_name1:1,2,3,4;input_op_name2:4,3,2,1" |
std::string GetInputShape() |
- 返回值: 已配置模型输入shape |
void SetOutputType(enum DataType output_type) |
指定模型输出type - output_type : 仅支持uint8、fp16和fp32 |
enum DataType GetOutputType() |
- 返回值: 已配置模型输出type |
void SetPrecisionMode(const std::string &precision_mode) |
配置模型精度模式 - precision_mode : 可选有"force_fp16" ,"allow_fp32_to_fp16" ,"must_keep_origin_dtype" 或者"allow_mix_precision" ,默认为"force_fp16" |
std::string GetPrecisionMode(t) |
- 返回值: 已配置模型精度模式 |
void SetOpSelectImplMode(const std::string &op_select_impl_mode) |
配置算子选择模式 - op_select_impl_mode : 可选有"high_performance" 和"high_precision" ,默认为"high_performance" |
std::string GetOpSelectImplMode() |
- 返回值: 已配置算子选择模式 |
Serialization¶
#include <serialization.h>
Serialization类汇总了模型文件读写的方法。
静态公有成员函数¶
Load¶
从文件加载模型。
Status Load(const std::string &file, ModelType model_type, Graph *graph, const Key &dec_key = {},
const std::string &dec_mode = kDecModeAesGcm);
参数
file
: 模型文件路径。model_type
: 模型文件类型,可选有ModelType::kMindIR
、ModelType::kMindIR_Lite
、ModelType::kOM
。MindSpore Lite支持ModelType::kMindIR
、ModelType::kMindIR_Lite
类型。graph
: 输出参数,保存图数据的对象。dec_key
: 解密密钥,用于解密密文模型,密钥长度为16、24或32。dec_mode
: 解密模式,可选有AES-GCM
、AES-CBC
。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Load¶
从多个文件加载多个模型,MindSpore Lite未提供此功能。
Status Load(const std::vector<std::string> &files, ModelType model_type, std::vector<Graph> *graphs,
const Key &dec_key = {}, const std::string &dec_mode = kDecModeAesGcm);
参数
files
: 多个模型文件路径,用vector存储。model_type
: 模型文件类型,可选有ModelType::kMindIR
、ModelType::kMindIR_Lite
、ModelType::kOM
。MindSpore Lite支持ModelType::kMindIR
、ModelType::kMindIR_Lite
类型。graphs
: 输出参数,依次保存图数据的对象。dec_key
: 解密密钥,用于解密密文模型,密钥长度为16、24或32。dec_mode
: 解密模式,可选有AES-GCM
、AES-CBC
。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Load¶
从内存缓冲区加载模型。
Status Load(const void *model_data, size_t data_size, ModelType model_type, Graph *graph,
const Key &dec_key = {}, const std::string &dec_mode = kDecModeAesGcm);
参数
model_data
:模型数据指针。data_size
:模型数据字节数。model_type
: 模型文件类型,可选有ModelType::kMindIR
、ModelType::kMindIR_Lite
、ModelType::kOM
。MindSpore Lite支持ModelType::kMindIR
、ModelType::kMindIR_Lite
类型。graph
:输出参数,保存图数据的对象。dec_key
: 解密密钥,用于解密密文模型,密钥长度为16、24或32。dec_mode
: 解密模式,可选有AES-GCM
、AES-CBC
。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
SetParameters¶
配置模型参数。
static Status SetParameters(const std::map<std::string, Buffer> ¶meters, Model *model);
参数
parameters
:参数。model
:模型。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
ExportModel¶
导出训练模型,MindSpore Lite训练使用。
static Status ExportModel(const Model &model, ModelType model_type, Buffer *model_data);
参数
model
:模型数据。model_type
:模型文件类型。model_data
:模型参数数据。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
ExportModel¶
导出训练模型,MindSpore Lite训练使用。
static Status ExportModel(const Model &model, ModelType model_type, const std::string &model_file,
QuantizationType quantization_type = kNoQuant, bool export_inference_only = true,
std::vector<std::string> output_tensor_name = {});
参数
model
:模型数据。model_type
:模型文件类型。model_file
:保存的模型文件。quantization_type
: 量化类型。export_inference_only
: 是否导出只做推理的模型。output_tensor_name
: 设置导出的推理模型的输出张量的名称,默认为空,导出完整的推理模型。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Buffer¶
#include <types.h>
Buffer定义了MindSpore中Buffer数据的结构。
构造函数和析构函数¶
Buffer();
Buffer(const void *data, size_t data_len);
~Buffer();
Model¶
#include <model.h>
Model定义了MindSpore中的模型,便于计算图管理。
构造函数和析构函数¶
Model();
~Model();
公有成员函数¶
Build¶
Status Build(GraphCell graph, const std::shared_ptr<Context> &model_context = nullptr,
const std::shared_ptr<TrainCfg> &train_cfg = nullptr);
将GraphCell存储的模型编译至可在Device上运行的状态。
Build¶
Status Build(const void *model_data, size_t data_size, ModelType model_type,
const std::shared_ptr<Context> &model_context = nullptr);
从内存缓冲区加载模型,并将模型编译至可在Device上运行的状态。
参数
model_data
: 指向存储读入模型文件缓冲区的指针。data_size
: 缓冲区大小。model_type
: 模型文件类型,可选有ModelType::kMindIR
、ModelType::kMindIR_Lite
、ModelType::kOM
。MindSpore Lite支持ModelType::kMindIR
、ModelType::kMindIR_Lite
类型。model_context
: 模型Context。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Build¶
Status Build(const void *model_data, size_t data_size, ModelType model_type,
const std::shared_ptr<Context> &model_context = nullptr, const Key &dec_key = {},
const std::string &dec_mode = kDecModeAesGcm, const std::string &cropto_lib_path);
从内存缓冲区加载模型,并将模型编译至可在Device上运行的状态。
参数
model_data
: 指向存储读入模型文件缓冲区的指针。data_size
: 缓冲区大小。model_type
: 模型文件类型,可选有ModelType::kMindIR
、ModelType::kMindIR_Lite
、ModelType::kOM
。MindSpore Lite支持ModelType::kMindIR
、ModelType::kMindIR_Lite
类型。model_context
: 模型Context。dec_key
: 解密密钥,用于解密密文模型,密钥长度为16。dec_mode
: 解密模式,可选有AES-GCM
。cropto_lib_path
: OpenSSL Crypto解密库路径。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Build¶
Status Build(const std::string &model_path, ModelType model_type,
const std::shared_ptr<Context> &model_context = nullptr);
根据路径读取加载模型,并将模型编译至可在Device上运行的状态。
参数
model_path
: 模型文件路径。model_type
: 模型文件类型,可选有ModelType::kMindIR
、ModelType::kMindIR_Lite
、ModelType::kOM
。MindSpore Lite支持ModelType::kMindIR
、ModelType::kMindIR_Lite
类型。model_context
: 模型Context。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Build¶
Status Build(const std::string &model_path, ModelType model_type,
const std::shared_ptr<Context> &model_context = nullptr, const Key &dec_key = {},
const std::string &dec_mode = kDecModeAesGcm, const std::string &cropto_lib_path);
根据路径读取加载模型,并将模型编译至可在Device上运行的状态。
参数
model_path
: 模型文件路径。model_type
: 模型文件类型,可选有ModelType::kMindIR
、ModelType::kMindIR_Lite
、ModelType::kOM
。MindSpore Lite支持ModelType::kMindIR
、ModelType::kMindIR_Lite
类型。model_context
: 模型Context。dec_key
: 解密密钥,用于解密密文模型,密钥长度为16。dec_mode
: 解密模式,可选有AES-GCM
。cropto_lib_path
: OpenSSL Crypto解密库路径。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Build
之后对model_context
的其他修改不再生效。
Predict¶
Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs, const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr)
推理模型。
参数
inputs
: 模型输入按顺序排列的vector
。outputs
: 输出参数,按顺序排列的vector
的指针,模型输出会按顺序填入该容器。before
: 一个MSKernelCallBack 结构体。定义了运行每个节点之前调用的回调函数。after
: 一个MSKernelCallBack 结构体。定义了运行每个节点之后调用的回调函数。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
LoadConfig¶
Status LoadConfig(const std::string &config_path);
根据路径读取配置文件。
参数
config_path
: 配置文件路径。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
用户可以调用
LoadConfig
接口进行混合精度推理的设置,配置文件举例如下:[execution_plan]
op_name1=data_type:float16
op_name2=data_type:float32
UpdateConfig¶
Status UpdateConfig(const std::string §ion, const std::pair<std::string, std::string> &config);
刷新配置,读文件相对比较费时,如果少部分配置发生变化可以通过该接口更新部分配置。
参数
section
: 配置的章节名。config
: 要更新的配置对。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
GetInputByTensorName¶
MSTensor GetInputByTensorName(const std::string &tensor_name);
获取模型指定名字的输入张量。
返回值
指定名字的输入张量,如果该名字不存在则返回非法张量。
ApplyGradients¶
Status ApplyGradients(const std::vector<MSTensor> &gradients);
应用所有Tensor的梯度。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
GetOptimizerParams¶
std::vector<MSTensor> GetOptimizerParams() const;
获取optimizer参数MSTensor。
返回值
所有optimizer参数MSTensor。
SetOptimizerParams¶
Status SetOptimizerParams(const std::vector<MSTensor> ¶ms);
更新optimizer参数。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
GetOutputTensorNames¶
std::vector<std::string> GetOutputTensorNames();
获取模型所有输出张量的名字。
返回值
包含模型所有输出张量名字的容器类型变量。
GetOutputByTensorName¶
MSTensor GetOutputByTensorName(const std::string &tensor_name);
获取模型指定名字的输出张量。
返回值
指定名字的输出张量,如果该名字不存在则返回非法张量。
GetOutputsByNodeName¶
std::vector<MSTensor> GetOutputsByNodeName(const std::string &node_name);
通过节点名获取模型的MSTensors输出张量。不建议使用,将在2.0版本废弃。
参数
node_name
: 节点名称。
返回值
包含在模型输出Tensor中的该节点输出Tensor的vector。
BindGLTexture2DMemory¶
Status BindGLTexture2DMemory(const std::map<std::string, unsigned int> &inputGLTexture,
std::map<std::string, unsigned int> *outputGLTexture);
将OpenGL纹理数据与模型的输入和输出进行绑定。
参数
inputGLTexture
: 模型输入的OpenGL纹理数据, key为输入Tensor的名称,value为OpenGL纹理。outputGLTexture
: 模型输出的OpenGL纹理数据,key为输出Tensor的名称,value为OpenGL纹理。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
InitMetrics¶
Status InitMetrics(std::vector<Metrics *> metrics);
训练指标参数初始化。
参数
metrics
: 训练指标参数。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
SetTrainMode¶
Status SetTrainMode(bool train);
session设置训练模式。
参数
train
: 是否为训练模式。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Train¶
Status Train(int epochs, std::shared_ptr<dataset::Dataset> ds, std::vector<TrainCallBack *> cbs);
模型训练。
参数
epochs
: 迭代轮数。ds
: 训练数据。cbs
: 包含训练回调类对象的vector
。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Evaluate¶
Status Evaluate(std::shared_ptr<dataset::Dataset> ds, std::vector<TrainCallBack *> cbs);
模型验证。
参数
ds
: 训练数据。cbs
: 包含训练回调类对象的vector
。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Resize¶
Status Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims);
调整已编译模型的输入形状。
参数
inputs
: 模型输入按顺序排列的vector
。dims
: 输入形状,按输入顺序排列的由形状组成的vector
,模型会按顺序依次调整张量形状。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
CheckModelSupport¶
static bool CheckModelSupport(enum DeviceType device_type, ModelType model_type);
检查设备是否支持该模型。
参数
device_type
: 设备类型,例如kMaliGPU
。model_type
: 模型类型,例如MindIR
。
返回值
状态码。
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();
注意:MSTensor构造时,若data指针通过malloc生成,用户在构造完成MSTensor后,需自行释放free,否则存在内存泄露。
静态公有成员函数¶
CreateTensor¶
MSTensor *CreateTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape,
const void *data, size_t data_len) noexcept;
创建一个MSTensor
对象,其数据需复制后才能由Model
访问,必须与DestroyTensorPtr
成对使用。
参数
name
: 名称。type
:数据类型。shape
:形状。data
:数据指针,指向一段已开辟的内存。data_len
:数据长度,以字节为单位。
返回值
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
指针。
CreateDevTensor¶
static inline MSTensor *CreateDevTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape,
const void *data, size_t data_len) noexcept;
创建一个MSTensor
对象,其device数据可以直接由Model
访问,必须与DestroyTensorPtr
成对使用。
参数
name
: 名称。type
:数据类型。shape
:形状。data
:数据指针,指向一段已开辟的device内存。data_len
:数据长度,以字节为单位。
返回值
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
返回的指针。
公有成员函数¶
MutableData¶
void *MutableData();
获取MSTensor
中的数据的指针。如果为空指针,为MSTensor
的数据申请内存,并返回申请内存的地址,如果不为空,返回数据的指针。
返回值
指向
MSTensor
中的数据的指针。
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相等,仅MindSpore Lite支持。
返回值
MSTensor
是否与另一个MSTensor相等。
SetShape¶
void SetShape(const std::vector<int64_t> &shape);
设置MSTensor
的Shape,仅MindSpore Lite支持,目前在Delegate机制使用。
SetDataType¶
void SetDataType(enum DataType data_type);
设置MSTensor
的DataType,仅MindSpore Lite支持,目前在Delegate机制使用。
SetTensorName¶
void SetTensorName(const std::string &name);
设置MSTensor
的名字,仅MindSpore Lite支持,目前在Delegate机制使用。
SetAllocator¶
void SetAllocator(std::shared_ptr<Allocator> allocator);
设置MSTensor
数据所属的内存池,仅MindSpore Lite支持。
参数
model
: 指向Allocator的指针。
allocator¶
std::shared_ptr<Allocator> allocator() const;
获取MSTensor
数据所属的内存池,仅MindSpore Lite支持。
返回值
指向Allocator的指针。
SetFormat¶
void SetFormat(mindspore::Format format);
设置MSTensor
数据的format,仅MindSpore Lite支持,目前在Delegate机制使用。
QuantParams¶
std::vector<QuantParam> QuantParams() const;
获取MSTensor
的量化参数,仅MindSpore Lite支持,目前在Delegate机制使用。
SetQuantParams¶
void SetQuantParams(std::vector<QuantParam> quant_params);
设置MSTensor
的量化参数,仅MindSpore Lite支持,目前在Delegate机制使用。
PadInfo¶
const std::vector<std::vector<int64_t>> &PadInfo() const;
获取MSTensor
的Pad信息,仅MindSpore Lite支持,目前在Delegate机制使用。
SetPadInfo¶
void SetPadInfo(const std::vector<std::vector<int64_t>> &pad_info);
设置MSTensor
的Pad信息,仅MindSpore Lite支持,目前在Delegate机制使用。
RawDataShape¶
const std::vector<int64_t> &RawDataShape() const;
获取MSTensor
的RawDataShape,仅MindSpore Lite支持,目前在Delegate机制使用。
MSKernelCallBack¶
#include <types.h>
using MSKernelCallBack = std::function<bool(const std::vector<MSTensor> &inputs, const std::vector<MSTensor> &outputs, const MSCallBackParam &opInfo)>
一个函数包装器。MSKernelCallBack 定义了指向回调函数的指针。
Delegate¶
#include <delegate.h>
Delegate
定义了第三方AI框架接入MindSpore Lite的代理接口。
构造函数和析构函数¶
Delegate() = default;
virtual ~Delegate() = default;
公有成员函数¶
Init¶
virtual Status Init() = 0;
初始化Delegate资源。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Build¶
virtual Status Build(DelegateModel *model) = 0;
Delegate在线构图。
参数
model
: 指向存储DelegateModel实例的指针。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
SchemaVersion¶
#include <delegate.h>
定义了Lite执行在线推理时模型文件的版本。
typedef enum {
SCHEMA_INVALID = -1, /**< invalid version */
SCHEMA_CUR, /**< current version for ms model defined in model.fbs*/
SCHEMA_V0, /**< previous version for ms model defined in model_v0.fbs*/
} SchemaVersion;
KernelIter¶
#include <delegate.h>
定义了Lite Kernel列表的迭代器。
using KernelIter = std::vector<kernel::Kernel *>::iterator;
DelegateModel¶
#include <delegate.h>
DelegateModel
定义了MindSpore Lite Delegate机制操作的的模型对象。
构造函数¶
DelegateModel(std::vector<kernel::Kernel *> *kernels, const std::vector<MSTensor> &inputs,
const std::vector<MSTensor> &outputs,
const std::map<kernel::Kernel *, const schema::Primitive *> &primitives, SchemaVersion version);
析构函数¶
~DelegateModel() = default;
保护成员¶
公有成员函数¶
GetPrimitive¶
const schema::Primitive *GetPrimitive(kernel::Kernel *kernel) const;
获取一个Kernel的属性值。
参数
kernel
: 指向Kernel的指针。
返回值
const schema::Primitive *,输入参数Kernel对应的该算子的属性值。
BeginKernelIterator¶
KernelIter BeginKernelIterator();
返回DelegateModel Kernel列表起始元素的迭代器。
返回值
KernelIter,指向DelegateModel Kernel列表起始元素的迭代器。
EndKernelIterator¶
KernelIter EndKernelIterator();
返回DelegateModel Kernel列表末尾元素的迭代器。
返回值
KernelIter,指向DelegateModel Kernel列表末尾元素的迭代器。
Replace¶
KernelIter Replace(KernelIter from, KernelIter end, kernel::Kernel *graph_kernel);
用Delegate子图Kernel替换Delegate支持的连续Kernel列表。
参数
from
: Delegate支持的连续Kernel列表的起始元素迭代器。end
: Delegate支持的连续Kernel列表的末尾元素迭代器。graph_kernel
: 指向Delegate子图Kernel实例的指针。
返回值
KernelIter,用Delegate子图Kernel替换之后,子图Kernel下一个元素的迭代器,指向下一个未被访问的Kernel。
inputs¶
const std::vector<mindspore::MSTensor> &inputs();
返回DelegateModel输入tensor列表。
返回值
MSTensor的列表。
outputs¶
const std::vector<mindspore::MSTensor> &outputs();
返回DelegateModel输出tensor列表。
返回值
MSTensor的列表。
GetVersion¶
const SchemaVersion GetVersion() { return version_; }
返回当前执行推理的模型文件的版本。
返回值
enum值,0: r1.2及r1.2之后的版本,1: r1.1及r1.1之前的版本,-1: 无效版本。
TrainCfg¶
#include <cfg.h>
TrainCfg
MindSpore Lite训练的相关配置参数。
构造函数¶
TrainCfg() { this->loss_name_ = "_loss_fn"; }
公有成员变量¶
OptimizationLevel optimization_level_ = kO0;
优化的数据类型。
enum OptimizationLevel : uint32_t {
kO0 = 0,
kO2 = 2,
kO3 = 3,
kAuto = 4,
kOptimizationType = 0xFFFFFFFF
};
std::string loss_name_;
损失节点的名称。
MixPrecisionCfg mix_precision_cfg_;
混合精度配置。
bool accumulate_gradients_;
是否累积梯度。
MixPrecisionCfg¶
#include <cfg.h>
MixPrecisionCfg
MindSpore Lite训练混合精度配置类。
构造函数¶
MixPrecisionCfg() {
dynamic_loss_scale_ = false;
loss_scale_ = 128.0f;
num_of_not_nan_iter_th_ = 1000;
}
共有成员变量¶
bool dynamic_loss_scale_ = false;
混合精度训练中是否启用动态损失比例。
float loss_scale_;
初始损失比例。
uint32_t num_of_not_nan_iter_th_;
动态损失阈值。
bool is_raw_mix_precision_;
原始模型是否是原生混合精度模型。
AccuracyMetrics¶
#include <accuracy.h>
AccuracyMetrics
MindSpore Lite训练精度类。
构造函数和析构函数¶
explicit AccuracyMetrics(int accuracy_metrics = METRICS_CLASSIFICATION, const std::vector<int> &input_indexes = {1}, const std::vector<int> &output_indexes = {0});
virtual ~AccuracyMetrics();
TrainCallBack¶
#include <callback.h>
Metrics
MindSpore Lite训练回调类。
析构函数¶
virtual ~TrainCallBack() = default;
CkptSaver¶
#include <ckpt_saver.h>
Metrics
MindSpore Lite训练模型文件保存类。
构造函数和析构函数¶
explicit CkptSaver(int save_every_n, const std::string &filename_prefix);
virtual ~CkptSaver();
LossMonitor¶
#include <loss_monitor.h>
Metrics
MindSpore Lite训练损失函数类。
构造函数和析构函数¶
explicit LossMonitor(int print_every_n_steps = INT_MAX);
virtual ~LossMonitor();
LRScheduler¶
#include <lr_scheduler.h>
Metrics
MindSpore Lite训练学习率调度类。
构造函数和析构函数¶
explicit LRScheduler(LR_Lambda lambda_func, void *lr_cb_data = nullptr, int step = 1);
virtual ~LRScheduler();
MultiplicativeLRLambda¶
#include <lr_scheduler.h>
每个epoch将学习率乘以一个因子。
using LR_Lambda = std::function<int(float *lr, int epoch, void *cb_data)>;
int MultiplicativeLRLambda(float *lr, int epoch, void *multiplication);
学习率更新。
参数
lr
: 学习率。epoch
: 迭代轮数。multiplication
: 更新方式。
返回值
int类型返回值,表示是否更新,DONT_UPDATE_LR为0表示不更新,UPDATE_LR为1表示更新。
constexpr int DONT_UPDATE_LR = 0; constexpr int UPDATE_LR = 1;
TimeMonitor¶
#include <time_monitor.h>
Metrics
MindSpore Lite训练时间监测类。
析构函数¶
virtual ~TimeMonitor() = default;
TrainAccuracy¶
#include <train_accuracy.h>
Metrics
MindSpore Lite训练学习率调度类。
构造函数和析构函数¶
explicit TrainAccuracy(int print_every_n = INT_MAX, int accuracy_metrics = METRICS_CLASSIFICATION, const std::vector<int> &input_indexes = {1}, const std::vector<int> &output_indexes = {0});
virtual ~TrainAccuracy();
参数
print_every_n
: 间隔print_every_n步打印一次。accuracy_metrics
: 精度指标,默认值为METRICS_CLASSIFICATION表示0,METRICS_MULTILABEL表示1。input_indexes
: 输入索引。output_indexes
: 输出索引。
constexpr int METRICS_CLASSIFICATION = 0;
constexpr int METRICS_MULTILABEL = 1;
GetAccuracyPoints¶
const std::vector<GraphPoint> &GetAccuracyPoints();
获取训练精度。
返回值
包含
GraphPoint
的vector
,训练精度数据。
CharVersion¶
#include <types.h>
std::vector<char> CharVersion();
全局方法,用于获取版本的字符vector。
返回值
MindSpore Lite版本的字符vector。
Allocator¶
#include <allocator.h>
内存管理基类。
公有成员函数¶
Malloc¶
virtual void *Malloc(size_t size)
内存分配。
参数
size
: 要分配的内存大小,单位为Byte。
virtual void *Malloc(size_t weight, size_t height, DataType type)
Image格式内存分配。
参数
weight
: 要分配的Image格式内存的宽度。height
: 要分配的Image格式内存的高度。type
: 要分配的Image格式内存的数据类型。
SetRefCount¶
virtual int SetRefCount(void *ptr, int ref_count)
设置分配内存的引用计数。
参数
ptr
: 要操作的内存地址,该值由Malloc分配。ref_count
: 引用计数值。
DecRefCount¶
virtual int DecRefCount(void *ptr, int ref_count)
分配的内存引用计数减一。
参数
ptr
: 要操作的内存地址,该值由Malloc分配。ref_count
: 引用计数值。
Status¶
#include <status.h>
构造函数和析构函数¶
Status();
inline Status(enum StatusCode status_code, const std::string &status_msg = "");
inline Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra = "");
~Status() = default;
SetErrDescription¶
inline std::string SetErrDescription(const std::string &err_description);
配置错误描述字符串。
参数
err_description
: 错误描述字符串。
返回值
状态信息字符串。
operator<<(std::ostream &os, const Status &s)¶
friend std::ostream &operator<<(std::ostream &os, const Status &s);
状态信息写到输出流。
参数
os
: 输出流。s
: 状态类。
返回值
输出流。
operator==(const Status &other)¶
bool operator==(const Status &other) const;
判断是否与另一个Status相等。
参数
other
: 另一个Status。
返回值
是否与另一个Status相等。
operator==(enum StatusCode other_code)¶
bool operator==(enum StatusCode other_code) const;
判断是否与一个StatusCode相等。
参数
other_code
: 一个StatusCode。
返回值
是否与一个StatusCode相等。
operator!=(enum StatusCode other_code)¶
bool operator!=(enum StatusCode other_code) const;
判断是否与一个StatusCode不等。
参数
other_code
: 一个StatusCode。
返回值
是否与一个StatusCode不等。
CodeAsString¶
static inline std::string CodeAsString(enum StatusCode c);
获取StatusCode对应的字符串。
参数
c
: 状态码枚举值。
返回值
状态码对应的字符串。
Graph¶
#include <graph.h>
构造函数和析构函数¶
Graph();
explicit Graph(const std::shared_ptr<GraphData> &graph_data);
explicit Graph(std::shared_ptr<GraphData> &&graph_data);
explicit Graph(std::nullptr_t);
~Graph();
参数
graph_data
: 输出通道数。
GraphCell¶
#include <cell.h>
构造函数和析构函数¶
GraphCell() = default;
~GraphCell() override = default;
explicit GraphCell(const Graph &);
explicit GraphCell(Graph &&);
explicit GraphCell(const std::shared_ptr<Graph> &);
RunnerConfig¶
#include <model_parallel_runner.h>
RunnerConfig定义了ModelParallelRunner中使用的配置选项参数。
构造函数和析构函数¶
RunnerConfig();
~RunnerConfig();
公有成员函数¶
SetWorkersNum¶
void SetWorkersNum(int32_t workers_num);
设置RunnerConfig的worker的个数。
参数
workers_num
: worker的数量。
GetWorkersNum¶
int32_t GetWorkersNum() const;
获取RunnerConfig配置的上下文参数。
返回值
RunnerConfig类中配置的worker数量。
SetContext¶
void SetContext(const std::shared_ptr<Context> &context);
设置RunnerConfig的context参数。
参数
context
: worker上下文配置。
GetContext¶
std::shared_ptr<Context> GetContext() const;
获取RunnerConfig配置的上下文参数。
返回值
上下文配置类
Context
对象。
ModelParallelRunner¶
#include <model_parallel_runner.h>
ModelParallelRunner定义了MindSpore的多个Model以及并发策略,便于多个Model的调度与管理。
构造函数和析构函数¶
ModelParallelRunner();
~ModelParallelRunner();
公有成员函数¶
Init¶
Status Init(const std::string &model_path, const std::shared_ptr<RunnerConfig> &runner_config = nullptr);
根据路径读取加载模型,生成一个或者多个模型,并将所有模型编译至可在Device上运行的状态。
参数
model_path
: 模型文件路径。runner_config
: 一个RunnerConfig类。定义了并发推理模型的配置参数。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Predict¶
Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs,
const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr);
并发推理模型。
参数
inputs
: 模型输入按顺序排列的vector
。outputs
: 输出参数,按顺序排列的vector
的指针,模型输出会按顺序填入该容器。before
: 一个MSKernelCallBack 结构体。定义了运行每个节点之前调用的回调函数。after
: 一个MSKernelCallBack 结构体。定义了运行每个节点之后调用的回调函数。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。