Spaces:
Running
Running
Commit ·
3f991b4
1
Parent(s): 718dfcb
Added full sequence to analysis, still need to do output scrubber
Browse files- app.py +41 -14
- tests/test_model_patterns.py +87 -0
- todo.md +8 -0
app.py
CHANGED
|
@@ -370,10 +370,14 @@ def run_generation(n_clicks, model_name, prompt, max_new_tokens, beam_width, pat
|
|
| 370 |
return (html.Div("Please select modules in the sidebar.", style={'color': 'red'}),
|
| 371 |
results, {'display': 'none'}, {'display': 'none'}, {}, {}, original_prompt_data, {})
|
| 372 |
|
| 373 |
-
#
|
| 374 |
-
#
|
| 375 |
-
#
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
results_ui = []
|
| 379 |
if max_new_tokens > 1:
|
|
@@ -410,25 +414,27 @@ def run_generation(n_clicks, model_name, prompt, max_new_tokens, beam_width, pat
|
|
| 410 |
|
| 411 |
@app.callback(
|
| 412 |
[Output('session-selected-beam-store', 'data', allow_duplicate=True),
|
| 413 |
-
Output('generation-results-container', 'children', allow_duplicate=True)
|
|
|
|
| 414 |
Input({'type': 'result-item', 'index': ALL}, 'n_clicks'),
|
| 415 |
-
[State('generation-results-store', 'data')
|
|
|
|
| 416 |
prevent_initial_call=True
|
| 417 |
)
|
| 418 |
-
def store_selected_beam(n_clicks_list, results_data):
|
| 419 |
"""
|
| 420 |
-
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
"""
|
| 426 |
if not any(n_clicks_list) or not results_data:
|
| 427 |
-
return no_update, no_update
|
| 428 |
|
| 429 |
ctx = dash.callback_context
|
| 430 |
if not ctx.triggered:
|
| 431 |
-
return no_update, no_update
|
| 432 |
|
| 433 |
triggered_id = json.loads(ctx.triggered[0]['prop_id'].split('.')[0])
|
| 434 |
index = triggered_id['index']
|
|
@@ -470,8 +476,29 @@ def store_selected_beam(n_clicks_list, results_data):
|
|
| 470 |
})
|
| 471 |
])
|
| 472 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
# Store the selected beam for comparison after experiments
|
| 474 |
-
return {'text': result['text'], 'score': result.get('score', 0), 'index': index},
|
|
|
|
| 475 |
|
| 476 |
|
| 477 |
# ============================================================================
|
|
|
|
| 370 |
return (html.Div("Please select modules in the sidebar.", style={'color': 'red'}),
|
| 371 |
results, {'display': 'none'}, {'display': 'none'}, {}, {}, original_prompt_data, {})
|
| 372 |
|
| 373 |
+
# For single-token generation, analyze the full output (prompt + generated token)
|
| 374 |
+
# so attention covers the entire sequence. For multi-token, start with prompt only;
|
| 375 |
+
# the full-sequence analysis runs when the user selects a beam.
|
| 376 |
+
if max_new_tokens == 1:
|
| 377 |
+
full_text = results[0]['text']
|
| 378 |
+
else:
|
| 379 |
+
full_text = prompt
|
| 380 |
+
activation_data = execute_forward_pass(model, tokenizer, full_text, config)
|
| 381 |
|
| 382 |
results_ui = []
|
| 383 |
if max_new_tokens > 1:
|
|
|
|
| 414 |
|
| 415 |
@app.callback(
|
| 416 |
[Output('session-selected-beam-store', 'data', allow_duplicate=True),
|
| 417 |
+
Output('generation-results-container', 'children', allow_duplicate=True),
|
| 418 |
+
Output('session-activation-store', 'data', allow_duplicate=True)],
|
| 419 |
Input({'type': 'result-item', 'index': ALL}, 'n_clicks'),
|
| 420 |
+
[State('generation-results-store', 'data'),
|
| 421 |
+
State('session-activation-store', 'data')],
|
| 422 |
prevent_initial_call=True
|
| 423 |
)
|
| 424 |
+
def store_selected_beam(n_clicks_list, results_data, existing_activation_data):
|
| 425 |
"""
|
| 426 |
+
Store selected beam and re-run forward pass on the full sequence.
|
| 427 |
|
| 428 |
+
When a beam is selected, this re-runs execute_forward_pass on the complete
|
| 429 |
+
generated text (prompt + output) so the attention visualization covers
|
| 430 |
+
the entire chosen output, not just the input.
|
| 431 |
"""
|
| 432 |
if not any(n_clicks_list) or not results_data:
|
| 433 |
+
return no_update, no_update, no_update
|
| 434 |
|
| 435 |
ctx = dash.callback_context
|
| 436 |
if not ctx.triggered:
|
| 437 |
+
return no_update, no_update, no_update
|
| 438 |
|
| 439 |
triggered_id = json.loads(ctx.triggered[0]['prop_id'].split('.')[0])
|
| 440 |
index = triggered_id['index']
|
|
|
|
| 476 |
})
|
| 477 |
])
|
| 478 |
|
| 479 |
+
# Re-run forward pass on the full beam text so attention covers entire output
|
| 480 |
+
new_activation_data = no_update
|
| 481 |
+
if existing_activation_data:
|
| 482 |
+
try:
|
| 483 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 484 |
+
model_name = existing_activation_data['model']
|
| 485 |
+
config = {
|
| 486 |
+
'attention_modules': existing_activation_data['attention_modules'],
|
| 487 |
+
'block_modules': existing_activation_data['block_modules'],
|
| 488 |
+
'norm_parameters': existing_activation_data.get('norm_parameters', [])
|
| 489 |
+
}
|
| 490 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, attn_implementation='eager')
|
| 491 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 492 |
+
model.eval()
|
| 493 |
+
new_activation_data = execute_forward_pass(model, tokenizer, result['text'], config)
|
| 494 |
+
except Exception as e:
|
| 495 |
+
import traceback
|
| 496 |
+
traceback.print_exc()
|
| 497 |
+
print(f"Warning: Could not re-run forward pass for full sequence: {e}")
|
| 498 |
+
|
| 499 |
# Store the selected beam for comparison after experiments
|
| 500 |
+
return ({'text': result['text'], 'score': result.get('score', 0), 'index': index},
|
| 501 |
+
selected_ui, new_activation_data)
|
| 502 |
|
| 503 |
|
| 504 |
# ============================================================================
|
tests/test_model_patterns.py
CHANGED
|
@@ -261,3 +261,90 @@ class TestMultiLayerHeadAblation:
|
|
| 261 |
|
| 262 |
assert 'error' in result
|
| 263 |
assert '99' in result['error'] # Should mention the invalid layer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
assert 'error' in result
|
| 263 |
assert '99' in result['error'] # Should mention the invalid layer
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
class TestFullSequenceAttentionData:
|
| 267 |
+
"""
|
| 268 |
+
Tests verifying that activation data for full sequences (prompt + generated output)
|
| 269 |
+
has correctly-sized attention matrices matching the full token count.
|
| 270 |
+
"""
|
| 271 |
+
|
| 272 |
+
def _make_activation_data(self, num_tokens, num_layers=2, num_heads=1):
|
| 273 |
+
"""Helper to build mock activation data for a given sequence length."""
|
| 274 |
+
input_ids = list(range(1, num_tokens + 1))
|
| 275 |
+
# Build uniform attention weights: [batch=1, heads, seq, seq]
|
| 276 |
+
uniform_val = 1.0 / num_tokens
|
| 277 |
+
attn_row = [uniform_val] * num_tokens
|
| 278 |
+
head_matrix = [attn_row] * num_tokens
|
| 279 |
+
head_block = [head_matrix] * num_heads
|
| 280 |
+
attn_weights = [head_block] # batch dim
|
| 281 |
+
|
| 282 |
+
attention_outputs = {}
|
| 283 |
+
for layer in range(num_layers):
|
| 284 |
+
module_name = f'model.layers.{layer}.self_attn'
|
| 285 |
+
attention_outputs[module_name] = {
|
| 286 |
+
'output': [
|
| 287 |
+
[[0.1] * num_tokens], # hidden states placeholder
|
| 288 |
+
attn_weights
|
| 289 |
+
]
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
return {
|
| 293 |
+
'model': 'mock-model',
|
| 294 |
+
'prompt': 'x ' * num_tokens,
|
| 295 |
+
'input_ids': [input_ids],
|
| 296 |
+
'attention_modules': list(attention_outputs.keys()),
|
| 297 |
+
'attention_outputs': attention_outputs,
|
| 298 |
+
'block_modules': [f'model.layers.{i}' for i in range(num_layers)],
|
| 299 |
+
'block_outputs': {f'model.layers.{i}': {'output': [[[0.1] * num_tokens]]} for i in range(num_layers)},
|
| 300 |
+
'norm_parameters': [],
|
| 301 |
+
'norm_data': [],
|
| 302 |
+
'actual_output': {'token': 'tok', 'probability': 0.5},
|
| 303 |
+
'global_top5_tokens': []
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
def test_short_prompt_attention_dimensions(self):
|
| 307 |
+
"""Attention matrix for a 4-token prompt should be 4x4."""
|
| 308 |
+
data = self._make_activation_data(num_tokens=4)
|
| 309 |
+
attn = data['attention_outputs']['model.layers.0.self_attn']['output'][1]
|
| 310 |
+
seq_len = len(attn[0][0]) # batch -> head -> rows
|
| 311 |
+
assert seq_len == 4
|
| 312 |
+
assert len(attn[0][0][0]) == 4 # columns
|
| 313 |
+
|
| 314 |
+
def test_full_sequence_attention_dimensions(self):
|
| 315 |
+
"""Attention matrix for a 10-token full sequence (prompt+output) should be 10x10."""
|
| 316 |
+
data = self._make_activation_data(num_tokens=10)
|
| 317 |
+
attn = data['attention_outputs']['model.layers.0.self_attn']['output'][1]
|
| 318 |
+
seq_len = len(attn[0][0])
|
| 319 |
+
assert seq_len == 10
|
| 320 |
+
assert len(attn[0][0][0]) == 10
|
| 321 |
+
|
| 322 |
+
def test_full_sequence_has_more_tokens_than_prompt(self):
|
| 323 |
+
"""Full-sequence activation data should have larger dimensions than prompt-only."""
|
| 324 |
+
prompt_data = self._make_activation_data(num_tokens=4)
|
| 325 |
+
full_data = self._make_activation_data(num_tokens=10)
|
| 326 |
+
|
| 327 |
+
prompt_ids = prompt_data['input_ids'][0]
|
| 328 |
+
full_ids = full_data['input_ids'][0]
|
| 329 |
+
assert len(full_ids) > len(prompt_ids)
|
| 330 |
+
|
| 331 |
+
prompt_attn = prompt_data['attention_outputs']['model.layers.0.self_attn']['output'][1]
|
| 332 |
+
full_attn = full_data['attention_outputs']['model.layers.0.self_attn']['output'][1]
|
| 333 |
+
assert len(full_attn[0][0]) > len(prompt_attn[0][0])
|
| 334 |
+
|
| 335 |
+
def test_input_ids_match_attention_seq_len(self):
|
| 336 |
+
"""input_ids length should match attention matrix dimensions."""
|
| 337 |
+
for n in [3, 7, 15]:
|
| 338 |
+
data = self._make_activation_data(num_tokens=n)
|
| 339 |
+
num_ids = len(data['input_ids'][0])
|
| 340 |
+
attn = data['attention_outputs']['model.layers.0.self_attn']['output'][1]
|
| 341 |
+
assert num_ids == len(attn[0][0]) == n
|
| 342 |
+
|
| 343 |
+
def test_all_layers_have_same_dimensions(self):
|
| 344 |
+
"""All layers should have the same attention matrix size for a given sequence."""
|
| 345 |
+
data = self._make_activation_data(num_tokens=8, num_layers=3)
|
| 346 |
+
for layer in range(3):
|
| 347 |
+
module = f'model.layers.{layer}.self_attn'
|
| 348 |
+
attn = data['attention_outputs'][module]['output'][1]
|
| 349 |
+
assert len(attn[0][0]) == 8
|
| 350 |
+
assert len(attn[0][0][0]) == 8
|
todo.md
CHANGED
|
@@ -200,3 +200,11 @@
|
|
| 200 |
- [x] Add CSS for `.chat-resize-handle` (cursor, hover highlight) in style.css
|
| 201 |
- [x] Add `assets/chat_resize.js` for mousedown/mousemove/mouseup drag logic
|
| 202 |
- Default size unchanged (25vw, 320px–450px); drag overrides max-width up to 80vw
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
- [x] Add CSS for `.chat-resize-handle` (cursor, hover highlight) in style.css
|
| 201 |
- [x] Add `assets/chat_resize.js` for mousedown/mousemove/mouseup drag logic
|
| 202 |
- Default size unchanged (25vw, 320px–450px); drag overrides max-width up to 80vw
|
| 203 |
+
|
| 204 |
+
## Completed: Full-Sequence Attention Visualization
|
| 205 |
+
|
| 206 |
+
- [x] Modified `run_generation()` in app.py: for single-token generation, run forward pass on full beam text (prompt + generated token) instead of prompt only
|
| 207 |
+
- [x] Enhanced `store_selected_beam()` in app.py: added `session-activation-store` output and `session-activation-store` State; re-runs `execute_forward_pass()` on selected beam's full text when user picks a beam
|
| 208 |
+
- [x] Added 5 tests in `test_model_patterns.py` (`TestFullSequenceAttentionData`) verifying attention matrix dimensions match full sequence length
|
| 209 |
+
- Attention visualization now covers the entire chosen output (input + generated tokens), not just the input prompt
|
| 210 |
+
- No changes needed in `model_patterns.py`, `beam_search.py`, `pipeline.py`, or `head_detection.py`
|