-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotly_utils.py
More file actions
629 lines (580 loc) · 24.1 KB
/
plotly_utils.py
File metadata and controls
629 lines (580 loc) · 24.1 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
from __future__ import annotations
import re
import einops
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import torch as t
from jaxtyping import Float
from plotly.subplots import make_subplots
from torch import Tensor
def to_numpy(tensor):
"""
Helper function to convert a tensor to a numpy array. Also works on lists, tuples, and numpy arrays.
"""
if isinstance(tensor, np.ndarray):
return tensor
elif isinstance(tensor, (list, tuple)):
array = np.array(tensor)
return array
elif isinstance(tensor, (t.Tensor, t.nn.parameter.Parameter)):
return tensor.detach().cpu().numpy()
elif isinstance(tensor, (int, float, bool, str)):
return np.array(tensor)
else:
raise ValueError(f"Input to to_numpy has invalid type: {type(tensor)}")
# GENERIC PLOTTING FUNCTIONS
update_layout_set = {
"xaxis_range",
"yaxis_range",
"hovermode",
"xaxis_title",
"yaxis_title",
"colorbar",
"colorscale",
"coloraxis",
"title_x",
"bargap",
"bargroupgap",
"xaxis_tickformat",
"yaxis_tickformat",
"title_y",
"legend_title_text",
"xaxis_showgrid",
"xaxis_gridwidth",
"xaxis_gridcolor",
"yaxis_showgrid",
"yaxis_gridwidth",
"yaxis_gridcolor",
"showlegend",
"xaxis_tickmode",
"yaxis_tickmode",
"margin",
"xaxis_visible",
"yaxis_visible",
"bargap",
"bargroupgap",
"coloraxis_showscale",
"xaxis_tickangle",
"yaxis_scaleanchor",
"xaxis_tickfont",
"yaxis_tickfont",
}
update_traces_set = {"textposition"}
def imshow(tensor: t.Tensor, renderer=None, **kwargs):
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
if ("size" in kwargs_pre) or ("shape" in kwargs_pre):
size = kwargs_pre.pop("size", None) or kwargs_pre.pop("shape", None)
kwargs_pre["height"], kwargs_pre["width"] = size # type: ignore
facet_labels = kwargs_pre.pop("facet_labels", None)
border = kwargs_pre.pop("border", False)
return_fig = kwargs_pre.pop("return_fig", False)
text = kwargs_pre.pop("text", None)
xaxis_tickangle = kwargs_post.pop("xaxis_tickangle", None)
# xaxis_tickfont = kwargs_post.pop("xaxis_tickangle", None)
static = kwargs_pre.pop("static", False)
if "color_continuous_scale" not in kwargs_pre:
kwargs_pre["color_continuous_scale"] = "RdBu"
if "color_continuous_midpoint" not in kwargs_pre:
kwargs_pre["color_continuous_midpoint"] = 0.0
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
fig = px.imshow(to_numpy(tensor), **kwargs_pre).update_layout(**kwargs_post)
if facet_labels:
# Weird thing where facet col wrap means labels are in wrong order
if "facet_col_wrap" in kwargs_pre:
facet_labels = reorder_list_in_plotly_way(facet_labels, kwargs_pre["facet_col_wrap"])
for i, label in enumerate(facet_labels):
fig.layout.annotations[i]["text"] = label # type: ignore
if border:
fig.update_xaxes(showline=True, linewidth=1, linecolor="black", mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor="black", mirror=True)
if text:
if tensor.ndim == 2:
# if 2D, then we assume text is a list of lists of strings
assert isinstance(text[0], list)
assert isinstance(text[0][0], str)
text = [text]
else:
# if 3D, then text is either repeated for each facet, or different
assert isinstance(text[0], list)
if isinstance(text[0][0], str):
text = [text for _ in range(len(fig.data))]
for i, _text in enumerate(text):
fig.data[i].update(text=_text, texttemplate="%{text}", textfont={"size": 12})
# Very hacky way of fixing the fact that updating layout with xaxis_* only applies to first facet by default
if xaxis_tickangle is not None:
n_facets = 1 if tensor.ndim == 2 else tensor.shape[0]
for i in range(1, 1 + n_facets):
xaxis_name = "xaxis" if i == 1 else f"xaxis{i}"
fig.layout[xaxis_name]["tickangle"] = xaxis_tickangle # type: ignore
return fig if return_fig else fig.show(renderer=renderer, config={"staticPlot": static})
def reorder_list_in_plotly_way(L: list, col_wrap: int):
"""
Helper function, because Plotly orders figures in an annoying way when there's column wrap.
"""
L_new = []
while len(L) > 0:
L_new.extend(L[-col_wrap:])
L = L[:-col_wrap]
return L_new
def line(y: t.Tensor | list, renderer=None, **kwargs):
"""
Edit to this helper function, allowing it to take args in update_layout (e.g. yaxis_range).
"""
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
if ("size" in kwargs_pre) or ("shape" in kwargs_pre):
size = kwargs_pre.pop("size", None) or kwargs_pre.pop("shape", None)
kwargs_pre["height"], kwargs_pre["width"] = size # type: ignore
return_fig = kwargs_pre.pop("return_fig", False)
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
if "xaxis_tickvals" in kwargs_pre:
tickvals = kwargs_pre.pop("xaxis_tickvals")
kwargs_post["xaxis"] = dict(
tickmode="array",
tickvals=kwargs_pre.get("x", np.arange(len(tickvals))),
ticktext=tickvals,
)
if "hovermode" not in kwargs_post:
kwargs_post["hovermode"] = "x unified"
if "use_secondary_yaxis" in kwargs_pre and kwargs_pre["use_secondary_yaxis"]:
del kwargs_pre["use_secondary_yaxis"]
if "labels" in kwargs_pre:
labels: dict = kwargs_pre.pop("labels")
kwargs_post["yaxis_title_text"] = labels.get("y1", None)
kwargs_post["yaxis2_title_text"] = labels.get("y2", None)
kwargs_post["xaxis_title_text"] = labels.get("x", None)
for k in ["title", "template", "width", "height"]:
if k in kwargs_pre:
kwargs_post[k] = kwargs_pre.pop(k)
fig = make_subplots(specs=[[{"secondary_y": True}]]).update_layout(**kwargs_post)
y0 = to_numpy(y[0])
y1 = to_numpy(y[1])
x0, x1 = kwargs_pre.pop("x", [np.arange(len(y0)), np.arange(len(y1))])
name0, name1 = kwargs_pre.pop("names", ["yaxis1", "yaxis2"])
fig.add_trace(go.Scatter(y=y0, x=x0, name=name0), secondary_y=False)
fig.add_trace(go.Scatter(y=y1, x=x1, name=name1), secondary_y=True)
else:
y = (
list(map(to_numpy, y))
if isinstance(y, list) and not (isinstance(y[0], int) or isinstance(y[0], float))
else to_numpy(y)
) # type: ignore
names = kwargs_pre.pop("names", None)
fig = px.line(y=y, **kwargs_pre).update_layout(**kwargs_post)
if names is not None:
fig.for_each_trace(lambda trace: trace.update(name=names.pop(0)))
return fig if return_fig else fig.show(renderer=renderer)
def scatter(x, y, renderer=None, **kwargs):
x = to_numpy(x)
y = to_numpy(y)
add_line = None
if "add_line" in kwargs:
add_line = kwargs.pop("add_line")
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_traces = {k: v for k, v in kwargs.items() if k in update_traces_set}
kwargs_pre = {
k: v for k, v in kwargs.items() if k not in (update_layout_set | update_traces_set)
}
if ("size" in kwargs_pre) or ("shape" in kwargs_pre):
size = kwargs_pre.pop("size", None) or kwargs_pre.pop("shape", None)
kwargs_pre["height"], kwargs_pre["width"] = size # type: ignore
return_fig = kwargs_pre.pop("return_fig", False)
facet_labels = kwargs_pre.pop("facet_labels", None)
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
fig = px.scatter(y=y, x=x, **kwargs_pre).update_layout(**kwargs_post)
if add_line is not None:
xrange = fig.layout.xaxis.range or [x.min(), x.max()] # type: ignore
yrange = fig.layout.yaxis.range or [y.min(), y.max()] # type: ignore
add_line = add_line.replace(" ", "")
if add_line in ["x=y", "y=x"]:
fig.add_trace(go.Scatter(mode="lines", x=xrange, y=xrange, showlegend=False))
elif re.match("(x|y)=", add_line):
try:
c = float(add_line.split("=")[1])
except:
raise ValueError(
f"Unrecognized add_line: {add_line}. Please use either 'x=y' or 'x=c' or 'y=c' for some float c."
)
x, y = ([c, c], yrange) if add_line[0] == "x" else (xrange, [c, c])
fig.add_trace(go.Scatter(mode="lines", x=x, y=y, showlegend=False))
else:
raise ValueError(
f"Unrecognized add_line: {add_line}. Please use either 'x=y' or 'x=c' or 'y=c' for some float c."
)
if facet_labels:
for i, label in enumerate(facet_labels):
fig.layout.annotations[i]["text"] = label # type: ignore
fig.update_traces(**kwargs_traces)
return fig if return_fig else fig.show(renderer=renderer)
def bar(tensor, renderer=None, **kwargs):
""" """
if isinstance(tensor, list):
if isinstance(tensor[0], t.Tensor):
arr = [to_numpy(tn) for tn in tensor]
elif isinstance(tensor[0], list):
arr = [np.array(tn) for tn in tensor]
else:
arr = np.array(tensor)
else:
arr = to_numpy(tensor)
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
return_fig = kwargs_pre.pop("return_fig", False)
names = kwargs_pre.pop("names", None)
if "hovermode" not in kwargs_post:
kwargs_post["hovermode"] = "x unified"
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
fig = px.bar(y=arr, **kwargs_pre).update_layout(**kwargs_post)
if names is not None:
for i in range(len(fig.data)):
fig.data[i]["name"] = names[i // 2 if "marginal" in kwargs_pre else i]
return fig if return_fig else fig.show(renderer=renderer)
def hist(tensor, renderer=None, **kwargs):
kwargs_post = {k: v for k, v in kwargs.items() if k in update_layout_set}
kwargs_pre = {k: v for k, v in kwargs.items() if k not in update_layout_set}
# draw = kwargs_pre.pop("draw", True)
# static = kwargs_pre.pop("static", False)
return_fig = kwargs_pre.pop("return_fig", False)
if isinstance(tensor, list):
if isinstance(tensor[0], t.Tensor):
arr = [to_numpy(tn) for tn in tensor]
elif isinstance(tensor[0], list):
arr = [np.array(tn) for tn in tensor]
else:
arr = np.array(tensor)
else:
arr = to_numpy(tensor)
if "modebar_add" not in kwargs_post:
kwargs_post["modebar_add"] = [
"drawline",
"drawopenpath",
"drawclosedpath",
"drawcircle",
"drawrect",
"eraseshape",
]
add_mean_line = kwargs_pre.pop("add_mean_line", False)
names = kwargs_pre.pop("names", None)
if "barmode" not in kwargs_post:
kwargs_post["barmode"] = "overlay"
if "bargap" not in kwargs_post:
kwargs_post["bargap"] = 0.0
if "margin" in kwargs_post and isinstance(kwargs_post["margin"], int):
kwargs_post["margin"] = dict.fromkeys(list("tblr"), kwargs_post["margin"])
if "hovermode" not in kwargs_post:
kwargs_post["hovermode"] = "x unified"
if "autosize" not in kwargs_post:
kwargs_post["autosize"] = False
# If `arr` has a list of arrays, then just doing px.histogram doesn't work annoyingly enough
# This is janky, even for my functions!
if isinstance(arr, list) and isinstance(arr[0], np.ndarray):
assert "marginal" not in kwargs_pre, "Can't use `marginal` with a list of arrays"
for thing_to_move_from_pre_to_post in ["title", "template", "height", "width", "labels"]:
if thing_to_move_from_pre_to_post in kwargs_pre:
kwargs_post[thing_to_move_from_pre_to_post] = kwargs_pre.pop(
thing_to_move_from_pre_to_post
)
if "labels" in kwargs_post:
kwargs_post["xaxis_title_text"] = kwargs_post["labels"].get("x", "")
kwargs_post["yaxis_title_text"] = kwargs_post["labels"].get("y", "")
del kwargs_post["labels"]
fig = go.Figure(layout=go.Layout(**kwargs_post))
if "nbins" in kwargs_pre:
kwargs_pre["nbinsx"] = int(kwargs_pre.pop("nbins"))
for x in arr:
fig.add_trace(
go.Histogram(x=x, name=names.pop(0) if names is not None else None, **kwargs_pre)
)
else:
fig = px.histogram(x=arr, **kwargs_pre).update_layout(**kwargs_post)
if names is not None:
for i in range(len(fig.data)):
fig.data[i]["name"] = names[i // 2 if "marginal" in kwargs_pre else i]
assert isinstance(arr, (np.ndarray, Tensor))
if add_mean_line:
if arr.ndim == 1:
fig.add_vline(
x=arr.mean(),
line_width=3,
line_dash="dash",
line_color="black",
annotation_text=f"Mean = {arr.mean():.3f}",
annotation_position="top",
)
elif arr.ndim == 2:
for i in range(arr.shape[0]):
fig.add_vline(
x=arr[i].mean(),
line_width=3,
line_dash="dash",
line_color="black",
annotation_text=f"Mean = {arr.mean():.3f}",
annotation_position="top",
)
return fig if return_fig else fig.show(renderer=renderer)
# PLOTTING FUNCTIONS FOR PART 2: INTRO TO MECH INTERP
def plot_comp_scores(model, comp_scores, title: str = "", baseline: t.Tensor | None = None):
px.imshow(
to_numpy(comp_scores),
y=[f"L0H{h}" for h in range(model.cfg.n_heads)],
x=[f"L1H{h}" for h in range(model.cfg.n_heads)],
labels={"x": "Layer 1", "y": "Layer 0"},
title=title,
color_continuous_scale="RdBu" if baseline is not None else "Blues",
color_continuous_midpoint=baseline if baseline is not None else None,
zmin=None if baseline is not None else 0.0,
).show()
def convert_tokens_to_string(model, tokens, batch_index=0):
"""
Helper function to convert tokens into a list of strings, for printing.
"""
if len(tokens.shape) == 2:
tokens = tokens[batch_index]
return [f"|{model.tokenizer.decode(tok)}|_{c}" for (c, tok) in enumerate(tokens)]
def plot_logit_attribution(model, logit_attr: t.Tensor, tokens: t.Tensor, title: str = ""):
tokens = tokens.squeeze()
y_labels = convert_tokens_to_string(model, tokens[:-1])
x_labels = ["Direct"] + [
f"L{l}H{h}" for l in range(model.cfg.n_layers) for h in range(model.cfg.n_heads)
]
imshow(
to_numpy(logit_attr), # type: ignore
x=x_labels,
y=y_labels,
labels={"x": "Term", "y": "Position", "color": "logit"},
title=title if title else None,
height=18 * len(y_labels),
width=24 * len(x_labels),
)
# PLOTTING FUNCTIONS FOR PART 4: INTERP ON ALGORITHMIC MODEL
color_discrete_map = dict(
zip(
["both failures", "just neg failure", "balanced", "just total elevation failure"],
px.colors.qualitative.D3,
)
)
# names = ["balanced", "just total elevation failure", "just neg failure", "both failures"]
# colors = ['#2CA02C', '#1c96eb', '#b300ff', '#ff4800']
# color_discrete_map = dict(zip(names, colors))
def plot_failure_types_scatter(
unbalanced_component_1: Float[Tensor, "batch"],
unbalanced_component_2: Float[Tensor, "batch"],
failure_types_dict: dict[str, Float[Tensor, "batch"]],
data,
):
failure_types = np.full(len(unbalanced_component_1), "", dtype=np.dtype("U32"))
for name, mask in failure_types_dict.items():
failure_types = np.where(to_numpy(mask), name, failure_types)
failures_df = pd.DataFrame(
{
"Head 2.0 contribution": to_numpy(unbalanced_component_1),
"Head 2.1 contribution": to_numpy(unbalanced_component_2),
"Failure type": to_numpy(failure_types),
}
)[data.starts_open.tolist()]
fig = px.scatter(
failures_df,
color_discrete_map=color_discrete_map,
x="Head 2.0 contribution",
y="Head 2.1 contribution",
color="Failure type",
title="h20 vs h21 for different failure types",
template="simple_white",
height=600,
width=800,
# category_orders={"color": failure_types_dict.keys()},
).update_traces(marker_size=4)
fig.show()
def plot_contribution_vs_open_proportion(
unbalanced_component: Float[Tensor, "batch"], title: str, failure_types_dict: dict, data
):
failure_types = np.full(len(unbalanced_component), "", dtype=np.dtype("U32"))
for name, mask in failure_types_dict.items():
failure_types = np.where(to_numpy(mask), name, failure_types)
fig = (
px.scatter(
x=to_numpy(data.open_proportion),
y=to_numpy(unbalanced_component),
color=failure_types,
color_discrete_map=color_discrete_map,
title=title,
template="simple_white",
height=500,
width=800,
labels={"x": "Open-proportion", "y": f"Head {title} contribution"},
)
.update_traces(marker_size=4, opacity=0.5)
.update_layout(legend_title_text="Failure type")
)
fig.show()
def mlp_attribution_scatter(
out_by_component_in_pre_20_unbalanced_dir: Float[Tensor, "comp batch"],
data,
failure_types_dict: dict,
) -> None:
failure_types = np.full(
out_by_component_in_pre_20_unbalanced_dir.shape[-1], "", dtype=np.dtype("U32")
)
for name, mask in failure_types_dict.items():
failure_types = np.where(to_numpy(mask), name, failure_types)
for layer in range(2):
mlp_output = out_by_component_in_pre_20_unbalanced_dir[3 + layer * 3]
fig = (
px.scatter(
x=to_numpy(data.open_proportion[data.starts_open]),
y=to_numpy(mlp_output[data.starts_open]),
color_discrete_map=color_discrete_map,
color=to_numpy(failure_types)[to_numpy(data.starts_open)],
title=f"Amount MLP {layer} writes in unbalanced direction for Head 2.0",
template="simple_white",
height=500,
width=800,
labels={"x": "Open-proportion", "y": "Head 2.0 contribution"},
)
.update_traces(marker_size=4, opacity=0.5)
.update_layout(legend_title_text="Failure type")
)
fig.show()
def plot_neurons(
neurons_in_unbalanced_dir: Float[Tensor, "batch neurons"],
model,
data,
failure_types_dict: dict,
layer: int,
renderer=None,
):
failure_types = np.full(neurons_in_unbalanced_dir.shape[0], "", dtype=np.dtype("U32"))
for name, mask in failure_types_dict.items():
failure_types = np.where(to_numpy(mask[to_numpy(data.starts_open)]), name, failure_types)
# Get data that can be turned into a dataframe (plotly express is sometimes easier to use with a dataframe)
# Plot a scatter plot of all the neuron contributions, color-coded according to failure type, with slider to view neurons
neuron_numbers = einops.repeat(
t.arange(model.cfg.d_model), "n -> (s n)", s=data.starts_open.sum()
)
failure_types = einops.repeat(failure_types, "s -> (s n)", n=model.cfg.d_model)
data_open_proportion = einops.repeat(
data.open_proportion[data.starts_open], "s -> (s n)", n=model.cfg.d_model
)
df = pd.DataFrame(
{
"Output in 2.0 direction": to_numpy(neurons_in_unbalanced_dir.flatten()),
"Neuron number": to_numpy(neuron_numbers),
"Open-proportion": to_numpy(data_open_proportion),
"Failure type": failure_types,
}
)
fig = (
px.scatter(
df,
x="Open-proportion",
y="Output in 2.0 direction",
color="Failure type",
animation_frame="Neuron number",
title=f"Neuron contributions from layer {layer}",
template="simple_white",
height=800,
width=1100,
)
.update_traces(marker_size=3)
.update_layout(xaxis_range=[0, 1], yaxis_range=[-5, 5])
)
fig.show(renderer=renderer)
def plot_attn_pattern(pattern: Float[Tensor, "batch head_idx seqQ seqK"]):
fig = px.imshow(
pattern,
title="Estimate for avg attn probabilities when query is from '('",
labels={
"x": "Key tokens (avg of left & right parens)",
"y": "Query tokens (all left parens)",
},
height=900,
width=900,
color_continuous_scale="RdBu_r",
range_color=[0, pattern.max().item()],
).update_layout(
xaxis=dict(
tickmode="array",
ticktext=["[start]", *[f"{i+1}" for i in range(40)], "[end]"],
tickvals=list(range(42)),
tickangle=0,
),
yaxis=dict(
tickmode="array",
ticktext=["[start]", *[f"{i+1}" for i in range(40)], "[end]"],
tickvals=list(range(42)),
),
)
fig.show()
def hists_per_comp(
out_by_component_in_unbalanced_dir: Float[Tensor, "component batch"], data, xaxis_range=(-1, 1)
):
"""
Plots the contributions in the unbalanced direction, as supplied by the `out_by_component_in_unbalanced_dir` tensor.
"""
titles = {
(1, 1): "embeddings",
(2, 1): "head 0.0",
(2, 2): "head 0.1",
(2, 3): "mlp 0",
(3, 1): "head `1.0`",
(3, 2): "head `1.1`",
(3, 3): "mlp 1",
(4, 1): "head 2.0",
(4, 2): "head 2.1",
(4, 3): "mlp 2",
}
n_layers = out_by_component_in_unbalanced_dir.shape[0] // 3
fig = make_subplots(rows=n_layers + 1, cols=3)
for ((row, col), title), in_dir in zip(titles.items(), out_by_component_in_unbalanced_dir):
fig.add_trace(
go.Histogram(
x=to_numpy(in_dir[data.isbal]),
name="Balanced",
marker_color="blue",
opacity=0.5,
legendgroup="1",
showlegend=title == "embeddings",
),
row=row,
col=col,
)
fig.add_trace(
go.Histogram(
x=to_numpy(in_dir[~data.isbal]),
name="Unbalanced",
marker_color="red",
opacity=0.5,
legendgroup="2",
showlegend=title == "embeddings",
),
row=row,
col=col,
)
fig.update_xaxes(title_text=title, row=row, col=col, range=xaxis_range)
fig.update_layout(
width=1200,
height=250 * (n_layers + 1),
barmode="overlay",
legend=dict(yanchor="top", y=0.92, xanchor="left", x=0.4),
title="Histograms of component significance",
)
fig.show()
def plot_loss_difference(log_probs, rep_str, seq_len):
fig = px.line(
to_numpy(log_probs),
hover_name=rep_str[1:],
title=f"Per token log prob on correct token, for sequence of length {seq_len}*2 (repeated twice)",
labels={"index": "Sequence position", "value": "Log prob"},
).update_layout(showlegend=False, hovermode="x unified")
fig.add_vrect(x0=0, x1=seq_len - 0.5, fillcolor="red", opacity=0.2, line_width=0)
fig.add_vrect(
x0=seq_len - 0.5, x1=2 * seq_len - 1, fillcolor="green", opacity=0.2, line_width=0
)
fig.show()