-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcircuit.py
More file actions
674 lines (612 loc) · 21.9 KB
/
circuit.py
File metadata and controls
674 lines (612 loc) · 21.9 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
import argparse
import gc
import json
import math
import os
from collections import defaultdict
import torch as t
from tqdm import tqdm
from attribution import patching_effect, jvp
from circuit_plotting import plot_circuit, plot_circuit_posaligned
from dictionary_learning import AutoEncoder
from data_loading_utils import load_examples, load_examples_nopair
from dictionary_loading_utils import load_saes_and_submodules
from nnsight import LanguageModel
from coo_utils import sparse_reshape
def get_circuit(
clean,
patch,
model,
embed,
attns,
mlps,
resids,
dictionaries,
metric_fn,
metric_kwargs=dict(),
aggregation="sum", # or "none" for not aggregating across sequence position
nodes_only=False,
parallel_attn=False,
node_threshold=0.1,
):
all_submods = ([embed] if embed is not None else []) + [
submod for layer_submods in zip(attns, mlps, resids) for submod in layer_submods
]
# first get the patching effect of everything on y
effects, deltas, grads, total_effect = patching_effect(
clean,
patch,
model,
all_submods,
dictionaries,
metric_fn,
metric_kwargs=metric_kwargs,
method="ig", # get better approximations for early layers by using ig
)
features_by_submod = {
submod: effects[submod].abs() > node_threshold for submod in all_submods
}
n_layers = len(resids)
nodes = {"y": total_effect}
if embed is not None:
nodes["embed"] = effects[embed]
for i in range(n_layers):
nodes[f"attn_{i}"] = effects[attns[i]]
nodes[f"mlp_{i}"] = effects[mlps[i]]
nodes[f"resid_{i}"] = effects[resids[i]]
if nodes_only:
if aggregation == "sum":
for k in nodes:
if k != "y":
nodes[k] = nodes[k].sum(dim=1)
nodes = {k: v.mean(dim=0) for k, v in nodes.items()}
return nodes, None
edges = defaultdict(lambda: {})
edges[f"resid_{len(resids) - 1}"] = {
"y": effects[resids[-1]].to_tensor().flatten().to_sparse()
}
def N(upstream, downstream, midstream=[]):
result = jvp(
clean,
model,
dictionaries,
downstream,
features_by_submod[downstream],
upstream,
grads[downstream],
deltas[upstream],
intermediate_stopgrads=midstream,
)
return result
# now we work backward through the model to get the edges
for layer in reversed(range(len(resids))):
resid = resids[layer]
mlp = mlps[layer]
attn = attns[layer]
MR_effect = N(mlp, resid)
AR_effect = N(attn, resid, [mlp])
edges[f"mlp_{layer}"][f"resid_{layer}"] = MR_effect
edges[f"attn_{layer}"][f"resid_{layer}"] = AR_effect
if not parallel_attn:
AM_effect = N(attn, mlp)
edges[f"attn_{layer}"][f"mlp_{layer}"] = AM_effect
if layer > 0:
prev_resid = resids[layer - 1]
else:
prev_resid = embed
if prev_resid is not None:
RM_effect = N(prev_resid, mlp, [attn])
RA_effect = N(prev_resid, attn)
RR_effect = N(prev_resid, resid, [mlp, attn])
if layer > 0:
edges[f"resid_{layer - 1}"][f"mlp_{layer}"] = RM_effect
edges[f"resid_{layer - 1}"][f"attn_{layer}"] = RA_effect
edges[f"resid_{layer - 1}"][f"resid_{layer}"] = RR_effect
else:
edges["embed"][f"mlp_{layer}"] = RM_effect
edges["embed"][f"attn_{layer}"] = RA_effect
edges["embed"]["resid_0"] = RR_effect
# rearrange weight matrices
for child in edges:
# get shape for child
bc, sc, fc = nodes[child].act.shape
for parent in edges[child]:
weight_matrix = edges[child][parent]
if parent == "y":
weight_matrix = sparse_reshape(weight_matrix, (bc, sc, fc + 1))
else:
continue
edges[child][parent] = weight_matrix
if aggregation == "sum":
# aggregate across sequence position
for child in edges:
for parent in edges[child]:
weight_matrix = edges[child][parent]
if parent == "y":
weight_matrix = weight_matrix.sum(dim=1)
else:
weight_matrix = weight_matrix.sum(dim=(1, 4))
edges[child][parent] = weight_matrix
for node in nodes:
if node != "y":
nodes[node] = nodes[node].sum(dim=1)
# aggregate across batch dimension
for child in edges:
bc, _ = nodes[child].act.shape
for parent in edges[child]:
weight_matrix = edges[child][parent]
if parent == "y":
weight_matrix = weight_matrix.sum(dim=0) / bc
else:
bp, _ = nodes[parent].act.shape
assert bp == bc
weight_matrix = weight_matrix.sum(dim=(0, 2)) / bc
edges[child][parent] = weight_matrix
for node in nodes:
if node != "y":
nodes[node] = nodes[node].mean(dim=0)
elif aggregation == "none":
# aggregate across batch dimensions
for child in edges:
# get shape for child
bc, sc, fc = nodes[child].act.shape
for parent in edges[child]:
weight_matrix = edges[child][parent]
if parent == "y":
weight_matrix = sparse_reshape(weight_matrix, (bc, sc, fc + 1))
weight_matrix = weight_matrix.sum(dim=0) / bc
else:
bp, sp, fp = nodes[parent].act.shape
assert bp == bc
weight_matrix = weight_matrix.sum(dim=(0, 3)) / bc
edges[child][parent] = weight_matrix
for node in nodes:
nodes[node] = nodes[node].mean(dim=0)
else:
raise ValueError(f"Unknown aggregation: {aggregation}")
return nodes, edges
def get_circuit_cluster(
dataset,
model_name="EleutherAI/pythia-70m-deduped",
d_model=512,
dict_id=10,
dict_size=32768,
max_length=64,
max_examples=100,
batch_size=2,
node_threshold=0.1,
edge_threshold=0.01,
device="cuda:0",
dict_path="dictionaries/pythia-70m-deduped/",
dataset_name="cluster_circuit",
circuit_dir="circuits/",
plot_dir="circuits/figures/",
model=None,
dictionaries=None,
):
n_layers = {
"EleutherAI/pythia-70m-deduped": 6,
"google/gemma-2-2b": 26,
}[model_name]
parallel_attn = {
"EleutherAI/pythia-70m-deduped": True,
"google/gemma-2-2b": False,
}[model_name]
include_embed = {
"EleutherAI/pythia-70m-deduped": True,
"google/gemma-2-2b": False,
}[model_name]
dtype = {
"EleutherAI/pythia-70m-deduped": t.float32,
"google/gemma-2-2b": t.bfloat16,
}[model_name]
if model_name == "EleutherAI/pythia-70m-deduped":
model = LanguageModel(model_name, device_map=device, dispatch=True, torch_dtype=dtype)
elif model_name == "google/gemma-2-2b":
model = LanguageModel(model_name, device_map=device, dispatch=True, attn_implementation="eager", torch_dtype=dtype)
submodules, dictionaries = load_saes_and_submodules(
model,
separate_by_type=True,
include_embed=include_embed,
device=device,
dtype=dtype,
)
examples = load_examples_nopair(dataset, max_examples, model)
num_examples = min(len(examples), max_examples)
n_batches = math.ceil(num_examples / batch_size)
batches = [
examples[batch * batch_size : (batch + 1) * batch_size]
for batch in range(n_batches)
]
if num_examples < max_examples: # warn the user
print(
f"Total number of examples is less than {max_examples}. Using {num_examples} examples instead."
)
running_nodes = None
running_edges = None
for batch in tqdm(batches, desc="Batches"):
clean_inputs = [e["clean_prefix"] for e in batch]
clean_answer_idxs = t.tensor(
[model.tokenizer(e["clean_answer"]).input_ids[-1] for e in batch],
dtype=t.long,
device=device
)
patch_inputs = None
def metric_fn(model):
return -1 * t.gather(
model.output.logits[:, -1, :],
dim=-1,
index=clean_answer_idxs.view(-1, 1),
).squeeze(-1)
nodes, edges = get_circuit(
clean_inputs,
patch_inputs,
model,
submodules.embed,
submodules.attns,
submodules.mlps,
submodules.resids,
dictionaries,
metric_fn,
aggregation="sum",
node_threshold=node_threshold,
edge_threshold=edge_threshold,
parallel_attn=parallel_attn,
)
if running_nodes is None:
running_nodes = {
k: len(batch) * nodes[k].to("cpu") for k in nodes.keys() if k != "y"
}
running_edges = {
k: {kk: len(batch) * edges[k][kk].to("cpu") for kk in edges[k].keys()}
for k in edges.keys()
}
else:
for k in nodes.keys():
if k != "y":
running_nodes[k] += len(batch) * nodes[k].to("cpu")
for k in edges.keys():
for v in edges[k].keys():
running_edges[k][v] += len(batch) * edges[k][v].to("cpu")
# memory cleanup
del nodes, edges
gc.collect()
nodes = {k: v.to(device) / num_examples for k, v in running_nodes.items()}
edges = {
k: {kk: 1 / num_examples * v.to(device) for kk, v in running_edges[k].items()}
for k in running_edges.keys()
}
save_dict = {"examples": examples, "nodes": nodes, "edges": edges}
save_basename = f"{dataset_name}_dict{dict_id}_node{node_threshold}_edge{edge_threshold}_n{num_examples}_aggsum"
with open(f"{circuit_dir}/{save_basename}.pt", "wb") as outfile:
t.save(save_dict, outfile)
nodes = save_dict["nodes"]
edges = save_dict["edges"]
annotations = None
plot_circuit(
nodes,
edges,
layers=n_layers,
node_threshold=node_threshold,
edge_threshold=edge_threshold,
pen_thickness=1,
annotations=annotations,
save_dir=os.path.join(plot_dir, save_basename),
gemma_mode=(model_name == "google/gemma-2-2b"),
parallel_attn=parallel_attn,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
"-d",
type=str,
default="simple_train",
help="A subject-verb agreement dataset in data/, or a path to a cluster .json.",
)
parser.add_argument(
"--num_examples",
"-n",
type=int,
default=100,
help="The number of examples from the --dataset over which to average indirect effects.",
)
parser.add_argument(
"--model",
type=str,
default="EleutherAI/pythia-70m-deduped",
help="The Huggingface ID of the model you wish to test.",
)
parser.add_argument(
"--dict_path",
type=str,
default="dictionaries/pythia-70m-deduped/",
help="Path to all dictionaries for your language model.",
)
parser.add_argument(
"--d_model", type=int, default=512, help="Hidden size of the language model."
)
parser.add_argument(
"--use_neurons",
default=False,
action="store_true",
help="Use neurons instead of features.",
)
parser.add_argument(
"--batch_size",
type=int,
default=32,
help="Number of examples to process at once when running circuit discovery.",
)
parser.add_argument(
"--aggregation",
type=str,
default="sum",
help="Aggregation across token positions. Should be one of `sum` or `none`.",
)
parser.add_argument(
"--node_threshold",
type=float,
default=0.2,
help="Indirect effect threshold for keeping circuit nodes.",
)
parser.add_argument(
"--edge_threshold",
type=float,
default=0.02,
help="Indirect effect threshold for keeping edges.",
)
parser.add_argument(
"--pen_thickness",
type=float,
default=1,
help="Scales the width of the edges in the circuit plot.",
)
parser.add_argument(
"--nopair",
default=False,
action="store_true",
help="Use if your data does not contain contrastive (minimal) pairs.",
)
parser.add_argument(
"--plot_circuit",
default=False,
action="store_true",
help="Plot the circuit after discovering it.",
)
parser.add_argument(
"--nodes_only",
default=False,
action="store_true",
help="Only search for causally implicated features; do not draw edges.",
)
parser.add_argument(
"--plot_only",
default=False,
action="store_true",
help="Do not run circuit discovery; just plot an existing circuit.",
)
parser.add_argument(
"--circuit_dir",
type=str,
default="circuits",
help="Directory to save/load circuits.",
)
parser.add_argument(
"--plot_dir",
type=str,
default="circuits/figures/",
help="Directory to save figures.",
)
parser.add_argument("--seed", type=int, default=12)
parser.add_argument("--device", type=str, default="cuda:0")
args = parser.parse_args()
device = t.device(args.device)
n_layers = {
"EleutherAI/pythia-70m-deduped": 6,
"google/gemma-2-2b": 26,
}[args.model]
parallel_attn = {
"EleutherAI/pythia-70m-deduped": True,
"google/gemma-2-2b": False,
}[args.model]
include_embed = {
"EleutherAI/pythia-70m-deduped": True,
"google/gemma-2-2b": False,
}[args.model]
dtype = {
"EleutherAI/pythia-70m-deduped": t.float32,
"google/gemma-2-2b": t.bfloat16,
}[args.model]
if args.model == "EleutherAI/pythia-70m-deduped":
model = LanguageModel(args.model, device_map=device, dispatch=True, torch_dtype=dtype)
elif args.model == "google/gemma-2-2b":
model = LanguageModel(
args.model,
device_map=device,
dispatch=True,
attn_implementation="eager",
torch_dtype=dtype,
)
if args.nopair:
data_path = f"data/{args.dataset}.json"
examples = load_examples_nopair(
data_path, args.num_examples, model
)
else:
data_path = f"data/{args.dataset}.json"
examples = load_examples(
data_path, args.num_examples, model, use_min_length_only=True
)
num_examples = min([args.num_examples, len(examples)])
if num_examples < args.num_examples: # warn the user
print(
f"Total number of examples is less than {args.num_examples}. Using {num_examples} examples instead."
)
batch_size = args.batch_size
n_batches = math.ceil(num_examples / batch_size)
batches = [
examples[batch * batch_size : (batch + 1) * batch_size]
for batch in range(n_batches)
]
loaded_from_disk = False
save_base = (
f"{args.model.split('/')[-1]}_{args.dataset}_n{num_examples}_agg{args.aggregation}"
+ ("_neurons" if args.use_neurons else "")
)
node_suffix = f"node{args.node_threshold}" if not args.nodes_only else "nodeall"
if os.path.exists(save_path := f"{args.circuit_dir}/{save_base}_{node_suffix}.pt"):
print(f"Loading circuit from {save_path}")
with open(save_path, "rb") as infile:
save_dict = t.load(infile)
nodes = save_dict["nodes"]
edges = save_dict["edges"]
loaded_from_disk = True
elif not args.nodes_only:
for f in os.listdir(args.circuit_dir):
if "nodeall" in f:
continue
if f.startswith(save_base):
node_thresh = float(f.split(".")[0].split("_node")[-1])
if node_thresh < args.node_threshold:
print(f"Loading circuit from {args.circuit_dir}/{f}")
with open(f"{args.circuit_dir}/{f}", "rb") as infile:
save_dict = t.load(infile)
nodes = save_dict["nodes"]
edges = save_dict["edges"]
loaded_from_disk = True
break
if not loaded_from_disk:
print("computing circuit")
submodules, dictionaries = load_saes_and_submodules(
model,
separate_by_type=True,
include_embed=include_embed,
neurons=args.use_neurons,
device=device,
dtype=dtype,
)
running_nodes = None
running_edges = None
for batch in tqdm(batches, desc="Batches"):
clean_inputs = [e["clean_prefix"] for e in batch]
clean_answer_idxs = t.tensor(
[model.tokenizer(e["clean_answer"]).input_ids[-1] for e in batch],
dtype=t.long,
device=device,
)
if args.nopair:
patch_inputs = None
def metric_fn(model):
return -1 * t.gather(
model.output.logits[:, -1, :],
dim=-1,
index=clean_answer_idxs.view(-1, 1),
).squeeze(-1)
else:
patch_inputs = [e["patch_prefix"] for e in batch]
patch_answer_idxs = t.tensor(
[model.tokenizer(e["patch_answer"]).input_ids[-1] for e in batch],
dtype=t.long,
device=device,
)
def metric_fn(model):
logits = model.output.logits[:, -1, :]
return t.gather(
logits, dim=-1, index=patch_answer_idxs.view(-1, 1)
).squeeze(-1) - t.gather(
logits, dim=-1, index=clean_answer_idxs.view(-1, 1)
).squeeze(-1)
nodes, edges = get_circuit(
clean_inputs,
patch_inputs,
model,
submodules.embed,
submodules.attns,
submodules.mlps,
submodules.resids,
dictionaries,
metric_fn,
nodes_only=args.nodes_only,
aggregation=args.aggregation,
node_threshold=args.node_threshold,
parallel_attn=parallel_attn,
)
if running_nodes is None:
running_nodes = {
k: len(batch) * nodes[k].to("cpu") for k in nodes.keys() if k != "y"
}
if not args.nodes_only:
running_edges = {
k: {
kk: len(batch) * edges[k][kk].to("cpu")
for kk in edges[k].keys()
}
for k in edges.keys()
}
else:
for k in nodes.keys():
if k != "y":
running_nodes[k] += len(batch) * nodes[k].to("cpu")
if not args.nodes_only:
for k in edges.keys():
for v in edges[k].keys():
running_edges[k][v] += len(batch) * edges[k][v].to("cpu")
# memory cleanup
del nodes, edges
gc.collect()
nodes = {k: v.to(device) / num_examples for k, v in running_nodes.items()}
if not args.nodes_only:
edges = {
k: {
kk: 1 / num_examples * v.to(device)
for kk, v in running_edges[k].items()
}
for k in running_edges.keys()
}
else:
edges = None
save_dict = {"examples": examples, "nodes": nodes, "edges": edges}
with open(save_path, "wb") as outfile:
t.save(save_dict, outfile)
# feature annotations
if os.path.exists(
annotations_path := f"annotations/{args.model.split('/')[-1]}.jsonl"
):
print(f"Loading feature annotations from {annotations_path}")
annotations = {}
with open(annotations_path, "r") as f:
for line in f:
line = json.loads(line)
if "Annotation" in line:
annotations[line["Name"]] = line["Annotation"]
else:
annotations = None
if args.aggregation == "none":
example = examples[0]["clean_prefix"]
plot_circuit_posaligned(
nodes,
edges,
layers=n_layers,
example_text=example,
node_threshold=args.node_threshold,
edge_threshold=args.edge_threshold,
pen_thickness=args.pen_thickness,
annotations=annotations,
save_dir=f"{args.plot_dir}/{save_base}_node{args.node_threshold}_edge{args.edge_threshold}",
gemma_mode=(args.model == "google/gemma-2-2b"),
parallel_attn=parallel_attn,
)
else:
plot_circuit(
nodes,
edges,
layers=n_layers,
node_threshold=args.node_threshold,
edge_threshold=args.edge_threshold,
pen_thickness=args.pen_thickness,
annotations=annotations,
save_dir=f"{args.plot_dir}/{save_base}_node{args.node_threshold}_edge{args.edge_threshold}_n{num_examples}_agg{args.aggregation}",
gemma_mode=(args.model == "google/gemma-2-2b"),
parallel_attn=parallel_attn,
)