6.1.3.6. 如何编译模型

HAT集成了编译定点模型的能力,将pytorch的模型编译为可以上板运行的形式。用户可以选择下面的方式来编译模型。

6.1.3.6.1. 输入为 config 文件

此方式使用 tools/compile_perf.py 脚本,将 int_infer_trainermodelmodel_convert_pipeline 得到的模型编译成hbm,命令如下:

python3 tools/compile_perf.py -c {$CONFIG_FILE}

此命令会从config文件的 compile_cfg 字段获取编译相关配置,举例说明如下:


import horizon_plugin_pytorch

compile_cfg = dict(
    march=horizon_plugin_pytorch.march.March.BERNOULLI2, # xj3 使用 BERNOULLI2
    name="example_model",  # perf 信息的文件名
    out_dir="perf_results",  # perf 信息的输出路径
    hbm="tmp_compile/model.hbm",  # 编译出的模型文件路径
    layer_details=True,  # 是否输出每层的性能情况
    input_source=["pyramid"],  # 模型上板时的输入来源,一般选 pyramid 即可
    opt="O2",  # 优化等级,可以选择 O0, O1, O2, O3,优化程度从低到高
)

编译完成后,会生成以下几个文件:

  • tmp_compile/model.hbm :编译出的模型文件。

  • tmp_compile/model.hbir :编译过程中使用的模型的中间表示。

  • perf_results/example_model.jsonperf_results/example_model.html :以不同格式保存的模型上板相关的信息,如运行速度、资源占用等。