-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
204 lines (164 loc) · 6.72 KB
/
run.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
import argparse
import os
import time
from io import BytesIO
from typing import Callable
import modal
import torch
from data import get_batch_factory, get_encoder_decoder_vocab_size
from gpt import GPTLanguageModel, batch_size, block_size
device = "cuda" if torch.cuda.is_available() else "cpu"
# Modal configuration
lora_image = modal.Image.debian_slim().pip_install_from_requirements("requirements.txt")
app = modal.App("lora-tutorial")
@torch.no_grad()
def estimate_loss(model: GPTLanguageModel, get_batch: Callable, eval_iters: int):
out = {}
model.eval()
for split in ["train", "val"]:
losses = torch.zeros(eval_iters)
for k in range(eval_iters):
X, Y = get_batch(split)
logits, loss = model(X, Y)
losses[k] = loss.item()
out[split] = losses.mean()
model.train()
return out
@app.function(image=lora_image, gpu="A100", timeout=3600)
def train(
files: dict[str, BytesIO],
max_iters: int = 5000,
eval_interval: int = 500,
learning_rate: float = 3e-4,
) -> dict[str, BytesIO]:
encode, _, vocab_size = get_encoder_decoder_vocab_size(files["shakespeare.txt"])
model = GPTLanguageModel(vocab_size)
model = model.to(device)
# print the number of parameters in the model
print(sum(p.numel() for p in model.parameters()) / 1e6, "M parameters")
print(sum(p.numel() for p in model.parameters() if p.requires_grad) / 1e6, "M trainable parameters")
# create a PyTorch optimizer
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)
get_batch = get_batch_factory(files["shakespeare.txt"], block_size, batch_size, device, encode)
start_time = time.time()
for iter in range(max_iters):
# every once in a while evaluate the loss on train and val sets
if iter % eval_interval == 0 or iter == max_iters - 1:
losses = estimate_loss(model, get_batch, eval_iters)
print(
f"step {iter}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}, timer {time.time() - start_time:.2f}s"
)
# sample a batch of data
xb, yb = get_batch("train")
# evaluate the loss
_, loss = model(xb, yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
# Save the model weights
buff = BytesIO()
torch.save(model.state_dict(), buff)
return {"shakespeare.pth": buff}
@app.function(image=lora_image, gpu="A100")
def generate(
files: dict[str, BytesIO],
base_model: str,
lora_model: str | None = None,
) -> dict[str, BytesIO]:
_, decode, vocab_size = get_encoder_decoder_vocab_size(files["shakespeare.txt"])
model = GPTLanguageModel(vocab_size)
model.load_state_dict(torch.load(files[base_model]), strict=False)
if lora_model:
model.load_state_dict(torch.load(files[lora_model]), strict=False)
model.eval()
model.to(device)
# generate from the model
context = torch.zeros((1, 1), dtype=torch.long, device=device)
print(decode(model.generate(context, max_new_tokens=500)[0].tolist()))
# Generate more and store to disk (a bit slow)
# more = decode(model.generate(context, max_new_tokens=5000)[0].tolist())
# buff = BytesIO()
# buff.write(more.encode("utf-8"))
# return {"more.txt": buff}
@app.function(image=lora_image, gpu="A100")
def tune(
files: dict[str, BytesIO],
max_iters: int = 300,
eval_interval: int = 100,
eval_iters: int = 200,
learning_rate: float = 3e-3,
):
encode, _, vocab_size = get_encoder_decoder_vocab_size(files["shakespeare.txt"])
model = GPTLanguageModel(vocab_size)
model.load_state_dict(torch.load(files["shakespeare.pth"]), strict=False)
model = model.to(device)
# Freeze all non-lora parameters
for name, param in model.named_parameters():
if "lora" not in name:
param.requires_grad = False
# print the number of parameters in the model
print(sum(p.numel() for p in model.parameters()) / 1e6, "M parameters")
print(sum(p.numel() for p in model.parameters() if p.requires_grad) / 1e6, "M trainable parameters")
# create a PyTorch optimizer (only for trainable parameters)
optimizer = torch.optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=learning_rate)
get_batch = get_batch_factory(files["hemingway.txt"], block_size, batch_size, device, encode)
start_time = time.time()
for iter in range(max_iters):
# every once in a while evaluate the loss on train and val sets
if iter % eval_interval == 0 or iter == max_iters - 1:
losses = estimate_loss(model, get_batch, eval_iters)
print(
f"step {iter}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}, timer {time.time() - start_time:.2f}s"
)
# sample a batch of data
xb, yb = get_batch("train")
# evaluate the loss
logits, loss = model(xb, yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
# Save the model weights
buff = BytesIO()
state_dict = model.state_dict()
lora_layers = {k: v for k, v in state_dict.items() if "lora" in k}
torch.save(lora_layers, buff)
return {"hemingway_lora.pth": buff}
class FileSyncer:
files_to_sync = [
"shakespeare.txt",
"hemingway.txt",
"shakespeare.pth",
"hemingway.pth",
"hemingway_lora.pth",
"more-shakespeare.txt",
]
@classmethod
def load(cls) -> dict[str, BytesIO]:
files: dict[str, BytesIO] = {}
for filename in cls.files_to_sync:
if os.path.exists(filename):
with open(filename, "rb") as f:
files[filename] = BytesIO(f.read())
return files
@classmethod
def store(cls, files: dict[str, BytesIO]) -> None:
for filename, data in files.items():
with open(filename, "wb") as f:
f.write(data.getbuffer())
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--mode", type=str, default="train")
parser.add_argument("--base_model", type=str, default="shakespeare.pth")
parser.add_argument("--lora_model", type=str, default=None)
args = parser.parse_args()
input_files = FileSyncer.load()
with modal.enable_output():
with app.run():
if args.mode == "train":
output_files = train.remote(input_files)
elif args.mode == "generate":
output_files = generate.remote(input_files, args.base_model, args.lora_model)
print("Done generating.")
elif args.mode == "tune":
output_files = tune.remote(input_files)
FileSyncer.store(output_files)