cla_gate_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:

    Fused operator for the weighted merging of two head-wise gate paths of CLA (Cross-Layer Attention) and dual-axis dynamic block quantization, registered as a Torch Extension and encapsulating aclnnClaGateQuant at the bottom layer. First, Sigmoid gating is applied to the Attention outputs of the Global/CLA branch and the Local/SWA branch, and the outputs are weighted and merged. Then, the merged result is reshaped to [T, K] (K = N × D), and block-based dynamic quantization is performed along the K direction ([1,32] block, row-wise) and the T direction ([32,1] block, col-wise), outputting low-precision FP8/FP4 tensors and the corresponding E8M0 scale factors.

  • Calculation Process:

    1. Two-path head-wise Sigmoid gating: s_g = sigmoid(global_gate_logits), s_l = sigmoid(local_gate_logits);
    2. Gate fusion: merged = s_g * global_attn + s_l * local_attn;
    3. Logical matrix before quantization: X = reshape(merged, [T, K]), K = N × D;
    4. row-wise quantization (K direction, [1,32] block) to obtain row_data and row_scale;
    5. When dual_axis_flag=True, col-wise quantization (T direction, [32,1] block) is additionally performed to obtain col_data and col_scale; when dual_axis_flag=False, col_data/col_scale return empty Tensors.

Prototype

cann_ops_nn.cla_gate_quant(
    global_attn,
    local_attn,
    global_gate_logits,
    local_gate_logits,
    *,
    dst_type=None,
    round_mode="rint",
    scale_alg=1,
    input_attn_layout="TND",
    dual_axis_flag=False,
) -> (Tensor, Tensor, Tensor, Tensor)

Parameters

NameTypeTypeDescriptionData TypeDimension (Shape)
global_attnTensorRequiredAttention output O_g of the Global/CLA branch.torch.float16, torch.bfloat163D [T, N, D]
local_attnTensorRequiredAttention output O_l of the Local/SWA branch, with a shape consistent with global_attn.torch.float16, torch.bfloat163D [T, N, D]
global_gate_logitsTensorRequiredPre-Sigmoid value z_g of the Global gate, with a dtype consistent with global_attn.torch.float16, torch.bfloat162D [T, N]
local_gate_logitsTensorRequiredPre-Sigmoid value z_l of the Local gate, with a shape consistent with global_gate_logits.torch.float16, torch.bfloat162D [T, N]
dst_typeintOptionaltorch dtype encoding of the target quantization type. See dst_type encoding description below. Passing None is equivalent to 24(torch.float8_e4m3fn). The torch.dtype type is supported.--
round_modestrOptionalRounding mode. For FP8, only "rint" is supported. For FP4, "rint", "round", and "floor" are supported. The default is "rint".--
scale_algintOptionalScale computation method. 1 indicates cuBLAS, and 0 indicates OCP. For FP4, only 0 is supported. The default is 1.--
input_attn_layoutstrOptionalLayout format of the input global_attn/local_attn. Currently, only "TND" is supported. The default is "TND".--
dual_axis_flagboolOptionalTrue indicates dual-axis quantization (outputs both the row and col results); False indicates single-axis quantization (outputs only the row-wise result, and the col output is an empty Tensor). The default is False.--

dst_type Encoding Description

dst_typeCorresponding TypeSourceType Dispatched To GE/ACL
23torch.float8_e5m2Native PyTorch dtype int valueDT_FLOAT8_E5M2 / ACL_FLOAT8_E5M2(35)
24torch.float8_e4m3fnNative PyTorch dtype int valueDT_FLOAT8_E4M3FN / ACL_FLOAT8_E4M3FN(36)
296torch_npu.float4_e2m1fn_x2torch_npu extended dtype encodingDT_FLOAT4_E2M1 / ACL_FLOAT4_E2M1(40)
297torch_npu.float4_e1m2fn_x2torch_npu extended dtype encodingDT_FLOAT4_E1M2 / ACL_FLOAT4_E1M2(41)

NOTE: None is normalized to 24 (torch.float8_e4m3fn). In the graph mode (TorchAir) path, convert_cla_gate_quant converts the torch dtype encodings in the preceding table to GE dtype attrs. In the ACLNN path, they are converted to the corresponding aclDataType before the underlying API is called.

Return Values

