FagerholmEmil
commited on
Commit
·
511e2ba
1
Parent(s):
7bc45a9
No changes
Browse files- app.py +93 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
from transformer_lens import HookedTransformer
|
5 |
+
from transformer_lens.utils import to_numpy
|
6 |
+
|
7 |
+
model_name = "gpt2-small"
|
8 |
+
model = HookedTransformer.from_pretrained(model_name)
|
9 |
+
|
10 |
+
def get_neuron_acts(text, layer, neuron_index):
|
11 |
+
cache = {}
|
12 |
+
def caching_hook(act, hook):
|
13 |
+
cache["activation"] = act[0, :, neuron_index]
|
14 |
+
|
15 |
+
model.run_with_hooks(
|
16 |
+
text, fwd_hooks=[(f"blocks.{layer}.mlp.hook_post", caching_hook)]
|
17 |
+
)
|
18 |
+
return to_numpy(cache["activation"])
|
19 |
+
|
20 |
+
def calculate_color(val, max_val, min_val):
|
21 |
+
normalized_val = (val - min_val) / max_val
|
22 |
+
return f"rgb(240, {240*(1-normalized_val)}, {240*(1-normalized_val)})"
|
23 |
+
|
24 |
+
style_string = """<style>
|
25 |
+
span.token {
|
26 |
+
border: 1px solid rgb(123, 123, 123)
|
27 |
+
}
|
28 |
+
</style>"""
|
29 |
+
|
30 |
+
def basic_neuron_vis(text, layer, neuron_index, max_val=None, min_val=None):
|
31 |
+
if layer is None:
|
32 |
+
return "Please select a Layer"
|
33 |
+
if neuron_index is None:
|
34 |
+
return "Please select a Neuron"
|
35 |
+
acts = get_neuron_acts(text, layer, neuron_index)
|
36 |
+
act_max = acts.max()
|
37 |
+
act_min = acts.min()
|
38 |
+
if max_val is None:
|
39 |
+
max_val = act_max
|
40 |
+
if min_val is None:
|
41 |
+
min_val = act_min
|
42 |
+
|
43 |
+
htmls = [style_string]
|
44 |
+
htmls.append(f"<h4>Layer: <b>{layer}</b>. Neuron Index: <b>{neuron_index}</b></h4>")
|
45 |
+
htmls.append(f"<h4>Max Range: <b>{max_val:.4f}</b>. Min Range: <b>{min_val:.4f}</b></h4>")
|
46 |
+
|
47 |
+
if act_max != max_val or act_min != min_val:
|
48 |
+
htmls.append(
|
49 |
+
f"<h4>Custom Range Set. Max Act: <b>{act_max:.4f}</b>. Min Act: <b>{act_min:.4f}</b></h4>"
|
50 |
+
)
|
51 |
+
|
52 |
+
str_tokens = model.to_str_tokens(text)
|
53 |
+
for tok, act in zip(str_tokens, acts):
|
54 |
+
htmls.append(
|
55 |
+
f"<span class='token' style='background-color:{calculate_color(act, max_val, min_val)}' >{tok}</span>"
|
56 |
+
)
|
57 |
+
|
58 |
+
return "".join(htmls)
|
59 |
+
|
60 |
+
default_text = """The sun rises red, sets golden.
|
61 |
+
Digits flow: 101, 202, 303—cyclic repetition.""" # Shortened for example
|
62 |
+
default_layer = 9
|
63 |
+
default_neuron_index = 652
|
64 |
+
default_max_val = 4.0
|
65 |
+
default_min_val = 0.0
|
66 |
+
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.HTML(value=f"Neuroscope for {model_name}")
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column():
|
71 |
+
text = gr.Textbox(label="Text", value=default_text)
|
72 |
+
layer = gr.Number(label="Layer", value=default_layer, precision=0)
|
73 |
+
neuron_index = gr.Number(
|
74 |
+
label="Neuron Index", value=default_neuron_index, precision=0
|
75 |
+
)
|
76 |
+
max_val = gr.Number(label="Max Value", value=default_max_val)
|
77 |
+
min_val = gr.Number(label="Min Value", value=default_min_val)
|
78 |
+
inputs = [text, layer, neuron_index, max_val, min_val]
|
79 |
+
with gr.Column():
|
80 |
+
out = gr.HTML(
|
81 |
+
label="Neuron Acts",
|
82 |
+
value=basic_neuron_vis(
|
83 |
+
default_text,
|
84 |
+
default_layer,
|
85 |
+
default_neuron_index,
|
86 |
+
default_max_val,
|
87 |
+
default_min_val,
|
88 |
+
),
|
89 |
+
)
|
90 |
+
for inp in inputs:
|
91 |
+
inp.change(basic_neuron_vis, inputs, out)
|
92 |
+
|
93 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.19.2
|
2 |
+
transformer_lens==1.13.0
|
3 |
+
torch==2.2.0
|