中文
注册
我要评分
文档获取效率
文档正确性
内容完整性
文档易理解
在线提单
论坛求助

AI辅助编译优化

当前的AI辅助编译优化模块,主要由三部分输入组成:

  • ONNX模型,训练后的辅助编译优化模型。
  • 编译器插件,用于运行ONNX模型推理并获取优化参数。
  • AI4Compiler框架,提供ONNX推理引擎和GCC优化编译命令。

用户实现根据开源机器学习框架训练一个AI模型,输出ONNX格式。同时,针对该AI模型提供一个对应的编译器插件,插件内至少包含三个模块:

  • 提取AI模型所需的编译器输入特征。
  • 驱动推理引擎调用AI模型执行推理。
  • 标注推理结果回编译器的数据结构。

在下述测试例中,仅需要在每次编译目标二进制的编译命令中,增加三个与插件相关的编译选项:插件路径、插件对应的AI模型路径、推理引擎路径、即可在编译时使能AI辅助编译优化模型。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 若 onnxruntime 安装在非系统的文件夹下,注意设置环境变量
# export LD_LIBRARY_PATH=path/to/your/onnxruntime/lib64/:$LD_LIBRARY_PATH

gcc_compiler=path/to/your/gcc
infer_engine_path=$(ai4c-gcc --inference-engine)
model_path=path/to/your/model.onnx
plugin_path=path/to/your/<model_plugin>.so

$gcc_compiler test.c -O2 -o test                            \
    -fplugin=$plugin_path                                   \
    -fplugin-arg-<model_plugin>-model=$model_path           \
    -fplugin-arg-<model_plugin>-engine=$infer_engine_path

当前已支持的插件存在于$(ai4c-gcc --inference-engine)的同目录下,已支持的模型存在于path/to/your/gcc/lib64/AI4C下。

  • 编译 AI 模型对应的编译器插件与编译目标优化应用的编译器需保证为同一个,否则会出现编译器版本不一致导致的编译报错。
  • 当前 AI4C 仅支持在 GCC 编译器 cc1 阶段实现的 AI 辅助编译优化 pass 使用插件形式。

下面通过两个位于不同编译阶段的AI辅助编译优化模型进行举例。循环展开与函数内联模型位域cc1编译优化阶段,使用GCC插件形式实现AI模型适配与推理;BOLT采样基本块精度修正模型位于BOLT连接后优化阶段。

循环展开与函数内联模型

循环展开与函数内联模型对应的编译优化选项如下:

表1

选项名

说明

-fplugin

指定循环展开与函数内联插件的绝对路径(-fplugin=/path/to/<ipa_inline_unroll_plugin>.so)。

-fplugin-arg-<ipa_inline_unroll_plugin>-engine

指定函数内联 ONNX 模型的推理引擎绝对路径(-fplugin-arg-<ipa_inline_unroll_plugin>-inline_model=/path/to/inference_engine.so),需要与-fplugin同时开启。/path/to/inference_engine.so的路径可通过ai4c-gcc --inference-engine获得。

-fplugin-arg-<ipa_inline_unroll_plugin>-inline_model

指定函数内联 ONNX 模型的绝对路径(-fplugin-arg-<ipa_inline_unroll_plugin>-inline_model=/path/to/inline_model.onnx),需要与-fplugin和-fplugin-arg-<ipa_inline_unroll_plugin>-engine同时开启。

-fplugin-arg-<ipa_inline_unroll_plugin>-unroll_model

指定循环展开 ONNX 模型的绝对路径(-fplugin-arg-<ipa_inline_unroll_plugin>-unroll_model=/path/to/unroll_model.onnx),需要与-fplugin和-fplugin-arg-<ipa_inline_unroll_plugin>-engine同时开启。

用户可同时启用一个 GCC 插件内的多个 AI 辅助编译优化模型,例如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
gxx_compiler=path/to/your/g++
infer_engine_path=$(ai4c-gcc --inference-engine)
inline_model_path=path/to/your/inline_model.onnx
unroll_model_path=path/to/your/unroll_model.onnx
plugin_path=path/to/your/<ipa_inline_unroll_plugin>.so

$gxx_compiler test.cc -O3 -o test -funroll-loops                           \    
  -fplugin=$plugin_path                                                    \
  -fplugin-arg-<ipa_inline_unroll_plugin>-engine=$infer_engine_path        \
  -fplugin-arg-<ipa_inline_unroll_plugin>-inline_model=$inline_model_path  \
  -fplugin-arg-<ipa_inline_unroll_plugin>-unroll_model=$unroll_model_path

BOLT采样基本块精度修正模型

BOLT 采样的基本块精度修正模型对应的 BOLT 优化选项如下:

表2

选项名

说明

-block-correction

开启 AI 优化 CFG BB Count 选项,需要与 -model-path 选项同时开启以指定 ONNX 模型。

-model-path

指定 ONNX 模型的绝对路径(-model-path=/path/to/model.onnx),需要与-block-correction同时开启。

-annotate-threshold

使用模型预测结果的置信度阈值,默认是 0.95。

BOLT 内自定义的优化选项可以通过 GCC 的-fbolt-option调用使能,例如:

1
g++ -fbolt-use=<gcov_file> -fbolt-target=<bin_file> -fbolt-option=\"-block-correction -model-path=path/to/your/block_correction_model.onnx\"