已关闭
[Bug-Report|缺陷反馈]: 开启精度工具后,发现expandFunction pass出现精度问题 #600
dice创建于  3月5日关闭于  3月26日
dice
dice
3月5日 创建

Thanks for sending an issue! Please fill in the following template to help quickly solve your problem.

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

算子代码如下:

import torch
import pypto


verify_options = {
    "enable_pass_verify": True,
    "pass_verify_save_tensor": True,
}


def embedding_backward_module(
    d_embeddings,
    indices,
    weight_shape,
):
    """
    inputs:
    - d_embeddings: shape=[tile, K*d]
    - indices: shape=[tile, K]
    - weight_shape: [Total_N, d]
    outputs:
    - d_input_ids: None
    - d_embedding_weight: shape=weight_shape
    - d_embeddings_offsets: None
    """

    tile, Kd = d_embeddings.shape
    K = indices.shape[-1]
    Total_N, d = weight_shape

    pypto.set_vec_tile_shapes(64, 16)
    indices_flat = indices.reshape([tile*K]) # [tile*K]
    pypto.set_vec_tile_shapes(64, 256)
    grad_embed_flat = d_embeddings.reshape([tile*K, d]) # [tile*K, d]
    grad_weight = pypto.full([Total_N, d], 0.0, dtype=d_embeddings.dtype)
    pypto.set_vec_tile_shapes(256, 64)
    output = pypto.index_add(grad_weight, 0, indices_flat, grad_embed_flat)
    return output


@pypto.jit(
    verify_options=verify_options
)
def embedding_backward_kernel(
    d_embeddings, # [B, L, D]
    indices, # [B, L, K]
    output, # [Total_N, d]
):
    B, L, D = d_embeddings.shape
    weight_shape = output.shape
    pypto.set_vec_tile_shapes(128, 128)
    output_tmp = pypto.full(weight_shape, 0.0, output.dtype)
    for b_idx in pypto.loop(B):
        for l_idx, tile in pypto.loop_unroll(L, unroll_list=[64]):
            pypto.set_vec_tile_shapes(1, 128, 128)
            d_embeddings_view = d_embeddings[b_idx, l_idx:l_idx+tile]
            indices_view = indices[b_idx, l_idx:l_idx+tile]
            d_weights = embedding_backward_module(d_embeddings_view, indices_view, weight_shape)
            pypto.set_vec_tile_shapes(128, 128)
            output_tmp[:] = output_tmp + d_weights

    pypto.assemble(output_tmp, [0, 0], output)


def embedding_backward_pto(
    d_embeddings, # [B, L, D]
    indices, # [B, L, K]
    weight_shape, # [Total_N, d]
):
    output = torch.empty(*weight_shape, dtype=d_embeddings.dtype, device=d_embeddings.device)

    inputs = {
        d_embeddings: [0, 1],
        indices: []
    }
    outputs = {
        output: []
    }
    inputs_pto = [pypto.from_torch(t, dynamic_axis=axis) for t, axis in inputs.items()]
    outputs_pto = [pypto.from_torch(t, dynamic_axis=axis) for t, axis in outputs.items()]
    embedding_backward_kernel(*inputs_pto, *outputs_pto)

    return output


def test_rms_norm_backward():
    device_id = 2
    torch.npu.set_device(device_id)
    device_str = f"npu:{device_id}"
    torch.manual_seed(42)
    B, L, D = 1, 64, 512
    Total_N = 256
    d =  64
    K = D//d
    d_embeddings = torch.randn(B, L, D, dtype=torch.float32, device=device_str)
    indices = torch.randint(0, Total_N-1, [B, L, K], dtype=torch.int32, device=device_str)
    weight_shape = [Total_N, d]
    inputs = [d_embeddings, indices]
    output_pto = embedding_backward_pto(*inputs, weight_shape)


if __name__ == '__main__':
    test_rms_norm_backward()

精度工具打印如下,后续的pass存在同样的精度问题,同时上板结果存在精度问题。

