Upload genesis_architecture.py with huggingface_hub
Browse files- genesis_architecture.py +45 -23
genesis_architecture.py
CHANGED
|
@@ -10,42 +10,61 @@ class GenesisConfig:
|
|
| 10 |
embed_dim: int = 768
|
| 11 |
num_layers: int = 12
|
| 12 |
num_heads: int = 12
|
| 13 |
-
kv_heads: int = 3
|
| 14 |
mlp_dim: int = 2560
|
|
|
|
|
|
|
| 15 |
tie_embeddings: bool = True
|
| 16 |
|
| 17 |
-
class
|
| 18 |
def __init__(self, cfg):
|
| 19 |
super().__init__()
|
| 20 |
-
self.nh = cfg.num_heads
|
| 21 |
self.hd = cfg.embed_dim // cfg.num_heads
|
| 22 |
-
self.
|
| 23 |
-
self.
|
| 24 |
-
self.k = nn.Linear(cfg.embed_dim, cfg.kv_heads * self.hd, bias=False)
|
| 25 |
-
self.v = nn.Linear(cfg.embed_dim, cfg.kv_heads * self.hd, bias=False)
|
| 26 |
-
self.o = nn.Linear(cfg.embed_dim, cfg.embed_dim, bias=False)
|
| 27 |
def forward(self, x):
|
| 28 |
-
B, T, C = x.shape
|
| 29 |
-
q = self.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return self.o(y.transpose(1, 2).contiguous().view(B, T, C))
|
| 34 |
|
| 35 |
-
class
|
| 36 |
-
def __init__(self,
|
| 37 |
super().__init__()
|
| 38 |
-
self.fc1 = nn.Linear(
|
| 39 |
-
self.fc2 = nn.Linear(
|
| 40 |
def forward(self, x): return self.fc2(F.gelu(self.fc1(x), approximate="tanh"))
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
class _Block(nn.Module):
|
| 43 |
def __init__(self, cfg):
|
| 44 |
super().__init__()
|
| 45 |
-
self.ln1 = nn.LayerNorm(cfg.embed_dim); self.attn =
|
| 46 |
-
self.ln2 = nn.LayerNorm(cfg.embed_dim); self.
|
| 47 |
def forward(self, x):
|
| 48 |
-
x = x + self.attn(self.ln1(x))
|
|
|
|
|
|
|
| 49 |
|
| 50 |
class GenesisLM(nn.Module):
|
| 51 |
def __init__(self, cfg):
|
|
@@ -65,5 +84,8 @@ class GenesisLM(nn.Module):
|
|
| 65 |
def forward(self, ids):
|
| 66 |
B, T = ids.shape
|
| 67 |
x = self.embed(ids) + self.pos_emb(torch.arange(T, device=ids.device))
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
embed_dim: int = 768
|
| 11 |
num_layers: int = 12
|
| 12 |
num_heads: int = 12
|
|
|
|
| 13 |
mlp_dim: int = 2560
|
| 14 |
+
n_experts: int = 4
|
| 15 |
+
top_k: int = 2
|
| 16 |
tie_embeddings: bool = True
|
| 17 |
|
| 18 |
+
class _Attn(nn.Module):
|
| 19 |
def __init__(self, cfg):
|
| 20 |
super().__init__()
|
| 21 |
+
self.nh = cfg.num_heads
|
| 22 |
self.hd = cfg.embed_dim // cfg.num_heads
|
| 23 |
+
self.qkv = nn.Linear(cfg.embed_dim, 3 * cfg.embed_dim, bias=False)
|
| 24 |
+
self.proj = nn.Linear(cfg.embed_dim, cfg.embed_dim, bias=False)
|
|
|
|
|
|
|
|
|
|
| 25 |
def forward(self, x):
|
| 26 |
+
B, T, C = x.shape
|
| 27 |
+
q, k, v = self.qkv(x).split(C, dim=-1)
|
| 28 |
+
def _s(t): return t.view(B, T, self.nh, self.hd).transpose(1, 2)
|
| 29 |
+
y = F.scaled_dot_product_attention(_s(q), _s(k), _s(v), is_causal=True)
|
| 30 |
+
return self.proj(y.transpose(1, 2).contiguous().view(B, T, C))
|
|
|
|
| 31 |
|
| 32 |
+
class _Expert(nn.Module):
|
| 33 |
+
def __init__(self, d, ffn):
|
| 34 |
super().__init__()
|
| 35 |
+
self.fc1 = nn.Linear(d, ffn, bias=False)
|
| 36 |
+
self.fc2 = nn.Linear(ffn, d, bias=False)
|
| 37 |
def forward(self, x): return self.fc2(F.gelu(self.fc1(x), approximate="tanh"))
|
| 38 |
|
| 39 |
+
class _MoE(nn.Module):
|
| 40 |
+
def __init__(self, cfg):
|
| 41 |
+
super().__init__()
|
| 42 |
+
self.ne = cfg.n_experts; self.tk = cfg.top_k
|
| 43 |
+
self.experts = nn.ModuleList([_Expert(cfg.embed_dim, cfg.mlp_dim) for _ in range(cfg.n_experts)])
|
| 44 |
+
self.gate = nn.Linear(cfg.embed_dim, cfg.n_experts, bias=False)
|
| 45 |
+
def forward(self, x):
|
| 46 |
+
B, T, C = x.shape; flat = x.view(-1, C)
|
| 47 |
+
probs = torch.softmax(self.gate(flat), dim=-1)
|
| 48 |
+
top_v, top_i = torch.topk(probs, self.tk, dim=-1)
|
| 49 |
+
top_v = top_v / (top_v.sum(-1, keepdim=True) + 1e-6)
|
| 50 |
+
out = torch.zeros_like(flat)
|
| 51 |
+
for i, expert in enumerate(self.experts):
|
| 52 |
+
mask = (top_i == i).any(-1)
|
| 53 |
+
if mask.any():
|
| 54 |
+
w = torch.where(top_i == i, top_v, torch.zeros_like(top_v)).sum(-1)
|
| 55 |
+
out[mask] += w[mask].unsqueeze(-1) * expert(flat[mask])
|
| 56 |
+
aux = self.ne * (probs.mean(0) ** 2).sum()
|
| 57 |
+
return out.view(B, T, C), aux
|
| 58 |
+
|
| 59 |
class _Block(nn.Module):
|
| 60 |
def __init__(self, cfg):
|
| 61 |
super().__init__()
|
| 62 |
+
self.ln1 = nn.LayerNorm(cfg.embed_dim); self.attn = _Attn(cfg)
|
| 63 |
+
self.ln2 = nn.LayerNorm(cfg.embed_dim); self.moe = _MoE(cfg)
|
| 64 |
def forward(self, x):
|
| 65 |
+
x = x + self.attn(self.ln1(x))
|
| 66 |
+
moe_out, aux = self.moe(self.ln2(x))
|
| 67 |
+
return x + moe_out, aux
|
| 68 |
|
| 69 |
class GenesisLM(nn.Module):
|
| 70 |
def __init__(self, cfg):
|
|
|
|
| 84 |
def forward(self, ids):
|
| 85 |
B, T = ids.shape
|
| 86 |
x = self.embed(ids) + self.pos_emb(torch.arange(T, device=ids.device))
|
| 87 |
+
total_aux = torch.zeros(1, device=ids.device)
|
| 88 |
+
for blk in self.layers:
|
| 89 |
+
x, aux = blk(x)
|
| 90 |
+
total_aux = total_aux + aux
|
| 91 |
+
return self.lm_head(self.norm(x)), total_aux / self.cfg.num_layers
|