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;
}

 

posted @ 2022-03-15 20:25  鸭子船长  阅读(213)  评论(0编辑  收藏  举报