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 277faaae8ccc854063bf3ac22512a82f4e55f09e..521f3738395ea2d29253b369473bf771d80930b8 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" @@ -14,6 +14,8 @@ - [4 精度调试常见问题](#4-精度调试常见问题) - [5 性能优化常见问题](#5-性能优化常见问题) - [5.1 如何使用AIPP进行性能提升](#51-如何使用aipp进行性能提升) +- [5.2 GPU推理(trtexec)报错](#52-gpu推理trtexec报错) + - [5.3 提升transpose的性能](#53-提升transpose的性能) # 1 介绍 本文目标读者为Ascend PyTorch模型离线推理开发者,用于指导开发者在昇腾服务器的CANN软件环境中,实现模型离线推理精度性能达标。这里仅列举模型离线推理中遇到的常见问题与解决方法,持续更新。 **FAQ上传格式** @@ -281,3 +283,50 @@ 1. 内存限制的问题,默认不测当前输入shape/batch_size +## 5.3 提升transpose的性能 +- 问题现象 + + om推理性能不达标,做完profiling后,查看op_summary文件,对aicore_time进行降序排列,发现transpose耗时占比较高。可以看出transopseD是其它算子耗时的10倍之多。 + ![transpose_profiling](./images/FAQ002_1.png) + +- 原因分析 + + 在ONNX模型中找到这些transpose出现的地方,发现是对数据量最大的两轴做transopse,进而导致性能差。 + ![transpose_src](./images/FAQ002_2.png) + +- 解决方案 + + 找到pytorch源码中的transpose出现地方,用于实现算子的等价替换。因为transose的输入是expand得到的,且transpose互换的两轴是相等的,所以想到了tile算子。最终通过实验使用reshape和tile实现同样功能。 + ```python + import torch + from torch import nn + + class SrcCode(nn.Module): + def forward(self,x): + y = x.expand(1024, 1024, 3).transpose(0, 1) + return y + + class Optimizer(nn.Module): + def forward(self,x): + t = x.reshape(1024,1,3).repeat((1,1024,1)) + return t + + src = SrcCode() + opt = Optimizer() + src.eval() + opt.eval() + + input1 = torch.randn(1024,3) + out = src(input1) + out2 = opt(input1) + # 精度对比 + print(((out2-out).abs()>1e-6).sum()) + torch.onnx.export(model,input1,'step_tile.onnx',opset_version=10) + ``` + pytorch侧精度对比通过,om侧精度对比也通过。导出的样例onnx对比如下 + + + + 性能提升结果:整网性能提升270%,性能达标。 + ![transpose_opt](./images/FAQ002_4.png) + ![opt_profiling](./images/FAQ002_5.png) diff --git "a/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/images/FAQ002_1.png" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/images/FAQ002_1.png" new file mode 100644 index 0000000000000000000000000000000000000000..31b731bcd6c3dc700d9858396af53a202b3cf437 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/images/FAQ002_1.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/images/FAQ002_2.png" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/images/FAQ002_2.png" new file mode 100644 index 0000000000000000000000000000000000000000..38159e419472a49a59e67b14c1cb7b3a9334a22f 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/images/FAQ002_2.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/images/FAQ002_3.png" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/images/FAQ002_3.png" new file mode 100644 index 0000000000000000000000000000000000000000..92ec1cd3226aab2b0f184cbae2b171affdd5e55a 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/images/FAQ002_3.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/images/FAQ002_4.png" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/images/FAQ002_4.png" new file mode 100644 index 0000000000000000000000000000000000000000..e2644e91b98b9e9f03e5e87141fa17f161522859 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/images/FAQ002_4.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/images/FAQ002_5.png" "b/Ascend-PyTorch\347\246\273\347\272\277\346\216\250\347\220\206\346\214\207\345\257\274/images/FAQ002_5.png" new file mode 100644 index 0000000000000000000000000000000000000000..99f0ecb98e12ce7e130e256f0d9f26084e523ea1 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/images/FAQ002_5.png" differ