Closed
[Bug-Report|缺陷反馈]: aclnnReplicationPad2dBackward 3维输入仅第一维为0时被误拒返回 161002(资料声明 self 支持 3-4 维,161002 仅当非第一维为0) #4527
liuhanwen8Add on 13 days agoClosed at 1 day ago
13 days ago Added label: bug-report
13 days ago Modify description of issue
陈思
13 days ago Comments:
13 days ago Comments:
您好,感谢反馈!
问题分析
源码确认rank3的空态分支额外以mode=="edge"拒绝,rank4则按C/H/W是否为0判断;这可以解释报告中的返回差异。不过,3维[0,5,5]是C=0的无batch输入,4维[0,2,5,5]是N=0、C/H/W非0的输入,两者不是同一条batch为空语义。我们在本地PyTorch 2.12.0 CPU参考中也观察到前者被拒、后者允许,这只是参考行为,不能代替Ascend产品契约。文档写“非第一维为0时报错”却没有区分3维第一维C与4维第一维N,因此说明存在歧义;仅由支持3-4维不能推出所有首维为0的组合都应成功。现有错误文案也未说明edge模式的附加限制。
本轮核对 cann/ops-math 9.3.0-beta.1@5bbae1cc784296c821e1c1aa897b91c8033c6601。aclnn_pad2d_backward.cpp aclnnReplicationPad2dBackward.md
处理方向
可以先明确产品对空channel和空batch的支持口径,再分别对齐参数表、161002说明和错误日志。若保留现有策略,应明确3维空态的edge限制,并将日志原因与空间维为0区分;若评估扩展C=0支持,则需一起核对正向和反向的形状关系、合法输出及各调用链兼容,不能只删除mode条件便认定支持完成。我们继续确认这两类零维的接口约定。


