-
Notifications
You must be signed in to change notification settings - Fork 37
/
training.py
167 lines (144 loc) · 5.02 KB
/
training.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
"""
Training dictionaries
"""
import json
import multiprocessing as mp
import os
from queue import Empty
import torch as t
from tqdm import tqdm
import wandb
from .dictionary import AutoEncoder
from .evaluation import evaluate
from .trainers.standard import StandardTrainer
def new_wandb_process(config, log_queue, entity, project):
wandb.init(entity=entity, project=project, config=config, name=config["wandb_name"])
while True:
try:
log = log_queue.get(timeout=1)
if log == "DONE":
break
wandb.log(log)
except Empty:
continue
wandb.finish()
def log_stats(
trainers,
step: int,
act: t.Tensor,
activations_split_by_head: bool,
transcoder: bool,
log_queues: list=[],
):
with t.no_grad():
# quick hack to make sure all trainers get the same x
z = act.clone()
for i, trainer in enumerate(trainers):
log = {}
act = z.clone()
if activations_split_by_head: # x.shape: [batch, pos, n_heads, d_head]
act = act[..., i, :]
if not transcoder:
act, act_hat, f, losslog = trainer.loss(act, step=step, logging=True)
# L0
l0 = (f != 0).float().sum(dim=-1).mean().item()
# fraction of variance explained
total_variance = t.var(act, dim=0).sum()
residual_variance = t.var(act - act_hat, dim=0).sum()
frac_variance_explained = 1 - residual_variance / total_variance
log[f"frac_variance_explained"] = frac_variance_explained.item()
else: # transcoder
x, x_hat, f, losslog = trainer.loss(act, step=step, logging=True)
# L0
l0 = (f != 0).float().sum(dim=-1).mean().item()
# log parameters from training
log.update({f"{k}": v for k, v in losslog.items()})
log[f"l0"] = l0
trainer_log = trainer.get_logging_parameters()
for name, value in trainer_log.items():
log[f"{name}"] = value
if log_queues:
log_queues[i].put(log)
def trainSAE(
data,
trainer_configs,
use_wandb=False,
wandb_entity="",
wandb_project="",
steps=None,
save_steps=None,
save_dir=None,
log_steps=None,
activations_split_by_head=False,
transcoder=False,
run_cfg={},
):
"""
Train SAEs using the given trainers
"""
trainers = []
for config in trainer_configs:
trainer_class = config["trainer"]
del config["trainer"]
trainers.append(trainer_class(**config))
wandb_processes = []
log_queues = []
if use_wandb:
for i, trainer in enumerate(trainers):
log_queue = mp.Queue()
log_queues.append(log_queue)
wandb_config = trainer.config | run_cfg
wandb_process = mp.Process(
target=new_wandb_process,
args=(wandb_config, log_queue, wandb_entity, wandb_project),
)
wandb_process.start()
wandb_processes.append(wandb_process)
# make save dirs, export config
if save_dir is not None:
save_dirs = [
os.path.join(save_dir, f"trainer_{i}") for i in range(len(trainer_configs))
]
for trainer, dir in zip(trainers, save_dirs):
os.makedirs(dir, exist_ok=True)
# save config
config = {"trainer": trainer.config}
try:
config["buffer"] = data.config
except:
pass
with open(os.path.join(dir, "config.json"), "w") as f:
json.dump(config, f, indent=4)
else:
save_dirs = [None for _ in trainer_configs]
for step, act in enumerate(tqdm(data, total=steps)):
if steps is not None and step >= steps:
break
# logging
if log_steps is not None and step % log_steps == 0:
log_stats(
trainers, step, act, activations_split_by_head, transcoder, log_queues=log_queues
)
# saving
if save_steps is not None and step % save_steps == 0:
for dir, trainer in zip(save_dirs, trainers):
if dir is not None:
if not os.path.exists(os.path.join(dir, "checkpoints")):
os.mkdir(os.path.join(dir, "checkpoints"))
t.save(
trainer.ae.state_dict(),
os.path.join(dir, "checkpoints", f"ae_{step}.pt"),
)
# training
for trainer in trainers:
trainer.update(step, act)
# save final SAEs
for save_dir, trainer in zip(save_dirs, trainers):
if save_dir is not None:
t.save(trainer.ae.state_dict(), os.path.join(save_dir, "ae.pt"))
# Signal wandb processes to finish
if use_wandb:
for queue in log_queues:
queue.put("DONE")
for process in wandb_processes:
process.join()