-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
1799 lines (1529 loc) · 73.3 KB
/
Copy pathcli.py
File metadata and controls
1799 lines (1529 loc) · 73.3 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 json
import hashlib
import torch
import numpy as np
from pathlib import Path
from tqdm import tqdm
from transformers import BitsAndBytesConfig
# Import library and utilities
from spectral_trust import GSPDiagnosticsFramework, GSPConfig
from spectral_trust.directed_topology import DirectedTopologist
from spectral_trust.spectral import calculate_spectral_velocity
from spectral_guardrails.utils.data import load_glaive_data
from spectral_guardrails.utils.models import MODEL_REGISTRY
from spectral_guardrails.utils.metrics import compute_classification_metrics
from spectral_guardrails.probes.labeling import assign_label
from spectral_guardrails.probes.features import extract_probe_features, find_token_positions
from spectral_guardrails.utils.stats import compute_cohens_d
from spectral_guardrails.probes.mlp import HallucinationProbe, train_probe, evaluate_probe
from spectral_guardrails.probes.gbt import (
compute_layerwise_features,
train_lmm_gbt, predict_lmm,
bootstrap_ci as lmm_bootstrap_ci,
save_lmm_probe, load_lmm_probe,
feature_importances,
)
from sklearn.utils.class_weight import compute_class_weight
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve, roc_curve, auc as sk_auc
# --- Framework Utilities ---
def get_content_split_indices(samples, train_ratio=0.7, val_ratio=0.15, seed=42):
"""
Deterministically splits samples based on prompt identifiers (Template-Consistent Splitting).
This ensures all samples with the same prompt template go to the same split.
"""
# Group samples by prompt_hash (or fallback to chat content if not in JSONL)
groups = {}
for i, s in enumerate(samples):
# Prefer prompt_hash saved during extract; fallback is deterministic SHA256
# (never Python's hash(), which is randomized per-process since Python 3.3)
pid = s.get('prompt_hash') or hashlib.sha256(
s.get('ground_truth', str(i)).encode('utf-8', errors='replace')
).hexdigest()
if pid not in groups:
groups[pid] = []
groups[pid].append(i)
unique_pids = sorted(list(groups.keys()))
rng = np.random.RandomState(seed)
rng.shuffle(unique_pids)
n_unique = len(unique_pids)
itrain = int(train_ratio * n_unique)
ival = int((train_ratio + val_ratio) * n_unique)
train_pids = unique_pids[:itrain]
val_pids = unique_pids[itrain:ival]
test_pids = unique_pids[ival:]
train_idx = [i for pid in train_pids for i in groups[pid]]
val_idx = [i for pid in val_pids for i in groups[pid]]
test_idx = [i for pid in test_pids for i in groups[pid]]
return np.array(train_idx), np.array(val_idx), np.array(test_idx)
def print_summary_table(metrics: dict, title: str = "Results Summary"):
print(f"\n{'=' * 40}")
print(f"{title:^40}")
print(f"{'=' * 40}")
print(f"{'Metric':<20} | {'Value':>17}")
print(f"{'-' * 20}-|-{'-' * 17}")
for k, v in metrics.items():
if isinstance(v, float):
print(f"{k:<20} | {v:>17.4f}")
elif isinstance(v, int):
print(f"{k:<20} | {v:>17d}")
else:
s = str(v)
if len(s) > 17:
s = s[:14] + "..."
print(f"{k:<20} | {s:>17}")
print(f"{'=' * 40}\n")
def find_optimal_threshold(y_true, y_score, target="recall80"):
"""Finds the threshold that achieves a specific target metric."""
precisions, recalls, thresholds = precision_recall_curve(y_true, y_score)
if target == "recall80":
# Find highest threshold where recall >= 0.8
valid_idx = np.where(recalls >= 0.8)[0]
if len(valid_idx) > 0:
return float(thresholds[valid_idx[-1]])
elif target == "recall90":
valid_idx = np.where(recalls >= 0.9)[0]
if len(valid_idx) > 0:
return float(thresholds[valid_idx[-1]])
elif target == "precision80":
# Find lowest threshold where precision >= 0.8
valid_idx = np.where(precisions >= 0.8)[0]
if len(valid_idx) > 0:
return float(thresholds[valid_idx[0]])
elif target == "f1":
f1 = 2 * (precisions * recalls) / (precisions + recalls + 1e-8)
return float(thresholds[np.argmax(f1)])
return 0.5
def plot_evaluation(y_true, y_score, title, save_path):
"""Generates ROC and Precision-Recall plots."""
fpr, tpr, _ = roc_curve(y_true, y_score)
roc_auc = sk_auc(fpr, tpr)
prec, rec, _ = precision_recall_curve(y_true, y_score)
plt.figure(figsize=(12, 5))
# ROC Curve
plt.subplot(1, 2, 1)
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC (AUC = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title(f'ROC: {title}')
plt.legend(loc="lower right")
# PR Curve
plt.subplot(1, 2, 2)
plt.plot(rec, prec, lw=2, color='blue', label='PR Curve')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title(f'PR: {title}')
plt.legend(loc="lower left")
plt.tight_layout()
plt.savefig(save_path)
plt.close()
print(f" [plot] Saved performance graph to {save_path}")
def build_framework(model_name, output_dir, device="cuda"):
"""GoR-style framework configuration with optimized 4-bit quantization."""
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
)
config = GSPConfig(
model_name=model_name,
device=device,
output_dir=str(output_dir),
verbose=False,
model_kwargs={
"quantization_config": quantization_config,
"output_attentions": True
}
)
return config
def compute_trajectory_features(samples: list, n_layers: int, metrics: list = None):
"""
Computes trajectory features (MMT) like delta, range, slope, and AUC for metrics across layers.
Mutates samples in-place by adding keys like 'Ltrajectory_{metric}_delta'.
"""
if metrics is None:
metrics = ["fiedler_value", "smoothness_index", "spectral_entropy", "hfer", "energy"]
for s in samples:
for m in metrics:
profile = []
for layer_idx in range(n_layers):
val = s.get(f"L{layer_idx}_{m}")
if val is not None:
profile.append(val)
if len(profile) < 2:
continue
profile = np.array(profile)
# GoR Standard Trajectory Features
s[f"Ltrajectory_{m}_delta"] = profile[-1] - profile[0]
s[f"Ltrajectory_{m}_range"] = np.ptp(profile)
# Dynamic Dynamics (GoR - Jump & Flux)
diffs = np.abs(np.diff(profile))
s[f"Ltrajectory_{m}_max_jump"] = np.max(diffs) if len(diffs) > 0 else 0.0
s[f"Ltrajectory_{m}_flux"] = np.sum(diffs) if len(diffs) > 0 else 0.0
try:
# Use linear regression to find global slope
slope = np.polyfit(np.arange(len(profile)), profile, 1)[0]
s[f"Ltrajectory_{m}_slope"] = slope
except BaseException:
s[f"Ltrajectory_{m}_slope"] = 0.0
# Use np.trapezoid if available (NumPy 2.0+), else np.trapz
if hasattr(np, 'trapezoid'):
s[f"Ltrajectory_{m}_auc"] = np.trapezoid(profile)
else:
s[f"Ltrajectory_{m}_auc"] = np.trapz(profile)
_SPECTRAL_METRICS = ["fiedler_value", "smoothness_index", "spectral_entropy", "energy", "hfer"]
def compute_rich_spectral_features(raw_samples: list) -> np.ndarray:
"""
Rich spectral feature matrix for spectral_rich probe mode.
For each sample, stacks:
- Trajectory stats (15 per metric × 5 metrics = 75 dim): mean, std, slope,
early-late delta, min-step, max-step, step-std, AUC, range, skew,
kurtosis, mid-mean, early/late ratio, inflection count, argmin position.
- Segmental features (8 per metric × 5 metrics = 40 dim): mean and std of
4 equal temporal segments.
- FFT magnitude (norm. by L, half-spectrum, per metric) concatenated.
Total dimension is 115 + L//2+1 per metric (architecture-dependent).
Returns (N, D) float32 array, NaN-safe.
"""
from scipy import stats as sp_stats
N = len(raw_samples)
if N == 0:
return np.empty((0, 0), dtype=np.float32)
ld0 = raw_samples[0].get('layer_diagnostics', [])
L = len(ld0)
if L == 0:
return np.empty((N, 0), dtype=np.float32)
# Build (N, L, M) tensor
M = len(_SPECTRAL_METRICS)
T = np.zeros((N, L, M), dtype=np.float64)
for i, s in enumerate(raw_samples):
for li, layer in enumerate(s.get('layer_diagnostics', [])):
for mi, m in enumerate(_SPECTRAL_METRICS):
T[i, li, mi] = float(layer.get(m, 0.0) or 0.0)
feats = []
for mi in range(M):
traj = T[:, :, mi] # (N, L)
# ── 15 trajectory statistics ──────────────────────────────────────────
x = np.arange(L, dtype=float)
q = max(1, L // 4)
feats.append(traj.mean(axis=1))
feats.append(traj.std(axis=1))
feats.append(np.array([np.polyfit(x, t, 1)[0] for t in traj]))
feats.append(traj[:, -q:].mean(axis=1) - traj[:, :q].mean(axis=1))
diffs = np.diff(traj, axis=1) if L > 1 else np.zeros((N, 1))
feats.append(diffs.min(axis=1))
feats.append(diffs.max(axis=1))
feats.append(diffs.std(axis=1))
auc_trap = np.trapz(
traj,
axis=1) if not hasattr(
np,
'trapezoid') else np.trapezoid(
traj,
axis=1)
feats.append(auc_trap / max(L, 1))
feats.append(traj.max(axis=1) - traj.min(axis=1))
feats.append(sp_stats.skew(traj, axis=1))
feats.append(sp_stats.kurtosis(traj, axis=1))
m0, m1 = L // 3, max(L // 3 + 1, 2 * L // 3)
feats.append(traj[:, m0:m1].mean(axis=1))
early_m = traj[:, :q].mean(axis=1)
late_m = traj[:, -q:].mean(axis=1)
feats.append(np.where(np.abs(early_m) > 1e-9, late_m / (early_m + 1e-9), 0.0))
if L > 2:
d2 = np.diff(traj, n=2, axis=1)
feats.append((np.diff(np.sign(d2), axis=1) != 0).sum(axis=1).astype(float))
else:
feats.append(np.zeros(N))
feats.append(traj.argmin(axis=1).astype(float) / max(L - 1, 1))
# ── 8 segmental features (4 segments × mean+std) ─────────────────────
segs = np.array_split(np.arange(L), 4)
for seg in segs:
sl = traj[:, seg]
feats.append(sl.mean(axis=1))
feats.append(sl.std(axis=1))
# ── FFT magnitude (half-spectrum, normalised) ─────────────────────────
fft_mag = np.abs(np.fft.rfft(traj, axis=1)) / max(L, 1)
for fi in range(fft_mag.shape[1]):
feats.append(fft_mag[:, fi])
X = np.column_stack(feats).astype(np.float32)
X = np.nan_to_num(X, nan=0.0, posinf=0.0, neginf=0.0)
return X
def flatten_sample(s: dict) -> dict:
"""
Normalise a JSONL record to a flat dict.
Supports both legacy 'spectral' and GoR-style 'layer_diagnostics' keys.
"""
flat = {k: v for k, v in s.items() if k not in ('spectral', 'hidden', 'layer_diagnostics')}
# Case 1: layer_diagnostics (list of metrics per layer, GoR Style)
if 'layer_diagnostics' in s:
for li, metrics in enumerate(s['layer_diagnostics']):
for metric_name, value in metrics.items():
flat[f"L{li}_{metric_name}"] = value
# Case 2: spectral (dict of layer indices, Legacy Style)
for layer_str, metrics in s.get('spectral', {}).items():
for metric_name, value in metrics.items():
flat[f"L{layer_str}_{metric_name}"] = value
# Nested hidden
for layer_str, vec in s.get('hidden', {}).items():
flat[f"L{layer_str}_hidden"] = vec
return flat
def generate_layer_profile_plots(samples, y_true, model, domain, output_dir, best_layer=None):
import matplotlib.pyplot as plt
plt.switch_backend('Agg')
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
metrics = ["fiedler_value", "smoothness_index", "spectral_entropy", "hfer", "energy"]
y_true = np.array(y_true)
max_layer = -1
for k in samples[0].keys():
if k.startswith("L") and "_" in k:
try:
parts = k.split("_")[0]
if parts[1:].isdigit():
max_layer = max(max_layer, int(parts[1:]))
except BaseException:
continue
n_layers = max_layer + 1
fig, axes = plt.subplots(2, 3, figsize=(18, 10))
fig.suptitle(f"{model} / {domain} — Spectral Layer Profiles", fontsize=16)
for ax, m in zip(axes.flatten()[:5], metrics):
data = np.full((len(samples), n_layers), np.nan)
for i, s in enumerate(samples):
for layer_idx in range(n_layers):
data[i, layer_idx] = s.get(f"L{layer_idx}_{m}", np.nan)
layers = np.arange(n_layers)
for i in range(len(samples)):
color = 'blue' if y_true[i] == 0 else 'red'
ax.plot(layers, data[i], color=color, alpha=0.15, linewidth=0.5)
valid_data = data[y_true == 0]
halluc_data = data[y_true == 1]
if len(valid_data) > 0:
v_mean = np.nanmean(valid_data, axis=0)
v_std = np.nanstd(valid_data, axis=0)
ax.plot(layers, v_mean, color='blue', linewidth=3, label='Valid Mean')
ax.fill_between(layers, v_mean - v_std, v_mean + v_std, color='blue', alpha=0.1)
if len(halluc_data) > 0:
h_mean = np.nanmean(halluc_data, axis=0)
h_std = np.nanstd(halluc_data, axis=0)
ax.plot(layers, h_mean, color='red', linewidth=3, label='Halluc Mean')
ax.fill_between(layers, h_mean - h_std, h_mean + h_std, color='red', alpha=0.1)
if best_layer is not None:
ax.axvline(
x=best_layer,
color='black',
linestyle='--',
alpha=0.6,
label=f'Best (L{best_layer})')
ax.set_title(f"{m.replace('_', ' ').title()}")
ax.set_xlabel("Layer Index")
ax.set_ylabel("Value")
ax.grid(alpha=0.3)
ax.legend(prop={'size': 8})
axes[1, 2].axis('off')
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plot_path_base = output_dir / f"layer_profile_{model}_{domain}"
plt.savefig(f"{plot_path_base}.png", dpi=300)
plt.close()
print(f"Generated layer profile plots -> {plot_path_base}.png")
def handle_prepare(args):
data = load_glaive_data(domain=args.domain, limit=args.n_samples)
output_path = Path(args.output_dir) / f"glaive_{args.domain}.jsonl"
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, 'w') as f:
for ex in data:
f.write(json.dumps(ex) + "\n")
print(f"Prepared {len(data)} samples for domain '{args.domain}' -> {output_path}")
def handle_extract(args):
model_id = MODEL_REGISTRY.get(args.model, args.model)
output_dir = Path(args.output_dir)
config = build_framework(model_id, output_dir, device=args.device)
input_file = Path(args.data_dir) / f"glaive_{args.domain}.jsonl"
if not input_file.exists():
input_file = output_dir / f"glaive_{args.domain}.jsonl"
# We proceed even if input_file doesn't exist, because load_glaive_data() fetches from source
samples = []
if input_file.exists():
with open(input_file, 'r', encoding='utf-8') as f:
samples = [json.loads(line) for line in f]
if args.n_samples and samples:
samples = samples[:args.n_samples]
output_path = output_dir / f"spectral_features_{args.model}_{args.domain}.jsonl"
output_path.parent.mkdir(parents=True, exist_ok=True)
# Increase diversity by striding through dataset (step=50)
import hashlib
dataset = load_glaive_data(domain=args.domain, limit=args.n_samples, step=50)
print(f"[extract] Running diverse extraction for {len(dataset)} samples (stride=50)...")
records = []
with GSPDiagnosticsFramework(config) as framework:
framework.instrumenter.load_model(model_id)
tokenizer = framework.instrumenter.tokenizer
model = framework.instrumenter.model
pbar = tqdm(dataset, desc="GSP Extraction", unit="sample")
for i, ex in enumerate(pbar):
# 1. Recover chat history
from spectral_guardrails.utils.data import parse_glaive_chat
chat_raw = ex.get('chat', '')
messages = parse_glaive_chat(chat_raw)
target_idx = -1
for idx, msg in enumerate(messages):
if msg['role'] == 'assistant':
target_idx = idx
break
if target_idx == -1:
continue
ground_truth = messages[target_idx]['content']
prompt_msgs = [{"role": "system", "content": ex.get(
'system', "You are a helpful assistant.")}]
if target_idx > 0:
prompt_msgs.append(messages[target_idx - 1])
prompt_text = tokenizer.apply_chat_template(
prompt_msgs, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device)
# 2. Generate
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=64,
temperature=args.temperature,
do_sample=args.temperature > 0,
pad_token_id=tokenizer.eos_token_id)
prediction = tokenizer.decode(
outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
label = assign_label(prediction, ground_truth)
full_text = prompt_text + prediction
# 3. Analyze (Spectral pass)
try:
analysis = framework.analyze_text(full_text, save_results=False)
layer_diagnostics = []
for li, diag in enumerate(analysis['layer_diagnostics']):
layer_diagnostics.append({
"fiedler_value": float(diag.fiedler_value),
"smoothness_index": float(diag.smoothness_index),
"spectral_entropy": float(diag.spectral_entropy),
"energy": float(diag.energy),
"hfer": float(diag.hfer)
})
# 4. Native Hidden Pass (Replaces monkeypatch capture)
probe_hidden = {}
with torch.no_grad():
# Explicit pass to capture hidden states correctly for the probe
# Using full_text (prompt + prediction)
full_inputs_eval = tokenizer(full_text, return_tensors="pt").to(model.device)
m_outputs = model(**full_inputs_eval, output_hidden_states=True)
hidden_states = [h.to(torch.float32).cpu() for h in m_outputs.hidden_states]
target_layers = [4, 8, 11, 15, 20, 24, 28, 31]
pos = find_token_positions(
tokenizer, full_inputs_eval.input_ids[0].tolist(), prediction)
for li in target_layers:
if li < len(hidden_states):
signals = hidden_states[li][0]
h_feat = extract_probe_features(signals, pos)
probe_hidden[str(li)] = h_feat.tolist()
prompt_hash = hashlib.sha256(prompt_text.encode('utf-8')).hexdigest()
result = {
"original_idx": ex.get('original_idx', i),
"prompt_hash": prompt_hash,
"prompt": prompt_text,
"label": int(label),
"prediction": prediction,
"ground_truth": ground_truth,
"layer_diagnostics": layer_diagnostics,
"hidden": probe_hidden,
"seq_len": len(tokenizer(full_text)['input_ids'])
}
records.append(result)
pbar.set_postfix(label="HALL" if label else "ok")
except Exception as e:
pbar.write(f"[warn] Framework failed on sample {i}: {e}")
continue
with open(output_path, 'w', encoding='utf-8') as out_f:
for record in records:
out_f.write(json.dumps(record) + "\n")
print(f"\n[DONE] Saved {len(records)} features to {output_path}")
def handle_train_probe(args):
input_file = Path(args.data_dir) / f"spectral_features_{args.model}_{args.domain}.jsonl"
if not input_file.exists():
print(f"Error: {input_file} not found.")
return
with open(input_file, 'r', encoding='utf-8') as f:
raw_samples = [json.loads(line) for line in f]
samples = [flatten_sample(s) for s in raw_samples]
y = np.array([s['label'] for s in samples])
train_idx, val_idx, test_idx = get_content_split_indices(samples)
print(
f"[train-probe] Split: {len(train_idx)} Train, "
f"{len(val_idx)} Val, {len(test_idx)} Test (Template-Consistent)")
if args.feature_type == "spectral_rich":
import pickle
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.metrics import roc_auc_score
X = compute_rich_spectral_features(raw_samples)
print(f"[spectral_rich] Feature matrix: {X.shape}")
best_C, best_val_auc = 1.0, 0.0
for C in [0.001, 0.01, 0.1, 1.0, 10.0]:
try:
pipe = Pipeline([('sc', StandardScaler()), ('lr', LogisticRegression(
C=C, max_iter=1000, class_weight='balanced'))])
pipe.fit(X[train_idx], y[train_idx])
val_auc = roc_auc_score(y[val_idx], pipe.predict_proba(X[val_idx])[:, 1])
if val_auc > best_val_auc:
best_val_auc, best_C = val_auc, C
except Exception:
pass
pipe = Pipeline([('sc', StandardScaler()), ('lr', LogisticRegression(
C=best_C, max_iter=1000, class_weight='balanced'))])
trainval_idx = list(train_idx) + list(val_idx)
pipe.fit(X[trainval_idx], y[trainval_idx])
test_scores = pipe.predict_proba(X[test_idx])[:, 1]
test_auc = roc_auc_score(y[test_idx], test_scores)
print(
f"[spectral_rich] Best C={best_C} Val AUC={best_val_auc:.4f} Test AUC={test_auc:.4f}")
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
probe_name = f"probe_{args.model}_spectral_rich.pkl"
with open(output_dir / probe_name, 'wb') as pf:
pickle.dump(pipe, pf)
print(f"[spectral_rich] Saved to {output_dir / probe_name}")
return
if args.feature_type == "lmm_gbt":
from sklearn.metrics import roc_auc_score as _roc
X_lmm = compute_layerwise_features(raw_samples)
n_features = X_lmm.shape[1]
n_layers = n_features // 5
print(f"[lmm_gbt] Feature matrix: {X_lmm.shape} (layers={n_layers}, metrics=5)")
model, scaler, method = train_lmm_gbt(X_lmm, y, train_idx, val_idx)
print(f"[lmm_gbt] Selected method: {method}")
test_scores = predict_lmm(model, scaler, X_lmm[test_idx])
test_auc = _roc(y[test_idx], test_scores)
lo, hi = lmm_bootstrap_ci(y[test_idx], test_scores)
print(f"[lmm_gbt] Test AUC: {test_auc:.4f} 95% CI [{lo:.4f}, {hi:.4f}]")
imp = feature_importances(model, n_layers)
if imp:
top = sorted(
[(layer, m, v) for layer, ms in imp.items() for m, v in ms.items()],
key=lambda x: -x[2],
)[:10]
print("[lmm_gbt] Top 10 (layer, metric, importance):")
for layer, m, v in top:
print(f" L{layer:02d} {m:<20s} {v:.4f}")
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
probe_name = f"probe_{args.model}_lmm_gbt.pkl"
save_lmm_probe(model, scaler, method, output_dir / probe_name)
print(f"[lmm_gbt] Saved to {output_dir / probe_name}")
return
X, y_list = [], []
for ex in samples:
features = []
if args.feature_type in ["hidden", "combined"]:
hidden_keys = sorted([k for k in samples[0].keys() if k.endswith("_hidden")])
for k in hidden_keys:
features.extend(ex.get(k, []))
if args.feature_type in ["spectral", "combined"]:
_spectral_keys = sorted(
[
k for k in samples[0].keys() if any(
k.endswith(
f"_{m}") for m in [
"hfer",
"fiedler_value",
"smoothness_index",
"spectral_entropy",
"energy"]) and k.startswith("L") and not k.endswith("_hidden")])
features.extend([ex.get(k, 0.0) for k in _spectral_keys])
X.append(features)
y_list.append(ex.get('label', 0))
X, y = np.array(X), np.array(y_list)
X_train, X_val, X_test = X[train_idx], X[val_idx], X[test_idx]
y_train, y_val, y_test = y[train_idx], y[val_idx], y[test_idx]
y_train_np = np.array(y_train)
weights = compute_class_weight('balanced', classes=np.array([0, 1]), y=y_train_np)
pos_weight = torch.tensor(weights[1] / weights[0], dtype=torch.float32)
probe = HallucinationProbe(input_dim=X_train.shape[1])
trained_probe, _ = train_probe(probe, X_train, y_train, X_val, y_val,
epochs=args.epochs, patience=args.patience,
pos_weight=pos_weight)
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
probe_name = f"probe_{args.model}_{args.feature_type}.pt"
torch.save({'state_dict': trained_probe.state_dict(),
'input_dim': X_train.shape[1]}, output_dir / probe_name)
metrics = evaluate_probe(trained_probe, X_test, y_test)
print_summary_table({k: v for k, v in metrics.items() if k not in (
'y_true', 'y_score')}, f"Probe Results ({args.model}) - TEST SPLIT")
def handle_evaluate(args):
input_file = Path(args.data_dir) / f"spectral_features_{args.model}_{args.domain}.jsonl"
if not input_file.exists():
print(f"Error: {input_file} not found.")
return
with open(input_file, 'r', encoding='utf-8') as f:
raw_samples = [json.loads(line) for line in f]
samples = [flatten_sample(s) for s in raw_samples]
# Template-Consistent split enforcement
_, _, test_idx = get_content_split_indices(samples)
raw_samples_test = [raw_samples[i] for i in test_idx]
samples = [samples[i] for i in test_idx]
y_true = np.array([s['label'] for s in samples])
print(
f"[evaluate] Strictly evaluating on TEST split "
f"({len(samples)} samples, template-consistent)")
# Store scores for hybrid mode
spectral_scores = None
probe_scores = None
if args.mode in ["spectral", "all", "hybrid"]:
best_params_path = Path("data/categories_sweeps/best_params.json")
if best_params_path.exists():
with open(best_params_path, 'r') as f:
best_params = json.load(f).get(f"{args.model}_{args.domain}")
if best_params:
key = f"L{best_params['layer']}_{best_params['metric']}"
if "fiedler" in key and "fiedler_value" not in key:
key = key.replace("fiedler", "fiedler_value")
# Ensure trajectory features are computed for evaluation
max_layer = -1
for k in samples[0].keys():
if k.startswith("L") and "_" in k:
try:
parts = k.split("_")[0]
if parts[1:].isdigit():
max_layer = max(max_layer, int(parts[1:]))
except BaseException:
continue
compute_trajectory_features(samples, max_layer + 1)
scores = np.array([s.get(key, np.nan) for s in samples], dtype=float)
valid = ~np.isnan(scores)
y_s, y_t = scores[valid], y_true[valid]
from sklearn.metrics import roc_auc_score
auc = roc_auc_score(y_t, y_s)
if auc < 0.5:
auc = 1 - auc
scores = -scores
# Scale spectral scores to [0,1] for hybrid
s_min, s_max = scores[valid].min(), scores[valid].max()
spectral_scores = (scores - s_min) / (s_max - s_min) if s_max > s_min else scores
d = compute_cohens_d(y_s[y_t == 0], y_s[y_t == 1])
print(f"--- SPECTRAL EVAL ({key}) ---\n AUC: {auc:.4f}\n Cohen's d: {d:.4f}\n")
if args.mode in ["probe", "all", "hybrid"]:
if args.feature_type == "lmm_gbt":
from sklearn.metrics import roc_auc_score as _roc
probe_name = f"probe_{args.model}_lmm_gbt.pkl"
probe_path = Path(args.output_dir) / probe_name
if not probe_path.exists():
print(
f"[ERROR] {probe_path} not found — run train-probe --feature-type lmm_gbt first"
)
else:
model, scaler, method = load_lmm_probe(probe_path)
X_lmm = compute_layerwise_features(raw_samples_test)
probe_scores = predict_lmm(model, scaler, X_lmm)
test_auc = _roc(y_true, probe_scores)
lo, hi = lmm_bootstrap_ci(y_true, probe_scores)
print(
f"--- PROBE EVAL ({method}) ---\n"
f" AUC: {test_auc:.4f} 95% CI [{lo:.4f}, {hi:.4f}]"
f" features={X_lmm.shape[1]}\n"
)
n_layers = X_lmm.shape[1] // 5
imp = feature_importances(model, n_layers)
if imp:
top = sorted(
[(layer, m, v) for layer, ms in imp.items() for m, v in ms.items()],
key=lambda x: -x[2],
)[:5]
print(" Top features:", [(f"L{layer} {m}", f"{v:.3f}") for layer, m, v in top])
elif args.feature_type == "spectral_rich":
import pickle
from sklearn.metrics import roc_auc_score as sk_roc_auc
probe_name = f"probe_{args.model}_spectral_rich.pkl"
probe_path = Path(args.output_dir) / probe_name
if not probe_path.exists():
print(
f"[ERROR] {probe_path} not found"
" — run train-probe --feature-type spectral_rich first")
else:
with open(probe_path, 'rb') as pf:
pipe = pickle.load(pf)
X_rich = compute_rich_spectral_features(raw_samples_test)
probe_scores = pipe.predict_proba(X_rich)[:, 1]
test_auc = sk_roc_auc(y_true, probe_scores)
print(
"--- PROBE EVAL (spectral_rich) ---\n"
f" AUC: {test_auc:.4f} dim={X_rich.shape[1]}\n")
else:
probe_name = f"probe_{args.model}_{args.feature_type}.pt"
probe_path = Path(args.output_dir) / probe_name
if probe_path.exists():
ckpt = torch.load(probe_path, map_location='cpu', weights_only=False)
# Reconstruct feature vector based on type (same logic as handle_train_probe)
X = []
for ex in samples:
features = []
if args.feature_type in ["hidden", "combined"]:
hidden_keys = sorted(
[k for k in samples[0].keys() if k.endswith("_hidden")])
for k in hidden_keys:
features.extend(ex.get(k, []))
if args.feature_type in ["spectral", "combined"]:
_metrics = ["hfer", "fiedler_value", "smoothness_index",
"spectral_entropy", "energy"]
spec_keys = sorted([
k for k in samples[0].keys()
if any(k.endswith(f"_{m}") for m in _metrics)
and k.startswith("L")
and not k.endswith("_hidden")])
features.extend([ex.get(k, 0.0) for k in spec_keys])
X.append(features)
X = np.array(X)
# Verify checkpoint dimension matches current data
if 'state_dict' in ckpt:
saved_dim = ckpt['state_dict']['net.0.weight'].shape[1]
else:
saved_dim = ckpt['net.0.weight'].shape[1]
expected_dim = X.shape[1]
if saved_dim != expected_dim:
print(f"[ERROR] Probe dim mismatch: saved={saved_dim} expected={expected_dim}")
print(f"[ERROR] Delete {probe_path} and re-run train-probe")
return
probe = HallucinationProbe(input_dim=X.shape[1])
probe.load_state_dict(ckpt['state_dict'] if 'state_dict' in ckpt else ckpt)
metrics = evaluate_probe(probe, X, y_true)
probe_scores = np.array(metrics['y_score'])
print(
"--- PROBE EVAL ---\n"
f" Accuracy: {metrics['accuracy']:.4f}\n"
f" AUC: {metrics['auc']:.4f}\n")
if args.mode == "hybrid" and spectral_scores is not None and probe_scores is not None:
# 1. Recalculate raw spectral scores
# IMPORTANT: Use the original samples and key. Re-extract to avoid previous mutations.
raw_scores_orig = np.array([s.get(key, np.nan) for s in samples], dtype=float)
# Determine the correct sign for AUC >= 0.5 using valid samples
valid_mask = ~np.isnan(raw_scores_orig)
ys_v = raw_scores_orig[valid_mask]
yt_v = y_true[valid_mask]
temp_auc = roc_auc_score(yt_v, ys_v)
sign = 1.0
if temp_auc < 0.5:
sign = -1.0
temp_auc = 1 - temp_auc
# 2. Compute stats on sign-corrected valid samples
raw_scores_corrected = sign * raw_scores_orig
spec_valid = raw_scores_corrected[valid_mask]
spec_std = spec_valid.std() if spec_valid.std() > 0 else 1.0
# 3. Get spectral threshold for the sign-corrected metric
spec_thresh = find_optimal_threshold(
yt_v, spec_valid, target=args.optimize_for or "recall80")
# 4. Normalize: (score - thresh) / std
spectral_norm = np.zeros_like(raw_scores_corrected)
spectral_norm[valid_mask] = (raw_scores_corrected[valid_mask] - spec_thresh) / spec_std
# 5. Hybrid Parameter Search (Maximize Joint AUC)
print("--- HYBRID PARAMETER SEARCH ---")
best_h_auc = -1
best_alpha = 0.5
best_beta = 0.0
# Grid for mixing weight alpha (spec vs probe). Include 0 and 1 for true maximization.
alphas = np.linspace(0.0, 1.0, 21)
# Grid for offset beta (sweep across quantiles of spectral scores)
betas = np.quantile(spec_valid, np.linspace(0.0, 1.0, 11))
from sklearn.metrics import roc_auc_score
for a in alphas:
for b_off in betas:
# Re-normalize with candidate beta
s_norm = np.zeros_like(raw_scores_corrected)
s_norm[valid_mask] = (raw_scores_corrected[valid_mask] - b_off) / spec_std
h_scores = a * s_norm + (1 - a) * probe_scores
try:
curr_auc = roc_auc_score(y_true, h_scores)
if curr_auc > best_h_auc:
best_h_auc = curr_auc
best_alpha = a
best_beta = b_off
except BaseException:
continue
# Final best scores
s_norm_best = np.zeros_like(raw_scores_corrected)
s_norm_best[valid_mask] = (raw_scores_corrected[valid_mask] - best_beta) / spec_std
hybrid_scores = best_alpha * s_norm_best + (1 - best_alpha) * probe_scores
print(f" Best Alpha (mixing): {best_alpha:.2f}")
print(f" Best Beta (offset): {best_beta:.4f}")
print(f" Joint AUC: {best_h_auc:.4f}")
if args.optimize_for:
h_thresh = find_optimal_threshold(y_true, hybrid_scores, target=args.optimize_for)
metrics = compute_classification_metrics(y_true, hybrid_scores, threshold=h_thresh)
print(
f" Optimized ({args.optimize_for}): thresh={h_thresh:.4f}, "
f"precision={metrics['precision']:.4f}, recall={metrics['recall']:.4f}")
if args.plots:
plot_path = Path(args.output_dir) / f"eval_hybrid_{args.model}_{args.domain}.png"
plot_evaluation(y_true, hybrid_scores, f"Hybrid ({args.model})", plot_path)
print("")
def handle_sweep(args):
input_file = Path(args.data_dir) / f"spectral_features_{args.model}_{args.domain}.jsonl"
if not input_file.exists():
print(f"Error: {input_file} not found.")
return
with open(input_file, 'r', encoding='utf-8') as f:
raw_samples = [json.loads(line) for line in f]
samples = [flatten_sample(s) for s in raw_samples]
y_true = np.array([s['label'] for s in samples])
max_layer = -1
for k in samples[0].keys():
if k.startswith("L") and "_" in k:
try:
parts = k.split("_")[0]
if parts[1:].isdigit():
max_layer = max(max_layer, int(parts[1:]))
except BaseException:
continue
n_layers = max_layer + 1
compute_trajectory_features(samples, n_layers)
metrics = ["fiedler_value", "smoothness_index", "spectral_entropy", "hfer", "energy"]
best_overall = {"auc": -1}
results = []
search_space = []
for layer_idx in range(n_layers):
for m in metrics:
search_space.append((layer_idx, m, f"L{layer_idx}_{m}"))
for m in metrics:
for tm in ["delta", "range", "slope", "auc", "max_jump", "flux"]:
search_space.append(("trajectory", f"{m}_{tm}", f"Ltrajectory_{m}_{tm}"))
from sklearn.metrics import roc_auc_score
for lid, mname, key in search_space:
vals = np.array([s.get(key, np.nan) for s in samples], dtype=float)
vmask = ~np.isnan(vals)
if vmask.sum() < len(samples) * 0.5:
continue
ys, yt = vals[vmask], y_true[vmask]
try:
auc = roc_auc_score(yt, ys)
if auc < 0.5:
auc = 1 - auc
res = {"layer": lid, "metric": mname, "auc": auc}
results.append(res)
if auc > best_overall["auc"]:
best_overall = res
except BaseException:
continue
print(f"Best: L{best_overall['layer']} {best_overall['metric']} AUC={best_overall['auc']:.4f}")
best_params_path = Path("data/categories_sweeps/best_params.json")
best_params_path.parent.mkdir(parents=True, exist_ok=True)
best_data = {}
if best_params_path.exists():
with open(best_params_path, 'r') as f:
best_data = json.load(f)
best_data[f"{args.model}_{args.domain}"] = best_overall
with open(best_params_path, 'w') as f:
json.dump(best_data, f, indent=2)
def handle_fusion(args):
"""
Trains a non-linear meta-classifier (Random Forest) on the Validation split
and evaluates on the Test split.
"""
input_file = Path(args.data_dir) / f"spectral_features_{args.model}_{args.domain}.jsonl"
if not input_file.exists():
print(f"Error: {input_file} not found.")
return
with open(input_file, 'r', encoding='utf-8') as f:
raw_samples = [json.loads(line) for line in f]
samples = [flatten_sample(s) for s in raw_samples]
train_idx, val_idx, test_idx = get_content_split_indices(samples)
# 1. Get Probe Scores (probabilities)
probe_name = f"probe_{args.model}_hidden.pt"
probe_path = Path(args.output_dir) / probe_name