Compare
Function Usage
Compares the sizes of two tensors element by element. If the comparison result is true, the corresponding bit of the output result is 1. Otherwise, the bit is 0.
The following comparison modes are supported:
- LT: lower than
- GT: greater than
- GE: greater than or equal to
- EQ: equal
- NE: not equal to
- LE: lower than or equal to
Prototype
- Computation of the entire tensor
1 2 3 4 5 6
dstLocal = src0Local < src1Local; dstLocal = src0Local > src1Local; dstLocal = src0Local <= src1Local; dstLocal = src0Local >= src1Local; dstLocal = src0Local == src1Local; dstLocal = src0Local != src1Local;
- 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>& dstLocal, const LocalTensor<T>& src0Local, const LocalTensor<T>& src1Local, CMPMODE cmpMode, uint32_t calCount)
- 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>& dstLocal, const LocalTensor<T>& src0Local, const LocalTensor<T>& src1Local, CMPMODE cmpMode, const uint64_t mask[], uint8_t repeatTimes, const BinaryRepeatParams& repeatParams)
- Contiguous mask mode
1 2
template <typename T, typename U, bool isSetMask = true> __aicore__ inline void Compare(const LocalTensor<U>& dstLocal, const LocalTensor<T>& src0Local, const LocalTensor<T>& src1Local, CMPMODE cmpMode, const uint64_t mask, uint8_t repeatTimes, const BinaryRepeatParams& repeatParams)
- Bitwise mask mode
Parameters
|
Parameter |
Description |
|---|---|
|
T |
Operand data type. |
|
isSetMask |
Reserved. Retain the default value. |
|
Parameter |
Input/Output |
Meaning |
|---|---|---|
|
dstLocal |
Output |
Destination operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. The start address of the LocalTensor must be 32-byte aligned. dstLocal is used to store the comparison result. The bits of uint8_t data in dstLocal from left to right indicate the comparison result of src0 and src1. If the comparison result is true, the corresponding bit value is 1. Otherwise, the value is 0. For the |
|
src0Local and src1Local |
Input |
Source operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. The start address of the LocalTensor must be 32-byte aligned. For the |
|
cmpMode |
Input |
Comparison mode, including EQ, NE, GE, LE, GT, and LT.
|
|
mask |
Input |
mask is used to control the elements that participate in computation in each iteration. For the |
|
repeatTimes |
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. repeatTimes indicates the number of iterations. |
|
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. |
|
calCount |
Input |
Number of elements of the input data. When setting calCount, ensure that the space occupied by calCount elements is 256-byte aligned. |
Returns
None
Availability
Precautions
- For details about the alignment requirements of the operand address offset, see General Restrictions.
- dstLocal stores binary results in little-endian order, corresponding to the comparison result of data in the corresponding position in src.
- If the operator reloading function involves the computation of the entire tensor, src0Local and src1Local must be 256-byte aligned. If the API for computing the first n pieces of tensor data is used, ensure that the space occupied by calCount elements is 256-byte aligned.
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.
This example shows only part of the code used in the computation process (Compute). To run the sample code, copy the code snippet and replace some code of the Compute function in Template Sample.
- Computation of the entire tensor
1dstLocal = src0Local < src1Local;
- Computation of the first n pieces of data of a tensor
1AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::LT, srcDataSize);
Template Sample
#include "kernel_operator.h"
class KernelCmp {
public:
__aicore__ inline KernelCmp() {}
__aicore__ inline void Init(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm, __gm__ uint8_t* dstGm)
{
src0Global.SetGlobalBuffer((__gm__ float*)src0Gm);
src1Global.SetGlobalBuffer((__gm__ float*)src1Gm);
dstGlobal.SetGlobalBuffer((__gm__ uint8_t*)dstGm);
pipe.InitBuffer(inQueueSrc0, 1, srcDataSize * sizeof(float));
pipe.InitBuffer(inQueueSrc1, 1, srcDataSize * sizeof(float));
pipe.InitBuffer(outQueueDst, 1, dstDataSize * sizeof(uint8_t));
}
__aicore__ inline void Process()
{
CopyIn();
Compute();
CopyOut();
}
private:
__aicore__ inline void CopyIn()
{
AscendC::LocalTensor<float> src0Local = inQueueSrc0.AllocTensor<float>();
AscendC::LocalTensor<float> src1Local = inQueueSrc1.AllocTensor<float>();
AscendC::DataCopy(src0Local, src0Global, srcDataSize);
AscendC::DataCopy(src1Local, src1Global, srcDataSize);
inQueueSrc0.EnQue(src0Local);
inQueueSrc1.EnQue(src1Local);
}
__aicore__ inline void Compute()
{
AscendC::LocalTensor<float> src0Local = inQueueSrc0.DeQue<float>();
AscendC::LocalTensor<float> src1Local = inQueueSrc1.DeQue<float>();
AscendC::LocalTensor<uint8_t> dstLocal = outQueueDst.AllocTensor<uint8_t>();
// Replace it with the actual interface Compare.
AscendC::Compare(dstLocal, src0Local, src1Local, AscendC::CMPMODE::LT, srcDataSize);
outQueueDst.EnQue<uint8_t>(dstLocal);
inQueueSrc0.FreeTensor(src0Local);
inQueueSrc1.FreeTensor(src1Local);
}
__aicore__ inline void CopyOut()
{
AscendC::LocalTensor<uint8_t> dstLocal = outQueueDst.DeQue<uint8_t>();
AscendC::DataCopy(dstGlobal, dstLocal, dstDataSize);
outQueueDst.FreeTensor(dstLocal);
}
private:
AscendC::TPipe pipe;
AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueSrc0, inQueueSrc1;
AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueueDst;
AscendC::GlobalTensor<float> src0Global, src1Global;
AscendC::GlobalTensor<uint8_t> dstGlobal;
uint32_t srcDataSize = 256;
uint32_t dstDataSize = srcDataSize / AscendC::AscendCUtils::GetBitSize(sizeof(uint8_t));
};
extern "C" __global__ __aicore__ void main_cpu_cmp_sel_demo(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm, __gm__ uint8_t* dstGm)
{
KernelCmp op;
op.Init(src0Gm, src1Gm, dstGm);
op.Process();
}