Update normalizer.py
Browse files- normalizer.py +13 -35
normalizer.py
CHANGED
|
@@ -1,40 +1,31 @@
|
|
| 1 |
import torch
|
| 2 |
from torch import nn
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
class GatingUnit(nn.Module):
|
| 8 |
-
def __init__(self,dim):
|
| 9 |
super().__init__()
|
| 10 |
-
self.proj_1 = nn.Linear(dim,dim)
|
| 11 |
-
self.proj_2 = nn.Linear(dim,dim)
|
| 12 |
-
self.proj_3 = nn.Linear(dim,dim)
|
| 13 |
self.silu = nn.SiLU()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
def forward(self, x):
|
| 17 |
u, v = x, x
|
| 18 |
u = self.proj_1(u)
|
| 19 |
u = self.silu(u)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
v = self.proj_2(v)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
g = u * v
|
| 26 |
g = self.proj_3(g)
|
| 27 |
|
| 28 |
out = g
|
| 29 |
return out
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
class NormalizerBlock(nn.Module):
|
| 34 |
def __init__(self, d_model, num_tokens):
|
| 35 |
super().__init__()
|
| 36 |
-
|
| 37 |
-
|
| 38 |
self.norm_global = nn.LayerNorm(d_model * num_tokens, elementwise_affine=False)
|
| 39 |
self.norm_local = nn.LayerNorm(d_model, elementwise_affine=False)
|
| 40 |
self.gating = GatingUnit(d_model)
|
|
@@ -51,37 +42,24 @@ class NormalizerBlock(nn.Module):
|
|
| 51 |
|
| 52 |
x = x.reshape([dim0,dim1,dim2])
|
| 53 |
x = x + residual
|
| 54 |
-
|
| 55 |
-
|
| 56 |
residual = x
|
| 57 |
-
|
| 58 |
-
|
| 59 |
x = self.norm_local(x)
|
| 60 |
x = self.gating(x)
|
| 61 |
|
| 62 |
out = x + residual
|
| 63 |
-
|
| 64 |
-
|
| 65 |
return out
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
class Normalizer(nn.Module):
|
| 70 |
-
def __init__(self, d_model,num_tokens, num_layers):
|
| 71 |
super().__init__()
|
| 72 |
|
| 73 |
self.model = nn.Sequential(
|
| 74 |
-
*[NormalizerBlock(d_model,num_tokens) for _ in range(num_layers)]
|
| 75 |
)
|
| 76 |
|
| 77 |
def forward(self, x):
|
| 78 |
|
| 79 |
return self.model(x)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 1 |
import torch
|
| 2 |
from torch import nn
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
class GatingUnit(nn.Module):
|
| 5 |
+
def __init__(self, dim):
|
| 6 |
super().__init__()
|
| 7 |
+
self.proj_1 = nn.Linear(dim, dim)
|
| 8 |
+
self.proj_2 = nn.Linear(dim, dim)
|
| 9 |
+
self.proj_3 = nn.Linear(dim, dim)
|
| 10 |
self.silu = nn.SiLU()
|
| 11 |
+
|
|
|
|
| 12 |
def forward(self, x):
|
| 13 |
u, v = x, x
|
| 14 |
u = self.proj_1(u)
|
| 15 |
u = self.silu(u)
|
| 16 |
+
|
|
|
|
| 17 |
v = self.proj_2(v)
|
| 18 |
+
|
|
|
|
| 19 |
g = u * v
|
| 20 |
g = self.proj_3(g)
|
| 21 |
|
| 22 |
out = g
|
| 23 |
return out
|
| 24 |
|
|
|
|
|
|
|
| 25 |
class NormalizerBlock(nn.Module):
|
| 26 |
def __init__(self, d_model, num_tokens):
|
| 27 |
super().__init__()
|
| 28 |
+
|
|
|
|
| 29 |
self.norm_global = nn.LayerNorm(d_model * num_tokens, elementwise_affine=False)
|
| 30 |
self.norm_local = nn.LayerNorm(d_model, elementwise_affine=False)
|
| 31 |
self.gating = GatingUnit(d_model)
|
|
|
|
| 42 |
|
| 43 |
x = x.reshape([dim0,dim1,dim2])
|
| 44 |
x = x + residual
|
| 45 |
+
|
|
|
|
| 46 |
residual = x
|
| 47 |
+
|
|
|
|
| 48 |
x = self.norm_local(x)
|
| 49 |
x = self.gating(x)
|
| 50 |
|
| 51 |
out = x + residual
|
| 52 |
+
|
|
|
|
| 53 |
return out
|
| 54 |
|
|
|
|
|
|
|
| 55 |
class Normalizer(nn.Module):
|
| 56 |
+
def __init__(self, d_model, num_tokens, num_layers):
|
| 57 |
super().__init__()
|
| 58 |
|
| 59 |
self.model = nn.Sequential(
|
| 60 |
+
*[NormalizerBlock(d_model, num_tokens) for _ in range(num_layers)]
|
| 61 |
)
|
| 62 |
|
| 63 |
def forward(self, x):
|
| 64 |
|
| 65 |
return self.model(x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|