[INFO ] PYPTO(2041778):2026-03-05 16:47:13.817 [flow_verifier.cpp:87][VERIFY]:function_TENSOR__LoopUnroll64_Unroll1_PATH0_hiddenfunc0_11_pass_00_RemoveRedundantReshape Verify for 1 data view list index 0 result PASS
[INFO ] PYPTO(2041778):2026-03-05 16:47:13.980 [flow_verifier.cpp:87][VERIFY]:function_TENSOR__LoopUnroll64_Unroll1_PATH0_hiddenfunc0_11_pass_01_AutoCast Verify for 1 data view list index 0 result PASS
[INFO ] PYPTO(2041778):2026-03-05 16:47:14.150 [flow_verifier.cpp:87][VERIFY]:function_TENSOR__LoopUnroll64_Unroll1_PATH0_hiddenfunc0_11_pass_02_InferMemoryConflict Verify for 1 data view list index 0 result PASS
[INFO ] PYPTO(2041778):2026-03-05 16:47:14.322 [flow_verifier.cpp:87][VERIFY]:function_TENSOR__LoopUnroll64_Unroll1_PATH0_hiddenfunc0_11_pass_03_RemoveUndrivenView Verify for 1 data view list index 0 result PASS
[ERROR] PYPTO(2041778):2026-03-05 16:47:14.545 [flow_verifier.cpp:83][VERIFY]:function_TENSOR__LoopUnroll64_Unroll1_PATH0_hiddenfunc0_11_pass_04_ExpandFunction Verify for 1 data view list index 0 result FAILED
[INFO ] PYPTO(2041778):2026-03-05 16:47:14.548 [flow_verifier.h:145][VERIFY]:
    Error rtol=0.001000 atol=0.001000 index:0 golden:1.28153 output:1.469 absDiff:0.187466 relDiff:0.136313
    Error rtol=0.001000 atol=0.001000 index:1 golden:-1.47144 output:-0.33221 absDiff:1.13923 relDiff:1.26325
    Error rtol=0.001000 atol=0.001000 index:2 golden:-2.36907 output:-1.62484 absDiff:0.744235 relDiff:0.372685
    Error rtol=0.001000 atol=0.001000 index:3 golden:0.0689692 output:0.459454 absDiff:0.390485 relDiff:1.47792
    Error rtol=0.001000 atol=0.001000 index:4 golden:-3.02792 output:-3.72688 absDiff:0.698964 relDiff:0.206953
    Error rtol=0.001000 atol=0.001000 index:5 golden:-2.43955 output:-2.73647 absDiff:0.296921 relDiff:0.114729
    Error rtol=0.001000 atol=0.001000 index:6 golden:1.52472 output:1.37733 absDiff:0.147393 relDiff:0.101578
    Error rtol=0.001000 atol=0.001000 index:7 golden:0.774642 output:2.03582 absDiff:1.26118 relDiff:0.897487
    Error rtol=0.001000 atol=0.001000 ind

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

npu: 910b
cann: 8.5.0
pypto: 33c49f1c

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

略

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

pass精度校验通过,上板精度正确

略

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

likedislike
lidongshenglidongsheng成员
3月5日 将 DCGDDD 设为负责人
lidongshenglidongsheng成员
3月5日 issue类型由 任务 改变为 缺陷
lidongshenglidongsheng成员
3月5日 添加了label:Interpreter
dicedice
3月6日 修改了issue 的描述
gaoxingwang
gaoxingwang成员
3月6日 评论:

已安排处理

likedislike
杨旭杨旭成员
3月9日 关联了看板:PyPTO
杨旭杨旭成员
3月9日 将 Immutable01 设为负责人
杨旭
杨旭成员
3月9日 评论:

请 @Immutable01 协助处理一下~

likedislike
杨旭杨旭成员
3月9日 移除了负责人 DCGDDD
高前程
高前程成员
3月16日 评论:

精度工具定位过程:
1.比对精度工具输出与上板输出结果,两者一致,明确无误报情况。
2.比对精度工具pass4前与golden结果,两者一致,说明用户算子编写正确,pass4前,框架处理正确,明确问题源头在pass4 ExpandFunction
3.比对pass4与pass3精度工具Dump结果,发现在INDEX_ADD之后,数据首次出现差异,明确具体出错op
后续由pass定位

likedislike
高前程高前程成员
3月16日 将 KaiqiJin 设为负责人
高前程高前程成员
3月16日 移除了负责人 Immutable01
高前程高前程成员
3月16日 添加了label:Passes
Catherie
Catherie成员
3月24日 评论:

index_add算子按照当前的切分逻辑,当srcInput的axis轴维度大于selfInput的维度,会产生不合理的切分,进而导致精度问题。
修改后验证无问题:
https://gitcode.com/cann/pypto/pull/1898

likedislike
CatherieCatherie成员
3月25日 关联了pull request:fix(Pass): Sync the bug fix to the 0.2.0 branch
CatherieCatherie成员
3月25日 关联了里程碑:0.2.0
CatherieCatherie成员
3月25日 关联了pull request:fix(Pass): Sync the bug fix to the 0.2.0 branch
dice
dice
3月26日 评论:

本地验证通过

likedislike
CatherieCatherie成员
3月26日 issue状态由 待办的 改变为 已完成
CatherieCatherie成员
3月26日 关闭了 issue