Closed
[Documentation|文档反馈]: aclnnReplicationPad1d/2d/3d 错误码表声明 padding>=末维应报 161002,实测放行(rc=0 且结果正确) #3886
liuhanwen8Add on  24 days agoClosed at  20 days ago
liuhanwen8
liuhanwen8
24 days ago Create

现象

aclnnReplicationPad1d 在 padding 数值 ≥ self 末维时仍返回成功(两段均 0、输出写满),而资料错误码表 161002 的第 5 条子项声明该入参应报 161002 —— 文档与实际行为不一致。

同一台 A2 逐项对照(FLOAT32,self=[1,5],值 1..5):

padding 输出 shape 实测返回码 输出内容 结论
2,2 [1,9] gws=0 exec=0 sync=0 1 1 1 2 3 4 5 5 5 合法对照
5,5(= 末维) [1,15] gws=0 exec=0 sync=0 1 1 1 1 1 1 2 3 4 5 5 5 5 … 资料称应 161002,实测放行
9,9(> 末维) [1,23] gws=0 exec=0 sync=0 1 1 1 1 1 1 1 1 1 1 2 3 4 … 资料称应 161002,实测放行

放行后的输出是正确的边缘复制结果(与 torch.nn.functional.pad(x, (9,9), mode='replicate') 一致),不是脏数据。同族 2d([2,3,5] + 9,9,1,1)与 3d([1,2,3,4,5] + 六向 9)实测同样 gws=0 exec=0 sync=0(同一份 SafePadSum / GetPaddingTensor 逻辑)。

期望

资料 docs/aclnnReplicationPad1d.md 两处自相矛盾:参数表 padding 使用说明写「长度为2,两个数值依次代表左右两边需要填充的值。」(没有上界);错误码表 161002 第 5 子项写「padding的数值大于等于self最后一维度的值。」(要求拦截)。实现(op_replication_pad.h 的 SafePadSum())只拦「负向裁剪超过维长」与「offset 溢出」,与参数表一致,也与 PyTorch 一致(torch 允许 replicate pad 大于输入维长)。期望以参数表与实现为准,修正错误码表该子项(删除,或改写为「padding 造成输出维非正 / 溢出时报 161002」)。

环境

cann/ops-math @ 584b2ea6(origin/master,2026-10-24 fetch);Ascend 910B2(A2);CANN 9.2.0(toolkit + 910b-ops);Linux 5.15 aarch64;实测日期 2026-10-24。

复现步骤

  1. 把下方「复现脚本」存为 repro.cpp
  2. 编译:g++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lopapi -lnnopbase
  3. 运行:ASCEND_LAUNCH_BLOCKING=1 ./repro
  4. 看 control-pad2 一行:返回码为 0(合法对照)
  5. 看 pad-eq-dim / pad-gt-dim 两行:返回码仍是 0(资料称应为 161002),且输出是正确复制结果

复现脚本

// aclnnReplicationPad1d:padding >= self 末维时未按资料报 161002(实测 rc=0 且结果正确)
#include <cstdio>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_replication_pad1d.h"

