-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefs.py
More file actions
29 lines (22 loc) · 789 Bytes
/
Copy pathrefs.py
File metadata and controls
29 lines (22 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import torch
def layernorm_with_affine(
x: torch.Tensor,
gamma: torch.Tensor,
beta: torch.Tensor,
eps: float = 1e-5,
) -> torch.Tensor:
mean = x.mean(dim=-1, keepdim=True)
var = ((x - mean) ** 2).mean(dim=-1, keepdim=True)
x_hat = (x - mean) / torch.sqrt(var + eps)
return x_hat * gamma + beta
def build_official_impl(hidden: int, dtype: torch.dtype, device: torch.device):
layer = torch.nn.LayerNorm(hidden, eps=1e-5, elementwise_affine=True).to(
device=device, dtype=dtype
)
def forward(x: torch.Tensor, gamma: torch.Tensor, beta: torch.Tensor, eps: float):
layer.eps = eps
with torch.no_grad():
layer.weight.copy_(gamma)
layer.bias.copy_(beta)
return layer(x)
return forward