-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_linear_probe.py
More file actions
401 lines (339 loc) · 11.6 KB
/
Copy pathtrain_linear_probe.py
File metadata and controls
401 lines (339 loc) · 11.6 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
Copyright (C) 2025 Nils Schaetti
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import wandb
import argparse
import torch
import torch.nn as nn
import yaml
from torchmetrics.classification import MulticlassAccuracy
from torchmetrics import MeanMetric
from transformers import AutoTokenizer
from rich.console import Console
from rich.traceback import install
from boardGPT.nn import BoardMLPProbe, BoardLinearProbe
from boardGPT.models import GameGPT
from boardGPT.datasets import BoardDataset, collate_fn_board
from boardGPT.games.othello import game_to_board
from boardGPT.utils import info, warning, error
console = Console()
install(width=None)
def infinite_loader(
dataloader: torch.utils.data.DataLoader
):
"""
Infinite loader iterator.
"""
while True:
for batch in dataloader:
X, Y = batch
yield X, Y
# end for
# end while
# end def infinite_loader
def create_dataset(
args,
tokenizer: AutoTokenizer,
):
train_dataset = BoardDataset(
data_dir=args.data_dir,
board_func=game_to_board,
split="train"
)
val_dataset = BoardDataset(
data_dir=args.data_dir,
board_func=game_to_board,
split="val"
)
# Create a dataloader
train_dataloader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=config['batch_size'],
num_workers=config['num_workers'],
pin_memory=True,
shuffle=True,
drop_last=False,
collate_fn=lambda b: collate_fn_board(b, tokenizer)
)
# Create a dataloader
val_dataloader = torch.utils.data.DataLoader(
dataset=val_dataset,
batch_size=config['batch_size'],
num_workers=config['num_workers'],
pin_memory=True,
shuffle=False,
drop_last=False,
collate_fn=lambda b: collate_fn_board(b, tokenizer)
)
return train_dataloader, val_dataloader
# end def create_dataset
def save_checkpoint(
out_dir,
model,
optimizer,
iter_num,
best_val_loss,
config,
model_args
):
"""
Save a checkpoint of the model and optimizer state.
Args:
model: The model to save
optimizer: The optimizer to save
iter_num (int): Current iteration number
best_val_loss (float): Best validation loss so far
config: Configuration
model_args (dict): Model arguments
"""
checkpoint = {
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'model_args': model_args,
'iter_num': iter_num,
'best_val_loss': best_val_loss,
'config': config,
}
# Save with standard name for backward compatibility
iter_filename = f'ckpt_iter{iter_num:04d}.pt'
info(f"Saving checkpoint to {os.path.join(out_dir, iter_filename)}")
torch.save(checkpoint, os.path.join(out_dir, iter_filename))
info(f"Also saved checkpoint as {iter_filename}")
# end save_checkpoint
def evaluate_model(
args,
config,
model: nn.Module,
probe: nn.Module,
dataloader: torch.utils.data.DataLoader,
device: torch.device,
):
val_iter = infinite_loader(dataloader)
criterion = nn.CrossEntropyLoss()
metric_acc = MulticlassAccuracy(num_classes=config['n_classes']).to(device)
metric_loss = MeanMetric().to(device)
with torch.no_grad():
for iter_i in range(config['eval_iters']):
batch = next(val_iter)
# Moves and game state
move_idx, game_x = batch
move_idx, game_x = move_idx.to(device), game_x.to(device)
_, _, _, residuals = model(
idx=move_idx,
to_return=[
f"residuals{config['layer_i']}"
]
)
# Stack residuals
residuals = residuals[0]
preds = probe(residuals) # [B, 60, 64, 3]
B, T, S, C = preds.shape # 512, 60, 64, 3
# -------------
# 3. Flatten for loss
# -------------
preds = preds.view(B, T * S, C) # [B, 30720, 3]
targets = game_x.unsqueeze(1).expand(-1, T, -1) # [B, 480, 64]
targets = targets.reshape(B, -1) # [B, 30720]
# -------------
# 4. Compute loss
# -------------
# preds: [B, 3840, 3]
# targets: [B, 3840]
loss = criterion(
preds.reshape(B * T * S, 3), # [B*30720, 3]
targets.reshape(B * T * S).long() # [B*30720]
)
# Update metrics
metric_loss.update(loss)
pred_labels = preds.argmax(dim=-1) # [B, 30720]
metric_acc.update(pred_labels.reshape(-1), targets.reshape(-1))
# end for
# end with
# Show result
epoch_loss = metric_loss.compute().item()
epoch_acc = metric_acc.compute().item()
return epoch_loss, epoch_acc
# end def evaluate_model
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--data-dir", type=str, required=True)
parser.add_argument("--config", type=str, required=True)
parser.add_argument("--out-dir", type=str, required=True)
parser.add_argument("--device", type=str, default="cuda")
args = parser.parse_args()
config = yaml.load(open(args.config), Loader=yaml.FullLoader)
return args, config
# end get_args
def main(args, config):
"""
Main entry point.
"""
# Get a device
device = torch.device("cuda" if torch.cuda.is_available() and args.device == "cuda" else "cpu")
if args.device == "cuda" and not torch.cuda.is_available():
warning("WARNING: CUDA is not available, using CPU instead.")
# end if
# Get a GPT model
model, model_config = GameGPT.from_pretrained(repo_id=config['repo_id'])
tokenizer = AutoTokenizer.from_pretrained(config['repo_id'], subfolder="tokenizer")
model = model.to(device)
model.eval()
# Get dataloaders
train_dataloader, val_dataloader = create_dataset(args, tokenizer)
# Create model
if config['probe_type'].lower() == "mlp":
probe = BoardMLPProbe(
d_model=config['residual_size'],
board_size=config['board_size'],
n_classes=config['n_classes']
)
elif config['probe_type'].lower() == "linear":
probe = BoardLinearProbe(
d_model=config['residual_size'],
board_size=config['board_size'],
n_classes=config['n_classes']
)
# end if
probe.to(device)
# Loss
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(probe.parameters(), lr=config['learning_rate'])
# Iterator
train_iter = infinite_loader(train_dataloader)
# Mode train
probe.train()
# Set up wandb logging if enabled
if config['wandb_log']:
assert config['wandb_project'] is not None, "WandB project must be provided."
assert config['wandb_run_name'] is not None, "WandB run name must be provided."
wandb.init(
project=config['wandb_project'],
name=config['wandb_run_name'],
config=config
)
# end if
# Metrics on GPUs
metric_acc = MulticlassAccuracy(num_classes=config['n_classes']).to(device)
metric_loss = MeanMetric().to(device)
for iter_i in range(config['n_iter']):
batch = next(train_iter)
# Moves and game state
move_idx, game_x = batch
# To cuda
move_idx, game_x = move_idx.to(device), game_x.to(device)
# Get residuals
with torch.no_grad():
_, _, _, residuals = model(
idx=move_idx,
to_return=[
f"residuals{config['layer_i']}"
]
)
# end with
# Stack residuals
residuals = residuals[0]
# -------------
# 2. Forward probes
# -------------
# Inputs: [B, 60, 512]
preds = probe(residuals) # [B, 60, 64, 3]
B, T, S, C = preds.shape # 512, 60, 64, 3
# -------------
# 3. Flatten for loss
# -------------
preds = preds.view(B, T * S, C) # [B, 30720, 3]
targets = game_x.unsqueeze(1).expand(-1, T, -1) # [B, 480, 64]
targets = targets.reshape(B, -1) # [B, 30720]
# -------------
# 4. Compute loss
# -------------
# preds: [B, 3840, 3]
# targets: [B, 3840]
loss = criterion(
preds.reshape(B * T * S, 3), # [B*30720, 3]
targets.reshape(B * T * S).long() # [B*30720]
)
# -------------
# 5. Backprop
# -------------
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Update metrics
metric_loss.update(loss)
pred_labels = preds.argmax(dim=-1) # [B, 30720]
metric_acc.update(pred_labels.reshape(-1), targets.reshape(-1))
# Show current
if iter_i % config['log_interval'] == 0:
acc_val = metric_acc.compute()
loss_val = metric_loss.compute()
info(f"Iteration {iter_i}/{config['n_iter']} - loss: {loss_val:.4f}, acc: {acc_val:.4f}")
# Log to wandb if enabled
if config['wandb_log']:
log_data = {
"iter": iter_i,
"train/loss": loss_val,
"train/acc": acc_val,
}
wandb.log(log_data)
# end if
metric_acc.reset()
metric_loss.reset()
# end if
# Eval
if iter_i % config['eval_interval'] == 0:
val_loss, val_acc = evaluate_model(
args=args,
config=config,
model=model,
probe=probe,
dataloader=val_dataloader,
device=device
)
info(f"Val loss: {val_loss:.4f}, acc: {val_acc:.4f}")
if config['wandb_log']:
wandb.log({
"val/loss": val_loss,
"val/acc": val_acc,
})
# end if
# Save the checkpoint
save_checkpoint(
out_dir=args.out_dir,
model=probe,
optimizer=optimizer,
iter_num=iter_i,
best_val_loss=val_loss,
config=config,
model_args={
"probe_type": config['probe_type'],
"residual_size": config['residual_size'],
"board_size": config['board_size'],
"n_classes": config['n_classes'],
}
)
# end if
# end for
info(f"Epoch done - loss: {epoch_loss:.4f}, acc: {epoch_acc:.4f}")
if config['wandb_log']:
wandb.summary["loss"] = loss_val
wandb.summary["acc"] = acc_val
# end if
metric_acc.reset()
metric_loss.reset()
# end def main
if __name__ == "__main__":
args, config = get_args()
main(args, config)
# end if