NameTypeDescriptionData TypeDimension (Shape)
row_dataTensorRow-wise (K direction) quantized data P_i.For FP8, torch.float8_e5m2/torch.float8_e4m3fn; for FP4, packed and stored as torch.uint8 on the torch side (two FP4 values occupy 1 byte)For FP8, [T, K]; for FP4, [T, K/2]
row_scaleTensorE8M0 scaling factor of each row-wise [1,32] group, stored in the last dimension in pairs, with even pads filled with 0.torch.uint8 (raw E8M0 values)[T, ceil(K/64), 2]
col_dataTensorCol-wise (T direction) quantized data P_j; an empty Tensor when dual_axis_flag=False.Same as row_dataDual-axis: same as row_data; single-axis: [0]
col_scaleTensorE8M0 scaling factor of each col-wise [32,1] group, with even pads filled with 0; an empty Tensor when dual_axis_flag=False.torch.uint8 (raw E8M0 values)Dual-axis: [ceil(T/64), K, 2]; single-axis: [0]

where T = global_attn.shape[0], N = global_attn.shape[1], D = global_attn.shape[2], and K = N × D.

Constraints

  • This API supports calls in single-operator mode and TorchAir graph mode.
  • global_attn, local_attn, global_gate_logits, and local_gate_logits must all be NPU Tensors.
  • global_attn/local_attn must be 3D tensors with the same shape, which is [T, N, D]; N ∈ [1,128], D ∈ {128, 256}.
  • global_gate_logits/local_gate_logits must be 2D tensors of shape [T, N], with the same dtype as global_attn/local_attn.
  • When dst_type is FP4 (296/297), K = N × D must be divisible by 4, and scale_alg must be 0.
  • FP8 output types (23/24) only support round_mode="rint".
  • When dual_axis_flag=False, col_data/col_scale are empty tensors and are not accessed by the underlying API; the row outputs are bit-wise consistent with those in the dual-axis mode.
  • round_mode must be one of "rint", "round", "floor"; scale_alg must be 0 or 1.
  • input_attn_layout only supports "TND".

Deterministic/Batch Consistency

  • Deterministic computation

    • Defaults to a deterministic implementation: fixed kernel partitioning, reduction order, and quantization block division. Same input yields the same output.
  • Batch consistency:

    • Ascend 950PR&950DT products: single-axis defaults to a Batch-consistent implementation; dual-axis is supported by default in use cases where T is a multiple of 64, and not supported otherwise.

Calling Instructions

  • Single-operator mode call (dual-axis FP8 quantization):

    import torch
    import torch_npu
    import cann_ops_nn
    
    t, n, d = 8, 24, 256
    global_attn = torch.randn(t, n, d, dtype=torch.bfloat16).npu()
    local_attn = torch.randn(t, n, d, dtype=torch.bfloat16).npu()
    global_gate_logits = torch.randn(t, n, dtype=torch.bfloat16).npu()
    local_gate_logits = torch.randn(t, n, dtype=torch.bfloat16).npu()
    
    row_data, row_scale, col_data, col_scale = cann_ops_nn.cla_gate_quant(
        global_attn,
        local_attn,
        global_gate_logits,
        local_gate_logits,
        dst_type=24,          # torch.float8_e4m3fn
        round_mode="rint",
        scale_alg=1,
        input_attn_layout="TND",
        dual_axis_flag=True,
    )
  • Single-axis FP4 quantization (col_data/col_scale are empty Tensors):

    row_data, row_scale, col_data, col_scale = cann_ops_nn.cla_gate_quant(
        global_attn,
        local_attn,
        global_gate_logits,
        local_gate_logits,
        dst_type=296,         # torch_npu.float4_e2m1fn_x2
        round_mode="rint",
        scale_alg=0,
        input_attn_layout="TND",
        dual_axis_flag=False,
    )
  • Graph mode (TorchAir) call:

    import torch
    import torch_npu
    import torchair
    import cann_ops_nn
    
    class Model(torch.nn.Module):
        def forward(self, global_attn, local_attn, global_gate_logits, local_gate_logits):
            row_data, row_scale, _, _ = cann_ops_nn.cla_gate_quant(
                global_attn,
                local_attn,
                global_gate_logits,
                local_gate_logits,
                dst_type=24,
                dual_axis_flag=False,
            )
            return row_data, row_scale
    
    model = torch.compile(Model().npu(), backend=torchair.get_npu_backend(), dynamic=False)
    model(global_attn, local_attn, global_gate_logits, local_gate_logits)