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
xalong the specified dimensiondiminto two halves: gate and up, and computes the output using the SiTU formula. The bottom layer encapsulatesaclnnSituGlu.Formula:
For a given input tensor
x, compute the SiTU outputyas follows:Merge the axes based on
dimso that the shape after merging is[pre, cut], wherecutmust be even. Leth = cut // 2.Split
xinto the front and rear parts based onactivate_left:activate_left=True(default):gate = x[..., :h],up = x[..., h:]activate_left=False:gate = x[..., h:],up = x[..., :h]
Perform the SiTU computation based on
betaandlinear_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,upis directly passed through.Output:
$$ y = situ\_a \cdot up $$The dimensions of output tensor
yare the same as those ofx, the size on thedimaxis is half of that ofx, and the other dimensions are the same asx.
Prototype
cann_ops_nn.situ_glu(
x,
*,
dim=-1,
beta=1.0,
linear_beta=0.0,
activate_left=True
) -> TensorParameters
| Name | Type | Required/Optional | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|---|
x | Tensor | Required | SiTU input. The dim dimension is evenly split into the gate part and the up part. | float16, float32, bfloat16 | 1 to 8 dimensions, and the size of the dim dimension is even. |
dim | int | Optional | Dimension along which the input is split. Value range: [-x.dim(), x.dim()-1]. Default: -1. | - | - |
beta | float | Optional | Scaling 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_beta | float | Optional | Scaling 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_left | bool | Optional | Specifies 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
| Name | Type | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|
y | Tensor | SiTU activation result. | Same as x | Same 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
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.
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)