-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_30.py
290 lines (220 loc) · 8.85 KB
/
main_30.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from config import config
config["layers_size"] = [20, 8, 4, 30]
print("config", config)
from gen_data import get_batch
import torch.nn as nn
import torch
from element import Element
import random
from torch.nn import functional as F
class RouteElement(nn.Module):
def __init__(self, element):
super().__init__()
self.ele = element
self.antenna = []
self.output = None
self.potential = []
def antenna_ready(self):
if len(self.antenna) == self.ele.input_n:
return True
else:
return False
def potential_rand_pick(self, layer_n):
if len(self.potential[layer_n]) == 0:
return None
else:
return self.potential[layer_n].pop(random.randrange(len(self.potential[layer_n])))
def calculate(self):
output_list = []
for are in self.antenna:
output_list.append(are.output)
self.output = self.ele(output_list)
class LanModelManual(nn.Module):
def __init__(self):
super().__init__()
# each token directly reads off the logits for the next token from a lookup table
self.token_embedding_table = nn.Embedding(
config["vocab_size"], config["vocab_embed_size"])
self.position_embedding_table = nn.Embedding(
config["sentence_len"], config["vocab_embed_size"])
self.input_ln = nn.LayerNorm(config["vocab_embed_size"])
#
self.output_vocab_proj = nn.Linear(
config["output_size"], config["vocab_size"])
#
self.routed = False
# build manually
layers_size = config["layers_size"]
layers_antenna_max_size = config["layers_antenna_max_size"]
# for parameter statics
self.param_elements = nn.ModuleList([])
#
self.layer_elements = []
for idx, l_size in enumerate(layers_size):
temp_layer_elements = []
if idx == 0:
for x in range(l_size):
re = RouteElement(Element(
config["vocab_embed_size"], 1, config["output_size"], config["sentence_len"]))
re.to(config["device"])
self.param_elements.append(re)
temp_layer_elements.append(re)
self.layer_elements.append(temp_layer_elements)
else:
for x in range(l_size):
re = RouteElement(Element(config["output_size"], random.randint(
1, layers_antenna_max_size[idx]), config["output_size"], config["sentence_len"]))
re.to(config["device"])
for ls in layers_size:
re.potential.append(list(range(ls)))
self.param_elements.append(re)
temp_layer_elements.append(re)
self.layer_elements.append(temp_layer_elements)
def route(self, input):
if self.routed == True:
return
self.routed = True
for idx, re_array in enumerate(self.layer_elements):
if idx != 0:
for r_ele in re_array:
while r_ele.antenna_ready() != True:
for l in range(idx):
if r_ele.antenna_ready():
break
if random.randint(1, config["layers_antenna_probability_inv"][l]) == 1:
pick_n = r_ele.potential_rand_pick(l)
if pick_n != None:
r_ele.antenna.append(
self.layer_elements[l][pick_n])
def forward(self, out_n, idx, targets=None):
B, T = idx.shape
if T != config["sentence_len"]:
exit("error T!=config['sentence_len']")
# idx and targets are both (B,T) tensor of integers
tok_emb = self.token_embedding_table(idx) # (B,T,C)
pos_emb = self.position_embedding_table(
torch.arange(T, device=config["device"])) # (T,C)
x = self.input_ln(tok_emb + pos_emb) # (B,T,C)
#
self.route(x)
#
for idx, re_array in enumerate(self.layer_elements):
if idx == 0:
for r_ele in re_array:
r_ele.output = r_ele.ele([x])
else:
for r_ele in re_array:
r_ele.calculate()
if out_n == -1:
# pick one of the output
rand_output = random.choice(self.layer_elements[-1])
else:
rand_output = self.layer_elements[-1][out_n]
logits = self.output_vocab_proj(rand_output.output)
if targets is None:
loss = None
else:
B, T, C = logits.shape
logits = logits.view(B*T, C)
targets = targets.view(B*T)
loss = F.cross_entropy(logits, targets)
return logits, loss
##########
output_layer_filter = list(range(config["layers_size"][-1]))
output_layer_score = {}
for x in range(config["layers_size"][-1]):
output_layer_score[x] = {
"recent_score": [],
"recent_avg_score": 0,
}
def update_output_layer_score(index, score):
output_layer_score[index]["recent_score"].append(score)
if len(output_layer_score[index]["recent_score"]) >= 4:
output_layer_score[index]["recent_score"].pop(0)
if len(output_layer_score[index]["recent_score"]) > 0:
avg_score = round(sum(output_layer_score[index]["recent_score"]) / len(
output_layer_score[index]["recent_score"]), 8)
output_layer_score[index]['recent_avg_score'] = avg_score
def get_rand_output_index():
return random.choice(output_layer_filter)
def remove_potentials():
if len(output_layer_filter) <= 1:
return
max_score_index = -1
max_score = 0
for score_index, score_item in output_layer_score.items():
if score_item['recent_avg_score'] > max_score:
max_score = score_item['recent_avg_score']
max_score_index = score_index
if max_score_index != -1:
output_layer_score.pop(max_score_index)
output_layer_filter.remove(max_score_index)
# @torch.no_grad()
# def estimate_loss_2():
# out_mean = {}
# model.eval()
# out_losses = {}
# for split in ['train', 'val']:
# out_losses[split] = torch.zeros(len(output_layer_filter))
# for out_idx, out_id in enumerate(output_layer_filter):
# losses = torch.zeros(config["eval_iters"])
# for k in range(config["eval_iters"]):
# X, Y = get_batch(split)
# logits, loss = model(out_id, X, Y)
# losses[k] = loss.item()
# out_losses[split][out_idx] = losses.mean()
# out_mean[split] = out_losses[split].mean()
# if split == 'val':
# val_losses = out_losses[split].tolist()
# max_index = val_losses.index(max(val_losses))
# to_remove_val = output_layer_filter[max_index]
# output_layer_filter.remove(to_remove_val)
# print("output_layer_filter", output_layer_filter)
# model.train()
# return out_mean
@torch.no_grad()
def estimate_loss():
out = {}
model.eval()
for split in ['train', 'val']:
losses = torch.zeros(config["eval_iters"])
for k in range(config["eval_iters"]):
X, Y = get_batch(split)
logits, loss = model(get_rand_output_index(), X, Y)
losses[k] = loss.item()
out[split] = losses.mean()
model.train()
return out
###########
model = LanModelManual()
m = model.to(config['device'])
# print the number of parameters in the model
print(sum(p.numel() for p in m.parameters())/1e6, 'M parameters')
# create a PyTorch optimizer
optimizer = torch.optim.AdamW(model.parameters(), lr=config["learning_rate"])
# counter = 0
for iter in range(config["max_iters"]):
# every once in a while evaluate the loss on train and val sets
if iter % config["eval_interval"] == 0 or iter == config["max_iters"] - 1:
# counter = counter+1
# if counter % 1000000 == 0:
# remove_potentials()
losses = estimate_loss()
print(
f"step {iter}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}")
remove_potentials()
continue
# sample a batch of data
xb, yb = get_batch('train')
# evaluate the loss
output_index = get_rand_output_index()
logits, loss = model(output_index, xb, yb)
loss_val = loss.item()
# print("output_index:", output_index, "loss_val:", loss_val)
update_output_layer_score(output_index, loss_val)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
# generate from the model
# context = torch.zeros((1, 1), dtype=torch.long, device=device)
# print(decode(m.generate(context, max_new_tokens=2000)[0].tolist()))