Lev McKinney commited on
Commit
d115284
1 Parent(s): e1b6e5d

App now runs by default and has better documentation

Browse files
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -10,13 +10,14 @@ print(f"Using device {device} for inference")
10
  model = AutoModelForCausalLM.from_pretrained("EleutherAI/pythia-410m-deduped")
11
  model = model.to(device)
12
  tokenizer = AutoTokenizer.from_pretrained("EleutherAI/pythia-410m-deduped")
13
- tuned_lens = TunedLens.load("lens/pythia-410m-deduped", map_location=device)
14
  logit_lens = LogitLens(model)
15
 
16
  lens_options_dict = {
17
  "Tuned Lens": tuned_lens,
18
  "Logit Lens": logit_lens,
19
  }
 
20
  statistic_options_dict = {
21
  "Entropy": "entropy",
22
  "Cross Entropy": "ce",
@@ -42,6 +43,12 @@ def make_plot(lens, text, statistic, token_cutoff):
42
  start_pos=max(len(input_ids[0]) - token_cutoff, 0),
43
  statistic=statistic_options_dict[statistic],
44
  )
 
 
 
 
 
 
45
 
46
  return fig
47
 
@@ -54,10 +61,13 @@ A tuned lens allows us to peak at the iterative computations a transformer uses
54
  A lens into a transformer with n layers allows you to replace the last $m$ layers of the model with an [affine transformation](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) (we call these affine translators).
55
 
56
  This essentially skips over these last few layers and lets you see the best prediction that can be made from the model's representations, i.e. the residual stream, at layer $n - m$. Since the representations may be rotated, shifted, or stretched from layer to layer it's useful to train the len's affine adapters specifically on each layer. This training is what differentiates this method from simpler approaches that decode the residual stream of the network directly using the unembeding layer i.e. the logit lens. We explain this process in [the paper](https://arxiv.org/abs/2303.08112).
57
- """
58
 
59
 
60
- with gr.Blocks() as iface:
 
 
 
 
61
  gr.Markdown(preamble)
62
  with gr.Column():
63
  text = gr.Textbox(
@@ -74,10 +84,13 @@ with gr.Blocks() as iface:
74
  label="Select Statistic",
75
  )
76
  token_cutoff = gr.Slider(
77
- maximum=20, minimum=2, value=10, step=1, label="Token Cut Off"
78
  )
79
  examine_btn = gr.Button(value="Submit")
80
  plot = gr.Plot()
81
  examine_btn.click(make_plot, [lens_options, text, statistic, token_cutoff], plot)
 
 
 
 
82
 
83
- iface.launch()
 
10
  model = AutoModelForCausalLM.from_pretrained("EleutherAI/pythia-410m-deduped")
11
  model = model.to(device)
12
  tokenizer = AutoTokenizer.from_pretrained("EleutherAI/pythia-410m-deduped")
13
+ tuned_lens = TunedLens.load("pythia-410m-deduped", map_location=device)
14
  logit_lens = LogitLens(model)
15
 
16
  lens_options_dict = {
17
  "Tuned Lens": tuned_lens,
18
  "Logit Lens": logit_lens,
19
  }
20
+
21
  statistic_options_dict = {
22
  "Entropy": "entropy",
23
  "Cross Entropy": "ce",
 
43
  start_pos=max(len(input_ids[0]) - token_cutoff, 0),
44
  statistic=statistic_options_dict[statistic],
45
  )
46
+ fig.update_layout(template="plotly_dark")
47
+
48
+ # Update the colorscale of the heatmap trace
49
+ for trace in fig.data:
50
+ if trace.type == "heatmap":
51
+ trace.update(colorscale="Inferno")
52
 
53
  return fig
54
 
 
61
  A lens into a transformer with n layers allows you to replace the last $m$ layers of the model with an [affine transformation](https://pytorch.org/docs/stable/generated/torch.nn.Linear.html) (we call these affine translators).
62
 
63
  This essentially skips over these last few layers and lets you see the best prediction that can be made from the model's representations, i.e. the residual stream, at layer $n - m$. Since the representations may be rotated, shifted, or stretched from layer to layer it's useful to train the len's affine adapters specifically on each layer. This training is what differentiates this method from simpler approaches that decode the residual stream of the network directly using the unembeding layer i.e. the logit lens. We explain this process in [the paper](https://arxiv.org/abs/2303.08112).
 
64
 
65
 
66
+ ## Usage
67
+ Since the tuned lens produces a distribution of predictions to visualize it's output we need to we need to provide a summary statistic to plot. The default is simply [entropy](https://en.wikipedia.org/wiki/Entropy_(information_theory)), but you can also choose the [cross entropy](https://en.wikipedia.org/wiki/Cross_entropy) with the target token, or the [KL divergence](https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence) between the model's predictions and the tuned lens' predictions. You can also hover over a token to see more of the distribution i.e. the top 10 most probable tokens and their probabilities.
68
+ """
69
+
70
+ with gr.Blocks() as demo:
71
  gr.Markdown(preamble)
72
  with gr.Column():
73
  text = gr.Textbox(
 
84
  label="Select Statistic",
85
  )
86
  token_cutoff = gr.Slider(
87
+ maximum=20, minimum=2, value=10, step=1, label="Plot Last N Tokens"
88
  )
89
  examine_btn = gr.Button(value="Submit")
90
  plot = gr.Plot()
91
  examine_btn.click(make_plot, [lens_options, text, statistic, token_cutoff], plot)
92
+ demo.load(make_plot, [lens_options, text, statistic, token_cutoff], plot)
93
+
94
+ if __name__ == "__main__":
95
+ demo.launch()
96