Skip to content
版 本

LeakyRelu

产 品 支 持 情 况

产 品是 否 支 持
Ascend 950PR/Ascend 950DT
Atlas A3 训 练 系 列 产 品/Atlas A3 推 理 系 列 产 品
Atlas A2 训 练 系 列 产 品/Atlas A2 推 理 系 列 产 品
Atlas 200I/500 A2 推 理 产 品
Atlas 推 理 系 列 产 品AI Core
Atlas 推 理 系 列 产 品Vector Core
x
Atlas 训 练 系 列 产 品
x
Kirin X90
Kirin 9030

功 能 说 明

头 文 件 路 径 为:"basic_api/kernel_operator_vec_binary_scalar_intf.h"

LeakyRelu属 于 双 目 标 量 类 计 算 接 口,输 入 为 一 个 矢 量tensor和 一 个 标 量scalar,LeakyRelu负 责 将 矢 量tensor内 的 每 个 元 素 执 行Leaky ReLu(Leaky Rectified Linear Unit)操 作。计 算 公 式 如 下:

$dst_i = \begin{cases} src_i, & src_i \geq 0 \ scalarValue \times src_i, & src_i < 0 \end{cases}$

Leaky ReLU带 泄 露 线 性 整 流 函 数 是 一 种 人 工 神 经 网 络 中 常 用 的 激 活 函 数,其 数 学 表 达 式 为:

$f(x) = \begin{cases} x, & x \geq 0 \ \alpha x, & x < 0 \end{cases}$

其 中 $\alpha$ 为 负 斜 率(对 应 参 数scalarValue)。

和ReLU的 区 别 是:ReLU是 将 所 有 的 负 值 都 设 为 零,而Leaky ReLU是 给 所 有 负 值 赋 予 一 个 斜 率。下 图 表 示 了ReLU和Leaky ReLU的 区 别:

函 数 原 型

  • tensor前n个 数 据 计 算

    C++
    template <typename T, bool isSetMask = true>
    __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, const int32_t& count)
    
  • tensor高 维 切 分 计 算

    • mask逐bit模 式

      C++
      template <typename T, bool isSetMask = true>
      __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
      
    • mask连 续 模 式

      C++
      template <typename T, bool isSetMask = true>
      __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
      

