Dequantize
Applicability
Product |
Supported |
|---|---|
Atlas 350 Accelerator Card |
√ |
x |
|
x |
|
x |
|
x |
|
x |
|
x |
Function Usage
Performs dequantization by element. For example, dequantize the int32_t data type to the half or float data type. This API supports input of data no more than two dimensions.
Dequantize has similar function to AscendDequant and is recommended because it has a unified form in different quantization scenarios.
- PER_TENSOR scenario (dequantization by tensor): The shape of scale and offset is [1].

- PER_CHANNEL scenario (dequantization by channel): The shape of srcTensor is [m, n]. Each channel dimension corresponds to a quantization parameter. The shape of scale and offset is [1, n].

- PER_TOKEN scenario (dequantization by token): The elements in each group of tokens (m groups of tokens in the n direction) of srcTensor share a quantization parameter. When the shape of srcTensor is [m, n], the shape of scale and offset is [m, 1].

- PER_GROUP scenario (dequantization by group): The compute direction of groups is defined as k. In the k direction, each groupSize elements of srcTensor shares a group of scale and offset. When the shape of srcTensor is [m, n], if kDim is 0, k is in the m direction, and the shape of scale and offset is [(m + groupSize – 1)/groupSize, n]; if kDim is 1, k is in the n direction, and the shape of scale and offset is [m, (n + groupSize – 1)/groupSize].
- k is the m direction. That means, the i axis in the formula is the compute direction of group (kDim = 0):

- k is the n direction. That means, the j axis in the formula is the compute direction of group (kDim = 1):