static aclTensor* MakeTensor(std::vector<float>& h, std::vector<int64_t>& sh, void** dev) {
    int64_t n = 1;
    for (auto d : sh) n *= d;
    size_t bytes = size_t(n) * sizeof(float);
    aclrtMalloc(dev, bytes, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(*dev, bytes, h.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE);
    std::vector<int64_t> st(sh.size(), 1);
    for (int64_t i = int64_t(sh.size()) - 2; i >= 0; --i) st[i] = sh[i + 1] * st[i + 1];
    return aclCreateTensor(sh.data(), sh.size(), ACL_FLOAT, st.data(), 0, ACL_FORMAT_ND,
                           sh.data(), sh.size(), *dev);
}

static void RunCase(const char* tag, std::vector<int64_t> in, std::vector<int64_t> out,
                    std::vector<int64_t> pads, aclrtStream s) {
    int64_t nIn = 1, nOut = 1;
    for (auto d : in) nIn *= d;
    for (auto d : out) nOut *= d;
    std::vector<float> sh;
    sh.assign(size_t(nIn), 0.0f);
    for (int64_t i = 0; i < nIn; ++i) sh[size_t(i)] = float(i % 5 + 1);
    std::vector<float> oh;
    oh.assign(size_t(nOut), -7.0f);
    void *sd = nullptr, *od = nullptr;
    aclTensor* self = MakeTensor(sh, in, &sd);
    aclTensor* o = MakeTensor(oh, out, &od);
    aclIntArray* pa = aclCreateIntArray(pads.data(), int64_t(pads.size()));
    uint64_t ws = 0;
    aclOpExecutor* ex = nullptr;
    auto r1 = aclnnReplicationPad1dGetWorkspaceSize(self, pa, o, &ws, &ex);
    void* wp = nullptr;
    if (ws > 0) aclrtMalloc(&wp, ws, ACL_MEM_MALLOC_HUGE_FIRST);
    auto r2 = aclnnReplicationPad1d(wp, ws, ex, s);
    auto r3 = (r1 == 0 && r2 == 0) ? aclrtSynchronizeStream(s) : -1;
    aclrtMemcpy(oh.data(), oh.size() * sizeof(float), od, oh.size() * sizeof(float),
                ACL_MEMCPY_DEVICE_TO_HOST);
    printf("[%-12s] padding=(%ld,%ld) gws=%d exec=%d sync=%d out=", tag, long(pads[0]),
           long(pads[1]), int(r1), int(r2), int(r3));
    for (size_t i = 0; i < oh.size() && i < 9; ++i) printf("%.0f ", oh[i]);
    printf("\n");
    aclDestroyTensor(self); aclDestroyTensor(o); aclDestroyIntArray(pa);
    if (wp) aclrtFree(wp);
    aclrtFree(sd); aclrtFree(od);
}

int main() {
    aclInit(nullptr);
    aclrtSetDevice(0);
    aclrtStream s = nullptr;
    aclrtCreateStream(&s);
    RunCase("control-pad2", {1, 5}, {1, 9}, {2, 2}, s);   // 合法对照:padding < 末维
    RunCase("pad-eq-dim", {1, 5}, {1, 15}, {5, 5}, s);    // 缺陷组:资料称应 161002
    RunCase("pad-gt-dim", {1, 5}, {1, 23}, {9, 9}, s);    // 缺陷组:资料称应 161002
    aclrtDestroyStream(s);
    aclrtResetDevice(0);
    aclFinalize();
    return 0;
}

实测输出(A2 + CANN 9.2.0,连续两次一致):

[control-pad2] padding=(2,2) gws=0 exec=0 sync=0 out=1 1 1 2 3 4 5 5 5
[pad-eq-dim  ] padding=(5,5) gws=0 exec=0 sync=0 out=1 1 1 1 1 1 2 3 4
[pad-gt-dim  ] padding=(9,9) gws=0 exec=0 sync=0 out=1 1 1 1 1 1 1 1 1

影响

按资料写用例的人会得到「期望 161002、实际 0」的失败,或反过来把这条当成实现漏校验提假缺陷。实现行为正确(与 torch 一致),只影响资料可信度,不影响功能。

建议

  • 修改 conversion/pad_v3/docs/aclnnReplicationPad{1,2,3}d.md 错误码表 161002 的第 5 子项(三份文档措辞相同)。
  • 若确实要求「padding ≥ 末维」报错,则需同时改 SafePadSum() 并在参数表写明上界,且说明与 PyTorch 的差异;建议各补一条 UT 锁定最终语义。
likedislike
liuhanwen8liuhanwen8
24 days ago Added label: documentation
陈思
陈思Collaborators
22 days ago Comments:

您好,感谢反馈!

问题分析

核对 cann/ops-math 固定版本 584b2ea6cbaeba684a51a74296d82d4d50ab3b0f,三份资料的错误码说明与当前参数检查存在差异。公共实现中的 SafePadSum 检查负向裁剪越界、尺寸求和溢出以及计算出的负尺寸,没有要求正向 padding 必须小于输入对应维长。1d、2d、3d 入口分别按最后一、二、三维计算期望输出 shape,并核对调用方提供的 out。

因此,您给出的 1d 输入末维为 5 时,padding=(5,5)、(9,9) 分别得到期望末维 15、23,不会仅因 padding 大于等于 5 而在这一步返回 161002。后续使用 edge 复制填充模式,这与您报告的成功返回和边缘复制结果相符;目前没有依据将本单判为数值错误。

资料侧需要同时核对多处:1d、2d、3d的错误码表都写了“padding 大于等于输入对应维长时报错”;其中 2d 的 padding 参数使用说明也另写了“小于对应维长”的限制,仅修改错误码表仍会留下不一致。

处理方向

一个可行的方向是按核实后的复制填充规则,统一三份错误码说明和 2d 参数表的上界表述,准确保留负向裁剪、尺寸溢出及输出 shape 不匹配等实际拒绝条件。零尺寸还有各入口的空张量分支处理,不能直接把新说明概括成“输出维非正时一律报错”,也不能把大正向 padding 的成功样例推广为任意尺寸均受支持。

若选择新增“padding 小于输入维长”的实现限制,则会改变您展示的现有成功调用,需要另行评估兼容影响。以上结论来自固定版本的源码与资料核对,尚未在 A2 设备独立运行;您已提供平台、版本和边界对照,无需重复补交。我们会继续确认资料与实现的对齐方式。

likedislike
陈思陈思Collaborators
22 days ago Assign chen_0715 as responsible person
陈思陈思Collaborators
22 days ago Added label: pending-confirmation
陈思陈思Collaborators
22 days ago Removed label: pending-confirmation
CchenyifengCollaborators
21 days ago Assign Anthony0331 as responsible person
CchenyifengCollaborators
21 days ago Removed responsible person chen_0715
WangJinChengWangJinChengCollaborators
20 days ago Associated pull request
CANN-robotCANN-robotCollaborators
20 days ago Close issue
CANN-robotCANN-robotCollaborators
20 days ago Added label: resolved