-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollect_probe.py
More file actions
1434 lines (1201 loc) · 58.9 KB
/
collect_probe.py
File metadata and controls
1434 lines (1201 loc) · 58.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import copy
import json
import os
from typing import List, Dict, Tuple
import torch
from vllm import LLM, SamplingParams, TokensPrompt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, average_precision_score, precision_recall_fscore_support, roc_auc_score
import streamlit as st
import pandas as pd
import plotly.express as px
import numpy as np
import subprocess
import tempfile
from scipy.optimize import minimize
from transformers import AutoTokenizer
from arg_utils import add_common_arguments
from hook_utils import InterventionDirection, MODEL_NUM_LAYERS_MAP
from query_llm import MODELS
from utils import get_save_dir
from multiprocessing import Pool
def get_probe_save_dir(model: str, dataset: str, intervention_direction: str, randomize: bool = False, use_last_token_embedding: bool = False) -> str:
"""
Get the directory path for saving/loading probe data.
Args:
model: Name of the model
dataset: Name of the dataset
intervention_direction: Direction of intervention
randomize: Whether random intervention was used
use_last_token_embedding: Whether to use last token embeddings instead of probe directions
Returns:
str: Path to the probe data directory
"""
if use_last_token_embedding:
probe_save_dir = f"probe_data/{model}/{dataset}/last_token_embedding"
else:
probe_save_dir = f"probe_data/{model}/{dataset}/{intervention_direction}"
if randomize:
probe_save_dir += "_random"
return probe_save_dir
def get_intervention_dir(model: str, dataset: str, intervention_direction: str, randomize: bool = False) -> Tuple[str, InterventionDirection]:
"""
Load intervention direction from file.
Args:
model: Name of the model
dataset: Name of the dataset
intervention_direction: Direction of intervention
randomize: Whether to randomize the intervention direction
Returns:
Tuple containing:
- str: Path to the intervention direction file
- InterventionDirection: Loaded intervention direction object
"""
intervention_path = f"intervention_direction/{model}/{dataset}/{intervention_direction}_dir.pt"
intervention_dir = InterventionDirection.load(intervention_path)
if randomize:
for component in intervention_dir.components:
orig_shape = intervention_dir.components[component].mean_diff.shape
intervention_dir.components[component].mean_diff = torch.randn(orig_shape).type_as(intervention_dir.components[component].mean_diff)
return intervention_path, intervention_dir
def extract_features_labels(probe_data: List[Tuple[torch.Tensor, bool]]) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Extract features and labels from probe data.
Args:
probe_data: List of tuples containing (feature, label)
Returns:
Tuple containing:
- torch.Tensor: Stacked features
- torch.Tensor: Labels tensor
"""
features = torch.stack([x[0] for x in probe_data])
labels = torch.tensor([x[1] for x in probe_data])
return features, labels
def get_think_end_token(full_prompt: str, prompt_tokens: List[int], probe_results: Dict, think_end_token: int) -> torch.Tensor:
think_end_token_index = prompt_tokens.index(think_end_token)
features = []
for key in sorted(probe_results.keys()):
try:
features.append(probe_results[key][0][think_end_token_index].item())
except:
print(f"Error at {key}")
print(full_prompt)
print(probe_results[key])
print(think_end_token_index)
print(len(prompt_tokens))
raise Exception("Error")
features = torch.Tensor(features)
return features
def get_average(full_prompt: str, prompt_tokens: List[int], probe_results: Dict, think_end_token: int) -> torch.Tensor:
think_end_token_index = prompt_tokens.index(think_end_token)
features = []
for key in sorted(probe_results.keys()):
features.append(probe_results[key][0][:think_end_token_index].mean().item())
features = torch.Tensor(features)
return features
def get_average_think_steps(full_prompt: str, prompt_tokens: List[int], probe_results: Dict, think_end_token: int, think_delim_tokens: List[int]) -> torch.Tensor:
think_end_token_index = prompt_tokens.index(think_end_token)
think_steps_positions = [i for i, tid in enumerate(prompt_tokens[:think_end_token_index]) if tid in think_delim_tokens]
features = []
for key in sorted(probe_results.keys()):
features.append(probe_results[key][0][think_steps_positions].mean().item())
features = torch.Tensor(features)
return features
def get_last_think_step(full_prompt: str, prompt_tokens: List[int], probe_results: Dict, think_end_token: int, think_delim_tokens: List[int], K=1) -> torch.Tensor:
think_end_token_index = prompt_tokens.index(think_end_token)
think_steps_positions = [i for i, tid in enumerate(prompt_tokens[:think_end_token_index]) if tid in think_delim_tokens]
features = []
for key in sorted(probe_results.keys()):
features.append(probe_results[key][0][think_steps_positions[-K]].item())
features = torch.Tensor(features)
return features
def load_outputs(dataset: str, model: str, instruction: str, with_intervention: float = 0.0,
intervention_direction: str = "reflect", intervention_layers: str = None,
step_begin_only: bool = False, intervention_type: str = "additive",
n_samples: int = 1) -> Tuple[List[str], List[Dict]]:
"""
Load model outputs from the saved results file.
Returns:
Tuple containing:
- List of questions
- List of response dictionaries with content, reasoning, and is_correct
"""
save_dir = get_save_dir(dataset, model, instruction, with_intervention,
intervention_direction, intervention_layers, step_begin_only, intervention_type)
results_file = f"{save_dir}/results_samples{n_samples}.json"
if not os.path.exists(results_file):
raise FileNotFoundError(f"Results file not found: {results_file}")
with open(results_file, 'r') as f:
analyzed_results = json.load(f)
# Extract data from analyzed_results format
questions = analyzed_results["questions"]
sample_results = analyzed_results["sample_results"][0] # Take first sample's results
# Convert to response format
responses = []
for content, reasoning, is_correct in zip(sample_results["response_texts"],
sample_results["think_texts"],
sample_results["correctness"]):
response = {
"content": content,
"reasoning": reasoning,
"is_correct": is_correct
}
responses.append(response)
return questions, responses
def collect_raw_probe_data(
llm: LLM,
questions: List[str],
responses: List[Dict],
instruction: str,
probe: object,
sampling_params: SamplingParams,
) -> List[Tuple[Dict, List[int], str, bool]]:
"""
Collect raw probe data for each model output.
Args:
llm: The LLM instance
questions: List of input questions
responses: List of model responses
instruction: Instruction to append to questions
probe: The probe object attached to model
sampling_params: SamplingParams instance for generation
Returns:
List of tuples containing:
- probe_results: Dict of probe data
- prompt_tokens: List of token IDs
- full_prompt: Complete prompt string
- is_correct: Boolean indicating correctness
"""
raw_probe_data = []
tokenizer = llm.get_tokenizer()
for question, response in zip(questions, responses):
# Create input by combining question and response
content = response["content"]
is_correct = response["is_correct"]
reasoning = response["reasoning"]
# Format the prompt using the template
prompt = tokenizer.apply_chat_template([{"role": "user", "content": question+instruction}], tokenize=False, add_generation_prompt=True)
# Clear probe cache and generate a single token to collect probe data
probe.clear_cache()
if reasoning is not None:
full_prompt = prompt + reasoning + "\n</think>\n" + content
else:
full_prompt = prompt + content
response = llm.generate(full_prompt, sampling_params)
# Get probe results
probe_results = probe.get_cache()
for key in probe_results.keys():
if len(probe_results[key]) > 1:
probe_results[key][0] = torch.cat(probe_results[key], dim=0)
assert probe_results[key][0].shape[0] == len(response[0].prompt_token_ids), \
f"Probe results shape: {[tensor.shape for tensor in probe_results[key]]}, response shape: {len(response[0].prompt_token_ids)}"
# Store raw results
raw_probe_data.append((copy.deepcopy(probe_results), response[0].prompt_token_ids, full_prompt, is_correct))
return raw_probe_data
class LastTokenEmbeddingHook():
"""Hook to capture the last token's or last thinking token's hidden state from the final layer."""
def __init__(self, act_store, use_last_thinking_token=False, think_end_token=None, use_prompt_embedding=False):
self.act_store = act_store
self.use_last_thinking_token = use_last_thinking_token
self.think_end_token = think_end_token
self.use_prompt_embedding = use_prompt_embedding
self.current_prompt_tokens = None
self.prompt_end_position = None
def set_prompt_tokens(self, prompt_tokens):
"""Set the current prompt tokens to find think_end_token position."""
self.current_prompt_tokens = prompt_tokens
def set_prompt_end_position(self, prompt_end_position):
"""Set the position where the original prompt ends (before model response)."""
self.prompt_end_position = prompt_end_position
def __call__(self, module, input, output):
if isinstance(output, tuple):
hidden_states = output[0] # Usually the first element is hidden states
else:
hidden_states = output
# Extract token embedding: [batch_size, seq_len, hidden_dim] -> [batch_size, hidden_dim]
if len(hidden_states.shape) == 3:
if self.use_prompt_embedding and self.prompt_end_position is not None:
# Take the embedding at the end of the prompt (before model response)
token_position = min(self.prompt_end_position, hidden_states.shape[1] - 1)
token_embedding = hidden_states[:, token_position, :].cpu()
elif self.use_last_thinking_token and self.think_end_token is not None and self.current_prompt_tokens is not None:
# Find the position of the think_end_token
try:
think_end_token_index = self.current_prompt_tokens.index(self.think_end_token)
# Take the token right before </think>
token_position = max(0, think_end_token_index)
token_embedding = hidden_states[:, token_position, :].cpu()
except (ValueError, IndexError):
# Fallback to last token if think_end_token not found
token_embedding = hidden_states[:, -1, :].cpu()
else:
# Take last token (original behavior)
token_embedding = hidden_states[:, -1, :].cpu()
else:
# Handle case where batch dimension might be squeezed
token_embedding = hidden_states[-1, :].cpu() if len(hidden_states.shape) == 2 else hidden_states.cpu()
# Convert to float32 if in BFloat16 to avoid numpy conversion issues later
if token_embedding.dtype == torch.bfloat16:
token_embedding = token_embedding.float()
self.act_store.append(token_embedding)
class LastTokenEmbeddingCacher():
"""Cacher specifically for capturing last token embeddings from the final layer."""
def __init__(self, use_last_thinking_token=False, think_end_token=None, use_prompt_embedding=False):
self.cache = []
self.handle = None
self.hook = None
self.use_last_thinking_token = use_last_thinking_token
self.think_end_token = think_end_token
self.use_prompt_embedding = use_prompt_embedding
def register_model(self, model):
"""Register hook on the last layer of the model."""
# Find the last layer - typically model.layers[-1] for transformer models
if hasattr(model, 'model') and hasattr(model.model, 'layers'):
# For models like Llama/Qwen
last_layer = model.model.layers[-1]
target_module_name = f"model.layers[{len(model.model.layers)-1}]"
elif hasattr(model, 'layers'):
last_layer = model.layers[-1]
target_module_name = f"layers[{len(model.layers)-1}]"
elif hasattr(model, 'transformer') and hasattr(model.transformer, 'h'):
# For GPT-style models
last_layer = model.transformer.h[-1]
target_module_name = f"transformer.h[{len(model.transformer.h)-1}]"
else:
raise ValueError("Could not find transformer layers in the model")
# Register hook on the last layer
self.hook = LastTokenEmbeddingHook(self.cache, self.use_last_thinking_token, self.think_end_token, self.use_prompt_embedding)
self.handle = last_layer.register_forward_hook(self.hook)
if self.use_prompt_embedding:
hook_type = "prompt end"
elif self.use_last_thinking_token:
hook_type = "last thinking token"
else:
hook_type = "last token"
print(f"Registered {hook_type} embedding hook on: {target_module_name}")
def set_prompt_tokens(self, prompt_tokens):
"""Set the current prompt tokens for the hook."""
if self.hook is not None:
self.hook.set_prompt_tokens(prompt_tokens)
def set_prompt_end_position(self, prompt_end_position):
"""Set the prompt end position for the hook."""
if self.hook is not None:
self.hook.set_prompt_end_position(prompt_end_position)
def get_cache(self):
return self.cache
def clear_cache(self):
self.cache.clear()
def remove_hook(self):
if self.handle is not None:
self.handle.remove()
self.handle = None
class AllLayerEmbeddingHook():
"""Hook to capture hidden states from a specific layer."""
def __init__(self, act_store, layer_idx, use_last_thinking_token=False, think_end_token=None, use_prompt_embedding=False):
self.act_store = act_store
self.layer_idx = layer_idx
self.use_last_thinking_token = use_last_thinking_token
self.think_end_token = think_end_token
self.use_prompt_embedding = use_prompt_embedding
self.current_prompt_tokens = None
self.prompt_end_position = None
def set_prompt_tokens(self, prompt_tokens):
"""Set the current prompt tokens to find think_end_token position."""
self.current_prompt_tokens = prompt_tokens
def set_prompt_end_position(self, prompt_end_position):
"""Set the position where the original prompt ends (before model response)."""
self.prompt_end_position = prompt_end_position
def __call__(self, module, input, output):
if isinstance(output, tuple):
hidden_states = output[0] # Usually the first element is hidden states
else:
hidden_states = output
# Extract token embedding: [batch_size, seq_len, hidden_dim] -> [batch_size, hidden_dim]
if len(hidden_states.shape) == 3:
if self.use_prompt_embedding and self.prompt_end_position is not None:
# Take the embedding at the end of the prompt (before model response)
token_position = min(self.prompt_end_position, hidden_states.shape[1] - 1)
token_embedding = hidden_states[:, token_position, :].cpu()
elif self.use_last_thinking_token and self.think_end_token is not None and self.current_prompt_tokens is not None:
# Find the position of the think_end_token
try:
think_end_token_index = self.current_prompt_tokens.index(self.think_end_token)
# Take the token right before </think>
token_position = max(0, think_end_token_index)
token_embedding = hidden_states[:, token_position, :].cpu()
except (ValueError, IndexError):
# Fallback to last token if think_end_token not found
token_embedding = hidden_states[:, -1, :].cpu()
else:
# Take last token (original behavior)
token_embedding = hidden_states[:, -1, :].cpu()
else:
# Handle case where batch dimension might be squeezed
token_embedding = hidden_states[-1, :].cpu() if len(hidden_states.shape) == 2 else hidden_states.cpu()
# Convert to float32 if in BFloat16 to avoid numpy conversion issues later
if token_embedding.dtype == torch.bfloat16:
token_embedding = token_embedding.float()
self.act_store[self.layer_idx] = token_embedding
class AllLayerEmbeddingCacher():
"""Cacher specifically for capturing hidden states from all layers."""
def __init__(self, use_last_thinking_token=False, think_end_token=None, use_prompt_embedding=False):
self.cache = {}
self.handles = []
self.hooks = []
self.use_last_thinking_token = use_last_thinking_token
self.think_end_token = think_end_token
self.use_prompt_embedding = use_prompt_embedding
def register_model(self, model):
"""Register hooks on all layers of the model."""
# Find all layers - typically model.layers for transformer models
if hasattr(model, 'model') and hasattr(model.model, 'layers'):
# For models like Llama/Qwen
layers = model.model.layers
layer_prefix = "model.layers"
elif hasattr(model, 'layers'):
layers = model.layers
layer_prefix = "layers"
elif hasattr(model, 'transformer') and hasattr(model.transformer, 'h'):
# For GPT-style models
layers = model.transformer.h
layer_prefix = "transformer.h"
else:
raise ValueError("Could not find transformer layers in the model")
# Register hook on each layer
for layer_idx, layer in enumerate(layers):
hook = AllLayerEmbeddingHook(self.cache, layer_idx, self.use_last_thinking_token, self.think_end_token, self.use_prompt_embedding)
handle = layer.register_forward_hook(hook)
self.hooks.append(hook)
self.handles.append(handle)
if self.use_prompt_embedding:
hook_type = "prompt end"
elif self.use_last_thinking_token:
hook_type = "last thinking token"
else:
hook_type = "last token"
print(f"Registered {hook_type} embedding hooks on {len(layers)} layers: {layer_prefix}[0-{len(layers)-1}]")
def set_prompt_tokens(self, prompt_tokens):
"""Set the current prompt tokens for all hooks."""
for hook in self.hooks:
hook.set_prompt_tokens(prompt_tokens)
def set_prompt_end_position(self, prompt_end_position):
"""Set the prompt end position for all hooks."""
for hook in self.hooks:
hook.set_prompt_end_position(prompt_end_position)
def get_cache(self):
return self.cache
def clear_cache(self):
self.cache.clear()
def remove_hook(self):
for handle in self.handles:
if handle is not None:
handle.remove()
self.handles.clear()
self.hooks.clear()
def collect_last_token_embeddings(
llm: LLM,
questions: List[str],
responses: List[Dict],
instruction: str,
sampling_params: SamplingParams,
use_last_thinking_token: bool = False,
use_prompt_embedding: bool = False,
) -> List[Tuple[torch.Tensor, bool]]:
"""
Collect last token embeddings from the final layer for each model output.
Args:
llm: The LLM instance
questions: List of input questions
responses: List of model responses
instruction: Instruction to append to questions
sampling_params: SamplingParams instance for generation
use_last_thinking_token: Whether to use last thinking token instead of last token
use_prompt_embedding: Whether to use prompt end embedding instead of last token
Returns:
List of tuples containing:
- last_token_embedding: The final layer hidden state of the specified token
- is_correct: Boolean indicating correctness
"""
embedding_data = []
tokenizer = llm.get_tokenizer()
# Get think_end_token if using last thinking token
think_end_token = None
if use_last_thinking_token:
think_end_token = tokenizer.encode("</think>", add_special_tokens=False)[0]
# Create and register the last token embedding cacher using apply_model
cacher = LastTokenEmbeddingCacher(use_last_thinking_token, think_end_token, use_prompt_embedding)
llm.apply_model(lambda model: cacher.register_model(model))
for question, response in zip(questions, responses):
# Create input by combining question and response
content = response["content"]
reasoning = response["reasoning"]
is_correct = response["is_correct"]
# Format the prompt using the template
prompt = tokenizer.apply_chat_template([{"role": "user", "content": question+instruction}], tokenize=False, add_generation_prompt=True)
# Clear cache before each generation
cacher.clear_cache()
full_prompt = prompt + reasoning + "\n</think>\n" + content
# Set prompt tokens for the hook if using last thinking token
if use_last_thinking_token:
# Tokenize the full prompt to get token positions
prompt_tokens = tokenizer.encode(full_prompt, add_special_tokens=False)
cacher.set_prompt_tokens(prompt_tokens)
# Set prompt end position if using prompt embedding
if use_prompt_embedding:
# Calculate the position where the original prompt ends
prompt_tokens = tokenizer.encode(prompt, add_special_tokens=False)
prompt_end_position = len(prompt_tokens) - 1 # 0-indexed position
cacher.set_prompt_end_position(prompt_end_position)
# Generate to trigger the hooks and capture last token embedding
outputs = llm.generate(full_prompt, sampling_params)
# Get the captured last token embedding
cache = cacher.get_cache()
if len(cache) == 0:
raise RuntimeError("No embeddings were captured. Check if hooks are properly registered.")
# The cache contains the last token embedding from the final layer
last_token_embedding = cache[0] # Should be [hidden_dim] tensor
# Handle batch dimension if present
if len(last_token_embedding.shape) > 1:
last_token_embedding = last_token_embedding[0] # Take first in batch
# Store the embedding and correctness
embedding_data.append((last_token_embedding, is_correct))
# Clean up hooks
cacher.remove_hook()
return embedding_data
def collect_all_layers_embeddings(
llm: LLM,
questions: List[str],
responses: List[Dict],
instruction: str,
sampling_params: SamplingParams,
use_last_thinking_token: bool = False,
use_prompt_embedding: bool = False,
) -> Dict[int, List[Tuple[torch.Tensor, bool]]]:
"""
Collect embeddings from all layers for each model output.
Args:
llm: The LLM instance
questions: List of input questions
responses: List of model responses
instruction: Instruction to append to questions
sampling_params: SamplingParams instance for generation
use_last_thinking_token: Whether to use last thinking token instead of last token
use_prompt_embedding: Whether to use prompt end embedding instead of last token
Returns:
Dict mapping layer index to list of tuples containing:
- embedding: The layer's hidden state of the specified token
- is_correct: Boolean indicating correctness
"""
all_layers_data = {}
tokenizer = llm.get_tokenizer()
# Get think_end_token if using last thinking token
think_end_token = None
if use_last_thinking_token:
think_end_token = tokenizer.encode("</think>", add_special_tokens=False)[0]
# Create and register the all layers embedding cacher using apply_model
cacher = AllLayerEmbeddingCacher(use_last_thinking_token, think_end_token, use_prompt_embedding)
llm.apply_model(lambda model: cacher.register_model(model))
for question, response in zip(questions, responses):
# Create input by combining question and response
content = response["content"]
reasoning = response["reasoning"]
is_correct = response["is_correct"]
# Format the prompt using the template
prompt = tokenizer.apply_chat_template([{"role": "user", "content": question+instruction}], tokenize=False, add_generation_prompt=True)
# Clear cache before each generation
cacher.clear_cache()
full_prompt = prompt + reasoning + "\n</think>\n" + content
# Set prompt tokens for the hook if using last thinking token
if use_last_thinking_token:
# Tokenize the full prompt to get token positions
prompt_tokens = tokenizer.encode(full_prompt, add_special_tokens=False)
cacher.set_prompt_tokens(prompt_tokens)
# Set prompt end position if using prompt embedding
if use_prompt_embedding:
# Calculate the position where the original prompt ends
prompt_tokens = tokenizer.encode(prompt, add_special_tokens=False)
prompt_end_position = len(prompt_tokens) - 1 # 0-indexed position
cacher.set_prompt_end_position(prompt_end_position)
# Generate to trigger the hooks and capture embeddings from all layers
outputs = llm.generate(full_prompt, sampling_params)
# Get the captured embeddings from all layers
cache = cacher.get_cache()
if len(cache) == 0:
raise RuntimeError("No embeddings were captured. Check if hooks are properly registered.")
# Organize embeddings by layer
for layer_idx, embedding in cache.items():
# Handle batch dimension if present
if len(embedding.shape) > 1:
embedding = embedding[0] # Take first in batch
# Initialize layer data if not exists
if layer_idx not in all_layers_data:
all_layers_data[layer_idx] = []
# Store the embedding and correctness for this layer
all_layers_data[layer_idx].append((embedding, is_correct))
# Clean up hooks
cacher.remove_hook()
return all_layers_data
def process_data_point(data_point, think_end_token, think_delim_tokens, aggregation_strategy):
probe_results, prompt_tokens, full_prompt, is_correct = data_point
# Aggregate features based on strategy
if aggregation_strategy == "think_end_token":
feature = get_think_end_token(full_prompt, prompt_tokens, probe_results, think_end_token)
elif aggregation_strategy == "average":
feature = get_average(full_prompt, prompt_tokens, probe_results, think_end_token)
elif aggregation_strategy == "average_think_steps":
feature = get_average_think_steps(full_prompt, prompt_tokens, probe_results, think_end_token, think_delim_tokens)
elif aggregation_strategy == "last_think_step":
feature = get_last_think_step(full_prompt, prompt_tokens, probe_results, think_end_token, think_delim_tokens, K=1)
elif aggregation_strategy == "sec_last_think_steps":
feature = get_last_think_step(full_prompt, prompt_tokens, probe_results, think_end_token, think_delim_tokens, K=2)
return feature, is_correct
def aggregate_probe_data(
raw_probe_data: List[Tuple[Dict, List[int], str, bool]],
tokenizer,
aggregation_strategy: str
) -> List[Tuple[torch.Tensor, bool]]:
"""
Aggregate raw probe data using specified strategy.
Args:
raw_probe_data: List of tuples containing raw probe data
tokenizer: Tokenizer instance
aggregation_strategy: Strategy to use for aggregation
Returns:
List of tuples containing:
- feature: Aggregated feature tensor
- is_correct: Boolean indicating correctness
"""
think_end_token = tokenizer.encode("</think>", add_special_tokens=False)[0]
think_delim_tokens = [tid for tid in range(tokenizer.vocab_size) if "\n\n" in tokenizer.decode(tid)]
probe_stores = []
for data_point in raw_probe_data:
feature, is_correct = process_data_point(data_point, think_end_token, think_delim_tokens, aggregation_strategy)
probe_stores.append((feature, is_correct))
return probe_stores
def create_weight_visualization(clf: LogisticRegression, feature_names: List[str]):
"""
Create and launch a Streamlit visualization for model weights.
"""
weights = clf.coef_[0] # Get weights from the model
abs_weights = np.abs(weights)
# Create a DataFrame with weights and their absolute values
df = pd.DataFrame({
'Component': feature_names,
'Weight': weights,
'Absolute Weight': abs_weights
})
# Sort by absolute weight for better visualization
df = df.sort_values('Absolute Weight', ascending=True)
# Create a temporary directory without using context manager
temp_dir = tempfile.mkdtemp()
# Save the weights data
weights_path = os.path.join(temp_dir, 'weights.json')
df.to_json(weights_path, orient='records')
# Create the Streamlit app
app_path = os.path.join(temp_dir, 'app.py')
with open(app_path, 'w') as f:
f.write("""import streamlit as st
import pandas as pd
import plotly.express as px
import json
import os
import shutil
import sys
# Get the directory of the current script
current_dir = os.path.dirname(os.path.abspath(__file__))
# Load the weights data
with open(os.path.join(current_dir, 'weights.json'), 'r') as f:
data = json.load(f)
df = pd.DataFrame(data)
st.title('Linear Probe Weight Analysis')
# Add description
st.write('This visualization shows the weights of the linear probe classifier. Larger absolute weights indicate more important components for classification.')
# Create horizontal bar chart of weights
fig = px.bar(df,
x='Weight',
y='Component',
orientation='h',
title='Component Weights',
color='Weight',
color_continuous_scale='RdBu',
labels={'Weight': 'Weight Value', 'Component': 'Component Name'})
# Update layout for better visualization
fig.update_layout(
height=max(400, len(df) * 20), # Dynamic height based on number of components
xaxis_title='Weight Value',
yaxis_title='Component',
showlegend=False
)
st.plotly_chart(fig, use_container_width=True)
# Show top influential components
st.subheader('Most Influential Components')
st.write('Components with largest absolute weights:')
top_n = st.slider('Number of top components to show', min_value=5, max_value=len(df), value=10)
top_components = df.sort_values('Absolute Weight', ascending=False).head(top_n)
st.table(top_components[['Component', 'Weight', 'Absolute Weight']].round(4))
# Cleanup function
def cleanup():
try:
shutil.rmtree(current_dir)
except:
pass
# Register cleanup on session end
st.session_state.setdefault('cleanup_done', False)
if not st.session_state.cleanup_done:
st.session_state.cleanup_done = True
import atexit
atexit.register(cleanup)
""")
# Launch the Streamlit app
print("\nLaunching visualization website...")
print(f"Temporary files are stored in: {temp_dir}")
subprocess.run(['streamlit', 'run', app_path])
def train_constrained_logistic_regression(X, y, enforce_negative=False, balance_classes=False):
"""
Train logistic regression with optional constraint that all weights must be negative.
"""
n_features = X.shape[1]
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def objective(params):
if balance_classes:
positive_freq = np.mean(y)
w = params[:-1] # weights
b = params[-1] # bias
z = X @ w + b
pred = sigmoid(z)
# Log loss with L2 regularization
if balance_classes:
loss = -np.mean((1 - positive_freq) * (y * np.log(pred + 1e-15) + positive_freq * (1 - y) * np.log(1 - pred + 1e-15)))
else:
loss = -np.mean(y * np.log(pred + 1e-15) + (1 - y) * np.log(1 - pred + 1e-15))
# Add L2 regularization
loss += 0.01 * np.sum(w ** 2) # 0.01 is the regularization strength
return loss
def grad(params):
w = params[:-1]
b = params[-1]
z = X @ w + b
pred = sigmoid(z)
# Gradient of log loss
dw = X.T @ (pred - y) / len(y)
db = np.mean(pred - y)
# Add gradient of L2 regularization
dw += 0.02 * w # 0.02 is 2 * regularization strength
return np.concatenate([dw, [db]])
# Initial weights and bias
initial_params = np.zeros(n_features + 1)
if enforce_negative:
# Constraint: all weights must be negative (except bias)
constraints = []
for i in range(n_features):
constraints.append({
'type': 'ineq',
'fun': lambda x, idx=i: -x[idx], # Constraint: w[i] <= 0
'jac': lambda x, idx=i: np.array([-1 if j == idx else 0 for j in range(len(x))])
})
else:
constraints = []
# Optimize
result = minimize(
objective,
initial_params,
method='SLSQP',
jac=grad,
constraints=constraints,
options={'maxiter': 1000}
)
# Create a custom classifier object with similar interface to sklearn's
class CustomLogisticRegression:
def __init__(self, weights, bias):
self.coef_ = weights.reshape(1, -1)
self.intercept_ = np.array([bias])
def predict(self, X):
return (self.decision_function(X) > 0).astype(int)
def decision_function(self, X):
return X @ self.coef_[0] + self.intercept_[0]
# Extract weights and bias
weights = result.x[:-1]
bias = result.x[-1]
return CustomLogisticRegression(weights, bias)
def create_layer_performance_visualization(layer_results: Dict, save_dir: str):
"""
Create and launch a Streamlit visualization for layer-wise performance.
"""
# Create a DataFrame with layer performance
data = []
for layer_idx, results in layer_results.items():
data.append({
'Layer': layer_idx,
'Train Accuracy': results['train_accuracy'],
'Val Accuracy': results['val_accuracy'],
'Val F1': results['val_f1'],
'AUROC': results['au_roc'],
'AUPR': results['au_pr']
})
df = pd.DataFrame(data).sort_values('Layer')
# Create a temporary directory
temp_dir = tempfile.mkdtemp()
# Save the performance data
performance_path = os.path.join(temp_dir, 'layer_performance.json')
df.to_json(performance_path, orient='records')
# Create the Streamlit app
app_path = os.path.join(temp_dir, 'layer_performance_app.py')
with open(app_path, 'w') as f:
f.write("""import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import json
import os
import shutil
# Get the directory of the current script
current_dir = os.path.dirname(os.path.abspath(__file__))
# Load the performance data
with open(os.path.join(current_dir, 'layer_performance.json'), 'r') as f:
data = json.load(f)
df = pd.DataFrame(data)
st.title('Layer-wise Probe Performance Analysis')
# Add description
st.write('This visualization shows the performance of linear probes trained on embeddings from different layers of the model.')
# Create line plots for different metrics
metrics = ['Train Accuracy', 'Val Accuracy', 'Val F1', 'AUROC', 'AUPR']
# Allow user to select which metrics to display
selected_metrics = st.multiselect(
'Select metrics to display:',
metrics,
default=['Val Accuracy', 'Val F1', 'AUROC']
)
if selected_metrics:
# Create line plot
fig = go.Figure()
colors = px.colors.qualitative.Set1
for i, metric in enumerate(selected_metrics):
fig.add_trace(go.Scatter(
x=df['Layer'],
y=df[metric],
mode='lines+markers',
name=metric,
line=dict(color=colors[i % len(colors)], width=3),
marker=dict(size=8)
))
fig.update_layout(
title='Layer-wise Performance Comparison',
xaxis_title='Layer Index',
yaxis_title='Performance Score',
height=500,
hovermode='x unified'
)
st.plotly_chart(fig, use_container_width=True)
# Show detailed performance table
st.subheader('Detailed Performance by Layer')
st.dataframe(df.round(4), use_container_width=True)
# Find and highlight best performing layers
st.subheader('Best Performing Layers')
best_layers = {}
for metric in metrics:
best_idx = df[metric].idxmax()
best_layer = df.loc[best_idx, 'Layer']
best_score = df.loc[best_idx, metric]
best_layers[metric] = (best_layer, best_score)
for metric, (layer, score) in best_layers.items():
st.write(f"**{metric}**: Layer {layer} ({score:.4f})")
# Performance distribution
st.subheader('Performance Distribution')
metric_for_dist = st.selectbox('Select metric for distribution analysis:', metrics, index=2)
fig_hist = px.histogram(df, x=metric_for_dist, nbins=20,
title=f'Distribution of {metric_for_dist} across layers')
st.plotly_chart(fig_hist, use_container_width=True)
# Cleanup function
def cleanup():
try:
shutil.rmtree(current_dir)
except:
pass
# Register cleanup on session end
st.session_state.setdefault('cleanup_done', False)
if not st.session_state.cleanup_done:
st.session_state.cleanup_done = True
import atexit
atexit.register(cleanup)
""")
# Launch the Streamlit app
print("\nLaunching layer performance visualization...")
print(f"Temporary files are stored in: {temp_dir}")
subprocess.run(['streamlit', 'run', app_path])
def eval_all_layers_probe(args):
"""
Evaluate probe features from all layers by training a linear classifier for each layer.
"""
# Load saved all layers embedding data