Closed
[Bug-Report|缺陷反馈]: LogicalAnd输入未预留256B空间以及临时空间偏移计算错误 #3589
刘奕哲Add on  Oct 16Closed at  Oct 16
刘奕哲
刘奕哲
Oct 16 Create

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

LogicalAnd输入未预留256B空间,导致在tileop阶段调用TCMP时会出现UB越界。
tileop中临时空间偏移未按照validshape来计算。

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

A3和A5
cann-9.2.0

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

import os
import pytest
import torch
import pypto

@pypto.frontend.jit(runtime_options={"run_mode": pypto.RunMode.NPU})
def binary_assembled_scratch(
    x3: pypto.Tensor([pypto.DYNAMIC, pypto.STATIC, pypto.STATIC]),
    y3: pypto.Tensor([pypto.DYNAMIC, pypto.STATIC, pypto.STATIC]),
    out3: pypto.Tensor([pypto.DYNAMIC, pypto.STATIC, pypto.STATIC]),
    operation: str,
):
    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(16, 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, :, :]
        y_t = y3[bi * tile:(bi + 1) * tile, :, :]
        x2 = pypto.reshape(x_t, [tile, sl * tile], valid_shape=[bv, sl * tile])
        y2 = pypto.reshape(y_t, [tile, sl * tile], valid_shape=[bv, sl * tile])
        for si in pypto.loop(ns, name="s", idx_name="si", unroll_list=[3, 1]):
            sv = (sl - si * seq_tile).min(seq_tile)
            cs = si * seq_tile * tile
            xs = x2[0:tile, cs:cs + seq_tile * tile]
            ys = y2[0:tile, cs:cs + seq_tile * tile]
            flat_x = pypto.reshape(xs, [tile * seq_tile, tile], valid_shape=[bv * sv, tile])
            flat_y = pypto.reshape(ys, [tile * seq_tile, tile], valid_shape=[bv * sv, tile])
            value0 = pypto.tanh(flat_x) * 0.5
            value1 = pypto.tanh(flat_y) * 0.5
            if operation == "logicaland":
                result = pypto.logical_and(value0, value1)
            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 run_binary_assembled_scratch_test(operation: str):
    device_id = int(os.environ.get("TILE_FWK_DEVICE_ID", 0))
    torch.npu.set_device(device_id)
    generator = torch.Generator().manual_seed(42)
    x_cpu = torch.randn(32, 32, 16, generator=generator)
    y_cpu = torch.randn(32, 32, 16, generator=generator)
    x = x_cpu.to(f"npu:{device_id}")
    y = y_cpu.to(f"npu:{device_id}")
    if operation == "logicaland":
        expected = torch.logical_and(torch.tanh(x_cpu) * 0.5, torch.tanh(y_cpu) * 0.5)
        output = torch.zeros_like(x, dtype=torch.bool)
    binary_assembled_scratch(x, y, output, operation)
    actual = output.cpu()
    torch.testing.assert_close(actual, expected, rtol=5e-3, atol=5e-3)


@pytest.mark.parametrize("operation", ["logicaland"])
def test_binary_assembled_scratch_onboard(operation: str):
    run_binary_assembled_scratch_test(operation)

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

Pass

精度失败

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

likedislike
刘奕哲刘奕哲
Oct 16 Modify description of issue
刘奕哲
刘奕哲
Oct 16 Comments:

/assign

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