situ_glu_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:
The backward gradient operator of situ_glu. Given the forward output gradient
grad_yand forward inputx, it differentiatesxusing the SiTU gated linear unit formula and returns the input gradientgrad_x. It encapsulatesaclnnSituGluGradat the bottom layer.Formula:
For the given input tensor
x, merge the axes based on the input parameterdim. After merging, the dimensions are[pre, cut], wherecutmust be even. Leth = cut // 2. Given the forward output gradient $\text{grad\_y}$ (whose shape is the same as the forward outputy, that is, thedimaxis is half ofx), compute the input gradientgrad_xin the following steps:Split
xinto front and back parts to obtaingateandupbased onactivate_left(consistent with the forward; each has shape[..., h]):activate_left=True(default):gate = x[..., :h],up = x[..., h:]activate_left=False:gate = x[..., h:],up = x[..., :h]
Compute the forward intermediates:
$$ t_g = \tanh\left(\frac{gate}{\beta}\right),\quad s_g = \text{sigmoid}(gate),\quad \text{situ\_a} = \beta \cdot t_g \cdot s_g $$When
linear_beta > 0:$$ t_u = \tanh\left(\frac{up}{linear\_beta}\right),\quad up' = linear\_beta \cdot t_u $$When
linear_beta <= 0, $up' = up$. The forward output is $y = \text{situ\_a} \cdot up'$.Product rule:
$$ \text{grad\_situ\_a} = \text{grad\_y} \cdot up' $$$$ \text{grad\_up'} = \text{grad\_y} \cdot \text{situ\_a} $$gate gradient:
$$ \text{grad\_gate} = \text{grad\_situ\_a} \cdot s_g \cdot \left((1 - t_g^2) + \beta \cdot t_g \cdot (1 - s_g)\right) $$up gradient:
- If
linear_beta > 0: $\text{grad\_up} = \text{grad\_up'} \cdot (1 - t_u^2)$ - If
linear_beta <= 0: $\text{grad\_up} = \text{grad\_up'}$
- If
Concatenate
grad_gateandgrad_upintograd_xbased onactivate_left(shape consistent withx):activate_left=True: $\text{grad\_x} = \text{concat}([\text{grad\_gate}, \text{grad\_up}], \text{dim})$activate_left=False: $\text{grad\_x} = \text{concat}([\text{grad\_up}, \text{grad\_gate}], \text{dim})$
Prototype
cann_ops_nn.situ_glu_grad(
grad_y,
x,
*,
dim=-1,
beta=1.0,
linear_beta=0.0,
activate_left=True
) -> TensorParameters
| Name | Type | Required/Optional | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|---|
grad_y | Tensor | Required | Gradient of the forward output y. | float16, float32, bfloat16 | Same as x, but the size of the dim dimension is x.shape[dim] // 2. |
x | Tensor | Required | Forward input. The dim dimension is evenly split into the gate and up parts. The data type of grad_y must be consistent with that of x. | float16, float32, bfloat16 | 1 to 8 dimensions, and the size of the dim dimension is even. |
dim | int | Optional | Dimension along which to split. Value range: [-x.dim(), x.dim()-1]. Default value: -1. Must match the forward situ_glu. | - | - |
beta | float | Optional | Scale factor of the SiTU gate part. Must remain consistent with the forward situ_glu. Default value: 1.0. | - | - |
linear_beta | float | Optional | Scale factor of the linear tanh in the up path. Must remain consistent with the forward situ_glu. Default value: 0.0. | - | - |
activate_left | bool | Optional | Whether the gate is the first half when x is split. Must remain consistent with the forward situ_glu. Default value: True. | - | - |
Return Values
| Name | Type | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|
grad_x | Tensor | Gradient of input x. | Same as x | Exactly the same as x |
Constraints
- This API can be called in single-operator mode and TorchAir graph mode.
- If
dimis not the last dimension and the combination of trailing dimensions does not meet half-row 32B alignment, the implementation falls back to the Long-H path. The result is correct but performance slightly degrades. - The values of
beta,linear_beta,activate_left, anddimmust match the forwardsitu_glucall. Otherwise, the gradient result is incorrect.
Deterministic Computation
- Defaults to a deterministic implementation.
Example
Single-operator mode call:
import torch import torch_npu import cann_ops_nn x = torch.randn(2, 32, dtype=torch.float32).npu() grad_y = torch.randn(2, 16, dtype=torch.float32).npu() # Call with parameters consistent with the forward grad_x = cann_ops_nn.situ_glu_grad( grad_y, x, dim=-1, beta=1.0, linear_beta=0.0, activate_left=True ) print(grad_x.shape, grad_x.dtype) # torch.Size([2, 32]) torch.float32TorchAir graph mode call:
import torch import torch_npu import torchair import cann_ops_nn class Model(torch.nn.Module): def forward(self, grad_y, x): return cann_ops_nn.situ_glu_grad( grad_y, x, dim=-1, beta=1.0, linear_beta=0.0, activate_left=True ) model = torch.compile(Model().npu(), backend=torchair.get_npu_backend(), dynamic=False) x = torch.randn(2, 32, dtype=torch.float32).npu() grad_y = torch.randn(2, 16, dtype=torch.float32).npu() grad_x = model(grad_y, x)