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
Yu_yangyang
Yu_yangyang
7 days ago Create

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 / 必填)

  1. 环境:CANN 9.2.0、昇腾 A3/A5,source CANN 环境,npu-smi info 正常。
  2. 取缺陷版本:hotfix/transpose 提交 9cdc311(即修复提交 8b01b4f 的父提交)。
  3. 编译安装:python3 build_ci.py --clean --no_isolation,再执行 cann-pypto_9.2.0_linux-aarch64.run --full -q --pylocal。
  4. 最小用例:fp16/ND,输入与 view_shape 均为 (2048, 8, 1, 128),tile_shape (32, 8, 1, 128),first_dim=0、second_dim=2,期望输出 (1, 8, 2048, 128);内核片段为 pypto.set_vec_tile_shapes(32, 8, 1, 128); out = pypto.transpose(x, 0, 2)(对应 QKV 场景 k_out[:] = pypto.transpose(past_key, 0, 2))。
  5. 触发条件:交换维中 dim2 为 1 通过首道判断;otherDim=0,vecTile[0]=32 < 2048 且 dim1/dim3 被 tile 全覆盖,两个 tile 门控同时成立;中间 dim1=8>1 未被校验,于是走 Reshape(self, (1,8,2048,128)),又因 shape 等于 rawshape 使 ReshapeNeedCopy=false,退化为纯视图、不搬数据。
  6. 实际/预期:无报错,输出数值错乱,与 torch.transpose(x, 0, 2) 不一致;修复([dim0+1, dim1) 内存在 size>1 即拒绝)后应回落到真实转置路径并比对通过,同时相邻交换用例不受影响。

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、两维等长的恒等交换)行为与性能不变。

$ 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 / 选填)

likedislike
Yu_yangyangYu_yangyang
7 days ago Change title to "[Bug-Report|缺陷反馈]: Reject degenerate reshape for non-adjacent transpose with size>1 middle dims", originally titled "[Bug-Report|缺陷反馈]: ".
Yu_yangyangYu_yangyang
7 days ago Associated pull request
Yu_yangyang
Yu_yangyang
7 days ago Comments:

/assign

likedislike
CANN-robotCANN-robotCollaborators
7 days ago Assign YuYangYang_ as responsible person
CANN-robotCANN-robotCollaborators
7 days ago Close issue
CANN-robotCANN-robotCollaborators
7 days ago Added label: resolved