Closed
[Bug-Report|缺陷反馈]: aclnnRandperm 在 INT8 n=128 / UINT8 n=256 / INT16 n=32768 时两段返回 0 但输出整块未写 #3911
liuhanwen8Add on  27 days agoClosed at  3 days ago
liuhanwen8
liuhanwen8
27 days ago Create

现象

aclnnRandperm 在 n 等于输出 dtype 的「上限」时两段返回码全为 0,但输出整块没有写,调用方缓冲保持原值。

实测(seed=1234、offset=0;出参缓冲 H2D 预填 0xFF 金丝雀,统计被改动的字节数):

组 dtype n ret_gws/exec/sync 写入字节 distinct 缺失值 最大值
对照 INT8 127 0/0/0 127 127 0 126
缺陷 INT8 128 0/0/0 0 1 128 -1
缺陷 UINT8 256 0/0/0 0 1 255 255
缺陷 INT16 32768 0/0/0 0 1 32768 -1

边界规律:n <= dtype 最大值 正常(INT8 127、UINT8 255、INT16 32767、FP16 2049、BF16 257、FP32 5000 均正确),n 一旦等于 dtype 最大值 + 1 就整块不写。

期望

资料《aclnnRandperm》功能说明(random/stateless_randperm/docs/aclnnRandperm.md:26):「返回从 0 到 n-1 的整数随机排列」;:226 明确写「INT8:n 不超过 128;UINT8:n 不超过 256;INT16:n 不超过 32768」。

即 INT8 + n=128、UINT8 + n=256、INT16 + n=32768 在声明支持面内,必须输出 0..n-1 的一个排列;若实现无法支持,应在第一段返回 161002,不能返回 0 后不写输出。

环境

  • 硬件 / CANN:Atlas A2 训练系列(Ascend 910B2,npu:6)+ CANN 9.2.0($ASCEND_HOME_PATH);g++ 12.3.0
  • 源码基线:cann/ops-math master 584b2ea6cbaeba684a51a74296d82d4d50ab3b0f(git fetch origin master 后核对)

复现步骤

  1. 把下方 repro.cpp 存成文件,编译:g++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lopapi -lnnopbase。
  2. 运行 ASCEND_RT_VISIBLE_DEVICES=6 ./repro,逐行看输出。
  3. 第 1 行对照组 INT8 n=127:写入字节=127 distinct=127(正常);第 2 行 INT8 n=128:写入字节=0 distinct=1,两段返回码仍全是 0。

复现脚本

// aclnnRandperm:INT8 n=128 / UINT8 n=256 / INT16 n=32768 两段返回 0 但输出整块未写
#include <acl/acl.h>
#include <aclnnop/aclnn_randperm.h>
#include <algorithm>
#include <cstdio>
#include <vector>

static aclTensor* mkT(void* d, std::vector<int64_t> sh, aclDataType dt) {
    std::vector<int64_t> st(sh.size());
    int64_t acc = 1;
    for (int i = (int)sh.size() - 1; i >= 0; --i) { st[i] = acc; acc *= sh[i]; }
    return aclCreateTensor(sh.data(), sh.size(), dt, st.data(), 0, ACL_FORMAT_ND,
                           sh.data(), sh.size(), d);
}

static void run(const char* tag, int64_t n, aclDataType dt, aclrtStream s) {
    const int64_t bytes = n * 8;
    void* dOut = nullptr;
    aclrtMalloc(&dOut, bytes, ACL_MEM_MALLOC_HUGE_FIRST);
    std::vector<char> canary(bytes, (char)0xFF);          // 金丝雀:未写即可见
    aclrtMemcpy(dOut, bytes, canary.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE);
    aclTensor* out = mkT(dOut, {n}, dt);
    uint64_t ws = 0;
    aclOpExecutor* ex = nullptr;
    int r1 = (int)aclnnRandpermGetWorkspaceSize(n, 1234, 0, out, &ws, &ex);
    int r2 = -1, r3 = -1;
    void* dWs = nullptr;
    if (r1 == 0) {
        if (ws) aclrtMalloc(&dWs, ws, ACL_MEM_MALLOC_HUGE_FIRST);
        r2 = (int)aclnnRandperm(dWs, ws, ex, s);
        r3 = (int)aclrtSynchronizeStream(s);
    }
    std::vector<unsigned char> raw(bytes, 0);
    aclrtMemcpy(raw.data(), bytes, dOut, bytes, ACL_MEMCPY_DEVICE_TO_HOST);
    int64_t changed = 0;
    for (int64_t i = 0; i < bytes; ++i) if (raw[i] != 0xFF) ++changed;
    std::vector<int64_t> host(n, -1);                     // 按 dtype 宽度解读前 n 个元素
    if (dt == ACL_INT8) { for (int64_t i = 0; i < n; ++i) host[i] = (int8_t)raw[i]; }
    else if (dt == ACL_UINT8) { for (int64_t i = 0; i < n; ++i) host[i] = (uint8_t)raw[i]; }
    else { aclrtMemcpy(host.data(), n * 8, dOut, n * 8, ACL_MEMCPY_DEVICE_TO_HOST); }
    std::vector<int64_t> srt(host);
    std::sort(srt.begin(), srt.end());
    int64_t distinct = 1, missing = 0;
    for (int64_t i = 1; i < n; ++i) if (srt[i] != srt[i - 1]) ++distinct;
    for (int64_t v = 0; v < n; ++v) if (!std::binary_search(srt.begin(), srt.end(), v)) ++missing;
    printf("%-24s n=%-6lld ret=%d/%d/%d 写入字节=%-6lld distinct=%-6lld 缺失值=%-6lld max=%lld\n",
           tag, (long long)n, r1, r2, r3, (long long)changed, (long long)distinct,
           (long long)missing, (long long)srt[n - 1]);
    if (r1 == 0) aclDestroyTensor(out);
    if (dWs) aclrtFree(dWs);
    aclrtFree(dOut);
}

