Repository navigation
Expand file tree
/
Copy pathstory.py
More file actions
243 lines (205 loc) · 10.5 KB
/
Copy pathstory.py
File metadata and controls
243 lines (205 loc) · 10.5 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""
story.py - AURA's storyteller: NINA's (the same model and code), telling weather stories.
The storyteller is Karpathy's TinyStories model from llama2.c: 15 million
parameters, small enough that a version of it runs on an ESP32. It only knows
how to write simple children's stories, so AURA writes the opening from the real
sky ("Today there was light rain and it was cold...") and the little model tells
the rest. She's obsessed with the weather: every story is about it. Text only.
This is the whole model in NumPy: no PyTorch, no GPU, a fraction of a second
per sentence on any PC.
python story.py NORA "fell asleep and would not wake up" try it
tell_story("NORA", ["fell asleep and would not wake up"]) from NINA
"""
import struct
import sys
from pathlib import Path
import re
import time
import numpy as np
MODELS = Path(__file__).resolve().parent / "models"
MODEL_URL = "https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin"
TOKENIZER_URL = "https://github.com/karpathy/llama2.c/raw/master/tokenizer.bin"
class Tokenizer:
"""llama2.c's tokenizer.bin: a SentencePiece vocabulary with merge scores."""
def __init__(self, path, vocab_size):
data = Path(path).read_bytes()
(self.max_len,) = struct.unpack_from("i", data, 0)
off = 4
self.vocab, self.scores = [], []
for _ in range(vocab_size):
score, n = struct.unpack_from("fi", data, off)
off += 8
self.vocab.append(data[off:off + n].decode("utf-8", errors="replace"))
self.scores.append(score)
off += n
self.index = {t: i for i, t in enumerate(self.vocab)}
def encode(self, text):
"""BPE the way llama2.c does it: characters first, then keep merging the best-scoring pair."""
tokens = [self.index[" "]] if " " in self.index else [] # the dummy prefix space
for ch in text:
if ch in self.index:
tokens.append(self.index[ch])
else: # unknown: fall back to its bytes
tokens.extend(b + 3 for b in ch.encode("utf-8"))
while True:
best, best_id, best_i = -1e10, -1, -1
for i in range(len(tokens) - 1):
merged = self.vocab[tokens[i]] + self.vocab[tokens[i + 1]]
j = self.index.get(merged)
if j is not None and self.scores[j] > best:
best, best_id, best_i = self.scores[j], j, i
if best_i < 0:
return tokens
tokens[best_i:best_i + 2] = [best_id]
def decode(self, prev, token):
piece = self.vocab[token]
if prev == 1 and piece.startswith(" "): # after BOS, drop the leading space
piece = piece[1:]
if piece.startswith("<0x") and piece.endswith(">"): # raw byte tokens
return bytes([int(piece[3:-1], 16)]).decode("utf-8", errors="ignore")
return piece
class TinyLlama:
"""llama2.c's model.bin (version 0 format), run with NumPy."""
def __init__(self, path):
raw = Path(path).read_bytes()
dim, hidden, layers, heads, kv_heads, vocab, seq = struct.unpack_from("7i", raw, 0)
self.shared = vocab > 0
vocab = abs(vocab)
self.dim, self.layers, self.heads, self.kv_heads, self.vocab, self.seq = dim, layers, heads, kv_heads, vocab, seq
self.hs = dim // heads
kv_dim = dim * kv_heads // heads
w = np.frombuffer(raw, dtype=np.float32, offset=28)
pos = 0
def take(*shape):
nonlocal pos
n = int(np.prod(shape))
a = w[pos:pos + n].reshape(shape)
pos += n
return a
self.emb = take(vocab, dim)
self.rms_att = take(layers, dim)
self.wq, self.wk = take(layers, dim, dim), take(layers, kv_dim, dim)
self.wv, self.wo = take(layers, kv_dim, dim), take(layers, dim, dim)
self.rms_ffn = take(layers, dim)
self.w1, self.w2, self.w3 = take(layers, hidden, dim), take(layers, dim, hidden), take(layers, hidden, dim)
self.rms_final = take(dim)
take(seq, self.hs // 2) # the file's precomputed RoPE tables: not needed, computed below
take(seq, self.hs // 2)
self.wcls = self.emb if self.shared else take(vocab, dim)
# RoPE frequencies, as llama2.c's run.c computes them
idx = np.arange(0, self.hs, 2, dtype=np.float32)
self.freq = 1.0 / (10000.0 ** (idx / self.hs))
self.kv_dim = kv_dim
@staticmethod
def _rms(x, w):
return w * (x / np.sqrt(np.mean(x * x) + 1e-5))
def _rope(self, v, pos, n_heads):
v = v.reshape(n_heads, self.hs // 2, 2)
ang = pos * self.freq
c, s = np.cos(ang), np.sin(ang)
a, b = v[..., 0], v[..., 1]
return np.stack([a * c - b * s, a * s + b * c], -1).reshape(-1)
def generate(self, prompt_tokens, steps=200, temperature=0.8, top_p=0.9, rng=None):
rng = rng or np.random.default_rng()
kc = np.zeros((self.layers, self.seq, self.kv_dim), np.float32)
vc = np.zeros((self.layers, self.seq, self.kv_dim), np.float32)
tokens = [1] + list(prompt_tokens) # 1 = BOS
out, token = [], tokens[0]
rep = self.heads // self.kv_heads
for pos in range(min(self.seq, len(tokens) + steps) - 1):
x = self.emb[token].copy()
for l in range(self.layers):
xb = self._rms(x, self.rms_att[l])
q = self._rope(self.wq[l] @ xb, pos, self.heads)
kc[l, pos] = self._rope(self.wk[l] @ xb, pos, self.kv_heads)
vc[l, pos] = self.wv[l] @ xb
qh = q.reshape(self.heads, self.hs)
kh = kc[l, :pos + 1].reshape(pos + 1, self.kv_heads, self.hs).repeat(rep, axis=1)
vh = vc[l, :pos + 1].reshape(pos + 1, self.kv_heads, self.hs).repeat(rep, axis=1)
att = np.einsum("hd,thd->ht", qh, kh) / np.sqrt(self.hs)
att = np.exp(att - att.max(axis=1, keepdims=True))
att /= att.sum(axis=1, keepdims=True)
x = x + self.wo[l] @ np.einsum("ht,thd->hd", att, vh).reshape(-1)
xb = self._rms(x, self.rms_ffn[l])
h1 = self.w1[l] @ xb
x = x + self.w2[l] @ ((h1 / (1.0 + np.exp(-h1))) * (self.w3[l] @ xb))
if pos + 1 < len(tokens): # still reading the prompt
token = tokens[pos + 1]
continue
logits = self.wcls @ self._rms(x, self.rms_final)
if temperature <= 0:
nxt = int(np.argmax(logits))
else:
p = np.exp((logits - logits.max()) / temperature)
p /= p.sum()
order = np.argsort(-p)
keep = order[: max(1, int(np.searchsorted(np.cumsum(p[order]), top_p)) + 1)]
nxt = int(rng.choice(keep, p=p[keep] / p[keep].sum()))
if nxt == 1: # BOS again = the story is over
break
out.append(nxt)
token = nxt
return out
_model = _tok = None
def ready():
return (MODELS / "stories15M.bin").exists() and (MODELS / "tokenizer.bin").exists()
def download():
"""Into a .part file first, renamed only when it's whole: a cut-off download is never mistaken
for the model. NINA's copy is used when it's next door (the same files)."""
import shutil
import urllib.request
MODELS.mkdir(exist_ok=True)
nina = MODELS.parent.parent / "NINA" / "models"
for url, name in ((MODEL_URL, "stories15M.bin"), (TOKENIZER_URL, "tokenizer.bin")):
if (MODELS / name).exists():
continue
part = MODELS / (name + ".part")
if (nina / name).exists():
shutil.copyfile(nina / name, part)
else:
urllib.request.urlretrieve(url, part)
part.replace(MODELS / name)
def _load():
global _model, _tok
if _model is None:
if not ready():
download()
_model = TinyLlama(MODELS / "stories15M.bin")
_tok = Tokenizer(MODELS / "tokenizer.bin", _model.vocab)
def tell(opening, steps=180, seed=None):
"""Carries on from `opening` (TinyStories style) and ends on a whole sentence, never a grim one."""
_load()
tokens = _tok.encode(opening)
rng = np.random.default_rng(seed)
for _ in range(4): # TinyStories can be grim: a weather report shouldn't end with a funeral
new = _model.generate(tokens, steps=steps, rng=rng)
text, prev = opening, tokens[-1] if tokens else 1
for t in new:
text += _tok.decode(prev, t)
prev = t
cut = max(text.rfind("."), text.rfind("!"), text.rfind("?")) # end on a whole sentence
text = text[:cut + 1] if cut > len(opening) else text
if not any(w in text.lower() for w in GRIM):
break
return text
FIRST = re.compile(r"\b(I|I'm|I'd|I've|I'll|me|my|myself|mine)\b")
SLIPS = re.compile(r"\b(she|her|herself|he|him|his)\b", re.I) # the story drifting to someone else
NAME_SLIPS = re.compile(r"\b(\w*laura|aura)\b", re.I) # the model doesn't know "Aura": it drifts to "A Laura"
def first_person_score(text):
"""How well a story stays hers: "I" and "my" up, "she" down, a drifting name way down."""
return len(FIRST.findall(text)) - 2 * len(SLIPS.findall(text)) - 6 * len(NAME_SLIPS.findall(text))
def weather_story(words, feel, moon=None, steps=130, seed=None, drafts=4):
"""AURA's kind of story, told by her, in the first person, always about the weather. words:
"light rain"; feel: "warm" / "cold" / "nice". TinyStories learned mostly third-person stories,
so she writes a few drafts and keeps the one that stays most "I"."""
sky = {"clear": "the sun was shining", "mostly clear": "the sun was shining", "partly cloudy": "big white clouds floated by",
"overcast": "the sky was grey", "foggy": "fog covered everything"}.get(words, f"there was {words}")
opening = (f"I love the weather more than anything. Every morning I look up at the sky. "
f"Today {sky} and it was {feel}.")
if moon:
opening += f" Tonight there would be a {moon} moon."
opening += " I went outside and"
base = seed if seed is not None else int(time.time())
stories = [tell(opening, steps=steps, seed=base + i) for i in range(drafts)]
return max(stories, key=lambda t: first_person_score(t[len(opening):]))
GRIM = (" dead", " died", " die ", " dies", " killed", " kill ", " buried", " funeral")