13 days ago Assign yangjinwen as responsible person
13 days ago Added label: pending-confirmation
12 days ago Removed label: pending-confirmation
2 days ago Assign onanfield as responsible person
2 days ago Removed responsible person yangjinwen
1 day ago Close issue
1 day ago changed custom state from 进行中 to 已完成
1 day ago Added label: resolved
关联:#4525(同属 pad 反向算子的空 tensor 处理缺口,机理相反:那条是「该写未写」,本条是「不该拒却拒」)
现象
aclnnReplicationPad2dBackward对 3 维 self(资料声明支持 3-4 维)在「只有第一维为 0、其余维非 0」时无条件返回 161002,而同一次调用换成 4 维(batch=0)却能正常放行。适用范围界定:本条只针对空 tensor 场景。非空 rank3 + edge 是支持的(
aclnn_pad2d_backward.cpp:284-291有if (dimCp == 3) SqueezeNd的 3D 路径),|| mode == "edge"只写在空 tensor 分支(:239-248)内。根因:
conversion/pad_v3_grad_replicate/op_host/op_api/aclnn_pad2d_backward.cpp:242-248的 rank3 分支把|| mode == "edge"写进了拒绝条件,即 3 维 + ReplicationPad(mode=edge)的空 tensor 一律拒绝;紧随其后的 rank4 分支(:250-257)没有这个条件,仅在第一/二/三维为 0 时拒绝。期望
资料
conversion/pad_v3_grad_replicate/docs/aclnnReplicationPad2dBackward.md(9.3.0-beta.1)参数说明 self 的「维度(shape)」列为 3-4,即 3 维是声明支持的输入;错误码表 161002 的空 tensor 子项原文是「self为空tensor且存在非第一维度的值为0」——本用例非第一维度为 5、5,不在该条件内,应返回 SUCCESS(3 维空态下 gradInput 本身为空,无需写值,与 rank4 batch=0 的处理一致)。环境
libopapi.so构建 2026-10-289.3.0-beta.1@5bbae1cc784296c821e1c1aa897b91c8033c6601(2026-11-04);master 同一条件仍在(aclnn_pad2d_backward.cpp:244)复现步骤
OFF=/usr/local/Ascend/ascend-toolkit/latest; source $OFF/set_env.sh; export ASCEND_RT_VISIBLE_DEVICES=<空闲卡号>## 复现脚本的代码存为probe.cppg++ -std=c++17 -O2 -o probe probe.cpp -I$OFF/include -I$OFF/include/aclnnop -I$OFF/include/aclnnop/level2 -L$OFF/lib64 -lopapi -lnnopbase -lascendclLD_LIBRARY_PATH=$OFF/lib64:$LD_LIBRARY_PATH ./probe[缺陷组 rank3仅首维0]:GetWorkspaceSize = 161002(资料只把「非第一维度为 0」列为错误)复现脚本
// aclnnReplicationPad2dBackward:rank3 空输入(仅第一维为 0)被无条件 161002 —— 对照/缺陷双路 // 契约:docs/aclnnReplicationPad2dBackward.md 维度列 self="3-4";161002 子项「self为空tensor且存在非第一维度的值为0」 // => rank3 只有第一维为 0 时不应报错。实测:rank4 同场景放行并 Fill(0)+ViewCopy,rank3 因 mode=="edge" 被拒。 #include <cstdio> #include <cstdint> #include <cstring> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_pad2d_backward.h" static aclTensor* T(const std::vector<int64_t>& d, void* p) { std::vector<int64_t> s(d.size(), 1); for (int i = static_cast<int>(d.size()) - 2; i >= 0; --i) s[i] = s[i + 1] * d[i + 1]; return aclCreateTensor(d.data(), d.size(), ACL_FLOAT, s.data(), 0, ACL_FORMAT_ND, d.data(), d.size(), p); } static void* Dev(int64_t b, const void* h) { void* p = nullptr; aclrtMalloc(&p, b, ACL_MEM_MALLOC_HUGE_FIRST); if (h) aclrtMemcpy(p, b, h, b, ACL_MEMCPY_HOST_TO_DEVICE); return p; } static void Run(const std::vector<int64_t>& shape, aclrtStream s, const char* tag) { int64_t elems = 1; for (int64_t d : shape) elems *= d; const int64_t alloc = 64 * 5 * 5 > elems ? 64 * 5 * 5 : elems; std::vector<float> h(static_cast<size_t>(alloc), 1.0f); void* dg = Dev(alloc * 4, h.data()); void* ds = Dev(alloc * 4, h.data()); void* di = Dev(alloc * 4, h.data()); aclTensor* tg = T(shape, dg); aclTensor* ts = T(shape, ds); aclTensor* ti = T(shape, di); int64_t pad[4] = {0, 0, 0, 0}; aclIntArray* padding = aclCreateIntArray(pad, 4); uint64_t ws = 0; aclOpExecutor* ex = nullptr; int rc1 = int(aclnnReplicationPad2dBackwardGetWorkspaceSize(tg, ts, padding, ti, &ws, &ex)); int rc2 = -2; if (rc1 == 0) { void* w = ws ? Dev(int64_t(ws), nullptr) : nullptr; rc2 = int(aclnnReplicationPad2dBackward(w, ws, ex, s)); aclrtSynchronizeStream(s); if (w) aclrtFree(w); } printf("[%s] GetWorkspaceSize = %d execute = %d wsSize=%llu\n", tag, rc1, rc2, (unsigned long long)ws); aclDestroyTensor(tg); aclDestroyTensor(ts); aclDestroyTensor(ti); aclDestroyIntArray(padding); aclrtFree(dg); aclrtFree(ds); aclrtFree(di); } int main() { aclInit(nullptr); aclrtSetDevice(0); aclrtStream s = nullptr; aclrtCreateStream(&s); Run({2, 5, 5}, s, "对照组 rank3非空"); Run({0, 2, 5, 5}, s, "对照组 rank4batch0"); Run({0, 5, 5}, s, "缺陷组 rank3仅首维0"); aclrtDestroyStream(s); aclrtResetDevice(0); aclFinalize(); return 0; }影响
batch=0 的空批在训练末尾、数据裁剪、动态组 batch 等场景是常规形态。调用方按资料写法传 3 维输入会直接拿到 161002,而同族 4 维(batch=0)却成功,导致同一份上层代码在 3 维/4 维两条路径上表现分裂;调用方只能自行加特判绕过。
建议
一并修正两个次生问题:① 错误文案
Input dim 1 or dim 2 should not be empty在 dim1/dim2 均非 0 时与真实原因不符(实际是 edge 模式空态限制),建议改成可区分原因的文案;② 文档aclnnReplicationPad2dBackward.md:169-190的 161002 七子项中没有「3D + edge」这一项,实现拒绝的理由在文档里无对应条目,建议补写。git 佐证:
git log -S 'mode == "edge"' -- '*pad2d_backward*'只命中 7cf8c6081 init,无后续正当理由;9.3.0-beta.1 与 master 该段逐行一致。删掉 rank3 分支里的
|| mode == "edge",与 rank4 分支对齐(空态走GetGradInputWithValueZero+ViewCopy后返回 SUCCESS);若产品口径确实不支持「3 维 + edge 空输入」,则应在资料错误码表补写该子项,不能让实现与资料口径不一致。