dst和src使 用TensorTrait类 型 时,其 数 据 类 型TensorTrait和scalarValue的 数 据 类 型(对 应TensorTrait中 的LiteType类 型)不 一 致。因 此 新 增 模 板 类 型U表 示scalarValue的 数 据 类 型,并 通 过std::enable_if检 查T中 萃 取 出 的LiteType和U是 否 完 全 一 致,一 致 则 接 口 通 过 编 译,否 则 编 译 失 败。接 口 原 型 定 义 如 下:

  • tensor前n个 数 据 计 算

    C++
    template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true>
    __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, const int32_t& count)
    
  • tensor高 维 切 分 计 算

    • mask逐bit模 式

      C++
      template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true>
      __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
      
    • mask连 续 模 式

      C++
      template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true>
      __aicore__ inline void LeakyRelu(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
      

参 数 说 明

表 1 模 板 参 数 说 明

参 数 名描 述
T操 作 数 数 据 类 型。
UscalarValue的 数 据 类 型。仅 在dst和src使 用TensorTrait类 型 时 有 效。
isSetMask是 否 在 接 口 内 部 设 置mask。
• true,表 示 在 接 口 内 部 设 置mask。
• false,表 示 在 接 口 外 部 设 置mask,开 发 者 需 要 使 用SetVectorMask接 口 设 置mask值。这 种 模 式 下,本 接 口 入 参 中 的mask值 必 须 设 置 为 占 位 符MASK_PLACEHOLDER
具 体 使 用 方 式 可 参 考掩 码
针 对 以 下 型 号,tensor前n个 数 据 计 算API中 的isSetMask参 数 不 生 效,保 持 默 认 值 即 可。
Ascend 950PR/Ascend 950DT

Atlas 200I/500 A2 推 理 产 品

表 2 参 数 说 明

参 数 名输 入/输 出描 述
dst输 出目 的 操 作 数。
类 型 为LocalTensor,支 持 的TPosition为VECIN/VECCALC/VECOUT。
地 址 对 齐 约 束 参 考通 用 地 址 对 齐 约 束
src输 入源 操 作 数。
类 型 为LocalTensor,支 持 的TPosition为VECIN/VECCALC/VECOUT。
地 址 对 齐 约 束 参 考通 用 地 址 对 齐 约 束
scalarValue输 入源 操 作 数,数 据 类 型 需 要 与 目 的 操 作 数 中 的 元 素 类 型 保 持 一 致。
count输 入参 与 计 算 的 元 素 个 数。关 于 该 参 数 的 具 体 说 明 请 参 考连 续 计 算
mask[]/mask输 入mask用 于 控 制 每 次 迭 代 内 参 与 计 算 的 元 素。详 细 设 置 参 考掩 码
repeatTime输 入重 复 迭 代 次 数。矢 量 计 算 单 元,每 次 读 取 连 续 的256Bytes数 据 进 行 计 算,为 完 成 对 输 入 数 据 的 处 理,必 须 通 过 多 次 迭 代(repeat)才 能 完 成 所 有 数 据 的 读 取 与 计 算。repeatTime表 示 迭 代 的 次 数。
关 于 该 参 数 的 具 体 说 明 请 参 考高 维 切 分
repeatParams输 入控 制 操 作 数 地 址 步 长 的 参 数。UnaryRepeatParams类 型,包 含 操 作 数 相 邻 迭 代 间 相 同DataBlock的 地 址 步 长,操 作 数 同 一 迭 代 内 不 同DataBlock的 地 址 步 长 等 参 数。
相 邻 迭 代 间 的 地 址 步 长 参 数 说 明 请 参 考repeatStride;同 一 迭 代 内DataBlock的 地 址 步 长 参 数 说 明 请 参 考dataBlockStride

数 据 类 型

T和U支 持 的 数 据 类 型 为:half、float。

返 回 值 说 明

约 束 说 明

  • 针 对 如 下 型 号,当 参 数count或repeatTime取 值 为0时,不 会 执 行 计 算 操 作,不 会 对 目 的 操 作 数 进 行 写 入,该 接 口 将 被 视 为NOP(空 操 作)。

    • Atlas A3 训 练 系 列 产 品/Atlas A3 推 理 系 列 产 品
    • Atlas A2 训 练 系 列 产 品/Atlas A2 推 理 系 列 产 品

调 用 示 例

  • tensor高 维 切 分 计 算 样 例-mask连 续 模 式

    C++
    uint64_t mask = 128;
    half scalar = 0.001;
    AscendC::LeakyRelu(dstLocal, srcLocal, scalar, mask, 4, {1, 1, 8, 8});
    
  • tensor高 维 切 分 计 算 样 例-mask逐bit模 式

    C++
    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    half scalar = 0.001;
    AscendC::LeakyRelu(dstLocal, srcLocal, scalar, mask, 4, {1, 1, 8, 8});
    
  • tensor前n个 数 据 计 算 样 例

    C++
    half scalar = 0.001;
    AscendC::LeakyRelu(dstLocal, srcLocal, scalar, 512);
    

结 果 示 例 如 下:

Bash
输 入 数 据src0Local:[-287.  246. -438.  177. 596. -950.  -293.  322. ... -900.]
输 入 数 据scalar = 0.001
输 出 数 据dstLocal:[-0.287 246. -0.438 177. 596. -0.950 -0.293 322. ... -0.900]

免 责 声 明:本 站 内 容 由 asc-devkit 仓 master 分 支 自 动 编 译 生 成,属 于 持 续 开 发 版 本,可 能 存 在 缺 陷,仅 供 预 览 与 参 考。如 需 稳 定 及 商 用 资 料,请 查 阅 官 方 昇 腾 社 区