Closed
[Bug-Report|缺陷反馈]: 部分算子计算临时空间中小块的偏移有误 #3574
刘奕哲Add on  Oct 14Closed at  Oct 14
刘奕哲
刘奕哲
Oct 14 Create

Describe the current behavior / 问题描述 (Mandatory / 必填)

算子的输入输出tensor的rawShape在pass阶段从tile切分后到最后一个pass之间会改变,而临时空间大小由tileshape得到,临时空间中的小块偏移不能直接由输入或输出的rawShape计算而是应由validshape计算

Environment / 环境信息 (Mandatory / 必填)

A3和A5
cann-9.2.0

Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)

import argparse
import os

import torch
import torch_npu
import pypto


@pypto.frontend.jit(runtime_options={"run_mode": pypto.RunMode.NPU})
def unary_assembled_scratch(
    x3: pypto.Tensor([pypto.DYNAMIC, pypto.STATIC, pypto.STATIC]),
    out3: pypto.Tensor([pypto.DYNAMIC, pypto.STATIC, pypto.STATIC]),
    operation: str,
    vec_rows: int,
    unroll: int,
):
    tile, seq_tile = 16, 8
    bs, sl = x3.shape[0], x3.shape[1]
    nb = (bs + tile - 1) // tile
    ns = (sl + seq_tile - 1) // seq_tile
    pypto.set_vec_tile_shapes(vec_rows, tile)
    for bi in pypto.loop(nb, name="b", idx_name="bi", unroll_list=[2, 1]):
        bv = (bs - bi * tile).min(tile)
        x_t = x3[bi * tile:(bi + 1) * tile, :, :]
        x2 = pypto.reshape(x_t, [tile, sl * tile], valid_shape=[bv, sl * tile])
        for si in pypto.loop(ns, name="s", idx_name="si", unroll_list=[unroll, 1]):
            sv = (sl - si * seq_tile).min(seq_tile)
            cs = si * seq_tile * tile
            xs = x2[0:tile, cs:cs + seq_tile * tile]
            flat = pypto.reshape(xs, [tile * seq_tile, tile], valid_shape=[bv * sv, tile])
            value = pypto.tanh(flat) * 0.5
            if operation == "asin":
                result = pypto.asin(value)
            elif operation == "acos":
                result = pypto.acos(value)
            elif operation == "asinh":
                result = pypto.asinh(value)
            elif operation == "atanh":
                result = pypto.atanh(value)
            elif operation == "sinh":
                result = pypto.sinh(value)
            back = pypto.reshape(result, [tile, seq_tile, tile], valid_shape=[bv, sv, tile])
            out3[bi * tile:(bi + 1) * tile, si * seq_tile:(si + 1) * seq_tile, :] = back


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--op", choices=["asin", "acos", "asinh", "atanh", "sinh"], default="asin")
    parser.add_argument("--device", type=int, default=int(os.environ.get("TILE_FWK_DEVICE_ID", 0)))
    parser.add_argument("--vec-rows", type=int, choices=[16, 128], default=16)
    parser.add_argument("--unroll", type=int, choices=[1, 3], default=3)
    args = parser.parse_args()
    torch_npu.npu.set_device(args.device)
    generator = torch.Generator().manual_seed(42)
    x_cpu = torch.randn(32, 32, 16, generator=generator)
    expected = getattr(torch, args.op)(torch.tanh(x_cpu) * 0.5)
    x = x_cpu.to(f"npu:{args.device}")
    output = torch.zeros_like(x)
    unary_assembled_scratch(x, output, args.op, args.vec_rows, args.unroll)
    torch_npu.npu.synchronize()
    actual = output.cpu()
    relative = (actual - expected).abs() / (expected.abs() + 1e-7)
    finite = torch.isfinite(relative)
    max_relative = relative[finite].max().item() if finite.any() else float("nan")
    print(f"op={args.op} vec_rows={args.vec_rows} unroll={args.unroll} "
          f"max_finite_rel={max_relative:.6e} bad(>5%)={(relative > 0.05).sum().item()} "
          f"nonfinite={(~torch.isfinite(actual)).sum().item()}", flush=True)
    torch.testing.assert_close(actual, expected, rtol=1e-4, atol=1e-5)
    print("PASS", flush=True)


if __name__ == "__main__":
    main()

Describe the expected behavior / 预期结果 (Mandatory / 必填)

Pass

实际精度不通过

Special notes for this issue/备注 (Optional / 选填)

likedislike
刘奕哲
刘奕哲
Oct 14 Comments:

/assign

likedislike
CANN-robotCANN-robotCollaborators
Oct 14 Assign Creeeeper as responsible person
刘奕哲刘奕哲
Oct 14 Associated pull request
刘奕哲刘奕哲
Oct 14 Modify description of issue
CANN-robotCANN-robotCollaborators
Oct 14 Close issue
CANN-robotCANN-robotCollaborators
Oct 14 Added label: resolved