溢出检测是针对NPU的PyTorch API,检测是否存在溢出的情况。当前仅支持识别aicore浮点溢出。
溢出检测原理:针对溢出阶段,开启acl dump模式,重新对溢出阶段执行,落盘数据。
from ptdbg_ascend import PrecisionDebugger debugger = PrecisionDebugger(dump_path="./overflow_dump", hook_name="overflow_check", step=[0]) debugger.configure_hook(overflow_nums=-1) # 请勿将以上初始化流程插入到循环代码中 # 模型初始化 # 下面代码也可以用PrecisionDebugger.start()和PrecisionDebugger.stop() debugger.start() # 需要dump的代码片段1 debugger.stop() debugger.start() # 需要dump的代码片段2 debugger.stop() debugger.step()
多卡使用时各卡单独计算溢出次数。
from ptdbg_ascend import PrecisionDebugger debugger = PrecisionDebugger(dump_path="./overflow_dump", hook_name="overflow_check", step=[0]) debugger.configure_hook(mode="acl", acl_config="./dump.json") # 请勿将以上初始化流程插入到循环代码中 # 模型初始化 # 下面代码也可以用PrecisionDebugger.start()和PrecisionDebugger.stop() debugger.start() # 需要dump的代码片段1 debugger.stop() debugger.start() # 需要dump的代码片段2 debugger.stop() debugger.step()
from ptdbg_ascend import PrecisionDebugger debugger = PrecisionDebugger(dump_path="./overflow_dump", hook_name="overflow_check", step=[0]) debugger.configure_hook(overflow_nums=-1) # 请勿将以上初始化流程插入到循环代码中 # 模型初始化 # 下面代码也可以用PrecisionDebugger.start()和PrecisionDebugger.stop() debugger.start() # 需要dump的代码片段1 debugger.stop() debugger.start() # 需要dump的代码片段2 debugger.stop() debugger.step()
from ptdbg_ascend import PrecisionDebugger debugger = PrecisionDebugger(dump_path="./overflow_dump", hook_name="dump", step=[0]) debugger.configure_hook(mode="acl", scope=["Functional_conv2d_1_backward"], acl_config="./dump.json", backward_input=["./overflow_dump/ptdbg_dump_v4.0/step0/rank0/Functional_conv2d_1_backward_1/Functional_conv2d_1_backward_input.0.npy"]) # 请勿将以上初始化流程插入到循环代码中 # 模型初始化 # 下面代码也可以用PrecisionDebugger.start()和PrecisionDebugger.stop() debugger.start() # 需要dump的代码片段1 debugger.stop() debugger.start() # 需要dump的代码片段2 debugger.stop() debugger.step()
针对前向溢出API,可以通过overflow_nums,配置允许的溢出次数,并将每次溢出API的全部ACL数据dump下来,到达指定溢出次数后停止,停止后会看到堆栈打印包含如下字段。
ValueError: [overflow xxx times]: dump file is saved in 'xxxxx.pkl'.
其中xxx times为用户设置的次数,xxxxx.pkl为文件生成路径。
针对输入正常但输出存在溢出的API,会训练执行目录下将溢出的API信息dump并保存为forward_info_{pid}.json和backward_info_{pid}.json,通过Ascend模型精度预检工具对json文件进行解析,输出溢出API为正常溢出还是非正常溢出,从而帮助用户快速判断。
精度预检工具执行命令如下:
# 下载att代码仓后执行如下命令 export PYTHONPATH=$PYTHONPATH:$ATT_HOME/debug/accuracy_tools/ cd $ATT_HOME/debug/accuracy_tools/api_accuracy_checker/run_ut python run_overflow_check.py -forward ./forward_info_0.json
反向过程溢出的API暂不支持精度预检功能。
当重复执行溢出检测dump操作时,需要删除上一次dump目录下的溢出检测dump数据,否则将因重名而报错。