mindspore
接口汇总
推理
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| MindSpore中的模型,便于计算图管理。 | √ | √ | |
| 包装多个Model类,用于调度多个Model对象 | √ | ✕ | |
| 包装多个ModelExecutor类,用于调度多个Model对象 | √ | ✕ | 
运行环境配置
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| 保存执行中的环境变量。 | √ | √ | |
| 不同硬件设备的环境信息。 | √ | √ | |
| 模型运行在CPU上的配置。 | √ | √ | |
| 模型运行在GPU上的配置。 | √ | √ | |
| 模型运行在NPU上的配置。 | ✕ | √ | |
| 模型运行在Atlas 200/300/500推理产品、Atlas推理系列产品上的配置。 | √ | √ | |
| 模型运行的代理模式。 | √ | √ | |
| 键 | √ | √ | 
并发推理
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| 模型并发推理配置参数。 | √ | ✕ | |
| 模型并发推理类。 | √ | ✕ | 
张量Tensor相关
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| MindSpore中的张量。 | √ | √ | |
| MSTensor中的一组量化参数。 | √ | √ | |
| MindSpore MSTensor保存的数据支持的类型。 | √ | √ | |
| MindSpore MSTensor保存的数据支持的排列格式。 | √ | √ | |
| 内存管理基类。 | √ | √ | 
模型分组
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| 模型分组。 | √ | ✕ | |
| 模型分组。 | √ | √ | 
状态
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| 返回状态类。 | √ | √ | |
| 返回计算类别。 | √ | √ | 
序列化保存与加载
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| 汇总了模型文件读写的方法。 | √ | √ | |
| Buff数据类。 | √ | √ | 
版本查询
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| MindSpore Lite 执行推理时,模型文件的版本。 | ✕ | √ | |
| 获取字符vector形式的当前版本号。 | ✕ | √ | |
| 获取字符串形式的当前版本号。 | ✕ | √ | 
回调函数
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| MindSpore回调函数包装器。 | √ | √ | |
| MindSpore回调函数的参数。 | √ | √ | 
MindSpore Lite 训练配置
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| MindSpore Lite训练配置类。 | ✕ | √ | |
| MindSpore Lite训练混合精度配置类。 | ✕ | √ | |
| MindSpore Lite训练精度类。 | ✕ | √ | |
| MindSpore Lite训练指标类。 | ✕ | √ | |
| MindSpore Lite训练回调类。 | ✕ | √ | |
| 定义了训练回调的一组参数。 | ✕ | √ | |
| MindSpore Lite训练模型文件保存类。 | ✕ | √ | |
| MindSpore Lite训练学习率调度类。 | ✕ | √ | |
| MindSpore Lite训练配置类。 | ✕ | √ | |
| MindSpore Lite训练学习率的一组参数。 | ✕ | √ | |
| 每个epoch将学习率乘以一个因子。 | ✕ | √ | |
| MindSpore Lite训练时间监测类。 | ✕ | √ | |
| MindSpore Lite训练学习率调度类。 | ✕ | √ | 
Delegate三方框架接入机制
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| MindSpore Lite接入第三方AI框架的代理。 | ✕ | √ | |
| MindSpore Lite Delegate机制封装的模型。 | ✕ | √ | |
| MindSpore Lite 算子列表的迭代器。 | ✕ | √ | |
| MindSpore Lite接入CoreML框架的代理。 | ✕ | √ | |
| MindSpore Lite接入代理(抽象类)。 | √ | ✕ | |
| MindSpore Lite接入代理(模板类)。 | √ | ✕ | 
图容器
| 类名 | 描述 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|---|
| 容器基类。 | ✕ | √ | |
| 容器类。 | ✕ | √ | |
| 图容器类。 | ✕ | √ | |
| 图类。 | ✕ | √ | |
| 输入输出类。 | √ | √ | 
Context
#include <context.h>
Context类用于保存执行中的环境变量。
构造函数和析构函数
Context()
~Context() = default;
公有成员变量
Data
struct Data
Context的数据。
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| void set_delegate(const std::shared_ptr<AbstractDelegate> &delegate) | ✕ | √ | 
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| std::vector<std::shared_ptr<DeviceInfoContext>> &MutableDeviceInfo() | √ | √ | 
SetThreadNum
void SetThreadNum(int32_t thread_num)
设置运行时的线程数。
- 参数 - thread_num: 运行时的线程数。
 
GetThreadNum
int32_t GetThreadNum() const
获取当前线程数设置。
- 返回值 - 当前线程数设置。 
SetGroupInfoFile
void SetGroupInfoFile(std::string group_info_file)
设置组信息文件。
- 参数 - group_info_file: 组信息文件名字。
 
SetInterOpParallelNum
void SetInterOpParallelNum(int32_t parallel_num)
设置运行时的算子并行推理数目。
- 参数 - parallel_num: 运行时的算子并行数。
 
GetInterOpParallelNum
int32_t GetInterOpParallelNum() const
获取当前算子并行数设置。
- 返回值 - 当前算子并行数设置。 
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则支持并行。
 
GetEnableParallel
bool GetEnableParallel() const
获取当前是否支持并行。
- 返回值 - 返回值为true,代表支持并行。 
SetBuiltInDelegate
void SetBuiltInDelegate(DelegateMode mode)
设置内置Delegate模式,以使用第三方AI框架辅助推理。
- 参数 - mode: 内置Delegate模式,可选配置选项- kNoDelegate、- kCoreML、- kNNAPI。- kNoDelegate表示不使用第三方AI框架辅助推理,- kCoreML表示使用CoreML进行推理(在iOS上可选),- kNNAPI表示使用NNAPI进行推理(在Android上可选)。
 
GetBuiltInDelegate
DelegateMode GetBuiltInDelegate() const
获取当前内置Delegate模式。
- 返回值 - 返回当前内置Delegate模式。 
set_delegate
void set_delegate(const std::shared_ptr<AbstractDelegate> &delegate)
设置Delegate,Delegate定义了用于支持第三方AI框架接入的代理。
- 参数 - delegate: Delegate指针。
 
SetDelegate
void SetDelegate(const std::shared_ptr<Delegate> &delegate)
设置Delegate,Delegate定义了用于支持第三方AI框架接入的代理。
- 参数 - delegate: Delegate指针。
 
get_delegate
std::shared_ptr<AbstractDelegate> get_delegate() const
获取当前Delegate。
- 返回值 - 当前Delegate的指针。 
GetDelegate
std::shared_ptr<Delegate> GetDelegate() const
获取当前Delegate。
- 返回值 - 当前Delegate的指针。 
SetMultiModalHW
void SetMultiModalHW(bool float_mode)
在多设备中,配置量化模型是否以浮点模式运行。
- 参数 - float_mode: 是否以浮点模式运行。
 
