CastDequant

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

Atlas 200I/500 A2 inference product

x

Atlas inference product AI Core

Atlas inference product Vector Core

x

Atlas training product

x

Function Usage

Quantizes the input and converts its precision. The formula varies depending on the data type.

  • For int16_t input, this API quantizes int16_t data and converts its precision to int8_t or uint8_t. Before using this API, call SetDeqScale to set quantization parameters such as scale, offset, and signMode.

    The template parameter isVecDeq controls whether to enable vector quantization.

    • If isVecDeq is set to false, this API quantizes the input and converts its precision based on the scale, offset, and signMode parameters set via SetDeqScale. The formula is as follows:

    • If isVecDeq is set to true, this API quantizes the input and converts its precision in a cyclic manner based on the 16 sets of quantization parameters (scale0-scale15, offset0-offset15, and signMode0-signMode15) stored in a 128-byte UB region set via SetDeqScale. The formula is as follows:

  • For int32_t input, this API quantizes int32_t data and converts its precision to half. Before using this API, call SetDeqScale to set the scale parameter.

Prototype

  • Computation of the first n pieces of data of a tensor
    1
    2
    template <typename T, typename U, bool isVecDeq = true, bool halfBlock = true>
    __aicore__ inline void CastDequant(const LocalTensor<T>& dst, const LocalTensor<U>& src, const uint32_t count)
    
  • High-dimensional tensor sharding computation
    • Bitwise mask mode
      1
      2
      template <typename T, typename U, bool isSetMask = true, bool isVecDeq = true, bool halfBlock = true>
      __aicore__ inline void CastDequant(const LocalTensor<T>& dst, const LocalTensor<U>& src, const uint64_t mask[], uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
      
    • Contiguous mask mode
      1
      2
      template <typename T, typename U, bool isSetMask = true, bool isVecDeq = true, bool halfBlock = true>
      __aicore__ inline void CastDequant(const LocalTensor<T>& dst, const LocalTensor<U>& src, const int32_t mask, uint8_t repeatTime, const UnaryRepeatParams& repeatParams)
      

Parameters

Table 1 Template parameters

Parameter

Description

T

Data type of the output tensor.

For the Atlas 350 Accelerator Card, the supported data types are int8_t, uint8_t, and half.

For the Atlas A3 training product / Atlas A3 inference product , the supported data types are int8_t, uint8_t, and half.

For the Atlas A2 training product / Atlas A2 inference product , the supported data types are int8_t, uint8_t, and half.

For the Atlas inference product AI Core, the supported data types are int8_t and uint8_t.

This parameter is used together with the input parameter signMode of the SetDeqScale API. When signMode is set to true, the output data type is int8_t. When signMode is set to false, the output data type is uint8_t.

U

Data type of the input tensor.

For the Atlas 350 Accelerator Card, the supported data types are int16_t and int32_t.

For the Atlas A3 training product / Atlas A3 inference product , the supported data types are int16_t and int32_t.

For the Atlas A2 training product / Atlas A2 inference product , the supported data types are int16_t and int32_t.

For the Atlas inference product AI Core, the supported data type is int16_t.

isSetMask

Indicates whether to set mask inside the API.

  • true: sets mask inside the API.
  • false: sets mask outside the API. Developers need to use the SetVectorMask API to set the mask value. In this mode, the mask value in the input parameter of this API must be set to the placeholder MASK_PLACEHOLDER.

isVecDeq

Whether to enable vector quantization. This parameter is used together with SetDeqScale(const LocalTensor<T>& src). If a tensor is passed to SetDeqScale, isVecDeq must be set to true.

halfBlock

Whether output elements are stored in the upper or lower half of a block. This parameter is used when int16_t data is quantized and converted to int8_t or uint8_t data. halfBlock=true indicates output elements are stored in the lower half of a block while halfBlock=false indicates output elements are stored in the upper half of a block, as shown in Figure 1.

Table 2 API parameters

Parameter

Input/Output

Description

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.

src

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.

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].

