Closed
[Bug-Report|缺陷反馈]: Reject degenerate reshape for non-adjacent transpose with size>1 middle dims #3911
Yu_yangyangAdd on 7 days agoClosed at 7 days ago
7 days ago Change title to "[Bug-Report|缺陷反馈]: Reject degenerate reshape for non-adjacent transpose with size>1 middle dims", originally titled "[Bug-Report|缺陷反馈]: ".
7 days ago Change title to "[Bug-Report|缺陷反馈]: Reject degenerate reshape for non-adjacent transpose with size>1 middle dims", originally titled "[Bug-Report|缺陷反馈]: ".
7 days ago Associated pull request
Yu_yangyang
7 days ago Comments:
7 days ago Comments:
/assign


7 days ago Assign YuYangYang_ as responsible person
7 days ago Close issue
7 days ago Added label: resolved
Describe the current behavior / 问题描述 (Mandatory / 必填)
TryReshapeDegenerateTranspose 会把 pairwise 转置降级为一次 reshape 视图,但此前只校验了"被交换的两维中至少有一维 size 为 1"(或两维等长时视为恒等),遗漏了另一必要前提:在被交换的两维之间严格夹着的所有维度也必须为 size 1。由于调用前 perm 已排序(dim0 <= dim1),对连续布局而言,仅当 (dim0, dim1) 区间内的维度全部退化时,交换才等价于一次形状重排;否则会改变各 size>1 轴的相对次序,本质上仍是一次真实的数据搬移。
缺失该约束时,形如 (2, 3, 1, 16) 交换 (0, 2)、vec tile 为 (1, 3, 1, 16) 的用例会同时满足 tileCoversBatch 与 otherDimNotCovered 两个 tile 门控,从而进入纯视图 reshape 路径;此时视图 shape 与底层 rawshape 相同,ReshapeNeedCopy 返回 false,数据不做任何搬移,最终静默产生错误结果。
Environment / 环境信息 (Mandatory / 必填)
硬件型号:昇腾A2/ A3
软件环境:CANN 包 9.2.0-beta.2;pypto(pto-isa 包)master 分支;Python 3.10;torch_npu 与 CANN 9.2.0 配套
Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)
Describe the expected behavior / 预期结果 (Mandatory / 必填)
对 (2048, 8, 1, 128) 做 transpose(0, 2) 时不得退化为纯 reshape 视图:因两交换维之间的 dim1=8 非退化,必须走 SelectMergeKind / InnerTranspose 的真实转置路径,产生实际数据搬移。
输出形状为 (1, 8, 2048, 128),且每个元素与 torch.transpose(x, 0, 2) 完全一致,不出现静默错数。
TryReshapeDegenerateTranspose 仅在“两交换维至少一个 size 为 1、[dim0+1, dim1) 内所有维度均为 1、且 tile 门控成立”时返回 true;任一条件不满足即返回 false 并回落转置路径。
原本合法的退化场景(如相邻交换 2↔3、两维等长的恒等交换)行为与性能不变。
Related log / screenshot / 日志 / 截图 (Mandatory / 必填)
$ source ~/Ascend920/cann-9.2.0-beta.2/set_env.sh
$ export ASCEND_HOME_PATH=/tmp/opencode/cann920_pto # CANN 9.2.0 + pto-isa include 合并
$ python3 --version -> 3.10.22
$ python3 -c "import torch_npu, pypto; print(torch_npu.version, pypto.version, torch_npu.npu.get_device_name(0))"
2.9.0.post9 0.2.1 Ascend910B1
$ TILE_FWK_DEVICE_ID=0 python3 repro_cases.py
原始日志:
[CASE] A_issue_2048_nonsadjacent: shape=(2048, 8, 1, 128) dims=(0,2) tile=(1, 8, 1, 128) out=[1, 8, 2048, 128] run=11.72s
[RESULT] FAIL
Mismatched elements: 2065447 / 2097152 (98.5%)
Greatest absolute difference: 0.9992415308952332 at index (0, 1, 1253, 24) (up to 0.005 allowed)
Greatest relative difference: 2818169.25 at index (0, 0, 1933, 64) (up to 0.005 allowed)
[CASE] B_issue_2048_real_transpose_path: shape=(2048, 8, 1, 128) dims=(0,2) tile=(1, 8, 1, 16) out=[1, 8, 2048, 128] run=4.65s
[RESULT] PASS
[CASE] C_st31_nonsadjacent: shape=(2, 3, 1, 16) dims=(0,2) tile=(1, 3, 1, 16) out=[1, 3, 2, 16] run=4.30s
[RESULT] FAIL
Mismatched elements: 63 / 96 (65.6%)
Greatest absolute difference: 0.9080257415771484 at index (0, 2, 0, 11) (up to 0.005 allowed)
Greatest relative difference: 27.389244079589844 at index (0, 2, 0, 11) (up to 0.005 allowed)
[CASE] D_st32_adjacent_control: shape=(2, 3, 1, 16) dims=(2,3) tile=(2, 3, 1, 8) out=[2, 3, 16, 1] run=4.30s
[RESULT] PASS
[CASE] E_legal_degenerate_mid_all_one: shape=(2048, 1, 1, 128) dims=(0,2) tile=(1, 1, 1, 128) out=[1, 1, 2048, 128] run=9.98s
[RESULT] PASS
Special notes for this issue/备注 (Optional / 选填)