Update rmsnorm.py
Browse filesModifies the forward pass of RMSNorm to avoid mixed precision issues as described in https://github.com/chandar-lab/AMPLIFY/issues/19
5 unmodified lines6789101112131415161718192021222324252627282930313233345 unmodified linesdef __init__(self, dim: int, eps: float = 1e-6):"""Initialize the RMSNorm normalization layer.Args:dim (int): The dimension of the input tensor.eps (float, optional): A small value added to the denominator for numerical stability. Default is 1e-6.Attributes:eps (float): A small value added to the denominator for numerical stability.weight (nn.Parameter): Learnable scaling parameter."""super().__init__()self.eps = epsself.weight = nn.Parameter(torch.ones(dim))def forward(self, x):"""Forward pass through the RMSNorm layer.Args:x (torch.Tensor): The input tensor.Returns:torch.Tensor: The output tensor after applying RMSNorm."""return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.weight5 unmodified lines678910111213141516171819202122232425262728293031323334353637385 unmodified linesdef __init__(self, dim: int, eps: float = 1e-6):"""Initialize the RMSNorm normalization layer.Args:dim (int): The dimension of the input tensor.eps (float, optional): A small value added to the denominator for numerical stability. Default is 1e-6.Attributes:eps (float): A small value added to the denominator for numerical stability.weight (nn.Parameter): Learnable scaling parameter."""super().__init__()self.eps = epsself.weight = nn.Parameter(torch.ones(dim))def _norm(self, x):return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)def forward(self, x):"""Forward pass through the RMSNorm layer.Args:x (torch.Tensor): The input tensor.Returns:torch.Tensor: The output tensor after applying RMSNorm."""output = self._norm(x.float()).type_as(x) # Avoids mixed precision issues as in https://github.com/chandar-lab/AMPLIFY/issues/19return output * self.weight
rmsnorm.py+11-7