swiglu_group
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description:
Performs SwiGLU activation on input
x. Supports optional per-token weights and count mode grouping. The underlying implementation encapsulatesaclnnSwigluGroup.Formula:
Let
Dbe the size of the last dimension of inputx,A = x[..., :D/2], andB = x[..., D/2:].$$ y = silu(A) \times B = \frac{A}{1 + e^{-A}} \times B $$When
clamp_limit > 0,AandBare clamped first. Whenweightis not empty, the result is multiplied byweighttoken by token.
Prototype
cann_ops_nn.swiglu_group(
x,
*,
weight=None,
group_index=None,
clamp_limit=-1.0,
) -> TensorParameters
| Name | Type | Required/Optional | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|---|
x | Tensor | Required | SwiGLU input. The last dimension is evenly split into two parts. | torch.float16, torch.bfloat16, torch.float32 | 1-8 dimensions, with the last dimension being an even number |
weight | Tensor | Optional | Per-token weight. When not empty, it is multiplied to the SwiGLU result. | torch.float32 | 1-8 dimensions, with the number of elements equal to that of x excluding the last dimension |
group_index | Tensor | Optional | Number of tokens grouped in count mode. | torch.int64 | 1 dimension |
clamp_limit | float | Optional | Clamps A and B before activation. The default value is -1.0, indicating that no clamping is performed. If clamping is enabled, the value must be greater than 0. | - | - |
Return Values
| Name | Type | Description | Data Type | Dimension (Shape) |
|---|---|---|---|---|
y | Tensor | SwiGLU activation result. | Same as x | Last dimension is x.shape[-1] // 2, and other dimensions are the same as x. |
Constraints
- This API can be called in single-operator mode and TorchAir graph mode.
x,weight, andgroup_indexmust be NPU tensors; optional tensors can be set toNone.- The last dimension of
xmust be even. xandweightsupport empty tensors, but an empty group_index alone is not supported.- Non-contiguous tensors are not supported.
group_indexindicates the number of tokens in each group. All elements must be greater than or equal to 0.
Deterministic Computation
Defaults to a deterministic implementation.
Example
Single-operator mode call:
import torch import torch_npu import cann_ops_nn.ops x = torch.randn(8, 512, dtype=torch.float16).npu() weight = torch.ones(8, dtype=torch.float32).npu() group_index = torch.tensor([4, 4], dtype=torch.int64).npu() y = torch.ops.cann_ops_nn.swiglu_group( x, weight=weight, group_index=group_index, clamp_limit=-1.0) print(y.shape, y.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, x): return torch.ops.cann_ops_nn.swiglu_group(x, clamp_limit=-1.0) model = torch.compile(Model().npu(), backend=torchair.get_npu_backend(), dynamic=False) x = torch.randn(8, 512, dtype=torch.float16).npu() y = model(x)