GetMultiModalHW
bool GetMultiModalHW() const
获取当前配置中,量化模型的运行模式。
- 返回值 - 当前配置中,量化模型是否以浮点模式运行。 
MutableDeviceInfo
std::vector<std::shared_ptr<DeviceInfoContext>> &MutableDeviceInfo()
修改该context下的DeviceInfoContext数组,仅端侧推理支持数组中有多个成员是异构场景。
- 返回值 - 存储DeviceInfoContext的vector的引用。 
DelegateMode
#include <context.h>
enum DelegateMode {
  kNoDelegate = 0,
  kCoreML = 1,
};
Delegate模式。
DeviceInfoContext
#include <context.h>
DeviceInfoContext类定义不同硬件设备的环境信息。
构造函数和析构函数
DeviceInfoContext()
virtual ~DeviceInfoContext() = default
公有成员变量
struct Data
数据。
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| void SetAllocator(const std::shared_ptr<Allocator> &allocator) | ✕ | ✕ | 
| ✕ | ✕ | 
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。
GetProvider
inline std::string GetProvider() const
获取设备的生产商名。
SetProvider
inline void SetProvider(const std::string &provider)
设置设备生产商名。
- 参数 - provider: 生产商名。
 
GetProviderDevice
inline std::string GetProviderDevice() const
获取生产商设备名。
SetProviderDevice
inline void SetProviderDevice(const std::string &device)
设置设备生产商设备名。
- 参数 - device: 设备名。
 
SetAllocator
void SetAllocator(const std::shared_ptr<Allocator> &allocator)
设置内存管理器。
- 参数 - allocator: 内存管理器。
 
GetAllocator
std::shared_ptr<Allocator> GetAllocator() const
获取内存管理器。
CPUDeviceInfo
#include <context.h>
派生自DeviceInfoContext,模型运行在CPU上的配置。
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| √ | √ | |
| √ | √ | 
GetDeviceType
enum DeviceType GetDeviceType() const override
- 返回值 - DeviceType::kCPU 
SetEnableFP16
void SetEnableFP16(bool is_fp16)
用于指定是否以FP16精度进行推理。
- 参数 - is_fp16: 是否以FP16精度进行推理。
 
GetEnableFP16
bool GetEnableFP16() const
- 返回值 - 已配置的精度模式。 
GPUDeviceInfo
#include <context.h>
派生自DeviceInfoContext,模型运行在GPU上的配置。
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| inline void SetPrecisionMode(const std::string &precision_mode) | √ | √ | 
| √ | √ | |
| √ | √ | |
| √ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | 
GetDeviceType
enum DeviceType GetDeviceType() const override
- 返回值 - DeviceType::kGPU 
SetDeviceID
void SetDeviceID(uint32_t device_id)
用于指定设备ID。
- 参数 - device_id: 设备ID。
 
GetDeviceID
uint32_t GetDeviceID() const
- 返回值 - 已配置的设备ID。 
GetRankID
int GetRankID() const
- 返回值 - 当前运行的RANK ID。 
GetGroupSize
int GetGroupSize() const
- 返回值 - 当前运行的GROUP SIZE。 
SetPrecisionMode
inline void SetPrecisionMode(const std::string &precision_mode)
用于指定推理时算子精度。
- 参数 - precision_mode: 可选值- origin(以模型中指定精度进行推理),- fp16(以FP16精度进行推理),默认值:- origin。
 
GetPrecisionMode
inline std::string GetPrecisionMode() const
- 返回值 - 已配置的精度模式。 
SetEnableFP16
void SetEnableFP16(bool is_fp16)
用于指定是否以FP16精度进行推理。
- 参数 - is_fp16: 是否以FP16精度进行推理。
 
GetEnableFP16
bool GetEnableFP16() const
- 返回值 - 已配置的精度模式。 
SetEnableGLTexture
void SetEnableGLTexture(bool is_enable_gl_texture)
用于指定是否绑定OpenGL纹理数据。
- 参数 - is_enable_gl_texture: 是否在推理时绑定OpenGL纹理数据。
 
GetEnableGLTexture
bool GetEnableGLTexture() const
- 返回值 - 已配置的绑定OpenGL纹理数据模式。 
SetGLContext
void SetGLContext(void *gl_context)
用于指定OpenGL EGLContext。
- 参数 - gl_context: OpenGL的当前运行时的EGLContext值。
 
GetGLContext
void *GetGLContext() const
- 返回值 - 已配置的指向OpenGL EGLContext的指针。 
SetGLDisplay
void SetGLDisplay(void *gl_display)
用于指定OpenGL EGLDisplay。
- 参数 - gl_display: OpenGL的当前运行时的EGLDisplay值。
 
GetGLDisplay
void *GetGLDisplay() const
- 返回值 - 已配置的指向OpenGL EGLDisplay的指针。 
KirinNPUDeviceInfo
#include <context.h>
派生自DeviceInfoContext,模型运行在NPU上的配置。
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | |
| ✕ | √ | 
GetDeviceType
enum DeviceType GetDeviceType() const override
- 返回值 - DeviceType::kKirinNPU 
SetEnableFP16
void SetEnableFP16(bool is_fp16)
用于指定是否以FP16精度进行推理。
- 参数 - is_fp16: 是否以FP16精度进行推理。
 
GetEnableFP16
bool GetEnableFP16() const
- 返回值 - 已配置的精度模式。 
SetFrequency
void SetFrequency(int frequency)
用于指定NPU频率。
- 参数 - frequency: 设置为1(低功耗)、2(均衡)、3(高性能)、4(极致性能),默认为3。
 
