0xZohar commited on
Commit
1e1d4d1
·
verified ·
1 Parent(s): a3e82d1

Add code/cube3d/model/transformers/norm.py

Browse files
code/cube3d/model/transformers/norm.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+
5
+ def fused_rms_norm(x: torch.Tensor, weight: nn.Parameter, eps: float):
6
+ """
7
+ Applies a fused Root Mean Square (RMS) normalization to the input tensor.
8
+ Args:
9
+ x (torch.Tensor): The input tensor to be normalized. Expected to have
10
+ at least one dimension.
11
+ weight (nn.Parameter): A learnable parameter used to scale the normalized
12
+ tensor. Its shape must be broadcastable to the shape of `x`.
13
+ eps (float): A small constant added to the denominator for numerical
14
+ stability during normalization.
15
+ Returns:
16
+ torch.Tensor: The normalized and scaled tensor with the same shape as `x`.
17
+ """
18
+
19
+ x = x.float()
20
+ return (x * torch.rsqrt((x * x).mean(-1, keepdim=True).add_(eps))) * weight
21
+
22
+
23
+ class LayerNorm(nn.LayerNorm):
24
+ def forward(self, input: torch.Tensor):
25
+ """
26
+ Wrapper to ensure that the input tensor is cast to float before normalization.
27
+ """
28
+ y = super().forward(input.float())
29
+ return y.type_as(input)
30
+
31
+
32
+ class RMSNorm(nn.Module):
33
+ def __init__(self, dim: int, eps: float = 1e-5, elementwise_affine: bool = True):
34
+ """
35
+ Initializes the normalization layer.
36
+ Args:
37
+ dim (int): The number of features in the input tensor.
38
+ eps (float, optional): A small value added to the denominator for numerical stability. Defaults to 1e-5.
39
+ elementwise_affine (bool, optional): If True, this layer will have learnable per-element affine parameters. Defaults to True.
40
+ """
41
+ super().__init__()
42
+ self.eps = eps
43
+ self.weight = nn.Parameter(torch.ones(dim), requires_grad=elementwise_affine)
44
+
45
+ def forward(self, x):
46
+ return fused_rms_norm(x, weight=self.weight, eps=self.eps).type_as(x)