Closed
[Bug-Report|缺陷反馈]: aclnnHardtanhBackward 在 self 恰等于 min/max 时返回 0,与资料公式(要求 1)不符 #7153
liuhanwen8Add on Oct 21Closed at 24 days ago
Oct 21 Added label: bug-report
Oct 21 Assign ASCEND222 as responsible person
yuning_chen
Oct 22 Comments:
Oct 22 Comments:
yuning_chen
Oct 22 Comments:
Oct 22 Comments:
您好,该问题已收到,当前问题@ASCEND222处理中


yuning_chen
Oct 22 Comments:
Oct 22 Comments:
您好,该问题已收到,当前问题 @east_yang 处理中


Oct 22 Removed responsible person ASCEND222
Oct 22 Assign east_yang as responsible person
25 days ago Assign fengying555682 as responsible person
25 days ago Associated pull request
24 days ago Close issue
24 days ago Added label: resolved
Describe the current behavior / 问题描述 (Mandatory / 必填)
aclnnHardtanhBackward在self恰好等于min或max时返回 0,与接口资料给的公式不一致。资料
activation/hardtanh_grad/docs/aclnnHardtanhBackward.md的计算公式是:实测(Atlas A2 910B2 / CANN 9.2.0-beta.2,
min=-1, max=1,gradOutput=1):即:实现按
min < self < max(ATen 口径)判,资料按min ≤ self ≤ max判,只在两个边界点上不一致。两段接口都返回 0,不报错,值静默不同。
顺带对照:同一批接口里
aclnnHardswishBackward是反过来的 —— 它的资料写-3 ≤ x ≤ 3 → x/3+0.5,实测在 x=±3 确实返回 ±1.5/-0.5(符合资料、与 ATen 不同)。也就是说这两个接口对"边界点归哪一侧"
采用了互相矛盾的口径,用户按资料写用例时必然有一个对不上。
Environment Information / 环境信息 (Mandatory / 必填)
activation/hardtanh_gradactivation/hardtanh_grad/docs/aclnnHardtanhBackward.mdSteps to reproduce the issue / 重现步骤 (Mandatory / 必填)
保存为
repro.cpp,source $ASCEND_HOME_PATH/set_env.sh后编译运行(只依赖 acl,不依赖 torch):g++ -std=c++17 repro.cpp -I${ASCEND_HOME_PATH}/include \ -L${ASCEND_HOME_PATH}/lib64 -lascendcl -lnnopbase -lopapi -o repro && ./repro// aclnnHardtanhBackward:x 恰等于 min/max 时的梯度 #include <cmath> #include <cstdio> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_hardtanh_backward.h" static aclTensor* MakeTensor(const std::vector<int64_t>& shape, const std::vector<float>& host, void** dev) { int64_t bytes = static_cast<int64_t>(host.size()) * static_cast<int64_t>(sizeof(float)); if (aclrtMalloc(dev, bytes, ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS) { return nullptr; } if (aclrtMemcpy(*dev, bytes, host.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE) != ACL_SUCCESS) { return nullptr; } std::vector<int64_t> strides(shape.size(), 1); for (int64_t i = static_cast<int64_t>(shape.size()) - 2; i >= 0; --i) { strides[i] = shape[i + 1] * strides[i + 1]; } return aclCreateTensor(shape.data(), shape.size(), ACL_FLOAT, strides.data(), 0, ACL_FORMAT_ND, shape.data(), shape.size(), *dev); } int main() { const std::vector<float> xs{-1.5f, -1.001f, -1.0f, -0.999f, 0.0f, 0.999f, 1.0f, 1.001f, 1.5f}; const std::vector<int64_t> shape{static_cast<int64_t>(xs.size())}; std::vector<float> g(xs.size(), 1.0f), zero(xs.size(), 0.0f); aclInit(nullptr); aclrtSetDevice(0); aclrtStream stream = nullptr; aclrtCreateStream(&stream); void *dg = nullptr, *dx = nullptr, *dout = nullptr; aclTensor* tg = MakeTensor(shape, g, &dg); aclTensor* tx = MakeTensor(shape, xs, &dx); aclTensor* tout = MakeTensor(shape, zero, &dout); float minv = -1.0f, maxv = 1.0f; aclScalar* smin = aclCreateScalar(&minv, ACL_FLOAT); aclScalar* smax = aclCreateScalar(&maxv, ACL_FLOAT); uint64_t ws = 0; aclOpExecutor* exec = nullptr; auto rc1 = aclnnHardtanhBackwardGetWorkspaceSize(tg, tx, smin, smax, tout, &ws, &exec); printf("第一段 rc=%d\n", rc1); void* wsa = nullptr; if (ws > 0) { aclrtMalloc(&wsa, ws, ACL_MEM_MALLOC_HUGE_FIRST); } auto rc2 = aclnnHardtanhBackward(wsa, ws, exec, stream); aclrtSynchronizeStream(stream); printf("第二段 rc=%d\n\n", rc2); std::vector<float> out(xs.size(), 0.0f); aclrtMemcpy(out.data(), out.size() * sizeof(float), dout, out.size() * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); printf("%-9s %-10s %-10s %s\n", "self", "实测", "资料", "判定"); for (size_t i = 0; i < xs.size(); ++i) { double x = xs[i]; double bydoc = (x > 1.0 || x < -1.0) ? 0.0 : 1.0; printf("%-9.4f %-10.4f %-10.4f %s\n", x, static_cast<double>(out[i]), bydoc, std::fabs(out[i] - bydoc) < 1e-6 ? "一致" : "**不符**"); } aclDestroyTensor(tg); aclDestroyTensor(tx); aclDestroyTensor(tout); aclrtFree(dg); aclrtFree(dx); aclrtFree(dout); aclrtDestroyStream(stream); aclrtResetDevice(0); aclFinalize(); return 0; }实测输出:
Describe the expected behavior / 预期结果 (Mandatory / 必填)
二选一(请按产品口径定夺):
min <= self <= max),与资料一致;self >= max/self <= min→ 0,与 ATen 的hardtanh_backward一致,并在约束说明里写明「边界点按 PyTorch 口径取外侧」。
建议选 2(PyTorch 兼容、且不影响已有训练语义),同时把
aclnnHardswishBackward的边界口径(该接口实测与资料一致、与 ATen 不同)一并确认,避免同族接口口径互相矛盾。
Related log / screenshot / 日志 / 截图 (Mandatory / 必填)
Special notes for this issue/备注 (Optional / 选填)
Hardtanh相关条目,未见本问题。self恰好等于min/max时差 1 个梯度单位;连续性上属零测点,但按资料写用例的验收会判失败(本次真机精度用例正是这样判出来的:12 条里 2 条边界用例
8/32 个元素不符,其余 10 条全过)。
Hardswish/LeakyRelu/LogSigmoid/LogSoftmax/Mish/Prelu)的资料公式与实测一致。