cla_gate_backward
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 that implements the backward gradient computation of the CLA (Cross-Layer Attention) gate merge. It takes the input upstream gradient G = ∂L/∂O, the two raw Attention outputs O_g/O_l, and the two gate logits z_g/z_l, and computes and returns four gradients in one call: the two Attention output gradients (dO_g, dO_l) and the two gate logits gradients (dz_g, dz_l). This API does not perform gradient quantization: the output retains the input precision (BF16/FP16), and no FP8 gradients are generated. The backward operator does not receive the forward Sigmoid intermediate results; s_g/s_l are recomputed inside the operator.
Formula:
Let T be the number of tokens on the current rank, N the number of attention heads, and D the value head dim.
Phase 1: Sigmoid recomputation (recomputed inside the operator, FP32 domain)
- Two head-wise Sigmoid gates and their derivative terms (z_g and z_l are the gate logits of the two branches):$$ s\_g = \sigma(z\_g), \qquad s\_l = \sigma(z\_l), \qquad \sigma(x) = \frac{1}{1 + e^{-x}} $$
Phase 2: Attention output gradients (element-wise chain rule, with s_g/s_l broadcast along dimension D)
$$ dO\_g = G \odot s\_g, \qquad dO\_l = G \odot s\_l $$Phase 3: Gate logits gradients (chain rule + Sigmoid derivative, with sum_d reduced along head_dim D only, not across tokens or heads)
$$ dz\_g = \left( \sum_{d=0}^{D-1} G \odot O\_g \right) \odot s\_g \odot (1 - s\_g), \qquad dz\_l = \left( \sum_{d=0}^{D-1} G \odot O\_l \right) \odot s\_l \odot (1 - s\_l) $$- The products G⊙O_g and G⊙O_l and the reduction along D are performed in the FP32 domain (completed within a single pass, without extra memory round trips) to reduce the accumulation error of the reduction.
- No division/exponent overflow paths; NaN/Inf inputs propagate per IEEE semantics (no special protection required).
- Two head-wise Sigmoid gates and their derivative terms (z_g and z_l are the gate logits of the two branches):
Prototype
cann_ops_nn.cla_gate_backward(
grad_merged,
global_attn,
local_attn,
global_gate_logits,
local_gate_logits,
*,
input_attn_layout="TND",
) -> (Tensor, Tensor, Tensor, Tensor)Parameters
| Name | Type | Type | Description | data type | Dimension (Shape) |
|---|---|---|---|---|---|
grad_merged | Tensor | Required | Gradient back-propagated from the output projection to the gate merge, corresponding to G in the formula. Supports non-contiguous tensors. Does not support empty tensors. | torch.float16, torch.bfloat16 | 3D [T, N, D], D supports only 128 or 256 |
global_attn | Tensor | Required | Forward Global/CLA branch attention output, corresponding to O_g in the formula. Supports non-contiguous tensors. Does not support empty tensors. | torch.float16, torch.bfloat16, same data type as gradMerged | 3D [T, N, D], exactly identical to grad_merged |
local_attn | Tensor | Required | Forward Local/SWA branch attention output, corresponding to O_l in the formula. Supports non-contiguous tensors. Does not support empty tensors. | torch.float16, torch.bfloat16, same data type as gradMerged | 3D [T, N, D], exactly identical to grad_merged |
global_gate_logits | Tensor | Required | Forward Global gate logits, corresponding to z_g in the formula. Supports non-contiguous tensors. Does not support empty tensors. | torch.float16, torch.bfloat16, same data type as gradMerged | 2D [T, N], T and N consistent with the first two dimensions of the three-way TND inputs |
local_gate_logits | Tensor | Required | Forward Local gate logits, corresponding to z_l in the formula. Supports non-contiguous tensors. Does not support empty tensors. | torch.float16, torch.bfloat16, same data type as gradMerged | 2D [T, N], exactly identical to global_gate_logits |
input_attn_layout | str | Optional | Layout format string of the input tensors. Currently only "TND" is supported. Default value: "TND". | - | - |
Return Values
| Name | Type | Type | Description | data type | Dimension (Shape) |
|---|---|---|---|---|---|
grad_global_attn_out | Tensor | Required | Gradient of the Global attention branch, corresponding to dO_g in the formula. | Same as global_attn | Same as global_attn |
grad_local_attn_out | Tensor | Required | Gradient of the Local attention branch, corresponding to dO_l in the formula. | Same as local_attn | Same as local_attn |
grad_global_gate_logits_out | Tensor | Required | Gradient of the Global gate logits, corresponding to dz_g in the formula. | Same as global_gate_logits | Same as global_gate_logits |
grad_local_gate_logits_out | Tensor | Required | Gradient of the Local gate logits, corresponding to dz_l in the formula. | Same as local_gate_logits | Same as local_gate_logits |
Constraints
- This API supports training use cases.
- This API can be called in single-operator mode.
- The operator does not support empty tensors; non-contiguous tensors are supported.
- The value of
Dcan only be 128 or 256.
Deterministic Computation
- Defaults to a deterministic implementation.
Example
Single-operator mode call:
import torch import torch_npu import cann_ops_nn # Construct inputs: three TND inputs [T, N, D], two gate logits [T, N], dtype must be consistent. grad_merged = torch.randn(8, 64, 256, dtype=torch.bfloat16).npu() global_attn = torch.randn(8, 64, 256, dtype=torch.bfloat16).npu() local_attn = torch.randn(8, 64, 256, dtype=torch.bfloat16).npu() global_gate_logits = torch.randn(8, 64, dtype=torch.bfloat16).npu() local_gate_logits = torch.randn(8, 64, dtype=torch.bfloat16).npu() grad_global_attn_out, grad_local_attn_out, grad_global_gate_logits_out, grad_local_gate_logits_out = \ cann_ops_nn.cla_gate_backward( grad_merged, global_attn, local_attn, global_gate_logits, local_gate_logits, input_attn_layout="TND", ) print(grad_global_attn_out.shape, grad_global_attn_out.dtype) print(grad_local_attn_out.shape, grad_local_attn_out.dtype) print(grad_global_gate_logits_out.shape, grad_global_gate_logits_out.dtype) print(grad_local_gate_logits_out.shape, grad_local_gate_logits_out.dtype)