Spaces:
Sleeping
Sleeping
Commit ·
bb577e6
1
Parent(s): 0f46e9c
Fix ablation head selection: handle old data format, prevent spurious selections on render
Browse files- app.py +36 -8
- components/investigation_panel.py +15 -0
app.py
CHANGED
|
@@ -674,8 +674,12 @@ def update_ablation_head_grid(layer_num, selected_heads, model_name):
|
|
| 674 |
from transformers import AutoConfig
|
| 675 |
config = AutoConfig.from_pretrained(model_name)
|
| 676 |
num_heads = config.num_attention_heads
|
| 677 |
-
#
|
| 678 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
except:
|
| 680 |
return html.P("Could not load model config.", style={'color': '#dc3545'})
|
| 681 |
|
|
@@ -693,18 +697,31 @@ def handle_ablation_head_selection(n_clicks_list, selected_heads):
|
|
| 693 |
Handle head button clicks - stores layer+head combinations and preserves
|
| 694 |
selections across layer changes.
|
| 695 |
"""
|
| 696 |
-
|
|
|
|
| 697 |
return no_update, no_update, no_update
|
| 698 |
|
| 699 |
ctx = dash.callback_context
|
| 700 |
if not ctx.triggered:
|
| 701 |
return no_update, no_update, no_update
|
| 702 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 703 |
if selected_heads is None:
|
| 704 |
selected_heads = []
|
|
|
|
|
|
|
|
|
|
| 705 |
|
| 706 |
try:
|
| 707 |
-
triggered_id = json.loads(
|
| 708 |
layer_num = triggered_id['layer']
|
| 709 |
head_idx = triggered_id['head']
|
| 710 |
|
|
@@ -724,7 +741,8 @@ def handle_ablation_head_selection(n_clicks_list, selected_heads):
|
|
| 724 |
else:
|
| 725 |
# Add if not selected
|
| 726 |
selected_heads.append(new_item)
|
| 727 |
-
except:
|
|
|
|
| 728 |
pass
|
| 729 |
|
| 730 |
# Create the display of selected heads
|
|
@@ -743,18 +761,27 @@ def handle_ablation_head_selection(n_clicks_list, selected_heads):
|
|
| 743 |
)
|
| 744 |
def handle_ablation_head_removal(n_clicks_list, selected_heads):
|
| 745 |
"""Handle removing individual head selections via the x button on chips."""
|
| 746 |
-
|
|
|
|
| 747 |
return no_update, no_update, no_update
|
| 748 |
|
| 749 |
ctx = dash.callback_context
|
| 750 |
if not ctx.triggered:
|
| 751 |
return no_update, no_update, no_update
|
| 752 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
if selected_heads is None:
|
| 754 |
selected_heads = []
|
| 755 |
|
| 756 |
try:
|
| 757 |
-
triggered_id = json.loads(
|
| 758 |
layer_num = triggered_id['layer']
|
| 759 |
head_idx = triggered_id['head']
|
| 760 |
|
|
@@ -763,7 +790,8 @@ def handle_ablation_head_removal(n_clicks_list, selected_heads):
|
|
| 763 |
item for item in selected_heads
|
| 764 |
if not (isinstance(item, dict) and item.get('layer') == layer_num and item.get('head') == head_idx)
|
| 765 |
]
|
| 766 |
-
except:
|
|
|
|
| 767 |
pass
|
| 768 |
|
| 769 |
# Create the updated display
|
|
|
|
| 674 |
from transformers import AutoConfig
|
| 675 |
config = AutoConfig.from_pretrained(model_name)
|
| 676 |
num_heads = config.num_attention_heads
|
| 677 |
+
# Filter to only valid dict entries and pass to button creator
|
| 678 |
+
valid_selected = [
|
| 679 |
+
item for item in (selected_heads or [])
|
| 680 |
+
if isinstance(item, dict) and 'layer' in item and 'head' in item
|
| 681 |
+
]
|
| 682 |
+
return create_ablation_head_buttons(num_heads, layer_num, valid_selected)
|
| 683 |
except:
|
| 684 |
return html.P("Could not load model config.", style={'color': '#dc3545'})
|
| 685 |
|
|
|
|
| 697 |
Handle head button clicks - stores layer+head combinations and preserves
|
| 698 |
selections across layer changes.
|
| 699 |
"""
|
| 700 |
+
# Check if any button was actually clicked (n_clicks > 0)
|
| 701 |
+
if not n_clicks_list or not any(n for n in n_clicks_list if n and n > 0):
|
| 702 |
return no_update, no_update, no_update
|
| 703 |
|
| 704 |
ctx = dash.callback_context
|
| 705 |
if not ctx.triggered:
|
| 706 |
return no_update, no_update, no_update
|
| 707 |
|
| 708 |
+
# Get the triggered button's n_clicks value
|
| 709 |
+
triggered_prop = ctx.triggered[0]
|
| 710 |
+
triggered_value = triggered_prop.get('value', 0)
|
| 711 |
+
|
| 712 |
+
# Only process if this is an actual click (not initial render)
|
| 713 |
+
if not triggered_value or triggered_value == 0:
|
| 714 |
+
return no_update, no_update, no_update
|
| 715 |
+
|
| 716 |
+
# Initialize or clean up selected_heads - ensure it's a list of dicts
|
| 717 |
if selected_heads is None:
|
| 718 |
selected_heads = []
|
| 719 |
+
else:
|
| 720 |
+
# Filter out any non-dict entries (cleanup old format data)
|
| 721 |
+
selected_heads = [item for item in selected_heads if isinstance(item, dict)]
|
| 722 |
|
| 723 |
try:
|
| 724 |
+
triggered_id = json.loads(triggered_prop['prop_id'].split('.')[0])
|
| 725 |
layer_num = triggered_id['layer']
|
| 726 |
head_idx = triggered_id['head']
|
| 727 |
|
|
|
|
| 741 |
else:
|
| 742 |
# Add if not selected
|
| 743 |
selected_heads.append(new_item)
|
| 744 |
+
except Exception as e:
|
| 745 |
+
print(f"Error in handle_ablation_head_selection: {e}")
|
| 746 |
pass
|
| 747 |
|
| 748 |
# Create the display of selected heads
|
|
|
|
| 761 |
)
|
| 762 |
def handle_ablation_head_removal(n_clicks_list, selected_heads):
|
| 763 |
"""Handle removing individual head selections via the x button on chips."""
|
| 764 |
+
# Check if any button was actually clicked
|
| 765 |
+
if not n_clicks_list or not any(n for n in n_clicks_list if n and n > 0):
|
| 766 |
return no_update, no_update, no_update
|
| 767 |
|
| 768 |
ctx = dash.callback_context
|
| 769 |
if not ctx.triggered:
|
| 770 |
return no_update, no_update, no_update
|
| 771 |
|
| 772 |
+
# Get the triggered button's n_clicks value
|
| 773 |
+
triggered_prop = ctx.triggered[0]
|
| 774 |
+
triggered_value = triggered_prop.get('value', 0)
|
| 775 |
+
|
| 776 |
+
# Only process if this is an actual click
|
| 777 |
+
if not triggered_value or triggered_value == 0:
|
| 778 |
+
return no_update, no_update, no_update
|
| 779 |
+
|
| 780 |
if selected_heads is None:
|
| 781 |
selected_heads = []
|
| 782 |
|
| 783 |
try:
|
| 784 |
+
triggered_id = json.loads(triggered_prop['prop_id'].split('.')[0])
|
| 785 |
layer_num = triggered_id['layer']
|
| 786 |
head_idx = triggered_id['head']
|
| 787 |
|
|
|
|
| 790 |
item for item in selected_heads
|
| 791 |
if not (isinstance(item, dict) and item.get('layer') == layer_num and item.get('head') == head_idx)
|
| 792 |
]
|
| 793 |
+
except Exception as e:
|
| 794 |
+
print(f"Error in handle_ablation_head_removal: {e}")
|
| 795 |
pass
|
| 796 |
|
| 797 |
# Create the updated display
|
components/investigation_panel.py
CHANGED
|
@@ -284,8 +284,17 @@ def create_selected_heads_display(selected_heads):
|
|
| 284 |
|
| 285 |
chips = []
|
| 286 |
for item in selected_heads:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
layer = item.get('layer')
|
| 288 |
head = item.get('head')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
label = f"L{layer}-H{head}"
|
| 290 |
|
| 291 |
chips.append(
|
|
@@ -321,6 +330,12 @@ def create_selected_heads_display(selected_heads):
|
|
| 321 |
})
|
| 322 |
)
|
| 323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
return html.Div(chips, style={
|
| 325 |
'display': 'flex',
|
| 326 |
'flexWrap': 'wrap',
|
|
|
|
| 284 |
|
| 285 |
chips = []
|
| 286 |
for item in selected_heads:
|
| 287 |
+
# Skip invalid items (non-dict entries from old format)
|
| 288 |
+
if not isinstance(item, dict):
|
| 289 |
+
continue
|
| 290 |
+
|
| 291 |
layer = item.get('layer')
|
| 292 |
head = item.get('head')
|
| 293 |
+
|
| 294 |
+
# Skip if missing required fields
|
| 295 |
+
if layer is None or head is None:
|
| 296 |
+
continue
|
| 297 |
+
|
| 298 |
label = f"L{layer}-H{head}"
|
| 299 |
|
| 300 |
chips.append(
|
|
|
|
| 330 |
})
|
| 331 |
)
|
| 332 |
|
| 333 |
+
if not chips:
|
| 334 |
+
return html.Div(
|
| 335 |
+
"No heads selected yet",
|
| 336 |
+
style={'color': '#6c757d', 'fontSize': '13px', 'fontStyle': 'italic', 'padding': '8px 0'}
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
return html.Div(chips, style={
|
| 340 |
'display': 'flex',
|
| 341 |
'flexWrap': 'wrap',
|