当前位置: 首页 > web >正文

鸿蒙进阶——Mindspore Lite AI框架源码解读之模型加载详解(一)

文章大纲

  • 引言
  • 一、模型加载概述
  • 二、核心数据结构
  • 三、模型加载核心流程

引言

Mindspore 是一款华为开发开源的AI推理框架,而Mindspore Lite则是华为为了适配在移动终端设备上运行专门定制的版本,使得我们可以在OpenHarmony快速实现模型加载和推理等功能,模型加载则是利用 Mindspore Lite 进行推理任务的首要步骤,它犹如打开智能应用大门的钥匙,精准且高效地将预训练模型导入到运行环境中,为后续的推理计算奠定坚实基础。无论是在移动端的智能应用开发,还是边缘计算设备的智能任务处理,Mindspore Lite 加载模型的能力都直接影响着整个系统的运行效率与智能化表现,值得我们深入探究其原理、流程与优化策略,接下来就重点解读核心API 及其流程。

本文非Mindspore入门科普文,需要具备一定的Mindspore 知识。

一、模型加载概述

加载的本质就是把模型从文件Buffer 转为Mindspore 后续推理时所需要的模型的相关对象
CPU 方式直接加载的话就是直接通过本地设备的CPU完成这些工作,这些作为推理的输入直接使用;而通过NNRT 的话就是在CPU完成这第一步加载后,再把转换完成后的输出作为输入通过Delegate 模式再次进行转换,转为NNRT 所需要的模型的相关对象,再把这些NNRT的对象通过NNRT传递到NNRT HOST 进而传递到NPU中进行运算。

二、核心数据结构

Model 由LiteGraph 呈现,LiteGraph 保存着所有的Tensor列表、Node列表、SubGrapha列表和输出、输入的索引,LiteGraph 包含着Node、和SubGrapha,相当于所有的子结构信息都保存到LiteGraph中,每个子结构只保存各自的索引数据,计算后再通过索引从LiteGraph中获取得到对应的数据。图由多个Node构成,每一个Node 承担一个运算操作。比如一个计算图有两个节点NodeA(执行加法操作)和NodeB(执行乘法操作,输入来自NodeA的输出),NodeA 输出张量生成的索引将被记录在output_indices_ 表示这个节点的输出Tensor,NodeB 就使用NodeA 的输出索引作为输入以便获取计算所需的数据。

struct MS_API LiteGraph {struct Node {std::string name_;std::string op_type_;int node_type_;const void *primitive_ = nullptr;std::shared_ptr<void> base_operator_ = nullptr;std::vector<uint32_t> input_indices_;std::vector<uint32_t> output_indices_;int quant_type_;int device_type_ = -1;};struct SubGraph {std::string name_;std::vector<uint32_t> input_indices_;std::vector<uint32_t> output_indices_;std::vector<uint32_t> node_indices_;std::vector<uint32_t> tensor_indices_;};std::string name_;std::string version_;std::vector<uint32_t> input_indices_;std::vector<uint32_t> output_indices_;std::vector<mindspore::schema::Tensor *> all_tensors_;std::vector<Node *> all_nodes_;std::vector<SubGraph *> sub_graphs_;std::string ToString() const;
};struct MS_API Model {LiteGraph graph_;char *buf = nullptr;size_t buf_size_ = 0;LiteModelType model_type_ = mindspore::lite::ModelType_MSLite;void *deobf = nullptr;/// \brief Static method to create a Model pointer.static Model *Import(const char *model_buf, size_t size);/// \brief Static method to create a Model pointer.static Model *Import(const char *filename);/// \brief  method to export model to file.static int Export(Model *model, const char *filename);/// \brief  method to export model to buffer.static int Export(Model *model, char *buf, size_t *size);/// \brief Free meta graph temporary buffervirtual void Free() = 0;/// \brief Free all temporary buffer.EG: nodes in the model.virtual void Destroy() = 0;/// \brief Model destruct, free all memoryvirtual ~Model() = default;
};
Struct LiteGraph{struct Node {std::string name_;std::string op_type_;int node_type_;const void *primitive_ = nullptr;std::shared_ptr<void> base_operator_ = nullptr;std::vector<uint32_t> input_indices_;std::vector<uint32_t> output_indices_;int quant_type_;int device_type_ = -1;};struct SubGraph {std::string name_;std::vector<uint32_t> input_indices_;std::vector<uint32_t> output_indices_;std::vector<uint32_t> node_indices_;std::vector<uint32_t> tensor_indices_;};std::string name_;std::string version_;std::vector<uint32_t> input_indices_;std::vector<uint32_t> output_indices_;std::vector<mindspore::schema::Tensor *> all_tensors_;std::vector<Node *> all_nodes_;std::vector<SubGraph *> sub_graphs_;std::string ToString() const;
}
class MS_API LiteModel : public Model {...
}

三、模型加载核心流程

在这里插入图片描述未完待续…

http://www.xdnf.cn/news/10650.html

相关文章:

  • Java线程同步与内存模型详解
  • react与vue的渲染原理
  • 第十二节:第四部分:集合框架:List系列集合:LinkedList集合的底层原理、特有方法、栈、队列
  • css使用scoped之后样式失效问题
  • Day43打卡(补41+42) @浙大疏锦行
  • Git实战--基于已有分支克隆进行项目开发的完整流程
  • springboot 集成webFilter登录认证信息过滤 DEMO原型介绍
  • 混和效应模型在医学分析中的应用
  • 16.FreeRTOS
  • 学习BI---基本操作---数据集操作
  • HealthBench医疗AI评估基准:技术路径与核心价值深度分析(下)
  • 机器人夹爪的选型与ROS通讯——机器人抓取系统基础系列(六)
  • TomatoSCI数据分析实战:探索社交媒体成瘾
  • 【计算机网络】第3章:传输层—拥塞控制原理
  • php执行后报502,无错误提示的排查和解决
  • 前端面经高阶组件HOC 和 HOOKS Redux
  • 企业展示型网站模板HTML5网站模板下载指南
  • 【具身智能】【机械臂】各类机械臂对比
  • SQL Views(视图)
  • c++类和对象-继承
  • 打家劫舍与最长有效括号:动态规划与字符串处理的双重魅力
  • 也说字母L:柔软的长舌
  • Cursor 0.51 全网首歌新功能深度体验:Generate Memories 让 AI 编程助手拥有“记忆“
  • Docker私有仓库Harbor安装指南
  • SQL进阶之旅 Day 11:复杂JOIN查询优化
  • pytorch学习之矩阵分解
  • 在Linux中配置内网可访问的YUM光盘源
  • Python实例题:自联想器的Python实现
  • 【存储基础】存储设备和服务器的关系和区别
  • UE特效Niagara性能分析