-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLM_train.py
174 lines (120 loc) · 4.78 KB
/
SLM_train.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
import torch
import random
import copy
from nanoFFT import BigramLanguageModel
import pickle
torch.manual_seed(1337)
batch_size = 64
time_intervals = 128
max_iter = 100000
eval_interval = 500
learning_rate = 3e-4
eval_iters = 30
n_embed_LLM=150
n_head_LLM=15
n_layers_LLM = 15
n_embed_SLM=100
n_head_SLM=10
n_layers_SLM = 10
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#device = "cpu"
print(device)
with open('./input/tokens.pkl', 'rb') as f:
tokens = pickle.load(f)
with open('./input/alice.pkl', 'rb') as f:
input_tokens = pickle.load(f)
vocab_size = len(tokens)
print('vocab token size: ', len(tokens))
print('input text token size: ', len(input_tokens))
stoi = {ch:i for i,ch in enumerate(tokens)}
itos = {i:ch for i,ch in enumerate(tokens)}
encode = lambda s: [stoi[c] for c in s]
decode = lambda l: ''.join([itos[i] for i in l])
data = torch.tensor(encode(input_tokens), dtype=torch.long)
torch.manual_seed(1337)
def get_batch():
var_time = time_intervals
ix = torch.randint(len(data) - var_time, (batch_size, ))
x = torch.stack([data[i:i+var_time] for i in ix])
y = torch.stack([data[i+1:i+var_time+1] for i in ix])
return x.to(device), y.to(device)
def get_random_block():
i = random.randint(0, len(data) - time_intervals)
block = data[i:i+time_intervals].reshape(1, -1).to(device)
return block.to(device)
@torch.no_grad()
def estimate_loss():
SLM.eval()
losses = torch.zeros(eval_iters)
for k in range(eval_iters):
X, Y = get_batch()
X_ = LLM.decode(X)
_, _, loss = SLM(X_, targets=Y)
losses[k] = loss.item()
out = losses.mean()
SLM.train()
return out
LLM = BigramLanguageModel(vocab_size, time_intervals, vocab_embed=n_embed_LLM, n_embed=n_embed_LLM, n_head=n_head_LLM, n_layers=n_layers_LLM, device=device).to(device)
SLM = BigramLanguageModel(vocab_size, time_intervals, vocab_embed=n_embed_SLM, n_embed=n_embed_SLM, n_head=n_head_SLM, n_layers=n_layers_SLM, device=device).to(device)
optimizer = torch.optim.Adam(SLM.parameters(), lr=learning_rate)
try:
SLM.load_state_dict(torch.load('SLM_dec_model.pt'))
print("SLM loaded")
except:
print("no SLM")
LLM.load_state_dict(torch.load('LLM_model.pt'))
LLM.to(device)
LLM.eval()
SLM.train()
#SLM.token_embedding_table = copy.deepcopy(LLM.token_embedding_table)
#SLM.position_embedding_table = copy.deepcopy(LLM.position_embedding_table)
pytorch_total_params = sum(p.numel() for p in LLM.parameters())
print('LLM parameters: ', pytorch_total_params)
pytorch_total_params = sum(p.numel() for p in SLM.parameters())
print('SLM parameters: ', pytorch_total_params)
context = get_random_block()
print("LLM alone:")
print("###########################################")
print(decode(LLM.generate(context, max_new_tokens=250)[0].tolist())[-250:])
print("###########################################")
print()
print("LLM with SLM:")
print("###########################################")
print(decode(SLM.generate(context, max_new_tokens=250, LLM=LLM)[0].tolist())[-250:])
print("###########################################")
print()
for iter in range(max_iter):
if iter % eval_interval == 0:
losses = estimate_loss()
context = get_random_block()
text = decode(SLM.generate(context, max_new_tokens=50, LLM=LLM)[0].tolist())[-50:]
text = text.replace("\n", " <new line> ")
print(f"step {iter}, val loss: {losses:.4f}, text: {text}")
if iter>=1000:
try:
torch.save(SLM.state_dict(), 'SLM_dec_model.pt')
except:
print("problem during saving SLM")
if iter>=10000 and iter%10000==0:
context = get_random_block()
print(decode(context[0].tolist()))
print("###########################################")
print("###########################################")
print(decode(SLM.generate(context, max_new_tokens=500, LLM=LLM)[0].tolist()))
print("###########################################")
print("###########################################")
#sample batch of data
xb, yb = get_batch()
#evaluate the loss
with torch.no_grad():
xb_ = LLM.decode(xb)
_, _, loss = SLM(xb_, targets=yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
context = get_random_block()
print(decode(context[0].tolist()))
print("###########################################")
print("###########################################")
print("###########################################")
print(decode(SLM.generate(context, max_new_tokens=500, LLM=LLM)[0].tolist()))