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
aclnnClaGateQuantat 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:
- Two-path head-wise Sigmoid gating:
s_g = sigmoid(global_gate_logits),s_l = sigmoid(local_gate_logits); - Gate fusion:
merged = s_g * global_attn + s_l * local_attn; - Logical matrix before quantization:
X = reshape(merged, [T, K]),K = N × D; - row-wise quantization (K direction,
[1,32]block) to obtainrow_dataandrow_scale; - When
dual_axis_flag=True, col-wise quantization (T direction,[32,1]block) is additionally performed to obtaincol_dataandcol_scale; whendual_axis_flag=False,col_data/col_scalereturn empty Tensors.
- Two-path head-wise Sigmoid gating:
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
| Name | Type | Type | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|---|
global_attn | Tensor | Required | Attention output O_g of the Global/CLA branch. | torch.float16, torch.bfloat16 | 3D [T, N, D] |
local_attn | Tensor | Required | Attention output O_l of the Local/SWA branch, with a shape consistent with global_attn. | torch.float16, torch.bfloat16 | 3D [T, N, D] |
global_gate_logits | Tensor | Required | Pre-Sigmoid value z_g of the Global gate, with a dtype consistent with global_attn. | torch.float16, torch.bfloat16 | 2D [T, N] |
local_gate_logits | Tensor | Required | Pre-Sigmoid value z_l of the Local gate, with a shape consistent with global_gate_logits. | torch.float16, torch.bfloat16 | 2D [T, N] |
dst_type | int | Optional | torch 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_mode | str | Optional | Rounding mode. For FP8, only "rint" is supported. For FP4, "rint", "round", and "floor" are supported. The default is "rint". | - | - |
scale_alg | int | Optional | Scale computation method. 1 indicates cuBLAS, and 0 indicates OCP. For FP4, only 0 is supported. The default is 1. | - | - |
input_attn_layout | str | Optional | Layout format of the input global_attn/local_attn. Currently, only "TND" is supported. The default is "TND". | - | - |
dual_axis_flag | bool | Optional | True 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_type | Corresponding Type | Source | Type Dispatched To GE/ACL |
|---|---|---|---|
23 | torch.float8_e5m2 | Native PyTorch dtype int value | DT_FLOAT8_E5M2 / ACL_FLOAT8_E5M2(35) |
24 | torch.float8_e4m3fn | Native PyTorch dtype int value | DT_FLOAT8_E4M3FN / ACL_FLOAT8_E4M3FN(36) |
296 | torch_npu.float4_e2m1fn_x2 | torch_npu extended dtype encoding | DT_FLOAT4_E2M1 / ACL_FLOAT4_E2M1(40) |
297 | torch_npu.float4_e1m2fn_x2 | torch_npu extended dtype encoding | DT_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
| Name | Type | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|
row_data | Tensor | Row-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_scale | Tensor | E8M0 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_data | Tensor | Col-wise (T direction) quantized data P_j; an empty Tensor when dual_axis_flag=False. | Same as row_data | Dual-axis: same as row_data; single-axis: [0] |
col_scale | Tensor | E8M0 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, andlocal_gate_logitsmust all be NPU Tensors.global_attn/local_attnmust be 3D tensors with the same shape, which is[T, N, D];N ∈ [1,128],D ∈ {128, 256}.global_gate_logits/local_gate_logitsmust be 2D tensors of shape[T, N], with the same dtype asglobal_attn/local_attn.- When
dst_typeis FP4 (296/297),K = N × Dmust be divisible by 4, andscale_algmust be0. - FP8 output types (
23/24) only supportround_mode="rint". - When
dual_axis_flag=False,col_data/col_scaleare 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_modemust be one of"rint","round","floor";scale_algmust be0or1.input_attn_layoutonly 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_scaleare 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)