File size: 52,162 Bytes
9e047c8
 
 
 
 
ac62897
9e047c8
ac62897
9e047c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac62897
 
 
9e047c8
ac62897
 
 
 
 
 
 
9e047c8
 
 
 
 
 
 
 
 
ac62897
 
 
 
 
 
 
9e047c8
 
 
 
ac62897
9e047c8
 
ac62897
 
 
 
9e047c8
ac62897
 
 
9e047c8
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
 
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
ac62897
 
 
 
 
9e047c8
 
 
 
 
 
 
ac62897
 
 
9e047c8
 
 
ac62897
 
9e047c8
 
 
ac62897
 
 
9e047c8
ac62897
 
 
 
 
 
 
 
9e047c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
ac62897
 
 
 
9e047c8
ac62897
9e047c8
 
ac62897
 
 
 
9e047c8
ac62897
 
 
 
9e047c8
ac62897
 
 
9e047c8
 
ac62897
 
 
 
 
 
 
 
 
 
b72be6b
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99487
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
 
 
b72be6b
 
9e047c8
ac62897
 
 
 
 
 
9e047c8
ac62897
9e047c8
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
ac62897
9e047c8
ac62897
9e047c8
b72be6b
 
 
9e047c8
 
 
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
 
 
ac62897
9e047c8
 
ac62897
 
9e047c8
 
ac62897
 
9e047c8
ac62897
 
 
 
 
9e047c8
 
 
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b72be6b
 
ac62897
 
 
 
 
 
 
 
 
 
b72be6b
 
 
ac62897
 
 
 
 
 
 
 
 
 
 
9e047c8
 
 
ac62897
 
9e047c8
 
 
b72be6b
9e047c8
b72be6b
9e047c8
b72be6b
 
ff99487
b72be6b
9e047c8
 
 
b72be6b
9e047c8
ac62897
b72be6b
 
 
 
 
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b72be6b
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b72be6b
 
 
 
 
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b72be6b
ac62897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e047c8
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
from __future__ import annotations

import csv
import gc
import json
import math
import os
import random
import time
import traceback
from pathlib import Path
from typing import Any


ROOT = Path(__file__).resolve().parent
os.environ.setdefault("HF_HOME", str((ROOT / ".hf_cache").resolve()))
os.environ.setdefault("HF_DATASETS_CACHE", str((ROOT / ".hf_cache" / "datasets").resolve()))
os.environ.setdefault("TRANSFORMERS_CACHE", str((ROOT / ".hf_cache" / "transformers").resolve()))
os.environ.setdefault("WANDB_DIR", str((ROOT / ".wandb").resolve()))
os.environ.setdefault("WANDB_CACHE_DIR", str((ROOT / ".wandb" / "cache").resolve()))
os.environ.setdefault("WANDB_CONFIG_DIR", str((ROOT / ".wandb" / "config").resolve()))
os.environ.setdefault("DISABLE_SAFETENSORS_CONVERSION", "1")

import gradio as gr
import torch
from datasets import load_dataset
from peft import LoraConfig, TaskType, get_peft_model
from transformers import AutoModelForCausalLM, AutoTokenizer

try:
    import lbw
except Exception as exc:  # pragma: no cover - shown in the Space UI.
    lbw = None
    LBW_IMPORT_ERROR = exc
else:
    LBW_IMPORT_ERROR = None


RUNS_DIR = ROOT / "runs"


def _device_default() -> str:
    return "cuda" if torch.cuda.is_available() else "cpu"


def _set_seed(seed: int) -> None:
    random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)


def _safe_float(value: Any) -> float | None:
    if value is None:
        return None
    try:
        out = float(value)
    except Exception:
        return None
    if not math.isfinite(out):
        return None
    return out


def _fmt_float(value: Any, digits: int = 4) -> str:
    number = _safe_float(value)
    return "-" if number is None else f"{number:.{digits}f}"


def _append_log(logs: list[str], message: str) -> None:
    logs.append(message)
    print(message, flush=True)


def _build_wikitext_chunks(
    tokenizer,
    *,
    split: str,
    max_chars: int | None,
    seq_len: int,
    logs: list[str],
) -> dict[str, Any]:
    cap = None if max_chars is None else int(max_chars)
    _append_log(
        logs,
        f"Preparing WikiText split={split!r}" + (f" with char cap {cap:,}" if cap is not None else " with full split"),
    )
    ds = load_dataset("wikitext", "wikitext-103-raw-v1", split=split)
    pieces: list[str] = []
    chars_used = 0
    rows_used = 0
    first_piece = True
    for row in ds:
        text = str(row.get("text", "") or "")
        if not text.strip():
            continue
        piece = text if first_piece else " " + text
        if cap is not None:
            remain = cap - chars_used
            if remain <= 0:
                break
            if len(piece) > remain:
                piece = piece[:remain]
        pieces.append(piece)
        chars_used += len(piece)
        rows_used += 1
        first_piece = False
        if cap is not None and chars_used >= cap:
            break

    token_ids = tokenizer("".join(pieces), add_special_tokens=False)["input_ids"]
    ids = torch.tensor(token_ids, dtype=torch.long)
    sequence_count = ids.numel() // int(seq_len)
    if sequence_count <= 0:
        raise RuntimeError("Not enough tokens. Increase the train/eval char cap or reduce sequence length.")
    ids = ids[: sequence_count * int(seq_len)].view(sequence_count, int(seq_len)).contiguous()
    _append_log(
        logs,
        f"Prepared split={split!r}: {chars_used:,} chars across {rows_used:,} rows -> {ids.size(0):,} sequences",
    )
    return {"input_ids": ids, "chars": chars_used, "rows": rows_used, "cap": cap}


def _batch_iter(chunks: dict[str, Any], *, batch_size: int, device: torch.device):
    ids = chunks["input_ids"]
    i = 0
    while True:
        if i + int(batch_size) > ids.size(0):
            i = 0
        batch = ids[i : i + int(batch_size)].to(device, non_blocking=True)
        i += int(batch_size)
        yield batch


def _load_lora_model(
    *,
    model_name: str,
    device: torch.device,
    lora_r: int,
    lora_alpha: int,
    lora_dropout: float,
):
    dtype = torch.float16 if device.type == "cuda" else torch.float32
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        torch_dtype=dtype,
        low_cpu_mem_usage=True,
    )
    if getattr(model.config, "use_cache", None) is not None:
        model.config.use_cache = False
    model.to(device)
    lora_cfg = LoraConfig(
        r=int(lora_r),
        lora_alpha=int(lora_alpha),
        lora_dropout=float(lora_dropout),
        target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
        task_type=TaskType.CAUSAL_LM,
        bias="none",
    )
    return get_peft_model(model, lora_cfg)


