Ands

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

Performs bitwise AND between a scalar and each element of a vector. The scalar can be before or after the vector. The scalar input can be a single element from a LocalTensor. idx indicates the position index of a LocalTensor single element.

Prototype

  • Computation of the first n pieces of data of a tensor
    1
    2
    template <typename T = BinaryDefaultType, bool isSetMask = true, const BinaryConfig& config = DEFAULT_BINARY_CONFIG, typename U, typename S, typename V>
    __aicore__ inline void Ands(const U& dst, const S& src0, const V& src1, const int32_t& count)
    
  • High-dimensional tensor sharding computation
    • Bitwise mask mode
      1
      2
      template <typename T = BinaryDefaultType, bool isSetMask = true, const BinaryConfig& config = DEFAULT_BINARY_CONFIG, typename U, typename S, typename V>
      __aicore__ inline void Ands(const U& dst, const S& src0, const V& src1, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
      
    • Contiguous mask mode
      1
      2
      template <typename T = BinaryDefaultType, bool isSetMask = true, const BinaryConfig& config = DEFAULT_BINARY_CONFIG, typename U, typename S, typename V>
      __aicore__ inline void Ands(const U& dst, const S& src0, const V& src1, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
      

Parameters

Table 1 Template parameters

Parameter

Description

T

This parameter is reserved for future use. If this parameter needs to be specified, pass the default value BinaryDefaultType.

isSetMask

Whether to set the mask mode and mask value inside the API.

  • true: indicates that the settings are performed inside the API.

    The APIs for high-dimensional tensor sharding computation and for computing the first n pieces of data in a tensor use the Normal or Counter mode of the mask. Generally, retain the default value of isSetMask, indicating that the mask mode and mask value are set in the API based on the mask and count parameters passed by developers.

  • false: indicates that the settings are performed outside the API.
    • For high-dimensional tensor sharding computation APIs, in some scenarios that require high performance, developers need to use SetMaskNorm or SetMaskCount to set the mask mode and use the SetVectorMask API to set the mask value. The mask value in the input parameter of this API must be set to MASK_PLACEHOLDER.
    • For the APIs that compute the first n pieces of data in a tensor, in some scenarios that require high performance, developers need to use SetMaskCount to set the mask mode to Counter and use the SetVectorMask API to set the mask value. The count value in the input parameter of this API does not take effect. You are advised to set it to 1.

For the models below, isSetMask is invalid for the APIs that compute the first n pieces of data in a tensor. Retain the default value.

  • Atlas 350 Accelerator Card

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

U

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.

S

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.

V

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 int16_t, uint16_t, int64_t, and uint64_t.

src0/src1

Input

Source operands.

  • 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 int16_t, uint16_t, int64_t, and uint64_t.

  • If the scalar input is an immediate value:

    For the Atlas 350 Accelerator Card, the supported data types are int16_t, uint16_t, int64_t, and uint64_t.

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

count

Input

Number of elements involved in the computation.

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

Structure for controlling element-wise operations. For details, see UnaryRepeatParams.

Returns

None

Restrictions

  • To save memory space when using high-dimensional tensor sharding computation APIs, developers can define a tensor shared by the source and destination operands (through address overlapping). The restrictions are as follows:
    • For a single repeat (repeatTime = 1), the source operand must completely overlap with the destination operand. Partial overlapping is not supported.
    • For multiple repeats (repeatTime > 1), address overlapping is not supported when there is a dependency between the source and destination operands (for example, when the destination operand of the Nth iteration is the source operand of the (N+1)th iteration).
    • If the scalar input is a LocalTensor single element, the source operand address cannot overlap with the destination operand address.
  • For the Atlas 350 Accelerator Card, uint64_t and int64_t 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.

Examples

  • Example of high-dimensional tensor sharding computation (contiguous mask mode)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    // dstLocal: output tensor
    // src0Local: input tensor
    // src1Local: input tensor
    
    uint64_t mask = 128;
    // repeatTime = 4. 128 elements are processed in a single iteration. To compute 512 elements, four iterations are required.
    // dstBlkStride, srcBlkStride = 1. The interval between src0 data addresses involved in computation in each iteration is one data block, indicating that data is continuously read and written in a single iteration.
    // dstRepStride, srcRepStride = 8. The interval between addresses of adjacent iterations is eight data blocks, indicating that data is continuously read and written between adjacent iterations.
    // The scalar is after the tensor.
    AscendC::Ands(dstLocal, src0Local, src1Local[0], mask, 4, { 1, 1, 8, 8 });
    
    // The scalar is before the tensor.
    static constexpr AscendC::BinaryConfig config = { 0 };
    AscendC::Ands<BinaryDefaultType, true, config>(dstLocal, src0Local[0], src1Local, mask, 4, {1, 1, 8, 8});
    
  • Example of high-dimensional tensor sharding computation (bitwise mask mode)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    // dstLocal: output tensor
    // src0Local: input tensor
    // src1Local: input tensor
    
    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    // repeatTime = 4. 128 elements are processed in a single iteration. To compute 512 elements, four iterations are required.
    // dstBlkStride, srcBlkStride = 1. The interval between src0 data addresses involved in computation in each iteration is one data block, indicating that data is continuously read and written in a single iteration.
    // dstRepStride, srcRepStride = 8. The interval between addresses of adjacent iterations is eight data blocks, indicating that data is continuously read and written between adjacent iterations.
    // The scalar is after the tensor.
    AscendC::Ands(dstLocal, src0Local, src1Local[0], mask, 4, {1, 1, 8, 8});
    
    // The scalar is before the tensor.
    static constexpr AscendC::BinaryConfig config = { 0 };
    AscendC::Ands<BinaryDefaultType, true, config>(dstLocal, src0Local[0], src1Local, mask, 4, {1, 1, 8, 8});
    
  • Example of computing the first n pieces of data of a tensor
    1
    2
    3
    4
    5
    6
    // The scalar is after the tensor.
    AscendC::Ands(dstLocal, src0Local, src1Local[0], 512);
    
    // The scalar is before the tensor.
    static constexpr AscendC::BinaryConfig config = { 0 };
    AscendC::Ands<BinaryDefaultType, true, config>(dstLocal, src0Local[0], src1Local, 512);
    
Result example:
// The scalar src1Local[0] is after the tensor.
Input (src0Local): [1 2 3 ... 512]
Input (src1Local): [0]
Output (dstLocal): [0 0 0 ... 0]

// The scalar src0Local[0] is before the tensor.
Input (src0Local): [0]
Input (src1Local): [1 2 3 ... 512]
Output (dstLocal): [0 0 0 ... 0]