| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Inkling RMSNorm (no bias, weight-scaled), backed by the vendored Triton kernel.""" |
| 4 | |
| 5 | from __future__ import annotations |
| 6 | |
| 7 | import torch |
| 8 | from torch import nn |
| 9 | |
| 10 | from .ops import rmsnorm |
| 11 | |
| 12 | |
| 13 | class InklingRMSNorm(nn.Module): |
| 14 | def __init__(self, hidden_size: int, eps: float = 1e-6) -> None: |
| 15 | super().__init__() |
| 16 | self.weight = nn.Parameter(torch.ones(hidden_size)) |
| 17 | self.variance_epsilon = eps |
| 18 | self.hidden_size = hidden_size |
| 19 | |
| 20 | def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 21 | if x.numel() == 0: |
| 22 | return x |
| 23 | original_shape = x.shape |
| 24 | x_2d = x.contiguous().view(-1, self.hidden_size) |
| 25 | y = rmsnorm(x_2d, self.weight, self.variance_epsilon) |
| 26 | return y.view(original_shape) |