Closed
[Bug-Report|缺陷反馈]: aclnnCompressor 属性取值非法返回 561002(资料为 161002),且 stateBlockTable 元素越界未拦截导致 AI Core 越界 507015 #6610
liuhanwen8Add on  23 days agoClosed at  12 days ago
liuhanwen8
liuhanwen8
23 days ago Create

问题描述

aclnnCompressor 在 A2(910B2) 上有两处取值域问题,都能稳定复现:

# 用例 资料声明 实测
1 cmpRatio=0 / 1 / 129、coff=0 / 3、cacheMode=0 / 3 错误码表:161002「cmpRatio、coff、cacheMode 的取值不在支持的范围之内」 561002(ACLNN_ERR_INNER_TILING_ERROR),6 例全部一致
2 stateBlockTableOptional 元素 = 99(block_num=1,合法域 [0,1)) 约束说明:「元素取值范围为 [0, block_num)……越界会导致内存非法访问」 两段均返回 0,aclrtSynchronizeStream 返回 507015(AI Core 越界)

第 2 条的合法对照组(元素 = 0)返回 0/0/0,越界组三轮连跑结果完全一致,不是偶发。

环境信息

  • 硬件:Atlas A2 训练系列(910B2,单卡)
  • CANN:9.2.0,驱动 26.1.1
  • 分支:master 15ef51a6
  • 复现时间:2026-10-23

重现步骤

基线取仓内官方样例 attention/compressor/examples/test_aclnn_compressor.cpp 的 C4A 组合
(B=1, S=4, H=4096, D=512, coff=2, cmpRatio=4, cacheMode=1, blockSize=128,
block_num=1, stateCacheStrideDim0=262144),只改被测量:

# 完整脚本见下方;核心是 run(cmpRatio=1) / run(sbt_vals=[99])
BASELINE (C4A)                              gws=0  exec=0  sync=0
cmpRatio=1  (<2)                            gws=561002
cmpRatio=129 (>128)                         gws=561002
coff=0 / coff=3                             gws=561002
cacheMode=0 / cacheMode=3                   gws=561002
stateBlockTable=[99], block_num=1            gws=0  exec=0  sync=507015
stateBlockTable=[0](合法对照)               gws=0  exec=0  sync=0
复现脚本(纯 ctypes,无需编译,`python3 repro.py [device_id]`)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""aclnnCompressor 取值域最小复现(纯 ctypes,无需编译)。

