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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import argparse
import json
import time
from typing import Optional, List, Dict
import torch.autograd as autograd
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from skdim.id import TwoNN
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
from tqdm import tqdm
from LID import build_parser,LID
from src.metrics import roc
from src.utils import last_token_stack,get_least_used_gpu,MODEL2LAYER
import copy
import matplotlib.pyplot as plt
# import wandb
# Known transformer layer counts for supported models


def build_saplma_parser() -> argparse.ArgumentParser:
    parser=build_parser()
    # parser.add_argument("--result_name", type=str, default="saplma_results.json", help="Name of the results file.")
    parser.add_argument(
        "--epochs",
        type=int,
        default=15,
        help="Number of training epochs."
    )
    parser.add_argument(
        "--weight_decay",
        type=float,
        default=1e-4,
        help="Weight decay for optimizer."
    )
    parser.add_argument(
        "--learning_rate",
        type=float,
        default=1e-2,
        help="Learning rate for optimizer."
    )
    parser.add_argument("-b","--batch_size",type=int,default=2048)
    return parser


class _LogReg(nn.Module):
    def __init__(self, dim: int):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(dim, 512),
            nn.ReLU(),
            nn.Linear(512, 256),
            nn.ReLU(),
            nn.Linear(256, 128),
            nn.ReLU(),
            nn.Linear(128, 64),
            nn.ReLU(),
            nn.Linear(64, 2),
        )
        self.num_layers = len(self.net)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = x.to(self.net[0].weight.dtype)
        return self.net(x)




