已合并
remove extra transpose in bmm by restore the stride(0) of input whose batch size is 1 #5288
XianglongZeng创建于 6月27日
remove extra transpose in bmm by restore the stride(0) of input whose batch size is 1 #5288
已合并
XianglongZeng创建于 6月27日
XianglongZeng成员
6月27日
# 【合入来源】 > **如有社区issue,请关联issue链接**\ > **请勿携带内部流程信息(需求链接、问题单、内部issue等)** - [ ] 需求 - [x] 问题单 - [ ] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 对于>=3D@>=3D、2D@>=3D 、>=3D@2D 且3D 输入前n-1维不连续(比如由转置得到)时,走进 bmm。进一步地,bmm内对输入固定会有对前n-2维的reshape操作,若reshape后 batch维为1 (2D 输入恒为 batch=1,≥3D 输入要求前 n-2 维全为 1)且reshape的tensor是由转置得到,则由于size(0)==1 时 stride(0) 任意值都"合法",torch 会取巧设为 stride(1)\*size(1) ,此时该tensor会变成对cann来说非标准的转置tensor,所以引入了额外的transpose。比如对与a[16, 12].T @ b [1,14384,16].T ,转置后的b会被reshape 成 [1,16,14384] 。虽然 shape 没变,但 reshape 仍会重新计算 stride——对 size(0)==1 的维度,把 stride(0) 规范化为 stride(1)\*size(1)=1*16=16 ,而不是保留原始的 229344,最后stride变成[16,1,16]。 对于这种情况,在bmm适配层对 size(0)==1 且矩阵维转置布局( stride(1)==1 && stride(2)==size(1) )且 stride(0) != size(1)\*size(2) 的张量,用 as_strided 把 stride(0) 规范化为 size(1)\*size(2) ,还原为cann所接受的标准转置tensor,从而消除transpose。 # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 单算子验证脚本: ``` import torch import numpy as np from torch.profiler import profile, ProfilerActivity torch.npu.set_device('npu:1') a_ori = torch.tensor(np.random.rand(16, 12).astype(np.float16)).npu() a = torch.permute(a_ori, (1, 0)) b_ori = torch.tensor(np.random.rand(1, 14384, 16).astype(np.float16)).npu() b = torch.permute(b_ori, (0, 2, 1)) c = torch.matmul(a, b) ``` 修改前: ![image.png](https://raw.gitcode.com/user-images/assets/7403085/63d403b7-3530-4b98-92db-f1f48dcea5a1/image.png 'image.png') 修改后: ![image.png](https://raw.gitcode.com/user-images/assets/7403085/2e9cb05f-30e7-40c3-b968-67f91cc73482/image.png 'image.png') 新增ut用例的执行结果: ![image.png](https://raw.gitcode.com/user-images/assets/7403085/9e46e9ca-1165-4d69-8f7d-f7185bf2fa2b/image.png 'image.png') ![image.png](https://raw.gitcode.com/user-images/assets/7403085/edacfbd8-9e03-4b45-a5e4-cdf1922f8d5a/image.png 'image.png') # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [ ] 代码注释完备,正确记录错误日志 - [ ] 代码实现进行了返回值、空指针等校验 - [ ] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 XianglongZeng 的贡献)
XXianglongZeng成员
6月27日 创建了 pull request,commit fb2790bb
ascend-robotascend-robot成员
6月27日 添加了label:ascend-cla/yes
ascend-robotascend-robot成员
6月27日 添加了label:needs-issue
ascend-robot
ascend-robot成员
6月27日 评论:

CLA Signature Pass

probiotics_53, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
此处折叠了53条消息 查看更多
ascend-robotascend-robot成员
6月30日 合入了pull request
ascend-robot
ascend-robot成员
6月30日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月30日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月30日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
6月30日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike