GatherMask
Applicability
Product |
Supported |
|---|---|
Atlas 350 Accelerator Card |
√ |
√ |
|
√ |
|
√ |
|
√ |
|
x |
|
x |
Function Usage
Selects elements from the source operand and writes them to the destination operand based on a gather mask (for data collection) that corresponds to either the binary of the built-in fixed mode or the binary of the user-defined input tensor values.
Prototype
- User-defined mode
1 2
template <typename T, typename U, GatherMaskMode mode = defaultGatherMaskMode> __aicore__ inline void GatherMask(const LocalTensor<T>& dst, const LocalTensor<T>& src0, const LocalTensor<U>& src1Pattern, const bool reduceMode, const uint32_t mask, const GatherMaskParams& gatherMaskParams, uint64_t& rsvdCnt)
- Built-in fixed mode
1 2
template <typename T, GatherMaskMode mode = defaultGatherMaskMode> __aicore__ inline void GatherMask(const LocalTensor<T>& dst, const LocalTensor<T>& src0, const uint8_t src1Pattern, const bool reduceMode, const uint32_t mask, const GatherMaskParams& gatherMaskParams, uint64_t& rsvdCnt)
Parameters
Parameter |
Meaning |
|---|---|
T |
Data type of the source operand src0 and destination operand dst. For the Atlas 350 Accelerator Card, the supported data types are int8_t, uint8_t, int16_t, uint16_t, half, bfloat16_t, float, int32_t, and uint32_t. For the For the For the For the |
U |
Data type of src1Pattern in user-defined mode. The supported data types are uint8_t/, uint16_t, and uint32_t.
|
mode |
Reserved. A default value is provided. You do not need to set this parameter. |
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. |
src0 |
Input |
Source operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The start address of LocalTensor must be 32-byte aligned. The data type must be the same as that of the destination operand. |
src1Pattern |
Input |
There are two modes of gather mask (for data collection): built-in fixed mode and custom mode. Elements are from the source operand and written to the destination operand based on either the binary of the built-in fixed mode or the binary of the custom input tensor values. 1: selected; 0: not selected.
|
reduceMode |
Input |
This parameter is used to select the mode of the mask parameter, and it is of the bool type. The options are as follows:
|
mask |
Input |
This parameter is used to control the elements that participate in computation in each iteration. Based on reduceMode, there are two mask modes:
|
gatherMaskParams |
Input |
Data structure that controls operand address strides. It is of GatherMaskParams type. For details, see ${INSTALL_DIR}/include/ascendc/basic_api/interface/kernel_struct_gather.h. Replace ${INSTALL_DIR} with the installation directory of CANN. For details, see Table 3. |
rsvdCnt |
Output |
Number of elements retained by the instruction, corresponding to the number of valid elements in dstLocal. The data type is uint64_t. |
Parameter |
Description |
|---|---|
src0BlockStride |
Address stride between different data blocks in an iteration of src0 (interval between start addresses). Unit: data block |
repeatTimes |
Number of repeats. |
src0RepeatStride |
Address stride between adjacent iterations of src0 (interval between start addresses). Unit: data block |
src1RepeatStride |
Address stride between adjacent iterations of src1 (interval between start addresses). Unit: data block |
Returns
None
Restrictions
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- For details about the constraints on operand address overlapping, see General Address Overlapping Restrictions.
- If the counter mode was used before this API is called, set the mode back to counter after this API is called. (The reason is that the counter mode switches to the normal mode after this API is called.)
Examples
- In the following example, a user-defined tensor is used:
1 2 3 4 5 6 7 8 9 10 11 12 13
uint32_t mask = 70; // Elements involved in computation in each iteration uint64_t rsvdCnt = 0; // Number of retained elements // src0Local: source operand // src1Local: tensor for storing the mask for data collection // dstLocal: destination operand // reduceMode = true; Counter mode is used. // The parameters in {} are as follows: // src0BlockStride = 1; Data is continuously read and written at an interval of one data block in a single iteration. // repeatTimes = 2; In counter mode, this parameter takes effect only for certain product models. // src0RepeatStride = 4; The interval between iterations of the source operand is four data blocks. // src1RepeatStride = 0; The interval between iterations of src1 is 0 data blocks, that is, the data is read from the original position. AscendC::GatherMask (dstLocal, src0Local, src1Local, true, mask, { 1, 2, 4, 0 }, rsvdCnt);
The following figure shows configuration method 1 in counter mode.
- mask = 70. 70 elements are computed in each repeat.
- repeatTimes = 2. There are two repeats in total.
- src0BlockStride = 1. There is no interval between data blocks in a single iteration of the source operand src0Local.
- src0RepeatStride = 4. The interval between adjacent iterations of the source operand src0Local is four data blocks. Therefore, the second repeat starts from the 33rd element.
- src1Pattern is set to the user-defined mode. src1RepeatStride = 0. The interval between adjacent iterations of src1Pattern is 0 data blocks. Therefore, the second repeat still starts from the start address of src1Pattern.
The following figure shows configuration method 2 in counter mode.
- mask = 70. A total of 70 elements are computed.
- repeatTimes does not take effect. The number of repeats is automatically inferred based on the source operand and mask. The data type of the source operand is uint32_t. Each iteration processes 256 bytes of data (64 elements). So, two repeats are required.
- src0BlockStride = 1. There is no interval between data blocks in a single iteration of the source operand src0Local.
- src0RepeatStride = 4. The interval between adjacent iterations of the source operand src0Local is four data blocks. Therefore, the second repeat starts from the 33rd element.
- src1Pattern is set to the user-defined mode. src1RepeatStride = 0. The interval between adjacent iterations of src1Pattern is 0 data blocks. Therefore, the second repeat still starts from the start address of src1Pattern.
- In the following example, the built-in fixed mode is used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
uint32_t mask = 0; // Elements involved in computation in each iteration. In normal mode, you are advised to set mask to 0. uint64_t rsvdCnt = 0; // Number of elements retained after filtering uint8_t src1Pattern = 2; // Built-in fixed mode // src0Local: source operand // src1Pattern: tensor for storing the mask for data collection // dstLocal: destination operand // reduceMode = false; Normal mode is used. // The parameters in {} are as follows: // src0BlockStride = 1; Data is continuously read and written at an interval of one data block in a single iteration. // repeatTimes = 1; One repeat // src0RepeatStride = 0; There is only one repeat, so set src0RepeatStride to 0. // src1RepeatStride = 0; There is only one iteration, so set src1RepeatStride to 0. AscendC::GatherMask(dstLocal, src0Local, src1Pattern, false, mask, { 1, 1, 0, 0 }, rsvdCnt);
Result example:
Input (src0Local): [1 2 3 ... 128] Input (src1Pattern): src1Pattern = 2; Output (dstLocal): [2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 undefined ..undefined] Output (rsvdCnt): 64

