Spaces:
Sleeping
Sleeping
Download hf_src/layers/rms_norm.py from OK-AI/ViT-Patch-PCA-Visualisation: direct link, hf CLI and curl.
- Browser
- Download file 745 Bytes
-
https://huggingface.co/spaces/OK-AI/ViT-Patch-PCA-Visualisation/resolve/main/hf_src/layers/rms_norm.py
- Command line
-
hf download hf://spaces/OK-AI/ViT-Patch-PCA-Visualisation/hf_src/layers/rms_norm.py
-
curl -L -o rms_norm.py https://huggingface.co/spaces/OK-AI/ViT-Patch-PCA-Visualisation/resolve/main/hf_src/layers/rms_norm.py
745 Bytes
| # Copyright (c) Meta Platforms, Inc. and affiliates. | |
| # | |
| # This software may be used and distributed in accordance with | |
| # the terms of the DINOv3 License Agreement. | |
| import torch | |
| from torch import Tensor, nn | |
| class RMSNorm(nn.Module): | |
| def __init__(self, dim: int, eps: float = 1e-5): | |
| super().__init__() | |
| self.weight = nn.Parameter(torch.ones(dim)) | |
| self.eps = eps | |
| def reset_parameters(self) -> None: | |
| nn.init.constant_(self.weight, 1) | |
| def _norm(self, x: Tensor) -> Tensor: | |
| return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) | |
| def forward(self, x: Tensor) -> Tensor: | |
| output = self._norm(x.float()).type_as(x) | |
| return output * self.weight | |