---
title: 应用级选项调优
description: "当前的应用级选项调优模块，主要由三部分输入组成："
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevkithistory/hgcc/hist-hgcc/kunpenghgcc_06_0074.html
sourcePath: /source/zh/kunpengdevkithistory/hgcc/hist-hgcc/kunpenghgcc_06_0074.html
indexId: 42b0167b4d1797ec769f605f9333c2eda3004a1950cd09b9f70cb1607959a35e71
---
# 应用级选项调优

当前的应用级选项调优模块，主要由三部分输入组成：

- 应用的编译与运行脚本（shell）：处理应用的编译流程（包括将生成的下一组选项替换进编译脚本内）、执行流程以及性能数据采集流程。
- 编译选项与动态库选项的参数搜索空间配置文件（YAML）：配置选项调优的参数搜索空间，可配置开关选项（编译优化/动态库）、编译参数、枚举选项。
- 性能值的配置文件（YAML格式）：配置多个性能项的权重，与目标优化方向（最大/最小值），需与“性能数据采集流程”所获取的性能值数量、顺序对应。

应用级选项调优工具将不断收集应用的性能数据，更新性能模型，并生成一组模型预期收益较高的新编译选项组合。通过应用的编译与运行脚本，将新的编译选项组合替换进编译脚本内，生成新二进制文件并执行下一轮运行，反复迭代调优以获得历史最优性能。

在开启应用级选项调优前，需安装以下依赖包：

```
pip install xgboost scikit-learn
yum install -y time
```

以下用例将使用不同的编译选项组合构建并调优test.cc 3 轮。应用的编译与运行脚本如下：

```
# ---------- run_test.sh ---------- #
parent_dir=$1                                               # path for intermediate tuning files
config=$(cat ${parent_dir}/tuning/config.txt)               # current compiler configuration file 
performance_file="${parent_dir}/tuning/performance.txt"     # current performance data file 

measure_raw_file="time.txt"

compiler=g++
compile_command="${compiler} test.cc -O2 -o test_opt_tuner"
eval "${compile_command} ${config}"                          # program compilation, appending tuning options 

run_command="time -p -o ${measure_raw_file} ./test_opt_tuner 3"
eval "${run_command}"                                        # program execution

info_collect_command="grep real ${measure_raw_file} | awk '{printf \"1 1 %s\", \$2}' > ${performance_file}"
eval "${info_collect_command}"                               # program performance collection

# ---------- run_option_tuner.sh ---------- #
ai4c-option-tune --test_limit 3 --runfile run_test.sh
    # --optionfile path/to/your/python<version>/site-packages/ai4c/option_tuner/input/options.yaml \
    # --libfile path/to/your/python<version>/site-packages/ai4c/option_tuner/input/options_lib.yaml \
    # --measurefile path/to/your/python<version>/site-packages/ai4c/option_tuner/input/config_measure.yaml
```

其中默认的选项与性能值配置文件存在于以下路径：path/to/your/python<version>/site-packages/ai4c/option_tuner/input/*.yaml

用户可根据需要修改编译选项与动态库选项配置文件，相关关键词为：

- required_*：必选调优项，将一直保留在调优中
- bool_*：可选的编译优化开关选项
- interval_*: 可选的编译参数（值选项，数据区间）
- enum_*: 可选的编译参数（枚举选项）

例如，```
required_config:
- -O2
bool_config:
- -funroll-loops
interval_config:
- name: --param max-inline-insns-auto
  default: 15
  min: 10
  max: 190
```


用户可根据需要修改性能值配置文件，相关关键词为：

- weight: 性能值权重
- optim: 目标优化方向（最大/最小值）

例如，

```
config_measure:
- name: throughput
  weight: 1
  optim: maximize
```

调优完成后，历史与最佳调优数据将保留在${parent_dir}/tuning/train.csv和${parent_dir}/tuning/result.txt中。