When the number of bits of the source operand is different from that of the destination operand, the data type with more bytes is used for the computation. For example, if the source operand is of int16_t type and the destination operand is of int8_t type, int16_t is used for mask computation.

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 UnaryRepeatParams type (see UnaryRepeatParams), and contain parameters such as the address stride of the operand for the same Data Block between adjacent iterations and the 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.

Figure 1 halfBlock

Returns

None

Restrictions

Examples

  • Example of high-dimensional sharding computation API - contiguous mask mode
    1
    2
    3
    4
    5
    int32_t mask = 256 / sizeof(int16_t);
    // repeatTime = 2, 128 elements one repeat, 256 elements total
    // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat
    // dstRepStride, srcRepStride = 8, no gap between repeats
    AscendC::CastDequant<uint8_t, int16_t, true, true, true>(dstLocal, srcLocal, mask, 2, { 1, 1, 8, 8 });
    
  • Example of high-dimensional sharding computation API - bitwise mask mode
    1
    2
    3
    4
    5
    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    // repeatTime = 2, 128 elements one repeat, 256 elements total
    // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat
    // dstRepStride, srcRepStride = 8, no gap between repeats
    AscendC::CastDequant<uint8_t, int16_t, true, true, true>(dstLocal, srcLocal, mask, 2, { 1, 1, 8, 8 });
    
  • Example of first n pieces of tensor data computation API
    1
    AscendC::CastDequant<uint8_t, int16_t, true, true>(dstLocal, srcLocal, 256);
    

Result example:

Input (srcLocal):
[20 53 26 12 36  6 20 93 66 30 56 99 59 92  7 37 22 47 98 10 85 29 14 46
 17 34 45 17 25 45 82 17 66 94 68 23 67  8 89  8 92  6 10 80 87 20  9 81
 70 62 11 58 38 83 32 14 38 47 41 63 94 26 96 89 88 35 86 55 60 82 15 65
 92 67 83 23 63 25 85 93 50 91 75 60 80 10 55 20 71 14 67 23 31 63  7 93
 69 45 61 23 43 86 11 81 81 36 76 58 53 25 23 51 59 78 82 10 39 40 24 50
 68 49 79 40  4 53 22 38 45 17 29 54  9 66 98 47 12 47 47 20 98  0 59 77
  1 21 39 70 66 20 68  8 77 77 54  0  3 33 37 37 48 60 83 88 27 70 31 49
 75 21 59  3 99 84 92 84 14 44 26 56 72 56 37 52 39 11  2 59 59 65 71 64
 10 65 62 48 42 79 69 69 27 99  8 38 36 77 34 34 60 50 52 50 41 31 95 68
 27 16 42 64 19 47  0 10 36 36 33 62 98 64 32 81 49 53 27 70 35  9 63  7
 10 89  3 39 94 23 89 16 23 60 71 42 46 58 65 90]
Output (dstLocal):
[ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 20 53 26 12 36  6 20 93
 66 30 56 99 59 92  7 37  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 22 47 98 10 85 29 14 46 17 34 45 17 25 45 82 17  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 66 94 68 23 67  8 89  8 92  6 10 80 87 20  9 81
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 70 62 11 58 38 83 32 14
 38 47 41 63 94 26 96 89  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 88 35 86 55 60 82 15 65 92 67 83 23 63 25 85 93  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 50 91 75 60 80 10 55 20 71 14 67 23 31 63  7 93
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 69 45 61 23 43 86 11 81
 81 36 76 58 53 25 23 51  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 59 78 82 10 39 40 24 50 68 49 79 40  4 53 22 38  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 45 17 29 54  9 66 98 47 12 47 47 20 98  0 59 77
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1 21 39 70 66 20 68  8
 77 77 54  0  3 33 37 37  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 48 60 83 88 27 70 31 49 75 21 59  3 99 84 92 84  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 14 44 26 56 72 56 37 52 39 11  2 59 59 65 71 64
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 10 65 62 48 42 79 69 69
 27 99  8 38 36 77 34 34  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 60 50 52 50 41 31 95 68 27 16 42 64 19 47  0 10  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 36 36 33 62 98 64 32 81 49 53 27 70 35  9 63  7
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 10 89  3 39 94 23 89 16
 23 60 71 42 46 58 65 90]