Skip to content
版 本

ReduceMin

产 品 支 持 情 况

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

功 能 说 明

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

ReduceMin接 口 用 于 从 所 有 输 入 数 据 中 找 出 最 小 值 和 最 小 值 索 引。

ReduceMin计 算 过 程 如 下 图 所 示:首 先,在 每 个repeat迭 代 中 计 算 得 到 最 小 值 和repeat内 部 索 引,这 些 中 间 结 果 暂 存 于sharedTmpBuffer工 作 区 中;然 后,在 中 间 结 果 的 基 础 上 继 续 按repeat迭 代 得 到 最 终 的 最 小 值 和 最 小 值 索 引。需 要 注 意 的 是,每 次repeat迭 代 获 取 的 最 小 值 索 引 是repeat内 部 索 引,返 回 最 终 结 果 时,需 要 根 据 迭 代 位 置 和repeat内 部 索 引 推 导 全 量 数 据 的 最 小 值 索 引。

图 1 ReduceMin计 算 示 意 图

ReduceMin计算示意图

函 数 原 型

  • tensor前n个 数 据 计 算:

    C++
    template <typename T>
    __aicore__ inline void ReduceMin(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const int32_t count, bool calIndex = 0)
    
  • tensor高 维 切 分 计 算:

    • mask逐bit模 式:

      C++
      template <typename T>
      __aicore__ inline void ReduceMin(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const uint64_t mask[], const int32_t repeatTime, const int32_t srcRepStride, bool calIndex = 0)
      
    • mask连 续 模 式:

      C++
      template <typename T>
      __aicore__ inline void ReduceMin(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const int32_t mask, const int32_t repeatTime, const int32_t srcRepStride, bool calIndex = 0)
      

参 数 说 明

表 1 模 板 参 数 说 明

参 数 名描 述
T操 作 数 数 据 类 型。

表 2 参 数 说 明

参 数 名 称输 入/输 出含 义
dst输 出目 的 操 作 数。
类 型 为LocalTensor,支 持 的TPosition为VECIN、VECCALC、VECOUT(存 储 位 置 为Unified Buffer)。
src输 入源 操 作 数。
类 型 为LocalTensor,支 持 的TPosition为VECIN、VECCALC、VECOUT(存 储 位 置 为Unified Buffer)。
sharedTmpBuffer输 入指 令 执 行 期 间 存 储 中 间 结 果,用 于 内 部 计 算 所 需 操 作 空 间,需 特 别 注 意 空 间 大 小。详 情 请 参 考关 键 特 性 说 明
类 型 为LocalTensor,支 持 的TPosition为VECIN、VECCALC、VECOUT(存 储 位 置 为Unified Buffer)。
count输 入参 与 计 算 的 元 素 个 数。关 于 该 参 数 的 具 体 说 明 请 参 考连 续 计 算。最 大 处 理 的 数 据 量 不 能 超 过UB大 小 限 制。
mask/mask[]输 入mask用 于 控 制 每 次 迭 代 内 参 与 计 算 的 源 操 作 数。详 细 设 置 参 考掩 码 概 述
repeatTime输 入迭 代 次 数。关 于 该 参 数 的 具 体 描 述 请 参 考高 维 切 分注:与 高 维 切 分 中 不 同 的 是,repeatTime可 以 支 持 更 大 的 取 值 范 围,保 证 不 超 过int32_t的 最 大 值 即 可。
srcRepStride输 入源 操 作 数 相 邻 迭 代 间 的 地 址 步 长,即 源 操 作 数 每 次 迭 代 跳 过 的DataBlock数 目。取 值 范 围 为[0, $2^{16}-1$]。
calIndex输 入指 定 是 否 获 取 最 小 值 的 索 引,bool类 型,默 认 值 为false,取 值:
true:同 时 获 取 最 小 值 和 最 小 值 索 引。
false:不 获 取 索 引,只 获 取 最 小 值。

注:以 上 高 维 切 分 相 关 参 数maskrepeatTimesrcRepStride请 参 考高 维 切 分中 的 介 绍。