基线取仓内官方样例 attention/compressor/examples/test_aclnn_compressor.cpp 的 C4A 组合。
用法:python3 repro.py [device_id]
"""
import ctypes as C
import sys

LIB = "/usr/local/Ascend/ascend-toolkit/latest/lib64/"
acl = C.CDLL(LIB + "libascendcl.so", mode=C.RTLD_GLOBAL)
base = C.CDLL(LIB + "libnnopbase.so", mode=C.RTLD_GLOBAL)   # aclCreateTensor 自 CANN 8.5 迁到此库
nn = C.CDLL(LIB + "libopapi.so", mode=C.RTLD_GLOBAL)

ACL_FLOAT, ACL_BF16, ACL_INT32 = 0, 27, 3
ND, H2D = 2, 1

acl.aclInit.argtypes = [C.c_char_p]
acl.aclrtSetDevice.argtypes = [C.c_int32]
acl.aclrtCreateStream.argtypes = [C.POINTER(C.c_void_p)]
acl.aclrtMalloc.argtypes = [C.POINTER(C.c_void_p), C.c_size_t, C.c_int32]
acl.aclrtMemcpy.argtypes = [C.c_void_p, C.c_size_t, C.c_void_p, C.c_size_t, C.c_int32]
acl.aclrtSynchronizeStream.argtypes = [C.c_void_p]
base.aclCreateTensor.argtypes = [C.POINTER(C.c_int64), C.c_uint64, C.c_int,
                                 C.POINTER(C.c_int64), C.c_int64, C.c_int,
                                 C.POINTER(C.c_int64), C.c_uint64, C.c_void_p]
base.aclCreateTensor.restype = C.c_void_p
nn.aclnnCompressorGetWorkspaceSize.restype = C.c_int32
nn.aclnnCompressorGetWorkspaceSize.argtypes = (
    [C.c_void_p] * 9 + [C.c_int64] * 4 + [C.c_bool] + [C.c_void_p] * 3
    + [C.POINTER(C.c_uint64), C.POINTER(C.c_void_p)])
nn.aclnnCompressor.restype = C.c_int32
nn.aclnnCompressor.argtypes = [C.c_void_p, C.c_uint64, C.c_void_p, C.c_void_p]


def alloc(n, init=None):
    p = C.c_void_p()
    assert acl.aclrtMalloc(C.byref(p), max(n, 1), 0) == 0
    if init:
        buf = C.create_string_buffer(bytes(init), len(init))
        assert acl.aclrtMemcpy(p, len(init), C.cast(buf, C.c_void_p), len(init), H2D) == 0
    return p


def mk(shape, dt, ptr):
    d = (C.c_int64 * len(shape))(*shape)
    st = [1] * len(shape)
    for i in range(len(shape) - 2, -1, -1):
        st[i] = shape[i + 1] * st[i + 1]
    s = (C.c_int64 * len(shape))(*st)
    return base.aclCreateTensor(d, len(shape), dt, s, 0, ND, d, len(shape), ptr)


_STATE = {"inited": False, "stream": None}


def ensure_init(dev):
    """aclInit / aclrtSetDevice 只能做一次;stream 全局复用(每条用例新建会泄漏)。"""
    if not _STATE["inited"]:
        assert acl.aclInit(None) == 0 and acl.aclrtSetDevice(dev) == 0
        st = C.c_void_p()
        assert acl.aclrtCreateStream(C.byref(st)) == 0
        _STATE["stream"] = st
        _STATE["inited"] = True
    return _STATE["stream"]


def run(tag, dev, cmp_ratio=4, coff=2, cache_mode=1, sbt_vals=(0,)):
    B, S, H, D, BS = 1, 4, 4096, 512, 128
    st = ensure_init(dev)
    coffD = coff * D
    bn = 1
    sr = max(1, (S + cmp_ratio - 1) // max(cmp_ratio, 1))
    x = mk([B, S, H], ACL_BF16, alloc(B * S * H * 2))
    wkv = mk([coffD, H], ACL_BF16, alloc(coffD * H * 2))
    wgate = mk([coffD, H], ACL_BF16, alloc(coffD * H * 2))
    sc = mk([bn, BS, 2 * coffD], ACL_FLOAT, alloc(bn * BS * 2 * coffD * 4))
    ape = mk([cmp_ratio, coffD], ACL_FLOAT, alloc(cmp_ratio * coffD * 4))
    sbt = mk([B, 1], ACL_INT32,
             alloc(len(sbt_vals) * 4, bytes((C.c_int32 * len(sbt_vals))(*sbt_vals))))
    sp = mk([B], ACL_INT32, alloc(4, bytes((C.c_int32 * 1)(0))))
    ck = mk([B, sr, D], ACL_BF16, alloc(B * sr * D * 2))
    ss = mk([B, sr, coff * cmp_ratio, D], ACL_FLOAT, alloc(B * sr * coff * cmp_ratio * D * 4))
    kv = mk([B, sr, coff * cmp_ratio, D], ACL_FLOAT, alloc(B * sr * coff * cmp_ratio * D * 4))
    ws, ex = C.c_uint64(0), C.c_void_p()
    rc1 = nn.aclnnCompressorGetWorkspaceSize(
        x, wkv, wgate, sc, ape, sbt, None, None, sp,
        cmp_ratio, coff, cache_mode, BS * 2 * coffD, False,
        ck, ss, kv, C.byref(ws), C.byref(ex))
    rc2 = rc3 = None
    if rc1 == 0:
        rc2 = nn.aclnnCompressor(alloc(ws.value) if ws.value else C.c_void_p(),
                                 ws.value, ex, st)
        rc3 = acl.aclrtSynchronizeStream(st)
    print("%-40s gws=%-7s exec=%-7s sync=%s" % (tag, rc1, rc2, rc3))


if __name__ == "__main__":
    dev = int(sys.argv[1]) if len(sys.argv) > 1 else 0
    run("BASELINE (C4A)", dev)
    run("cmpRatio=1", dev, cmp_ratio=1)
    run("cmpRatio=129", dev, cmp_ratio=129)
    run("coff=0", dev, coff=0)
    run("coff=3", dev, coff=3)
    run("cacheMode=0", dev, cache_mode=0)
    run("cacheMode=3", dev, cache_mode=3)
    run("stateBlockTable=[99] (block_num=1)", dev, sbt_vals=(99,))
    run("stateBlockTable=[0] (合法对照)", dev, sbt_vals=(0,))

期望结果

  1. cmpRatio / coff / cacheMode 非法取值在第一段返回 ACLNN_ERR_PARAM_INVALID(161002),与资料错误码表一致(当前返回 561002,调用方按 161002 分支处理时会漏判成"tiling 内部异常")。
  2. stateBlockTableOptional 的元素越界应在执行前被拒绝,而不是在 Kernel 里以 AI Core 越界(507015)的形式暴露 —— 507015 会污染进程后续调用,且调用方无法区分是"自己的索引错了"还是"算子写坏了"。

定位参考

  • attention/compressor/op_host/arch92/compressor_tiling_arch92.cpp:stateBlockTable 只校验了 dtype / dimNum / shape size(CheckDtypeSupport / CheckDimNumSupport),没有值域校验;元素在 device 上,host 侧当前不读取。
  • 属性取值(cmpRatio/coff/cacheMode)在 tiling 阶段被拒,因此返回的是 561002 而非 aclnn 层的 161002。

关联

同一天在 aclnnGroupedMatmulAdd 上也实测到「device 侧索引值域未校验 → AI Core 越界 507015」(#6605),本仓 aclnnGroupedMatmulFinalizeRouting 族有同类 open issue(#6542 / #6543 / #6544)。若能在公共层统一核对「索引类 device 张量」的值域,这一族可以一并收敛。

likedislike
周云飞
周云飞Collaborators
23 days ago Comments:

感谢提供完整脚本、合法对照组和分阶段返回值。这两项需要分别判断:一项是非法属性的返回码与资料不一致,另一项是设备侧索引越界的防护方式。

问题分析

  1. 属性返回码:按您在 A2(910B2)、CANN 9.2.0 上的测试结果,非法属性在 aclnnCompressorGetWorkspaceSize 阶段返回了 561002,而不是资料声明的 161002。这会影响调用方对参数错误的分类处理。您提出的“属性在 tiling 阶段被拒绝”是一个合理的定位方向,但具体的检查位置及错误码转换路径仍需确认。

  2. 索引越界:您提供的结果显示,block_num=1 时,元素为 99 的用例在两段接口均返回成功后,于流同步时报 507015;元素为 0 的对照组正常。两段接口返回成功不等于设备计算已完成,因此不能据此判断索引有效。不过,您引用的资料已明确提示越界可能导致非法访问,尚不能仅凭这一现象认定接口违反了“执行前拒绝”的约定。这里需要区分已有的索引合法性要求与新增的运行时防护诉求。

处理方向

对于属性返回码,一个可行方向是在 aclnn 入口检查 cmpRatio、coff、cacheMode 的合法范围,使非法属性按接口约定返回 161002,同时保留 tiling 阶段的必要检查。

对于 stateBlockTableOptional,仅检查类型和形状不能证明元素落在 [0, block_num) 内。若要增加防护,需要评估设备侧检查,或读取设备数据后再检查的方案;后者涉及同步和数据传输开销,前者也需要明确如何阻止非法访存并向调用方报告错误。您提出的公共层统一校验值得评估,但不能直接假定所列算子具有相同根因或适用同一方案。当前可先在调用侧生成索引时保证其合法性,避免将已知越界值提交执行。

另外,所引用的官方样例标注为 A3,而本次环境为 A2;这本身不能证明 A2 不支持,但需要核对对应版本的支持范围。烦请补充本次参考的接口资料链接或版本,以便确认 A2 适用范围及 161002 的具体约定。

likedislike
游震Collaborators
23 days ago Comments:

/assign @shasha_an

likedislike
CANN-robotCANN-robotCollaborators
23 days ago Assign shasha_an as responsible person
周云飞周云飞Collaborators
17 days ago Assign qiansunchi159 as responsible person
周云飞周云飞Collaborators
17 days ago Removed responsible person shasha_an
Qqiansunchi159Collaborators
17 days ago Associated pull request
qiansunchi159Collaborators
12 days ago Comments:

关联PR已合入,issue关闭

likedislike
Qqiansunchi159Collaborators
12 days ago changed custom state from 进行中 to 已完成
Qqiansunchi159Collaborators
12 days ago Close issue
CANN-robotCANN-robotCollaborators
12 days ago Added label: resolved
周云飞
周云飞Collaborators
6 days ago Comments:

处置结果

已修正 aclnnCompressor 文档,将非法 cmpRatio、coff、cacheMode 的返回码与现有 tiling 行为对齐为 561002。本 PR 的变更范围为文档,stateBlockTable 元素越界防护未包含在此次处置中。

变更已合入 master,详见 PR #14976。

likedislike