mindspore¶
接口汇总¶
运行环境配置¶
类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
Context | 保存执行中的环境变量。 | √ | √ |
DeviceInfoContext | 不同硬件设备的环境信息。 | √ | √ |
CPUDeviceInfo | 模型运行在CPU上的配置。 | √ | √ |
GPUDeviceInfo | 模型运行在GPU上的配置。 | √ | √ |
KirinNPUDeviceInfo | 模型运行在NPU上的配置。 | ✕ | √ |
AscendDeviceInfo | 模型运行在Ascend 310/310P上的配置。 | √ | √ |
并发推理¶
类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
RunnerConfig | 模型并发推理配置参数。 | √ | ✕ |
ModelParallelRunner | 模型并发推理类。 | √ | ✕ |
张量Tensor相关¶
类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
MSTensor | MindSpore中的张量。 | √ | √ |
QuantParam | MSTensor中的一组量化参数。 | √ | √ |
mindspore::DataType | MindSpore MSTensor保存的数据支持的类型。 | √ | √ |
mindspore::Format | MindSpore MSTensor保存的数据支持的排列格式。 | √ | √ |
Allocator | 内存管理基类。 | √ | √ |
序列化保存与加载¶
类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
Serialization | 汇总了模型文件读写的方法。 | √ | √ |
Buffer | Buff数据类。 | √ | √ |
版本查询¶
类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
SchemaVersion | MindSpore Lite 执行推理时,模型文件的版本。 | ✕ | √ |
CharVersion | 获取字符vector形式的当前版本号。 | ✕ | √ |
Version | 获取字符串形式的当前版本号。 | ✕ | √ |
回调函数¶
类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
MSKernelCallBack | MindSpore回调函数包装器。 | √ | √ |
MSCallBackParam | MindSpore回调函数的参数。 | √ | √ |
Mindspore Lite 训练配置¶
类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
TrainCfg | MindSpore Lite训练配置类。 | ✕ | √ |
MixPrecisionCfg | MindSpore Lite训练混合精度配置类。 | ✕ | √ |
AccuracyMetrics | MindSpore Lite训练精度类。 | ✕ | √ |
Metrics | MindSpore Lite训练指标类。 | ✕ | √ |
TrainCallBack | MindSpore Lite训练回调类。 | ✕ | √ |
TrainCallBackData | 定义了训练回调的一组参数。 | ✕ | √ |
CkptSaver | MindSpore Lite训练模型文件保存类。 | ✕ | √ |
LossMonitor | MindSpore Lite训练学习率调度类。 | ✕ | √ |
LRScheduler | MindSpore Lite训练配置类。 | ✕ | √ |
StepLRLambda | MindSpore Lite训练学习率的一组参数。 | ✕ | √ |
MultiplicativeLRLambda | 每个epoch将学习率乘以一个因子。 | ✕ | √ |
TimeMonitor | MindSpore Lite训练时间监测类。 | ✕ | √ |
TrainAccuracy | MindSpore Lite训练学习率调度类。 | ✕ | √ |
Delegate三方框架接入机制¶
类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
Delegate | MindSpore Lite接入第三方AI框架的代理。 | ✕ | √ |
DelegateModel | MindSpore Lite Delegate机制封装的模型。 | ✕ | √ |
KernelIter | MindSpore Lite 算子列表的迭代器。 | ✕ | √ |
CoreMLDelegate | MindSpore Lite接入CoreML框架的代理。 | ✕ | √ |
Context¶
#include <context.h>
Context类用于保存执行中的环境变量。
构造函数和析构函数¶
Context()
~Context() = default;
公有成员函数¶
SetInterOpParallelNum¶
void SetInterOpParallelNum(int32_t parallel_num)
设置运行时的算子并行推理数目。
参数
parallel_num
: 运行时的算子并行数。
SetThreadAffinity¶
void SetThreadAffinity(int mode)
设置运行时的CPU绑核策略。
参数
mode
: 绑核的模式,有效值为0-2,0为默认不绑核,1为绑大核,2为绑中核。
GetThreadAffinityMode¶
int GetThreadAffinityMode() const;
获取当前CPU绑核策略。
返回值
当前CPU绑核策略,有效值为0-2,0为默认不绑核,1为绑大核,2为绑中核。
SetThreadAffinity¶
void SetThreadAffinity(const std::vector<int> &core_list)
设置运行时的CPU绑核列表。如果SetThreadAffinity和SetThreadAffinity同时设置,core_list生效,mode不生效。
参数
core_list
: CPU绑核的列表。
GetThreadAffinityCoreList¶
std::vector<int32_t> GetThreadAffinityCoreList() const;
获取当前CPU绑核列表。
返回值
当前CPU绑核列表。
SetEnableParallel¶
void SetEnableParallel(bool is_parallel)
设置运行时是否支持并行。
参数
is_parallel
: bool量,为true则支持并行。
SetBuiltInDelegate¶
void SetBuiltInDelegate(DelegateMode mode);
设置内置Delegate模式,以使用第三方AI框架辅助推理。
参数
mode
: 内置Delegate模式,可选配置选项kNoDelegate
、kCoreML
、kNNAPI
。kNoDelegate
表示不使用第三方AI框架辅助推理,kCoreML
表示使用CoreMI进行推理(在iOS上可选),kNNAPI
表示使用NNAPI进行推理(在Android上可选)。
GetBuiltInDelegate¶
DelegateMode GetBuiltInDelegate() const;
获取当前内置Delegate模式。
返回值
返回当前内置Delegate模式。
SetDelegate¶
void SetDelegate(const std::shared_ptr<Delegate> &delegate)
设置Delegate,Delegate定义了用于支持第三方AI框架接入的代理。
参数
delegate
: Delegate指针。
set_delegate¶
void set_delegate(const std::shared_ptr<AbstractDelegate> &delegate)
设置Delegate,Delegate定义了用于支持第三方AI框架接入的代理。
参数
delegate
: Delegate指针。
get_delegate¶
std::shared_ptr<AbstractDelegate> get_delegate() const;
获取当前Delegate。
返回值
当前Delegate的指针。
SetMultiModalHW¶
void SetMultiModalHW(bool float_mode);
在多设备中,配置量化模型是否以浮点模式运行。
参数
float_mode
: 是否以浮点模式运行。
MutableDeviceInfo¶
std::vector<std::shared_ptr<DeviceInfoContext>> &MutableDeviceInfo()
修改该context下的DeviceInfoContext数组,仅端侧推理支持数组中有多个成员是异构场景。
返回值
存储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上的配置。
公有成员函数¶
函数 | 说明 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
enum DeviceType GetDeviceType() const |
- 返回值: DeviceType::kCPU | √ | √ |
void SetEnableFP16(bool is_fp16) |
用于指定是否以FP16精度进行推理 - is_fp16 : 是否以FP16精度进行推理 |
√ | √ |
bool GetEnableFP16() const |
- 返回值: 已配置的精度模式 | √ | √ |
GPUDeviceInfo¶
#include <context.h>
派生自DeviceInfoContext,模型运行在GPU上的配置。
公有成员函数¶
函数 | 说明 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
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 | √ | √ |
int GetGroupSize() const |
- 返回值: 当前运行的GROUP SIZE | √ | √ |
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的指针 | ✕ | √ |
void SetEnableGLTexture(bool is_enable_gl_texture) |
用于指定是否绑定OpenGL纹理数据 - is_enable_gl_texture : 是否在推理时绑定OpenGL纹理数据 |
✕ | √ |
bool GetEnableGLTexture() const |
- 返回值: 已配置的绑定OpenGL纹理数据模式 | ✕ | √ |
KirinNPUDeviceInfo¶
#include <context.h>
派生自DeviceInfoContext,模型运行在NPU上的配置。
公有成员函数¶
函数 | 说明 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
enum DeviceType GetDeviceType() const |
- 返回值: DeviceType::kKirinNPU | √ | √ |
void SetFrequency(int frequency) |
用于指定NPU频率 - frequency : 设置为1(低功耗)、2(均衡)、3(高性能)、4(极致性能),默认为3 |
✕ | √ |
int GetFrequency() const |
- 返回值: 已配置的NPU频率模式 | ✕ | √ |
void SetEnableFP16(bool is_fp16) |
用于指定是否以FP16精度进行推理 - is_fp16 : 是否以FP16精度进行推理 |
✕ | √ |
bool GetEnableFP16() const |
- 返回值: 已配置的精度模式 | ✕ | √ |
AscendDeviceInfo¶
#include <context.h>
派生自DeviceInfoContext,模型运行在Ascend 310、Ascend 310P上的配置。
公有成员函数¶
函数 | 说明 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|---|
enum DeviceType GetDeviceType() const |
- 返回值: DeviceType::kAscend | √ | √ |
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) |
指定模型输入format - format : 可选有"NCHW" ,"NHWC" ,"ND" |
√ | √ |
std::string GetInputFormat() |
- 返回值: 已配置模型输入format | √ | √ |
void SetInputShape(const std::string &shape) |
指定模型输入shape,为字符串形式,需指定输入名称,每个shape值由, 隔开,不同输入由; 隔开- shape : 如"input_op_name1:1,2,3,4;input_op_name2:4,3,2,1" |
√ | √ |
std::string GetInputShape() |
- 返回值: 已配置模型输入shape | √ | √ |
void SetInputShapeMap(const std::map<int, std::vector<int>> &shape) |
指定模型输入shape - shape : map的key对应输入的下标,例如第一个输入对应下标0,第二个对应下标1。value对应输入shape,为数组形式。 |
√ | √ |
std::map<int, std::vector<int>> GetInputShapeMap() |
- 返回值: 已配置模型输入shape | √ | √ |
void SetDynamicBatchSize(const std::vector<size_t> &dynamic_batch_size) |
指定模型动态batch的挡位,支持个数范围[2, 100],为数组形式。 - dynamic_batch_size : 如{1, 2} 。 |
√ | √ |
std::string GetDynamicBatchSize() |
- 返回值: 已配置模型的动态batch | √ | √ |
void SetDynamicImageSize(const std::string &dynamic_image_size) |
指定模型动态分辨率的挡位,支持个数范围[2, 100],为字符串形式,每个shape值由, 隔开,不同输入由; 隔开。- dynamic_image_size : 如"64,64;128,128" 。 |
√ | √ |
std::string GetDynamicImageSize() |
- 返回值: 已配置模型的动态分辨率 | √ | √ |
void SetOutputType(enum DataType output_type) |
指定模型输出type - output_type : 仅支持uint8、fp16和fp32 |
√ | √ |
enum DataType GetOutputType() |
- 返回值: 已配置模型的输出类型 | √ | √ |
void SetPrecisionMode(const std::string &precision_mode) |
配置模型精度模式 - precision_mode : 可选有"enforce_fp16" ,"preferred_fp32" ,"enforce_origin" ,"enforce_fp32" 或者"preferred_optimal" ,默认为"enforce_fp16" |
√ | √ |
std::string GetPrecisionMode() |
- 返回值: 已配置模型的精度模式 | √ | √ |
void SetOpSelectImplMode(const std::string &op_select_impl_mode) |
配置算子实现方式 - op_select_impl_mode : 可选有"high_performance" 和"high_precision" ,默认为"high_performance" |
√ | √ |
std::string GetOpSelectImplMode() |
- 返回值: 已配置的算子选择模式 | √ | √ |
void SetFusionSwitchConfigPath(const std::string &cfg_path) |
配置融合开关 - cfg_path : 融合开关配置文件,可指定关闭特定融合规则 |
√ | √ |
std::string GetFusionSwitchConfigPath() |
- 返回值: 已配置的融合开关文件路径 | √ | √ |
void SetBufferOptimizeMode(const std::string &buffer_optimize_mode) |
配置缓存优化模式 - buffer_optimize_mode : 可选有l1_optimize ,l2_optimize 或off_optimize ,默认为l2_optimize |
√ | √ |
std::string GetBufferOptimizeMode() |
- 返回值: 已配置的缓存优化模式 | √ | √ |
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
。graph
: 输出参数,保存图数据的对象。dec_key
: 解密密钥,用于解密密文模型,密钥长度为16、24或32。dec_mode
: 解密模式,可选有AES-GCM
、AES-CBC
。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Load¶
从多个文件加载多个模型。
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
。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
。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()
公有成员函数¶
函数 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|
const void *Data() const | √ | √ |
void *MutableData() | √ | √ |
size_t DataSize() const | √ | √ |
bool ResizeData(size_t data_len) | √ | √ |
bool SetData(const void *data, size_t data_len) | √ | √ |
Buffer Clone() const | √ | √ |
SetData¶
bool SetData(const void *data, size_t data_len)
配置Data和大小。
参数
data
: data地址data_len
: data大小
返回值
是否配置成功。
Model¶
#include <model.h>
Model定义了MindSpore中的模型,便于计算图管理。
构造函数和析构函数¶
Model()
~Model()
公有成员函数¶
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_Lite
、ModelType::kMindIR
,分别对应ms
模型(converter_lite
工具导出)和mindir
模型(MindSpore导出或converter_lite
工具导出)。在端侧和云侧推理包中,端侧推理只支持ms
模型推理,该入参值被忽略。云端推理支持ms
和mindir
模型推理,需要将该参数设置为模型对应的选项值。云侧推理对ms
模型的支持,将在未来的迭代中删除,推荐通过mindir
模型进行云侧推理。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_Lite
、ModelType::kMindIR
,分别对应ms
模型(converter_lite
工具导出)和mindir
模型(MindSpore导出或converter_lite
工具导出)。在端侧和云侧推理包中,端侧推理只支持ms
模型推理,该入参值被忽略。云端推理支持ms
和mindir
模型推理,需要将该参数设置为模型对应的选项值。云侧推理对ms
模型的支持,将在未来的迭代中删除,推荐通过mindir
模型进行云侧推理。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_Lite
、ModelType::kMindIR
,分别对应ms
模型(converter_lite
工具导出)和mindir
模型(MindSpore导出或converter_lite
工具导出)。在端侧和云侧推理包中,端侧推理只支持ms
模型推理,该入参值被忽略。云端推理支持ms
和mindir
模型推理,需要将该参数设置为模型对应的选项值。云侧推理对ms
模型的支持,将在未来的迭代中删除,推荐通过mindir
模型进行云侧推理。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_Lite
、ModelType::kMindIR
,分别对应ms
模型(converter_lite
工具导出)和mindir
模型(MindSpore导出或converter_lite
工具导出)。在端侧和云侧推理包中,端侧推理只支持ms
模型推理,该入参值被忽略。云端推理支持ms
和mindir
模型推理,需要将该参数设置为模型对应的选项值。云侧推理对ms
模型的支持,将在未来的迭代中删除,推荐通过mindir
模型进行云侧推理。model_context
: 模型Context。dec_key
: 解密密钥,用于解密密文模型,密钥长度为16。dec_mode
: 解密模式,可选有AES-GCM
。cropto_lib_path
: OpenSSL Crypto解密库路径。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Build
之后对model_context
的其他修改不再生效。
Build¶
Status Build(GraphCell graph, const std::shared_ptr<Context> &model_context = nullptr,
const std::shared_ptr<TrainCfg> &train_cfg = nullptr)
将GraphCell存储的模型编译至可在Device上运行的状态。
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
函数来获取具体错误码及错误信息。
Predict¶
Status Predict(const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr)
推理模型。
参数
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
在使用GPU推理时,用户可以使用
LoadConfig
接口进行TensorRT设置,配置文件内容及说明如下:[ms_cache]
serialize_path=(序列化模型的存储路径)
[gpu_context]
input_shape=input_name:[input_dim](模型输入维度,用于动态维度张量输入)
dynamic_dims=[min_dim~max_dim](模型输入的动态维度范围,用于动态维度张量输入)
opt_dims=[opt_dim](模型最优输入维度,用于动态维度张量输入)
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
函数来获取具体错误码及错误信息。
GetTrainableParams¶
std::vector<MSTensor> GetTrainableParams() const;
获取optimizer中所有参与权重更新的MSTensor。
返回值
optimizer中所有参与权重更新的MSTensor。
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
,模型会按顺序依次调整对应输入顺序的inputs
张量形状。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
CheckModelSupport¶
static bool CheckModelSupport(enum DeviceType device_type, ModelType model_type)
检查设备是否支持该模型。
参数
device_type
: 设备类型,例如kMaliGPU
。model_type
: 模型类型,例如MindIR
。
返回值
状态码。
BuildTransferLearning¶
Status BuildTransferLearning(GraphCell backbone, GraphCell head, const std::shared_ptr<Context> &context,
const std::shared_ptr<TrainCfg> &train_cfg = nullptr)
构建一个迁移学习模型,其中主干权重是固定的,头部权重是可训练的。
SetLearningRate¶
Status SetLearningRate(float learning_rate)
设置学习率。
参数
learning_rate
: 指定的学习率。
返回值
状态码。
SetupVirtualBatch¶
Status SetupVirtualBatch(int virtual_batch_multiplier, float lr = -1.0f, float momentum = -1.0f)
设置虚拟batch用于训练。
参数
virtual_batch_multiplier
: 虚拟batch乘法器,当设置值小于1时,表示禁用虚拟batch。lr
: 学习率,默认为-1.0f。momentum
: 动量,默认为-1.0f。
返回值
状态码。
RunStep¶
Status RunStep(const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr)
单步训练模型。
参数
before
: 一个MSKernelCallBack 结构体。定义了运行每个节点之前调用的回调函数。after
: 一个MSKernelCallBack 结构体。定义了运行每个节点之后调用的回调函数。
返回值
状态码。
PredictWithPreprocess¶
Status PredictWithPreprocess(const std::vector<std::vector<MSTensor>> &inputs, std::vector<MSTensor> *outputs,
const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr)
进行推理模型,并在推理前进行数据预处理。
参数
inputs
: 模型输入按顺序排列的vector
。outputs
: 输出参数,按顺序排列的vector
的指针,模型输出会按顺序填入该容器。before
: 一个MSKernelCallBack 结构体。定义了运行每个节点之前调用的回调函数。after
: 一个MSKernelCallBack 结构体。定义了运行每个节点之后调用的回调函数。
返回值
状态码。
Preprocess¶
Status Preprocess(const std::vector<std::vector<MSTensor>> &inputs, std::vector<MSTensor> *outputs)
若模型配置了数据预处理,对模型输入数据进行数据预处理。
参数
inputs
: 模型输入按顺序排列的vector
。outputs
: 输出参数,按顺序排列的vector
的指针,模型输出会按顺序填入该容器。
返回值
状态码。
UpdateFeatureMaps¶
Status UpdateFeatureMaps(const std::vector<MSTensor> &new_weights)
更新模型的权重Tensor内容。
参数
new_weights
: 要更新的权重Tensor。
返回值
状态码。
UpdateWeights¶
Status UpdateWeights(const std::vector<MSTensor> &new_weights)
更新模型的权重Tensor的大小和内容。
参数
new_weights
: 要更新的权重Tensor,可同时更新大小和内容。
返回值
状态码。
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
指针。
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
返回的指针。
公有成员函数¶
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相等。
返回值
MSTensor
是否与另一个MSTensor相等。
SetAllocator¶
void SetAllocator(std::shared_ptr<Allocator> allocator)
设置MSTensor
数据所属的内存池。
参数
model
: 指向Allocator的指针。
SetData¶
void SetData(void *data, bool own_data = true)
设置指向MSTensor
数据的指针。
参数
data
: 新的数据的地址。own_data
: 是否在MSTensor
析构时释放数据内存。如果为true,将在MSTensor
析构时释放数据内存,如果重复调用SetData
,将仅释放新的数据内存,老的数据内存需要用户释放;如果为false,需要用户释放数据内存。由于向前兼容,默认为true,建议用户设置为false。
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
函数来获取具体错误码及错误信息。
CoreMLDelegate¶
#include <delegate.h>
CoreMLDelegate
继承自Delegate
类,定义了CoreML框架接入MindSpore Lite的代理接口。
构造函数¶
CoreMLDelegate()
公有成员函数¶
Init¶
Status Init() overirde;
初始化CoreMLDelegate资源,仅在内部图编译阶段调用。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Build¶
Status Build(DelegateModel *model) override;
CoreMLDelegate在线构图,仅在内部图编译阶段调用。
参数
model
: 指向存储DelegateModel实例的指针。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
SchemaVersion¶
#include <delegate.h>
定义了MindSpore 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>
定义了MindSpore 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¶
函数 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|
std::vector |
✕ | √ |
#include <types.h>
std::vector<char> CharVersion()
全局方法,用于获取版本的字符vector。
返回值
MindSpore Lite版本的字符vector。
Version¶
函数 | 云侧推理是否支持 | 端侧推理是否支持 |
---|---|---|
std::string Version() | ✕ | √ |
#include <types.h>
std::string Version()
全局方法,用于获取版本的字符串。
返回值
MindSpore Lite版本的字符串。
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
: 错误描述字符串。
返回值
状态信息字符串。
SetStatusMsg¶
inline void SetStatusMsg(const std::string &status_msg)
配置状态描述字符串。
参数
status_msg
: 状态描述字符串。
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!=(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不等。
explicit operator int() const¶
explicit operator int() const;
重载int操作。当Status
对象被作为整型表达式使用时,返回整型表示的当前状态值。
返回值
当前状态值。
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的worker的个数。
返回值
RunnerConfig类中配置的worker数量。
SetContext¶
void SetContext(const std::shared_ptr<Context> &context)
设置RunnerConfig的context参数。
参数
context
: worker上下文配置。
GetContext¶
std::shared_ptr<Context> GetContext() const;
获取RunnerConfig配置的上下文参数。
返回值
上下文配置类
Context
对象。
SetConfigInfo¶
void SetConfigInfo(const std::string &key, const std::map<std::string, std::string> &config)
设置RunnerConfig的配置参数。
参数
key
: string类型关键字。config
: map类型的配置参数。
GetConfigInfo¶
std::map<std::string, std::map<std::string, std::string>> GetConfigInfo() const;
获取RunnerConfig配置参数信息。
返回值
map
类型的配置信息。
SetConfigPath¶
void SetConfigPath(const std::string &config_path)
设置RunnerConfig中的配置文件路径。
参数
config_path
: 配置文件路径。
GetConfigPath¶
std::string GetConfigPath() const;
获取RunnerConfig中的配置文件的路径。
返回值
RunnerConfig类中的配置文件路径。
ModelParallelRunner¶
#include <model_parallel_runner.h>
ModelParallelRunner定义了MindSpore的多个Model以及并发策略,便于多个Model的调度与管理。
构造函数和析构函数¶
ModelParallelRunner()
~ModelParallelRunner()
公有成员函数¶
Init¶
inline Status Init(const std::string &model_path, const std::shared_ptr<RunnerConfig> &runner_config = nullptr)
根据路径读取加载模型,生成一个或者多个模型,并将所有模型编译至可在Device上运行的状态。该接口支持传入ms
模型(converter_lite
工具导出)和mindir
模型(MindSpore导出或converter_lite
工具导出),但对ms
模型的支持,将在未来的迭代中删除,推荐使用mindir
模型进行推理。当使用ms
模型进行推理时,请保持模型的后缀名为.ms
,否则无法识别。
参数
model_path
: 模型文件路径。runner_config
: 一个RunnerConfig类。定义了并发推理模型的配置参数。
返回值
状态码类
Status
对象,可以使用其公有函数StatusCode
或ToString
函数来获取具体错误码及错误信息。
Init¶
Status Init(const void *model_data, const size_t data_size, const std::shared_ptr<RunnerConfig> &runner_config = nullptr)
根据模型文件数据,生成一个或者多个模型,并将所有模型编译至可在Device上运行的状态。该接口仅支持传入mindir
模型文件数据。
参数
model_data
: 模型文件数据。data_size
: 模型文件数据大小。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
函数来获取具体错误码及错误信息。