Text Generation
Safetensors
English
Chinese
xTimeCrystal commited on
Commit
8357c72
·
verified ·
1 Parent(s): cb44cb3

Upload 2 files

Browse files
Files changed (2) hide show
  1. config.json +7 -0
  2. model_demo.ipynb +277 -0
config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "layers": 24,
3
+ "num_heads": 12,
4
+ "vocab_size": 32768,
5
+ "input_dims": 768,
6
+ "hidden_dims": 3072
7
+ }
model_demo.ipynb ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "fa3b9b85-762d-4a0b-a0f2-70c9a6aa59b9",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import json\n",
11
+ "import torch\n",
12
+ "from safetensors import safe_open\n",
13
+ "from model import Transformer as Model\n",
14
+ "from transformers import PreTrainedTokenizerFast\n",
15
+ "\n",
16
+ "with open(\"./config.json\", \"r\") as f:\n",
17
+ " config = json.load(f)\n",
18
+ "\n",
19
+ "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
20
+ "torch.set_default_device(device)\n",
21
+ "\n",
22
+ "model = Model(**config)\n",
23
+ "model.zero_grad()\n",
24
+ "model.bfloat16()\n",
25
+ "\n",
26
+ "saved_states = {}\n",
27
+ "with safe_open(\"./model.safetensors\", framework=\"pt\", device=device) as f:\n",
28
+ " for key in f.keys():\n",
29
+ " saved_states[key] = f.get_tensor(key)\n",
30
+ "model.load_state_dict(saved_states)\n",
31
+ "model.eval()\n",
32
+ "\n",
33
+ "tokenizer = PreTrainedTokenizerFast.from_pretrained(\"./\")"
34
+ ]
35
+ },
36
+ {
37
+ "cell_type": "code",
38
+ "execution_count": 2,
39
+ "id": "fe7449b5-0993-40e5-89f3-d9f0e5196839",
40
+ "metadata": {},
41
+ "outputs": [
42
+ {
43
+ "name": "stdout",
44
+ "output_type": "stream",
45
+ "text": [
46
+ "<s> def fibonacci(n: int):\n",
47
+ " if n < 2:\n",
48
+ " return n\n",
49
+ " return fibonacci(n - 1) + fibonacci(n - 2)\n",
50
+ "\n",
51
+ "\n",
52
+ "def fibonacci_recursive(n: int):\n",
53
+ " if n < 2:\n",
54
+ " return n\n",
55
+ " return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)\n",
56
+ "\n",
57
+ "\n",
58
+ "def fibonacci_iterative(n: int):\n",
59
+ " if n < 2:\n",
60
+ " return n\n",
61
+ " return fibonacci_iterative"
62
+ ]
63
+ }
64
+ ],
65
+ "source": [
66
+ "tokens = tokenizer('''def fibonacci(n: int):''')['input_ids']\n",
67
+ "\n",
68
+ "current = tokenizer.decode(tokens)\n",
69
+ "print(current, end=\"\")\n",
70
+ "\n",
71
+ "temperature = 1e-4\n",
72
+ "\n",
73
+ "for _ in range(128):\n",
74
+ "\n",
75
+ " tok = torch.tensor(tokens).reshape(1, -1)\n",
76
+ " \n",
77
+ " logits = model(tok)\n",
78
+ "\n",
79
+ " nxt = torch.multinomial(torch.softmax(logits[:, -1].float()/temperature, dim=-1).squeeze(), num_samples=1).item()\n",
80
+ " \n",
81
+ " tokens += [nxt]\n",
82
+ "\n",
83
+ " print(tokenizer.decode(tokens).replace(current, \"\", 1), end=\"\")\n",
84
+ " \n",
85
+ " current = tokenizer.decode(tokens)"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 3,
91
+ "id": "f053099d-acf4-4432-9c27-1a0f9d9352c1",
92
+ "metadata": {},
93
+ "outputs": [
94
+ {
95
+ "name": "stdout",
96
+ "output_type": "stream",
97
+ "text": [
98
+ "<s> Digits of pi:\n",
99
+ "\n",
100
+ "# What is the value of pi?\n",
101
+ "\n",
102
+ "## What is the value of pi?\n",
103
+ "\n",
104
+ "The value of pi is 3.14159265358979323846273284627328462732846273284627328462732846273284627328462732846273284627328462"
105
+ ]
106
+ }
107
+ ],
108
+ "source": [
109
+ "tokens = tokenizer('''Digits of pi:''')['input_ids']\n",
110
+ "\n",
111
+ "current = tokenizer.decode(tokens)\n",
112
+ "print(current, end=\"\")\n",
113
+ "\n",
114
+ "temperature = 1e-4\n",
115
+ "\n",
116
+ "for _ in range(128):\n",
117
+ "\n",
118
+ " tok = torch.tensor(tokens).reshape(1, -1)\n",
119
+ " \n",
120
+ " logits = model(tok)\n",
121
+ "\n",
122
+ " nxt = torch.multinomial(torch.softmax(logits[:, -1].float()/temperature, dim=-1).squeeze(), num_samples=1).item()\n",
123
+ " \n",
124
+ " tokens += [nxt]\n",
125
+ "\n",
126
+ " print(tokenizer.decode(tokens).replace(current, \"\", 1), end=\"\")\n",
127
+ " \n",
128
+ " current = tokenizer.decode(tokens)"
129
+ ]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": 6,
134
+ "id": "aca6f531-0267-44dc-a632-8d249c0bf3fd",
135
+ "metadata": {},
136
+ "outputs": [
137
+ {
138
+ "name": "stdout",
139
+ "output_type": "stream",
140
+ "text": [
141
+ "<s> The chemical formula for water is H2O. What does it mean?\n",
142
+ "The chemical formula for water is H2O. What does it mean?\n",
143
+ "Water is the purest liquid on Earth. It is the basis of life. Water is found in the soil, rivers, lakes, oceans, and the ocean. Water is also found in our bodies. Water is found in everything we take in on a daily basis. Water is essential for life. Water is found in the cells, tissues, and organs of all living things. Water is a key element of life because it enables the creation and maintenance of the various chemical and physical processes"
144
+ ]
145
+ }
146
+ ],
147
+ "source": [
148
+ "tokens = tokenizer('''The chemical formula for water is''')['input_ids']\n",
149
+ "\n",
150
+ "current = tokenizer.decode(tokens)\n",
151
+ "print(current, end=\"\")\n",
152
+ "\n",
153
+ "temperature = 7e-1\n",
154
+ "\n",
155
+ "for _ in range(128):\n",
156
+ "\n",
157
+ " tok = torch.tensor(tokens).reshape(1, -1)\n",
158
+ " \n",
159
+ " logits = model(tok)\n",
160
+ "\n",
161
+ " nxt = torch.multinomial(torch.softmax(logits[:, -1].float()/temperature, dim=-1).squeeze(), num_samples=1).item()\n",
162
+ " \n",
163
+ " tokens += [nxt]\n",
164
+ "\n",
165
+ " print(tokenizer.decode(tokens).replace(current, \"\", 1), end=\"\")\n",
166
+ " \n",
167
+ " current = tokenizer.decode(tokens)"
168
+ ]
169
+ },
170
+ {
171
+ "cell_type": "code",
172
+ "execution_count": 13,
173
+ "id": "72220140-d4c3-4f4a-9d8a-0677e66d8b05",
174
+ "metadata": {},
175
+ "outputs": [
176
+ {
177
+ "name": "stdout",
178
+ "output_type": "stream",
179
+ "text": [
180
+ "<s> The purpose of life is to build up the body’s strength, endurance, and energy reserves through the accumulation of acquired skills, and to get rid of worn or damaged parts of the body. All of this depends on day’s activities and deeds. The process of building up the body and taking on new challenges, such as accumulating health, will require the use of skills and abilities.\n",
181
+ "The main purpose of building up skills and abilities in life is to make new people capable of doing the things that they need to do. This process requires you to develop skills that are applicable to everyday life. Skills can either be formal, or in the"
182
+ ]
183
+ }
184
+ ],
185
+ "source": [
186
+ "tokens = tokenizer('''The purpose of life is to''')['input_ids']\n",
187
+ "\n",
188
+ "current = tokenizer.decode(tokens)\n",
189
+ "print(current, end=\"\")\n",
190
+ "\n",
191
+ "temperature = 8e-1\n",
192
+ "\n",
193
+ "for _ in range(128):\n",
194
+ "\n",
195
+ " tok = torch.tensor(tokens).reshape(1, -1)\n",
196
+ " \n",
197
+ " logits = model(tok)\n",
198
+ "\n",
199
+ " nxt = torch.multinomial(torch.softmax(logits[:, -1].float()/temperature, dim=-1).squeeze(), num_samples=1).item()\n",
200
+ " \n",
201
+ " tokens += [nxt]\n",
202
+ "\n",
203
+ " print(tokenizer.decode(tokens).replace(current, \"\", 1), end=\"\")\n",
204
+ " \n",
205
+ " current = tokenizer.decode(tokens)"
206
+ ]
207
+ },
208
+ {
209
+ "cell_type": "code",
210
+ "execution_count": 14,
211
+ "id": "a6f71778-01e8-40b0-b4c0-ef3e7e934e66",
212
+ "metadata": {},
213
+ "outputs": [
214
+ {
215
+ "name": "stdout",
216
+ "output_type": "stream",
217
+ "text": [
218
+ "<s> Charles Darwin: The Origin of Species\n",
219
+ "Suggested Citation: (Author's description, 2007-08-02)\n",
220
+ "In the early 1900s the scientific community found that Darwin's theories would provide a mechanism for the further evolutionary history of living beings, assuming there was not been a series of intelligent, representative \"histors\" of life. Through careful research, Darwin's theory of the Origin of Species proved to be compatible with a single evolutionary process, that of speciation (Darwin, 1886). In other words, Darwin had"
221
+ ]
222
+ }
223
+ ],
224
+ "source": [
225
+ "tokens = tokenizer('''Charles Darwin''')['input_ids']\n",
226
+ "\n",
227
+ "current = tokenizer.decode(tokens)\n",
228
+ "print(current, end=\"\")\n",
229
+ "\n",
230
+ "temperature = 8e-1\n",
231
+ "\n",
232
+ "for _ in range(128):\n",
233
+ "\n",
234
+ " tok = torch.tensor(tokens).reshape(1, -1)\n",
235
+ " \n",
236
+ " logits = model(tok)\n",
237
+ "\n",
238
+ " nxt = torch.multinomial(torch.softmax(logits[:, -1].float()/temperature, dim=-1).squeeze(), num_samples=1).item()\n",
239
+ " \n",
240
+ " tokens += [nxt]\n",
241
+ "\n",
242
+ " print(tokenizer.decode(tokens).replace(current, \"\", 1), end=\"\")\n",
243
+ " \n",
244
+ " current = tokenizer.decode(tokens)"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": null,
250
+ "id": "6a6e1068-b3a8-4019-9e31-5336a337a25a",
251
+ "metadata": {},
252
+ "outputs": [],
253
+ "source": []
254
+ }
255
+ ],
256
+ "metadata": {
257
+ "kernelspec": {
258
+ "display_name": "Python 3 (ipykernel)",
259
+ "language": "python",
260
+ "name": "python3"
261
+ },
262
+ "language_info": {
263
+ "codemirror_mode": {
264
+ "name": "ipython",
265
+ "version": 3
266
+ },
267
+ "file_extension": ".py",
268
+ "mimetype": "text/x-python",
269
+ "name": "python",
270
+ "nbconvert_exporter": "python",
271
+ "pygments_lexer": "ipython3",
272
+ "version": "3.12.3"
273
+ }
274
+ },
275
+ "nbformat": 4,
276
+ "nbformat_minor": 5
277
+ }