Update You_can_still_run_it_directly_on_your_own_machine_if_it's_too_small.ipynb
Browse files
You_can_still_run_it_directly_on_your_own_machine_if_it's_too_small.ipynb
CHANGED
|
@@ -3,21 +3,15 @@
|
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
"execution_count": null,
|
| 6 |
-
"metadata": {
|
| 7 |
-
"id": "OEKVU8aAXeQx"
|
| 8 |
-
},
|
| 9 |
"outputs": [],
|
| 10 |
-
"source": [
|
| 11 |
-
"REPO_ID = \"mondk/msh-tiny.For-Test-In-Note-Book-Google-Colab\"\n"
|
| 12 |
-
],
|
| 13 |
"id": "OEKVU8aAXeQx"
|
| 14 |
},
|
| 15 |
{
|
| 16 |
"cell_type": "code",
|
| 17 |
"execution_count": null,
|
| 18 |
-
"metadata": {
|
| 19 |
-
"id": "Muc0ftk9XeQz"
|
| 20 |
-
},
|
| 21 |
"outputs": [],
|
| 22 |
"source": [
|
| 23 |
"!pip install -q -U huggingface_hub tokenizers\n",
|
|
@@ -30,9 +24,7 @@
|
|
| 30 |
{
|
| 31 |
"cell_type": "code",
|
| 32 |
"execution_count": null,
|
| 33 |
-
"metadata": {
|
| 34 |
-
"id": "h5Lf-nDoXeQ0"
|
| 35 |
-
},
|
| 36 |
"outputs": [],
|
| 37 |
"source": [
|
| 38 |
"from huggingface_hub import hf_hub_download\n",
|
|
@@ -49,9 +41,7 @@
|
|
| 49 |
{
|
| 50 |
"cell_type": "code",
|
| 51 |
"execution_count": null,
|
| 52 |
-
"metadata": {
|
| 53 |
-
"id": "rhTG3mtsXeQ0"
|
| 54 |
-
},
|
| 55 |
"outputs": [],
|
| 56 |
"source": [
|
| 57 |
"import math\n",
|
|
@@ -123,7 +113,7 @@
|
|
| 123 |
" self.blocks = nn.ModuleList([Block(n_embd, n_head, block_size, dropout) for _ in range(n_layer)])\n",
|
| 124 |
" self.ln_f = nn.LayerNorm(n_embd)\n",
|
| 125 |
" self.head = nn.Linear(n_embd, vocab_size, bias=False)\n",
|
| 126 |
-
" self.head.weight = self.tok_emb.weight
|
| 127 |
"\n",
|
| 128 |
" def forward(self, idx, targets=None):\n",
|
| 129 |
" B, T = idx.shape\n",
|
|
@@ -158,9 +148,7 @@
|
|
| 158 |
{
|
| 159 |
"cell_type": "code",
|
| 160 |
"execution_count": null,
|
| 161 |
-
"metadata": {
|
| 162 |
-
"id": "PP6KcuhQXeQ1"
|
| 163 |
-
},
|
| 164 |
"outputs": [],
|
| 165 |
"source": [
|
| 166 |
"import json\n",
|
|
@@ -196,21 +184,13 @@
|
|
| 196 |
{
|
| 197 |
"cell_type": "code",
|
| 198 |
"execution_count": null,
|
| 199 |
-
"metadata": {
|
| 200 |
-
"id": "fix1chatFn"
|
| 201 |
-
},
|
| 202 |
"outputs": [],
|
| 203 |
"source": [
|
| 204 |
-
"# --- FIX: ham chat() bi thieu, day la ham duoc them lai ---\n",
|
| 205 |
-
"# Encode/decode qua tokenizers backend, va tu sinh token cho den khi\n",
|
| 206 |
-
"# gap END_ID (thay vi dung model.generate() goc, vi ham do khong biet\n",
|
| 207 |
-
"# dung lai o END_TOK va se tra ve ca prompt lan phan sinh ra).\n",
|
| 208 |
-
"\n",
|
| 209 |
"@torch.no_grad()\n",
|
| 210 |
"def chat(user_input, max_new_tokens=200, temperature=0.8, top_k=40):\n",
|
| 211 |
" prompt = f\"{USER_TOK}{user_input}{ASSISTANT_TOK}\"\n",
|
| 212 |
" prompt_ids = tokenizer_backend.encode(prompt).ids\n",
|
| 213 |
-
" # cat bot neu prompt dai hon block_size\n",
|
| 214 |
" prompt_ids = prompt_ids[-model.block_size:]\n",
|
| 215 |
" idx = torch.tensor([prompt_ids], dtype=torch.long, device=device)\n",
|
| 216 |
"\n",
|
|
@@ -239,9 +219,7 @@
|
|
| 239 |
{
|
| 240 |
"cell_type": "code",
|
| 241 |
"execution_count": null,
|
| 242 |
-
"metadata": {
|
| 243 |
-
"id": "u1CRQJL2XeQ2"
|
| 244 |
-
},
|
| 245 |
"outputs": [],
|
| 246 |
"source": [
|
| 247 |
"print(\"Chat started. Type 'exit' to quit.\\n\")\n",
|
|
@@ -258,18 +236,10 @@
|
|
| 258 |
],
|
| 259 |
"metadata": {
|
| 260 |
"accelerator": "GPU",
|
| 261 |
-
"colab": {
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
},
|
| 265 |
-
"kernelspec": {
|
| 266 |
-
"display_name": "Python 3",
|
| 267 |
-
"name": "python3"
|
| 268 |
-
},
|
| 269 |
-
"language_info": {
|
| 270 |
-
"name": "python"
|
| 271 |
-
}
|
| 272 |
},
|
| 273 |
"nbformat": 4,
|
| 274 |
"nbformat_minor": 5
|
| 275 |
-
}
|
|
|
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
"execution_count": null,
|
| 6 |
+
"metadata": {"id": "OEKVU8aAXeQx"},
|
|
|
|
|
|
|
| 7 |
"outputs": [],
|
| 8 |
+
"source": ["REPO_ID = \"mondk/msh-tiny.For-Test-In-Note-Book-Google-Colab\"\n"],
|
|
|
|
|
|
|
| 9 |
"id": "OEKVU8aAXeQx"
|
| 10 |
},
|
| 11 |
{
|
| 12 |
"cell_type": "code",
|
| 13 |
"execution_count": null,
|
| 14 |
+
"metadata": {"id": "Muc0ftk9XeQz"},
|
|
|
|
|
|
|
| 15 |
"outputs": [],
|
| 16 |
"source": [
|
| 17 |
"!pip install -q -U huggingface_hub tokenizers\n",
|
|
|
|
| 24 |
{
|
| 25 |
"cell_type": "code",
|
| 26 |
"execution_count": null,
|
| 27 |
+
"metadata": {"id": "h5Lf-nDoXeQ0"},
|
|
|
|
|
|
|
| 28 |
"outputs": [],
|
| 29 |
"source": [
|
| 30 |
"from huggingface_hub import hf_hub_download\n",
|
|
|
|
| 41 |
{
|
| 42 |
"cell_type": "code",
|
| 43 |
"execution_count": null,
|
| 44 |
+
"metadata": {"id": "rhTG3mtsXeQ0"},
|
|
|
|
|
|
|
| 45 |
"outputs": [],
|
| 46 |
"source": [
|
| 47 |
"import math\n",
|
|
|
|
| 113 |
" self.blocks = nn.ModuleList([Block(n_embd, n_head, block_size, dropout) for _ in range(n_layer)])\n",
|
| 114 |
" self.ln_f = nn.LayerNorm(n_embd)\n",
|
| 115 |
" self.head = nn.Linear(n_embd, vocab_size, bias=False)\n",
|
| 116 |
+
" self.head.weight = self.tok_emb.weight\n",
|
| 117 |
"\n",
|
| 118 |
" def forward(self, idx, targets=None):\n",
|
| 119 |
" B, T = idx.shape\n",
|
|
|
|
| 148 |
{
|
| 149 |
"cell_type": "code",
|
| 150 |
"execution_count": null,
|
| 151 |
+
"metadata": {"id": "PP6KcuhQXeQ1"},
|
|
|
|
|
|
|
| 152 |
"outputs": [],
|
| 153 |
"source": [
|
| 154 |
"import json\n",
|
|
|
|
| 184 |
{
|
| 185 |
"cell_type": "code",
|
| 186 |
"execution_count": null,
|
| 187 |
+
"metadata": {"id": "fix1chatFn"},
|
|
|
|
|
|
|
| 188 |
"outputs": [],
|
| 189 |
"source": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
"@torch.no_grad()\n",
|
| 191 |
"def chat(user_input, max_new_tokens=200, temperature=0.8, top_k=40):\n",
|
| 192 |
" prompt = f\"{USER_TOK}{user_input}{ASSISTANT_TOK}\"\n",
|
| 193 |
" prompt_ids = tokenizer_backend.encode(prompt).ids\n",
|
|
|
|
| 194 |
" prompt_ids = prompt_ids[-model.block_size:]\n",
|
| 195 |
" idx = torch.tensor([prompt_ids], dtype=torch.long, device=device)\n",
|
| 196 |
"\n",
|
|
|
|
| 219 |
{
|
| 220 |
"cell_type": "code",
|
| 221 |
"execution_count": null,
|
| 222 |
+
"metadata": {"id": "u1CRQJL2XeQ2"},
|
|
|
|
|
|
|
| 223 |
"outputs": [],
|
| 224 |
"source": [
|
| 225 |
"print(\"Chat started. Type 'exit' to quit.\\n\")\n",
|
|
|
|
| 236 |
],
|
| 237 |
"metadata": {
|
| 238 |
"accelerator": "GPU",
|
| 239 |
+
"colab": {"provenance": [], "gpuType": "T4"},
|
| 240 |
+
"kernelspec": {"display_name": "Python 3", "name": "python3"},
|
| 241 |
+
"language_info": {"name": "python"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
},
|
| 243 |
"nbformat": 4,
|
| 244 |
"nbformat_minor": 5
|
| 245 |
+
}
|