int main() {
    aclInit(nullptr);
    aclrtSetDevice(0);
    aclrtStream s;
    aclrtCreateStream(&s);
    run("对照组 INT8 n=127", 127, ACL_INT8, s);
    run("缺陷组 INT8 n=128", 128, ACL_INT8, s);
    run("缺陷组 UINT8 n=256", 256, ACL_UINT8, s);
    aclrtDestroyStream(s);
    aclrtResetDevice(0);
    aclFinalize();
    return 0;
}

实测输出:

对照组 INT8 n=127     n=127    ret=0/0/0 写入字节=127    distinct=127    缺失值=0      max=126
缺陷组 INT8 n=128     n=128    ret=0/0/0 写入字节=0      distinct=1      缺失值=128    max=-1
缺陷组 UINT8 n=256    n=256    ret=0/0/0 写入字节=0      distinct=1      缺失值=255    max=255
(INT16 同规律:n=32768 写入字节 0;n=32767 对照写入 65407 字节、32767 个不同值)

影响

randperm 用于数据集打乱、负采样与随机置换初始化。缺陷组下调用方拿到的是自己缓冲里的旧数据,返回码却是 0:下游按索引取值不会报错,只会拿到错的样本,且无法通过返回码发现。INT8 n=128、UINT8 n=256、INT16 n=32768 正是资料给出的上限,属于声明支持面上的必然触发。

建议

n == dtype 最大值 + 1 时实现内部把 n(或 n-1)放进了与输出同宽的整数类型,溢出后写回分支被跳过。建议把 n 与中间量全部按 int64_t 计算、写回前再窄化;同时在第一段按 dtype 校验 n 上限,不支持返回 161002。回归用例覆盖每个 dtype 的 n = 上限-1 / 上限 / 上限+1 三点,并用金丝雀缓冲校验是否真的写了。

likedislike
liuhanwen8liuhanwen8
27 days ago Added label: bug-report
陈思
陈思Collaborators
26 days ago Comments:

您好,感谢反馈!您给出的 INT8、UINT8 用例在成功返回后仍未形成 0..n-1 的排列,需要沿实际执行路径继续核查。按报告的 cann/ops-math@584b2ea6cbaeba684a51a74296d82d4d50ab3b0f,这里有两个与定位直接相关的边界。

问题分析

首先,API 将 n 明确构造成 INT64 Tensor,当前参数检查只校验支持类型、输出 shape 为 [n] 和 n >= 0,没有按输出类型设置上限。因此,不能直接认定是在 Host 侧把 n 转成 INT8/UINT8 时溢出。A2 在该版本走的是 StatelessRandpermAiCpu,公开的 arch35 AI Core 实现也不能作为本条 A2 根因证据。参数构造、检查范围、后端分派

其次,您引用的 128/256/32768 上限位于资料的 Ascend 950 产品约束下,不能直接扩大为 A2 的明确上限说明。不过,从输出表示范围看,INT8,n=128 只需表示 0..127,并不要求长度 128 本身也能放入 INT8;UINT8、INT16 的对应边界同理。成功返回后的结果仍应满足排列语义。具体产品约束

处理方向

潜在方向是核对 CANN 9.2 的 AICPU 实现中边界判断、计数/索引类型及结果写回,保持长度计算与输出元素类型分离,避免按 n 而非 n-1 误判可表示范围。如果某平台确实存在额外限制,需要与第一段校验及资料说明一致,而不能成功返回却留下无效结果。

维护侧验证宜按实际 dtype 宽度读取,并以完整结果排序后等于 0..n-1 为准;金丝雀可以辅助判断,但“变化字节数”不等于实际写入字节数,例如 UINT8 的合法值 255 本身就与 0xFF 相同。相关回归可覆盖这三种类型的边界前后,INT16 应按 2 字节元素解析。我们会继续确认 A2 边界行为及支持范围。

likedislike
陈思陈思Collaborators
26 days ago Assign liu-wei as responsible person
陈思陈思Collaborators
26 days ago Added label: pending-confirmation
陈思陈思Collaborators
26 days ago Removed label: pending-confirmation
liu-weiliu-weiCollaborators
16 days ago Removed responsible person liu-wei
陈思陈思Collaborators
16 days ago Assign chen_0715 as responsible person
陈思陈思Collaborators
16 days ago Added label: pending-confirmation
chenyifengchenyifengCollaborators
15 days ago Assign zhuzemao as responsible person
chenyifengchenyifengCollaborators
15 days ago Removed responsible person chen_0715
CANN-robotCANN-robotCollaborators
3 days ago Close issue
CANN-robotCANN-robotCollaborators
3 days ago changed custom state from 进行中 to 已完成
CANN-robotCANN-robotCollaborators
3 days ago Added label: resolved