| 2410 | |
| 2411 | |
| 2412 | class RMSNormalize(nn.Module): |
| 2413 | |
| 2414 | def __init__(self, dim=None, dtype=torch.float, eps=1e-5, weight=None): |
| 2415 | super(RMSNormalize, self).__init__() |
| 2416 | if weight is not None: |
| 2417 | self.weight = weight |
| 2418 | else: |
| 2419 | self.weight = nn.Parameter(torch.ones(dim, dtype=dtype, device=get_accelerator().current_device_name())) |
| 2420 | |
| 2421 | self.eps = eps |
| 2422 | |
| 2423 | def forward(self, hidden_states): |
| 2424 | variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True) |
| 2425 | hidden_states = hidden_states * torch.rsqrt(variance + self.eps) |
| 2426 | if self.weight.dtype in [torch.float16, torch.bfloat16]: |
| 2427 | hidden_states = hidden_states.to(self.weight.dtype) |
| 2428 | |
| 2429 | return hidden_states * self.weight |
no outgoing calls
no test coverage detected