File size: 4,643 Bytes
c9fc3d0 |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"from torch import Tensor\n",
"import random\n",
"from tqdm.auto import tqdm\n",
"from mamba_ssm.modules.mamba_simple import Mamba\n",
"\n",
"def model_numel(m: nn.Module):\n",
" return sum(p.numel() for p in m.parameters())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"raw_txt = Path(\"../shake.txt\").read_text()\n",
"total_len = len(raw_txt)\n",
"aux_len = int(total_len * 0.05)\n",
"\n",
"head_txt, test_txt = raw_txt[:-aux_len], raw_txt[-aux_len:]\n",
"train_txt, valid_txt = head_txt[:-aux_len], head_txt[-aux_len:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(train_txt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from mambabit import string_to_bits, bits_to_string\n",
"\n",
"train_ds = string_to_bits(train_txt)\n",
"valid_ds = string_to_bits(valid_txt)\n",
"test_ds = string_to_bits(test_txt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def random_batches(split: Tensor, n_batch: int, bs: int):\n",
" assert bs % 8 == 0, \"have mercy\"\n",
" max_allowed_pos = len(split) // 8 - bs // 8\n",
"\n",
" values = []\n",
" for i in range(n_batch):\n",
" pos = random.randint(0, max_allowed_pos)\n",
" values.append(split[pos*8: pos*8+bs])\n",
" return torch.stack(values).cuda()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from mambabit import dim_model, n_vocab, n_layers, MambaBit"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mamba_bit = MambaBit().cuda()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if True:\n",
" mamba_bit.load_state_dict(torch.load(\"mamba_bit.bin\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def train(m: nn.Module, \n",
" n_epoch: int = 100, \n",
" n_batch: int = 4, \n",
" bs: int = 256):\n",
" opt = torch.optim.AdamW(m.parameters(), lr=0.0001, fused=True)\n",
"\n",
" for e in (bar := tqdm(range(n_epoch))): \n",
" b = random_batches(train_ds, n_batch, bs)\n",
"\n",
" y_pred = m(b)\n",
" y_pred = y_pred[:, :-1].reshape(-1, n_vocab)\n",
" y_true = b[:, 1:].ravel()\n",
"\n",
" loss = F.cross_entropy(y_pred,y_true)\n",
" loss.backward()\n",
" opt.step()\n",
" opt.zero_grad()\n",
" \n",
" l = loss.item()\n",
" bar.set_description(f\"L:{l:.10f}\")\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if True:\n",
" train(mamba_bit, 5000, 9, 8*128)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"torch.save(mamba_bit.state_dict(), \"mamba_bit.bin\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TEST\n",
"@torch.no_grad()\n",
"def test(prompt: str, chars=10):\n",
" x0 = decode_bits(prompt).cuda()[None]\n",
" x = x0.clone()\n",
" process = chars * 8\n",
" for _ in tqdm(range(process)):\n",
" y = mamba_bit(x)\n",
" new = y[:, -1:].argmax(-1)\n",
" x = torch.cat((x, new), 1) \n",
" return encode_bits(x)\n",
"\n",
" \n",
"print(test(\"FIRST CIT\", chars=10))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "sd",
"language": "python",
"name": "sd"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|