ReduceXorSum
【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit
产品支持情况
功能说明
按照元素执行Xor(按位异或)运算,并将计算结果ReduceSum求和。
[!NOTE]说明注意:当最终计算结果超出int16范围[-32768,32767]后,将输出-32768 或者 32767。
函数原型
通过sharedTmpBuffer入参传入临时空间
template <typename T, bool isReuseSource = false> __aicore__ inline void ReduceXorSum(LocalTensor<T>& dstTensor, const LocalTensor<T>& src0Tensor, const LocalTensor<T>& src1Tensor, LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount)接口框架申请临时空间
template <typename T, bool isReuseSource = false> __aicore__ inline void ReduceXorSum(LocalTensor<T>& dstTensor, const LocalTensor<T>& src0Tensor, const LocalTensor<T>& src1Tensor, const uint32_t calCount);
由于该接口的内部实现中需要保存异或结果以及进行其他运算,需要额外的临时空间来存储计算过程中的中间变量。临时空间支持开发者通过sharedTmpBuffer入参传入和接口框架申请两种方式。
- 通过sharedTmpBuffer入参传入,使用该tensor作为临时空间进行处理,接口框架不再申请。该方式开发者可以自行管理sharedTmpBuffer内存空间,并在接口调用完成后,复用该部分内存,内存不会反复申请释放,灵活性较高,内存利用率也较高。
- 接口框架申请临时空间,开发者无需申请,但是需要预留临时空间的大小。
通过sharedTmpBuffer传入的情况,开发者需要为tensor申请空间;接口框架申请的方式,开发者需要预留临时空间。临时空间大小BufferSize的获取方式如下:通过GetReduceXorSumMaxMinTmpSize中提供的接口获取需要预留空间范围的大小。
参数说明
表 1模板参数说明
表 2接口参数说明
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 输出值需要sizeof(T)大小的空间进行保存。开发者要根据该大小和框架的对齐要求来为dstTensor分配实际内存空间。 | ||
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 | ||
返回值说明
无
约束说明
操作数地址对齐要求请参见通用地址对齐约束。
不支持源操作数与目的操作数地址重叠。
不支持sharedTmpBuffer与源操作数和目的操作数地址重叠。
calCount需要保证小于或等于src0Tensor和src1Tensor的元素范围。
当最终计算结果超出int16范围[-32768,32767]后,将输出-32768 或者 32767。
调用示例
AscendC::TPipe pipe; AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueX; AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueY; AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueue; AscendC::TQue<AscendC::TPosition::VECCALC, 1> tmpQue; pipe.InitBuffer(inQueueX, 1, 32 * sizeof(int16_t)); pipe.InitBuffer(inQueueY, 1, 32 * sizeof(int16_t)); pipe.InitBuffer(outQueue, 1, 32); pipe.InitBuffer(tmpQue, 1, bufferSize); // bufferSize 通过Host侧tiling参数获取 AscendC::LocalTensor<int16_t> dstLocal = outQueue.AllocTensor<int16_t>(); AscendC::LocalTensor<int16_t> src0Local = inQueueX.AllocTensor<int16_t>(); AscendC::LocalTensor<int16_t> src1Local = inQueueY.AllocTensor<int16_t>(); AscendC::LocalTensor<uint8_t> sharedTmpBuffer = tmpQue.AllocTensor<uint8_t>(); // 不使用输入内存,输入shape信息为32, 算子输入的数据类型为int16_t, 实际计算个数为前32 AscendC::ReduceXorSum<int16_t, false>(dstLocal, src0Local, src1Local, sharedTmpBuffer, 32);结果示例如下:
输入输出的数据类型为int16_t 输入数据(src0Local): [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 输入数据(src1Local): [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] 输出数据(dstLocal): [32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] // 仅32为有效值【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考