Closed
[Bug-Report|缺陷反馈]: aclnnHardtanhBackward 在 self 恰等于 min/max 时返回 0,与资料公式(要求 1)不符 #7153
liuhanwen8Add on  Oct 21Closed at  24 days ago
liuhanwen8
liuhanwen8
Oct 21 Create

Describe the current behavior / 问题描述 (Mandatory / 必填)

aclnnHardtanhBackward 在 self 恰好等于 min 或 max 时返回 0,与接口资料给的公式不一致。

资料 activation/hardtanh_grad/docs/aclnnHardtanhBackward.md 的计算公式是:

grad_self = 0,  if self > max
            0,  if self < min
            1,  otherwise          ← 边界点应落在 otherwise 分支(返回 1)

实测(Atlas A2 910B2 / CANN 9.2.0-beta.2,min=-1, max=1,gradOutput=1):

self 实测 out 资料公式 PyTorch aten
-1.5 0 0 0
-1.001 0 0 0
-1.000 0 1 0
-0.999 1 1 1
0 1 1 1
0.999 1 1 1
1.000 0 1 0
1.001 0 0 0
1.5 0 0 0

即:实现按 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 / 必填)

  • 昇腾硬件型号:Atlas A2 训练系列产品(910B2)
  • CANN 版本:9.2.0-beta.2(Linux aarch64)
  • 算子仓:ops-nn activation/hardtanh_grad
  • 接口资料:activation/hardtanh_grad/docs/aclnnHardtanhBackward.md

Steps 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;
}

实测输出:

第一段 rc=0
第二段 rc=0

self      实测       资料       判定
-1.5000   0.0000     0.0000     一致
-1.0010   0.0000     0.0000     一致
-1.0000   0.0000     1.0000     **不符**
-0.9990   1.0000     1.0000     一致
0.0000    1.0000     1.0000     一致
0.9990    1.0000     1.0000     一致
1.0000    0.0000     1.0000     **不符**
1.0010    0.0000     0.0000     一致
1.5000    0.0000     0.0000     一致

Describe the expected behavior / 预期结果 (Mandatory / 必填)

二选一(请按产品口径定夺):

  1. 改实现:边界点返回 1(min <= self <= max),与资料一致;
  2. 改资料:把公式写成 self >= max / self <= min → 0,与 ATen 的 hardtanh_backward 一致,
    并在约束说明里写明「边界点按 PyTorch 口径取外侧」。

建议选 2(PyTorch 兼容、且不影响已有训练语义),同时把 aclnnHardswishBackward 的边界口径
(该接口实测与资料一致、与 ATen 不同)一并确认,避免同族接口口径互相矛盾。

第一段 rc=0,第二段 rc=0,无告警;out 在 x=±1 处为 0(资料要求 1)

Special notes for this issue/备注 (Optional / 选填)

  • 查重:已检索 ops-nn 最近 1000 条 issue(标题 + 正文)里 Hardtanh 相关条目,未见本问题。
  • 影响面:仅在 self 恰好等于 min/max 时差 1 个梯度单位;连续性上属零测点,
    但按资料写用例的验收会判失败(本次真机精度用例正是这样判出来的:12 条里 2 条边界用例
    8/32 个元素不符,其余 10 条全过)。
  • 同一批十个激活反向接口里,只有本接口与资料不符;其余九个(Glu/Hardshrink/Hardsigmoid/
    Hardswish/LeakyRelu/LogSigmoid/LogSoftmax/Mish/Prelu)的资料公式与实测一致。
likedislike
liuhanwen8liuhanwen8
Oct 21 Added label: bug-report
yuning_chenyuning_chenCollaborators
Oct 21 Assign ASCEND222 as responsible person
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

初步分析

感谢反馈。

现象理解:aclnnHardtanhBackward 在 self 恰好等于 min 或 max 时返回 0。资料的 otherwise 分支要求边界点返回 1。内部点和区间外与资料一致。

当前判断:初步判断,待复现确认。

涉及范围:activation/hardtanh_grad 的边界判断。

下一步:用 min 为 -1、max 为 1、self 恰好为边界的输入再对输出。确认后按资料把等号放进 otherwise。

likedislike
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

您好,该问题已收到,当前问题@ASCEND222处理中

likedislike
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

您好,该问题已收到,当前问题 @east_yang 处理中

likedislike
yuning_chenyuning_chenCollaborators
Oct 22 Removed responsible person ASCEND222
yuning_chenyuning_chenCollaborators
Oct 22 Assign east_yang as responsible person
fengying555682fengying555682Collaborators
25 days ago Assign fengying555682 as responsible person
fengying555682fengying555682Collaborators
25 days ago Associated pull request
CANN-robotCANN-robotCollaborators
24 days ago Close issue
CANN-robotCANN-robotCollaborators
24 days ago Added label: resolved