def _make_optimizer(
    name: str,
    model,
    *,
    lr: float,
    betas: tuple[float, float],
    weight_decay: float,
    lbw_stats_freq: int,
    lbw_stress_th: float,
    lbw_spike_th: float,
    lbw_rec_fast: float,
    lbw_ema_decay: float,
):
    params = [param for param in model.parameters() if param.requires_grad]
    if name == "adamw":
        return torch.optim.AdamW(params, lr=float(lr), betas=betas, weight_decay=float(weight_decay))
    if name == "lbw_guard":
        if lbw is None:
            raise RuntimeError(f"LBW Guard package import failed: {LBW_IMPORT_ERROR}")
        return lbw.Guard(
            params,
            lr=float(lr),
            betas=betas,
            weight_decay=float(weight_decay),
            mode="eval",
            auto_enabled=True,
            stats_freq=int(lbw_stats_freq),
            stress_threshold=float(lbw_stress_th),
            spike_threshold=float(lbw_spike_th),
            recovery_fast=float(lbw_rec_fast),
            ema_decay=float(lbw_ema_decay),
            use_max_rms=True,
        )
    raise ValueError(f"Unknown optimizer: {name}")


@torch.no_grad()
def _evaluate_ppl(
    model,
    eval_chunks: dict[str, Any],
    *,
    batch_size: int,
    eval_batches: int,
    device: torch.device,
    full_pass: bool,
) -> tuple[float, float]:
    model.eval()
    ids = eval_chunks["input_ids"]
    max_sequences = ids.size(0) if full_pass else min(ids.size(0), int(eval_batches) * int(batch_size))
    losses: list[float] = []
    for start in range(0, max_sequences, int(batch_size)):
        xb = ids[start : start + int(batch_size)].to(device, non_blocking=True)
        with torch.autocast(device_type=device.type, dtype=torch.float16, enabled=(device.type == "cuda")):
            loss = model(input_ids=xb, labels=xb).loss
        losses.append(float(loss.detach().cpu()))
    avg_loss = sum(losses) / max(len(losses), 1)
    return avg_loss, math.exp(min(avg_loss, 20.0))


def _optimizer_state(opt) -> dict[str, Any]:
    state = dict(getattr(opt, "state", {}).get("lbw", {}) or {})
    return {
        "scale": float(state.get("scale", state.get("lbw_scale", 1.0))),
        "ratio": float(state.get("ratio", 1.0)),
        "stress_mode": str(state.get("stress_mode", "none")),
    }


def _status_markdown(
    *,
    device_name: str,
    rows: list[dict[str, Any]],
    logs: list[str],
    phase: str,
) -> str:
    summary = [
        f"Device: `{device_name}`",
        "",
        f"Status: {phase}",
        "",
        "## Results",
        "",
        "| Optimizer | Final Eval PPL | Final Eval Loss | Scope | Scale | Ratio | Stress Mode | Wall Time (s) |",
        "| --- | --- | --- | --- | --- | --- | --- | --- |",
    ]
    if rows:
        for row in rows:
            summary.append(
                "| {optimizer} | {ppl} | {loss} | {scope} | {scale} | {ratio} | {stress} | {wall} |".format(
                    optimizer=row.get("optimizer"),
                    ppl=_fmt_float(row.get("final_eval_ppl")),
                    loss=_fmt_float(row.get("final_eval_loss")),
                    scope=row.get("final_eval_scope") or "-",
                    scale=_fmt_float(row.get("scale")),
                    ratio=_fmt_float(row.get("ratio")),
                    stress=row.get("stress_mode") or "-",
                    wall=_fmt_float(row.get("wall_time_sec"), digits=2),
                )
            )
    else:
        summary.append("| - | - | - | - | - | - | - | - |")

    gains = _gain_rows(rows)
    if gains:
        summary.extend(["", "## LBW vs AdamW", ""])
        for gain in gains:
            pct = _safe_float(gain.get("eval_perplexity_pct_gain_vs_adamw"))
            wall_speedup = _safe_float(gain.get("wall_time_speedup_vs_adamw"))
            summary.append(
                f"- `{gain.get('optimizer')}` PPL gain vs AdamW: `{_fmt_float(gain.get('eval_perplexity_gain_vs_adamw'))}`"
                + (f" (`{pct * 100.0:.2f}%`)." if pct is not None else ".")
            )
            if wall_speedup is not None:
                summary.append(f"- `{gain.get('optimizer')}` wall-time speedup vs AdamW: `{wall_speedup:.3f}x`.")

    summary.extend(["", "## Runtime Log", "", "```text", "\n".join(logs[-80:]), "```"])
    return "\n".join(summary)


