Select (Flexible Scalar Position)

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

x

Atlas A2 training product / Atlas A2 inference product

x

Atlas 200I/500 A2 inference product

x

Atlas inference product AI Core

x

Atlas inference product Vector Core

x

Atlas training product

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)
      

Parameters

Table 1 Template 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.

1
2
3
4
struct BinaryConfig {
    int8_t scalarTensorIndex = 1; // Position of the scalar when the scalar input is a LocalTensor single element. 0 indicates the scalar is before the tensor and 1 indicates the scalar is after the tensor.
};
constexpr BinaryConfig DEFAULT_BINARY_CONFIG = {1};

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.

Table 2 Parameters

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.

  • Mode 0: Elements are selected from two tensors based on selMask. Each iteration selects elements based on the fixed truncated bit-length of selMask, regardless of the number of iterations. For 8-bit source operands, the first 256 bits of selMask are valid; for 16-bit source operands, the first 128 bits of selMask are valid; for 32-bit source operands, the first 64 bits of selMask are valid; for 64-bit source operands, the first 32 bits of selMask are valid.
  • Mode 1: Elements are selected from either a tensor or a scalar based on selMask. Multiple iterations can be performed. If a bit value of selMask is 1, the element in the corresponding position in src0 is selected. If a bit value of selMask is 0, the scalar is selected. selMask is stored continuously. In each iteration: for 8-bit source operands, 256 bits of selMask are valid; for 16-bit source operands, 128 bits of selMask are valid; for 32-bit source operands, 64 bits of selMask are valid; for 64-bit source operands, 32 bits of selMask are valid.
  • Mode 2: Elements are selected from two tensors based on selMask. Multiple iterations can be performed. If a bit value of selMask is 1, the element in the corresponding position in src0 is selected. If a bit value of selMask is 0, the element in the corresponding position in src1 is selected. selMask is stored continuously. In each iteration: for 8-bit source operands, 256 bits of selMask are valid; for 16-bit source operands, 128 bits of selMask are valid; for 32-bit source operands, 64 bits of selMask are valid; for 64-bit source operands, 32 bits of selMask are valid.

src0

Input

Source operand.
  • If the scalar input is a LocalTensor, the source operands are a vector operand and a single element from the LocalTensor. 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.

  • If the scalar input is an immediate value:

    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.

The data type must be the same as that of the destination operand.

src1

Input

Source operand.
  • If the scalar input is a LocalTensor, the source operands are a vector operand and a single element from the LocalTensor. 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.

  • If the scalar input is an immediate value:

    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.

The data type must be the same as that of the destination operand.

selMode

Input

Instruction mode (selection mode). The values are as follows:

1
2
3
4
5
enum class SELMODE : uint8_t {
    VSEL_CMPMASK_SPR = 0, 
    VSEL_TENSOR_SCALAR_MODE,
    VSEL_TENSOR_TENSOR_MODE,
};
  • Mode 0: The value is VSEL_CMPMASK_SPR. Elements are selected 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: The value is VSEL_TENSOR_SCALAR_MODE. 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: The value is VSEL_TENSOR_TENSOR_MODE. 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.

mask/mask[]

Input

mask controls the elements that participate in computation in each iteration.

  • Bitwise mode: controls the elements that participate in computation by bit. If a bit is set to 1, the corresponding element participates in the computation. If a bit is set to 0, the corresponding element is masked from the computation.

    The mask value is an array. The array length and the value range of the array elements are related to the operand data type. When the operand is 16-bit, the array length is 2, with mask[0] and mask[1] each in the range [0, 264 – 1], and they cannot both be 0 at the same time. When the operand is 32-bit, the array length is 1, with mask[0] in the range (0, 264 – 1]. When the operand is 64-bit, the array length is 1, with mask[0] in the range (0, 232 – 1].

    For example, if mask = [0, 8] and 8 = 0b1000, only the fourth element participates in computation.

  • Contiguous mode: indicates the number of contiguous elements that participate in computation. The value range is related to the operand data type. The maximum number of elements that can be processed in each iteration varies according to the data type. When the operand is 16-bit, mask ∈ [1, 128]. When the operand is 32-bit, mask ∈ [1, 64]. When the operand is 64-bit, mask ∈ [1, 32].

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);