Skip to content

ReducePairElem

产品支持情况

  • Ascend 950PR/Ascend 950DT:支持
  • Atlas A3 训练系列产品/Atlas A3 推理系列产品:支持
  • Atlas A2 训练系列产品/Atlas A2 推理系列产品:支持
  • Atlas 200I/500 A2 推理产品:支持
  • Atlas 推理系列产品AI Core:支持
  • Atlas 推理系列产品Vector Core:不支持
  • Atlas 训练系列产品:支持

功能说明

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

ReducePairElem接口根据模板参数reduceType,对相邻两个(奇偶)元素进行归约操作,结果按顺序写入目标地址。当前仅支持求和操作。

例如,[a1, a2, a3, a4, a5, a6, ...]的求和结果为[a1+a2, a3+a4, a5+a6, ...]。计算过程如下图所示,灰色部分代表无效元素。

图1 ReducePairElem<ReduceType::SUM>示意图

ReducePairElemReduceType::SUM示意图

函数原型

  • mask逐bit模式:

    C++
    template <ReduceType reduceType, typename T, typename U, bool isSetMask = true>
    __aicore__ inline void ReducePairElem(const LocalTensor<T>& dst, const LocalTensor<U>& src, const uint64_t mask[], const int32_t repeatTime, const int32_t dstRepStride, const int32_t srcBlkStride, const int32_t srcRepStride)
    
  • mask连续模式:

    C++
    template <ReduceType reduceType, typename T, typename U, bool isSetMask = true>
    __aicore__ inline void ReducePairElem(const LocalTensor<T>& dst, const LocalTensor<U>& src, const int32_t mask, const int32_t repeatTime, const int32_t dstRepStride, const int32_t srcBlkStride, const int32_t srcRepStride)
    

参数说明

表1 模板参数说明

参数名描述
reduceType归约操作类型。
类型为ReduceType枚举类型,支持:
ReduceType::SUM:求和。
T目的操作数数据类型。
U源操作数数据类型。
isSetMask是否在接口内部设置mask。
• true,表示在接口内部设置mask。
• false,表示在接口外部设置mask,开发者需要使用SetVectorMask接口设置mask值。这种模式下,接口入参中的mask值设置为占位符MASK_PLACEHOLDER,用于占位,无实际含义。

表2 参数说明

参数名称输入/输出含义
dst输出目的操作数。
类型为LocalTensor,支持的TPosition为VECIN、VECCALC、VECOUT。
src输入源操作数。
类型为LocalTensor,支持的TPosition为VECIN、VECCALC、VECOUT。
mask[]/mask输入mask用于控制每次迭代内参与计算的源操作数。详细设置参考掩码概述
repeatTime输入迭代次数。取值范围为[0, 255]。
dstRepStride输入目的操作数相邻迭代间的地址步长,以一个repeatTime归约后的长度为单位,即128字节。取值范围为[0, ]。

注意,Atlas 训练系列产品不支持此参数值配为0。
srcBlkStride输入单次迭代内DataBlock的地址步长,单位为32字节。取值范围为[0, ]。
srcRepStride输入源操作数相邻迭代间的地址步长,即源操作数每次迭代跳过的DataBlock数目。取值范围为[0, ]。

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

数据类型

支持的数据类型如下:

  • Ascend 950PR/Ascend 950DT,支持half、float。
  • 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。

目的操作数与源操作数的数据类型需要保持一致。

返回值说明

约束说明

  • 操作数的起始地址对齐约束请参考通用地址对齐约束,需要32字节对齐。
  • 操作数地址重叠约束请参考通用地址重叠约束
  • dstRepStridesrcBlkStridesrcRepStride取值范围为[0, ],需要结合UB的实际大小避免出现越界。
  • 当参数mask或repeatTime取值为0时,该接口的行为如下:

    • 针对如下型号,当参数mask或repeatTime取值为0时,不会执行计算操作,不会对目的操作数进行写入,该接口将被视为NOP(空操作)。
      • Atlas A3 训练系列产品/Atlas A3 推理系列产品
      • Atlas A2 训练系列产品/Atlas A2 推理系列产品
    • 针对Ascend 950PR/Ascend 950DT,该接口通过VF调用Reg矢量计算API实现兼容,当参数mask或repeatTime取值为0时,不保证该接口被视为NOP(空操作)。
  • 对UB空间的占用说明。针对Ascend 950PR/Ascend 950DT:
    • tensor高维切分计算占用8KB Unified Buffer。
    • tensor前n个数据连续计算不涉及8KB Unified Buffer的占用。
  • 当一对相邻元素的掩码均为0时,该接口的行为如下:

    • 针对如下型号,对应的目的元素将跳过写入,保持不变。
      • Atlas A3 训练系列产品/Atlas A3 推理系列产品
      • Atlas A2 训练系列产品/Atlas A2 推理系列产品
      • Atlas 推理系列产品AI Core
    • 针对如下型号,对应的目的操作数中的值会置为0。
      • Ascend 950PR/Ascend 950DT
      • Atlas 200I/500 A2 推理产品

调用示例

本样例中只展示Compute流程中的部分代码。更多样例可参考ReducePairElem样例

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

    C++
    // 设定mask为最多的128个全部元素参与计算
    int32_t mask = 256/sizeof(half);
    // 每个repeat128个元素,一共128个元素。
    int repeat = 1;
    // dstLocal: 目的操作数tensor
    // srcLocal: 源操作数tensor
    // srcBlkStride = 1,在一个repeat中,block间没有空隙。
    // dstRepStride = 1,srcRepStride = 8,repeat间没有空隙。
    AscendC::ReducePairElem<AscendC::ReduceType::SUM, half>(dstLocal, srcLocal, mask, repeat, 1, 1, 8);
    
  • ReducePairElem tensor高维切分计算样例-mask逐bit模式:

    C++
    // 设定mask为最多的128个全部元素参与计算
    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    // 每个repeat128个元素,一共128个元素。
    int repeat = 1;
    // dstLocal: 目的操作数tensor
    // srcLocal: 源操作数tensor
    // srcBlkStride = 1,在一个repeat中,block间没有空隙。
    // dstRepStride = 1,srcRepStride = 8,repeat间没有空隙。
    AscendC::ReducePairElem<AscendC::ReduceType::SUM, half>(dstLocal, srcLocal, mask, repeat, 1, 1, 8);
    
  • 示例结果

    输入数据src_gm:

    [1, 1, 1, -1, 2, 2, -1, 2,
     3, 3, 3, -1, 4, 4, -2, 4,
     ....
    ]
    

    输出数据dst_gm:

    [2, 0, 4, 1, 6, 2, 8, 2,
    ....
    ]
    

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