-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.py
310 lines (256 loc) · 9.57 KB
/
demo.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
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
import torch as t
from nnsight import LanguageModel
import argparse
import itertools
import os
import random
import json
import torch.multiprocessing as mp
import time
import huggingface_hub
from datasets import config
import demo_config
from dictionary_learning.utils import hf_dataset_to_generator
from dictionary_learning.buffer import ActivationBuffer
from dictionary_learning.evaluation import evaluate
from dictionary_learning.training import trainSAE
import dictionary_learning.utils as utils
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--save_dir", type=str, required=True, help="where to store sweep")
parser.add_argument("--use_wandb", action="store_true", help="use wandb logging")
parser.add_argument("--dry_run", action="store_true", help="dry run sweep")
parser.add_argument("--save_checkpoints", action="store_true", help="save checkpoints")
parser.add_argument(
"--layers", type=int, nargs="+", required=True, help="layers to train SAE on"
)
parser.add_argument(
"--model_name",
type=str,
required=True,
help="which language model to use",
)
parser.add_argument(
"--architectures",
type=str,
nargs="+",
choices=[e.value for e in demo_config.TrainerType],
required=True,
help="which SAE architectures to train",
)
parser.add_argument("--device", type=str, default="cuda:0", help="device to train on")
parser.add_argument("--hf_repo_id", type=str, help="Hugging Face repo ID to push results to")
args = parser.parse_args()
return args
def run_sae_training(
model_name: str,
layer: int,
save_dir: str,
device: str,
architectures: list,
num_tokens: int,
random_seeds: list[int],
dictionary_widths: list[int],
learning_rates: list[float],
dry_run: bool = False,
use_wandb: bool = False,
save_checkpoints: bool = False,
buffer_tokens: int = 250_000,
):
random.seed(demo_config.random_seeds[0])
t.manual_seed(demo_config.random_seeds[0])
# model and data parameters
context_length = demo_config.LLM_CONFIG[model_name].context_length
llm_batch_size = demo_config.LLM_CONFIG[model_name].llm_batch_size
sae_batch_size = demo_config.LLM_CONFIG[model_name].sae_batch_size
dtype = demo_config.LLM_CONFIG[model_name].dtype
num_buffer_inputs = buffer_tokens // context_length
print(f"buffer_size: {num_buffer_inputs}, buffer_size_in_tokens: {buffer_tokens}")
log_steps = 100 # Log the training on wandb or print to console every log_steps
steps = int(num_tokens / sae_batch_size) # Total number of batches to train
if save_checkpoints:
# Creates checkpoints at 0.0%, 0.1%, 0.316%, 1%, 3.16%, 10%, 31.6%, 100% of training
desired_checkpoints = t.logspace(-3, 0, 7).tolist()
desired_checkpoints = [0.0] + desired_checkpoints[:-1]
desired_checkpoints.sort()
print(f"desired_checkpoints: {desired_checkpoints}")
save_steps = [int(steps * step) for step in desired_checkpoints]
save_steps.sort()
print(f"save_steps: {save_steps}")
else:
save_steps = None
model = LanguageModel(model_name, dispatch=True, device_map=device)
model = model.to(dtype=dtype)
submodule = utils.get_submodule(model, layer)
submodule_name = f"resid_post_layer_{layer}"
io = "out"
activation_dim = model.config.hidden_size
generator = hf_dataset_to_generator("monology/pile-uncopyrighted")
activation_buffer = ActivationBuffer(
generator,
model,
submodule,
n_ctxs=num_buffer_inputs,
ctx_len=context_length,
refresh_batch_size=llm_batch_size,
out_batch_size=sae_batch_size,
io=io,
d_submodule=activation_dim,
device=device,
)
trainer_configs = demo_config.get_trainer_configs(
architectures,
learning_rates,
random_seeds,
activation_dim,
dictionary_widths,
model_name,
device,
layer,
submodule_name,
steps,
)
print(f"len trainer configs: {len(trainer_configs)}")
assert len(trainer_configs) > 0
save_dir = f"{save_dir}/{submodule_name}"
if not dry_run:
# actually run the sweep
trainSAE(
data=activation_buffer,
trainer_configs=trainer_configs,
use_wandb=use_wandb,
steps=steps,
save_steps=save_steps,
save_dir=save_dir,
log_steps=log_steps,
wandb_project=demo_config.wandb_project,
normalize_activations=True,
verbose=False,
autocast_dtype=t.bfloat16,
)
@t.no_grad()
def eval_saes(
model_name: str,
ae_paths: list[str],
n_inputs: int,
device: str,
overwrite_prev_results: bool = False,
transcoder: bool = False,
) -> dict:
random.seed(demo_config.random_seeds[0])
t.manual_seed(demo_config.random_seeds[0])
if transcoder:
io = "in_and_out"
else:
io = "out"
context_length = demo_config.LLM_CONFIG[model_name].context_length
llm_batch_size = demo_config.LLM_CONFIG[model_name].llm_batch_size
loss_recovered_batch_size = max(llm_batch_size // 5, 1)
sae_batch_size = loss_recovered_batch_size * context_length
dtype = demo_config.LLM_CONFIG[model_name].dtype
model = LanguageModel(model_name, dispatch=True, device_map=device)
model = model.to(dtype=dtype)
buffer_size = n_inputs
io = "out"
n_batches = n_inputs // loss_recovered_batch_size
generator = hf_dataset_to_generator("monology/pile-uncopyrighted")
input_strings = []
for i, example in enumerate(generator):
input_strings.append(example)
if i > n_inputs * 5:
break
eval_results = {}
for ae_path in ae_paths:
output_filename = f"{ae_path}/eval_results.json"
if not overwrite_prev_results:
if os.path.exists(output_filename):
print(f"Skipping {ae_path} as eval results already exist")
continue
dictionary, config = utils.load_dictionary(ae_path, device)
dictionary = dictionary.to(dtype=model.dtype)
layer = config["trainer"]["layer"]
submodule = utils.get_submodule(model, layer)
activation_dim = config["trainer"]["activation_dim"]
activation_buffer = ActivationBuffer(
iter(input_strings),
model,
submodule,
n_ctxs=buffer_size,
ctx_len=context_length,
refresh_batch_size=llm_batch_size,
out_batch_size=sae_batch_size,
io=io,
d_submodule=activation_dim,
device=device,
)
eval_results = evaluate(
dictionary,
activation_buffer,
context_length,
loss_recovered_batch_size,
io=io,
device=device,
n_batches=n_batches,
)
hyperparameters = {
"n_inputs": n_inputs,
"context_length": context_length,
}
eval_results["hyperparameters"] = hyperparameters
print(eval_results)
with open(output_filename, "w") as f:
json.dump(eval_results, f)
# return the final eval_results for testing purposes
return eval_results
def push_to_huggingface(save_dir: str, repo_id: str):
api = huggingface_hub.HfApi()
api.upload_folder(
folder_path=save_dir,
repo_id=repo_id,
repo_type="model",
path_in_repo=save_dir,
)
if __name__ == "__main__":
"""python demo.py --save_dir ./run2 --model_name EleutherAI/pythia-70m-deduped --layers 3 --architectures standard jump_relu batch_top_k top_k gated --use_wandb
python demo.py --save_dir ./run3 --model_name google/gemma-2-2b --layers 12 --architectures standard top_k --use_wandb
python demo.py --save_dir ./jumprelu --model_name EleutherAI/pythia-70m-deduped --layers 3 --architectures jump_relu --use_wandb"""
args = get_args()
hf_repo_id = args.hf_repo_id
if hf_repo_id:
assert huggingface_hub.repo_exists(repo_id=hf_repo_id, repo_type="model")
# This prevents random CUDA out of memory errors
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
# For wandb to work with multiprocessing
mp.set_start_method("spawn", force=True)
# Rarely I have internet issues on cloud GPUs and then the streaming read fails
# Hopefully the outage is shorter than 100 * 20 seconds
config.STREAMING_READ_MAX_RETRIES = 100
config.STREAMING_READ_RETRY_INTERVAL = 20
start_time = time.time()
save_dir = f"{args.save_dir}_{args.model_name}_{'_'.join(args.architectures)}".replace("/", "_")
for layer in args.layers:
run_sae_training(
model_name=args.model_name,
layer=layer,
save_dir=save_dir,
device=args.device,
architectures=args.architectures,
num_tokens=demo_config.num_tokens,
random_seeds=demo_config.random_seeds,
dictionary_widths=demo_config.dictionary_widths,
learning_rates=demo_config.learning_rates,
dry_run=args.dry_run,
use_wandb=args.use_wandb,
save_checkpoints=args.save_checkpoints,
)
ae_paths = utils.get_nested_folders(save_dir)
eval_saes(
args.model_name,
ae_paths,
demo_config.eval_num_inputs,
args.device,
overwrite_prev_results=True,
)
print(f"Total time: {time.time() - start_time}")
if hf_repo_id:
push_to_huggingface(save_dir, hf_repo_id)