This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwkv.py
190 lines (141 loc) · 5.38 KB
/
rwkv.py
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
import numpy as np
import numba
from tqdm import tqdm
from tokenizers import Tokenizer
def trace_shape(*args):
print("trace_shape", [arg.shape for arg in args])
jit = numba.jit(nopython=True, cache=True)
exp = np.exp
# zero also works
eps_std = np.array(0.000009999999747378752, dtype=np.float32)
@jit
def layer_norm(x, w, b):
xee2 = x - np.mean(x)
x2 = np.sqrt(np.mean(xee2*xee2) + eps_std)
return (xee2/x2) * w + b
sigmoid = jit(lambda x: 1 / (1 + exp(-x)))
@jit
def time_mixing(
x, last_x, last_num, last_den, decay, bonus, mix_k, mix_v, mix_r, Wk, Wv, Wr, Wout
):
# trace_shape(x, last_x, last_num, last_den, decay, bonus, mix_k, mix_v, mix_r, Wk, Wv, Wr, Wout)
k = Wk @ (x * mix_k + last_x * (1 - mix_k))
v = Wv @ (x * mix_v + last_x * (1 - mix_v))
r = Wr @ (x * mix_r + last_x * (1 - mix_r))
common_0 = exp(bonus + k)
wkv = (last_num + common_0 * v) / (last_den + common_0)
rwkv = sigmoid(r) * wkv
common_1 = exp(-exp(decay))
common_2 = exp(k)
num = common_1 * last_num + common_2 * v
den = common_1 * last_den + common_2
return Wout @ rwkv, (x, num, den)
@jit
def channel_mixing(x, last_x, mix_k, mix_r, Wk, Wr, Wv):
k = Wk @ (x * mix_k + last_x * (1 - mix_k))
r = Wr @ (x * mix_r + last_x * (1 - mix_r))
vk = Wv @ np.maximum(k, 0) ** 2
return sigmoid(r) * vk, x
from typing import NamedTuple
class Model(NamedTuple):
tensors: dict[str, np.array]
n_embd: int
n_layer: int
@staticmethod
def load_safetensors(file: str) -> "Model":
from safetensors.numpy import load_file
tensors = load_file(file)
for k in tensors.keys():
if ".time_" in k:
tensors[k] = tensors[k].squeeze()
tensors[k] = np.float32(tensors[k])
n_embd = int(tensors["ln_out.weight"].shape[0])
n_layer = 0
while f"blocks.{n_layer}.ln2.weight" in tensors:
n_layer += 1
return Model(tensors=tensors, n_embd=n_embd, n_layer=n_layer)
def RWKV(model: Model, token, state):
tensors = model.tensors
def p(key):
return tensors[key]
def p_ln(prefix):
return [
p(key)
for key in [
prefix + ".weight",
prefix + ".bias",
]
]
def p_att(prefix):
return [
p(key)
for key in [
prefix + ".time_decay",
prefix + ".time_first",
prefix + ".time_mix_k",
prefix + ".time_mix_v",
prefix + ".time_mix_r",
prefix + ".key.weight",
prefix + ".value.weight",
prefix + ".receptance.weight",
prefix + ".output.weight",
]
]
def p_ffn(prefix):
return [
p(key)
for key in [
prefix + ".time_mix_k",
prefix + ".time_mix_r",
prefix + ".key.weight",
prefix + ".receptance.weight",
prefix + ".value.weight",
]
]
x = p("emb.weight")[token]
x = layer_norm(x, *p_ln("blocks.0.ln0"))
for i in range(model.n_layer):
x_ = layer_norm(x, *p_ln(f"blocks.{i}.ln1"))
dx, state[i][:3] = time_mixing(x_, *state[i][:3], *p_att(f"blocks.{i}.att"))
x = x + dx
x_ = layer_norm(x, *p_ln(f"blocks.{i}.ln2"))
dx, state[i][3] = channel_mixing(x_, state[i][3], *p_ffn(f"blocks.{i}.ffn"))
x = x + dx
x = layer_norm(x, *p_ln("ln_out"))
x = p("head.weight") @ x
e_x = exp(x - np.max(x))
probs = e_x / e_x.sum() # Softmax of x
return probs, state
##########################################################################################################
def sample_probs(probs, temperature=1.0, top_p=0.85):
sorted_probs = np.sort(probs)[::-1]
cumulative_probs = np.cumsum(sorted_probs)
cutoff = sorted_probs[np.argmax(cumulative_probs > top_p)]
probs[probs < cutoff] = 0
probs = probs ** (1 / temperature)
return np.random.choice(a=len(probs), p=probs / np.sum(probs))
if __name__ == "__main__":
import argparse, shelve
parser = argparse.ArgumentParser(prog="rwkv.py")
parser.add_argument("model_file", help="model file in safetensors format")
args = parser.parse_args()
model = Model.load_safetensors(args.model_file)
# Available at https://github.com/BlinkDL/ChatRWKV/blob/main/20B_tokenizer.json
tokenizer = Tokenizer.from_file("20B_tokenizer.json")
prompt = "In a shocking finding, scientist discovered a herd of dragons living in a remote, previously unexplored valley, in Tibet. Even more surprising to the researchers was the fact that the dragons spoke perfect Chinese."
with shelve.open(
f"__pycache__/rwkv.{model.n_embd}-{model.n_layer}.prompt_cache.shelf"
) as db:
try:
probs, state = db[prompt]
except KeyError:
state = np.zeros((model.n_layer, 4, model.n_embd), dtype=np.float32)
for token in tqdm(tokenizer.encode(prompt).ids, desc="Feeding prompt"):
probs, state = RWKV(model, token, state)
db[prompt] = probs, state
print(prompt, end="")
while True:
token = sample_probs(probs, temperature=1, top_p=0.8)
word = tokenizer.decode([token])
print(word, end="", flush=True)
probs, state = RWKV(model, token, state)