diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/PyTorch\347\246\273\347\272\277\346\216\250\347\220\206-FAQ.md" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/PyTorch\347\246\273\347\272\277\346\216\250\347\220\206-FAQ.md" index e74e7ae560fee7f3a0cbe35e285cb0a56a717648..4961a6b7448b965ceb7188a53111e291229c552b 100644 --- "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/PyTorch\347\246\273\347\272\277\346\216\250\347\220\206-FAQ.md" +++ "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/PyTorch\347\246\273\347\272\277\346\216\250\347\220\206-FAQ.md" @@ -29,21 +29,21 @@ - 注册ONNX自定义算子(方式一) ```python class AdaptiveAvgPoolOp(torch.autograd.Function): - + @staticmethod def forward(ctx, x, output_size): out = torch.randn(x.shape[0], x.shape[1], output_size[0], output_size[1]).to(x.dtype) return out - + @staticmethod def symbolic(g, x, output_size): out = g.op('AdaptiveAvgPool2d', x, output_size_i = output_size) return out - + def adaptive_avg_pool_op(x, output_size): out = AdaptiveAvgPoolOp.apply(x, output_size) return out - + #x = F.adaptive_avg_pool2d(input, output_size=bin_size) x = adaptive_avg_pool_op(input, (bin_size, bin_size)) # 替换上面代码 ``` @@ -87,22 +87,22 @@ out_name = node.outputs[0] node.set_input(0, f'Reshape_{node.name}') node.set_output(0, f'{node.name}_reshape') - + reshape2 = graph.add_node(f'Reshape2_{node.name}', 'Reshape') graph.add_initializer(f'shape2_{node.name}', np.array(shapes[idx][2])) reshape2.inputs = [f'{node.name}_reshape', f'shape2_{node.name}'] reshape2.outputs = [f'Reshape2_{node.name}_out'] - + resize2 = graph.add_node(f'Resize2_{node.name}', 'Resize') graph.add_initializer(f'size_{node.name}', np.array(shapes[idx][3])) resize2.inputs = [f'Reshape2_{node.name}_out', node.inputs[1], node.inputs[1], f'size_{node.name}'] resize2.outputs = [f'Resize2_{node.name}'] - + reshape3 = graph.add_node(f'Reshape3_{node.name}', 'Reshape') graph.add_initializer(f'shape3_{node.name}', np.array(shapes[idx][4])) reshape3.inputs = [f'Resize2_{node.name}', f'shape3_{node.name}'] reshape3.outputs = [out_name] - + graph.save('modify.onnx') if __name__ == "__main__": @@ -193,5 +193,45 @@ rbuv_swap_switch: false } ``` + - 增加atc参数 在原有atc命令基础上增加 [--enable_small_channel=1](https://support.huaweicloud.com/atctool-cann503alpha2infer/atlasatc_16_0077.html) 和 [--insert_op_conf=path/to/insert_op.cfg](https://support.huaweicloud.com/atctool-cann503alpha2infer/atlasatc_16_0068.html#ZH-CN_TOPIC_0000001152734182) + +# 5.2 GPU推理(trtexec)报错 + ++ 错误现象 + + GPU环境基于trtexec进行推理常见的报错问题: + + 1. 算子问题 + + ```shell + # trt不支持xxx算子,且没有对应的plugin实现 + [12/09/2021-02:46:14] [I] [TRT] ModelImporter.cpp:135: No importer registered for op: xxx. Attempting to import as plugin. + [12/09/2021-02:46:14] [I] [TRT] builtin_op_importers.cpp:3771: Searching for plugin: xxx, plugin_version: 1, plugin_namespace: + [12/09/2021-02:46:14] [E] [TRT] INVALID_ARGUMENT: getPluginCreator could not find plugin xxx version 1 + ``` + + 2. 内存限制 + + ```shell + # out of memory + [TRT] Internal error: plugin node (Unnamed Layer* xxx) [PluginV2xxx]requires xxx bytes of scratch space, but only xxx is available. Try increasing the workspace size with IBuilderConfig::setMaxWorkspaceSize() if using IBuilder::buildEngineWithConfig, or IBuilder::setMaxWorkspaceSize() if using IBuilder::buildCudaEngine. + ``` + + + ++ 原因分析&&解决方案 + + 1. 算子问题 + + 对于算子支持问题,默认采用以下两种解决方案: + + 1. 采用更高版本的trt版本(默认为trt7.xxx),如trt8 + 2. 基于onnxruntime进行onnx的离线推理得到性能 + 3. 基于2.方案仍然不生效,则基于在线推理得到性能 + + 2. 内存限制 + + 1. 内存限制的问题,默认不测当前输入shape/batch_size + diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/readme.md" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/readme.md" new file mode 100644 index 0000000000000000000000000000000000000000..d3cdddb8e7e65d73f5195e98d23f849906c31ae6 --- /dev/null +++ "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/readme.md" @@ -0,0 +1,26 @@ +# 案例格式 + +基本目录格式(尽量遵循): + ++ 模型背景 ++ 问题现象 ++ 调试思路 ++ 解决方案 ++ 总结 ++ 附件 + +详情见: [案例模板](https://gitee.com/wangjiangben_hw/ascend-pytorch-crowdintelligence-doc/blob/master/Ascend-PyTorch%E7%A6%BB%E7%BA%BF%E6%8E%A8%E7%90%86%E6%8C%87%E5%AF%BC/%E4%B8%93%E9%A2%98%E6%A1%88%E4%BE%8B/%E6%A1%88%E4%BE%8B%E6%A8%A1%E6%9D%BF.md) + +# 案例 TODO-List + +| 所属类别 | 问题描述 | 状态 | +| -------- | ---------------------- | ---- | +| 功能打通 | 动态分档问题 | DONE | +| 功能打通 | 自定义算子问题 | TODO | +| 功能打通 | 网络模块连续调用问题 | TODO | +| 性能调优 | AICPU优化问题 | TODO | +| 相关工具 | 精度调试工具及流程介绍 | TODO | +| 相关工具 | 性能调优工具及流程介绍 | TODO | + + + diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/HigherNet\346\250\241\345\236\213\345\212\250\346\200\201\345\210\206\346\241\243\351\227\256\351\242\230\350\247\243\345\206\263\346\241\210\344\276\213.md" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/HigherNet\346\250\241\345\236\213\345\212\250\346\200\201\345\210\206\346\241\243\351\227\256\351\242\230\350\247\243\345\206\263\346\241\210\344\276\213.md" new file mode 100644 index 0000000000000000000000000000000000000000..8dc728884413acfda2467767201c510b3a5ed449 --- /dev/null +++ "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/HigherNet\346\250\241\345\236\213\345\212\250\346\200\201\345\210\206\346\241\243\351\227\256\351\242\230\350\247\243\345\206\263\346\241\210\344\276\213.md" @@ -0,0 +1,60 @@ +# HigherNet模型动态分档问题解决案例 + +[TOC] + +## 模型背景 + +[HigherNet](https://github.com/HRNet/HigherHRNet-Human-Pose-Estimation)出自CVPR2020,为经典的人体检测网络模型,基本算法框架见下图。HigherNet在推理过程中,输入图片的shape不是单一的固定输入,但是不是完全的动态,而是按照64(stride)倍数分为若干挡位,适合采用分档模型的方案。对应的代码可见:[modelzoo](https://gitee.com/ascend/modelzoo/tree/master/contrib/ACL_PyTorch/Research/cv/pose_estimation)。 + +![image-20211209110458956](C:\Users\hwsc\AppData\Roaming\Typora\typora-user-images\image-20211209110458956.png) + +关于模型分档,这里简要做下说明,具体可以参考:[昇腾社区文档](https://support.huaweicloud.com/onlineinfer-cann503alpha2training/atlasoiug_26_0013.html)。动态分档特性指的是模型支持有限个输入尺寸,且模型精度性能和单模型推理基本保持一致,转换后的动态模型结构如下: + +![image-20211209110458956](C:\Users\hwsc\AppData\Roaming\Typora\typora-user-images\image-20211209110458956.png) + +## 问题现象 + +对于`HigherNet`来说,模型的输入尺寸(宽高)如下: + +``` +1024,512;960,512;896,512;832,512;768,512;704,512;640,512;576,512;512,512;512,576;512,640;512,704;512,768;512,832;512,896;512,960;512,1024 +``` + +采用静态shape的方案无法解决上述问题,可以采用动态分档的方案。 + +## 调试思路 + +### 定位阶段 + +略 + + + +### 解决思路 + +核心:动态分档模型转化+动态分档模型推理+动态分档模型数据后处理 + + + +## 解决方案 + +1. 动态分档模型转化 + + ```shell + atc --framework=5 --model=pose_higher_hrnet_w32_512_bs1_dynamic.onnx --output=higherhrnet_sim_fendang --input_format=NCHW --input_shape="input:1,3,-1,-1" --dynamic_image_size="1024,512;960,512;896,512;832,512;768,512;704,512;640,512;576,512;512,512;512,576;512,640;512,704;512,768;512,832;512,896;512,960;512,1024" --log=debug --soc_version=Ascend310 + ``` + +2. 动态分档模型推理 + +3. 动态分档模型数据后处理 + + + +## 关于xxx问题的总结 + +概括整个解决方案,总结该案例得到的经验教训,如果有认为需要新增或者增强的功能需求,也可以在社区提issue并附上链接 + + + +## 附件 + diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/d14b7167-e835-4932-8e31-cf3adfafda75.png" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/d14b7167-e835-4932-8e31-cf3adfafda75.png" new file mode 100644 index 0000000000000000000000000000000000000000..89e31389062748cc328186a7a395cb1ab9c3bf4e Binary files /dev/null and "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/d14b7167-e835-4932-8e31-cf3adfafda75.png" differ diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/ed7abd81-490d-4d96-af5e-1feb41756b41.png" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/ed7abd81-490d-4d96-af5e-1feb41756b41.png" new file mode 100644 index 0000000000000000000000000000000000000000..d57633a784801fc64089ecc4401b74c27c3edc6e Binary files /dev/null and "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/ed7abd81-490d-4d96-af5e-1feb41756b41.png" differ diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/image-20211209110458956.png" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/image-20211209110458956.png" new file mode 100644 index 0000000000000000000000000000000000000000..803e045d7564b4e36dc71140dae143cf6f367eba Binary files /dev/null and "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/\344\270\223\351\242\230\346\241\210\344\276\213/\345\212\237\350\203\275\346\211\223\351\200\232/imgs/image-20211209110458956.png" differ