写点什么

飞桨 x 昇腾生态适配方案:12_ 动态 OM 推理

作者:小顺637
  • 2025-05-12
    北京
  • 本文字数:2640 字

    阅读完需:约 9 分钟

动态 BatchSize OM 推理

以档位 1 2 4 8 档为例,设置档位为 2,本程序将获取实际模型输入组 Batch,每 2 个输入为一组,进行组 Batch。示例命令:


python3 -m ais_bench --model ./inference/om/dynamic_batch_size_det.om --input ./inference/input/ --output ./inference/output/ --dymBatch 2
复制代码


推理结果:



输出结果:执行完毕后,输出会被保存在 ./inference/output/路径下:


inference/output/    ├── 2025_03_17-11_11_48        ├── image_0.bin    └── 2025_03_17-11_11_48_summary.json
复制代码

动态分辨率 OM 推理

以档位 224,224;448,448 档为例,设置档位为 224,224,本程序将获取实际模型输入组 Batch。示例命令:


python3 -m ais_bench --model ./inference/om/dynamic_image_size_det.om --input ./inference/input/ --output ./inference/output/ --dymHW 224,224
复制代码


推理结果:



输出结果:执行完毕后,输出会被保存在 ./inference/output/路径下:


inference/output/    ├── 2025_03_17-11_47_25        ├── image_0.bin    └── 2025_03_17-11_47_25_summary.json
复制代码

动态维度 OM 推理

固定维度模式

以设置档位 1,3,224,224 为例,本程序将获取实际模型输入组 Batch。示例命令:


python3 -m ais_bench --model ./inference/om/dynamic_dims_det.om --input ./inference/input/ --output ./inference/output/ --dymDims x:1,3,224,224
复制代码


推理结果:



输出结果:执行完毕后,输出会被保存在 ./inference/output/路径下:


inference/output/    ├── 2025_03_17-11_53_04        ├── image_0.bin    └── 2025_03_17-11_53_04_summary.json            
复制代码

自动设置维度模式

动态维度模型输入数据的 Shape 可能是不固定的,比如一个输入文件 Shape 为 1,3,224,224,另一个输入文件 Shape 为 1,3,448,448。若两个文件同时推理,则需要设置两次动态 Shape 参数,当前不支持该操作。针对该场景,增加动态 Dims 模式,可以根据输入文件的 Shape 信息,自动设置模型的 Shape 参数。示例命令:


python3 -m ais_bench --model ./inference/om/dynamic_dims_det.om --input ./inference/input/ --output ./inference/output/ --auto_set_dymdims_mode 1
复制代码


推理结果:



输入文件:


# 输入的input文件夹内容如下ls ./inference/input/image_1_224_224.npy  image_1_448_448.npy
复制代码


输出结果:执行完毕后,输出会被保存在 ./inference/output/路径下:


inference/output/    ├── 2025_03_18-23_54_55        ├── image_1_224_224_0.bin        ├── image_1_448_448_0.bin    └── 2025_03_18-23_54_55_summary.json
复制代码

动态 shape OM 推理

固定 shape 模式

以 ATC 设置[18,3,224448,224~448],设置档位 1,3,256,256 为例,本程序将获取实际模型输入组 Batch。动态 Shape 的输出大小通常为 0,建议通过--outputSize 参数设置对应输出的内存大小。


示例命令:


python3 -m ais_bench --model ./inference/om/dynamic_shape_det_linux_aarch64.om --input ./inference/image.npy --output ./inference/output/ --dymShape x:1,3,256,256 --outputSize 10000
复制代码


推理结果:



输出结果:执行完毕后,输出会被保存在 ./inference/output/路径下:


inference/output/    ├── 2025_03_19-00_12_07        ├── image_0.bin    └── 2025_03_19-00_12_07_summary.json            
复制代码

自动设置 Shape 模式

动态 Shape 模型输入数据的 Shape 可能是不固定的,比如输入文件 Shape 分别为 1,3,224,224;1,3,256,256;1,3,448,448。若三个文件同时推理,则需要设置三次动态 Shape 参数,当前不支持该操作。针对该场景,增加动态 Shape 模式,可以根据输入文件的 Shape 信息,自动设置模型的 Shape 参数。


示例命令:


python3 -m ais_bench --model ./inference/om/dynamic_shape_det_linux_aarch64.om --auto_set_dymshape_mode 1  --input ./inference/input/ --output ./inference/output/ --outputSize 10000
复制代码


推理结果:



输入文件:


# 输入的input文件夹内容如下ls ./inference/input/image_1_224_224.npy  image_1_256_256.npy  image_1_448_448.npy
复制代码


输出结果:执行完毕后,输出会被保存在 ./inference/output/路径下:


inference/output/    ├── 2025_03_19-00_21_55        ├── image_1_224_224_0.bin        ├── image_1_256_256_0.bin        ├── image_1_448_448_0.bin    └── 2025_03_19-00_21_55_summary.json
复制代码


注意:该场景下的输入文件必须为 npy 格式,如果是 bin 文件将获取不到真实的 Shape 信息。

profiler 或 dump 场景

--profiler 参数和--dump 参数,必须要增加--output 参数,指示输出路径。


--profiler 和--dump 可以分别使用,但不能同时启用。


python3 -m ais_bench  --model ./inference/om/det.om --output ./inference/output/ --dump 1python3 -m ais_bench  --model ./inference/om/det.om --output ./inference/output/ --profiler 1
复制代码

常见问题

未设置环境变量

每次使用离线推理前务必先设置环境变量:source /usr/local/Ascend/ascend-toolkit/set_env.sh

实际输入 HW 和 OM 模型不一致

报错信息:



错误原因:det.om 模型的输入 input_shape=x:1,3,224,224;而 image.npy 的输入为:1,3,256,256。

实际输入 Dims 和 OM 模型不一致

报错信息:



错误原因:dynamic_dims_det.om 模型的输入 dynamic_dims="1,224,224;8,448,448";而实际的输入为:1,3,224,224;1,3,448,448。

动态 OM 模型未设置动态参数

报错信息:



错误原因:使用动态 OM 模型(包括动态 BatchSize、动态分辨率、动态维度、动态 shape)推理,但并未设置相关的动态参数。

CANN 包版本老旧

报错信息:



错误原因:CANN 包版本老旧,参考“环境准备->更新 CANN 包”章节更换 CANN 版本。

API 推理

请参考 ais_bench interface python API 使用指南:https://gitee.com/ascend/tools/blob/master/ais-bench_workload/tool/ais_bench/API_GUIDE.md

参考链接


用户头像

小顺637

关注

还未添加个人签名 2023-01-19 加入

还未添加个人简介

评论

发布
暂无评论
飞桨x昇腾生态适配方案:12_动态OM推理_飞桨_小顺637_InfoQ写作社区