Compare
Applicability
|
Product |
Supported |
|---|---|
|
Atlas 350 Accelerator Card |
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
√ |
Function Usage
Performs element-wise comparison of two tensors. If the comparison result is true, the corresponding output bit is set to 1; otherwise, it is set to 0.
The following comparison modes are supported:
- 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
Prototype
- Computation of the entire tensor
1 2 3 4 5 6
dst = src0 < src1; dst = src0 > src1; dst = src0 <= src1; dst = src0 >= src1; dst = src0 == src1; dst = src0 != src1;
Currently, the
Atlas 200I/500 A2 inference product does not support operator overloading in the computation of an entire tensor. - Computation of the first n pieces of data of a tensor
1 2
template <typename T, typename U> __aicore__ inline void Compare(const LocalTensor<U>& dst, const LocalTensor<T>& src0, const LocalTensor<T>& src1, CMPMODE cmpMode, uint32_t count)
- High-dimensional tensor sharding computation
- Bitwise mask mode
1 2
template <typename T, typename U, bool isSetMask = true> __aicore__ inline void Compare(const LocalTensor<U>& dst, const LocalTensor<T>& src0, const LocalTensor<T>& src1, CMPMODE cmpMode, const uint64_t mask[], uint8_t repeatTime, const BinaryRepeatParams& repeatParams)
- Contiguous mask mode
1 2
template <typename T, typename U, bool isSetMask = true> __aicore__ inline void Compare(const LocalTensor<U>& dst, const LocalTensor<T>& src0, const LocalTensor<T>& src1, CMPMODE cmpMode, const uint64_t mask, uint8_t repeatTime, const BinaryRepeatParams& repeatParams)
- Bitwise mask mode
Parameters
|
Parameter |
Description |
|---|---|
|
T |
Data type of the source operands. For the Atlas 350 Accelerator Card, the supported data types are int8_t, uint8_t, int16_t, uint16_t, half, bfloat16_t, int32_t, uint32_t, float, int64_t, uint64_t, and double. For the For the For the For the For the |
|
U |
Data type of the destination operand. For the Atlas 350 Accelerator Card, the supported data types are int8_t and uint8_t. For the For the For the For the For the |
|
isSetMask |
Reserved. Retain the default value. |
|
Parameter |
Input/Output |
Meaning |
|---|---|---|
|
dst |
Output |
Destination operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The start address of LocalTensor must be 32-byte aligned. dst is used to store the comparison result. The uint8_t data in dst is expanded bit-wise, with each bit from left to right representing the comparison result of the corresponding positions in src0 and src1. If the comparison result is true, the corresponding bit is set to 1; otherwise, it is set to 0. |
|
src0/src1 |
Input |
Source operands. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The start address of LocalTensor must be 32-byte aligned. |
|
cmpMode |
Input |
Comparison mode, including EQ, NE, GE, LE, GT, and LT.
|
|
mask/mask[] |
Input |
mask controls the elements that participate in computation in each iteration. For the Atlas 350 Accelerator Card, setting this parameter is valid. For the For the For the For the For the
|
|
repeatTime |
Input |
Number of iteration repeats. The Vector Unit reads 256 bytes of contiguous data for computation each time. To read the complete data for processing, the unit needs to read the input data in multiple repeats. repeatTime indicates the number of iterations. For details about this parameter, see High-dimensional Sharding APIs. |
|
repeatParams |
Input |
Parameters that control the operand address strides. They are of the BinaryRepeatParams type, and contain such parameters as those that specify the address stride of the operand for the same data block between adjacent iterations and address stride of the operand between different data blocks in a single iteration. For details about the address stride of the operand between adjacent iterations, see repeatStride. For details about the address stride of the operand between different data blocks in a single iteration, see dataBlockStride. |
|
count |
Input |
Number of elements involved in the computation. When setting count, the total memory occupied by the count elements must be 256-byte aligned. |
Returns
None
Restrictions
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- dst is arranged in little-endian order as a binary result, with each bit corresponding to the comparison outcome of the respective position in src.
- When using operator overloading for entire-tensor computation, both src0 and src1 must be 256-byte aligned. For the APIs that compute the first n pieces of data in a tensor, when setting count, the total memory occupied by the count elements must be 256-byte aligned.
- For the Atlas 350 Accelerator Card, int8_t, uint8_t, uint64_t, int64_t, and double data types only support APIs that compute the first n pieces of data in a tensor and operator overloading for entire-tensor computation.
Examples
In this example, the source operands src0 and src1 each store 256 pieces of data of the float type. The example compares the data in src0 and src1 element by element. If the element in src0 is smaller than that in src1, the corresponding bit in the dst result is set to 1. Otherwise, the bit is set to 0. The dst result is stored in uint8_t format.
- Computation of the entire tensor
1 2 3 4 5 6
dstLocal = src0Local < src1Local; // LT dstLocal = src0Local > src1Local; // GT dstLocal = src0Local <= src1Local; // LE dstLocal = src0Local >= src1Local; // GE dstLocal = src0Local == src1Local; // EQ dstLocal = src0Local! = src1Local; // NE
- Computation of the first n pieces of data of a tensor
1 2 3 4 5 6 7
// srcDataSize: number of elements involved in the computation 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);
Result example:LT: Input (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 ] Input (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 ] Output (dstLocal): [ 0 127 127 127 0 0 0 0 ] GT: Input (src0Local): [ 2 3 5 7 11 13 17 19 ... ] Input (src1Local): [ 2 4 6 8 10 12 14 16 ... ] Element-wise comparison result: [ 0 0 0 0 1 1 1 1 ... ] Output (dstLocal): [ 240(0b11110000) ... ] GE: Input (src0Local): [ 2 3 5 7 11 13 17 19 ... ] Input (src1Local): [ 2 4 6 8 10 12 14 16 ... ] Output (dstLocal): [ 241(0b11110001) ... ] LE: Input (src0Local): [ 2 3 5 7 11 13 17 19 ... ] Input (src1Local): [ 2 4 6 8 10 12 14 16 ... ] Output (dstLocal): [ 15(0b00001111) ... ] EQ: Input (src0Local): [ 2 3 5 7 11 13 17 19 ... ] Input (src1Local): [ 2 4 6 8 10 12 14 16 ... ] Output (dstLocal): [ 1(0b00000001) ... ] NE: Input (src0Local): [ 2 3 5 7 11 13 17 19 ... ] Input (src1Local): [ 2 4 6 8 10 12 14 16 ... ] Output (dstLocal): [ 126(0b11111110) ... ] - High-dimensional tensor sharding computation (bitwise mask mode)
1 2 3 4 5 6 7 8 9 10 11
// The masks array controls which elements participate in each iteration. The two uint64_t values together provide 128 bits, with each bit controlling one element: a bit set to 1 means the element participates in the computation, while a bit set to 0 means it does not. // masks[0] controls the first 64 elements, with lower-order bits controlling elements with smaller indices. masks[1] controls the last 64 elements in a similar manner. // For example, for float data, each iteration processes 64 elements (256 bytes in total), so control can be achieved solely through 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 });
Result example:LE: Input (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 ] Input (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 ] Input (masks): { 858993459, 0 } Output (dstLocal): [ 51 51 51 51 0 0 0 0 ] - High-dimensional tensor sharding computation (contiguous mask mode)
1 2 3 4 5 6 7 8 9 10
// mask controls the number of contiguous elements involved in each iteration. // For example, for float data, each iteration processes 64 elements (256 bytes in total), so mask ranges from 1 to 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 });
Result example:LE: Input (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 ] Input (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 ] Input (mask): 28 Output (dstLocal): [ 127 127 127 16 0 0 0 0 ]