clipped_swiglu_grad
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description:
ClippedSwigluGradis the backward gradient computation operator ofClippedSwiglu. Based on the gradientgrad_yof the forward outputy, the forward inputx, and the optional group indexgroup_index, it computes the gradientgrad_xof the inputx. The underlying implementation isaclnnClippedSwigluGrad.Calculation Process:
- Merge the axes of
xbased on the parameterdim. The dimension ofxafter merging is[pre, dim2H]; the dimension ofgrad_yafter merging is[pre, dim2H // 2]. - Filter the
preaxis ofxbased ongroup_index. Only the firstgrouprows participate in the computation, and the gradient of the remaining rows is 0. - Split
xinto two partsx0andx1based oninterleaved(same as the forward pass). - Recompute the forward intermediate quantities:
- $\mathbf{x}_0' = \min(\mathbf{x}_0, \text{limit})$
- $\mathbf{x}_1' = \min(\max(\mathbf{x}_1, -\text{limit}), \text{limit})$
- $s = \sigma(\alpha \times \mathbf{x}_0') = \frac{1}{1 + e^{-\alpha \times \mathbf{x}_0'}}$
- Compute the backward gradients:
- $\text{mask}_{x_0} = (\mathbf{x}_0 \le \text{limit})$
- $\text{mask}_{x_1} = (-\text{limit} \le \mathbf{x}_1 \le \text{limit})$
- $\mathbf{grad}_{x_0} = \text{grad\_y} \times (\mathbf{x}_1' + \text{bias}) \times s \times (1 + \alpha \times \mathbf{x}_0' \times (1 - s)) \times \text{mask}_{x_0}$
- $\mathbf{grad}_{x_1} = \text{grad\_y} \times \mathbf{x}_0' \times s \times \text{mask}_{x_1}$
- Scatter
grad_x0andgrad_x1back todx(corresponding to the splitting method), and reshape the output to the same dimensions asx.
- Merge the axes of
Prototype
cann_ops_nn.clipped_swiglu_grad(
grad_y,
x,
*,
group_index=None,
dim=-1,
alpha=1.702,
limit=7.0,
bias=1.0,
interleaved=True,
clamp_mode=0,
) -> TensorParameters
| Name | Type | Type | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|---|
grad_y | Tensor | Required | Gradient of the forward output y, with the dim dimension being half of x. | torch.float16, torch.bfloat16, torch.float32 | Same as x, but the dim dimension is half of the corresponding dimension of x |
x | Tensor | Required | Forward input, used to recompute the forward intermediate quantities. | torch.float16, torch.bfloat16, torch.float32 | 1 to 8 dimensions, with an even size in the dim dimension |
group_index | Tensor | Optional | Group index. The i-th element indicates the number of batches processed by the i-th group. | torch.int64 | 1 dimension, with no more than 8192 elements |
dim | int | Optional | Index of the dimension for axis merging and splitting. Value range: [-x.dim(), x.dim()-1]. Default: -1. | - | - |
alpha | float | Optional | SwiGLU activation coefficient. Default: 1.702. | - | - |
limit | float | Optional | Clipping threshold. The value must be greater than 0. Default: 7.0. | - | - |
bias | float | Optional | Bias of the linear computation. Default: 1.0. | - | - |
interleaved | bool | Optional | Splitting method. true indicates odd-even splitting, and false indicates front-back splitting. Default: true. | - | - |
clamp_mode | int | Optional | Clamp position control. Default: 0. Currently, only 0 (clamp before silu) is supported. 1 (clamp after silu) will be supported in later versions. | - | - |
Return Values
| Name | Type | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|
grad_x | Tensor | Gradient of input x. | Same as x | Same as x |
Constraints
- This API can be called in single-operator mode and TorchAir graph mode.
grad_yandxmust both be NPU tensors;group_indexcan beNone.- The data types of
grad_yandxmust be the same. - The rank of
xmust be greater than 0, and the size of thedimdimension must be an even number. - The size of the
dimdimension ofgrad_ymust be half of the corresponding dimension ofx, and the other dimensions must be the same as those ofx. limitmust be greater than 0.- Currently,
clamp_modesupports only the value0. - Empty tensors and non-contiguous tensors are not supported.
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() grad_y = torch.randn(8, 256, dtype=torch.float16).npu() grad_x = torch.ops.cann_ops_nn.clipped_swiglu_grad( grad_y, x, dim=-1, alpha=1.702, limit=7.0, bias=1.0, interleaved=True) print(grad_x.shape, grad_x.dtype)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, grad_y, x): return torch.ops.cann_ops_nn.clipped_swiglu_grad(grad_y, x) model = torch.compile(Model().npu(), backend=torchair.get_npu_backend(), dynamic=False) grad_y = torch.randn(8, 256, dtype=torch.float16).npu() x = torch.randn(8, 512, dtype=torch.float16).npu() grad_x = model(grad_y, x)