#014

gated_rms_norm

bf16 sglang · both · sglang.srt.layers.attention.fla.layernorm_gated · importance 0.5%

Reference Implementation

reference.py
import torch
import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):

    def __init__(self, eps: float=1e-06, norm_before_gate: bool=True, activation: str='silu') -> None:
        super().__init__()
        self.eps = float(eps)
        self.norm_before_gate = bool(norm_before_gate)
        self.activation = activation

    def forward(self, x: torch.Tensor, weight: torch.Tensor, z: torch.Tensor) -> torch.Tensor:
        dtype = x.dtype
        x_f = x.float()
        z_f = z.float()
        if not self.norm_before_gate:
            x_f = x_f * self._activate(z_f)
        rstd = torch.rsqrt(x_f.square().mean(dim=-1, keepdim=True) + self.eps)
        out = x_f * rstd * weight.float()
        if self.norm_before_gate:
            out = out * self._activate(z_f)
        return out.to(dtype)

    def _activate(self, z: torch.Tensor) -> torch.Tensor:
        if self.activation in ('silu', 'swish'):
            return F.silu(z)
        if self.activation == 'sigmoid':
            return torch.sigmoid(z)
        raise ValueError(f'unsupported activation: {self.activation}')

Shapes

TSOL hardware:
# rowshidden_size TSOL(XPU-A)TProdS
0 352864128 51.13 us 248.70 us 20.6%
1 192960128 27.96 us 141.10 us 19.8%
2 191456128 27.74 us 140.00 us 19.8%
3 161376128 23.38 us 119.70 us 19.5%
4 159872128 23.17 us 118.70 us 19.5%
5 139616128 20.23 us 104.40 us 19.4%
6 134240128 19.45 us 100.00 us 19.4%

Input Generation

input.py
import torch

def _make_inputs(rows: int, hidden_size: int) -> dict[str, torch.Tensor]:
    x = torch.randn(rows, hidden_size, dtype=torch.bfloat16, device='cuda') * 0.02
    weight = torch.randn(hidden_size, dtype=torch.bfloat16, device='cuda') * 0.02 + 1.0
    z = torch.randn_like(x)
    return {'x': x, 'weight': weight, 'z': z}