| 113 | |
| 114 | |
| 115 | class RMSNormalize(nn.Module): |
| 116 | |
| 117 | def __init__(self, dim=None, dtype=torch.float, eps=1e-5, weight=None): |
| 118 | super(RMSNormalize, self).__init__() |
| 119 | if weight is not None: |
| 120 | self.weight = weight |
| 121 | else: |
| 122 | self.weight = nn.Parameter(torch.ones(dim, dtype=dtype, device=get_accelerator().current_device_name())) |
| 123 | |
| 124 | self.eps = eps |
| 125 | |
| 126 | def forward(self, hidden_states): |
| 127 | variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True) |
| 128 | hidden_states = hidden_states * torch.rsqrt(variance + self.eps) |
| 129 | print(self.weight) |
| 130 | if self.weight.dtype in [torch.float16, torch.bfloat16]: |
| 131 | hidden_states = hidden_states.to(self.weight.dtype) |
| 132 | |
| 133 | return hidden_states * self.weight |
no outgoing calls
no test coverage detected