def _run_one_optimizer_events(
    *,
    optimizer_name: str,
    model_name: str,
    train_chunks: dict[str, Any],
    eval_chunks: dict[str, Any],
    device: torch.device,
    seed: int,
    max_steps: int,
    eval_every: int,
    eval_batches: int,
    seq_len: int,
    batch_size: int,
    lr: float,
    betas: tuple[float, float],
    weight_decay: float,
    full_validation_ppl: bool,
    lora_r: int,
    lora_alpha: int,
    lora_dropout: float,
    lbw_stats_freq: int,
    lbw_stress_th: float,
    lbw_spike_th: float,
    lbw_rec_fast: float,
    lbw_ema_decay: float,
    logs: list[str],
):
    _set_seed(int(seed))
    _append_log(logs, f"Loading {model_name} with LoRA for {optimizer_name}.")
    model = _load_lora_model(
        model_name=model_name,
        device=device,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
    )
    model.train()
    opt = _make_optimizer(
        optimizer_name,
        model,
        lr=lr,
        betas=betas,
        weight_decay=weight_decay,
        lbw_stats_freq=lbw_stats_freq,
        lbw_stress_th=lbw_stress_th,
        lbw_spike_th=lbw_spike_th,
        lbw_rec_fast=lbw_rec_fast,
        lbw_ema_decay=lbw_ema_decay,
    )
    train_batches = _batch_iter(train_chunks, batch_size=batch_size, device=device)
    start_time = time.time()
    last_loss = None
    last_eval_loss = None
    last_eval_ppl = None
    state = _optimizer_state(opt)
    trainable_params = [param for param in model.parameters() if param.requires_grad]

    for step in range(1, int(max_steps) + 1):
        xb = next(train_batches)
        with torch.autocast(device_type=device.type, dtype=torch.float16, enabled=(device.type == "cuda")):
            loss = model(input_ids=xb, labels=xb).loss
        loss.backward()
        torch.nn.utils.clip_grad_norm_(trainable_params, 1.0)
        opt.step()
        opt.zero_grad(set_to_none=True)
        last_loss = float(loss.detach().cpu())
        state = _optimizer_state(opt)

        if step == 1 or step == int(max_steps) or step % int(eval_every) == 0:
            last_eval_loss, last_eval_ppl = _evaluate_ppl(
                model,
                eval_chunks,
                batch_size=batch_size,
                eval_batches=eval_batches,
                device=device,
                full_pass=False,
            )
            message = (
                f"{optimizer_name} step {step}/{int(max_steps)}: "
                f"loss={last_loss:.4f}, sampled_eval_ppl={last_eval_ppl:.4f}, "
                f"scale={state['scale']:.4f}, ratio={state['ratio']:.4f}"
            )
            _append_log(logs, message)
            yield {"type": "progress", "message": message}
            model.train()

    final_full_pass = bool(full_validation_ppl)
    if final_full_pass and eval_chunks["cap"] is None:
        final_scope = "full_wikitext"
    elif final_full_pass:
        final_scope = "full_loaded_subset"
    else:
        final_scope = "sampled"
    _append_log(logs, f"Running final {final_scope} validation PPL for {optimizer_name}.")
    final_loss, final_ppl = _evaluate_ppl(
        model,
        eval_chunks,
        batch_size=batch_size,
        eval_batches=eval_batches,
        device=device,
        full_pass=final_full_pass,
    )
    state = _optimizer_state(opt)
    wall_time = time.time() - start_time
    result = {
        "optimizer": optimizer_name,
        "final_eval_ppl": final_ppl,
        "final_eval_loss": final_loss,
        "final_eval_scope": final_scope,
        "train_chars": train_chunks["chars"],
        "eval_chars": eval_chunks["chars"],
        "train_sequences": int(train_chunks["input_ids"].size(0)),
        "eval_sequences": int(eval_chunks["input_ids"].size(0)),
        "tokens_per_step": int(batch_size) * int(seq_len),
        "last_train_loss": last_loss,
        "last_sampled_eval_loss": last_eval_loss,
        "last_sampled_eval_ppl": last_eval_ppl,
        "scale": state["scale"],
        "ratio": state["ratio"],
        "stress_mode": state["stress_mode"],
        "wall_time_sec": wall_time,
    }
    del model, opt
    gc.collect()
    if device.type == "cuda":
        torch.cuda.empty_cache()
    yield {"type": "result", "result": result}


