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 encapsulates aclnnSwigluGroup.

  • Formula:

    Let D be the size of the last dimension of input x, A = x[..., :D/2], and B = x[..., D/2:].

    $$ y = silu(A) \times B = \frac{A}{1 + e^{-A}} \times B $$

    When clamp_limit > 0, A and B are clamped first. When weight is not empty, the result is multiplied by weight token by token.

Prototype

cann_ops_nn.swiglu_group(
    x,
    *,
    weight=None,
    group_index=None,
    clamp_limit=-1.0,
) -> Tensor

Parameters

NameTypeRequired/OptionalDescriptionData TypeDimension (Shape)
xTensorRequiredSwiGLU input. The last dimension is evenly split into two parts.torch.float16, torch.bfloat16, torch.float321-8 dimensions, with the last dimension being an even number
weightTensorOptionalPer-token weight. When not empty, it is multiplied to the SwiGLU result.torch.float321-8 dimensions, with the number of elements equal to that of x excluding the last dimension
group_indexTensorOptionalNumber of tokens grouped in count mode.torch.int641 dimension
clamp_limitfloatOptionalClamps 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

NameTypeDescriptionData TypeDimension (Shape)
yTensorSwiGLU activation result.Same as xLast 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, and group_index must be NPU tensors; optional tensors can be set to None.
  • The last dimension of x must be even.
  • x and weight support empty tensors, but an empty group_index alone is not supported.
  • Non-contiguous tensors are not supported.
  • group_index indicates 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)