-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
131 lines (116 loc) · 4.04 KB
/
train.py
File metadata and controls
131 lines (116 loc) · 4.04 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
from typing import Optional
import torch
import wandb
from autoencoder import *
from buffer import *
import time
from tqdm import tqdm
from utils import *
import argparse
import gc
lr = 1e-4
num_activations = int(2e9) # total number of tokens to train on, the dataset will wrap around as needed
batch_size = 8192
beta1 = 0
beta2 = 0.9999
steps_per_report = 100
steps_per_save = 10000
expansion = 4
n_dim = 4096
m_dim = n_dim * expansion
# To be used when gpu memory is tight, shuffles the encoder and buffer model back and forth from gpu to cpu to limit
# peak gpu memory usage
perform_offloading = False
primary_device = "cuda:0"
offload_device = "cpu"
wandb_project = "autoencoder"
wandb_entity = "collingray"
argparser = argparse.ArgumentParser()
argparser.add_argument("--wb_name", type=Optional[str], default=None)
argparser.add_argument("--wb_notes", type=Optional[str], default=None)
args = argparser.parse_args()
wb_name = args.wb_name
wb_notes = args.wb_notes
wandb.init(project=wandb_project, entity=wandb_entity, name=wb_name, notes=wb_notes)
buffer_cfg = ActivationsBufferConfig(
model_name="mistralai/Mistral-7B-Instruct-v0.1",
layers=[0],
act_site="hook_mlp_out",
dataset_name="roneneldan/TinyStories",
dataset_split="train",
buffer_size=2 ** 20,
device=primary_device,
buffer_device=offload_device,
offload_device=offload_device if perform_offloading else None,
shuffle_buffer=True,
model_batch_size=16,
samples_per_seq=None,
max_seq_length=2048,
)
buffer = ActivationsBuffer(buffer_cfg)
encoder_cfg = AutoEncoderConfig(
n_dim=n_dim,
m_dim=m_dim,
device=primary_device,
lambda_reg=1e-3,
tied=False,
record_data=True,
)
encoder = AutoEncoder(encoder_cfg)
wandb.config.update({
"max_lr": lr,
"num_activations": num_activations,
"batch_size": batch_size,
"beta1": beta1,
"beta2": beta2,
"perform_offloading": perform_offloading,
"encoder": encoder_cfg.__dict__,
"buffer": buffer_cfg.__dict__,
})
optimizer = torch.optim.Adam(encoder.parameters(), lr=lr, betas=(beta1, beta2), foreach=False)
scheduler = torch.optim.lr_scheduler.OneCycleLR(
optimizer,
max_lr=lr,
total_steps=num_activations // batch_size,
pct_start=0.1
)
try:
prev_time = time.time()
for i in tqdm(range(num_activations // batch_size)):
# If offloading is enabled and the buffer needs to be refreshed, offload the encoder and its optimizer to the
# offload device to free up memory on the primary device, which is needed by the buffer to load the next batch
# of activations.
if perform_offloading and buffer.will_refresh(batch=batch_size):
encoder = encoder.to(offload_device)
optimizer_to(optimizer, offload_device)
torch.cuda.empty_cache()
acts = buffer.next(batch=batch_size).to(encoder_cfg.device, non_blocking=True)
encoder = encoder.to(primary_device)
optimizer_to(optimizer, primary_device)
gc.collect()
else:
acts = buffer.next(batch=batch_size).to(encoder_cfg.device, non_blocking=True)
# 0 in the second dimension since we are only using one layer
enc, loss, l1, mse = encoder(acts[:, 0, :])
loss.backward()
optimizer.step()
scheduler.step()
optimizer.zero_grad()
if i % steps_per_report == 0 and i > 0:
freqs, avg_fired, fvu = encoder.get_data()
wandb.log({
"l1": l1.item(),
"mse": mse.item(),
"total_loss": loss.item(),
"ms_per_act": 1000 * (time.time() - prev_time) / (batch_size * steps_per_report),
"avg_neurons_fired": avg_fired,
"lr": scheduler.get_last_lr()[0],
"feature_density": wandb.Histogram(freqs.log10().nan_to_num(neginf=-10).cpu()),
})
if i % steps_per_save == 0:
encoder.save("chk")
torch.cuda.empty_cache()
prev_time = time.time()
finally:
# Save the model
encoder.save("final")