CANN/asc-devkit:SIMD矢量比较API Compare产品支持情况产品是否支持Ascend 950PR/Ascend 950DT√Atlas A3 训练系列产品 / Atlas A3 推理系列产品√Atlas A2 训练系列产品 / Atlas A2 推理系列产品√Atlas 200I/500 A2 推理产品√Atlas 推理系列产品 AI Core√Atlas 推理系列产品 Vector CorexAtlas 训练系列产品√Kirin X90√Kirin 9030√功能说明逐元素比较两个tensor大小如果比较后的结果为真则输出结果的对应比特位为1否则为0。支持多种比较模式LT小于less thanGT大于greater thanGE大于或等于greater than or equal toEQ等于equal toNE不等于not equal toLE小于或等于less than or equal to函数原型整个Tensor参与计算dst src0 src1; dst src0 src1; dst src0 src1; dst src0 src1; dst src0 src1; dst src0 ! src1;Atlas 200I/500 A2 推理产品暂不支持整个Tensor参与计算的运算符重载。Tensor前n个数据计算template typename T, typename U __aicore__ inline void Compare(const LocalTensorU dst, const LocalTensorT src0, const LocalTensorT src1, CMPMODE cmpMode, uint32_t count)Tensor高维切分计算mask逐bit模式template typename T, typename U, bool isSetMask true __aicore__ inline void Compare(const LocalTensorU dst, const LocalTensorT src0, const LocalTensorT src1, CMPMODE cmpMode, const uint64_t mask[], uint8_t repeatTime, const BinaryRepeatParams repeatParams)mask连续模式template typename T, typename U, bool isSetMask true __aicore__ inline void Compare(const LocalTensorU dst, const LocalTensorT src0, const LocalTensorT src1, CMPMODE cmpMode, const uint64_t mask, uint8_t repeatTime, const BinaryRepeatParams repeatParams)参数说明表 1模板参数说明参数名描述T源操作数数据类型。Ascend 950PR/Ascend 950DT支持的数据类型为int8_t、uint8_t、int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float、int64_t、uint64_t、double。Atlas A3 训练系列产品 / Atlas A3 推理系列产品支持的数据类型为half所有CMPMODE都支持、float所有CMPMODE都支持、 int32_t只支持CMPMODE::EQ。Atlas A2 训练系列产品 / Atlas A2 推理系列产品支持的数据类型为half所有CMPMODE都支持、float所有CMPMODE都支持、 int32_t只支持CMPMODE::EQ。Atlas 200I/500 A2 推理产品支持的数据类型为half、float。Atlas 推理系列产品 AI Core支持的数据类型为half、float。Atlas 训练系列产品支持的数据类型为half、float。Kirin X90支持的数据类型为half所有CMPMODE都支持、float所有CMPMODE都支持、 int32_t只支持CMPMODE::EQ。Kirin 9030支持的数据类型为half所有CMPMODE都支持、float所有CMPMODE都支持、 int32_t只支持CMPMODE::EQ。U目的操作数数据类型。Ascend 950PR/Ascend 950DT支持的数据类型为int8_t、uint8_t。Atlas A3 训练系列产品 / Atlas A3 推理系列产品支持的数据类型为int8_t、uint8_t。Atlas A2 训练系列产品 / Atlas A2 推理系列产品支持的数据类型为int8_t、uint8_t。Atlas 200I/500 A2 推理产品支持的数据类型为int8_t、uint8_t。Atlas 推理系列产品 AI Core支持的数据类型为int8_t、uint8_t。Atlas 训练系列产品支持的数据类型为int8_t、uint8_t。Kirin X90支持的数据类型为uint8_t。Kirin 9030支持的数据类型为uint8_t。isSetMask保留参数保持默认值即可。表 2接口参数说明参数名称输入/输出含义dst输出目的操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。LocalTensor的起始地址需要32字节对齐。dst用于存储比较结果将dst中uint8_t类型的数据按照bit位展开由左至右依次表征对应位置的src0和src1的比较结果如果比较后的结果为真则对应比特位为1否则为0。src0、src1输入源操作数。类型为LocalTensor支持的TPosition为VECIN/VECCALC/VECOUT。LocalTensor的起始地址需要32字节对齐。cmpMode输入CMPMODE类型表示比较模式包括EQNEGELEGTLT。LT:src0小于less thansrc1GT:src0大于greater thansrc1GEsrc0大于或等于greater than or equal tosrc1EQsrc0等于equal tosrc1NEsrc0不等于not equal tosrc1LEsrc0小于或等于less than or equal tosrc1mask/mask[]输入mask用于控制每次迭代内参与计算的元素。Ascend 950PR/Ascend 950DT设置有效。Atlas A3 训练系列产品 / Atlas A3 推理系列产品保留参数设置无效。Atlas A2 训练系列产品 / Atlas A2 推理系列产品保留参数设置无效。Atlas 200I/500 A2 推理产品设置有效。Atlas 推理系列产品 AI Core保留参数设置无效。Atlas 训练系列产品保留参数设置无效。连续模式表示前面连续的多少个元素参与计算。取值范围和操作数的数据类型有关数据类型不同每次迭代内能够处理的元素个数最大值不同。当操作数为16位时mask∈[1, 128]当操作数为32位时mask∈[1, 64]。逐bit模式可以按位控制哪些元素参与计算bit位的值为1表示参与计算0表示不参与。参数类型为长度为2或者4的uint64_t类型数组。例如mask[8, 0]80b1000表示仅第4个元素参与计算。参数取值范围和操作数的数据类型有关数据类型不同每次迭代内能够处理的元素个数最大值不同。当操作数为16位时mask[0]、mask[1]∈[0, 264-1]并且不同时为0当操作数为32位时mask[1]为0mask[0]∈(0, 264-1]。Kirin X90保留参数设置无效。Kirin 9030保留参数设置无效。repeatTime输入重复迭代次数。矢量计算单元每次读取连续的256Bytes数据进行计算为完成对输入数据的处理必须通过多次迭代repeat才能完成所有数据的读取与计算。repeatTime表示迭代的次数。关于该参数的具体描述请参考高维切分API。repeatParams输入控制操作数地址步长的参数。BinaryRepeatParams类型包含操作数相邻迭代间相同datablock的地址步长操作数同一迭代内不同datablock的地址步长等参数。相邻迭代间的地址步长参数说明请参考repeatStride同一迭代内DataBlock的地址步长参数说明请参考dataBlockStride。count输入参与计算的元素个数。设置count时需要保证count个元素所占空间256字节对齐。返回值说明无约束说明操作数地址对齐要求请参见通用地址对齐约束。dst按照小端顺序排序成二进制结果对应src中相应位置的数据比较结果。使用整个tensor参与计算的运算符重载功能src0和src1需满足256字节对齐使用tensor前n个数据参与计算的接口设置count时需要保证count个元素所占空间256字节对齐。针对Ascend 950PR/Ascend 950DTint8_t/uint8_t/uint64_t/int64_t/double数据类型仅支持tensor前n个数据计算接口和整个tensor参与计算的运算符重载。调用示例本样例中源操作数src0和src1各存储了256个float类型的数据。样例实现的功能为逐元素对src0和src1中的数据进行比较如果src0中的元素小于src1中的元素dst结果中对应的比特位置1反之则置0。dst结果使用uint8_t类型数据存储。完整的调用样例可参考Compare类样例场景一。整个tensor参与计算dstLocal src0Local src1Local; // 小于 LT dstLocal src0Local src1Local; // 大于 GT dstLocal src0Local src1Local; // 小于等于 LE dstLocal src0Local src1Local; // 大于等于 GE dstLocal src0Local src1Local; // 等于 EQ dstLocal src0Local ! src1Local; // 不等于 NEtensor前n个数据计算// srcDataSize参与计算的元素个数 AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::LT, srcDataSize); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::GT, srcDataSize); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::LE, srcDataSize); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::GE, srcDataSize); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::EQ, srcDataSize); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::NE, srcDataSize);结果示例如下LT小于 输入数据src0Local[ 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 11 11 11 11 11 11 11 11 13 13 13 13 13 13 13 13 17 17 17 17 17 17 17 17 19 19 19 19 19 19 19 19 ] 输入数据src1Local[ 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 12 12 12 12 12 12 12 12 14 14 14 14 14 14 14 14 16 16 16 16 16 16 16 16 ] 输出数据dstLocal [ 0 127 127 127 0 0 0 0 ] GT大于 输入数据src0Local[ 2 3 5 7 11 13 17 19 ... ] 输入数据src1Local[ 2 4 6 8 10 12 14 16 ... ] 逐元素比较结果 [ 0 0 0 0 1 1 1 1 ... ] 输出数据dstLocal [ 240(0b11110000) ... ] GE大于或等于 输入数据src0Local[ 2 3 5 7 11 13 17 19 ... ] 输入数据src1Local[ 2 4 6 8 10 12 14 16 ... ] 输出数据dstLocal [ 241(0b11110001) ... ] LE小于或等于 输入数据src0Local[ 2 3 5 7 11 13 17 19 ... ] 输入数据src1Local[ 2 4 6 8 10 12 14 16 ... ] 输出数据dstLocal [ 15(0b00001111) ... ] EQ等于 输入数据src0Local[ 2 3 5 7 11 13 17 19 ... ] 输入数据src1Local[ 2 4 6 8 10 12 14 16 ... ] 输出数据dstLocal [ 1(0b00000001) ... ] NE不等于 输入数据src0Local[ 2 3 5 7 11 13 17 19 ... ] 输入数据src1Local[ 2 4 6 8 10 12 14 16 ... ] 输出数据dstLocal [ 126(0b11111110) ... ]Tensor高维切分计算mask逐bit模式// masks数组控制每次迭代参与计算的元素两个uint64_t的值一共128bit每个bit可以控制一个元素为1则参与计算为0则不参与计算 // masks[0]可以控制前64个元素低bit位控制索引小的元素masks[1]同理可以控制后64个元素 // 例如对float类型数据每次迭代处理256B / sizeof(float) 64个元素因此只需要通过masks[0]即可进行控制 uint64_t masks[2] {858993459, 0}; // 858993459(0x33333333) // repeat: 1, dstBlkStride: 1, src0BlkStride: 1, src1BlkStride: 1, dstRepStride: 1, src0RepStride: 8, src1RepStride: 8 AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::LT, masks, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::GT, masks, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::LE, masks, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::GE, masks, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::EQ, masks, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::NE, masks, 1, { 1, 1, 1, 1, 8, 8 });结果示例如下LE小于等于 输入数据src0Local[ 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 11 11 11 11 11 11 11 11 13 13 13 13 13 13 13 13 17 17 17 17 17 17 17 17 19 19 19 19 19 19 19 19 ] 输入数据src1Local[ 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 12 12 12 12 12 12 12 12 14 14 14 14 14 14 14 14 16 16 16 16 16 16 16 16 ] 输入数据masks{ 858993459, 0 } 输出数据dstLocal [ 51 51 51 51 0 0 0 0 ]Tensor高维切分计算mask连续模式// mask控制每次迭代参与计算的连续元素个数 // 例如对float类型数据每次迭代处理256B / sizeof(float) 64个元素因此mask可取值1至64 uint64_t mask 28; // repeat: 1, dstBlkStride: 1, src0BlkStride: 1, src1BlkStride: 1, dstRepStride: 1, src0RepStride: 8, src1RepStride: 8 AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::LT, mask, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::GT, mask, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::LE, mask, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::GE, mask, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::EQ, mask, 1, { 1, 1, 1, 1, 8, 8 }); AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::NE, mask, 1, { 1, 1, 1, 1, 8, 8 });结果示例如下LE小于等于 输入数据src0Local[ 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 11 11 11 11 11 11 11 11 13 13 13 13 13 13 13 13 17 17 17 17 17 17 17 17 19 19 19 19 19 19 19 19 ] 输入数据src1Local[ 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 12 12 12 12 12 12 12 12 14 14 14 14 14 14 14 14 16 16 16 16 16 16 16 16 ] 输入数据mask28 输出数据dstLocal [ 127 127 127 16 0 0 0 0 ]创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考