Compares(灵活标量位置)
产品支持情况
|
产品 |
是否支持 |
|---|---|
|
Atlas 350 加速卡 |
√ |
|
|
x |
|
|
x |
|
|
x |
|
|
x |
|
|
x |
|
|
x |
功能说明
提供灵活标量位置的接口,支持标量在前和标量在后两种场景。其中标量输入支持配置LocalTensor单点元素,计算公式如下,idx表示LocalTensor单点元素的位置系数。

支持多种比较模式:
- LT:小于(less than)
- GT:大于(greater than)
- GE:大于或等于(greater than or equal to)
- EQ:等于(equal to)
- NE:不等于(not equal to)
- LE:小于或等于(less than or equal to)
函数原型
- tensor前n个数据计算
1 2
template <typename T0 = BinaryDefaultType, typename T1 = BinaryDefaultType, bool isSetMask = true, const BinaryConfig &config = DEFAULT_BINARY_CONFIG, typename T2, typename T3, typename T4> __aicore__ inline void Compares(const T2& dst, const T3& src0, const T4& src1, CMPMODE cmpMode, uint32_t count)
- tensor高维切分计算
- mask逐bit模式
1 2
template <typename T0 = BinaryDefaultType, typename T1 = BinaryDefaultType, bool isSetMask = true, const BinaryConfig &config = DEFAULT_BINARY_CONFIG, typename T2, typename T3, typename T4> __aicore__ inline void Compares(const T2& dst, const T3& src0, const T4& src1, CMPMODE cmpMode, const uint64_t mask[], uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
- mask连续模式
1 2
template <typename T0 = BinaryDefaultType, typename T1 = BinaryDefaultType, bool isSetMask = true, const BinaryConfig &config = DEFAULT_BINARY_CONFIG, typename T2, typename T3, typename T4> __aicore__ inline void Compares(const T2& dst, const T3& src0, const T4& src1, CMPMODE cmpMode, const uint64_t mask, uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
- mask逐bit模式
参数说明
|
参数名 |
描述 |
||
|---|---|---|---|
|
T0 |
对于固定标量位置接口,表示源操作数数据类型。 特别地,对于灵活标量位置接口,为预留参数,暂未启用,为后续的功能扩展做保留,需要指定时,传入默认值BinaryDefaultType即可。 |
||
|
T1 |
对于固定标量位置接口,表示目的操作数数据类型。 特别地,对于灵活标量位置接口,为预留参数,暂未启用,为后续的功能扩展做保留,需要指定时,传入默认值BinaryDefaultType即可。 |
||
|
isSetMask |
是否在接口内部设置mask。
|
||
|
config |
类型为BinaryConfig,当标量为LocalTensor单点元素类型时生效,用于指定单点元素操作数位置。默认值DEFAULT_BINARY_CONFIG,表示右操作数为标量。
|
||
|
T2 |
LocalTensor类型,根据输入参数dst自动推导相应的数据类型,开发者无需配置该参数,保证dst满足数据类型的约束即可。 |
||
|
T3 |
LocalTensor类型或标量类型,根据输入参数src0自动推导相应的数据类型,开发者无需配置该参数,保证src0满足数据类型的约束即可。 |
||
|
T4 |
LocalTensor类型或标量类型,根据输入参数src1自动推导相应的数据类型,开发者无需配置该参数,保证src1满足数据类型的约束即可。 |
|
参数名称 |
输入/输出 |
含义 |
|---|---|---|
|
dst |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 dst用于存储比较结果,将dst中uint8_t类型的数据按照bit位展开,由左至右依次表征对应位置的src0和src1的比较结果,如果比较后的结果为真,则对应比特位为1,否则为0。 Atlas 350 加速卡,支持的数据类型为:uint8_t |
|
src0/src1 |
输入 |
灵活标量位置接口中源操作数。
数据类型需要与目的操作数保持一致。 |
|
cmpMode |
输入 |
CMPMODE类型,表示比较模式,包括EQ,NE,GE,LE,GT,LT。
|
|
mask/mask[] |
输入 |
|
|
repeatTime |
输入 |
重复迭代次数。矢量计算单元,每次读取连续的256Bytes数据进行计算,为完成对输入数据的处理,必须通过多次迭代(repeat)才能完成所有数据的读取与计算。repeatTime表示迭代的次数。 |
|
repeatParams |
输入 |
控制操作数地址步长的参数。UnaryRepeatParams类型,包含操作数相邻迭代间相同DataBlock的地址步长,操作数同一迭代内不同DataBlock的地址步长等参数。 相邻迭代间的地址步长参数说明请参考repeatStride;同一迭代内DataBlock的地址步长参数说明请参考dataBlockStride。 |
|
count |
输入 |
参与计算的元素个数。设置count时,需要保证count个元素所占空间256字节对齐。 |
返回值说明
无
约束说明
- 操作数地址对齐要求请参见通用地址对齐约束。
- 调用灵活标量位置接口且源操作数为LocalTensor单点元素的场景,不支持源操作数和目的操作数地址重叠。
- dst按照小端顺序排序成二进制结果,对应src中相应位置的数据比较结果。
- 使用tensor前n个数据参与计算的接口,设置count时,需要保证count个元素所占空间256字节对齐。
- 针对Atlas 350 加速卡,int8_t/uint8_t/uint64_t/int64_t/double数据类型仅支持tensor前n个数据计算接口,double只支持CMPMODE::EQ。
- 左操作数及右操作数中,必须有一个为矢量;当前不支持左右操作数同时为标量。
- 本接口传入LocalTensor单点数据作为标量时,idx参数需要传入编译期已知的常量,传入变量时需要声明为constexpr。
调用示例
对于灵活标量位置接口,支持直接传入立即数或单点LocalTensor作为标量,并且支持标量在前和在后两种调用方式,调用示例如下;
- tensor前n个数据计算接口样例
1 2 3 4 5 6
// 标量在后,src1Local[0]作为标量 AscendC::Compares(dstLocal, src0Local, src1Local[0], AscendC::CMPMODE::LT, srcDataSize); // 标量在前,src0Local[0]作为标量 static constexpr AscendC::BinaryConfig config = { 0 }; AscendC::Compares<BinaryDefaultType, BinaryDefaultType, true, config>(dstLocal, src0Local[0], src1Local, AscendC::CMPMODE::LT, srcDataSize);
- tensor高维切分计算-mask连续模式
1 2 3 4 5 6 7 8 9 10 11 12
uint64_t mask = 256 / sizeof(float); // 256为每个迭代处理的字节数 int repeat = 4; AscendC::UnaryRepeatParams repeatParams = { 1, 1, 8, 8 }; // repeat = 4, 64 elements one repeat, 256 elements total // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat // dstRepStride, srcRepStride = 8, no gap between repeats // 标量在后,src1Local[0]作为标量 AscendC::Compares(dstLocal, src0Local, src1Local[0], AscendC::CMPMODE::LT, mask, repeat, repeatParams); // 标量在前,src0Local[0]作为标量 static constexpr AscendC::BinaryConfig config = { 0 }; AscendC::Compares<BinaryDefaultType, BinaryDefaultType, true, config>(dstLocal, src0Local[0], src1Local, AscendC::CMPMODE::LT, mask, repeat, repeatParams);
- tensor高维切分计算-mask逐bit模式
1 2 3 4 5 6 7 8 9 10 11 12
uint64_t mask[2] = { UINT64_MAX, 0}; int repeat = 4; AscendC::UnaryRepeatParams repeatParams = { 1, 1, 8, 8 }; // repeat = 4, 64 elements one repeat, 256 elements total // srcBlkStride, = 1, no gap between blocks in one repeat // dstRepStride, srcRepStride = 8, no gap between repeats // 标量在后,src1Local[0]作为标量 AscendC::Compares(dstLocal, src0Local, src1Local[0], AscendC::CMPMODE::LT, mask, repeat, repeatParams); // 标量在前,src0Local[0]作为标量 static constexpr AscendC::BinaryConfig config = { 0 }; AscendC::Compares<BinaryDefaultType, BinaryDefaultType, true, config>(dstLocal, src0Local[0], src1Local, AscendC::CMPMODE::LT, mask, repeat, repeatParams);