flat_quant
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description:
Performs two Kronecker product small-matrix multiplications on matrix x in sequence, and then quantizes the multiplication result. The underlying implementation calls
aclnnFlatQuantV3.Formula:
$$ \text{intermediate} = x \cdot \text{kronecker\_p2} $$$$ \text{result} = \text{kronecker\_p1} \cdot \text{intermediate} $$$$ \text{scale} = \text{compute\_quant\_scale}(\text{result}, \text{clip\_ratio}, \text{dst\_type\_max}) $$$$ \text{out} = \text{quantize}(\text{result}, \text{scale}) $$
Prototype
cann_ops_nn.flat_quant(x, kronecker_p1, kronecker_p2, clip_ratio=1.0, dst_dtype=torch.quint4x2, dst_type_max=0.0, group_list=None, group_list_type=0)
-> (Tensor out, Tensor quant_scale)Parameters
| Parameter | Type | Optional/Required | Description | Data Type | Dimension (shape) |
|---|---|---|---|---|---|
| x | Tensor | Required | Input matrix, the data tensor for Kronecker product multiplication and quantization. N2 must be a multiple of 8 (when dst_dtype = torch.quint4x2) or an even number (when dst_dtype = torch_npu.float4_e2m1fn_x2). | float16, bfloat16 | (M, N1, N2) |
| kronecker_p1 | Tensor | Required | The first Kronecker product matrix. Its N1 dimension is consistent with the N1 dimension of x. | float16, bfloat16 | (N1, N1) |
| kronecker_p2 | Tensor | Required | The second Kronecker product matrix. Its N2 dimension must be consistent with the N2 dimension of x. | float16, bfloat16 | (N2, N2) |
| clip_ratio | float | Optional | Quantization clipping ratio, used to control the clipping ratio in quantization. Range: (0, 1]. Default value: 1.0. | - | - |
| dst_dtype | int | Optional | Specifies the output data type. Supports torch.quint4x2 (default value) and torch_npu.float4_e2m1fn_x2. | - | - |
| dst_type_max | float | Optional | Maximum value of the target data type. The input value must be 0 or a value within [6, 12]. Default value: 0.0. | - | - |
| group_list | Tensor | Optional | Group list, used for grouped quantization. Supports 1D or 2D input. Default value: None. | int64 | (G,) or (G, 2) |
| group_list_type | int | Optional | Grouping mode of the group_list input. Value range: 0-2. Default value: 0. | - | - |
Return Values
| Output | Output Type | Optional/Required | Description | Data Type | Dimension (shape) |
|---|---|---|---|---|---|
| out | Tensor | Required | Quantized output. When dst_dtype = torch.quint4x2, the data type is int32 and the shape is (M, N1, N2//8). When dst_dtype = torch_npu.float4_e2m1fn_x2, the data type is uint8 and the shape is (M, N1 * N2//2). | int32, uint8 | (M, N1, N2//8) or (M, N1 * N2//2) |
| quant_scale | Tensor | Required | Quantization scale factor. When dst_dtype = torch.quint4x2, the data type is float32 and the shape is (M,). When dst_dtype = torch_npu.float4_e2m1fn_x2, the data type is uint8 and the shape is (M, ceildiv(N1*N2,64), 2). | float32, uint8 | (M,) or (M, ceildiv(N1*N2,64), 2) |
Constraints
- This API supports both training and inference use cases.
- This API can be called in single-operator mode and TorchAir graph mode.
- The N2 dimension of input x must meet the following conditions:
- If dst_dtype = torch.quint4x2, N2 must be a multiple of 8.
- If dst_dtype = torch_npu.float4_e2m1fn_x2, N2 must be an even number.
- The data types of inputs kronecker_p1 and kronecker_p2 must be the same as that of x.
- dst_dtype supports torch.quint4x2 (default) and torch_npu.float4_e2m1fn_x2. The outputs are described as follows:
- If dtype is torch.quint4x2, output out is of type int32, formed by concatenating 8 int4 values. Unpack the values manually to view them. Output quant_scale is of type float32.
- If dtype is torch_npu.float4_e2m1fn_x2, output out is of type uint8, formed by concatenating 2 float4_e2m1fn_x2 values. Unpack the values manually to view them. Output quant_scale is of type uint8. Convert it to float8_e8m0fnu manually to view the actual values.
- The value range of group_list_type is 0-2. If group_list_type is 0 or 1, the shape of group_list is (G,); if group_list_type is 2, the shape of group_list is (G, 2). G indicates the number of groups and must be less than or equal to 1024.
- group_list must meet the following conditions:
Ascend 950PR&950DT products : group_list supports only None input.
Atlas A3 products ,Atlas A2 products :- If group_list is not None, the values of the group_list input must meet the following conditions. Otherwise, the output cannot be guaranteed to meet expectations:
- When group_list_type is 0, group_list must be a non-negative monotonically non-decreasing sequence, representing the cumsum result (cumulative sum) of the size of each group after grouping. The last value must be less than or equal to the first dimension of the tensor in x.
- When group_list_type is 1, group_list must be a non-negative sequence, representing the size of each group after grouping. The sum of the values must be less than or equal to the first dimension of the tensor in x.
- When group_list_type is 2, group_list must be a non-negative sequence arranged as [[groupIdx0, groupSize0], [groupIdx1, groupSize1]...], where groupSize is the size of each group after grouping. The sum of the values in the second column must be less than or equal to the first dimension of the tensor in x.
- If group_list is not None, the values of the group_list input must meet the following conditions. Otherwise, the output cannot be guaranteed to meet expectations:
Deterministic Computation
Defaults to a deterministic implementation.
Calling Instructions
Single-operator mode call (eager)
import torch import torch_npu import cann_ops_nn M, N1, N2 = 128, 16, 16 x = torch.randn(M, N1, N2, dtype=torch.float16).npu() kronecker_p1 = torch.randn(N1, N1, dtype=torch.float16).npu() kronecker_p2 = torch.randn(N2, N2, dtype=torch.float16).npu() # int32 output (default) out, quant_scale = cann_ops_nn.flat_quant(x, kronecker_p1, kronecker_p2) print("out: ", out) print("quant_scale: ", quant_scale)With optional inputs:
group_list = torch.tensor([0, 16, 32, 48, 64], dtype=torch.int64).npu() out, quant_scale = cann_ops_nn.flat_quant( x, kronecker_p1, kronecker_p2, group_list=group_list, clip_ratio=0.95, group_list_type=0 )Graph mode (torchair) call
import torch, torch_npu, cann_ops_nn @torch.compile(backend="npu") def func(x, p1, p2): return cann_ops_nn.flat_quant(x, p1, p2) M, N1, N2 = 128, 16, 16 x = torch.randn(M, N1, N2, dtype=torch.float16).npu() kronecker_p1 = torch.randn(N1, N1, dtype=torch.float16).npu() kronecker_p2 = torch.randn(N2, N2, dtype=torch.float16).npu() out, quant_scale = func(x, kronecker_p1, kronecker_p2) print("out: ", out) print("quant_scale: ", quant_scale)