def _gain_rows(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
    by_optimizer = {str(row.get("optimizer")): row for row in rows}
    baseline = by_optimizer.get("adamw")
    if baseline is None:
        return []
    baseline_ppl = _safe_float(baseline.get("final_eval_ppl"))
    baseline_wall = _safe_float(baseline.get("wall_time_sec"))
    gains: list[dict[str, Any]] = []
    for row in rows:
        if row.get("optimizer") == "adamw":
            continue
        candidate_ppl = _safe_float(row.get("final_eval_ppl"))
        candidate_wall = _safe_float(row.get("wall_time_sec"))
        gains.append(
            {
                "optimizer": row.get("optimizer"),
                "eval_perplexity_gain_vs_adamw": (
                    None if baseline_ppl is None or candidate_ppl is None else baseline_ppl - candidate_ppl
                ),
                "eval_perplexity_pct_gain_vs_adamw": (
                    None
                    if baseline_ppl in (None, 0.0) or candidate_ppl is None
                    else (baseline_ppl - candidate_ppl) / baseline_ppl
                ),
                "wall_time_speedup_vs_adamw": (
                    None
                    if baseline_wall in (None, 0.0) or candidate_wall in (None, 0.0)
                    else baseline_wall / candidate_wall
                ),
            }
        )
    return gains


def _write_csv(path: Path, rows: list[dict[str, Any]]) -> None:
    if not rows:
        path.write_text("", encoding="utf-8")
        return
    with path.open("w", encoding="utf-8", newline="") as handle:
        writer = csv.DictWriter(handle, fieldnames=list(rows[0].keys()))
        writer.writeheader()
        writer.writerows(rows)


def _set_lr(opt, value: float) -> None:
    for group in getattr(opt, "param_groups", []) or []:
        group["lr"] = float(value)


def _scheduled_lr(cfg: dict[str, Any], step: int) -> float:
    base_lr = float(cfg["lr"])
    warmup = max(int(cfg.get("warmup_steps", 0)), 0)
    max_steps = max(int(cfg["max_steps"]), 1)
    if warmup > 0 and int(step) <= warmup:
        return base_lr * float(step) / float(warmup)
    mode = str(cfg.get("schedule_mode", "constant")).strip().lower()
    if mode == "cosine":
        progress = (int(step) - warmup) / max(max_steps - warmup, 1)
        progress = min(max(progress, 0.0), 1.0)
        return base_lr * 0.5 * (1.0 + math.cos(math.pi * progress))
    return base_lr


def _parse_float_sweep(text: str, default: list[float]) -> list[float]:
    raw = str(text or "").replace("\n", ",").replace(";", ",").split(",")
    values: list[float] = []
    for item in raw:
        item = item.strip()
        if not item:
            continue
        values.append(float(item))
    return values or list(default)


def _parse_int_sweep(text: str, default: list[int]) -> list[int]:
    return [int(value) for value in _parse_float_sweep(text, [float(item) for item in default])]


def run_easy_test(
    model_name: str,
    run_lbw_guard: bool,
    max_steps: int,
    eval_every: int,
    eval_batches: int,
    seq_len: int,
    batch_size: int,
    train_chars: int,
    eval_chars: int,
    full_wikitext_train: bool,
    full_wikitext_eval: bool,
    full_validation_ppl: bool,
    lr: float,
    seed: int,
):
    logs: list[str] = []
    rows: list[dict[str, Any]] = []
    run_dir = RUNS_DIR / f"easy_test_{int(time.time())}"
    run_dir.mkdir(parents=True, exist_ok=True)
    device_name = _device_default()
    device = torch.device(device_name)
    optimizers = ["adamw", "lbw_guard"] if bool(run_lbw_guard) else ["adamw"]

    try:
        if device.type == "cpu" and (
            int(max_steps) > 1
            or int(train_chars) > 20_000
            or int(eval_chars) > 8_000
            or bool(full_wikitext_train)
            or bool(full_wikitext_eval)
            or bool(full_validation_ppl)
        ):
            yield (
                "This Space is currently on `cpu-basic`. CPU mode is capped to 1 step, 20k train chars, "
                "8k eval chars, and sampled validation. Switch the Space hardware to GPU for the Quick Comparison defaults.",
                None,
                None,
                None,
            )
            return
        if device.type == "cuda" and bool(run_lbw_guard) and torch.cuda.device_count() > 1:
            yield (
                "LBW Guard should run with one visible GPU. Set the Space to single-GPU hardware or restrict CUDA_VISIBLE_DEVICES.",
                None,
                None,
                None,
            )
            return

        _append_log(logs, f"Device: {device_name}")
        if device.type == "cuda":
            _append_log(logs, f"GPU: {torch.cuda.get_device_name(0)}")
        _append_log(logs, f"Optimizers: {', '.join(optimizers)}")
        yield _status_markdown(device_name=device_name, rows=rows, logs=logs, phase="Loading tokenizer"), None, None, None

        _set_seed(int(seed))
        resolved_model = str(model_name).strip() or "TinyLlama/TinyLlama_v1.1"
        tokenizer = AutoTokenizer.from_pretrained(resolved_model, use_fast=True)
        if tokenizer.pad_token is None:
            tokenizer.pad_token = tokenizer.eos_token

        train_cap = None if bool(full_wikitext_train) else int(train_chars)
        eval_cap = None if bool(full_wikitext_eval) else int(eval_chars)
        train_chunks = _build_wikitext_chunks(
            tokenizer,
            split="train",
            max_chars=train_cap,
            seq_len=int(seq_len),
            logs=logs,
        )
        yield _status_markdown(device_name=device_name, rows=rows, logs=logs, phase="Prepared train split"), None, None, None
        eval_chunks = _build_wikitext_chunks(
            tokenizer,
            split="validation",
            max_chars=eval_cap,
            seq_len=int(seq_len),
            logs=logs,
        )
        yield _status_markdown(device_name=device_name, rows=rows, logs=logs, phase="Prepared validation split"), None, None, None

        for optimizer_name in optimizers:
            _append_log(logs, f"=== {optimizer_name} ===")
            yield _status_markdown(
                device_name=device_name,
                rows=rows,
                logs=logs,
                phase=f"Running {optimizer_name}",
            ), None, None, None
            for event in _run_one_optimizer_events(
                optimizer_name=optimizer_name,
                model_name=resolved_model,
                train_chunks=train_chunks,
                eval_chunks=eval_chunks,
                device=device,
                seed=int(seed),
                max_steps=int(max_steps),
                eval_every=max(1, int(eval_every)),
                eval_batches=int(eval_batches),
                seq_len=int(seq_len),
                batch_size=int(batch_size),
                lr=float(lr),
                betas=(0.9, 0.999),
                weight_decay=0.01,
                full_validation_ppl=bool(full_validation_ppl),
                lora_r=8,
                lora_alpha=16,
                lora_dropout=0.05,
                lbw_stats_freq=10,
                lbw_stress_th=1.1,
                lbw_spike_th=1.5,
                lbw_rec_fast=0.01,
                lbw_ema_decay=0.95,
                logs=logs,
            ):
                if event.get("type") == "result":
                    rows.append(event["result"])
                yield _status_markdown(
                    device_name=device_name,
                    rows=rows,
                    logs=logs,
                    phase=f"Running {optimizer_name}",
                ), None, None, None

        gains = _gain_rows(rows)
        payload = {
            "source": "HF Quick Comparison Runner",
            "based_on_colab": "LBW_Guard_Easy_Test_COLAB.ipynb",
            "config": {
                "model_name": resolved_model,
                "device": device_name,
                "optimizers": optimizers,
                "seed": int(seed),
                "max_steps": int(max_steps),
                "eval_every": int(eval_every),
                "eval_batches": int(eval_batches),
                "seq_len": int(seq_len),
                "batch_size": int(batch_size),
                "max_chars": train_cap,
                "eval_chars": eval_cap,
                "full_wikitext_train": bool(full_wikitext_train),
                "full_wikitext_eval": bool(full_wikitext_eval),
                "full_validation_ppl": bool(full_validation_ppl),
                "lr": float(lr),
                "betas": [0.9, 0.999],
                "weight_decay": 0.01,
                "lora_r": 8,
                "lora_alpha": 16,
                "lora_dropout": 0.05,
                "lbw_stats_freq": 10,
                "lbw_stress_th": 1.1,
                "lbw_spike_th": 1.5,
                "lbw_rec_fast": 0.01,
                "lbw_ema_decay": 0.95,
            },
            "results": rows,
            "gains": gains,
            "logs": logs,
        }
        json_path = run_dir / "lbw_guard_hf_quick_comparison_results.json"
        csv_path = run_dir / "lbw_guard_hf_quick_comparison_results.csv"
        gains_path = run_dir / "lbw_guard_hf_quick_comparison_gains.csv"
        json_path.write_text(json.dumps(payload, indent=2), encoding="utf-8")
        _write_csv(csv_path, rows)
        _write_csv(gains_path, gains)
        _append_log(logs, f"Wrote {csv_path}")
        yield (
            _status_markdown(device_name=device_name, rows=rows, logs=logs, phase="Complete"),
            str(json_path),
            str(csv_path),
            str(gains_path),
        )
    except Exception:
        error_text = traceback.format_exc()
        error_path = run_dir / "error.txt"
        error_path.write_text(error_text + "\n\n" + "\n".join(logs), encoding="utf-8")
        yield f"Run failed.\n\n```text\n{error_text}\n```", str(error_path), None, None


def _make_ablation_scenario(slug: str, label: str, note: str, base_config: dict[str, Any], overrides=None):
    cfg = dict(base_config)
    if overrides:
        cfg.update(overrides)
    return {
        "slug": slug,
        "label": label,
        "note": note,
        "config": cfg,
    }


def _build_ablation_scenarios(
    *,
    selected_ablations: list[str],
    base_config: dict[str, Any],
    lr_sweep: list[float],
    step_sweep: list[int],
    lora_r_sweep: list[int],
) -> list[dict[str, Any]]:
    selected = {str(item).strip().lower() for item in selected_ablations if str(item).strip()}
    if not selected:
        selected = {"optimizer"}
    scenarios: list[dict[str, Any]] = []

    if "optimizer" in selected:
        scenarios.append(
            _make_ablation_scenario(
                "optimizer-adamw-vs-lbw-guard",
                "Optimizer: AdamW vs lbw_guard",
                "Direct optimizer comparison with the base config.",
                base_config,
            )
        )

    if "lr" in selected:
        for lr in lr_sweep:
            scenarios.append(
                _make_ablation_scenario(
                    f"lr-{lr:g}",
                    f"Learning Rate: {lr:g}",
                    "Learning-rate sensitivity check.",
                    base_config,
                    {"lr": float(lr)},
                )
            )

    if "schedule" in selected:
        for mode in ["constant", "cosine"]:
            scenarios.append(
                _make_ablation_scenario(
                    f"schedule-{mode}",
                    f"Schedule: {mode}",
                    "Scheduler-shape sensitivity check.",
                    base_config,
                    {"schedule_mode": mode},
                )
            )

    if "steps" in selected:
        for steps in step_sweep:
            scenarios.append(
                _make_ablation_scenario(
                    f"steps-{steps}",
                    f"Steps: {steps}",
                    "Training-length sensitivity check.",
                    base_config,
                    {"max_steps": int(steps), "eval_every": max(1, int(steps) // 4)},
                )
            )

    if "data" in selected:
        for item in [
            {"max_chars": 20_000, "eval_chars": 8_000, "label": "small-data"},
            {"max_chars": 80_000, "eval_chars": 20_000, "label": "larger-data"},
        ]:
            scenarios.append(
                _make_ablation_scenario(
                    item["label"],
                    f"Data Slice: {item['label']}",
                    "WikiText slice-size sensitivity check.",
                    base_config,
                    {"max_chars": int(item["max_chars"]), "eval_chars": int(item["eval_chars"])},
                )
            )

    if "lora" in selected:
        for rank in lora_r_sweep:
            scenarios.append(
                _make_ablation_scenario(
                    f"lora-r{rank}",
                    f"LoRA Rank: {rank}",
                    "Adapter-capacity sensitivity check.",
                    base_config,
                    {"lora_r": int(rank), "lora_alpha": int(rank) * 2},
                )
            )

    if not scenarios:
        raise ValueError("No scenarios selected. Choose optimizer, lr, schedule, steps, data, or lora.")
    return scenarios


def _ablation_status_markdown(
    *,
    device_name: str,
    rows: list[dict[str, Any]],
    logs: list[str],
    phase: str,
    plan: list[dict[str, Any]],
) -> str:
    summary = [
        f"Device: `{device_name}`",
        "",
        f"Status: {phase}",
        "",
        "## Plan",
        "",
        "| Scenario | Steps | LR | Schedule | Train Chars | Eval Chars | LoRA r |",
        "| --- | --- | --- | --- | --- | --- | --- |",
    ]
    for item in plan:
        cfg = item["config"]
        summary.append(
            "| {label} | {steps} | {lr:g} | {schedule} | {train_chars} | {eval_chars} | {lora_r} |".format(
                label=item["label"],
                steps=int(cfg["max_steps"]),
                lr=float(cfg["lr"]),
                schedule=cfg["schedule_mode"],
                train_chars="FULL" if cfg["full_wikitext_train"] else int(cfg["max_chars"]),
                eval_chars="FULL" if cfg["full_wikitext_eval"] else int(cfg["eval_chars"]),
                lora_r=int(cfg["lora_r"]),
            )
        )

    summary.extend(
        [
            "",
            "## Metrics",
            "",
            "| Scenario | Optimizer | Final Eval PPL | Final Eval Loss | Tokens/s | Scale | Ratio | Stress Mode |",
            "| --- | --- | --- | --- | --- | --- | --- | --- |",
        ]
    )
    if rows:
        for row in rows:
            summary.append(
                "| {scenario} | {optimizer} | {ppl} | {loss} | {tps} | {scale} | {ratio} | {stress} |".format(
                    scenario=row.get("scenario"),
                    optimizer=row.get("optimizer"),
                    ppl=_fmt_float(row.get("final_eval_ppl")),
                    loss=_fmt_float(row.get("final_eval_loss")),
                    tps=_fmt_float(row.get("tokens_per_sec_wall"), digits=2),
                    scale=_fmt_float(row.get("scale")),
                    ratio=_fmt_float(row.get("ratio")),
                    stress=row.get("stress_mode") or "-",
                )
            )
    else:
        summary.append("| - | - | - | - | - | - | - | - |")

    gains = _build_ablation_gain_rows(rows)
    if gains:
        summary.extend(["", "## LBW vs AdamW", ""])
        for gain in gains:
            summary.append(
                f"- `{gain.get('scenario')}`: `{gain.get('optimizer')}` "
                f"PPL gain `{_fmt_float(gain.get('ppl_gain_pct_vs_adamw'))}%`, "
                f"loss gain `{_fmt_float(gain.get('loss_gain_pct_vs_adamw'))}%`, "
                f"speed gain `{_fmt_float(gain.get('speed_gain_pct_vs_adamw'))}%`."
            )

    summary.extend(["", "## Runtime Log", "", "```text", "\n".join(logs[-100:]), "```"])
    return "\n".join(summary)


def _run_ablation_optimizer_events(
    *,
    scenario_item: dict[str, Any],
    optimizer_name: str,
    model_name: str,
    train_chunks: dict[str, Any],
    eval_chunks: dict[str, Any],
    device: torch.device,
    logs: list[str],
):
    cfg = scenario_item["config"]
    _set_seed(int(cfg["seed"]))
    _append_log(logs, f"Loading {model_name} with LoRA for {scenario_item['slug']} / {optimizer_name}.")
    model = _load_lora_model(
        model_name=model_name,
        device=device,
        lora_r=int(cfg["lora_r"]),
        lora_alpha=int(cfg["lora_alpha"]),
        lora_dropout=float(cfg["lora_dropout"]),
    )
    model.train()
    opt = _make_optimizer(
        optimizer_name,
        model,
        lr=float(cfg["lr"]),
        betas=tuple(cfg["betas"]),
        weight_decay=float(cfg["weight_decay"]),
        lbw_stats_freq=int(cfg["lbw_stats_freq"]),
        lbw_stress_th=float(cfg["lbw_stress_th"]),
        lbw_spike_th=float(cfg["lbw_spike_th"]),
        lbw_rec_fast=float(cfg["lbw_rec_fast"]),
        lbw_ema_decay=float(cfg["lbw_ema_decay"]),
    )
    train_batches = _batch_iter(train_chunks, batch_size=int(cfg["batch_size"]), device=device)
    trainable_params = [param for param in model.parameters() if param.requires_grad]
    start_time = time.time()
    losses: list[float] = []
    eval_loss = None
    eval_ppl = None
    last_lr = float(cfg["lr"])
    state = _optimizer_state(opt)

    for step in range(1, int(cfg["max_steps"]) + 1):
        last_lr = _scheduled_lr(cfg, step)
        _set_lr(opt, last_lr)
        xb = next(train_batches)
        with torch.autocast(device_type=device.type, dtype=torch.float16, enabled=(device.type == "cuda")):
            loss = model(input_ids=xb, labels=xb).loss
        loss.backward()
        torch.nn.utils.clip_grad_norm_(trainable_params, 1.0)
        opt.step()
        opt.zero_grad(set_to_none=True)
        loss_value = float(loss.detach().cpu())
        losses.append(loss_value)

        if step == 1 or step == int(cfg["max_steps"]) or step % int(cfg["eval_every"]) == 0:
            eval_loss, eval_ppl = _evaluate_ppl(
                model,
                eval_chunks,
                batch_size=int(cfg["batch_size"]),
                eval_batches=int(cfg["eval_batches"]),
                device=device,
                full_pass=False,
            )
            state = _optimizer_state(opt)
            message = (
                f"[{scenario_item['slug']}] {optimizer_name} step {step}/{cfg['max_steps']}: "
                f"loss={loss_value:.4f}, sampled_eval_ppl={eval_ppl:.4f}, "
                f"lr={last_lr:.2e}, scale={state['scale']:.4f}, ratio={state['ratio']:.4f}"
            )
            _append_log(logs, message)
            yield {"type": "progress", "message": message}
            model.train()

    final_full_pass = bool(cfg["full_validation_ppl"])
    if final_full_pass and eval_chunks["cap"] is None:
        final_scope = "full_wikitext"
    elif final_full_pass:
        final_scope = "full_loaded_subset"
    else:
        final_scope = "sampled"
    _append_log(logs, f"Running final {final_scope} validation PPL for {scenario_item['slug']} / {optimizer_name}.")
    final_loss, final_ppl = _evaluate_ppl(
        model,
        eval_chunks,
        batch_size=int(cfg["batch_size"]),
        eval_batches=int(cfg["eval_batches"]),
        device=device,
        full_pass=final_full_pass,
    )
    state = _optimizer_state(opt)
    wall_time = max(time.time() - start_time, 1e-9)
    trained_tokens = int(cfg["max_steps"]) * int(cfg["batch_size"]) * int(cfg["seq_len"])
    result = {
        "scenario_slug": scenario_item["slug"],
        "scenario": scenario_item["label"],
        "optimizer": optimizer_name,
        "final_eval_ppl": final_ppl,
        "final_eval_loss": final_loss,
        "train_loss_last": losses[-1] if losses else None,
        "last_sampled_eval_loss": eval_loss,
        "last_sampled_eval_ppl": eval_ppl,
        "final_eval_scope": final_scope,
        "max_steps": int(cfg["max_steps"]),
        "lr": float(cfg["lr"]),
        "scheduled_lr_last": float(last_lr),
        "schedule_mode": str(cfg["schedule_mode"]),
        "batch_size": int(cfg["batch_size"]),
        "seq_len": int(cfg["seq_len"]),
        "lora_r": int(cfg["lora_r"]),
        "train_chars": int(train_chunks["chars"]),
        "eval_chars": int(eval_chunks["chars"]),
        "train_sequences": int(train_chunks["input_ids"].size(0)),
        "eval_sequences": int(eval_chunks["input_ids"].size(0)),
        "scale": state["scale"],
        "ratio": state["ratio"],
        "stress_mode": state["stress_mode"],
        "wall_time_sec": wall_time,
        "tokens_per_sec_wall": trained_tokens / wall_time,
    }

    del model, opt
    gc.collect()
    if device.type == "cuda":
        torch.cuda.empty_cache()
    yield {"type": "result", "result": result}


def _build_ablation_gain_rows(metrics: list[dict[str, Any]]) -> list[dict[str, Any]]:
    grouped: dict[str, list[dict[str, Any]]] = {}
    for row in metrics:
        grouped.setdefault(str(row.get("scenario_slug")), []).append(row)
    gain_rows: list[dict[str, Any]] = []
    for scenario_slug, rows in grouped.items():
        baseline = next((row for row in rows if row.get("optimizer") == "adamw"), None)
        if baseline is None:
            continue
        baseline_ppl = _safe_float(baseline.get("final_eval_ppl"))
        baseline_loss = _safe_float(baseline.get("final_eval_loss"))
        baseline_tps = _safe_float(baseline.get("tokens_per_sec_wall"))
        for row in rows:
            if row.get("optimizer") == "adamw":
                continue
            candidate_ppl = _safe_float(row.get("final_eval_ppl"))
            candidate_loss = _safe_float(row.get("final_eval_loss"))
            candidate_tps = _safe_float(row.get("tokens_per_sec_wall"))
            gain_rows.append(
                {
                    "scenario_slug": scenario_slug,
                    "scenario": row.get("scenario"),
                    "optimizer": row.get("optimizer"),
                    "adamw_final_eval_ppl": baseline_ppl,
                    "optimizer_final_eval_ppl": candidate_ppl,
                    "ppl_gain_pct_vs_adamw": (
                        None
                        if baseline_ppl in (None, 0.0) or candidate_ppl is None
                        else (baseline_ppl - candidate_ppl) / baseline_ppl * 100.0
                    ),
                    "loss_gain_pct_vs_adamw": (
                        None
                        if baseline_loss in (None, 0.0) or candidate_loss is None
                        else (baseline_loss - candidate_loss) / baseline_loss * 100.0
                    ),
                    "speed_gain_pct_vs_adamw": (
                        None
                        if baseline_tps in (None, 0.0) or candidate_tps is None
                        else (candidate_tps - baseline_tps) / baseline_tps * 100.0
                    ),
                    "adamw_tokens_per_sec_wall": baseline_tps,
                    "optimizer_tokens_per_sec_wall": candidate_tps,
                    "lbw_scale": row.get("scale"),
                    "lbw_ratio": row.get("ratio"),
                    "lbw_stress_mode": row.get("stress_mode"),
                }
            )
    return gain_rows


def run_ablation_test(
    model_name: str,
    selected_ablations: list[str],
    run_lbw_guard: bool,
    max_steps: int,
    eval_every: int,
    eval_batches: int,
    seq_len: int,
    batch_size: int,
    train_chars: int,
    eval_chars: int,
    full_wikitext_train: bool,
    full_wikitext_eval: bool,
    full_validation_ppl: bool,
    lr: float,
    schedule_mode: str,
    warmup_steps: int,
    seed: int,
    lr_sweep_text: str,
    step_sweep_text: str,
    lora_r_sweep_text: str,
):
    logs: list[str] = []
    rows: list[dict[str, Any]] = []
    run_dir = RUNS_DIR / f"ablation_test_{int(time.time())}"
    run_dir.mkdir(parents=True, exist_ok=True)
    device_name = _device_default()
    device = torch.device(device_name)
    optimizers = ["adamw", "lbw_guard"] if bool(run_lbw_guard) else ["adamw"]

    try:
        base_config = {
            "seed": int(seed),
            "max_steps": int(max_steps),
            "eval_every": max(1, int(eval_every)),
            "eval_batches": int(eval_batches),
            "seq_len": int(seq_len),
            "batch_size": int(batch_size),
            "max_chars": int(train_chars),
            "eval_chars": int(eval_chars),
            "full_wikitext_train": bool(full_wikitext_train),
            "full_wikitext_eval": bool(full_wikitext_eval),
            "full_validation_ppl": bool(full_validation_ppl),
            "lr": float(lr),
            "betas": (0.9, 0.999),
            "weight_decay": 0.01,
            "warmup_steps": int(warmup_steps),
            "schedule_mode": str(schedule_mode or "constant").strip().lower(),
            "lora_r": 8,
            "lora_alpha": 16,
            "lora_dropout": 0.05,
            "lbw_stats_freq": 10,
            "lbw_stress_th": 1.1,
            "lbw_spike_th": 1.5,
            "lbw_rec_fast": 0.01,
            "lbw_ema_decay": 0.95,
        }
        lr_sweep = _parse_float_sweep(lr_sweep_text, [1e-3, 5e-4])
        step_sweep = _parse_int_sweep(step_sweep_text, [100, 200])
        lora_r_sweep = _parse_int_sweep(lora_r_sweep_text, [4, 8, 16])
        scenarios = _build_ablation_scenarios(
            selected_ablations=list(selected_ablations or ["optimizer"]),
            base_config=base_config,
            lr_sweep=lr_sweep,
            step_sweep=step_sweep,
            lora_r_sweep=lora_r_sweep,
        )

        if device.type == "cpu" and (
            len(scenarios) > 1
            or int(max_steps) > 1
            or int(train_chars) > 20_000
            or int(eval_chars) > 8_000
            or bool(full_wikitext_train)
            or bool(full_wikitext_eval)
            or bool(full_validation_ppl)
        ):
            yield (
                "This Space is currently on `cpu-basic`. CPU ablation mode is capped to one optimizer scenario, "
                "1 step, 20k train chars, 8k eval chars, and sampled validation. Switch the Space hardware to GPU for ablations.",
                None,
                None,
                None,
            )
            return
        if device.type == "cuda" and bool(run_lbw_guard) and torch.cuda.device_count() > 1:
            yield (
                "LBW Guard should run with one visible GPU. Set the Space to single-GPU hardware or restrict CUDA_VISIBLE_DEVICES.",
                None,
                None,
                None,
            )
            return

        resolved_model = str(model_name).strip() or "Qwen/Qwen2.5-0.5B"
        _append_log(logs, f"Device: {device_name}")
        if device.type == "cuda":
            _append_log(logs, f"GPU: {torch.cuda.get_device_name(0)}")
        _append_log(logs, f"Selected ablations: {', '.join(selected_ablations or ['optimizer'])}")
        _append_log(logs, f"Optimizers: {', '.join(optimizers)}")
        yield _ablation_status_markdown(
            device_name=device_name,
            rows=rows,
            logs=logs,
            phase="Loading tokenizer",
            plan=scenarios,
        ), None, None, None

        tokenizer = AutoTokenizer.from_pretrained(resolved_model, use_fast=True)
        if tokenizer.pad_token is None:
            tokenizer.pad_token = tokenizer.eos_token
        data_cache: dict[tuple[int, int | None, int | None], dict[str, dict[str, Any]]] = {}

        for scenario_item in scenarios:
            cfg = scenario_item["config"]
            train_cap = None if cfg["full_wikitext_train"] else int(cfg["max_chars"])
            eval_cap = None if cfg["full_wikitext_eval"] else int(cfg["eval_chars"])
            cache_key = (int(cfg["seq_len"]), train_cap, eval_cap)
            if cache_key not in data_cache:
                data_cache[cache_key] = {
                    "train": _build_wikitext_chunks(
                        tokenizer,
                        split="train",
                        max_chars=train_cap,
                        seq_len=int(cfg["seq_len"]),
                        logs=logs,
                    ),
                    "eval": _build_wikitext_chunks(
                        tokenizer,
                        split="validation",
                        max_chars=eval_cap,
                        seq_len=int(cfg["seq_len"]),
                        logs=logs,
                    ),
                }

            _append_log(logs, f"=== Scenario: {scenario_item['label']} ===")
            for optimizer_name in optimizers:
                _append_log(logs, f"--- {optimizer_name} ---")
                yield _ablation_status_markdown(
                    device_name=device_name,
                    rows=rows,
                    logs=logs,
                    phase=f"Running {scenario_item['label']} / {optimizer_name}",
                    plan=scenarios,
                ), None, None, None
                for event in _run_ablation_optimizer_events(
                    scenario_item=scenario_item,
                    optimizer_name=optimizer_name,
                    model_name=resolved_model,
                    train_chunks=data_cache[cache_key]["train"],
                    eval_chunks=data_cache[cache_key]["eval"],
                    device=device,
                    logs=logs,
                ):
                    if event.get("type") == "result":
                        rows.append(event["result"])
                    yield _ablation_status_markdown(
                        device_name=device_name,
                        rows=rows,
                        logs=logs,
                        phase=f"Running {scenario_item['label']} / {optimizer_name}",
                        plan=scenarios,
                    ), None, None, None

        gains = _build_ablation_gain_rows(rows)
        payload = {
            "source": "HF Ablation Matrix Runner",
            "based_on_colab": "LBW_Guard_Ablation_Test_COLAB.ipynb",
            "model_name": resolved_model,
            "device": device_name,
            "optimizers": optimizers,
            "selected_ablations": list(selected_ablations or ["optimizer"]),
            "base_config": base_config,
            "scenarios": scenarios,
            "results": rows,
            "gains": gains,
            "logs": logs,
        }
        json_path = run_dir / "lbw_guard_hf_ablation_matrix_results.json"
        metrics_path = run_dir / "lbw_guard_hf_ablation_matrix_metrics.csv"
        gains_path = run_dir / "lbw_guard_hf_ablation_matrix_gains.csv"
        json_path.write_text(json.dumps(payload, indent=2), encoding="utf-8")
        _write_csv(metrics_path, rows)
        _write_csv(gains_path, gains)
        _append_log(logs, f"Wrote {metrics_path}")
        _append_log(logs, f"Wrote {gains_path}")
        yield (
            _ablation_status_markdown(device_name=device_name, rows=rows, logs=logs, phase="Complete", plan=scenarios),
            str(json_path),
            str(metrics_path),
            str(gains_path),
        )
    except Exception:
        error_text = traceback.format_exc()
        error_path = run_dir / "error.txt"
        error_path.write_text(error_text + "\n\n" + "\n".join(logs), encoding="utf-8")
        yield f"Run failed.\n\n```text\n{error_text}\n```", str(error_path), None, None


INTRO = """
# LBW Guard HF Evaluation Runner

This private Space has two HF-specific runners based on the customer Colab notebooks:

- **Quick Comparison**: a short AdamW vs `lbw_guard` WikiText LoRA run.
- **Ablation Matrix**: a selectable scenario sweep across optimizer, LR, schedule, steps, data, and LoRA rank.

Each run writes JSON and CSV artifacts. GPU hardware is recommended; CPU mode is only for tiny smoke checks.
"""


with gr.Blocks(title="LBW Guard HF Evaluation Runner") as demo:
    gr.Markdown(INTRO)
    with gr.Tabs():
        with gr.Tab("Quick Comparison"):
            gr.Markdown(
                "Runs the HF version of the Easy Test flow: one shared WikiText slice, then AdamW and optional "
                "`lbw_guard` LoRA training with final perplexity and gain CSV output."
            )
            with gr.Row():
                easy_model_name = gr.Textbox(value="TinyLlama/TinyLlama_v1.1", label="Model")
                easy_run_lbw_guard = gr.Checkbox(value=True, label="Run LBW Guard comparison")
            with gr.Row():
                easy_max_steps = gr.Slider(1, 1000, value=5, step=1, label="Optimizer steps")
                easy_eval_every = gr.Slider(1, 200, value=5, step=1, label="Eval every")
                easy_eval_batches = gr.Slider(1, 128, value=8, step=1, label="Eval batches")
            with gr.Row():
                easy_seq_len = gr.Dropdown([64, 128, 256, 512], value=64, label="Sequence length")
                easy_batch_size = gr.Slider(1, 8, value=1, step=1, label="Batch size")
                easy_lr = gr.Number(value=5e-4, label="Learning rate")
            with gr.Row():
                easy_train_chars = gr.Slider(5_000, 2_000_000, value=20_000, step=5_000, label="Train char cap")
                easy_eval_chars = gr.Slider(1_000, 500_000, value=8_000, step=1_000, label="Eval char cap")
                easy_seed = gr.Number(value=42, precision=0, label="Seed")
            with gr.Row():
                easy_full_wikitext_train = gr.Checkbox(value=False, label="Full WikiText train")
                easy_full_wikitext_eval = gr.Checkbox(value=False, label="Full WikiText eval")
                easy_full_validation_ppl = gr.Checkbox(value=False, label="Full validation PPL")
            easy_run_button = gr.Button("Run Quick Comparison", variant="primary")
            easy_summary = gr.Markdown()
            easy_json_file = gr.File(label="Raw JSON")
            easy_results_file = gr.File(label="Results CSV")
            easy_gains_file = gr.File(label="Gains CSV")

            easy_run_button.click(
                fn=run_easy_test,
                inputs=[
                    easy_model_name,
                    easy_run_lbw_guard,
                    easy_max_steps,
                    easy_eval_every,
                    easy_eval_batches,
                    easy_seq_len,
                    easy_batch_size,
                    easy_train_chars,
                    easy_eval_chars,
                    easy_full_wikitext_train,
                    easy_full_wikitext_eval,
                    easy_full_validation_ppl,
                    easy_lr,
                    easy_seed,
                ],
                outputs=[easy_summary, easy_json_file, easy_results_file, easy_gains_file],
            )

        with gr.Tab("Ablation Matrix"):
            gr.Markdown(
                "Runs the HF version of the ablation flow: build selected scenarios, run AdamW and optional "
                "`lbw_guard` for each scenario, then export metrics and LBW-vs-AdamW gains."
            )
            with gr.Row():
                ablation_model_name = gr.Textbox(value="Qwen/Qwen2.5-0.5B", label="Model")
                ablation_run_lbw_guard = gr.Checkbox(value=True, label="Run LBW Guard comparison")
            selected_ablations = gr.CheckboxGroup(
                choices=["optimizer", "lr", "schedule", "steps", "data", "lora"],
                value=["optimizer"],
                label="Ablations",
            )
            with gr.Row():
                ablation_max_steps = gr.Slider(1, 1000, value=200, step=1, label="Base optimizer steps")
                ablation_eval_every = gr.Slider(1, 200, value=50, step=1, label="Eval every")
                ablation_eval_batches = gr.Slider(1, 128, value=8, step=1, label="Eval batches")
            with gr.Row():
                ablation_seq_len = gr.Dropdown([64, 128, 256, 512], value=64, label="Sequence length")
                ablation_batch_size = gr.Slider(1, 8, value=1, step=1, label="Batch size")
                ablation_lr = gr.Number(value=5e-4, label="Base learning rate")
            with gr.Row():
                ablation_train_chars = gr.Slider(5_000, 2_000_000, value=20_000, step=5_000, label="Train char cap")
                ablation_eval_chars = gr.Slider(1_000, 500_000, value=8_000, step=1_000, label="Eval char cap")
                ablation_seed = gr.Number(value=42, precision=0, label="Seed")
            with gr.Row():
                ablation_schedule_mode = gr.Dropdown(["constant", "cosine"], value="constant", label="Base schedule")
                ablation_warmup_steps = gr.Slider(0, 100, value=10, step=1, label="Warmup steps")
            with gr.Row():
                ablation_full_wikitext_train = gr.Checkbox(value=False, label="Full WikiText train")
                ablation_full_wikitext_eval = gr.Checkbox(value=False, label="Full WikiText eval")
                ablation_full_validation_ppl = gr.Checkbox(value=False, label="Full validation PPL")
            with gr.Row():
                lr_sweep_text = gr.Textbox(value="1e-3, 5e-4", label="LR sweep")
                step_sweep_text = gr.Textbox(value="100, 200", label="Step sweep")
                lora_r_sweep_text = gr.Textbox(value="4, 8, 16", label="LoRA r sweep")
            ablation_run_button = gr.Button("Run Ablation Matrix", variant="primary")
            ablation_summary = gr.Markdown()
            ablation_json_file = gr.File(label="Raw JSON")
            ablation_metrics_file = gr.File(label="Metrics CSV")
            ablation_gains_file = gr.File(label="Gains CSV")

            ablation_run_button.click(
                fn=run_ablation_test,
                inputs=[
                    ablation_model_name,
                    selected_ablations,
                    ablation_run_lbw_guard,
                    ablation_max_steps,
                    ablation_eval_every,
                    ablation_eval_batches,
                    ablation_seq_len,
                    ablation_batch_size,
                    ablation_train_chars,
                    ablation_eval_chars,
                    ablation_full_wikitext_train,
                    ablation_full_wikitext_eval,
                    ablation_full_validation_ppl,
                    ablation_lr,
                    ablation_schedule_mode,
                    ablation_warmup_steps,
                    ablation_seed,
                    lr_sweep_text,
                    step_sweep_text,
                    lora_r_sweep_text,
                ],
                outputs=[ablation_summary, ablation_json_file, ablation_metrics_file, ablation_gains_file],
            )


if __name__ == "__main__":
    demo.queue(default_concurrency_limit=1).launch()