swiglu_group_quant
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description:
Performs grouped low-bit quantization after applying SwiGLU activation to the input
x. It supports Block FP8, MX FP8, and HiFloat8 static quantization, as well as HiFloat8 dynamic quantization.quant_mode=5usesaclnnSwigluGroupQuantV2as the underlying implementation, and other modes useaclnnSwigluGroupQuant.Calculation Process:
- Evenly split the last dimension of
xintoAandB, and compute the SwiGLU result; mode 5 usesA * sigmoid(alpha * A) * (B + bias). - When
weightis not empty, multiplies the SwiGLU result byweighttoken by token to obtain the quantization input. - Quantize the weighted result according to
quant_modeand outputyandy_scale. - When
output_origin=True, additionally return the pre-weighted SwiGLU resulty_origin; otherwise, return a placeholder tensor of shape[0].
- Evenly split the last dimension of
Prototype
cann_ops_nn.swiglu_group_quant(
x,
*,
weight=None,
group_index=None,
scale=None,
dst_type=291,
quant_mode=0,
block_size=0,
round_scale=False,
clamp_limit=-1.0,
dst_type_max=15.0,
output_origin=False,
alpha=1.0,
bias=0.0,
) -> (Tensor, Tensor, Tensor)Parameters
| Name | Type | Required/Optional | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|---|
x | Tensor | Required | SwiGLU input. The last dimension is evenly split into two parts. | torch.float16, torch.bfloat16 (extra support for torch.float32 when quant_mode is 2/3) | Only 2 dimensions for quant_mode=5; 2-8 dimensions for other modes (2-7 dimensions when quant_mode is 1) |
weight | Tensor | Optional | Token-wise weight. If not empty, it is multiplied with the SwiGLU result, and the weighted result is used for quantization. | For quant_mode=5: torch.float16, torch.bfloat16, and torch.float32; for other modes: only torch.float32 | 1-8 dimensions, with the number of elements equal to that of x excluding the last dimension |
group_index | Tensor | Optional | Number of grouped tokens in count mode. | torch.int64 | 1 dimension |
scale | Tensor | Optional | Scale used in HiFloat8 static quantization. Used only when quant mode is 2. | torch.float32 | 1 dimension |
dst_type | int | Optional | torch dtype encoding of the target quantization type. The default value is 291. | - | - |
quant_mode | int | Optional | Quantization mode. Value options: 0, 1, 2, 3, and 5. 1 indicates the original MX formula, and 5 indicates the MX V2 formula. | - | - |
block_size | int | Optional | Quantization block size. 0 indicates that the mode default value is used. | - | - |
round_scale | bool | Optional | Whether the scale is rounded to a power of 2 in MX quantization. | - | - |
clamp_limit | float | Optional | Clamping threshold before the SwiGLU computation. The default value -1.0 indicates that clamping is disabled. | - | - |
dst_type_max | float | Optional | Maximum finite value used to compute the scale in HiFloat8 dynamic quantization. | - | - |
output_origin | bool | Optional | Whether to return the pre-weighted SwiGLU result. | - | - |
alpha | float | Optional | Scaling factor of the Sigmoid input. Default: 1.0. Effective only when quant_mode=5. | - | - |
bias | float | Optional | Addition bias of the second SwiGLU branch. Default: 0.0. Effective only when quant_mode=5. | - | - |
quant_mode and dst_type
quant_mode | Meaning | Supported Values of dst_type | torch dtype of y | torch dtype of y_scale |
|---|---|---|---|---|
0 | Block FP8 | 23/291 indicates float8_e5m2; 24/292 indicates float8_e4m3fn. | torch.float8_e5m2 or torch.float8_e4m3fn | torch.float32 |
1 | MX FP8 | 23, 24, 291, 292 | torch.float8_e5m2 or torch.float8_e4m3fn | torch.float8_e8m0fnu |
2 | HiFloat8 static quantization | 290 indicates HiFloat8. | torch.uint8 | torch.float32, with shape [0] |
3 | HiFloat8 dynamic quantization | 290 indicates HiFloat8. | torch.uint8 | torch.float32 |
5 | MX V2 FP8 | 23, 24, 291, 292 | torch.float8_e5m2 or torch.float8_e4m3fn | torch.float8_e8m0fnu |
dst_type Encoding Description
dst_type | Corresponding Type | Source | Type Dispatched to GE/ACL | Applicable quant_mode |
|---|---|---|---|---|
23 | torch.float8_e5m2 | Native PyTorch dtype int value | DT_FLOAT8_E5M2 / ACL_FLOAT8_E5M2 | 0, 1, 5 |
24 | torch.float8_e4m3fn | Native PyTorch dtype int value | DT_FLOAT8_E4M3FN / ACL_FLOAT8_E4M3FN | 0, 1, 5 |
291 | torch_npu.float8_e5m2, same semantics as torch.float8_e5m2 | torch_npu extension dtype encoding | DT_FLOAT8_E5M2 / ACL_FLOAT8_E5M2 | 0, 1, 5 |
292 | torch_npu.float8_e4m3fn, same semantics as torch.float8_e4m3fn | torch_npu extension dtype encoding | DT_FLOAT8_E4M3FN / ACL_FLOAT8_E4M3FN | 0, 1, 5 |
290 | torch_npu.hifloat8 | torch_npu extension dtype encoding | DT_HIFLOAT8 / ACL_HIFLOAT8 | 2, 3 |
Note: quant_mode=2/3 indicates HiFloat8 mode, and the actual dispatch is DT_HIFLOAT8/ACL_HIFLOAT8. graph_convert converts the torch dtype encodings in the preceding table to GE dtype attrs. The ACLNN path converts the corresponding encodings to aclDataType and then calls the underlying API.
Return Values
| Name | Type | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|
y | Tensor | Quantized output. | See quant_mode and dst_type | x.shape[:-1] + [D/2] |
y_scale | Tensor | Quantization scale output. | See quant_mode and dst_type | x.shape[:-1] + [ceil((D/2)/128)] for quant_mode=0; x.shape[:-1] + [ceil(ceil((D/2)/32)/2), 2] for quant_mode=1/5; [0] for quant_mode=2; group_index.shape or [1] for quant_mode=3 |
y_origin | Tensor | Pre-weighted SwiGLU result of weight or a placeholder Tensor. | Same as x | x.shape[:-1] + [D/2] when output_origin=True, otherwise [0] |
Where D = x.shape[-1].
Constraints
- This API can be called in single-operator mode and TorchAir graph mode.
x,weight,group_index, andscalemust all be NPU tensors; optional tensors can beNone.- For
quant_mode=5, inputxsupports only 2D[T,D], and an error is reported for more than 2 dimensions. Other modes support 2-8 dimensions (2-7 dimensions forquant_mode=1). Forquant_mode=5, the last dimensionDmust be greater than or equal to 64 and divisible by 64. For other modes,Dmust still be greater than or equal to 256 and divisible by 256. dst_typesupports the torch dtype encodings of FP8 and HiFloat8. For details, seedst_type encoding description.- When
quant_mode=0, only FP8 output is supported.dst_typesupports23,24,291, and292.block_sizesupports0or128. - When
quant_mode=1, FP8 output is supported.dst_typesupports23,24,291, and292.block_sizesupports0or32.round_scalemust beTrue. - For
quant_mode=5, the MX V2 quantization formula is used, and only FP8 output is supported.block_sizesupports0or32,round_scalemust beTrue, andgroup_indexandscaleinputs are not supported. - When
quant_mode=2, HiFloat8 static quantization output is supported.scaleis required.dst_type,block_size, andround_scaledo not take effect, and the actual dispatch is HiFloat8. - For
quant_mode=3, HiFloat8 dynamic quantization output is supported.scaleis not used,dst_type,block_size, andround_scaledo not take effect, and the actual dispatch is HiFloat8. - The data type of
y_scalemust matchquant_mode:torch.float32for Block FP8,torch.float8_e8m0fnufor MX, andtorch.float32for HiFloat8. - When
quant_mode=3,group_indexcan be used for group dynamic quantization in MoE use cases. The shape ofy_scaleisgroup_index.shape, or[1]whengroup_indexis not passed. - If
clamp_limitis not enabled, the default placeholder value-1.0is used. If enabled, it must be greater than 0. Forquant_mode=5, it must also be a finite value. - For all supported
quant_modevalues, the pre-weightedy_origincan be output by settingoutput_origin=True. - Element values in group_index must be greater than or equal to 0.
- When quant_mode is 0 or 1, empty tensors are supported for x and weight, but group_index alone cannot be an empty tensor; empty tensors are not supported when quant_mode is 2, 3, or 5.
- The Torch API with quant_mode=5 supports non-contiguous
xandweight; non-contiguous tensors are not supported in other modes.
Deterministic Computation
Defaults to a deterministic implementation.
Calling Instructions
Single-operator mode call:
import torch import torch_npu import cann_ops_nn.ops x = torch.randn(8, 512, dtype=torch.float16).npu() y, y_scale, y_origin = torch.ops.cann_ops_nn.swiglu_group_quant( x, dst_type=291, quant_mode=0, block_size=128, )Call in graph mode (torchair):
import torch import torch_npu import torchair import cann_ops_nn.ops class Model(torch.nn.Module): def forward(self, x): y, y_scale, _ = torch.ops.cann_ops_nn.swiglu_group_quant( x, dst_type=291, quant_mode=0, block_size=128, ) return y, y_scale model = torch.compile(Model().npu(), backend=torchair.get_npu_backend(), dynamic=False) x = torch.randn(8, 512, dtype=torch.float16).npu() y, y_scale = model(x)