cdpearlman commited on
Commit
96418df
·
1 Parent(s): a836411

fixed ablation bug, ablated output was shown in pipeline

Browse files
Files changed (3) hide show
  1. .claude/settings.local.json +2 -1
  2. app.py +18 -18
  3. showcase-plan.md +0 -198
.claude/settings.local.json CHANGED
@@ -10,7 +10,8 @@
10
  "Bash(grep:*)",
11
  "Bash(python3:*)",
12
  "Bash(find /c/Users/cdpea/OneDrive/Documents/GradProject/.context -type f -name *.md)",
13
- "Bash(wc:*)"
 
14
  ]
15
  }
16
  }
 
10
  "Bash(grep:*)",
11
  "Bash(python3:*)",
12
  "Bash(find /c/Users/cdpea/OneDrive/Documents/GradProject/.context -type f -name *.md)",
13
+ "Bash(wc:*)",
14
+ "Bash(ls -la /c/Users/cdpea/OneDrive/Documents/GradProject/utils/*.py)"
15
  ]
16
  }
17
  }
app.py CHANGED
@@ -66,6 +66,7 @@ app.layout = html.Div([
66
  dcc.Store(id='session-original-prompt-store', storage_type='memory'), # Original user prompt
67
  dcc.Store(id='session-selected-beam-store', storage_type='memory'), # Selected beam for comparison
68
  dcc.Store(id='head-categories-store', storage_type='memory'), # Category→heads mapping for ablation
 
69
 
70
  # Main container
71
  html.Div([
@@ -215,16 +216,17 @@ def toggle_glossary(open_clicks, close_clicks, overlay_clicks):
215
  Output('session-selected-beam-store', 'data', allow_duplicate=True),
216
  Output('ablation-selected-heads', 'data', allow_duplicate=True),
217
  Output('pipeline-container', 'style', allow_duplicate=True),
218
- Output('investigation-panel', 'style', allow_duplicate=True)],
 
219
  [Input('model-dropdown', 'value')],
220
  prevent_initial_call=True
221
  )
222
  def load_model_patterns(selected_model):
223
  """Load and categorize model patterns when a model is selected."""
224
- # 9 new cleared outputs: activation, activation-original, gen-results-store,
225
  # gen-results-container, original-prompt, selected-beam, ablation-heads,
226
- # pipeline style, investigation style
227
- cleared_stores = ({}, {}, None, [], {}, {}, [], {'display': 'none'}, {'display': 'none'})
228
  if not selected_model:
229
  return ({}, [], [], [], None, None, None, None) + cleared_stores
230
 
@@ -954,7 +956,7 @@ def manage_ablation_heads(add_clicks, clear_clicks, remove_clicks, category_clic
954
 
955
  @app.callback(
956
  [Output('ablation-results-container', 'children'),
957
- Output('session-activation-store', 'data', allow_duplicate=True),
958
  Output('session-activation-store-original', 'data', allow_duplicate=True)],
959
  [Input('run-ablation-btn', 'n_clicks')],
960
  [State('ablation-selected-heads', 'data'),
@@ -1076,7 +1078,7 @@ def run_ablation_experiment(n_clicks, selected_heads, activation_data, model_nam
1076
  Output('ablation-divergence-indicator', 'children')],
1077
  [Input('ablation-scrubber-slider', 'value')],
1078
  [State('session-activation-store-original', 'data'),
1079
- State('session-activation-store', 'data')],
1080
  prevent_initial_call=True
1081
  )
1082
  def update_ablation_scrubber(position, original_data, ablated_data):
@@ -1198,33 +1200,31 @@ def run_attribution_experiment(n_clicks, method, target_token, activation_data,
1198
 
1199
  @app.callback(
1200
  Output('reset-ablation-container', 'style'),
1201
- Input('session-activation-store', 'data'),
1202
  prevent_initial_call=False
1203
  )
1204
- def toggle_reset_ablation_button(activation_data):
1205
- if activation_data and activation_data.get('ablated', False):
1206
  return {'display': 'block'}
1207
  return {'display': 'none'}
1208
 
1209
 
1210
  @app.callback(
1211
- [Output('session-activation-store', 'data', allow_duplicate=True),
1212
- Output('session-activation-store-original', 'data'),
1213
  Output('model-status', 'children', allow_duplicate=True)],
1214
  Input('reset-ablation-btn', 'n_clicks'),
1215
- [State('session-activation-store-original', 'data')],
1216
  prevent_initial_call=True
1217
  )
1218
- def reset_ablation(n_clicks, original_data):
1219
- if not n_clicks or not original_data:
1220
- return no_update, no_update, no_update
1221
-
1222
  success_message = html.Div([
1223
  html.I(className="fas fa-undo", style={'marginRight': '8px', 'color': '#28a745'}),
1224
  "Ablation reset"
1225
  ], className="status-success")
1226
-
1227
- return original_data, {}, success_message
1228
 
1229
 
1230
  # ============================================================================
 
66
  dcc.Store(id='session-original-prompt-store', storage_type='memory'), # Original user prompt
67
  dcc.Store(id='session-selected-beam-store', storage_type='memory'), # Selected beam for comparison
68
  dcc.Store(id='head-categories-store', storage_type='memory'), # Category→heads mapping for ablation
69
+ dcc.Store(id='session-ablation-results-store', storage_type='memory'), # Ablated activation data (separate from pipeline)
70
 
71
  # Main container
72
  html.Div([
 
216
  Output('session-selected-beam-store', 'data', allow_duplicate=True),
217
  Output('ablation-selected-heads', 'data', allow_duplicate=True),
218
  Output('pipeline-container', 'style', allow_duplicate=True),
219
+ Output('investigation-panel', 'style', allow_duplicate=True),
220
+ Output('session-ablation-results-store', 'data', allow_duplicate=True)],
221
  [Input('model-dropdown', 'value')],
222
  prevent_initial_call=True
223
  )
224
  def load_model_patterns(selected_model):
225
  """Load and categorize model patterns when a model is selected."""
226
+ # 10 new cleared outputs: activation, activation-original, gen-results-store,
227
  # gen-results-container, original-prompt, selected-beam, ablation-heads,
228
+ # pipeline style, investigation style, ablation-results
229
+ cleared_stores = ({}, {}, None, [], {}, {}, [], {'display': 'none'}, {'display': 'none'}, {})
230
  if not selected_model:
231
  return ({}, [], [], [], None, None, None, None) + cleared_stores
232
 
 
956
 
957
  @app.callback(
958
  [Output('ablation-results-container', 'children'),
959
+ Output('session-ablation-results-store', 'data'),
960
  Output('session-activation-store-original', 'data', allow_duplicate=True)],
961
  [Input('run-ablation-btn', 'n_clicks')],
962
  [State('ablation-selected-heads', 'data'),
 
1078
  Output('ablation-divergence-indicator', 'children')],
1079
  [Input('ablation-scrubber-slider', 'value')],
1080
  [State('session-activation-store-original', 'data'),
1081
+ State('session-ablation-results-store', 'data')],
1082
  prevent_initial_call=True
1083
  )
1084
  def update_ablation_scrubber(position, original_data, ablated_data):
 
1200
 
1201
  @app.callback(
1202
  Output('reset-ablation-container', 'style'),
1203
+ Input('session-ablation-results-store', 'data'),
1204
  prevent_initial_call=False
1205
  )
1206
+ def toggle_reset_ablation_button(ablation_results_data):
1207
+ if ablation_results_data:
1208
  return {'display': 'block'}
1209
  return {'display': 'none'}
1210
 
1211
 
1212
  @app.callback(
1213
+ [Output('session-ablation-results-store', 'data', allow_duplicate=True),
 
1214
  Output('model-status', 'children', allow_duplicate=True)],
1215
  Input('reset-ablation-btn', 'n_clicks'),
 
1216
  prevent_initial_call=True
1217
  )
1218
+ def reset_ablation(n_clicks):
1219
+ if not n_clicks:
1220
+ return no_update, no_update
1221
+
1222
  success_message = html.Div([
1223
  html.I(className="fas fa-undo", style={'marginRight': '8px', 'color': '#28a745'}),
1224
  "Ablation reset"
1225
  ], className="status-success")
1226
+
1227
+ return {}, success_message
1228
 
1229
 
1230
  # ============================================================================
showcase-plan.md DELETED
@@ -1,198 +0,0 @@
1
- # Showcase Demo Plan
2
-
3
- ## Context
4
- Presenting the Transformer Explanation Dashboard at a science-fair-style showcase. Free-roam format, K-12 students (mixed grades, elementary through high school), groups of 3-5 at a time, ~1-3 minute interactions. Large external monitor with mouse for students. Engagement is the primary goal; feedback (QR code form on phone) is secondary.
5
-
6
- ## Strategy: "Guided Magic Trick" (Hybrid of Options 1 + 3)
7
-
8
- The chatbot is the anchor. You run a short, repeatable demo loop that feels like showing a magic trick, with a volunteer moment built in. The loop is designed to be restarted at any point when a new group walks up.
9
-
10
- ---
11
-
12
- ### The Demo Loop (~2 minutes)
13
-
14
- **1. Hook (15 sec)**
15
- - Chatbot is open on screen with a friendly pre-loaded exchange visible (something like "How does AI understand words?")
16
- - As students approach: *"Have you ever used ChatGPT? Want to see what's happening inside its brain?"*
17
- - For younger kids: *"Want to see how a robot reads?"*
18
-
19
- **2. The Question (15 sec)**
20
- - Ask a student to suggest a short sentence, or use a pre-loaded one if they're shy
21
- - Type it into the app (or have them type it — handing the mouse over is a good engagement moment for one student)
22
- - Run inference so the dashboard populates
23
-
24
- **3. The Reveal (30 sec)**
25
- - Switch to the visualization view. *"This is what the AI sees when it reads your sentence."*
26
- - Show the attention map — point out how some words "look at" other words more. Use simple language:
27
- - High school: *"Each word figures out which other words matter most for understanding it"*
28
- - Middle school: *"It's like the AI highlighting the important connections"*
29
- - Elementary: *"See how these words are friends? The bright colors mean they're paying attention to each other"*
30
- - This is the visual wow moment — attention heatmaps are inherently striking
31
-
32
- **4. The Experiment (30 sec) — Volunteer Moment**
33
- - *"What do you think happens if we break part of the AI's brain?"*
34
- - Hand the mouse to a student. Guide them to disable an attention head (ablation)
35
- - Let them click. Output changes. Visible reaction.
36
- - *"You just did what AI researchers do — you ran an experiment on a neural network."*
37
-
38
- **5. The Exit (15 sec)**
39
- - *"If you thought that was cool, I'd love your feedback — scan this QR code and tell me what you'd want to explore next"*
40
- - Point to QR code
41
- - If they linger, offer the chatbot: *"You can also ask the AI chatbot any question about how it works"* — this gives curious students a self-directed path while you reset for the next group
42
-
43
- ---
44
-
45
- ### Adapting to the Audience On-the-Fly
46
-
47
- | Signal | Adapt |
48
- |--------|-------|
49
- | Young kids (K-3) | Skip the experiment step, focus on the visual ("look at the colors!"). Keep it to 60 seconds. |
50
- | Shy group | Don't force the mouse hand-off. You drive, narrate enthusiastically. |
51
- | Engaged teen | Let them drive. Show them the chatbot. Point them to activation steering if they want more. Let them stay. |
52
- | Large crowd (5+) | You drive the whole thing. Speak louder. Keep it short and punchy. Don't hand off the mouse. |
53
- | Someone asks "how does it really work?" | This is your dream scenario — point them to the chatbot and the data flow visualization. Let them explore. |
54
- | Empty moment (no one at table) | Reset to chatbot with an eye-catching exchange on screen. Good time for a break. |
55
-
56
- ---
57
-
58
- ### Physical Setup
59
-
60
- - **Monitor**: Dashboard visible, chatbot tab open as default state
61
- - **Laptop**: Facing you, used for quick resets or switching views
62
- - **Mouse**: On the student side of the table, positioned for easy hand-off
63
- - **QR code**: Printed, propped up visibly near the monitor (not flat on table — eye level if possible)
64
- - **Optional**: A one-line sign/card: *"See inside an AI's brain"* or *"How does ChatGPT think?"* — a hook for passersby
65
-
66
- ---
67
-
68
- ### Concrete "Wow Moment" Experiments (GPT-2 — 12 layers, 12 heads)
69
-
70
- Use **5-8 generated tokens** — fast enough to keep the demo snappy, long enough to see divergence when heads are ablated.
71
-
72
- #### Experiment 1: "Breaking Pattern Memory" (Induction Heads) — PRIMARY DEMO
73
-
74
- **Prompt:** `The cat sat on the mat. The cat sat on the`
75
-
76
- **Why it's the best pick:**
77
- - Every student can see the pattern and predict "mat"
78
- - The model should also predict "mat" — normal output feels satisfying
79
- - Ablating induction heads should break pattern completion — the model "forgets" it already saw this
80
- - Universal across all ages
81
-
82
- **Attention view talking points:**
83
- - Before ablation: attention lines from the second "cat" reach back to the first "cat" and "mat"
84
- - *"See how the AI is looking back at the first sentence? It's copying the pattern."*
85
-
86
- **Heads to ablate (ranked by impact):**
87
- 1. **Layer 5, Head 1** (score 0.44) — strongest induction head, try first
88
- 2. **Layer 5, Head 5** (score 0.41)
89
- 3. **Layer 5, Head 0** (score 0.34)
90
-
91
- GPT-2's induction heads cluster heavily in Layer 5. This is actually a bonus for the demo — you can say *"all the pattern-matching lives in one layer"* and ablate multiple heads in the same layer with a few clicks.
92
-
93
- **How to ablate:** Use the "Induction" and "Duplicate Token" category buttons to select all heads at once.
94
-
95
- **Confirmed result:** Ablating induction + duplicate-token heads together successfully breaks pattern completion.
96
-
97
- **Narration by age:**
98
- - Elementary: *"We broke its memory! It forgot what it already read!"*
99
- - Middle school: *"We turned off the pattern-matching part."*
100
- - High school: *"These are called induction heads — they detect and complete repeated sequences. Notice how they're all in Layer 5."*
101
-
102
- ---
103
-
104
- #### Experiment 2: "Breaking Grammar" (Previous Token Heads) — BACKUP DEMO
105
-
106
- **Prompt:** `The chef gave the waiter a generous tip because`
107
-
108
- **Why it works:**
109
- - Natural sentence anyone understands
110
- - Previous-token heads maintain word-by-word coherence (grammar)
111
- - Ablating them can produce broken or nonsensical continuations
112
-
113
- **Heads to ablate:**
114
- 1. **Layer 4, Head 11** (score 0.97) — near-perfect previous-token head, GPT-2's strongest specialized head overall
115
- 2. **Layer 2, Head 2** (score 0.57)
116
- 3. **Layer 3, Head 7** (score 0.44)
117
-
118
- L4-H11 at 0.97 is remarkable — it's almost entirely dedicated to "look at the word right before me." This makes it a great single-click ablation target.
119
-
120
- **How to ablate:** Use the "Previous Token" category button to select all heads at once.
121
-
122
- **Confirmed result:** Ablating previous-token heads successfully breaks grammar/coherence.
123
-
124
- **Narration:** *"This part of the AI's brain reads one word at a time, left to right — like you do. This one head spends 97% of its effort just looking at the previous word. What happens if we turn it off?"*
125
-
126
- ---
127
-
128
- #### Experiment 3: "Double Knockout" (Induction + Duplicate Token) — GRAND FINALE
129
-
130
- **Same prompt as Experiment 1:** `The cat sat on the mat. The cat sat on the`
131
-
132
- This layers on top of Experiment 1 for engaged groups who want more.
133
-
134
- **How it works:** Duplicate-token heads notice "I've seen 'the' and 'cat' before" while induction heads complete the pattern. They're a team — ablating both should be devastating.
135
-
136
- **Duplicate-token heads to ablate:**
137
- 1. **Layer 0, Head 1** (score 0.42) — strongest
138
- 2. **Layer 0, Head 5** (score 0.42)
139
- 3. **Layer 1, Head 11** (score 0.33)
140
-
141
- Note: GPT-2's duplicate-token heads are in early layers (0-1) while induction heads are in Layer 5. This makes a nice story: *"The early layers spot the repetition, the middle layers use it to predict what comes next."*
142
-
143
- **How to ablate:** Use both the "Induction" and "Duplicate Token" category buttons.
144
-
145
- **Confirmed result:** Combined induction + duplicate-token ablation produces maximum disruption — the model completely loses pattern completion ability.
146
-
147
- **Narration:** *"First we turned off pattern matching. Now let's ALSO turn off the part that notices repeated words... watch what happens."*
148
-
149
- ---
150
-
151
- #### Experiment 4: "The Bat Problem" (Ambiguity — Attention Viz Only)
152
-
153
- **Prompt:** `The bat flew over the`
154
-
155
- **Why it works:**
156
- - "Bat" is ambiguous (animal vs. baseball)
157
- - Attention should show the model connecting "bat" to "flew" to disambiguate
158
- - Great for the "Reveal" step (step 3 in demo loop) — pure attention visualization wow
159
- - Less about ablation, more about "look what the AI notices"
160
-
161
- **How to ablate:** Use the "Previous Token" category button.
162
-
163
- **Confirmed result:** Ablating previous-token heads successfully disrupts this prompt.
164
-
165
- **Talking points:**
166
- - *"The AI figured out this is a flying bat, not a baseball bat — look at how it connects 'bat' to 'flew'."*
167
- - Show attention visualization first (the "Reveal" step), then ablate for engaged groups
168
-
169
- ---
170
-
171
- #### Quick Reference Card
172
-
173
- | Demo | Prompt | Ablate (Category Buttons) | Effect |
174
- |------|--------|---------------------------|--------|
175
- | Primary | `The cat sat on the mat. The cat sat on the` | Induction + Duplicate Token | Breaks pattern completion |
176
- | Backup 1 | `The chef gave the waiter a generous tip because` | Previous Token | Breaks grammar/coherence |
177
- | Backup 2 | `The bat flew over the` | Previous Token | Disrupts ambiguity resolution |
178
-
179
- ---
180
-
181
- ### Pre-Showcase Prep Checklist
182
-
183
- - [x] Identify pre-loaded sentences that produce visually striking attention patterns (3 confirmed — see Quick Reference Card)
184
- - [x] Verify ablation demos work reliably with chosen sentences (all 3 tested and confirmed)
185
- - [ ] Practice the 2-minute loop 3-5 times until it's smooth and natural
186
- - [ ] Test the monitor setup — make sure text is readable from 4-5 feet away (increase font/zoom if needed)
187
- - [ ] Print QR code large enough to scan from arm's length
188
- - [ ] Check that dark mode / light mode looks good on the external monitor under showcase lighting
189
-
190
- ---
191
-
192
- ### Key Principles
193
-
194
- 1. **You are the narrator, not the app.** The app is a prop for your story. The magic is in your framing, not the UI.
195
- 2. **Repeatable > perfect.** You'll run this loop 20+ times. It needs to be tight and restartable, not elaborate.
196
- 3. **One wow moment is enough.** Don't try to show everything. The attention map + ablation is your one-two punch.
197
- 4. **Hand off the mouse, not the narrative.** Students click where you point. You keep talking.
198
- 5. **Read the room fast.** You have ~5 seconds to gauge the group's age/interest level and calibrate your language.