Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from mamba_ssm import Mamba2
|
| 5 |
+
from mamba_ssm.utils.generation import InferenceParams
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class Params():
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.SEED = 13
|
| 11 |
+
|
| 12 |
+
#Architecture
|
| 13 |
+
self.n_layers = 1
|
| 14 |
+
self.model_dim = 512
|
| 15 |
+
self.d_state = 256
|
| 16 |
+
self.mamba_expand = 2
|
| 17 |
+
self.d_conv = 4
|
| 18 |
+
self.matrix_div = 4
|
| 19 |
+
|
| 20 |
+
self.length = 1024
|
| 21 |
+
|
| 22 |
+
#Tokeniser
|
| 23 |
+
self.tok_vocab_n = 32000
|
| 24 |
+
self.tok_min_freq = 100
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class SwiGLU(nn.Module):
|
| 28 |
+
def __init__(self, dim):
|
| 29 |
+
super().__init__()
|
| 30 |
+
|
| 31 |
+
self.l0 = nn.Linear(dim, dim*2)
|
| 32 |
+
self.l1 = nn.Linear(dim, dim*2)
|
| 33 |
+
|
| 34 |
+
self.l2 = nn.Linear(dim*2, dim)
|
| 35 |
+
|
| 36 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 37 |
+
x = F.silu(self.l0(x)) * self.l1(x)
|
| 38 |
+
|
| 39 |
+
return self.l2(x)
|
| 40 |
+
|
| 41 |
+
class WeightGen(nn.Module):
|
| 42 |
+
def __init__(self, params: Params, shuffle: bool):
|
| 43 |
+
super().__init__()
|
| 44 |
+
self.div = params.matrix_div
|
| 45 |
+
self.chunk = params.model_dim//params.matrix_div
|
| 46 |
+
|
| 47 |
+
self.mlp = nn.Sequential(
|
| 48 |
+
SwiGLU(params.model_dim),
|
| 49 |
+
nn.RMSNorm(params.model_dim),
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
self.gen = nn.Linear(params.model_dim, (self.chunk**2) * self.div)
|
| 53 |
+
|
| 54 |
+
self.bias_head = nn.Linear(params.model_dim, params.model_dim)
|
| 55 |
+
|
| 56 |
+
self.shuffle = nn.Linear(params.model_dim, params.model_dim, bias=False) if shuffle else None
|
| 57 |
+
if self.shuffle is not None:
|
| 58 |
+
nn.init.eye_(self.shuffle.weight)
|
| 59 |
+
|
| 60 |
+
nn.init.zeros_(self.gen.bias)
|
| 61 |
+
nn.init.zeros_(self.bias_head.bias)
|
| 62 |
+
|
| 63 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 64 |
+
B, S, D = x.shape
|
| 65 |
+
|
| 66 |
+
gx = self.mlp(x)
|
| 67 |
+
dense = self.gen(gx).view((B, S, self.div, self.chunk, self.chunk))
|
| 68 |
+
|
| 69 |
+
x = x.view((B, S, self.div, self.chunk)).unsqueeze(-2)
|
| 70 |
+
x = (x @ dense).squeeze(-2)
|
| 71 |
+
x = x.view((B, S, D)) + self.bias_head(gx)
|
| 72 |
+
|
| 73 |
+
if self.shuffle is not None:
|
| 74 |
+
x = self.shuffle(x)
|
| 75 |
+
|
| 76 |
+
return x
|
| 77 |
+
|
| 78 |
+
class Bundle(nn.Module):
|
| 79 |
+
def __init__(self, params: Params, layer: int, shuffle: bool):
|
| 80 |
+
super().__init__()
|
| 81 |
+
|
| 82 |
+
self.norm = nn.RMSNorm(params.model_dim)
|
| 83 |
+
self.mamba = Mamba2(
|
| 84 |
+
d_model=params.model_dim,
|
| 85 |
+
d_state=params.d_state,
|
| 86 |
+
expand=params.mamba_expand,
|
| 87 |
+
d_conv=params.d_conv,
|
| 88 |
+
layer_idx=layer,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
self.alpha = nn.Parameter(torch.ones((params.model_dim)))
|
| 92 |
+
self.gen_norm = nn.RMSNorm(params.model_dim)
|
| 93 |
+
self.weight_gen = WeightGen(params, shuffle)
|
| 94 |
+
|
| 95 |
+
def forward(self, x: torch.Tensor, inf_params: InferenceParams | None = None) -> torch.Tensor:
|
| 96 |
+
x = x + self.mamba(self.norm(x), inference_params = inf_params)
|
| 97 |
+
x = x * self.alpha + self.weight_gen(self.gen_norm(x))
|
| 98 |
+
|
| 99 |
+
return x
|
| 100 |
+
|
| 101 |
+
class Block(nn.Module):
|
| 102 |
+
def __init__(self, params: Params, idx: int):
|
| 103 |
+
super().__init__()
|
| 104 |
+
self.params = params
|
| 105 |
+
|
| 106 |
+
self.steps = nn.ModuleList(
|
| 107 |
+
[Bundle(params, b + params.matrix_div * idx, b!=params.matrix_div-1) for b in range(params.matrix_div)]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
def forward(self, x: torch.Tensor, inf_params: InferenceParams | None = None) -> torch.Tensor:
|
| 111 |
+
for b in self.steps:
|
| 112 |
+
x = b(x, inf_params)
|
| 113 |
+
|
| 114 |
+
return x
|
| 115 |
+
|
| 116 |
+
class NeuralNetwork(nn.Module):
|
| 117 |
+
def __init__(self, params: Params):
|
| 118 |
+
super().__init__()
|
| 119 |
+
self.params = params
|
| 120 |
+
|
| 121 |
+
self.embedding = nn.Embedding(params.tok_vocab_n, params.model_dim)
|
| 122 |
+
nn.init.normal_(self.embedding.weight, mean=0.0, std=0.02)
|
| 123 |
+
|
| 124 |
+
self.emb_norm = nn.RMSNorm(params.model_dim)
|
| 125 |
+
|
| 126 |
+
self.layers = nn.ModuleList([
|
| 127 |
+
Block(params, b) for b in range(params.n_layers)
|
| 128 |
+
])
|
| 129 |
+
|
| 130 |
+
self.final_norm = nn.RMSNorm(params.model_dim)
|
| 131 |
+
|
| 132 |
+
def forward(self, x: torch.Tensor, inf_params: InferenceParams | None = None) -> torch.Tensor:
|
| 133 |
+
x = self.emb_norm(self.embedding(x))
|
| 134 |
+
|
| 135 |
+
for layer in self.layers:
|
| 136 |
+
x = layer(x, inf_params=inf_params)
|
| 137 |
+
|
| 138 |
+
x = self.final_norm(x)
|
| 139 |
+
|
| 140 |
+
out = F.linear(x, self.embedding.weight)
|
| 141 |
+
return out
|