| 42 | |
| 43 | |
| 44 | class T5LayerNorm(nn.Module): |
| 45 | def __init__(self, dim, eps=1e-6): |
| 46 | super(T5LayerNorm, self).__init__() |
| 47 | self.dim = dim |
| 48 | self.eps = eps |
| 49 | self.weight = nn.Parameter(torch.ones(dim)) |
| 50 | |
| 51 | def forward(self, x): |
| 52 | x = x * torch.rsqrt(x.float().pow(2).mean(dim=-1, keepdim=True) + |
| 53 | self.eps) |
| 54 | if self.weight.dtype in [torch.float16, torch.bfloat16]: |
| 55 | x = x.type_as(self.weight) |
| 56 | return self.weight * x |
| 57 | |
| 58 | |
| 59 | class T5Attention(nn.Module): |