GetFrequency
int GetFrequency() const
- 返回值 - 已配置的NPU频率模式。 
AscendDeviceInfo
#include <context.h>
派生自DeviceInfoContext,模型运行在Atlas 200/300/500推理产品、Atlas推理系列产品上的配置。
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| inline void SetInsertOpConfigPath(const std::string &cfg_path) | √ | √ | 
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| void SetInputShapeMap(const std::map<int, std::vector | √ | √ | 
| √ | √ | |
| void SetDynamicBatchSize(const std::vector<size_t> &dynamic_batch_size) | √ | √ | 
| √ | √ | |
| inline void SetDynamicImageSize(const std::string &dynamic_image_size) | √ | √ | 
| √ | √ | |
| √ | √ | |
| √ | √ | |
| inline void SetPrecisionMode(const std::string &precision_mode) | √ | √ | 
| √ | √ | |
| inline void SetOpSelectImplMode(const std::string &op_select_impl_mode) | √ | √ | 
| √ | √ | |
| inline void SetFusionSwitchConfigPath(const std::string &cfg_path) | √ | √ | 
| √ | √ | |
| inline void SetBufferOptimizeMode(const std::string &buffer_optimize_mode) | √ | √ | 
| √ | √ | 
GetDeviceType
enum DeviceType GetDeviceType() const override
- 返回值 - DeviceType::kAscend 
SetDeviceID
void SetDeviceType(uint32_t device_id)
用于指定设备ID。
- 参数 - device_id: 设备ID。
 
GetDeviceID
uint32_t GetDeviceID() const
- 返回值 - 已配置的设备ID。 
SetRankID
void SetRankID(uint32_t rank_id)
指定模型rank_id。
- 参数 - rank_id: Rank ID。
 
GetRankID
uint32_t GetRankID() const
- 返回值 - 获取模型的rank_id。 
SetInsertOpConfigPath
inline void SetInsertOpConfigPath(const std::string &cfg_path)
模型插入AIPP算子。
- 参数 - cfg_path: AIPP配置文件路径。
 
GetInsertOpConfigPath
inline std::string GetInsertOpConfigPath() const
- 返回值 - 已配置的AIPP。 
SetInputFormat
inline void SetInputFormat(const std::string &format)
指定模型输入格式。
- 参数 - format: 输入格式,可选值有- "NCHW"、- "NHWC"和- "ND"。
 
GetInputFormat
inline std::string GetInputFormat() const
- 返回值 - 已配置的模型输入格式。 
SetInputShape
inline void SetInputShape(const std::string &shape)
指定模型输入shape,为字符串形式,需指定输入名称,每个shape值由,隔开,不同输入由;隔开。
- 参数 - shape: 如- "input_op_name1:1,2,3,4;input_op_name2:4,3,2,1"。
 
GetInputShape
inline std::string GetInputShape() const
- 返回值 - 已配置的模型输入shape。 
SetInputShapeMap
inline void SetInputShapeMap(const std::map<int, std::vector<int>> &shape)
指定模型输入shape。
- 参数 - shape: map的key对应输入的下标,例如第一个输入对应下标0,第二个对应下标1。value对应输入shape,为数组形式。
 
GetInputShapeMap
std::map<int, std::vector<int>> GetInputShapeMap() const
- 返回值 - 已配置模型输入shape。 
SetDynamicBatchSize
void SetDynamicBatchSize(const std::vector<size_t> &dynamic_batch_size)
指定模型动态batch的档位,支持个数范围[2, 100],为数组形式。
- 参数 - dynamic_batch_size: 如- {1, 2}。
 
GetDynamicBatchSize
inline std::string GetDynamicBatchSize() const
- 返回值 - 已配置模型的动态batch。 
SetDynamicImageSize
inline void SetDynamicImageSize(const std::string &dynamic_image_size)
指定模型动态分辨率的档位,支持个数范围[2, 100],为字符串形式,每个shape值由,隔开,不同输入由;隔开。
- 参数 - dynamic_image_size: 如- "64,64;128,128"。
 
GetDynamicImageSize
inline std::string GetDynamicImageSize() const
- 返回值 - 已配置模型的动态分辨率。 
SetOutputType
void SetOutputType(enum DataType output_type)
指定模型输出type。
- 参数 - output_type: 可选有- "FP32"、- "UINT8"或- "FP16"。
 
GetOutputType
enum DataType GetOutputType() const
- 返回值 - 已配置模型的输出类型。 
SetPrecisionMode
inline void SetPrecisionMode(const std::string &precision_mode)
配置模型精度模式。
- 参数 - precision_mode: 可选有- "enforce_fp16"、- "preferred_fp32"、- "enforce_origin"、- "enforce_fp32"或者- "preferred_optimal",默认为- "enforce_fp16"。
 
GetPrecisionMode
inline std::string GetPrecisionMode() const
- 返回值 - 已配置模型的精度模式。 
SetOpSelectImplMode
inline void SetOpSelectImplMode(const std::string &op_select_impl_mode)
配置算子实现方式。
- 参数 - op_select_impl_mode: 可选有- "high_performance"和- "high_precision",默认为- "high_performance"。
 
GetOpSelectImplMode
inline std::string GetOpSelectImplMode() const
- 返回值 - 已配置的算子选择模式。 
SetFusionSwitchConfigPath
inline void SetFusionSwitchConfigPath(const std::string &cfg_path)
配置融合开关。
- 参数 - cfg_path: 融合开关配置文件,可指定关闭特定融合规则。
 
GetFusionSwitchConfigPath
inline std::string GetFusionSwitchConfigPath() const
- 返回值 - 已配置的融合开关文件路径。 
SetBufferOptimizeMode
inline void SetBufferOptimizeMode(const std::string &buffer_optimize_mode)
配置缓存优化模式。
- 参数 - buffer_optimize_mode: 可选有- "l1_optimize"、- "l2_optimize"或- "off_optimize",默认为- "l2_optimize"。
 
GetBufferOptimizeMode
inline std::string GetBufferOptimizeMode() const
- 返回值 - 已配置的缓存优化模式。 
Key
using Key = struct MS_API Key {
  size_t max_key_len = 32;
  size_t len = 0;
  unsigned char key[32] = {0};
  Key() : len(0) {}
  explicit Key(const char *dec_key, size_t key_len)
};
键结构体。
Serialization
#include <serialization.h>
Serialization类汇总了模型文件读写的方法。
静态公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| ✕ | √ | |
| ✕ | √ | |
| ✕ | ✕ | |
| static inline Status SetParameters(const std::map<std::string, Buffer> ¶meters, Model *model) | ✕ | ✕ | 
| static inline Status ExportModel(const Model &model, ModelType model_type, Buffer *model_data) | ✕ | √ | 
| ✕ | √ | |
| ✕ | √ | 
Load
从内存缓冲区加载模型。
static inline 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函数来获取具体错误码及错误信息。
Load
从文件加载模型。
static inline 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
从多个文件加载多个模型。
static inline 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函数来获取具体错误码及错误信息。
SetParameters
配置模型参数。
static inline Status SetParameters(const std::map<std::string, Buffer> ¶meters, Model *model)
- 参数 - parameters:参数。
- model:模型。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
ExportModel
导出训练模型,MindSpore Lite训练使用。
static inline Status ExportModel(const Model &model, ModelType model_type, Buffer *model_data)
- 参数 - model:模型数据。
- model_type:模型文件类型。
- model_data:模型参数数据。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
ExportModel
导出训练模型,MindSpore Lite训练使用。
static inline 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函数来获取具体错误码及错误信息。
ExportWeightsCollaborateWithMicro
试验接口,导出供micro推理使用的模型权重,MindSpore Lite训练使用。
static inline Status ExportWeightsCollaborateWithMicro(const Model &model, ModelType model_type,
                                                const std::string &weight_file, bool is_inference = true,
                                                bool enable_fp16 = false,
                                                const std::vector<std::string> &changeable_weights_name = {})
- 参数 - model:模型数据。
- model_type:模型文件类型。
- weight_file:保存的权重文件。
- is_inference: 是否是对推理模型的导出,当前仅支持推理模型。
- enable_fp16: 权重保存类型。
- changeable_weights_name: 设置shape会变化的权重名称。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
Buffer
#include <types.h>
Buffer定义了MindSpore中Buffer数据的结构。
构造函数和析构函数
  Buffer()
  Buffer(const void *data, size_t data_len)
  ~Buffer()
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | 
Data
const void *Data() const
获取只读的数据地址。
- 返回值 - const void指针。 
MutableData
void *MutableData()
获取可写的数据地址。
- 返回值 - void指针。 
DataSize
size_t DataSize() const
获取data大小。
- 返回值 - 当前data大小。 
ResizeData
bool ResizeData(size_t data_len)
重置data大小。
- 参数 - data_len: data大小
 
- 返回值 - 是否配置成功。 
SetData
bool SetData(const void *data, size_t data_len)
配置Data和大小。
- 参数 - data: data地址
- data_len: data大小
 
- 返回值 - 是否配置成功。 
Clone
Buffer Clone() const
拷贝一份自身的副本。
- 返回值 - 指向副本的指针。 
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
inline 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
inline Status Build(const void *model_data, size_t data_size, ModelType model_type,
             const std::shared_ptr<Context> &model_context, const Key &dec_key,
             const std::string &dec_mode, 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
inline Status Build(const std::string &model_path, ModelType model_type,
             const std::shared_ptr<Context> &model_context, const Key &dec_key,
             const std::string &dec_mode, 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上运行的状态。
BuildTransferLearning
Status BuildTransferLearning(GraphCell backbone, GraphCell head, const std::shared_ptr<Context> &context,
                      const std::shared_ptr<TrainCfg> &train_cfg = nullptr)
构建一个迁移学习模型,其中主干权重是固定的,头部权重是可训练的。
Resize
Status Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims)
调整已编译模型的输入张量形状。
- 参数 - inputs: 模型输入按顺序排列的- vector。
- dims: 输入张量形状,按输入顺序排列的由形状组成的- vector,模型会按顺序依次调整对应输入顺序的- inputs张量形状。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
UpdateWeights
Status UpdateWeights(const std::vector<MSTensor> &new_weights)
更新模型的权重Tensor的大小和内容。
- 参数 - new_weights: 要更新的权重Tensor,可同时更新大小和内容。
 
- 返回值 - 状态码。 
UpdateWeights
Status UpdateWeights(const std::vector<std::vector<MSTensor>> &new_weights)
更新模型的权重的大小和内容。
- 参数 - new_weights: 要更新的权重Tensor,可同时更新大小和内容。
 
- 返回值 - 状态码。 
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函数来获取具体错误码及错误信息。
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的指针,模型输出会按顺序填入该容器。
 
- 返回值 - 状态码。 
HasPreprocess
bool HasPreprocess()
模型是否配置了数据预处理。
- 返回值 - 模型是否配置了数据预处理。 
LoadConfig
inline 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
inline Status UpdateConfig(const std::string §ion, const std::pair<std::string, std::string> &config)
刷新配置,读文件相对比较费时,如果少部分配置发生变化可以通过该接口更新部分配置。
- 参数 - section: 配置的章节名。
- config: 要更新的配置对。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
GetInputs
std::vector<MSTensor> GetInputs()
获取模型所有输入张量。
- 返回值 - 包含模型所有输入张量的容器类型变量。 
GetInputByTensorName
inline MSTensor GetInputByTensorName(const std::string &tensor_name)
获取模型指定名字的输入张量。
- 返回值 - 指定名字的输入张量,如果该名字不存在则返回非法张量。 
GetGradients
std::vector<MSTensor> GetGradients() const
获取所有Tensor的梯度。
- 返回值 - 获取所有Tensor的梯度。 
ApplyGradients
Status ApplyGradients(const std::vector<MSTensor> &gradients)
应用所有Tensor的梯度。
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
GetFeatureMaps
std::vector<MSTensor> GetFeatureMaps() const
获取模型的所有权重Tensors。
- 返回值 - 获取模型的所有权重Tensor。 
GetTrainableParams
std::vector<MSTensor> GetTrainableParams() const
获取optimizer中所有参与权重更新的MSTensor。
- 返回值 - optimizer中所有参与权重更新的MSTensor。 
UpdateFeatureMaps
Status UpdateFeatureMaps(const std::vector<MSTensor> &new_weights)
更新模型的权重Tensor内容。
- 参数 - new_weights: 要更新的权重Tensor。
 
- 返回值 - 状态码。 
GetOptimizerParams
std::vector<MSTensor> GetOptimizerParams() const
获取optimizer参数MSTensor。
- 返回值 - 所有optimizer参数MSTensor。 
SetOptimizerParams
Status SetOptimizerParams(const std::vector<MSTensor> ¶ms)
更新optimizer参数。
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
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。
 
- 返回值 - 状态码。 
SetLearningRate
Status SetLearningRate(float learning_rate)
设置学习率。
- 参数 - learning_rate: 指定的学习率。
 
- 返回值 - 状态码。 
GetLearningRate
float GetLearningRate()
获取学习率。
- 返回值 - float类型,获取学习率。如果为0.0,表示没有找到优化器。 
InitMetrics
Status InitMetrics(std::vector<Metrics *> metrics)
训练指标参数初始化。
- 参数 - metrics: 训练指标参数。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
GetMetrics
std::vector<Metrics *> GetMetrics()
获取训练指标参数。
- 返回值 - 训练指标参数。 
GetOutputs
std::vector<MSTensor> GetOutputs()
获取模型所有输出张量。
- 返回值 - 包含模型所有输出张量的容器类型变量。 
GetOutputTensorNames
inline std::vector<std::string> GetOutputTensorNames()
获取模型所有输出张量的名字。
- 返回值 - 包含模型所有输出张量名字的容器类型变量。 
GetOutputByTensorName
inline MSTensor GetOutputByTensorName(const std::string &tensor_name)
获取模型指定名字的输出张量。
- 返回值 - 指定名字的输出张量,如果该名字不存在则返回非法张量。 
GetOutputsByNodeName
inline 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函数来获取具体错误码及错误信息。
CheckModelSupport
static bool CheckModelSupport(enum DeviceType device_type, ModelType model_type)
检查设备是否支持该模型。
- 参数 - device_type: 设备类型,例如- kMaliGPU。
- model_type: 模型类型,例如- MindIR。
 
- 返回值 - 状态码。 
SetTrainMode
Status SetTrainMode(bool train)
session设置训练模式。
- 参数 - train: 是否为训练模式。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
GetTrainMode
bool GetTrainMode() const
获取session是否是训练模式。
- 返回值 - bool类型,表示是否是训练模式。 
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函数来获取具体错误码及错误信息。
impl
const std::shared_ptr<ModelImpl> impl() const
获得模型的实现。
- 返回值 - 获得模型的实现。 
GetModelInfo
inline std::string GetModelInfo(const std::string &key)
获取模型的信息。
- 参数 - key: 模型的key。
 
- 返回值 - 模型的信息。 
Finalize
Status Finalize()
模型终止。
- 返回值 - 状态码。 
ModelExecutor
#include <multi_model_runner.h>
ModelExecutor定义了对Model的封装,用于调度多个Model的推理。
构造函数
ModelExecutor()
ModelExecutor(const std::vector<std::shared_ptr<ModelImpl>> &models, const std::vector<std::string> &executor_input_names,
                const std::vector<std::string> &executor_output_names,
                const std::vector<std::vector<std::string>> &subgraph_input_names,
                const std::vector<std::vector<MSTensor>> &model_output_tensors)
- 参数 - models: 一个由ModelImplPtr组成的向量,用于在ModelExecutor中推理。
- executor_input_names: 由string组成的向量,当前ModelExecutor的输入名。
- executor_output_names: 由string组成的向量,当前ModelExecutor的输出名。
- subgraph_input_names: 由string组成的向量,当前ModelExecutor中所有模型的输入以及输出名。
- model_output_tensors: 由MSTensor组成的向量,模型的输出tensor。
 
析构函数
~ModelExecutor()
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs) | √ | ✕ | 
| √ | ✕ | |
| √ | ✕ | 
Predict
Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs)
ModelExecutor的推理接口。
- 参数 - inputs: 模型输入按顺序排列的- vector。
- outputs: 输出参数,按顺序排列的- vector的指针,模型输出会按顺序填入该容器。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
GetInputs
std::vector<MSTensor> GetInputs() const
获取模型所有输入张量。
- 返回值 - 包含模型所有输入张量的容器类型变量。 
GetOutputs
std::vector<MSTensor> GetOutputs() const
获取模型所有输出张量。
- 返回值 - 包含模型所有输出张量的容器类型变量。 
MultiModelRunner
#include <multi_model_runner.h>
MultiModelRunner用于创建包含多个Model的mindir,并提供调度多个模型的方式。
构造函数
MultiModelRunner()
析构函数
~MultiModelRunner()
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | ✕ | |
| √ | ✕ | |
| √ | ✕ | |
| √ | ✕ | 
Build
inline Status Build(const std::string &model_path, const ModelType &model_type,
             const std::shared_ptr<Context> &model_context = nullptr)
