situ_glu

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:

    SiTU Gated Linear Unit activation function. Splits the input tensor x along the specified dimension dim into two halves: gate and up, and computes the output using the SiTU formula. The bottom layer encapsulates aclnnSituGlu.

  • Formula:

    For a given input tensor x, compute the SiTU output y as follows:

    1. Merge the axes based on dim so that the shape after merging is [pre, cut], where cut must be even. Let h = cut // 2.

    2. Split x into the front and rear parts based on activate_left:

      • activate_left=True (default): gate = x[..., :h], up = x[..., h:]
      • activate_left=False: gate = x[..., h:], up = x[..., :h]
    3. Perform the SiTU computation based on beta and linear_beta:

      $$ situ\_a = \beta \cdot \tanh\left(\frac{gate}{\beta}\right) \cdot \text{sigmoid}(gate) $$

      When linear_beta > 0:

      $$ up = linear\_beta \cdot \tanh\left(\frac{up}{linear\_beta}\right) $$

      When linear_beta <= 0, up is directly passed through.

    4. Output:

      $$ y = situ\_a \cdot up $$
    5. The dimensions of output tensor y are the same as those of x, the size on the dim axis is half of that of x, and the other dimensions are the same as x.

Prototype

cann_ops_nn.situ_glu(
    x,
    *,
    dim=-1,
    beta=1.0,
    linear_beta=0.0,
    activate_left=True
) -> Tensor

Parameters

NameTypeRequired/OptionalDescriptionData TypeDimension (Shape)
xTensorRequiredSiTU input. The dim dimension is evenly split into the gate part and the up part.float16, float32, bfloat161 to 8 dimensions, and the size of the dim dimension is even.
dimintOptionalDimension along which the input is split. Value range: [-x.dim(), x.dim()-1]. Default: -1.--
betafloatOptionalScaling factor of the gate part in SiTU, which controls the intensity of the tanh nonlinearity. A non-zero positive value is recommended. Default: 1.0.--
linear_betafloatOptionalScaling factor for the linear tanh of the up path. If the value is greater than 0, a bounded transformation is applied to up; if the value is less than or equal to 0, up is passed through directly. Default: 0.0.--
activate_leftboolOptionalSpecifies whether gate is the first half when x is split. True indicates that gate is the first half and up is the second half; False indicates that gate is the second half and up is the first half. Default: True.--

Return Values

NameTypeDescriptionData TypeDimension (Shape)
yTensorSiTU activation result.Same as xSame as x, but the size of the dim dimension is x.shape[dim] // 2

Constraints

  • This API supports both training and inference use cases.
  • 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.

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()
    
    # Call with default parameters.
    y = cann_ops_nn.situ_glu(x)
    print(y.shape, y.dtype)  # torch.Size([2, 16]) torch.float32
    
    # Specify all parameters.
    y = cann_ops_nn.situ_glu(x, dim=-1, beta=1.0, linear_beta=0.0, activate_left=True)
    print(y.shape, y.dtype)
  • TorchAir graph mode call:

    import torch
    import torch_npu
    import torchair
    import cann_ops_nn
    
    class Model(torch.nn.Module):
        def forward(self, x):
            return cann_ops_nn.situ_glu(x, dim=-1, beta=1.0)
    
    model = torch.compile(Model().npu(), backend=torchair.get_npu_backend(), dynamic=False)
    x = torch.randn(2, 32, dtype=torch.float32).npu()
    y = model(x)