亲和API分析工具来自CANN Toolkit 开发套件包,可通过该工具分析出当前框架已有的可替换API,使用前请确保已参见《CANN 软件安装指南》完成开发套件包的安装。
亲和API扫描调优操作步骤如下。
cd Ascend-cann-toolkit安装目录/ascend-toolkit/latest/tools/ms_fmk_transplt/
./pytorch_analyse.sh -i 待分析脚本路径 -o 分析结果输出路径 -v 待分析脚本框架版本 -m affinity_apis
更多参数介绍请参见表1。
├── xxxx_analysis // 分析结果输出目录 │ ├── affinity_api_call.csv // 可替换为亲和API的原生API调用列表 │ ├── pytorch_analysis.txt // 分析过程日志
affinity_api_call.csv存放了原生API的调用信息,包括以下内容:
其中Api Type包括class(类)、function(方法)、torch(Pytorch框架API)以及special(特殊表达式)。
用户可以根据csv文件,在脚本文件的指定位置找到原生API调用,将原生API调用手动替换为指定的亲和API以提高模型性能。
以开源OpenLab仓库mmdetection为例。
git clone https://github.com/open-mmlab/mmdetection.git
cd /usr/local/Ascend/ascend-toolkit/latest/tools/ms_fmk_transplt/
./pytorch_analyse.sh -i /path_to_mmdeteciton/ -o /home/test/ -v 1.8.1 -m affinity_apis
回显如下:
2023-06-27 16:48:34 [INFO] Start to check input path... 2023-06-27 16:48:34 [INFO] PyTorch analysis start working now, please wait for a moment. 2023-06-27 16:48:34 [INFO] Analysis start... 2023-06-27 16:48:34 [INFO] [Progress: 0.00%] Start analysis mmdetection/setup.py. 2023-06-27 16:48:39 [INFO] [Progress: 0.00%] Analysis mmdetection/setup.py complete. 2023-06-27 16:48:39 [INFO] [Progress: 0.07%] Start analysis mmdetection/.dev_scripts/batch_test_list.py. 2023-06-27 16:48:40 [INFO] [Progress: 0.07%] Analysis mmdetection/.dev_scripts/batch_test_list.py complete. ... 2023-06-27 17:00:22 [INFO] [Progress:100.00%] Analyse run success, welcome to the next use. 2023-06-27 17:00:22 [INFO] The detailed transplant result files are in the output path you defined, the relative path is T1_analysis. +-----------------------+------------+ | files | statistics | +-----------------------+------------+ | affinity_api_call.csv | 57 | +-----------------------+------------+
修改前:
condition = input1 < 0.5 value = 0. input1[condition] = value return input1
修改后:
condition = input1 < 0.5 value = 0. return npu_fast_condition_index_put(input1, condition, value)