-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathARENA_Indirect_Object_Identification.py
More file actions
1722 lines (1364 loc) · 63.3 KB
/
ARENA_Indirect_Object_Identification.py
File metadata and controls
1722 lines (1364 loc) · 63.3 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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# %%
import re
import sys
from functools import partial
from itertools import product
from pathlib import Path
from typing import Callable, Literal
import circuitsvis as cv
import einops
import numpy as np
import plotly.express as px
import torch as t
from IPython.display import HTML, display
from jaxtyping import Bool, Float, Int
from rich import print as rprint
from rich.table import Column, Table
from torch import Tensor
from tqdm.notebook import tqdm
from transformer_lens import ActivationCache, HookedTransformer, utils
from transformer_lens.components import MLP, Embed, LayerNorm, Unembed
from transformer_lens.hook_points import HookPoint
t.set_grad_enabled(False)
device = t.device("mps" if t.backends.mps.is_available() else "cuda" if t.cuda.is_available() else "cpu")
import ARENA_files.arena_part41_ioi_tests as tests
from ARENA_files.arena_plotly_utils import bar, imshow, line, scatter
MAIN = __name__ == "__main__"
# %%
if MAIN:
model = HookedTransformer.from_pretrained(
"gpt2-small",
center_unembed=True,
center_writing_weights=True,
fold_ln=True,
refactor_factored_attn_matrices=True,
)
# %%
if MAIN:
# Here is where we test on a single prompt
# Result: 70% probability on Mary, as we expect
example_prompt = "After John and Mary went to the store, John gave a bottle of milk to"
example_answer = " Mary"
utils.test_prompt(example_prompt, example_answer, model, prepend_bos=True)
# %%
if MAIN:
prompt_format = [
"When John and Mary went to the shops,{} gave the bag to",
"When Tom and James went to the park,{} gave the ball to",
"When Dan and Sid went to the shops,{} gave an apple to",
"After Martin and Amy went to the park,{} gave a drink to",
]
name_pairs = [
(" Mary", " John"),
(" Tom", " James"),
(" Dan", " Sid"),
(" Martin", " Amy"),
]
# Define 8 prompts, in 4 groups of 2 (with adjacent prompts having answers swapped)
prompts = [prompt.format(name) for (prompt, names) in zip(prompt_format, name_pairs) for name in names[::-1]]
# Define the answers for each prompt, in the form (correct, incorrect)
answers = [names[::i] for names in name_pairs for i in (1, -1)]
# Define the answer tokens (same shape as the answers)
answer_tokens = t.concat([model.to_tokens(names, prepend_bos=False).T for names in answers])
rprint(prompts)
rprint(answers)
rprint(answer_tokens)
table = Table("Prompt", "Correct", "Incorrect", title="Prompts & Answers:")
for prompt, answer in zip(prompts, answers):
table.add_row(prompt, repr(answer[0]), repr(answer[1]))
rprint(table)
# %%
if MAIN:
tokens = model.to_tokens(prompts, prepend_bos=True)
# Move the tokens to the GPU
tokens = tokens.to(device)
# Run the model and cache all activations
original_logits, cache = model.run_with_cache(tokens)
# %%
if MAIN:
def logits_to_ave_logit_diff(
logits: Float[Tensor, "batch seq d_vocab"],
answer_tokens: Int[Tensor, "batch 2"] = answer_tokens,
per_prompt: bool = False,
) -> Float[Tensor, "*batch"]:
"""
Returns logit difference between the correct and incorrect answer.
If per_prompt=True, return the array of differences rather than the average.
"""
# Only the final logits are relevant for the answer
final_logits: Float[Tensor, "batch d_vocab"] = logits[:, -1, :]
# Get the logits corresponding to the indirect object / subject tokens respectively
answer_logits: Float[Tensor, "batch 2"] = final_logits.gather(dim=-1, index=answer_tokens)
# Find logit difference
correct_logits, incorrect_logits = answer_logits.unbind(dim=-1)
answer_logit_diff = correct_logits - incorrect_logits
return answer_logit_diff if per_prompt else answer_logit_diff.mean()
tests.test_logits_to_ave_logit_diff(logits_to_ave_logit_diff)
original_per_prompt_diff = logits_to_ave_logit_diff(original_logits, answer_tokens, per_prompt=True)
print("Per prompt logit difference:", original_per_prompt_diff)
original_average_logit_diff = logits_to_ave_logit_diff(original_logits, answer_tokens)
print("Average logit difference:", original_average_logit_diff)
cols = [
"Prompt",
Column("Correct", style="rgb(0,200,0) bold"),
Column("Incorrect", style="rgb(255,0,0) bold"),
Column("Logit Difference", style="bold"),
]
table = Table(*cols, title="Logit differences")
for prompt, answer, logit_diff in zip(prompts, answers, original_per_prompt_diff):
table.add_row(prompt, repr(answer[0]), repr(answer[1]), f"{logit_diff.item():.3f}")
rprint(table)
# %%
if MAIN:
answer_residual_directions = model.tokens_to_residual_directions(answer_tokens) # [batch 2 d_model]
print("Answer residual directions shape:", answer_residual_directions.shape)
correct_residual_directions, incorrect_residual_directions = answer_residual_directions.unbind(dim=1)
logit_diff_directions = correct_residual_directions - incorrect_residual_directions # [batch d_model]
print("Logit difference directions shape:", logit_diff_directions.shape)
# %%
if MAIN:
# cache syntax - resid_post is the residual stream at the end of the layer, -1 gets the final layer. The general syntax is [activation_name, layer_index, sub_layer_type].
final_residual_stream: Float[Tensor, "batch seq d_model"] = cache["resid_post", -1]
print(f"Final residual stream shape: {final_residual_stream.shape}")
final_token_residual_stream: Float[Tensor, "batch d_model"] = final_residual_stream[:, -1, :]
# Apply LayerNorm scaling (to just the final sequence position)
# pos_slice is the subset of the positions we take - here the final token of each prompt
scaled_final_token_residual_stream = cache.apply_ln_to_stack(final_token_residual_stream, layer=-1, pos_slice=-1)
average_logit_diff = einops.einsum(
scaled_final_token_residual_stream, logit_diff_directions, "batch d_model, batch d_model ->"
) / len(prompts)
print(f"Calculated average logit diff: {average_logit_diff:.10f}")
print(f"Original logit difference: {original_average_logit_diff:.10f}")
t.testing.assert_close(average_logit_diff, original_average_logit_diff)
# %%
if MAIN:
def residual_stack_to_logit_diff(
residual_stack: Float[Tensor, "... batch d_model"],
cache: ActivationCache,
logit_diff_directions: Float[Tensor, "batch d_model"] = logit_diff_directions,
) -> Float[Tensor, "..."]:
"""
Gets the avg logit difference between the correct and incorrect answer for a given stack of components in the
residual stream.
"""
batch_size = residual_stack.size(-2)
scaled_residual_stack = cache.apply_ln_to_stack(residual_stack, layer=-1, pos_slice=-1)
return (
einops.einsum(scaled_residual_stack, logit_diff_directions, "... batch d_model, batch d_model -> ...")
/ batch_size
)
# Test function by checking that it gives the same result as the original logit difference
t.testing.assert_close(
residual_stack_to_logit_diff(final_token_residual_stream, cache), original_average_logit_diff
)
# %%
if MAIN:
accumulated_residual, labels = cache.accumulated_resid(layer=-1, incl_mid=True, pos_slice=-1, return_labels=True)
# accumulated_residual has shape (component, batch, d_model)
logit_lens_logit_diffs: Float[Tensor, "component"] = residual_stack_to_logit_diff(accumulated_residual, cache)
line(
logit_lens_logit_diffs,
hovermode="x unified",
title="Logit Difference From Accumulated Residual Stream",
labels={"x": "Layer", "y": "Logit Diff"},
xaxis_tickvals=labels,
width=800,
)
# %%
if MAIN:
per_layer_residual, labels = cache.decompose_resid(layer=-1, pos_slice=-1, return_labels=True)
per_layer_logit_diffs = residual_stack_to_logit_diff(per_layer_residual, cache)
line(
per_layer_logit_diffs,
hovermode="x unified",
title="Logit Difference From Each Layer",
labels={"x": "Layer", "y": "Logit Diff"},
xaxis_tickvals=labels,
width=800,
)
# %%
if MAIN:
per_head_residual, labels = cache.stack_head_results(layer=-1, pos_slice=-1, return_labels=True)
per_head_residual = einops.rearrange(
per_head_residual, "(layer head) ... -> layer head ...", layer=model.cfg.n_layers
)
per_head_logit_diffs = residual_stack_to_logit_diff(per_head_residual, cache)
imshow(
per_head_logit_diffs,
labels={"x": "Head", "y": "Layer"},
title="Logit Difference From Each Head",
width=600,
)
# %%
def topk_of_Nd_tensor(tensor: Float[Tensor, "rows cols"], k: int):
"""
Helper function: does same as tensor.topk(k).indices, but works over 2D tensors.
Returns a list of indices, i.e. shape [k, tensor.ndim].
Example: if tensor is 2D array of values for each head in each layer, this will
return a list of heads.
"""
i = t.topk(tensor.flatten(), k).indices
return np.array(np.unravel_index(utils.to_numpy(i), tensor.shape)).T.tolist()
if MAIN:
k = 3
for head_type in ["Positive", "Negative"]:
# Get the heads with largest (or smallest) contribution to the logit difference
top_heads = topk_of_Nd_tensor(per_head_logit_diffs * (1 if head_type == "Positive" else -1), k)
# Get all their attention patterns
attn_patterns_for_important_heads: Float[Tensor, "head q k"] = t.stack(
[cache["pattern", layer][:, head][0] for layer, head in top_heads]
)
# Display results
display(HTML(f"<h2>Top {k} {head_type} Logit Attribution Heads</h2>"))
display(
cv.attention.attention_patterns(
attention=attn_patterns_for_important_heads,
tokens=model.to_str_tokens(tokens[0]),
attention_head_names=[f"{layer}.{head}" for layer, head in top_heads],
)
)
# %%
from transformer_lens import patching
# %%
if MAIN:
clean_tokens = tokens
# Swap each adjacent pair to get corrupted tokens
indices = [i + 1 if i % 2 == 0 else i - 1 for i in range(len(tokens))]
corrupted_tokens = clean_tokens[indices]
print(
"Clean string 0: ",
model.to_string(clean_tokens[0]),
"\nCorrupted string 0:",
model.to_string(corrupted_tokens[0]),
)
clean_logits, clean_cache = model.run_with_cache(clean_tokens)
corrupted_logits, corrupted_cache = model.run_with_cache(corrupted_tokens)
clean_logit_diff = logits_to_ave_logit_diff(clean_logits, answer_tokens)
print(f"Clean logit diff: {clean_logit_diff:.4f}")
corrupted_logit_diff = logits_to_ave_logit_diff(corrupted_logits, answer_tokens)
print(f"Corrupted logit diff: {corrupted_logit_diff:.4f}")
# %%
if MAIN:
def ioi_metric(
logits: Float[Tensor, "batch seq d_vocab"],
answer_tokens: Int[Tensor, "batch 2"] = answer_tokens,
corrupted_logit_diff: float = corrupted_logit_diff,
clean_logit_diff: float = clean_logit_diff,
) -> Float[Tensor, ""]:
"""
Linear function of logit diff, calibrated so that it equals 0 when performance is same as on corrupted input, and 1
when performance is same as on clean input.
"""
patched_logit_diff = logits_to_ave_logit_diff(logits, answer_tokens)
return (patched_logit_diff - corrupted_logit_diff) / (clean_logit_diff - corrupted_logit_diff)
t.testing.assert_close(ioi_metric(clean_logits).item(), 1.0)
t.testing.assert_close(ioi_metric(corrupted_logits).item(), 0.0)
t.testing.assert_close(ioi_metric((clean_logits + corrupted_logits) / 2).item(), 0.5)
# %%
if MAIN:
act_patch_resid_pre = patching.get_act_patch_resid_pre(
model=model, corrupted_tokens=corrupted_tokens, clean_cache=clean_cache, patching_metric=ioi_metric
)
labels = [f"{tok} {i}" for i, tok in enumerate(model.to_str_tokens(clean_tokens[0]))]
imshow(
act_patch_resid_pre,
labels={"x": "Position", "y": "Layer"},
x=labels,
title="resid_pre Activation Patching",
width=700,
)
# %%
def patch_residual_component(
corrupted_residual_component: Float[Tensor, "batch pos d_model"],
hook: HookPoint,
pos: int,
clean_cache: ActivationCache,
) -> Float[Tensor, "batch pos d_model"]:
"""
Patches a given sequence position in the residual stream, using the value
from the clean cache.
"""
corrupted_residual_component[:, pos, :] = clean_cache[hook.name][:, pos, :]
return corrupted_residual_component
def get_act_patch_resid_pre(
model: HookedTransformer,
corrupted_tokens: Float[Tensor, "batch pos"],
clean_cache: ActivationCache,
patching_metric: Callable[[Float[Tensor, "batch pos d_vocab"]], float],
) -> Float[Tensor, "3 layer pos"]:
"""
Returns an array of results of patching each position at each layer in the residual
stream, using the value from the clean cache.
The results are calculated using the patching_metric function, which should be
called on the model's logit output.
"""
model.reset_hooks()
seq_len = corrupted_tokens.size(1)
results = t.zeros(model.cfg.n_layers, seq_len, device=device, dtype=t.float32)
for layer in tqdm(range(model.cfg.n_layers)):
for position in range(seq_len):
hook_fn = partial(patch_residual_component, pos=position, clean_cache=clean_cache)
patched_logits = model.run_with_hooks(
corrupted_tokens,
fwd_hooks=[(utils.get_act_name("resid_pre", layer), hook_fn)],
)
results[layer, position] = patching_metric(patched_logits)
return results
if MAIN:
act_patch_resid_pre_own = get_act_patch_resid_pre(model, corrupted_tokens, clean_cache, ioi_metric)
t.testing.assert_close(act_patch_resid_pre, act_patch_resid_pre_own)
# %%
if MAIN:
imshow(
act_patch_resid_pre_own,
x=labels,
title="Logit Difference From Patched Residual Stream",
labels={"x": "Sequence Position", "y": "Layer"},
width=700,
)
# %%
if MAIN:
act_patch_block_every = patching.get_act_patch_block_every(model, corrupted_tokens, clean_cache, ioi_metric)
imshow(
act_patch_block_every,
x=labels,
facet_col=0, # This argument tells plotly which dimension to split into separate plots
facet_labels=["Residual Stream", "Attn Output", "MLP Output"], # Subtitles of separate plots
title="Logit Difference From Patched Attn Head Output",
labels={"x": "Sequence Position", "y": "Layer"},
width=1200,
)
# %%
def get_act_patch_block_every(
model: HookedTransformer,
corrupted_tokens: Float[Tensor, "batch pos"],
clean_cache: ActivationCache,
patching_metric: Callable[[Float[Tensor, "batch pos d_vocab"]], float],
) -> Float[Tensor, "3 layer pos"]:
"""
Returns an array of results of patching each position at each layer in the residual stream, using the value from the
clean cache.
The results are calculated using the patching_metric function, which should be called on the model's logit output.
"""
model.reset_hooks()
results = t.zeros(3, model.cfg.n_layers, tokens.size(1), device=device, dtype=t.float32)
for component_idx, component in enumerate(["resid_pre", "attn_out", "mlp_out"]):
for layer in tqdm(range(model.cfg.n_layers)):
for position in range(corrupted_tokens.shape[1]):
hook_fn = partial(patch_residual_component, pos=position, clean_cache=clean_cache)
patched_logits = model.run_with_hooks(
corrupted_tokens,
fwd_hooks=[(utils.get_act_name(component, layer), hook_fn)],
)
results[component_idx, layer, position] = patching_metric(patched_logits)
return results
if MAIN:
act_patch_block_every_own = get_act_patch_block_every(model, corrupted_tokens, clean_cache, ioi_metric)
t.testing.assert_close(act_patch_block_every, act_patch_block_every_own)
imshow(
act_patch_block_every_own,
x=labels,
facet_col=0,
facet_labels=["Residual Stream", "Attn Output", "MLP Output"],
title="Logit Difference From Patched Attn Head Output",
labels={"x": "Sequence Position", "y": "Layer"},
width=1200,
)
# %%
if MAIN:
act_patch_attn_head_out_all_pos = patching.get_act_patch_attn_head_out_all_pos(
model, corrupted_tokens, clean_cache, ioi_metric
)
imshow(
act_patch_attn_head_out_all_pos,
labels={"y": "Layer", "x": "Head"},
title="attn_head_out Activation Patching (All Pos)",
width=600,
)
# %%
def patch_head_vector(
corrupted_head_vector: Float[Tensor, "batch pos head_index d_head"],
hook: HookPoint,
head_index: int,
clean_cache: ActivationCache,
) -> Float[Tensor, "batch pos head_index d_head"]:
"""
Patches the output of a given head (before it's added to the residual stream) at every sequence position, using the
value from the clean cache.
"""
corrupted_head_vector[:, :, head_index] = clean_cache[hook.name][:, :, head_index]
return corrupted_head_vector
def get_act_patch_attn_head_out_all_pos(
model: HookedTransformer,
corrupted_tokens: Float[Tensor, "batch pos"],
clean_cache: ActivationCache,
patching_metric: Callable,
) -> Float[Tensor, "layer head"]:
"""
Returns an array of results of patching at all positions for each head in each layer, using the value from the clean
cache. The results are calculated using the patching_metric function, which should be called on the model's logit
output.
"""
model.reset_hooks()
results = t.zeros(model.cfg.n_layers, model.cfg.n_heads, device=device, dtype=t.float32)
for layer in tqdm(range(model.cfg.n_layers)):
for head in range(model.cfg.n_heads):
hook_fn = partial(patch_head_vector, head_index=head, clean_cache=clean_cache)
patched_logits = model.run_with_hooks(
corrupted_tokens, fwd_hooks=[(utils.get_act_name("z", layer), hook_fn)], return_type="logits"
)
results[layer, head] = patching_metric(patched_logits)
return results
if MAIN:
act_patch_attn_head_out_all_pos_own = get_act_patch_attn_head_out_all_pos(
model, corrupted_tokens, clean_cache, ioi_metric
)
t.testing.assert_close(act_patch_attn_head_out_all_pos, act_patch_attn_head_out_all_pos_own)
imshow(
act_patch_attn_head_out_all_pos_own,
title="Logit Difference From Patched Attn Head Output",
labels={"x": "Head", "y": "Layer"},
width=600,
)
# %%
if MAIN:
act_patch_attn_head_all_pos_every = patching.get_act_patch_attn_head_all_pos_every(
model, corrupted_tokens, clean_cache, ioi_metric
)
imshow(
act_patch_attn_head_all_pos_every,
facet_col=0,
facet_labels=["Output", "Query", "Key", "Value", "Pattern"],
title="Activation Patching Per Head (All Pos)",
labels={"x": "Head", "y": "Layer"},
width=1200,
)
# %%
def patch_attn_patterns(
corrupted_head_vector: Float[Tensor, "batch head_index pos_q pos_k"],
hook: HookPoint,
head_index: int,
clean_cache: ActivationCache,
) -> Float[Tensor, "batch pos head_index d_head"]:
"""
Patches the attn patterns of a given head at every sequence position, using the value from the clean cache.
"""
corrupted_head_vector[:, head_index] = clean_cache[hook.name][:, head_index]
return corrupted_head_vector
def get_act_patch_attn_head_all_pos_every(
model: HookedTransformer,
corrupted_tokens: Float[Tensor, "batch pos"],
clean_cache: ActivationCache,
patching_metric: Callable,
) -> Float[Tensor, "layer head"]:
"""
Returns an array of results of patching at all positions for each head in each layer (using the value from the clean
cache) for output, queries, keys, values and attn pattern in turn.
The results are calculated using the patching_metric function, which should be called on the model's logit output.
"""
results = t.zeros(5, model.cfg.n_layers, model.cfg.n_heads, device=device, dtype=t.float32)
# Loop over each component in turn
for component_idx, component in enumerate(["z", "q", "k", "v", "pattern"]):
for layer in tqdm(range(model.cfg.n_layers)):
for head in range(model.cfg.n_heads):
# Get different hook function if we're doing attention probs
hook_fn_general = patch_attn_patterns if component == "pattern" else patch_head_vector
hook_fn = partial(hook_fn_general, head_index=head, clean_cache=clean_cache)
# Get patched logits
patched_logits = model.run_with_hooks(
corrupted_tokens, fwd_hooks=[(utils.get_act_name(component, layer), hook_fn)], return_type="logits"
)
results[component_idx, layer, head] = patching_metric(patched_logits)
return results
if MAIN:
act_patch_attn_head_all_pos_every_own = get_act_patch_attn_head_all_pos_every(
model, corrupted_tokens, clean_cache, ioi_metric
)
t.testing.assert_close(act_patch_attn_head_all_pos_every, act_patch_attn_head_all_pos_every_own)
imshow(
act_patch_attn_head_all_pos_every_own,
facet_col=0,
facet_labels=["Output", "Query", "Key", "Value", "Pattern"],
title="Activation Patching Per Head (All Pos)",
labels={"x": "Head", "y": "Layer"},
width=1200,
)
# %%
from ARENA_files.ioi_dataset import NAMES, IOIDataset
# %%
if MAIN:
N = 25
ioi_dataset = IOIDataset(
prompt_type="mixed",
N=N,
tokenizer=model.tokenizer,
prepend_bos=False,
seed=1,
device=str(device),
)
# %%
if MAIN:
abc_dataset = ioi_dataset.gen_flipped_prompts("ABB->XYZ, BAB->XYZ")
# %%
def format_prompt(sentence: str) -> str:
"""Format a prompt by underlining names (for rich print)"""
return re.sub("(" + "|".join(NAMES) + ")", lambda x: f"[u bold dark_orange]{x.group(0)}[/]", sentence) + "\n"
def make_table(cols, colnames, title="", n_rows=5, decimals=4):
"""Makes and displays a table, from cols rather than rows (using rich print)"""
table = Table(*colnames, title=title)
rows = list(zip(*cols))
f = lambda x: x if isinstance(x, str) else f"{x:.{decimals}f}"
for row in rows[:n_rows]:
table.add_row(*list(map(f, row)))
rprint(table)
if MAIN:
make_table(
colnames=["IOI prompt", "IOI subj", "IOI indirect obj", "ABC prompt"],
cols=[
map(format_prompt, ioi_dataset.sentences),
model.to_string(ioi_dataset.s_tokenIDs).split(),
model.to_string(ioi_dataset.io_tokenIDs).split(),
map(format_prompt, abc_dataset.sentences),
],
title="Sentences from IOI vs ABC distribution",
)
# %%
if MAIN:
def logits_to_ave_logit_diff_2(
logits: Float[Tensor, "batch seq d_vocab"], ioi_dataset: IOIDataset = ioi_dataset, per_prompt=False
) -> Float[Tensor, "*batch"]:
"""
Returns logit difference between the correct and incorrect answer.
If per_prompt=True, return the array of differences rather than the average.
"""
# Only the final logits are relevant for the answer
# Get the logits corresponding to the indirect object / subject tokens respectively
io_logits: Float[Tensor, "batch"] = logits[
range(logits.size(0)), ioi_dataset.word_idx["end"], ioi_dataset.io_tokenIDs
]
s_logits: Float[Tensor, "batch"] = logits[
range(logits.size(0)), ioi_dataset.word_idx["end"], ioi_dataset.s_tokenIDs
]
# Find logit difference
answer_logit_diff = io_logits - s_logits
return answer_logit_diff if per_prompt else answer_logit_diff.mean()
model.reset_hooks(including_permanent=True)
ioi_logits_original, ioi_cache = model.run_with_cache(ioi_dataset.toks)
abc_logits_original, abc_cache = model.run_with_cache(abc_dataset.toks)
ioi_per_prompt_diff = logits_to_ave_logit_diff_2(ioi_logits_original, per_prompt=True)
abc_per_prompt_diff = logits_to_ave_logit_diff_2(abc_logits_original, per_prompt=True)
ioi_average_logit_diff = logits_to_ave_logit_diff_2(ioi_logits_original).item()
abc_average_logit_diff = logits_to_ave_logit_diff_2(abc_logits_original).item()
print(f"Average logit diff (IOI dataset): {ioi_average_logit_diff:.4f}")
print(f"Average logit diff (ABC dataset): {abc_average_logit_diff:.4f}")
make_table(
colnames=["IOI prompt", "IOI logit diff", "ABC prompt", "ABC logit diff"],
cols=[
map(format_prompt, ioi_dataset.sentences),
ioi_per_prompt_diff,
map(format_prompt, abc_dataset.sentences),
abc_per_prompt_diff,
],
title="Sentences from IOI vs ABC distribution",
)
# %%
if MAIN:
def ioi_metric_2(
logits: Float[Tensor, "batch seq d_vocab"],
clean_logit_diff: float = ioi_average_logit_diff,
corrupted_logit_diff: float = abc_average_logit_diff,
ioi_dataset: IOIDataset = ioi_dataset,
) -> float:
"""
We calibrate this so that the value is 0 when performance isn't harmed (i.e. same as IOI dataset),
and -1 when performance has been destroyed (i.e. is same as ABC dataset).
"""
patched_logit_diff = logits_to_ave_logit_diff_2(logits, ioi_dataset)
return (patched_logit_diff - clean_logit_diff) / (clean_logit_diff - corrupted_logit_diff)
print(f"IOI metric (IOI dataset): {ioi_metric_2(ioi_logits_original):.4f}")
print(f"IOI metric (ABC dataset): {ioi_metric_2(abc_logits_original):.4f}")
# %%
if MAIN:
def patch_or_freeze_head_vectors(
orig_head_vector: Float[Tensor, "batch pos head_index d_head"],
hook: HookPoint,
new_cache: ActivationCache,
orig_cache: ActivationCache,
head_to_patch: tuple[int, int],
) -> Float[Tensor, "batch pos head_index d_head"]:
"""
This helps implement step 2 of path patching. We freeze all head outputs (i.e. set them to their values in
orig_cache), except for head_to_patch (if it's in this layer) which we patch with the value from new_cache.
head_to_patch: tuple of (layer, head)
"""
# Setting using ..., otherwise changing orig_head_vector will edit cache value too
orig_head_vector[...] = orig_cache[hook.name][...]
if head_to_patch[0] == hook.layer():
orig_head_vector[:, :, head_to_patch[1]] = new_cache[hook.name][:, :, head_to_patch[1]]
return orig_head_vector
def get_path_patch_head_to_final_resid_post(
model: HookedTransformer,
patching_metric: Callable,
new_dataset: IOIDataset = abc_dataset,
orig_dataset: IOIDataset = ioi_dataset,
new_cache: ActivationCache | None = abc_cache,
orig_cache: ActivationCache | None = ioi_cache,
) -> Float[Tensor, "layer head"]:
"""
Performs path patching (see algorithm in appendix B of IOI paper), with:
sender head = (each head, looped through, one at a time)
receiver node = final value of residual stream
Returns:
tensor of metric values for every possible sender head
"""
model.reset_hooks()
results = t.zeros(model.cfg.n_layers, model.cfg.n_heads, device=device, dtype=t.float32)
resid_post_hook_name = utils.get_act_name("resid_post", model.cfg.n_layers - 1)
resid_post_name_filter = lambda name: name == resid_post_hook_name
# ========== Step 1 ==========
# Gather activations on x_orig and x_new
# Note the use of names_filter for the run_with_cache function. Using it means we
# only cache the things we need (in this case, just attn head outputs).
z_name_filter = lambda name: name.endswith("z")
if new_cache is None:
_, new_cache = model.run_with_cache(new_dataset.toks, names_filter=z_name_filter, return_type=None)
if orig_cache is None:
_, orig_cache = model.run_with_cache(orig_dataset.toks, names_filter=z_name_filter, return_type=None)
# Looping over every possible sender head (the receiver is always the final resid_post)
for sender_layer, sender_head in tqdm(list(product(range(model.cfg.n_layers), range(model.cfg.n_heads)))):
# ========== Step 2 ==========
# Run on x_orig, with sender head patched from x_new, every other head frozen
hook_fn = partial(
patch_or_freeze_head_vectors,
new_cache=new_cache,
orig_cache=orig_cache,
head_to_patch=(sender_layer, sender_head),
)
model.add_hook(z_name_filter, hook_fn)
_, patched_cache = model.run_with_cache(
orig_dataset.toks, names_filter=resid_post_name_filter, return_type=None
)
# if (sender_layer, sender_head) == (9, 9):
# return patched_cache
assert set(patched_cache.keys()) == {resid_post_hook_name}
# ========== Step 3 ==========
# Unembed the final residual stream value, to get our patched logits
patched_logits = model.unembed(model.ln_final(patched_cache[resid_post_hook_name]))
# Save the results
results[sender_layer, sender_head] = patching_metric(patched_logits)
return results
path_patch_head_to_final_resid_post = get_path_patch_head_to_final_resid_post(model, ioi_metric_2)
imshow(
100 * path_patch_head_to_final_resid_post,
title="Direct effect on logit difference",
labels={"x": "Head", "y": "Layer", "color": "Logit diff. variation"},
coloraxis=dict(colorbar_ticksuffix="%"),
width=600,
)
# %%
if MAIN:
def patch_head_input(
orig_activation: Float[Tensor, "batch pos head_idx d_head"],
hook: HookPoint,
patched_cache: ActivationCache,
head_list: list[tuple[int, int]],
) -> Float[Tensor, "batch pos head_idx d_head"]:
"""
Function which can patch any combination of heads in layers,
according to the heads in head_list.
"""
heads_to_patch = [head for layer, head in head_list if layer == hook.layer()]
orig_activation[:, :, heads_to_patch] = patched_cache[hook.name][:, :, heads_to_patch]
return orig_activation
def get_path_patch_head_to_heads(
receiver_heads: list[tuple[int, int]],
receiver_input: str,
model: HookedTransformer,
patching_metric: Callable,
new_dataset: IOIDataset = abc_dataset,
orig_dataset: IOIDataset = ioi_dataset,
new_cache: ActivationCache | None = None,
orig_cache: ActivationCache | None = None,
) -> Float[Tensor, "layer head"]:
"""
Performs path patching (see algorithm in appendix B of IOI paper), with:
sender head = (each head, looped through, one at a time)
receiver node = input to a later head (or set of heads)
The receiver node is specified by receiver_heads and receiver_input, for example if receiver_input = "v" and
receiver_heads = [(8, 6), (8, 10), (7, 9), (7, 3)], we're doing path patching from each head to the value inputs of
the S-inhibition heads.
Returns:
tensor of metric values for every possible sender head
"""
model.reset_hooks()
assert receiver_input in ("k", "q", "v")
receiver_layers = set(next(zip(*receiver_heads)))
receiver_hook_names = [utils.get_act_name(receiver_input, layer) for layer in receiver_layers]
receiver_hook_names_filter = lambda name: name in receiver_hook_names
results = t.zeros(max(receiver_layers), model.cfg.n_heads, device=device, dtype=t.float32)
# ========== Step 1 ==========
# Gather activations on x_orig and x_new
# Note the use of names_filter for the run_with_cache function. Using it means we
# only cache the things we need (in this case, just attn head outputs).
z_name_filter = lambda name: name.endswith("z")
if new_cache is None:
_, new_cache = model.run_with_cache(new_dataset.toks, names_filter=z_name_filter, return_type=None)
if orig_cache is None:
_, orig_cache = model.run_with_cache(orig_dataset.toks, names_filter=z_name_filter, return_type=None)
# Note, the sender layer will always be before the final receiver layer, otherwise there will
# be no causal effect from sender -> receiver. So we only need to loop this far.
for sender_layer, sender_head in tqdm(list(product(range(max(receiver_layers)), range(model.cfg.n_heads)))):
# ========== Step 2 ==========
# Run on x_orig, with sender head patched from x_new, every other head frozen
hook_fn = partial(
patch_or_freeze_head_vectors,
new_cache=new_cache,
orig_cache=orig_cache,
head_to_patch=(sender_layer, sender_head),
)
model.add_hook(z_name_filter, hook_fn, level=1)
_, patched_cache = model.run_with_cache(
orig_dataset.toks, names_filter=receiver_hook_names_filter, return_type=None
)
# model.reset_hooks(including_permanent=True)
assert set(patched_cache.keys()) == set(receiver_hook_names)
# ========== Step 3 ==========
# Run on x_orig, patching in the receiver node(s) from the previously cached value
hook_fn = partial(
patch_head_input,
patched_cache=patched_cache,
head_list=receiver_heads,
)
patched_logits = model.run_with_hooks(
orig_dataset.toks, fwd_hooks=[(receiver_hook_names_filter, hook_fn)], return_type="logits"
)
# Save the results
results[sender_layer, sender_head] = patching_metric(patched_logits)
return results
model.reset_hooks()
s_inhibition_value_path_patching_results = get_path_patch_head_to_heads(
receiver_heads=[(8, 6), (8, 10), (7, 9), (7, 3)], receiver_input="v", model=model, patching_metric=ioi_metric_2
)
imshow(
100 * s_inhibition_value_path_patching_results,
title="Direct effect on S-Inhibition Heads' values",
labels={"x": "Head", "y": "Layer", "color": "Logit diff.<br>variation"},
width=600,
coloraxis=dict(colorbar_ticksuffix="%"),
)
# %%
def scatter_embedding_vs_attn(
attn_from_end_to_io: Float[Tensor, "batch"],
attn_from_end_to_s: Float[Tensor, "batch"],
projection_in_io_dir: Float[Tensor, "batch"],
projection_in_s_dir: Float[Tensor, "batch"],
layer: int,
head: int,
):
scatter(
x=t.concat([attn_from_end_to_io, attn_from_end_to_s], dim=0),
y=t.concat([projection_in_io_dir, projection_in_s_dir], dim=0),
color=["IO"] * N + ["S"] * N,
title=f"Projection of the output of {layer}.{head} along the name<br>embedding vs attention probability on name",
title_x=0.5,
labels={"x": "Attn prob on name", "y": "Dot w Name Embed", "color": "Name type"},
color_discrete_sequence=["#72FF64", "#C9A5F7"],
width=650,
)
if MAIN:
def calculate_and_show_scatter_embedding_vs_attn(
layer: int,
head: int,
cache: ActivationCache = ioi_cache,
dataset: IOIDataset = ioi_dataset,
) -> None:
"""
Creates and plots a figure equivalent to 3(c) in the paper.
This should involve computing the four 1D tensors:
attn_from_end_to_io
attn_from_end_to_s
projection_in_io_dir
projection_in_s_dir
and then calling the scatter_embedding_vs_attn function.
"""
# Get the value written to the residual stream at the end token by this head
z = cache[utils.get_act_name("z", layer)][:, :, head] # [batch seq d_head]
N = z.size(0)
output = z @ model.W_O[layer, head] # [batch seq d_model]
output_on_end_token = output[t.arange(N), dataset.word_idx["end"]] # [batch d_model]
# Get the directions we'll be projecting onto
io_unembedding = model.W_U.T[dataset.io_tokenIDs] # [batch d_model]
s_unembedding = model.W_U.T[dataset.s_tokenIDs] # [batch d_model]
# Get the value of projections, by multiplying and summing over the d_model dimension