CastDequant
Applicability
|
Product |
Supported |
|---|---|
|
Atlas 350 Accelerator Card |
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
√ |
|
|
x |
|
|
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:

- 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:
- 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)
- Bitwise mask mode
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 For the For the 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 For the For the |
|
isSetMask |
Indicates whether to set mask inside the API.
|
|
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. |
|
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.
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. |
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.
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
1AscendC::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]
