ACPO
The current ACPO module primarily consists of three input parts.
- ONNX model, which is a trained ACPO model.
- Compiler plug-in, which is used to run ONNX model inference and obtain tuning parameters.
- AI4Compiler framework, which provides ONNX inference engine and GCC optimization commands.
Users can train an AI model based on an open-source machine learning framework and export the model in ONNX format. For the AI model, a corresponding compiler plug-in must be provided. The plug-in includes at least three modules that can:
- Extract the compiler input features required by the AI model.
- Drive the inference engine to call AI model inference.
- Annotate the inference results back into the compiler's data structure.
In the following test case, you only need to add three compilation options related to the plug-in to the compile command for compiling the object binary file each time: plug-in path, AI model path corresponding to the plug-in, and inference engine path. In this way, you can enable the ACPO model during compilation.
1 2 3 4 5 6 7 8 9 10 11 12 | # If onnxruntime is installed in a non-system folder, set environment variables. # 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 |
The currently supported plug-ins are located in the directory at the same level as the $(ai4c-gcc --inference-engine) output path, and the supported models are stored in path/to/your/gcc/lib64/AI4C.
- The compiler plug-in for compiling the AI model must match the compiler for the target optimization application; otherwise, a compilation error may occur due to inconsistent compiler versions.
- Currently, AI4C only supports using plug-ins for the ACPO pass implemented in the CC1 stage of the GCC compiler.
The following examples use two ACPO models that operate at different compilation stages. The loop unrolling and function inlining models are in the CC1 optimization stage, using the GCC plug-in form to implement AI model adaptation and inference. The BOLT basic block sampling precision correction model is located in the BOLT post-link optimization stage.
Loop Unrolling and Function Inlining Models
The following lists compilation tuning options for loop unrolling and function inlining models.
Option Name |
Description |
|---|---|
-fplugin |
Specifies the absolute path to the loop unrolling and function inlining plug-in (-fplugin=/path/to/<ipa_inline_unroll_plugin>.so). |
-fplugin-arg-<ipa_inline_unroll_plugin>-engine |
Specifies the absolute path to the function inlining ONNX model's inference engine (-fplugin-arg-<ipa_inline_unroll_plugin>-inline_model=/path/to/inference_engine.so), and it must be enabled together with -fplugin. The path to /path/to/inference_engine.so can be obtained using ai4c-gcc --inference-engine. |
-fplugin-arg-<ipa_inline_unroll_plugin>-inline_model |
Specifies the absolute path to the function inlining ONNX model (-fplugin-arg-<ipa_inline_unroll_plugin>-inline_model=/path/to/inline_model.onnx), and it must be enabled together with -fplugin and -fplugin-arg-<ipa_inline_unroll_plugin>-engine. |
-fplugin-arg-<ipa_inline_unroll_plugin>-unroll_model |
Specifies the absolute path to the loop unrolling ONNX model (-fplugin-arg-<ipa_inline_unroll_plugin>-unroll_model=/path/to/unroll_model.onnx), and it must be enabled together with the -fplugin and -fplugin-arg-<ipa_inline_unroll_plugin>-engine options. |
Users can simultaneously enable multiple ACPO models within a GCC plug-in, for example:
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 Basic Block Sampling Precision Correction Model
The BOLT basic block sampling precision correction model corresponds to the following BOLT optimization options.
Option Name |
Description |
|---|---|
-block-correction |
Enables the CFG BB Count option for AI optimization. This option must be enabled together with -model-path to specify the ONNX model. |
-model-path |
Specifies the absolute path to the ONNX model (-model-path=/path/to/model.onnx). This option must be enabled together with -block-correction. |
-annotate-threshold |
Sets the confidence threshold of the model prediction result. The default value is 0.95. |
Custom optimization options in BOLT can be enabled through GCC's -fbolt-option, for example:
1 | g++ -fbolt-use=<gcov_file> -fbolt-target=<bin_file> -fbolt-option=\"-block-correction -model-path=path/to/your/block_correction_model.onnx\" |