[Onnx简化库深度剖析] OnnxSimplifier和OnnxOptimizer解读-(2)
简介
上篇重点介绍了OnnxSimplifier的库,其中的FixedPointFn迭代优化函数利用到了OnnxOptimizer这个官方的优化库,大部分的优化细节是在OnnxOptimizer内的。因此本篇重点介绍一下OnnxOptimizer库的内部优化细节和逐个介绍每个优化pass和手段。
OnnxOptimizer基本概念原理
Pass
- 含义:所有优化项的基类,每个优化项必须拥有唯一的名字,用来注册和管理。
- 概念:
- 优化类型(PassType):
- Fuse: 会融合算子的优化项
- Nop: 会移除无用的算子的优化项
- Separate:
- Immutable:
- Replace: 会进行节点替换的优化项
- Other: 其他
- (PassAnalysisType):
- Empty
- CountBased
- (PassEfficiency):
- Partial
- Complete
- (PassOptimizationType):
- None
- Compute: 针对计算优化
- Memory: 针对内存优化
- ComputeMemory: 针对计算和内存优化
- Stability: 稳定
- 优化类型(PassType):
- 基类的代码:
class Pass {PassType pass_type;PassEfficiency pass_efficiency;PassOptimizationType pass_optimization_type;public:Pass(PassType pass_type, PassEfficiency pass_efficiency,PassOptimizationType pass_optimization_type);virtual ~Pass();PassType getPassType() const {return this->pass_type;}PassEfficiency getPassEfficiency() const {return this->pass_efficiency;}PassOptimizationType getPassOptimizationType() const {return this->pass_optimization_type;}virtual PassAnalysisType getPassAnalysisType() const = 0; // virtual std::string getPassName() const = 0; // 返回优化项的名字virtual bool initializePass(Graph &) { // 可有可无的函数实现return false;}virtual bool finalizePass(Graph &) { // 可有可无的函数实现return false;}virtual std::shared_ptr<PostPassAnalysis> runPass(Graph &graph) = 0; // 子类必须实现该函数,用于运行模型优化protected:// Iterates through the elements in the graph and counts the number of times// the transform is successfully run.unsigned int DescendOnGraphAttributesAndCount(Node *n, std::function<unsigned int(Graph &)> fn);// A more general version of the function above that doesn't constrain the// return type of fn.void DescendOnGraphAttributesUnconstrained(Node *n,std::function<void(Graph &)> fn);
};
具体的Pass实现和细节罗列
Pass | PassName | PassType | PassEfficiency | PassOptimizationType | 描述 |
---|---|---|---|---|---|
AdjustAdd | adjust_add | Immutable | Complete | Compute | 交换add第一个constant_input到第二位置上 |
RenameInputOutput | rename_input_output | Other | Complete | None | 修改模型输入/输出为统一的预设的或者默认的格式:input_%d/output_%d |
SetUniqueNameForNodes | set_unique_name_for_nodes | Other | Complete | None | 为name为空的node设置唯一的node_name |
EliminateNopCast | eliminate_nop_cast | Nop | Complete | Compute | 移除那些目标输出数据类型等于输入数据类型的cast算子 |
EliminateNopDropout | eliminate_nop_dropout | Nop | Complete | Compute | 移除那些ratio=0.0的dropout算子 |
EliminateNopFlatten | eliminate_nop_flatten | Nop | Complete | Compute | 移除那些对shape无影响的Flatten算子 |
ExtractConstantToInitializer | extract_constant_to_initializer | Nop | Complete | Memory | 移除Constant算子,放置到Initializer内 |
EliminateConsecutiveIdempotentOps | eliminate_consecutive_idempotent_ops | Fuse | Complete | Compute | 融合连续的op,比如"Ceil", “Floor”, “Round”, “Relu”, "Reshape"等 |
EliminateIfWithConstCond | eliminate_if_with_const_cond | Nop | Complete | Compute | 消除输入条件cond为常量的if算子 |
EliminateNopMonotoneArgmax | eliminate_nop_monotone_argmax | Nop | Partial | Compute | 消除输入条件cond为常量的if算子 |
…
总结
上述的Pass只是其中的一小部分,还有将近二十来个Pass没有说明清楚,因此这个留到后续的文章继续说明。