PaddlePaddle inference 源码分析(五)-graph和pass
本节讲述图的解析以及pass图优化。
模型文件使用protobuf保存。它的嵌套关系如下:block->operator->var。
block中保存了很多operator,而operator则包含了自身的参数定义等。
一般情况下,大多数operator都放置在block0中,block1和2中一般放置那种算子内部的block,如while_op等。
op之间的连接关系依靠var名称来区别。一般情况下,所有var的名称都不相同,op1的输出var与op2的输入var对应。
进行pass处理时,会先调用理解类pass将整图读入,并且保存op间的依赖关系。
而后进行图优化。大部分图优化处理都是融合处理,即已知op1为calc1、op2为calc2,两者符合规则可以融合。
优化后仍旧得到PromDesc(proto)
读取后生成顺序执行的vector<operator> ,每次预测时顺序执行op。
paddle/fluid/frame/op_def.proto包含了op的格式
message OpDef { message VarDef { required string name = 1; // For the type of input / output variables. reserved 2; } message AttrDef { required string name = 1; required AttrType type = 2; } message Desc { repeated VarDef inputs = 1; repeated VarDef outputs = 2; repeated AttrDef attrs = 3; } required string type = 1; required Desc def = 2; optional Desc extra = 3; }
paddle/fluid/framework/framework.proto包含了图的格式定义
message VarDesc { message Attr { required string name = 1; required AttrType type = 2; optional int32 i = 3; optional string s = 4; repeated int32 ints = 5; }; required string name = 1; required VarType type = 2; optional bool persistable = 3 [ default = false ]; // True if the variable is an input data and // have to check the feed data shape and dtype optional bool need_check_feed = 4 [ default = false ]; optional bool is_parameter = 5 [ default = false ]; optional bool stop_gradient = 6 [ default = false ]; repeated Attr attrs = 7; } //block中包含了op的集合,以及输入输出的var定义 //当前只有0是整图,从1开始都是while_op对应的内部block message BlockDesc { required int32 idx = 1; required int32 parent_idx = 2; repeated VarDesc vars = 3; repeated OpDesc ops = 4; optional int32 forward_block_idx = 5 [ default = -1 ]; } // In some cases, Paddle may perform operator definition iterations, // and the operator uses OpVersionMap for compatibility testing. message OpVersion { required int32 version = 1; } message OpVersionMap { message OpVersionPair { required string op_name = 1; required OpVersion op_version = 2; } repeated OpVersionPair pair = 1; } // Please refer to // https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/program.md // for more details. // TODO(panyx0718): A model can have multiple programs. Need a // way to distinguish them. Maybe ID or name? //整体图的结构,包含了block集合 message ProgramDesc { reserved 2, 3; // For backward compatibility. repeated BlockDesc blocks = 1; optional Version version = 4; optional OpVersionMap op_version_map = 5; }
联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-03-15 Leetcode 48. 图像旋转 tag 数组
2019-03-15 docker占满linux磁盘根目录的解决办法
2018-03-15 bazel、tensorflow_serving、opencv编译问题
2017-03-15 ubuntu 16.04 更新后搜狗输入法无法输入中文的问题
2017-03-15 apt-get指令的autoclean,clean,autoremove的区别
2017-03-15 储备的小站——更新中
2017-03-15 apk解包——修改后缀为zip