| 20 | |
| 21 | |
| 22 | class T5LayerNorm(nn.Module): |
| 23 | |
| 24 | def __init__(self, dim, eps=1e-6): |
| 25 | super(T5LayerNorm, self).__init__() |
| 26 | self.dim = dim |
| 27 | self.eps = eps |
| 28 | self.weight = nn.Parameter(torch.ones(dim)) |
| 29 | |
| 30 | def forward(self, x): |
| 31 | x = x * torch.rsqrt(x.float().pow(2).mean(dim=-1, keepdim=True) + |
| 32 | self.eps) |
| 33 | if self.weight.dtype in [torch.float16, torch.bfloat16]: |
| 34 | x = x.type_as(self.weight) |
| 35 | return self.weight * x |
| 36 | |
| 37 | |
| 38 | class T5Attention(nn.Module): |