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=5 uses aclnnSwigluGroupQuantV2 as the underlying implementation, and other modes use aclnnSwigluGroupQuant.

  • Calculation Process:

    1. Evenly split the last dimension of x into A and B, and compute the SwiGLU result; mode 5 uses A * sigmoid(alpha * A) * (B + bias).
    2. When weight is not empty, multiplies the SwiGLU result by weight token by token to obtain the quantization input.
    3. Quantize the weighted result according to quant_mode and output y and y_scale.
    4. When output_origin=True, additionally return the pre-weighted SwiGLU result y_origin; otherwise, return a placeholder tensor of shape [0].

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

NameTypeRequired/OptionalDescriptionData TypeDimension (Shape)
xTensorRequiredSwiGLU 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)
weightTensorOptionalToken-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.float321-8 dimensions, with the number of elements equal to that of x excluding the last dimension
group_indexTensorOptionalNumber of grouped tokens in count mode.torch.int641 dimension
scaleTensorOptionalScale used in HiFloat8 static quantization. Used only when quant mode is 2.torch.float321 dimension
dst_typeintOptionaltorch dtype encoding of the target quantization type. The default value is 291.--
quant_modeintOptionalQuantization mode. Value options: 0, 1, 2, 3, and 5. 1 indicates the original MX formula, and 5 indicates the MX V2 formula.--
block_sizeintOptionalQuantization block size. 0 indicates that the mode default value is used.--
round_scaleboolOptionalWhether the scale is rounded to a power of 2 in MX quantization.--
clamp_limitfloatOptionalClamping threshold before the SwiGLU computation. The default value -1.0 indicates that clamping is disabled.--
dst_type_maxfloatOptionalMaximum finite value used to compute the scale in HiFloat8 dynamic quantization.--
output_originboolOptionalWhether to return the pre-weighted SwiGLU result.--
alphafloatOptionalScaling factor of the Sigmoid input. Default: 1.0. Effective only when quant_mode=5.--
biasfloatOptionalAddition bias of the second SwiGLU branch. Default: 0.0. Effective only when quant_mode=5.--

quant_mode and dst_type

quant_modeMeaningSupported Values of dst_typetorch dtype of ytorch dtype of y_scale
0Block FP823/291 indicates float8_e5m2; 24/292 indicates float8_e4m3fn.torch.float8_e5m2 or torch.float8_e4m3fntorch.float32
1MX FP823, 24, 291, 292torch.float8_e5m2 or torch.float8_e4m3fntorch.float8_e8m0fnu
2HiFloat8 static quantization290 indicates HiFloat8.torch.uint8torch.float32, with shape [0]
3HiFloat8 dynamic quantization290 indicates HiFloat8.torch.uint8torch.float32
5MX V2 FP823, 24, 291, 292torch.float8_e5m2 or torch.float8_e4m3fntorch.float8_e8m0fnu

dst_type Encoding Description

dst_typeCorresponding TypeSourceType Dispatched to GE/ACLApplicable quant_mode
23torch.float8_e5m2Native PyTorch dtype int valueDT_FLOAT8_E5M2 / ACL_FLOAT8_E5M20, 1, 5
24torch.float8_e4m3fnNative PyTorch dtype int valueDT_FLOAT8_E4M3FN / ACL_FLOAT8_E4M3FN0, 1, 5
291torch_npu.float8_e5m2, same semantics as torch.float8_e5m2torch_npu extension dtype encodingDT_FLOAT8_E5M2 / ACL_FLOAT8_E5M20, 1, 5
292torch_npu.float8_e4m3fn, same semantics as torch.float8_e4m3fntorch_npu extension dtype encodingDT_FLOAT8_E4M3FN / ACL_FLOAT8_E4M3FN0, 1, 5
290torch_npu.hifloat8torch_npu extension dtype encodingDT_HIFLOAT8 / ACL_HIFLOAT82, 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

NameTypeDescriptionData TypeDimension (Shape)
yTensorQuantized output.See quant_mode and dst_typex.shape[:-1] + [D/2]
y_scaleTensorQuantization scale output.See quant_mode and dst_typex.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_originTensorPre-weighted SwiGLU result of weight or a placeholder Tensor.Same as xx.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, and scale must all be NPU tensors; optional tensors can be None.
  • For quant_mode=5, input x supports only 2D [T,D], and an error is reported for more than 2 dimensions. Other modes support 2-8 dimensions (2-7 dimensions for quant_mode=1). For quant_mode=5, the last dimension D must be greater than or equal to 64 and divisible by 64. For other modes, D must still be greater than or equal to 256 and divisible by 256.
  • dst_type supports the torch dtype encodings of FP8 and HiFloat8. For details, see dst_type encoding description.
  • When quant_mode=0, only FP8 output is supported. dst_type supports 23, 24, 291, and 292. block_size supports 0 or 128.
  • When quant_mode=1, FP8 output is supported. dst_type supports 23, 24, 291, and 292. block_size supports 0 or 32. round_scale must be True.
  • For quant_mode=5, the MX V2 quantization formula is used, and only FP8 output is supported. block_size supports 0 or 32, round_scale must be True, and group_index and scale inputs are not supported.
  • When quant_mode=2, HiFloat8 static quantization output is supported. scale is required. dst_type, block_size, and round_scale do not take effect, and the actual dispatch is HiFloat8.
  • For quant_mode=3, HiFloat8 dynamic quantization output is supported. scale is not used, dst_type, block_size, and round_scale do not take effect, and the actual dispatch is HiFloat8.
  • The data type of y_scale must match quant_mode: torch.float32 for Block FP8, torch.float8_e8m0fnu for MX, and torch.float32 for HiFloat8.
  • When quant_mode=3, group_index can be used for group dynamic quantization in MoE use cases. The shape of y_scale is group_index.shape, or [1] when group_index is not passed.
  • If clamp_limit is not enabled, the default placeholder value -1.0 is used. If enabled, it must be greater than 0. For quant_mode=5, it must also be a finite value.
  • For all supported quant_mode values, the pre-weighted y_origin can be output by setting output_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 x and weight; 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)