
Power【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit产品支持情况Ascend 950PR/Ascend 950DT支持Atlas A3 训练系列产品/Atlas A3 推理系列产品支持Atlas A2 训练系列产品/Atlas A2 推理系列产品支持Atlas 200I/500 A2 推理产品不支持Atlas 推理系列产品AI Core支持Atlas 推理系列产品Vector Core不支持Atlas 训练系列产品不支持功能说明实现按元素做幂运算功能提供3类接口处理逻辑如下函数原型Power(dstTensor, src0Tensor, src1Tensor)通过sharedTmpBuffer入参传入临时空间源操作数Tensor全部/部分参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const LocalTensorT src0Tensor, const LocalTensorT src1Tensor, const LocalTensoruint8_t sharedTmpBuffer, uint32_t calCount)源操作数Tensor全部参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const LocalTensorT src0Tensor, const LocalTensorT src1Tensor, const LocalTensoruint8_t sharedTmpBuffer)接口框架申请临时空间源操作数Tensor全部/部分参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const LocalTensorT src0Tensor, const LocalTensorT src1Tensor, uint32_t calCount)源操作数Tensor全部参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const LocalTensorT src0Tensor, const LocalTensorT src1Tensor)Power(dstTensor, src0Tensor, src1Scalar)通过sharedTmpBuffer入参传入临时空间源操作数Tensor全部/部分参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const LocalTensorT src0Tensor, const T src1Scalar, const LocalTensoruint8_t sharedTmpBuffer, uint32_t calCount)源操作数Tensor全部参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const LocalTensorT src0Tensor, const T src1Scalar, const LocalTensoruint8_t sharedTmpBuffer)接口框架申请临时空间源操作数Tensor全部/部分参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const LocalTensorT src0Tensor, const T src1Scalar, uint32_t calCount)源操作数Tensor全部参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const LocalTensorT src0Tensor, const T src1Scalar)Power(dstTensor, src0Scalar, src1Tensor)通过sharedTmpBuffer入参传入临时空间源操作数Tensor全部/部分参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const T src0Scalar, const LocalTensorT src1Tensor, const LocalTensoruint8_t sharedTmpBuffer, uint32_t calCount)源操作数Tensor全部参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const T src0Scalar, const LocalTensorT src1Tensor, const LocalTensoruint8_t sharedTmpBuffer)接口框架申请临时空间源操作数Tensor全部/部分参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const T src0Scalar, const LocalTensorT src1Tensor, uint32_t calCount)源操作数Tensor全部参与计算template typename T, bool isReuseSource false, const PowerConfig config defaultPowerConfig __aicore__ inline void Power(const LocalTensorT dstTensor, const T src0Scalar, const LocalTensorT src1Tensor)由于该接口的内部实现中涉及复杂的数学计算需要额外的临时空间来存储计算过程中的中间变量。临时空间支持接口框架申请和开发者通过sharedTmpBuffer入参传入两种方式。接口框架申请临时空间开发者无需申请但是需要预留临时空间的大小。通过sharedTmpBuffer入参传入使用该tensor作为临时空间进行处理接口框架不再申请。该方式开发者可以自行管理sharedTmpBuffer内存空间并在接口调用完成后复用该部分内存内存不会反复申请释放灵活性较高内存利用率也较高。接口框架申请的方式开发者需要预留临时空间通过sharedTmpBuffer传入的情况开发者需要为sharedTmpBuffer申请空间。临时空间大小BufferSize的获取方式如下通过GetPowerMaxMinTmpSize中提供的GetPowerMaxMinTmpSize接口获取需要预留空间的范围大小。参数说明表1模板参数说明参数名描述T操作数的数据类型。不同型号支持的数据类型请参考支持的数据类型。isReuseSource是否允许修改源操作数。该参数预留传入默认值false即可。config该参数仅支持Ascend 950PR/Ascend 950DT。Power计算的相关配置。此参数可选配PowerConfig类型具体定义如下方代码所示其中参数的含义为algo不同的数据类型支持的不同Power算法。该参数支持的取值如下INTRINSIC默认值。如果数据类型是整型INTRINSIC算法使用快速幂算法实现Power计算支持的数据类型为uint8_t、int8_t、uint16_t、int16_t、uint32_t、int32_t。如果数据类型是浮点数类型INTRINSIC算法按照公式Power(x, y) exp(y * ln(x))进行Power计算支持的数据类型为half、float。DOUBLE_FLOAT_TECHDOUBLE_FLOAT_TECH算法是高精度浮点数算法将源操作数的精度提升后按照公式Power(x, y) exp(y * ln(x))进行Power计算减少计算过程中的精度损失支持的数据类型为bfloat16_t、half、float。enum class PowerAlgo { INTRINSIC 0, DOUBLE_FLOAT_TECH, }; struct PowerConfig { PowerAlgo algo PowerAlgo::INTRINSIC; };表2接口参数说明参数名输入/输出描述dstTensor输出目的操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。src0Tensor输入源操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。源操作数的数据类型需要与目的操作数保持一致。src1Tensor输入源操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。源操作数的数据类型需要与目的操作数保持一致。src0Scalar/src1Scalar输入源操作数类型为Scalar。源操作数的数据类型需要与目的操作数保持一致。sharedTmpBuffer输入临时内存空间。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。针对3个power接口不同输入数据类型情况下临时空间大小BufferSize的获取方式请参考GetPowerMaxMinTmpSize。calCount输入参与计算的元素个数。返回值说明无约束说明不支持源操作数与目的操作数地址重叠。对于Atlas 推理系列产品AI Core幂运算的指数必须小于231-1。操作数地址对齐要求请参见通用地址对齐约束。支持的数据类型Ascend 950PR/Ascend 950DT支持的数据类型为uint8_t、int8_t、uint16_t、int16_t、uint32_t、int32_t、half、bfloat16_t、float。Atlas A3 训练系列产品/Atlas A3 推理系列产品支持的数据类型为half、float、int32_t。Atlas A2 训练系列产品/Atlas A2 推理系列产品支持的数据类型为half、float、int32_t。Atlas 推理系列产品AI Core支持的数据类型为half、float、int32_t。调用示例完整的调用样例请参考Power样例。// dstLocal: 存放计算结果的Tensor // srcLocalExp: Power计算使用的指数Tensor // srcLocalBase: Power计算使用的底数Tensor // 使用srcLocalBase做底数对srcLocalExp中的全部元素做幂运算 AscendC::PowerT, false(dstLocal, srcLocalBase, srcLocalExp); // scalarValueBase: Power计算使用的底数 T scalarValueBase srcLocalBase.GetValue(0); // 使用同一个底数scalarValueBase对srcLocalExp中的全部元素做幂运算 AscendC::PowerT, false(dstLocal, scalarValueBase, srcLocalExp); // scalarValueExp: Power计算使用的指数 T scalarValueExp srcLocalExp.GetValue(0); // 使用同一个指数scalarValueExp对srcLocalBase中的全部元素做幂运算 AscendC::PowerT, false(dstLocal, srcLocalBase, scalarValueExp); // static constexpr AscendC::PowerConfig config { AscendC::PowerAlgo::DOUBLE_FLOAT_TECH }; // AscendC::PowersrcType, false, config(dstLocal, scalarValue, srcLocal2);AscendC::PowerT, false(dstLocal, srcLocalBase, srcLocalExp)示例数据如下输入数据(srcLocalBase): [2 3 4 5 6 7 8 9] 输入数据(srcLocalExp): [4 3 2 1 4 3 2 1] 输出数据(dstLocal): [16 27 16 5 1296 343 64 9]AscendC::PowerT, false(dstLocal, scalarValueBase, srcLocalExp)示例数据如下输入数据(scalarValueBase): 2 输入数据(srcLocalExp): [4 3 2 1 4 3 2 1] 输出数据(dstLocal): [16 8 4 2 16 8 4 2]AscendC::PowerT, false(dstLocal, srcLocalBase, scalarValueExp)示例数据如下输入数据(srcLocalBase): [2 3 4 5 6 7 8 9] 输入数据(scalarValueExp): 4 输出数据(dstLocal): [16 81 256 625 1296 2401 4096 6561]【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考