数 据 类 型

支 持 的 数 据 类 型 如 下:

  • Ascend 950PR/Ascend 950DT,支 持int16_t、uint16_t、half、int32_t、uint32_t、float、int64_t、uint64_t。
  • Atlas A3 训 练 系 列 产 品/Atlas A3 推 理 系 列 产 品,支 持half、float。
  • Atlas A2 训 练 系 列 产 品/Atlas A2 推 理 系 列 产 品,支 持half、float。
  • Atlas 200I/500 A2 推 理 产 品,支 持half、float。
  • Atlas 推 理 系 列 产 品 AI Core,支 持half、float。
  • Atlas 训 练 系 列 产 品,支 持half。
  • Kirin X90,支 持half、float。
  • Kirin 9030,支 持half、float。

返 回 值 说 明

约 束 说 明

  • 源 操 作 数 及sharedTmpBuffer的 地 址 对 齐 约 束 请 参 考通 用 地 址 对 齐 约 束,起 始 地 址 需 要32字 节 对 齐;目 的 操 作 数 的 起 始 地 址 对 齐 约 束 请 参 考ReduceRepeat-表3
  • 操 作 数 地 址 重 叠 约 束 请 参 考通 用 地 址 重 叠 约 束
  • 需 要 使 用sharedTmpBuffer的 情 况 下,支 持dstsharedTmpBuffer地 址 重 叠(通 常 情 况 下dstsharedTmpBuffer所 需 的 空 间 要 小),此 时sharedTmpBuffer必 须 满 足 所 需 空 间 要 求,详 情 请 参 考关 键 特 性 说 明
  • 针 对 如 下 型 号,当mask=0repeatTime=0时,不 会 执 行 归 约 操 作,不 会 对 目 的 操 作 数 进 行 写 入,该 接 口 将 被 视 为NOP(空 操 作)。
    • Atlas A3 训 练 系 列 产 品/Atlas A3 推 理 系 列 产 品
    • Atlas A2 训 练 系 列 产 品/Atlas A2 推 理 系 列 产 品
  • srcRepStride取 值 范 围 为[0, $2^{16}-1$],需 要 结 合UB的 实 际 大 小 避 免 出 现 越 界。
  • 如 果 存 在 多 个 最 小 值,该 指 令 会 将 最 小 索 引 写 入 目 的 操 作 数。
  • dst结 果 存 储 顺 序 为 最 小 值,最 小 值 索 引,若 不 需 要 索 引,只 会 存 储 最 小 值。
  • 当 输 入 类 型 是half的 时 候,只 支 持 获 取 最 大 不 超 过65535(uint16_t能 表 示 的 最 大 值)的 索 引 值。
  • 索 引 按 操 作 数 的 数 据 类 型 存 储,读 取 索 引 需 要 将 类 型 转 换 到 整 型。请 参 考ReduceRepeat关 键 特 性 说 明
  • 针 对Ascend 950PR/Ascend 950DT,int64_t/uint64_t数 据 类 型 仅 支 持tensor前n个 数 据 计 算 接 口。
  • 对 于Ascend 950PR/Ascend 950DT,因 接 口 内 部 算 法 实 现 不 同,无 需 使 用sharedTmpBuffer,可 以 直 接 传 入src或 者 任 意 大 小 的sharedTmpBuffer
  • 针 对 如 下 型 号,需 要 使 用sharedTmpBuffer
    • Atlas A3 训 练 系 列 产 品/Atlas A3 推 理 系 列 产 品
    • Atlas A2 训 练 系 列 产 品/Atlas A2 推 理 系 列 产 品
    • Atlas 200I/500 A2 推 理 产 品
    • Atlas 推 理 系 列 产 品 AI Core
    • Atlas 训 练 系 列 产 品
    • Kirin X90
    • Kirin 9030

关 键 特 性 说 明

调 用 示 例

