Reciprocal

产品支持情况

产品 是否支持(不传入config的原型) 是否支持(传入config的原型)
Ascend 950PR/Ascend 950DT x
Atlas A3 训练系列产品/Atlas A3 推理系列产品 x
Atlas A2 训练系列产品/Atlas A2 推理系列产品 x
Atlas 200I/500 A2 推理产品 x
Atlas 推理系列产品AI Core x
Atlas 推理系列产品Vector Core x x
Atlas 训练系列产品 x
Kirin X90 x
Kirin 9030 x

功能说明

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

Reciprocal属于单目矢量类计算接口,负责将输入的tensor按元素取倒数。计算公式如下:

dsti=1srcidst_i = \frac{1}{src_i}

函数原型

  • 传入config的原型

    • tensor前n个数据连续计算

      template <typename T, const ReciprocalConfig& config = DEFAULT_RECIPROCAL_CONFIG>
      __aicore__ inline void Reciprocal(const LocalTensor<T>& dst, const LocalTensor<T>& src, const int32_t& count)
      
    • tensor高维切分计算

      • mask逐bit模式

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

        template <typename T, bool isSetMask = true, const ReciprocalConfig& config = DEFAULT_RECIPROCAL_CONFIG>
        __aicore__ inline void Reciprocal(const LocalTensor<T>& dst, const LocalTensor<T>& src, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
        
  • 不传入config的原型

    • tensor前n个数据连续计算

      template <typename T>
      __aicore__ inline void Reciprocal(const LocalTensor<T>& dst, const LocalTensor<T>& src, const int32_t& count)
      
    • tensor高维切分计算

      • mask逐bit模式

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

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

参数说明

表 1 模板参数说明

参数名 描述
T 操作数数据类型。
isSetMask 是否在接口内部设置mask。
• true,表示在接口内部设置mask。
• false,表示在接口外部设置mask,开发者需要使用SetVectorMask接口设置mask值。这种模式下,本接口入参中的mask值必须设置为占位符MASK_PLACEHOLDER
具体使用方式可参考掩码
config 该参数仅支持Ascend 950PR/Ascend 950DT。
用于配置Subnormal计算模式,ReciprocalConfig类型,定义如下:

enum class ReciprocalAlgo {
    INTRINSIC = 0,
    PRECISION_1ULP_FTZ_TRUE,
    PRECISION_1ULP_FTZ_FALSE,
};
struct ReciprocalConfig {
    ReciprocalAlgo algo = ReciprocalAlgo::INTRINSIC;
};
通过ReciprocalConfig结构体的参数algo来配置Subnormal计算模式。algo取值如下:
• ReciprocalAlgo::INTRINSIC、ReciprocalAlgo::PRECISION_1ULP_FTZ_TRUE,使用单指令计算得出结果,所有Subnormal被近似为0。
• ReciprocalAlgo::PRECISION_1ULP_FTZ_FALSE,支持Subnormal数据计算。
该参数的默认值DEFAULT_RECIPROCAL_CONFIG的取值如下:

constexpr ReciprocalConfig DEFAULT_RECIPROCAL_CONFIG = { ReciprocalAlgo::INTRINSIC };

表 2 参数说明

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

数据类型

  • 针对Ascend 950PR/Ascend 950DT,T支持的数据类型为:half、float、int64_t、uint64_t。数据类型int64_t、uint64_t仅支持tensor前n个数据计算接口。
  • 针对Atlas A3 训练系列产品/Atlas A3 推理系列产品,T支持的数据类型为:half、float。
  • 针对Atlas A2 训练系列产品/Atlas A2 推理系列产品,T支持的数据类型为:half、float。
  • 针对Atlas 200I/500 A2 推理产品,T支持的数据类型为:half、float。
  • 针对Atlas 推理系列产品AI Core,T支持的数据类型为:half、float。
  • 针对Atlas 训练系列产品,T支持的数据类型为:half、float。
  • 针对Kirin X90,T支持的数据类型为:half、float。
  • 针对Kirin 9030,T支持的数据类型为:half、float。

返回值说明

约束说明

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

    • Atlas A3 训练系列产品/Atlas A3 推理系列产品
    • Atlas A2 训练系列产品/Atlas A2 推理系列产品
  • 如果src中的数值为0,可能会产生未知结果。
  • 使用Reciprocal时,half的算子结果对比误差不满足双千分之一的要求,float的算子结果对比误差不满足双万分之一的要求,如果需要高精度,建议使用Div替代实现。

调用示例

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

    uint64_t mask = 256 / sizeof(half);
    // repeatTime = 4, 128 elements one repeat, 512 elements total.
    // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat.
    // dstRepStride, srcRepStride = 8, no gap between repeats.
    AscendC::Reciprocal(dstLocal, srcLocal, mask, 4, { 1, 1, 8, 8 });
    
  • tensor高维切分计算样例-mask逐bit模式

    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    // repeatTime = 4, 128 elements one repeat, 512 elements total.
    // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat.
    // dstRepStride, srcRepStride = 8, no gap between repeats.
    AscendC::Reciprocal(dstLocal, srcLocal, mask, 4, { 1, 1, 8, 8 });
    
  • tensor前n个数据计算接口样例

    AscendC::Reciprocal(dstLocal, srcLocal, 512);
    

    以下示例仅支持Ascend 950PR/Ascend 950DT

    static constexpr ReciprocalConfig config = { ReciprocalAlgo::PRECISION_1ULP_FTZ_FALSE };
    AscendC::Reciprocal<T, config>(dstLocal, srcLocal, 512);
    

结果示例如下:

输入数据srcLocal:[-7.152 -7.24 1.771 ... -1.339 4.473]
输出数据dstLocal:[-0.1396 -0.1382 0.5645 ... -0.748 0.2231]