class SAPLMA(LID):
    """
    Implementation of the method from "The Internal State of an LLM Knows When It's Lying"
    (Azaria & Mitchell, 2023): train a lightweight classifier on hidden layer
    activations to predict truthfulness/hallucination labels.

    This class consumes activations saved by xp_prj1/get_activation.py, using
    predicted_activations as features and labels as binary targets.
    """

    def __init__(
        self,
        args: Optional[argparse.Namespace] = None,
    ) -> None:
        super().__init__(args)

        self.layer_num=MODEL2LAYER[args.model]
        self.epochs = args.epochs
        self.lr = args.learning_rate
        self.weight_decay = args.weight_decay

    def run_saplma(self):
        """
        Trains a small classifier on activations for each layer, evaluates test AUROC,
        and saves per-layer metrics with timing info to CSV and a plot.
        """
        device = torch.device({
            'cuda': f'cuda:{get_least_used_gpu()}',
            'cpu': 'cpu'
        }['cuda' if torch.cuda.is_available() else 'cpu'])

        metrics_rows = []  # one row per layer: layer, auroc_saplma, best_val_loss, rgn, timing
        hidden_dim1 = None
        total_training_process_time_sec = 0.0
        total_best_val_loss_compute_time_sec = 0.0
        total_rgn_compute_time_sec = 0.0
        total_snr_compute_time_sec = 0.0

        for layer in tqdm(range(self.layer_num), desc="saplma for each layer"):
            train_acts, train_labels, test_acts, test_labels = self._load_layer_data(layer)

            # ========= prepare train / val / test data =========
            train_X_all = train_acts.to(device=device, dtype=torch.float32)   # [N_train, D]
            train_y_all = train_labels.to(device=device, dtype=torch.long)    # [N_train]

            test_X = test_acts.to(device=device, dtype=torch.float32)         # [N_test, D]
            test_y = test_labels.cpu().numpy()                                # [N_test]

            # ---- split train / val ----
            val_ratio = 0.1
            n_train_total = train_X_all.size(0)
            n_val = max(1, int(n_train_total * val_ratio))

            perm = torch.randperm(n_train_total, device=device)
            val_idx_local   = perm[:n_val]
            train_idx_local = perm[n_val:]

            train_X = train_X_all[train_idx_local]
            train_y = train_y_all[train_idx_local]
            val_X   = train_X_all[val_idx_local]
            val_y   = train_y_all[val_idx_local]

            # ========= define model, loss, optimizer =========
            model = _LogReg(train_X.shape[1]).to(device)
            if hidden_dim1 is None:
                hidden_dim1 = model.net[0].out_features

            criterion = nn.CrossEntropyLoss()
            optimizer = optim.Adam(
                model.parameters(),
                lr=self.lr,
                weight_decay=self.weight_decay,
            )
            batch_size = self.args.batch_size

            train_loader = DataLoader(
                TensorDataset(train_X, train_y),
                batch_size=batch_size,
                shuffle=True,
            )
            val_loader = DataLoader(
                TensorDataset(val_X, val_y),
                batch_size=batch_size,
                shuffle=False,
            )

            scheduler = torch.optim.lr_scheduler.StepLR(
                optimizer, step_size=5, gamma=0.1
            )

            best_val_loss = float("inf")
            best_state_dict = None

            # ========= training =========
            self._sync_device(device)
            training_process_start = time.perf_counter()
            for epoch in range(self.epochs):
                # ---- train epoch ----
                model.train()
                # train_loss_sum = 0.0
                # train_count = 0

                for batch_X, batch_y in train_loader:
                    optimizer.zero_grad()
                    batch_X = batch_X.to(device)
                    batch_y = batch_y.to(device)

                    out = model(batch_X)
                    loss = criterion(out, batch_y)
                    loss.backward()
                    optimizer.step()

                    # train_loss_sum += loss.item() * batch_X.size(0)
                    # train_count += batch_X.size(0)

                # mean_train_loss = train_loss_sum / train_count

                # ---- validation epoch ----
                model.eval()
                val_loss_sum = 0.0
                val_count = 0
                with torch.no_grad():
                    for batch_X, batch_y in val_loader:
                        batch_X = batch_X.to(device)
                        batch_y = batch_y.to(device)

                        out = model(batch_X)
                        loss = criterion(out, batch_y)
                        val_loss_sum += loss.item() * batch_X.size(0)
                        val_count += batch_X.size(0)

                mean_val_loss = val_loss_sum / max(1, val_count)

                # save best val loss checkpoint
                if mean_val_loss < best_val_loss:
                    best_val_loss = mean_val_loss
                    best_state_dict = copy.deepcopy(model.state_dict())

                scheduler.step()
            self._sync_device(device)
            training_process_time_sec = time.perf_counter() - training_process_start

            # ========= evaluate AUROC on test set using best val loss checkpoint =========
            if best_state_dict is not None:
                model.load_state_dict(best_state_dict)

            self._sync_device(device)
            best_val_loss_start = time.perf_counter()
            best_val_loss = self._compute_validation_loss(
                model=model,
                criterion=criterion,
                val_X=val_X,
                val_y=val_y,
                device=device,
                batch_size=batch_size,
            )
            self._sync_device(device)
            best_val_loss_compute_time_sec = time.perf_counter() - best_val_loss_start

            self._sync_device(device)
            rgn_start = time.perf_counter()
            rgn_layer = self._compute_rgn_for_layer(
                model=model,
                criterion=criterion,
                val_X=val_X,
                val_y=val_y,
                device=device,
                batch_size=1,
            )
            self._sync_device(device)
            rgn_compute_time_sec = time.perf_counter() - rgn_start

            self._sync_device(device)
            snr_start = time.perf_counter()
            snr_layer = self._compute_snr_for_layer(
                model=model,
                criterion=criterion,
                val_X=val_X,
                val_y=val_y,
                device=device,
                batch_size=1,
            )
            self._sync_device(device)
            snr_compute_time_sec = time.perf_counter() - snr_start

            total_time_sec = (
                training_process_time_sec
                + best_val_loss_compute_time_sec
                + rgn_compute_time_sec
                + snr_compute_time_sec
            )

            model.eval()
            test_dataset = TensorDataset(test_X)
            test_loader  = DataLoader(
                test_dataset,
                batch_size=batch_size,
                shuffle=False,
            )

            pos_scores_list = []
            with torch.no_grad():
                for (batch_X,) in test_loader:
                    batch_X = batch_X.to(device)
                    logits = model(batch_X)                  # [b, 2]
                    probs  = torch.softmax(logits, dim=-1)   # [b, 2]
                    pos    = probs[:, 1]                     # positive class probability [b]
                    pos_scores_list.append(pos.cpu().numpy())

            pos_scores = np.concatenate(pos_scores_list, axis=0)  # [N_test]
            auroc_saplma = roc(test_y, pos_scores)

            # record one row of metrics for this layer
            metrics_rows.append(
                {
                    "layer": int(layer),
                    "auroc_saplma": float(auroc_saplma),
                    "best_val_loss": float(best_val_loss),
                    "rgn": float(rgn_layer),
                    "snr": float(snr_layer),
                    
                    "training_process_time_sec": float(training_process_time_sec),
                    "best_val_loss_compute_time_sec": float(best_val_loss_compute_time_sec),
                    "rgn_compute_time_sec": float(rgn_compute_time_sec),
                    "snr_compute_time_sec": float(snr_compute_time_sec),
                    "total_time_sec": float(total_time_sec),
                }
            )
            total_training_process_time_sec += training_process_time_sec
            total_best_val_loss_compute_time_sec += best_val_loss_compute_time_sec
            total_rgn_compute_time_sec += rgn_compute_time_sec
            total_snr_compute_time_sec += snr_compute_time_sec

        # ========= save results =========
        os.makedirs(self.results_dir, exist_ok=True)

        results_df = pd.DataFrame(
            metrics_rows,
            columns=[
                "layer",
                "auroc_saplma",
                "best_val_loss",
                "rgn",
                "snr",
                "id_lastfeat_twonn",
                "training_process_time_sec",
                "best_val_loss_compute_time_sec",
                "rgn_compute_time_sec",
                "snr_compute_time_sec",
                "total_time_sec",
            ],
        )
        csv_path = os.path.join(
            self.results_dir,
            f"auroc_valLoss_rgn_snr_lr{self.lr}_epochs{self.epochs}"
            f"_h1={hidden_dim1}_layer{self.layer_num}.csv",
        )
        results_df.to_csv(csv_path, index=False)
        print(f"Saved SAPLMA metrics CSV to {csv_path}")

        summary_path = os.path.join(
            self.results_dir,
            f"auroc_valLoss_rgn_snr_lr{self.lr}_epochs{self.epochs}"
            f"_h1={hidden_dim1}_layer{self.layer_num}_summary.json",
        )
        summary = {
            "total_training_process_time_sec": float(total_training_process_time_sec),
            "total_best_val_loss_compute_time_sec": float(total_best_val_loss_compute_time_sec),
            "total_rgn_compute_time_sec": float(total_rgn_compute_time_sec),
            "total_snr_compute_time_sec": float(total_snr_compute_time_sec),
            "total_time_sec": float(
                total_training_process_time_sec
                + total_best_val_loss_compute_time_sec
                + total_rgn_compute_time_sec
                + total_snr_compute_time_sec
            ),
        }
        with open(summary_path, "w", encoding="utf-8") as f:
            json.dump(summary, f, indent=2)
        print(f"Saved SAPLMA timing summary JSON to {summary_path}")

        # plot
        self._plot_layer_metrics(results_df, hidden_dim1)
        print(f"SAPLMA computation complete for model={self.model}, dataset={self.dataset}")

    @staticmethod
    def _sync_device(device: torch.device) -> None:
        if device.type == "cuda":
            torch.cuda.synchronize(device)



    def _compute_validation_loss(
        self,
        model: nn.Module,
        criterion: nn.Module,
        val_X: torch.Tensor,
        val_y: torch.Tensor,
        device: torch.device,
        batch_size: int,
    ) -> float:
        model.eval()

        val_dataset = TensorDataset(val_X, val_y)
        val_loader = DataLoader(
            val_dataset,
            batch_size=batch_size,
            shuffle=False,
        )

        val_loss_sum = 0.0
        val_count = 0
        with torch.no_grad():
            for batch_X, batch_y in val_loader:
                batch_X = batch_X.to(device)
                batch_y = batch_y.to(device)

                out = model(batch_X)
                loss = criterion(out, batch_y)
                val_loss_sum += loss.item() * batch_X.size(0)
                val_count += batch_X.size(0)

        return float(val_loss_sum / max(1, val_count))

    def _compute_rgn_for_layer(
        self,
        model: nn.Module,
        criterion: nn.Module,
        val_X: torch.Tensor,
        val_y: torch.Tensor,
        device: torch.device,
        batch_size: int,
        eps: float = 1e-12,
    ) -> float:
        """
        Runs backward on the validation set loss of the trained probe
        to estimate RGN for this layer: ||g||_2 / ||theta||_2.
        """
        model.eval()

        val_dataset = TensorDataset(val_X, val_y)
        val_loader  = DataLoader(
            val_dataset,
            batch_size=batch_size,
            shuffle=False,
        )

        rgn_vals = []

        for batch_X, batch_y in val_loader:
            batch_X = batch_X.to(device)
            batch_y = batch_y.to(device)

            # clear gradients
            model.zero_grad(set_to_none=True)

            # forward + backward
            out = model(batch_X)
            loss = criterion(out, batch_y)
            loss.backward()

            # ===== collect all gradients & parameters, then flatten and concatenate =====
            all_g = []
            all_theta = []
            for p in model.parameters():
                # if p.grad is None:
                #     continue
                # cast to float() to avoid numerical issues with half/bfloat16
                all_g.append(p.grad.detach().float().view(-1))
                all_theta.append(p.detach().float().view(-1))

            g_flat = torch.cat(all_g)        # [total_num_params]
            theta_flat = torch.cat(all_theta)

            g_norm = torch.linalg.norm(g_flat)         # sqrt(sum_j g_j^2)
            theta_norm = torch.linalg.norm(theta_flat) # sqrt(sum_j theta_j^2)
            rgn_batch = (g_norm / (theta_norm + eps)).item()
            rgn_vals.append(rgn_batch)
            
        # average over batches to get the global RGN for this layer's probe
        rgn_layer = float(torch.tensor(rgn_vals).mean().item())

        model.zero_grad(set_to_none=True)
        return rgn_layer

    def _compute_snr_for_layer(
        self,
        model: nn.Module,
        criterion: nn.Module,
        val_X: torch.Tensor,
        val_y: torch.Tensor,
        device: torch.device,
        batch_size: int,
        eps: float = 1e-12,
    ) -> float:
        """
        Runs backward on the validation set loss of the trained probe
        to estimate SNR for this layer: (sum_j g_j)^2 / sum_j g_j^2.
        """
        model.eval()

        val_dataset = TensorDataset(val_X, val_y)
        val_loader  = DataLoader(
            val_dataset,
            batch_size=batch_size,
            shuffle=False,
        )

        snr_vals = []

        for batch_X, batch_y in val_loader:
            batch_X = batch_X.to(device)
            batch_y = batch_y.to(device)

            model.zero_grad(set_to_none=True)

            out = model(batch_X)
            loss = criterion(out, batch_y)
            loss.backward()

            all_g = []
            for p in model.parameters():
                all_g.append(p.grad.detach().float().view(-1))

            g_flat = torch.cat(all_g)

            sum_g = g_flat.sum()
            sum_g2 = (g_flat * g_flat).sum()
            snr_batch = ((sum_g * sum_g) / (sum_g2 + eps)).item()
            snr_vals.append(snr_batch)

        snr_layer = float(torch.tensor(snr_vals).mean().item())

        model.zero_grad(set_to_none=True)
        return snr_layer

    def _plot_layer_metrics(self, df: pd.DataFrame, hidden_dim1: int) -> None:
        """
        df must contain columns: ["layer", "auroc_saplma", "best_val_loss", "rgn", "snr"]

        Left axis: AUROC
        Right axis: best_val_loss / rgn / snr each min-max normalized to [0, 1],
                    plotted on the same axis to compare their shapes across layers.
        """
        layers     = df["layer"].values
        aurocs     = df["auroc_saplma"].values
        val_losses = df["best_val_loss"].values
        rgns       = df["rgn"].values
        snrs       = df["snr"].values
        ID=df['id_lastfeat_twonn'].values
        def minmax_norm(x: np.ndarray, eps: float = 1e-12) -> np.ndarray:
            x_min = float(x.min())
            x_max = float(x.max())
            denom = max(x_max - x_min, eps)
            return (x - x_min) / denom
        
        val_losses_norm = minmax_norm(val_losses)
        rgns_norm       = minmax_norm(rgns)
        snrs_norm       = minmax_norm(snrs)

        fig, ax1 = plt.subplots(figsize=(8, 5))

        # ---------- left axis: AUROC ----------
        ln1 = ax1.plot(
            layers,
            aurocs,
            marker="o",
            linestyle="-",
            label="AUROC (SAPLMA)",
        )
        ax1.set_xlabel("Layer")
        ax1.set_ylabel("AUROC (SAPLMA)")
        ax1.grid(True, linestyle="--", alpha=0.5)

        # ---------- right axis: normalized ValLoss / RGN / SNR ----------
        ax2 = ax1.twinx()


        ln4 = ax2.plot(
            layers,
            ID,
            marker="s",
            linestyle="-",
            color="tab:red",
            label="ID",
        )
        ax2.set_ylabel("Normalized Val Loss / RGN / SNR (0–1)")

        # ---------- merge legend ----------
        # lines  = ln1 + ln2 + ln3 + ln4
        lines=ln1+ln4
        labels = [l.get_label() for l in lines]
        ax1.legend(lines, labels, loc="best")

        fig.tight_layout()
        fig_path = os.path.join(
            self.results_dir,
            f"auroc_valLoss_rgn_snr_lr{self.lr}_epochs{self.epochs}"
            f"_h1={hidden_dim1}_layer{self.layer_num}.png",
        )
        plt.savefig(fig_path, bbox_inches="tight")
        plt.close()

        print(f"Saved SAPLMA AUROC + ValLoss + RGN + SNR figure to {fig_path}")

if __name__ == "__main__":
    args = build_saplma_parser().parse_args()
    if args.all_data:
        for dataset_name in ['coqa','hotpotqa','squad','triviaqa','psiloqa','math']:
            args.dataset=dataset_name
            sap = SAPLMA(args=args)
            sap.run_saplma()
    else:
        sap = SAPLMA(args=args)
        sap.run_saplma()