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_y and forward input x, it differentiates x using the SiTU gated linear unit formula and returns the input gradient grad_x. It encapsulates aclnnSituGluGrad at the bottom layer.

  • Formula:

    For the given input tensor x, merge the axes based on the input parameter dim. After merging, the dimensions are [pre, cut], where cut must be even. Let h = cut // 2. Given the forward output gradient $\text{grad\_y}$ (whose shape is the same as the forward output y, that is, the dim axis is half of x), compute the input gradient grad_x in the following steps:

    1. Split x into front and back parts to obtain gate and up based on activate_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]
    2. 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'$.

    3. Product rule:

      $$ \text{grad\_situ\_a} = \text{grad\_y} \cdot up' $$
      $$ \text{grad\_up'} = \text{grad\_y} \cdot \text{situ\_a} $$
    4. 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) $$
    5. 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'}$
    6. Concatenate grad_gate and grad_up into grad_x based on activate_left (shape consistent with x):

      • 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
) -> Tensor

Parameters

NameTypeRequired/OptionalDescriptionData TypeDimension (Shape)
grad_yTensorRequiredGradient of the forward output y.float16, float32, bfloat16Same as x, but the size of the dim dimension is x.shape[dim] // 2.
xTensorRequiredForward 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, bfloat161 to 8 dimensions, and the size of the dim dimension is even.
dimintOptionalDimension along which to split. Value range: [-x.dim(), x.dim()-1]. Default value: -1. Must match the forward situ_glu.--
betafloatOptionalScale factor of the SiTU gate part. Must remain consistent with the forward situ_glu. Default value: 1.0.--
linear_betafloatOptionalScale factor of the linear tanh in the up path. Must remain consistent with the forward situ_glu. Default value: 0.0.--
activate_leftboolOptionalWhether the gate is the first half when x is split. Must remain consistent with the forward situ_glu. Default value: True.--

Return Values

NameTypeDescriptionData TypeDimension (Shape)
grad_xTensorGradient of input x.Same as xExactly the same as x

Constraints

  • This API can be called in single-operator mode and TorchAir graph mode.
  • If dim is 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, and dim must match the forward situ_glu call. 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.float32
  • TorchAir 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)