-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassifier_accuracy.py
335 lines (309 loc) · 10.2 KB
/
classifier_accuracy.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
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
# %%
from functools import partial
import itertools
import gc
import numpy as np
import torch
from torch import tensor, Tensor
from torch.utils.data import DataLoader
from datasets import load_dataset
import einops
from jaxtyping import Float, Int, Bool
from typing import Dict, Iterable, List, Optional, Tuple, Union, Literal
from transformer_lens import HookedTransformer
from transformer_lens.evals import make_owt_data_loader
from transformer_lens.utils import (
get_dataset,
tokenize_and_concatenate,
get_act_name,
test_prompt,
)
from transformer_lens.hook_points import HookPoint
from circuitsvis.activations import text_neuron_activations
from circuitsvis.utils.render import RenderedHTML
from tqdm.notebook import tqdm
from IPython.display import display, HTML
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import os
import pandas as pd
import scipy.stats as stats
from utils.store import (
load_array,
save_html,
save_array,
is_file,
get_model_name,
clean_label,
save_text,
to_csv,
get_csv,
save_pdf,
save_pickle,
update_csv,
)
from utils.neuroscope import (
plot_neuroscope,
get_dataloader,
get_projections_for_text,
plot_top_p,
plot_topk,
harry_potter_start,
harry_potter_fr_start,
get_batch_pos_mask,
extract_text_window,
extract_activations_window,
get_activations_from_dataloader,
get_activations_cached,
)
# %%
pd.set_option("display.max_colwidth", 200)
torch.set_grad_enabled(False)
# %%
DIRECTIONS = [
"kmeans_simple_train_ADJ_layer1",
"pca_simple_train_ADJ_layer1",
"mean_diff_simple_train_ADJ_layer1",
"logistic_regression_simple_train_ADJ_layer1",
"das_simple_train_ADJ_layer1",
]
# %%
device = "cuda"
MODEL_NAME = "mistralai/Mistral-7B-v0.1"
BATCH_SIZE = 16
model = HookedTransformer.from_pretrained(
MODEL_NAME,
device=device,
torch_dtype=torch.float16,
dtype="float16",
fold_ln=False,
center_writing_weights=False,
center_unembed=False,
)
# %%
dataloader = get_dataloader(model, "stas/openwebtext-10k", batch_size=BATCH_SIZE)
# %%
def get_window(
batch: int,
pos: int,
dataloader: torch.utils.data.DataLoader,
window_size: int = 10,
) -> Tuple[int, int]:
"""Helper function to get the window around a position in a batch (used in topk plotting))"""
lb = max(0, pos - window_size)
ub = min(len(dataloader.dataset[batch]["tokens"]), pos + window_size + 1)
return lb, ub
def extract_text_window(
batch: int,
pos: int,
dataloader: torch.utils.data.DataLoader,
model: HookedTransformer,
window_size: int = 10,
) -> List[str]:
"""Helper function to get the text window around a position in a batch (used in topk plotting)"""
assert model.tokenizer is not None
expected_size = 2 * window_size + 1
lb, ub = get_window(batch, pos, dataloader=dataloader, window_size=window_size)
tokens = dataloader.dataset[batch]["tokens"][lb:ub]
str_tokens = model.to_str_tokens(tokens, prepend_bos=False)
padding_to_add = expected_size - len(str_tokens)
if padding_to_add > 0 and model.tokenizer.padding_side == "right":
str_tokens += [model.tokenizer.bos_token] * padding_to_add
elif padding_to_add > 0 and model.tokenizer.padding_side == "left":
str_tokens = [model.tokenizer.bos_token] * padding_to_add + str_tokens
assert len(str_tokens) == expected_size, (
f"Expected text window of size {expected_size}, "
f"found {len(str_tokens)}: {str_tokens}"
)
return str_tokens # type: ignore
# %%
def sample_by_bin(
data: Float[Tensor, "batch pos"],
bins: int = 20,
samples_per_bin: int = 20,
seed: int = 0,
window_size: int = 10,
verbose: bool = True,
):
if verbose:
print("Calling sample_by_bin...")
np.random.seed(seed)
torch.manual_seed(seed)
flat = data.flatten().cpu().numpy()
flat = flat[flat != 0]
hist, bin_edges = np.histogram(flat, bins=bins)
# plot histogram
if verbose:
print("Plotting histogram...")
sample_to_plot = flat[np.random.randint(0, len(flat), bins * samples_per_bin)]
fig = px.histogram(
sample_to_plot,
nbins=bins,
labels={"value": "Activation"},
title="Histogram of activations",
)
fig.show()
bin_indices: Int[np.ndarray, "batch pos"] = np.digitize(
data.cpu().numpy(), bin_edges
)
if verbose:
print(bin_edges)
indices = []
for bin_idx in range(1, bins + 1):
lb = bin_edges[bin_idx - 1]
ub = bin_edges[bin_idx]
bin_batches, bin_positions = np.where(bin_indices == bin_idx)
bin_samples = np.random.randint(0, len(bin_batches), samples_per_bin)
indices += [
(
bin_idx,
lb,
ub,
bin_batches[bin_sample],
bin_positions[bin_sample],
activations[
bin_batches[bin_sample], bin_positions[bin_sample], 1
].item(),
)
for bin_sample in bin_samples
]
if verbose:
print("Constructing dataframe...")
df = pd.DataFrame(
indices, columns=["bin", "lb", "ub", "batch", "position", "activation"]
)
tokens = []
texts = []
for _, row in df.iterrows():
text = extract_text_window(
int(row.batch),
int(row.position),
dataloader,
model,
window_size=window_size,
)
tokens.append(text[window_size])
texts.append("".join(text))
df.reset_index(drop=True, inplace=True)
df["token"] = tokens
df["text"] = texts
return df.sample(frac=1, random_state=seed).reset_index(drop=True)
# %% # Save samples
bar = tqdm(DIRECTIONS)
for direction_label in bar:
bar.set_description(direction_label)
samples_path = direction_label + "_bin_samples.csv"
if is_file(samples_path, model):
continue
activations = get_activations_cached(dataloader, direction_label, model)
bin_samples = sample_by_bin(activations[:, :, 1], verbose=False)
to_csv(bin_samples, samples_path, model)
# %%
def plot_bin_proportions(
df: pd.DataFrame,
label: str,
sentiments=(
"Positive",
"Negative",
"Neutral",
),
nbins: Optional[int] = 50,
):
if "activation" in df.columns:
assert nbins is not None
if df.activation.dtype == pd.StringDtype:
df.activation = df.activation.map(lambda x: eval(x).item()).astype(float)
sentiments = sorted(df["sentiment"].unique())
df = df.sort_values(by="activation").reset_index(drop=True)
df["activation_cut"] = pd.cut(df.activation, bins=nbins)
df.activation_cut = df.activation_cut.apply(lambda x: 0.5 * (x.left + x.right))
else:
df["activation"] = df["activation_cut"] = (df.lb + df.ub) * 0.5
df = df.sort_values(by="activation_cut").reset_index(drop=True)
fig = go.Figure()
data = []
for x, bin_df in df.groupby("activation_cut"):
if bin_df.empty:
continue
label_props = bin_df.value_counts("sentiment", normalize=True, sort=False)
data.append([label_props.get(sentiment, 0) for sentiment in sentiments])
data = pd.DataFrame(data, columns=sentiments)
cumulative_data = data.cumsum(axis=1) # Cumulative sum along columns
x_values = df["activation_cut"].unique()
# Adding traces for the rest of the sentiments
for idx, sentiment in enumerate(sentiments):
fig.add_trace(
go.Scatter(
x=x_values,
y=cumulative_data[sentiment],
name=sentiment,
hovertemplate="<br>".join(
[
"Sentiment: " + sentiment,
"Sentiment Activation: %{x}",
"Cum. Label proportion: %{y:.4f}",
]
),
fill="tonexty",
mode="lines",
)
)
fig.update_layout(
title=dict(
text=f"Proportion of Sentiment by Activation ({label})",
x=0.5,
font=dict(size=18),
),
showlegend=True,
xaxis_title="Sentiment Activation",
yaxis_title="Cum. Label proportion",
font=dict(size=24), # global font settings # global font size
)
return fig
# %%
for direction_label in DIRECTIONS:
labelled_bin_samples = get_csv(
"labelled_" + direction_label + "_bin_samples", model
)
out_name = direction_label + "_bin_proportions"
if "das" in direction_label or "pca" in direction_label:
labelled_bin_samples.activation *= -1
fig = plot_bin_proportions(
labelled_bin_samples, f"{direction_label.split('_')[0]}, {model.cfg.model_name}"
)
save_pdf(fig, out_name, model)
save_html(fig, out_name, model)
save_pdf(fig, out_name, model)
fig.show()
activations = get_activations_cached(dataloader, direction_label, model)
flat = activations[:, :, 1].flatten().cpu()
flat = flat[flat != 0]
if "das" in direction_label or "pca" in direction_label:
flat *= -1
positive_threshold = flat.quantile(0.999).item()
negative_threshold = flat.quantile(0.001).item()
bottom_counts = labelled_bin_samples[
labelled_bin_samples.activation.lt(negative_threshold)
].sentiment.value_counts(normalize=True)
bottom_counts = bottom_counts[bottom_counts.index != "Neutral"]
top_counts = labelled_bin_samples[
labelled_bin_samples.activation.gt(positive_threshold)
].sentiment.value_counts(normalize=True)
top_counts = top_counts[top_counts.index != "Neutral"]
metric = (bottom_counts.max() + top_counts.max()) / 2
df = pd.DataFrame(
{
"direction": [direction_label],
"metric": [metric],
}
)
assert not np.isnan(metric), (
f"Metric is NaN for {direction_label}. "
f"Bottom counts: {bottom_counts}. "
f"Top counts: {top_counts}. "
f"Thresholds: {negative_threshold}, {positive_threshold}"
)
print(direction_label, metric)
update_csv(df, "classifier_accuracy", model, key_cols=["direction"])
# %%