Select (Flexible Scalar Position)
Applicability
|
Product |
Supported |
|---|---|
|
Atlas 350 Accelerator Card |
√ |
|
|
x |
|
|
x |
|
|
x |
|
|
x |
|
|
x |
|
|
x |
Function Usage
Selects elements from two source operands src0 and src1 based on bit values of selMask (the selection mask) to generate the destination operand dst. When a bit value of selMask is 1, the elements are selected from src0. When a bit value of selMask is 0, the elements are selected from src1.
For high-dimensional tensor sharding computation APIs, the selection results can be filtered again based on the mask parameter. Valid-bit elements update dst and invalid-bit elements preserve its original values. For example, src0 is [1,2,3,4,5,6,7,8], src1 is [9,10,11,12,13,14,15,16], selMask is [0,0,0,0,1,1,1,1], mask is [1,1,1,1,0,0,0,0], and the original dst is [-1,-2,-3,-4,-5,-6,-7,-8]. After bitwise selection based on selMask, dst_temp is [9,10,11,12,5,6,7,8]. The final dst filtered by mask is [9,10,11,12,-5,-6,-7,-8].
This function supports the following modes:
- Mode 0: Selects elements from two tensors based on selMask. The number of valid bits in selMask is restricted by the data type of the source operands. In each iteration, selection is performed based on the valid bits of selMask. The same valid bits of selMask are applied in each iteration.
- Mode 1: Elements are selected from either a tensor or a scalar based on selMask. The number of valid bits in selMask is not restricted. In multiple iterations, different parts of selMask are used consecutively in each iteration.
- Mode 2: Elements are selected from two tensors based on selMask. The number of valid bits in selMask is not restricted. In multiple iterations, different parts of selMask are used consecutively in each iteration.
For mode 1, this API is used with a flexible scalar position.
Prototype
- Computation of the first n pieces of data of a tensor
1 2
template <typename T0 = BinaryDefaultType, typename T1 = BinaryDefaultType, const BinaryConfig &config = DEFAULT_BINARY_CONFIG, typename T2, typename T3, typename T4> __aicore__ inline void Select(const T2& dst, const LocalTensor<T1>& selMask, const T3& src0, const T4& src1, SELMODE selMode, uint32_t count)
- High-dimensional tensor sharding computation
- Bitwise mask mode
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 Select(const T2& dst, const LocalTensor<T1>& selMask, const T3& src0, const T4& src1, SELMODE selMode, uint64_t mask[], uint8_t repeatTime, const BinaryRepeatParams& repeatParams)
- Contiguous mask mode
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 Select(const T2& dst, const LocalTensor<T1>& selMask, const T3& src0, const T4& src1, SELMODE selMode, uint64_t mask, uint8_t repeatTime, const BinaryRepeatParams& repeatParams)
- Bitwise mask mode
Parameters
|
Parameter |
Meaning |
||
|---|---|---|---|
|
T0 |
Data type of the source and destination operands. For an API with a flexible scalar position, this parameter is reserved for future use. If this parameter needs to be specified, pass the default value BinaryDefaultType. |
||
|
T1 |
Data type of selMask. |
||
|
isSetMask |
Reserved parameter. Retain the default value. To set mask outside the API, call an API that does not require the mask parameter. |
||
|
selMode |
For details, see selMode in Table 2. |
||
|
config |
Position of a single element. This parameter is of BinaryConfig type and takes effect when the scalar input is a LocalTensor single element. The default value is DEFAULT_BINARY_CONFIG, indicating that the scalar is after the tensor.
|
||
|
T2 |
LocalTensor data type. The data type is automatically inferred based on dst. Developers do not need to configure this parameter. Ensure that dst meets the data type requirements. |
||
|
T3 |
LocalTensor or scalar data type. The data type is automatically inferred based on src0. Developers do not need to configure this parameter. Ensure that src0 meets the data type requirements. |
||
|
T4 |
LocalTensor or scalar data type. The data type is automatically inferred based on src1. Developers do not need to configure this parameter. Ensure that src1 meets the data type requirements. |
|
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. 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, uint32_t, complex32, int64_t, uint64_t, and complex64. |
||
|
selMask |
Input |
Mask used for element selection. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The start address of LocalTensor must be 32-byte aligned. For the Atlas 350 Accelerator Card, the supported data types are uint8_t, uint16_t, uint32_t, and uint64_t. Each bit represents the selection of one element. When a bit in selMask is 1, the element is selected from src0. When the bit is 0, the element is selected from src1. When selMode is set to mode 0, in each iteration, selection is performed based on the valid bits of selMask. The selMask applied in every iteration remains identical, using the same valid value of selMask. If selMode is set to mode 1 or mode 2, different parts of selMask are used consecutively in each iteration.
|
||
|
src0 |
Input |
Source operand.
The data type must be the same as that of the destination operand. |
||
|
src1 |
Input |
Source operand.
The data type must be the same as that of the destination operand. |
||
|
selMode |
Input |
Instruction mode (selection mode). The values are as follows:
|
||
|
mask/mask[] |
Input |
mask controls the elements that participate in computation in each iteration.
|
||
|
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. |
Returns
None
Restrictions
- If this API is called when the scalar input is a LocalTensor single element, the source operand address cannot overlap with the destination operand address.
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- For the Atlas 350 Accelerator Card, int8_t, uint8_t, uint64_t, int64_t, complex32, and complex64 support only the APIs that compute the first n pieces of data in a tensor.
- Either the left or right source operand must be a vector. Currently, the left and right operands cannot be scalars at the same time.
- If the scalar input is a LocalTensor single element, idx must be a compile-time constant. If it is a variable, it must be declared as constexpr.
- In mode 1, the template parameter config needs to be set to prevent the API from matching other modes.
Examples
- Computation of the first n pieces of data of a tensor (mode 1)
1 2 3 4 5 6 7
// Flexible scalar position. src1Local[0] is used as a scalar. static constexpr AscendC::BinaryConfig config = { 1 }; AscendC::Select<BinaryDefaultType, uint8_t, config>(dstLocal, maskLocal, src0Local, src1Local[0], AscendC::SELMODE::VSEL_TENSOR_SCALAR_MODE, dataSize); // Flexible scalar position. src0Local[0] is used as a scalar. static constexpr AscendC::BinaryConfig config = { 0 }; AscendC::Select<BinaryDefaultType, uint8_t, config>(dstLocal, maskLocal, src0Local[0], src1Local, AscendC::SELMODE::VSEL_TENSOR_SCALAR_MODE, dataSize);