根据路径读取加载模型,并将模型编译至可在Device上运行的状态。与Model接口中同名接口的差别在于传入的model_path指定的mindir文件中包含多个Model。
- 参数 - model_path: 模型文件路径。
- model_type:- ModelType::kMindIR,对应``mindir- 模型(MindSpore导出或converter_lite`工具导出)。
- model_context: 模型Context。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
GetModelExecutor
std::vector<ModelExecutor> GetModelExecutor() const
获取MultiModelRunner中创建的多个ModelExecutor对象。
- 返回值 - 包含MultiModelRunner中创建的所有ModelExecutor对象的列表。 
LoadConfig
inline Status LoadConfig(const std::string &config_path)
根据路径读取配置文件。
- 参数 - config_path: 配置文件路径。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
UpdateConfig
inline Status UpdateConfig(const std::string §ion, const std::pair<std::string, std::string> &config)
刷新配置,读文件相对比较费时,如果少部分配置发生变化可以通过该接口更新部分配置。
- 参数 - section: 配置的章节名。
- config: 要更新的配置对。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
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,否则存在内存泄露。
静态公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | ✕ | |
| √ | ✕ | |
| 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()
获取实现类的指针。
QuantParam
#include <types.h>
一个结构体。QuantParam定义了MSTensor的一组量化参数。
公有属性
bit_num
bit_num
int 类型变量。量化的bit数。
scale
scale
double 类型变量。
zero_point
zero_point
int32_t 类型变量。
min
min
double 类型变量。量化的最小值。
max
max
double 类型变量。量化的最大值。
MSKernelCallBack
#include <types.h>
using MSKernelCallBack = std::function<bool(const std::vector<MSTensor> &inputs, const std::vector<MSTensor> &outputs, const MSCallBackParam &opInfo)>
一个函数包装器。MSKernelCallBack 定义了指向回调函数的指针。
MSCallBackParam
#include <types.h>
一个结构体。MSCallBackParam定义了回调函数的输入参数。
公有属性
node_name
node_name
string 类型变量。节点名参数。
node_type
node_type
string 类型变量。节点类型参数。
execute_time
execute_time
double 类型变量。GPU执行时间。
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函数来获取具体错误码及错误信息。
CreateKernel
std::shared_ptr<kernel::Kernel> CreateKernel(const std::shared_ptr<kernel::Kernel> &node) override
创建Kernel。
- 参数 - node: 指向Kernel[Kernel]实例的共享指针。
 
- 返回值 - Kernel类共享指针。 
IsDelegateNode
bool IsDelegateNode(const std::shared_ptr<kernel::Kernel> &node) override
是否是Delegate节点。
- 参数 - node: 指向Kernel[Kernel]实例的共享指针。
 
- 返回值 - bool值。 
ReplaceNode
void ReplaceNodes(const std::shared_ptr<LiteDelegateGraph> &graph) override
替换节点。
- 参数 - graph: 要被替换的节点。
 
CoreMLDelegate
#include <delegate.h>
CoreMLDelegate继承自Delegate类,定义了CoreML框架接入MindSpore Lite的代理接口。
构造函数
CoreMLDelegate()
公有成员函数
Init
Status Init() override
初始化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
保护成员
kernels_
std::vector<kernel::Kernel *> *kernels_
Kernel的列表,保存模型的所有算子。
inputs_
const std::vector<mindspore::MSTensor> &inputs_
MSTensor的列表,保存这个算子的输入tensor。
outputs_
const std::vector<mindspore::MSTensor> &outputs
MSTensor的列表,保存这个算子的输出tensor。
primitives_
const std::map<kernel::Kernel *, const schema::Primitive *> &primitives_
Kernel和schema::Primitive的Map,保存所有算子的属性。
version_
SchemaVersion version_
enum值,当前执行推理的模型的版本SchemaVersion。
公有成员函数
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。 
nodes
std::vector<kernel::Kernel *> *nodes()
获取nodes。
- 返回值 - Kernel组成的vector。 
inputs
const std::vector<mindspore::MSTensor> &inputs()
返回DelegateModel输入tensor列表。
- 返回值 - MSTensor的列表。 
outputs
const std::vector<mindspore::MSTensor> &outputs()
返回DelegateModel输出tensor列表。
- 返回值 - MSTensor的列表。 
GetVersion
const SchemaVersion GetVersion()
返回当前执行推理的模型文件的版本。
- 返回值 - enum值,0: r1.2及r1.2之后的版本,1: r1.1及r1.1之前的版本,-1: 无效版本。 
AbstractDelegate
#include <delegate_api.h>
AbstractDelegate定义了MindSpore Lite 创建Delegate(抽象类)。
构造函数
AbstractDelegate()
AbstractDelegate(const std::vector<mindspore::MSTensor> &inputs, const std::vector<mindspore::MSTensor> &outputs)
      : inputs_(inputs), outputs_(outputs)
析构函数
virtual ~AbstractDelegate() = default
公有成员函数
inputs
const std::vector<mindspore::MSTensor> &inputs()
返回AbstractDelegate的inputTensor。
outputs
const std::vector<mindspore::MSTensor> &outputs()
返回AbstractDelegate的outputTensor。
保护成员变量
inputs_
std::vector<mindspore::MSTensor> inputs_
outputs_
std::vector<mindspore::MSTensor> outputs_
IDelegate
std::vector<mindspore::MSTensor> outputs_
#include <delegate_api.h>
IDelegate定义了MindSpore Lite 创建Delegate(模板类)。
构造函数
IDelegate()
IDelegate(const std::vector<mindspore::MSTensor> &inputs, const std::vector<mindspore::MSTensor> &outputs)
      : AbstractDelegate(inputs, outputs)
析构函数
virtual ~IDelegate() = default
公有成员函数
ReplaceNodes
virtual void ReplaceNodes(const std::shared_ptr<Graph> &graph) = 0
替换Delegate节点。
IsDelegateNode
virtual bool IsDelegateNode(const std::shared_ptr<Node> &node) = 0
判断节点是否属于Delegate。
CreateKernel
virtual std::shared_ptr<Kernel> CreateKernel(const std::shared_ptr<Node> &node) = 0
创建Kernel。
TrainCfg
#include <cfg.h>
TrainCfgMindSpore Lite训练的相关配置参数。
构造函数
TrainCfg() { this->loss_name_ = "_loss_fn"; }\
TrainCfg(const TrainCfg &rhs) {
    this->loss_name_ = rhs.loss_name_;
    this->mix_precision_cfg_ = rhs.mix_precision_cfg_;
    this->accumulate_gradients_ = rhs.accumulate_gradients_;
  }
- 参数 - rhs: 训练配置。
 
析构函数
~TrainCfg() = default
公有成员变量
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_
是否累加梯度。
公有成员函数
GetLossName
inline std::vector<std::string> GetLossName() const
获得损失名称。
- 返回值 - 损失的名称。 
SetLossName
inline void SetLossName(const std::vector<std::string> &loss_name)
设置损失名称。
- 参数 - loss_name: 损失的名称。
 
MixPrecisionCfg
#include <cfg.h>
MixPrecisionCfgMindSpore Lite训练混合精度配置类。
构造函数
  MixPrecisionCfg() {
    dynamic_loss_scale_ = false;
    loss_scale_ = 128.0f;
    num_of_not_nan_iter_th_ = 1000;
  }
  MixPrecisionCfg(const MixPrecisionCfg &rhs) {
    this->dynamic_loss_scale_ = rhs.dynamic_loss_scale_;
    this->loss_scale_ = rhs.loss_scale_;
    this->keep_batchnorm_fp32_ = rhs.keep_batchnorm_fp32_;
    this->num_of_not_nan_iter_th_ = rhs.num_of_not_nan_iter_th_;
  }
- 参数 - rhs: 混合精度配置。tr
 
析构函数
~MixPrecisionCfg() = default
共有成员变量
bool dynamic_loss_scale_ = false
混合精度训练中是否启用动态损失比例。
float loss_scale_
初始损失比例。
uint32_t num_of_not_nan_iter_th_
动态损失阈值。
bool is_raw_mix_precision_
原始模型是否是原生混合精度模型。
bool keep_batchnorm_fp32_ = true
原型模型是否保持BatchNorm算子为Fp32格式。
AccuracyMetrics
#include <accuracy.h>
AccuracyMetricsMindSpore Lite训练精度类。
构造函数
explicit AccuracyMetrics(int accuracy_metrics = METRICS_CLASSIFICATION, const std::vector<int> &input_indexes = {1}, const std::vector<int> &output_indexes = {0})
- 参数 - accuracy_metrics: 精度度量标准。
- input_indexes: 输入的序列号。
- output_indexes: 输出的序列号。
 
析构函数
virtual ~AccuracyMetrics()
公有成员函数
Clear
void Clear() override
精度清零。
Eval
float Eval() override
模型验证。
- 返回值 - float,模型验证精度。 
Metrics
#include <metrics.h>
MetricsMindSpore Lite训练指标类。
析构函数
virtual ~Metrics() = default
公有成员函数
Clear
virtual void Clear()
训练指标清零。
Eval
virtual float Eval()
模型验证。
- 返回值 - float,模型验证精度。 
Update
virtual void Update(std::vector<MSTensor *> inputs, std::vector<MSTensor *> outputs)
模型输入输出数据更新。
- 参数 - inputs: 模型输入MSTensor的- vector。
- outputs: 模型输输出MSTensor的- vector。
 
TrainCallBack
#include <callback.h>
MetricsMindSpore Lite训练回调类。
析构函数
virtual ~TrainCallBack() = default
公有成员函数
Begin
virtual void Begin(const TrainCallBackData &cb_data)
网络执行前调用。
- 参数 - cb_data: 回调参数。
 
End
  virtual void End(const TrainCallBackData &cb_data)
网络执行后调用。
- 参数 - cb_data: 回调参数。
 
EpochBegin
  virtual void EpochBegin(const TrainCallBackData &cb_data)
每轮迭代前回调。
- 参数 - cb_data: 回调参数。
 
EpochEnd
  virtual CallbackRetValue EpochEnd(const TrainCallBackData &cb_data)
每轮迭代后回调。
- 参数 - cb_data: 回调参数。
 
- 返回值 - CallbackRetValue,表示是否在训练中继续循环。- enum CallbackRetValue : uint32_t { kContinue = 0, kStopTraining = 1, kExit = 2, kUnknownRetValue = 0xFFFFFFFF }; 
StepBegin
  virtual void StepBegin(const TrainCallBackData &cb_data)
每步迭代前回调。
- 参数 - cb_data: 回调参数。
 
StepEnd
  virtual void StepEnd(const TrainCallBackData &cb_data)
每步迭代后回调。
- 参数 - cb_data: 回调参数。
 
TrainCallBackData
#include <callback.h>
一个结构体。TrainCallBackData定义了训练回调的一组参数。
公有属性
train_mode_
train_mode_
bool 类型变量。训练模式。
epoch_
epoch_
unsigned int 类型变量。训练迭代的epoch次数。
step_
step_
unsigned int 类型变量。训练迭代的step次数。
model_
model_
Model 类型指针。训练模型对象。
CkptSaver
#include <ckpt_saver.h>
MetricsMindSpore Lite训练模型文件保存类。
构造函数和析构函数
  explicit CkptSaver(int save_every_n, const std::string &filename_prefix)
  virtual ~CkptSaver()
LossMonitor
#include <loss_monitor.h>
MetricsMindSpore Lite训练损失函数类。
构造函数和析构函数
  explicit LossMonitor(int print_every_n_steps = INT_MAX)
  virtual ~LossMonitor()
公有成员函数
GetLossPoints
  const std::vector<GraphPoint> &GetLossPoints()
获取训练损失数据。
- 返回值 - 包含 - GraphPoint数据的- vector,训练的损失数据。
LRScheduler
#include <lr_scheduler.h>
MetricsMindSpore Lite训练学习率调度类。
构造函数和析构函数
  explicit LRScheduler(LR_Lambda lambda_func, void *lr_cb_data = nullptr, int step = 1)
  virtual ~LRScheduler()
StepLRLambda
#include <lr_scheduler.h>
一个结构体。StepLRLambda定义了训练学习率的一组参数。
公有属性
step_size
step_size
int 类型变量。学习率衰减步长。
gamma
gamma
float 类型变量。学习率衰减因子。
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>
MetricsMindSpore Lite训练时间监测类。
析构函数
  virtual ~TimeMonitor() = default
公有成员函数
EpochBegin
  void EpochBegin(const TrainCallBackData &cb_data) override
每轮迭代前调用。
- 参数 - cb_data: 回调参数。
 
- 返回值 - CallbackRetValue,表示是否在训练中继续循环。
EpochEnd
  CallbackRetValue EpochEnd(const TrainCallBackData &cb_data) override
每轮迭代后调用。
- 参数 - cb_data: 回调参数。
 
- 返回值 - CallbackRetValue,表示是否在训练中继续循环。
TrainAccuracy
#include <train_accuracy.h>
MetricsMindSpore 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。 
Version
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| ✕ | √ | 
#include <types.h>
std::string Version()
全局方法,用于获取版本的字符串。
- 返回值 - MindSpore Lite版本的字符串。 
Allocator
#include <allocator.h>
内存管理基类。
析构函数
virtual ~Allocator()
析构函数。
公有成员函数
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格式内存的数据类型。
 
Free
virtual void *Free(void *ptr)
内存释放。
- 参数 - ptr: 要释放的内存地址,该值由Malloc分配。
 
RefCount
virtual int RefCount(void *ptr)
返回分配内存的引用计数。
- 参数 - ptr: 要操作的内存地址,该值由Malloc分配。
 
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: 引用计数值。
 
IncRefCount
virtual int IncRefCount(void *ptr, int ref_count)
分配的内存引用计数加一。
- 参数 - ptr: 要操作的内存地址,该值由Malloc分配。
- ref_count: 引用计数值。
 
Create
static std::shared_ptr<Allocator> Create()
创建默认的内存分配器。
Prepare
virtual void *Prepare(void *ptr)
对分配的内存进行预处理。
- 参数 - ptr: 要操作的内存地址,该值由Malloc分配。
 
保护的数据成员
aligned_size_
内存对齐的字节数。默认值: 32 。
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;
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| inline std::string SetErrDescription(const std::string &err_description) | √ | √ | 
| √ | √ | |
| friend std::ostream &operator<<(std::ostream &os, const Status &s) | √ | √ | 
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | |
| √ | √ | 
StatusCode
enum StatusCode StatusCode() const
获取状态码。
- 返回值 - 状态码。 
ToString
inline std::string ToString() const
状态码转成字符串。
- 返回值 - 状态码的字符串。 
GetLineOfCode
int GetLineOfCode() const
获取代码行数。
- 返回值 - 代码行数。 
GetFileName
inline std::string GetFileName() const
获取文件名。
- 返回值 - 文件名。 
GetErrDescription
inline std::string GetErrDescription() const
获取错误描述字符串。
- 返回值 - 错误描述字符串。 
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不等。 
operator bool()
explicit operator bool() const
重载bool操作,判断是否当前状态为kSuccess。
- 返回值 - 是否当前状态为kSuccess。 
explicit operator int() const
explicit operator int() const
重载int操作。当Status对象被作为整型表达式使用时,返回整型表示的当前状态值。
- 返回值 - 当前状态值。 
OK
static Status OK()
获取kSuccess的状态码。
- 返回值 - StatusCode::kSuccess。 
IsOk
bool IsOk() const
判断是否是kSuccess的状态码。
- 返回值 - 是否是kSuccess。 
IsError
bool IsError() const
判断是否不是kSuccess的状态码。
- 返回值 - 是否不是kSuccess。 
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: 输出通道数。
 
公有成员函数
ModelType
  enum ModelType ModelType() const
获取模型类型。
- 返回值 - 模型类型。 
operator==(std::nullptr_t)
  bool operator==(std::nullptr_t) const
判断是否为空指针。
- 返回值 - 是否为空指针。 
operator!=(std::nullptr_t)
  bool operator!=(std::nullptr_t) const
判断是否为非空指针。
- 返回值 - 是否为非空指针。 
CompCode
enum CompCode : uint32_t {
  kCore = 0x00000000u,
  kMD = 0x10000000u,
  kME = 0x20000000u,
  kMC = 0x30000000u,
  kLite = 0xF0000000u,
};
计算的类型。
InputAndOutput
#include <cell.h>
构造函数
  InputAndOutput()
  InputAndOutput(const std::shared_ptr<CellBase> &cell, const std::vector<InputAndOutput> &prev, int32_t index)
- 参数 - cell: 输入CellBase。
- prev: 之前的InputAndOutput。
- index: 序列号。
 
析构函数
~InputAndOutput() = default
公有成员函数
GetIndex
int32_t GetIndex() const
返回index。
- 返回值 - 返回输入/输出的index。 
SetIndex
void SetIndex(int32_t index)
设置输入/输出的index。
- 参数 - index: 输入/输出的index。
 
CellBase
#include <cell.h>
构造函数和析构函数
  CellBase() = default;
  virtual ~CellBase() = default;
公有成员函数
Clone
  virtual std::shared_ptr<CellBase> Clone() const = 0
拷贝一份自身的副本。
- 返回值 - 指向副本的指针。 
Construct
virtual std::vector<Output> Construct(const std::vector<Input> &inputs)
构建CellBase。
- 参数 - inputs: 输入。
 
- 返回值 - 输出。 
Cell
#include <cell.h>
析构函数
  virtual ~Cell() = default
公有成员函数
Clone
  std::shared_ptr<CellBase> Clone() const
拷贝一份自身的副本。
- 返回值 - 指向副本的指针。 
Construct
virtual std::vector<Output> Construct(const std::vector<Input> &inputs)
构造一份CellBase。
- 参数 - inputs: Input组成的vector。
 
- 返回值 - Output组成的vector。 
Run
virtual Status Run(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs)
运行CellBase。
- 参数 - inputs: Input组成的vector。
- outputs: Output组成的vector。
 
- 返回值 - 状态码。 
operator()
std::vector<Output> operator()(const std::vector<Input> &inputs) const
括号运行符。
- 参数 - inputs: Input组成的vector。
 
- 返回值 - Output组成的vector。 
GraphCell
#include <cell.h>
构造函数和析构函数
  GraphCell() = default;
  ~GraphCell() override = default;
  explicit GraphCell(const Graph &)
  explicit GraphCell(Graph &&)
  explicit GraphCell(const std::shared_ptr<Graph> &)
公有成员函数
GetGraph
  const std::shared_ptr<Graph> &GetGraph() const
获取Graph指针。
- 返回值 - 指向Graph的指针。 
SetContext
void SetContext(const std::shared_ptr<Context> &context)
设置Context。
- 参数 - context: 指向Context[Context]实例的共享指针。
 
Run
Status Run(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs) override
运行。
- 参数 - inputs: [MSTensor]构成的vector。
- outputs: [MSTensor]构成的vector。
 
- 返回值 - 状态码。 
GetInputs
std::vector<MSTensor> GetInputs()
获取输入。
- 返回值 - [MSTensor]构成的vector。 
GetOutputs
std::vector<MSTensor> GetOutputs()
获取输出。
- 返回值 - [MSTensor]构成的vector。 
Load
Status Load(uint32_t device_id)
加载。
- 参数 - device_id: 芯片编号。
 
- 输出 - 状态码。 
RunnerConfig
#include <model_parallel_runner.h>
RunnerConfig定义了ModelParallelRunner中使用的配置选项参数。
构造函数和析构函数
RunnerConfig()
~RunnerConfig()
公有成员函数
| 函数 | 云侧推理是否支持 | 端侧推理是否支持 | 
|---|---|---|
| √ | ✕ | |
| √ | ✕ | |
| √ | ✕ | |
| √ | ✕ | |
| √ | ✕ | |
| inline std::map<std::string, std::map<std::string, std::string>> GetConfigInfo() const | √ | ✕ | 
| √ | ✕ | |
| √ | ✕ | |
| √ | ✕ | |
| √ | ✕ | 
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类中的配置文件路径。 
SetDeviceIds
void SetDeviceIds(const std::vector<uint32_t> &device_ids)
设置RunnerConfig中的设备ID列表。
- 参数 - device_ids: 设备ID列表。
 
GetDeviceIds
std::vector<uint32_t> GetDeviceIds() const
获取RunnerConfig中的设备ID列表。
- 返回值 - RunnerConfig类中的设备ID列表。 
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函数来获取具体错误码及错误信息。
GetInputs
std::vector<MSTensor> GetInputs()
获取模型所有输入张量。
- 返回值 - 包含模型所有输入张量的容器类型变量。 
GetOutputs
std::vector<MSTensor> GetOutputs()
获取模型所有输出张量。
- 返回值 - 包含模型所有输出张量的容器类型变量。 
ModelGroup
#include <model_group.h>
ModelGroup 类定义MindSpore Lite模型分组信息,用于共享工作空间(Workspace)内存或者权重(包括常量和变量)内存。
构造函数和析构函数
ModelGroup(ModelGroupFlag flags = ModelGroupFlag::kShareWorkspace)
~ModelGroup()
- 参数 - flags: 指示 ModelGroup 的类型,取值- ModelGroupFlag::kShareWorkspace,- ModelGroupFlag::kShareWorkspace。默认- ModelGroupFlag::kShareWorkspace。
 
公有成员函数
AddModel
Status AddModel(const std::vector<std::string> &model_path_list)
共享工作空间内存时,添加需要共享工作空间内存的模型路径。
- 参数 - model_path_list: 需要共享工作空间内存的模型路径。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
AddModel
Status AddModel(const std::vector<std::pair<const void *, size_t>> &model_buff_list)
共享工作空间内存时,添加需要共享工作空间内存的模型缓存。
- 参数 - model_buff_list: 需要共享工作空间内存的模型缓存。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
AddModel
Status AddModel(const std::vector<Model> &model_list)
共享权重内存时,添加需要共享权重内存的模型对象。
- 参数 - model_list: 需要共享权重内存的模型对象Model列表。
 
- 返回值 - 状态码类 - Status对象,可以使用其公有函数- StatusCode或- ToString函数来获取具体错误码及错误信息。
CalMaxSizeOfWorkspace
Status CalMaxSizeOfWorkspace(ModelType model_type, const std::shared_ptr<Context> &ms_context)
共享工作空间内存时,计算最大的工作空间内存大小。
- 参数 - 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函数来获取具体错误码及错误信息。
ModelGroupFlag
enum class ModelGroupFlag : int {
  kUnknown = 0x0000,
  kShareWeight = 0x0001,
  kShareWorkspace = 0x0002,
  kShareWeightAndWorkspace = 0x0003,
};
ModelGroup类型。