更 多 样 例 请 参 考ReduceMin样 例

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

    C++
    // dstLocal,srcLocal和sharedTmpBuffer均 为half类 型,srcLocal的 计 算 数 据 量 为8320,并 且 连 续 排 布,需 要 索 引 值,使 用tensor高 维 切 分 计 算 接 口,设 定repeatTime为65,mask为 全 部 元 素 参 与 计 算
    int32_t mask = 128;
    AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, 65, 8, true);
    
  • tensor高 维 切 分 计 算 样 例-mask逐bit模 式:

    C++
    // dstLocal,srcLocal和sharedTmpBuffer均 为half类 型,srcLocal的 计 算 数 据 量 为8320,并 且 连 续 排 布,需 要 索 引 值,使 用tensor高 维 切 分 计 算 接 口,设 定repeatTime为65,mask为 全 部 元 素 参 与 计 算
    uint64_t mask[2] = { 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF };
    AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, 65, 8, true);
    
  • tensor前n个 数 据 计 算 样 例:

    C++
    // dstLocal,srcLocal和sharedTmpBuffer均 为half类 型,srcLocal的 计 算 数 据 量 为8320,并 且 连 续 排 布,需 要 索 引 值,使 用tensor前n个 数 据 计 算 接 口
    AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, 8320, true);
    
  • tensor高 维 切 分 计 算 接 口 完 整 调 用 示 例:

    C++
    #include "kernel_operator.h"
    
    int srcDataSize = 512;
    int dstDataSize = 512;
    int mask = 128;
    int repStride = 8;
    int repeat = srcDataSize / mask;
    
    // 初 始 化srcLocal 、dstLocal 、sharedTmpBuffer
    AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>();
    AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>();
    AscendC::LocalTensor<half> sharedTmpBuffer = workQueue.AllocTensor<half>();
    
    // mask为128一 次 计 算128个 元 素,4次repeat计 算 完512个 数,calIndex为true,获 取 最 小 值 的 索 引
    AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, repeat, repStride, true);
    // 释 放Tensor
    outQueueDst.EnQue<half>(dstLocal);
    inQueueSrc.FreeTensor(srcLocal);
    workQueue.FreeTensor(sharedTmpBuffer);
    

    示 例 结 果 如 下:

    输 入 数 据src_gm:

    [0.769 0.8584 0.1082 0.2715 0.1759 0.7646 0.6406 0.2944 0.4255 0.927 0.8022 0.04507 0.9688 0.919 0.3008 0.7144 0.3206 0.6753 0.8276 ... 0.304 ]

    输 出 数 据dst_gm:

    [0.01034, 2.104e-05] // 2.104e-05需 要 使 用reinterpret_cast方 法 转 换 得 到 索 引 值353

  • tensor前n个 数 据 计 算 接 口 完 整 调 用 示 例:

    C++
    #include "kernel_operator.h"
    
    int srcDataSize = 288;
    // 初 始 化srcLocal 、dstLocal 、sharedTmpBuffer
    AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>();
    AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>();
    AscendC::LocalTensor<half> sharedTmpBuffer = workQueue.AllocTensor<half>();
    
    // level2接 口 计 算 前288个 数,calIndex为true,获 取 最 小 值 的 索 引
    AscendC::ReduceMin<half>(dstLocal, srcLocal, sharedTmpBuffer, srcDataSize, true);
    // 释 放Tensor
    outQueueDst.EnQue<half>(dstLocal);
    inQueueSrc.FreeTensor(srcLocal);
    workQueue.FreeTensor(sharedTmpBuffer);
    

    示 例 结 果 如 下:

    输 入 数 据src_gm:

    [0.556 0.5225 0.3623 0.214 0.556 0.0643 0.769 0.594 0.261 0.3652 0.911 0.924 0.386 0.3696 0.2296 0.5957 0.1709 0.79 0.8516 ... 0.2242 ]

    输 出 数 据dst_gm:

    [0.00393, 4.3e-06] // 4.3e-06需 要 使 用reinterpret_cast方 法 转 换 得 到 索 引 值72

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