mondk commited on
Commit
dab8ab1
·
verified ·
1 Parent(s): 54c23b2

Upload 7 files

Browse files
LICENSE CHANGED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 1. Grant of Rights
2
+ The Author grants you a perpetual, worldwide, royalty-free right to use, copy, modify, merge, distribute, sublicense, and commercially exploit this source code, including any directly related patents owned or licensable by the Author.
3
+
4
+ 2. Redistribution
5
+ When sharing or reselling the source code (whether unmodified or modified), you must include a copy of this License and retain the Author's original copyright notice.
6
+
7
+ 3. Limitations & Protection
8
+
9
+ Trademarks: This License does not grant any rights to use the Author's name, logo, or trademarks for marketing or commercial purposes.
10
+
11
+ Patents: The patent rights granted in Section 1 will automatically terminate if you initiate legal proceedings against the Author alleging patent infringement related to this source code.
12
+
13
+ 4. Disclaimer
14
+ The source code is provided on an "AS IS" basis, without warranty of any kind. The Author assumes no liability or legal responsibility for any claims, losses, or damages arising from the use of the source code. Any rights not expressly restricted in this License remain at your sole discretion.
README (1).md ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ language:
4
+ - en
5
+ tags:
6
+ - msh
7
+ - ggcolab
8
+ ---
9
+ hi guys, im lazy to write, so this was written by claude, ty
10
+
11
+ This is my MSH-Tiny model before the redesign (you could say it's the original version, lol)
12
+
13
+ ## Files
14
+
15
+ - `config.json` — architecture + hyperparameters
16
+ - `model_weights.pt` — trained PyTorch weights
17
+ - `tokenizer.json` — custom BPE tokenizer
18
+
19
+ ## How to run it
20
+
21
+ Use the included notebook **`You_can_still_run_it_directly_on_your_own_machine_if_it's_too_small.ipynb`** — open it in Google Colab, run all cells.
22
+
23
+ Looking for a ready-to-use format instead?
24
+ - `.safetensors`: [`mondk/Safetensors.msh-tiny`](https://huggingface.co/mondk/Safetensors.msh-tiny)
25
+ - `.gguf`: [`mondk/GGUF.msh-tiny`](https://huggingface.co/mondk/GGUF.msh-tiny)
26
+
27
+ ---
28
+
29
+ my license: https://huggingface.co/mondk/see_upcoming_models/blob/main/LICENSE
You_can_still_run_it_directly_on_your_own_machine_if_it%27s_too_small.ipynb ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
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\" # <-- CHANGE THIS to the actual Hugging Face repo, e.g. \"john/my-chat-model\"\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",
24
+ "import torch\n",
25
+ "print(\"PyTorch:\", torch.__version__)\n",
26
+ "print(\"CUDA available:\", torch.cuda.is_available())\n"
27
+ ],
28
+ "id": "Muc0ftk9XeQz"
29
+ },
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",
39
+ "\n",
40
+ "local_dir = \"/content/downloaded_model\"\n",
41
+ "config_path = hf_hub_download(repo_id=REPO_ID, filename=\"config.json\", local_dir=local_dir)\n",
42
+ "weights_path = hf_hub_download(repo_id=REPO_ID, filename=\"model_weights.pt\", local_dir=local_dir)\n",
43
+ "tokenizer_path = hf_hub_download(repo_id=REPO_ID, filename=\"tokenizer.json\", local_dir=local_dir)\n",
44
+ "\n",
45
+ "print(\"Downloaded to:\", local_dir)\n"
46
+ ],
47
+ "id": "h5Lf-nDoXeQ0"
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": null,
52
+ "metadata": {
53
+ "id": "rhTG3mtsXeQ0"
54
+ },
55
+ "outputs": [],
56
+ "source": [
57
+ "import math\n",
58
+ "import torch.nn as nn\n",
59
+ "import torch.nn.functional as F\n",
60
+ "\n",
61
+ "class CausalSelfAttention(nn.Module):\n",
62
+ " def __init__(self, n_embd, n_head, block_size, dropout=0.1):\n",
63
+ " super().__init__()\n",
64
+ " assert n_embd % n_head == 0\n",
65
+ " self.n_head = n_head\n",
66
+ " self.head_dim = n_embd // n_head\n",
67
+ " self.qkv = nn.Linear(n_embd, 3 * n_embd)\n",
68
+ " self.proj = nn.Linear(n_embd, n_embd)\n",
69
+ " self.attn_dropout = nn.Dropout(dropout)\n",
70
+ " self.resid_dropout = nn.Dropout(dropout)\n",
71
+ " mask = torch.tril(torch.ones(block_size, block_size)).view(1, 1, block_size, block_size)\n",
72
+ " self.register_buffer(\"mask\", mask)\n",
73
+ "\n",
74
+ " def forward(self, x):\n",
75
+ " B, T, C = x.shape\n",
76
+ " qkv = self.qkv(x)\n",
77
+ " q, k, v = qkv.split(C, dim=2)\n",
78
+ " q = q.view(B, T, self.n_head, self.head_dim).transpose(1, 2)\n",
79
+ " k = k.view(B, T, self.n_head, self.head_dim).transpose(1, 2)\n",
80
+ " v = v.view(B, T, self.n_head, self.head_dim).transpose(1, 2)\n",
81
+ "\n",
82
+ " att = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim)\n",
83
+ " att = att.masked_fill(self.mask[:, :, :T, :T] == 0, float(\"-inf\"))\n",
84
+ " att = F.softmax(att, dim=-1)\n",
85
+ " att = self.attn_dropout(att)\n",
86
+ " out = att @ v\n",
87
+ " out = out.transpose(1, 2).contiguous().view(B, T, C)\n",
88
+ " return self.resid_dropout(self.proj(out))\n",
89
+ "\n",
90
+ "\n",
91
+ "class MLP(nn.Module):\n",
92
+ " def __init__(self, n_embd, dropout=0.1):\n",
93
+ " super().__init__()\n",
94
+ " self.fc1 = nn.Linear(n_embd, 4 * n_embd)\n",
95
+ " self.fc2 = nn.Linear(4 * n_embd, n_embd)\n",
96
+ " self.dropout = nn.Dropout(dropout)\n",
97
+ "\n",
98
+ " def forward(self, x):\n",
99
+ " return self.dropout(self.fc2(F.gelu(self.fc1(x))))\n",
100
+ "\n",
101
+ "\n",
102
+ "class Block(nn.Module):\n",
103
+ " def __init__(self, n_embd, n_head, block_size, dropout=0.1):\n",
104
+ " super().__init__()\n",
105
+ " self.ln1 = nn.LayerNorm(n_embd)\n",
106
+ " self.attn = CausalSelfAttention(n_embd, n_head, block_size, dropout)\n",
107
+ " self.ln2 = nn.LayerNorm(n_embd)\n",
108
+ " self.mlp = MLP(n_embd, dropout)\n",
109
+ "\n",
110
+ " def forward(self, x):\n",
111
+ " x = x + self.attn(self.ln1(x))\n",
112
+ " x = x + self.mlp(self.ln2(x))\n",
113
+ " return x\n",
114
+ "\n",
115
+ "\n",
116
+ "class GPTFromScratch(nn.Module):\n",
117
+ " def __init__(self, vocab_size, block_size, n_layer=6, n_head=6, n_embd=384, dropout=0.1):\n",
118
+ " super().__init__()\n",
119
+ " self.block_size = block_size\n",
120
+ " self.tok_emb = nn.Embedding(vocab_size, n_embd)\n",
121
+ " self.pos_emb = nn.Embedding(block_size, n_embd)\n",
122
+ " self.drop = nn.Dropout(dropout)\n",
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 # weight tying\n",
127
+ "\n",
128
+ " def forward(self, idx, targets=None):\n",
129
+ " B, T = idx.shape\n",
130
+ " pos = torch.arange(T, device=idx.device).unsqueeze(0)\n",
131
+ " x = self.drop(self.tok_emb(idx) + self.pos_emb(pos))\n",
132
+ " for block in self.blocks:\n",
133
+ " x = block(x)\n",
134
+ " x = self.ln_f(x)\n",
135
+ " logits = self.head(x)\n",
136
+ "\n",
137
+ " loss = None\n",
138
+ " if targets is not None:\n",
139
+ " loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1))\n",
140
+ " return logits, loss\n",
141
+ "\n",
142
+ " @torch.no_grad()\n",
143
+ " def generate(self, idx, max_new_tokens, temperature=0.8, top_k=40):\n",
144
+ " for _ in range(max_new_tokens):\n",
145
+ " idx_cond = idx[:, -self.block_size :]\n",
146
+ " logits, _ = self(idx_cond)\n",
147
+ " logits = logits[:, -1, :] / temperature\n",
148
+ " if top_k is not None:\n",
149
+ " v, _ = torch.topk(logits, top_k)\n",
150
+ " logits[logits < v[:, [-1]]] = float(\"-inf\")\n",
151
+ " probs = F.softmax(logits, dim=-1)\n",
152
+ " next_id = torch.multinomial(probs, num_samples=1)\n",
153
+ " idx = torch.cat([idx, next_id], dim=1)\n",
154
+ " return idx\n"
155
+ ],
156
+ "id": "rhTG3mtsXeQ0"
157
+ },
158
+ {
159
+ "cell_type": "code",
160
+ "execution_count": null,
161
+ "metadata": {
162
+ "id": "PP6KcuhQXeQ1"
163
+ },
164
+ "outputs": [],
165
+ "source": [
166
+ "import json\n",
167
+ "\n",
168
+ "from tokenizers import Tokenizer\n",
169
+ "\n",
170
+ "with open(config_path) as f:\n",
171
+ " cfg = json.load(f)\n",
172
+ "\n",
173
+ "tokenizer_backend = Tokenizer.from_file(tokenizer_path)\n",
174
+ "\n",
175
+ "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
176
+ "model = GPTFromScratch(\n",
177
+ " vocab_size=cfg[\"vocab_size\"],\n",
178
+ " block_size=cfg[\"block_size\"],\n",
179
+ " n_layer=cfg[\"n_layer\"],\n",
180
+ " n_head=cfg[\"n_head\"],\n",
181
+ " n_embd=cfg[\"n_embd\"],\n",
182
+ ").to(device)\n",
183
+ "model.load_state_dict(torch.load(weights_path, map_location=device))\n",
184
+ "model.eval()\n",
185
+ "\n",
186
+ "USER_TOK = cfg[\"user_tok\"]\n",
187
+ "ASSISTANT_TOK = cfg[\"assistant_tok\"]\n",
188
+ "END_TOK = cfg[\"end_tok\"]\n",
189
+ "END_ID = tokenizer_backend.token_to_id(END_TOK)\n",
190
+ "\n",
191
+ "n_params = sum(p.numel() for p in model.parameters())\n",
192
+ "print(f\"Model loaded successfully: {n_params:,} parameters, running on {device}\")\n"
193
+ ],
194
+ "id": "PP6KcuhQXeQ1"
195
+ },
196
+ {
197
+ "cell_type": "code",
198
+ "execution_count": null,
199
+ "metadata": {
200
+ "id": "u1CRQJL2XeQ2"
201
+ },
202
+ "outputs": [],
203
+ "source": [
204
+ "print(\"Chat started. Type 'exit' to quit.\\n\")\n",
205
+ "while True:\n",
206
+ " user_input = input(\"You: \")\n",
207
+ " if user_input.strip().lower() == \"exit\":\n",
208
+ " print(\"Goodbye!\")\n",
209
+ " break\n",
210
+ " reply = chat(user_input)\n",
211
+ " print(\"Bot:\", reply)\n"
212
+ ],
213
+ "id": "u1CRQJL2XeQ2"
214
+ }
215
+ ],
216
+ "metadata": {
217
+ "accelerator": "GPU",
218
+ "colab": {
219
+ "provenance": [],
220
+ "gpuType": "T4"
221
+ },
222
+ "kernelspec": {
223
+ "display_name": "Python 3",
224
+ "name": "python3"
225
+ },
226
+ "language_info": {
227
+ "name": "python"
228
+ }
229
+ },
230
+ "nbformat": 4,
231
+ "nbformat_minor": 5
232
+ }
config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "vocab_size": 8192,
3
+ "block_size": 256,
4
+ "n_layer": 6,
5
+ "n_head": 6,
6
+ "n_embd": 384,
7
+ "user_tok": "<|user|>",
8
+ "assistant_tok": "<|assistant|>",
9
+ "end_tok": "<|end|>"
10
+ }
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
model_weights.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:70769a57a51f4ebf1526ecbf57ebc437b40f28a323a3a3cb1238c5810d9f4f40
3
+ size 57169021
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff