-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_model_results_ioi.py
163 lines (139 loc) · 5.04 KB
/
get_model_results_ioi.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
import os
import pickle
import torch
import argparse
from collections import namedtuple
import json
import utils.circuit_utils as cu
from utils.model_utils import load_model, clear_gpu_memory
from utils.data_utils import generate_data_and_caches
from utils.metrics import _logits_to_mean_logit_diff, _logits_to_mean_accuracy, _logits_to_rank_0_rate, ave_logit_diff
from torchtyping import TensorType as TT
# Set up argument parser
parser = argparse.ArgumentParser(description='Run model with specified settings')
parser.add_argument('model_name', type=str, help='Name of the model to load')
parser.add_argument('circuit_file', type=str, help='Filename for the circuit dictionary')
# Parse arguments
args = parser.parse_args()
# Use the parsed model name and circuit file
model_name = args.model_name
circuit_file = args.circuit_file
# Settings
if torch.cuda.is_available():
device = int(os.environ.get("LOCAL_RANK", 0))
else:
device = "cpu"
#
#DO_SLOW_RUNS = True
#
model_full_name = f"EleutherAI/{model_name}"
#
cache_dir = "model_cache"
##cache_dir = "/media/curttigges/project-files/projects/circuits"
#
## load model
model = load_model(
model_full_name,model_full_name, "step143000", cache_dir=cache_dir
)
CircuitComponent = namedtuple(
"CircuitComponent", ["heads", "position", "receiver_type"]
)
# Load the circuit dictionary from the specified file
circuit_root = "results/circuits/"
with open(circuit_root + circuit_file, 'rb') as f:
circuit = pickle.load(f)
# set up data
N = 70
ioi_dataset, abc_dataset, ioi_cache, abc_cache, ioi_metric_noising = generate_data_and_caches(model, N, verbose=True)
# get baselines
#clean_logits, clean_cache = model.run_with_cache(ioi_dataset.toks)
#corrupted_logits, corrupted_cache = model.run_with_cache(abc_dataset.toks)
#
#clean_logit_diff = _logits_to_mean_logit_diff(clean_logits, ioi_dataset)
#print(f"Clean logit diff: {clean_logit_diff:.4f}")
#
#corrupted_logit_diff = _logits_to_mean_logit_diff(corrupted_logits, ioi_dataset)
#print(f"Corrupted logit diff: {corrupted_logit_diff:.4f}")
#
#clean_logit_accuracy = _logits_to_mean_accuracy(clean_logits, ioi_dataset).item()
#print(f"Clean logit accuracy: {clean_logit_accuracy:.4f}")
#
#corrupted_logit_accuracy = _logits_to_mean_accuracy(corrupted_logits, ioi_dataset).item()
#print(f"Corrupted logit accuracy: {corrupted_logit_accuracy:.4f}")
#
#clean_logit_rank_0_rate = _logits_to_rank_0_rate(clean_logits, ioi_dataset)
#print(f"Clean logit rank 0 rate: {clean_logit_rank_0_rate:.4f}")
#
#corrupted_logit_rank_0_rate = _logits_to_rank_0_rate(corrupted_logits, ioi_dataset)
#print(f"Corrupted logit rank 0 rate: {corrupted_logit_rank_0_rate:.4f}")
#
#CLEAN_BASELINE = clean_logit_diff
#CORRUPTED_BASELINE = corrupted_logit_diff
clear_gpu_memory(model)
# get values over time
# ckpts = [round((2**i) / 1000) * 1000 if 2**i > 1000 else 2**i for i in range(18)]
ckpts = [142000, 143000]
with torch.no_grad():
clean_logits = model(ioi_dataset.toks)
corrupt_logits = model(abc_dataset.toks)
clean_logit_diff = ave_logit_diff(clean_logits,
io_tokenIDs = ioi_dataset.io_tokenIDs,
s_tokenIDs = ioi_dataset.s_tokenIDs,
word_idx = ioi_dataset.word_idx['end']).item()
corrupt_logit_diff = ave_logit_diff(corrupt_logits,
io_tokenIDs = abc_dataset.io_tokenIDs,
s_tokenIDs = abc_dataset.s_tokenIDs,
word_idx = abc_dataset.word_idx['end']).item()
threshold = 0.02
g, nodes, edges, logits = cu.get_acdcpp_circuits(
model_full_name,
clean_logit_diff = clean_logit_diff,
corrupt_logit_diff = -corrupt_logit_diff,
cache_dir = cache_dir,
ckpts = ckpts,
clean_data=ioi_dataset,
corrupted_data=abc_dataset,
threshold = threshold,
batch_size = 9,
)
graph = {}
for k,v in edges.items():
if v.in_graph:
graph[k] = float(v.score)
with open(f"results/{model_name}-acdcpp-{threshold}.json", 'w') as f:
json.dump(graph, f)
#a = torch.stack((torch.tensor(ioi_dataset.io_tokenIDs), torch.tensor(ioi_dataset.s_tokenIDs)), dim = -1)
'''
results_dict = cu.get_chronological_circuit_data(
model_full_name,
cache_dir = cache_dir,
ckpts = ckpts,
circuit=circuit,
clean_tokens=ioi_dataset.toks,
corrupted_tokens=abc_dataset.toks,
answer_token_indices = a.cuda()
)
# save results
os.makedirs(f"results/{model_name}-no-dropout", exist_ok=True)
torch.save(
results_dict["logit_diffs"], f"results/{model_name}-no-dropout/overall_perf.pt"
)
torch.save(
results_dict["clean_baselines"],
f"results/{model_name}-no-dropout/clean_baselines.pt",
)
torch.save(
results_dict["corrupted_baselines"],
f"results/{model_name}-no-dropout/corrupted_baselines.pt",
)
torch.save(
results_dict["attn_head_vals"], f"results/{model_name}-no-dropout/attn_head_perf.pt"
)
torch.save(
results_dict["value_patch_vals"], f"results/{model_name}-no-dropout/value_perf.pt"
)
with open(f"results/{model_name}-no-dropout/circuit_vals.pkl", "wb") as f:
pickle.dump(results_dict["circuit_vals"], f)
with open(f"results/{model_name}-no-dropout/knockout_drops.pkl", "wb") as f:
pickle.dump(results_dict["knockout_drops"], f)
'''