- k is the m direction. That means, the i axis in the formula is the compute direction of group (kDim = 0):
Prototype
- Pass the temporary space through the sharedTmpBuffer input parameter.
1 2
template <const DequantizeConfig& config, typename DstT, typename SrcT, typename ScaleT, typename OffsetT> __aicore__ inline void Dequantize(const LocalTensor<DstT>& dstTensor, const LocalTensor<SrcT>& srcTensor, const ScaleT& scale, const OffsetT& offset, const LocalTensor<uint8_t>& sharedTmpBuffer, const DequantizeParams& params)
- Allocate the temporary space through the API framework.
1 2
template <const DequantizeConfig& config, typename DstT, typename SrcT, typename ScaleT, typename OffsetT> __aicore__ inline void Dequantize(const LocalTensor<DstT>& dstTensor, const LocalTensor<SrcT>& srcTensor, const ScaleT& scale, const OffsetT& offset, const DequantizeParams& params)
Due to the complex mathematical computation involved in the internal implementation of this API, extra temporary space is required to store intermediate variables generated during computation. The temporary space can be allocated through the API framework or passed by developers through the sharedTmpBuffer input parameter.
- When the API framework is used for temporary space allocation, developers do not need to allocate the space, but must reserve the required size for the temporary space.
- When the sharedTmpBuffer input parameter is used for passing the temporary space, the tensor serves as the temporary space. In this case, the API framework is not required for temporary space allocation. This enables developers to manage the sharedTmpBuffer space and reuse the buffer after calling the API, so that the buffer is not repeatedly allocated or deallocated, improving the flexibility and buffer utilization.
If the API framework is used, developers must reserve the temporary space. If sharedTmpBuffer is used, developers must allocate space for sharedTmpBuffer. To obtain the size of the temporary space (BufferSize) to be reserved, use the GetDequantizeMaxMinTmpSize API provided in GetDequantizeMaxMinTmpSize.
Parameters
Parameter |
Description |
||||
|---|---|---|---|---|---|
config |
Dequantization configuration. The type is DequantizeConfig. The definition is as follows:
|
||||
DstT |
Data type of the destination operand. The data type is automatically derived from the input parameter dstTensor in the API. You do not need to set this parameter. Ensure that dstTensor meets the data type combinations supported by the input and output in Table 3. |
||||
SrcT |
Data type of the source operand. The data type is automatically derived from the input parameter srcTensor in the API. You do not need to set this parameter. Ensure that srcTensor meets the data type combinations supported by the input and output in Table 3. |
||||
ScaleT |
Data type of scale. The data type is automatically derived from the input parameter scale in the API. You do not need to set this parameter. ScaleT can be a scalar or LocalTensor. Note:
|
||||
OffsetT |
Data type of offset. The data type is automatically derived from the input parameter offset in the API. You do not need to set this parameter. OffsetT can be a scalar or LocalTensor. Note:
|
Parameter |
Input/Output |
Description |
||
|---|---|---|---|---|
dstTensor |
Output |
Destination operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. |
||
srcTensor |
Input |
Source operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. Assume that the shape of srcTensor is [m, n]. The number of bytes occupied by each row of data (n pieces of input data) must be 32-byte aligned. |
||
scale |
Input |
Scaling factor when the input data is dequantized. |
||
offset |
Input |
Offset when the input data is dequantized. This parameter is reserved and can be set to 0 or an empty tensor. |
||
sharedTmpBuffer |
Input |
Temporary buffer. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. For details about how to obtain the temporary space size (BufferSize), see GetDequantizeMaxMinTmpSize. |
||
params |
Input |
Dequantization API parameter, which is of the DequantizeParams type. The definition is as follows:
|
Quantization Policy |
dstTensor |
srcTensor |
scale/offset |
|---|---|---|---|
PER_TENSOR |
bfloat16_t |
int32_t |
bfloat16_t |
bfloat16_t |
int32_t |
float |
|
float |
int32_t |
bfloat16_t |
|
float |
int32_t |
float |
|
PER_CHANNEL |
half |
int32_t |
uint64_t Note: When the data type of scale is uint64_t, the lower 32 bits of the data are of the float data type, and the upper 32 bits are not used by the API. |
float |
int32_t |
float |
|
float |
int32_t |
bfloat16_t |
|
bfloat16_t |
int32_t |
bfloat16_t |
|
bfloat16_t |
int32_t |
float |
|
PER_TOKEN/PER_GROUP |
half |
int32_t |
half |
bfloat16_t |
int32_t |
bfloat16_t |
|
float |
int32_t |
float |
|
half |
int32_t |
float |
|
bfloat16_t |
int32_t |
float |
|
half |
float |
half |
|
bfloat16_t |
float |
bfloat16_t |
|
float |
float |
float |
|
half |
float |
float |
|
bfloat16_t |
float |
float |
Returns
None
Constraints
- The source operand address must not overlap the destination operand address.
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- The data volume in the continuous computation direction (n direction) must be 32-byte aligned.
Examples
- PER_CHANNEL, PER_TOKEN, and PER_GROUP modes
1 2 3 4 5 6 7 8 9 10 11 12
constexpr static DequantizePolicy tokenPolicy = DequantizePolicy::PER_TOKEN; constexpr static DequantizePolicy channelPolicy = DequantizePolicy::PER_CHANNEL; constexpr static DequantizePolicy groupPolicy = DequantizePolicy::PER_GROUP; // The PER_CHANNEL mode is used as an example. Disable the offset function. kDim is valid only in the PER_GROUP scenario, indicating that the compute direction of groups is n. constexpr static DequantizeConfig config = {channelPolicy, false, 1}; DequantizeParams params; // m and n are external input parameters, indicating the number of elements in the m and n directions involved in srcLocal. params.m = m; params.n = n; params.groupSize = n; // This is valid only in the PER_GROUP scenario. It indicates that all elements in the n direction share a group of scale and offset. // srcLocal is a LocalTensor of the int32_t type, and dstLocal, scaleLocal, and offsetLocal are a LocalTensor of the float type. Dequantize<config>(dstLocal, srcLocal, scaleLocal, offsetLocal, params); // offsetLocal is a reserved parameter and can be set to an empty tensor.
- PER_TENSOR mode
1 2 3 4 5 6 7 8 9 10
constexpr static DequantizePolicy tensorPolicy = DequantizePolicy::PER_TENSOR; // Disable the offset function. constexpr static DequantizeConfig config = {tensorPolicy, false, -1}; DequantizeParams params; // m and n are external input parameters, indicating the number of elements in the m and n directions involved in srcLocal. params.m = m; params.n = n; params.groupSize = 0; // This is valid only in the PER_GROUP scenario. // srcLocal is a LocalTensor of the int32_t type, dstLocal is a LocalTensor of the float type, and scale and offset are scalars of the float type. Dequantize<config>(dstLocal, srcLocal, scale, offset, params); // offset is a reserved parameter and can be set to 0.
Result example:
Input (srcLocal): [-4, 2, -2, -3, -1, -4, 1, 3, 4, 1, -2, 0, ... 1] Input (scale vector): [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... 1] Input (scale scalar): [1] Output (dstLocal). In this case, dstLocal = srcLocal: [-4, 2, -2, -3, -1, -4, 1, 3, 4, 1, -2, 0, ... 1]