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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
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
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
import contextlib
import json
import os
import re
import shutil
import signal
import subprocess
import threading
import time
import urllib.request
from datetime import datetime, timezone
from pathlib import Path
from typing import Any

from fastapi import Depends, FastAPI, File, Header, HTTPException, UploadFile
from pydantic import BaseModel, Field, model_validator

app = FastAPI(title="Qwen 3.5 SFT Fine-Tuning API", version="2.0.0")

API_SECRET = os.environ.get("API_SECRET", "")
WORKSPACE = Path("/workspace")
DATA_DIR = WORKSPACE / "data"
OUTPUT_DIR = WORKSPACE / "output"
CONFIG_DIR = WORKSPACE / "config"
LOG_FILE = WORKSPACE / "training.log"

DATA_DIR.mkdir(parents=True, exist_ok=True)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
CONFIG_DIR.mkdir(parents=True, exist_ok=True)

# Global event: set when training + HF push are fully done.
# The SIGTERM handler waits on this before allowing the process to exit,
# so the container is never killed while a push is in flight.
_training_done = threading.Event()
_training_done.set()  # starts "done" (no training in progress)
_training_thread: threading.Thread | None = None


# ---------------------------------------------------------------------------
# Multi-node readiness barrier
# ---------------------------------------------------------------------------
# In a cluster, every node pre-downloads model/dataset independently.
# Once done, workers POST to the master's /barrier/ready.  The master
# counts itself + all workers, then every node polls /barrier/wait until
# the count reaches num_nodes.  Only then does any node start torchrun.



# ---------------------------------------------------------------------------
# HuggingFace Hub push helpers
# ---------------------------------------------------------------------------


def _ensure_readme_metadata(checkpoint_path: Path, model_id: str):
    """Ensure the checkpoint has a README.md with correct base_model metadata.

    ms-swift either writes a local cache path as base_model (which HF rejects)
    or the README may be missing entirely.  This function fixes existing READMEs
    or creates a minimal one so HF Hub always has the base_model field set to
    the canonical model ID (e.g. ``Qwen/Qwen3.5-4B``).
    """
    readme = checkpoint_path / "README.md"

    if readme.exists():
        text = readme.read_text(encoding="utf-8")
        fm_match = re.match(r"^---\n(.*?\n)---", text, re.DOTALL)

        if fm_match:
            front_matter = fm_match.group(1)
            original = front_matter

            # Fix base_model values that are local paths
            front_matter = re.sub(
                r'(base_model\s*:\s*)["\']?(/[^\s"\']+)["\']?',
                rf"\1{model_id}",
                front_matter,
            )

            # If base_model is missing entirely, add it
            if "base_model" not in front_matter:
                front_matter = f"base_model: {model_id}\n{front_matter}"

            if front_matter != original:
                text = f"---\n{front_matter}---" + text[fm_match.end() :]
                readme.write_text(text, encoding="utf-8")
                print(
                    f"[HF Push] Fixed README metadata in {checkpoint_path.name} → base_model: {model_id}",
                    flush=True,
                )
            return

    # No README or no front-matter — create a minimal one
    readme.write_text(
        f"---\nbase_model: {model_id}\ntags:\n- fine-tuned\n- ms-swift\nlibrary_name: transformers\n---\n\n"
        f"# {checkpoint_path.name}\n\nFine-tuned from [{model_id}](https://huggingface.co/{model_id}) using [ms-swift](https://github.com/modelscope/ms-swift).\n",
        encoding="utf-8",
    )
    print(
        f"[HF Push] Created README.md for {checkpoint_path.name} with base_model: {model_id}",
        flush=True,
    )


def _fix_adapter_config_base_model(checkpoint_path: Path, model_id: str):
    """Rewrite base_model_name_or_path in adapter_config.json from a local
    cache path (e.g. /workspace/hf-cache/hub/models--Qwen--Qwen3.5-9B/...)
    to the canonical HF model ID (e.g. Qwen/Qwen3.5-9B)."""
    adapter_cfg = checkpoint_path / "adapter_config.json"
    if not adapter_cfg.exists():
        return
    try:
        cfg = json.loads(adapter_cfg.read_text(encoding="utf-8"))
        base = cfg.get("base_model_name_or_path", "")
        if base.startswith("/") or "--" in base:
            cfg["base_model_name_or_path"] = model_id
            adapter_cfg.write_text(
                json.dumps(cfg, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
            )
            print(
                f"[HF Push] Fixed adapter_config.json base_model_name_or_path: {base}{model_id}",
                flush=True,
            )
    except Exception as e:
        print(f"[HF Push] Warning: could not fix adapter_config.json: {e}", flush=True)


CHECKPOINT_IGNORE_PATTERNS = [
    "optimizer.pt",
    "optim_states.pt",
    "scheduler.pt",
    "rng_state*.pth",
    "global_step*",
    "zero_to_fp32.py",
    "*.distcp",
]


def _checkpoint_is_ready(checkpoint_path: Path, settle_seconds: float = 30) -> bool:
    """Return True only when no file in the checkpoint has been modified recently.

    This prevents uploading a half-written checkpoint while the trainer is
    still flushing large .safetensors / optimizer files to disk.
    """
    cutoff = time.time() - settle_seconds
    try:
        for f in checkpoint_path.rglob("*"):
            if f.is_file() and f.stat().st_mtime > cutoff:
                return False
    except OSError:
        return False
    return True


def _hf_push_checkpoint(
    checkpoint_path: Path, repo_id: str, hf_token: str, commit_message: str, model_id: str = ""
):
    """Push a single checkpoint directory to HuggingFace Hub."""
    try:
        if model_id:
            _ensure_readme_metadata(checkpoint_path, model_id)
            _fix_adapter_config_base_model(checkpoint_path, model_id)

        from huggingface_hub import HfApi

        api = HfApi(token=hf_token)
        api.create_repo(repo_id, exist_ok=True, private=True)
        api.upload_folder(
            folder_path=str(checkpoint_path),
            repo_id=repo_id,
            commit_message=commit_message,
            path_in_repo=checkpoint_path.name,
            ignore_patterns=CHECKPOINT_IGNORE_PATTERNS,
        )
        print(f"[HF Push] Pushed {checkpoint_path.name} to {repo_id}")
    except Exception as e:
        print(f"[HF Push] Failed to push {checkpoint_path.name}: {e}")


_HF_PUSH_STATE_FILE = OUTPUT_DIR / ".hf_push_state.json"


def _save_push_state(repo_id: str, hf_token: str, model_id: str, checkpoint_name: str):
    """Persist push intent so it can be recovered after a crash/restart."""
    try:
        _HF_PUSH_STATE_FILE.write_text(
            json.dumps(
                {
                    "repo_id": repo_id,
                    "hf_token": hf_token,
                    "model_id": model_id,
                    "checkpoint_name": checkpoint_name,
                    "created_at": datetime.now(timezone.utc).isoformat(),
                }
            )
        )
    except Exception as e:
        print(f"[HF Push] Warning: could not save push state: {e}", flush=True)


def _clear_push_state():
    with contextlib.suppress(Exception):
        _HF_PUSH_STATE_FILE.unlink(missing_ok=True)


def _verify_hf_push(repo_id: str, hf_token: str, checkpoint_name: str | None = None) -> bool:
    """Verify that the model was actually pushed to HuggingFace by checking for key files."""
    try:
        from huggingface_hub import HfApi

        api = HfApi(token=hf_token)
        # list_repo_files returns strings (file paths relative to repo root)
        files = list(api.list_repo_files(repo_id))

        prefix = f"{checkpoint_name}/" if checkpoint_name else ""
        has_config = f"{prefix}config.json" in files
        has_weights = any(
            f.startswith(prefix) and (f.endswith(".safetensors") or f.endswith(".bin"))
            for f in files
        )
        return has_config and has_weights
    except Exception as e:
        print(f"[HF Push] Verification failed: {e}", flush=True)
        return False


def _hf_push_final_model(
    output_dir: Path,
    repo_id: str,
    hf_token: str,
    model_id: str = "",
    tuner_type: str = "full",
    max_retries: int = 3,
) -> bool:
    """Push the final trained model to HuggingFace Hub as the repo root.

    Returns True if the push succeeded and was verified, False otherwise.
    Retries on transient failures.
    """
    best = _find_best_checkpoint(output_dir)
    if not best:
        print("[HF Push] No checkpoint found for final push", flush=True)
        return False

    _save_push_state(repo_id, hf_token, model_id, best.name)

    upload_dir = best
    if model_id:
        _ensure_readme_metadata(upload_dir, model_id)

    from huggingface_hub import HfApi

    for attempt in range(1, max_retries + 1):
        try:
            print(
                f"[HF Push] Final push attempt {attempt}/{max_retries}: {best.name} -> {repo_id}",
                flush=True,
            )
            api = HfApi(token=hf_token)
            api.create_repo(repo_id, exist_ok=True, private=True)
            api.upload_folder(
                folder_path=str(upload_dir),
                repo_id=repo_id,
                commit_message=f"Final model from {best.name}",
                ignore_patterns=CHECKPOINT_IGNORE_PATTERNS,
            )
            print("[HF Push] Upload complete, verifying...", flush=True)

            if _verify_hf_push(repo_id, hf_token):
                print(
                    f"[HF Push] Verified: final model from {best.name} is on {repo_id}", flush=True
                )
                _clear_push_state()
                return True
            else:
                print(f"[HF Push] Verification failed after upload (attempt {attempt})", flush=True)
        except Exception as e:
            print(f"[HF Push] Attempt {attempt} failed: {e}", flush=True)

        if attempt < max_retries:
            wait = 15 * attempt
            print(f"[HF Push] Retrying in {wait}s...", flush=True)
            time.sleep(wait)

    print(f"[HF Push] CRITICAL: All {max_retries} attempts to push final model failed!", flush=True)
    return False


def _recover_pending_push():
    """On startup, check if a previous push was interrupted and retry it."""
    if not _HF_PUSH_STATE_FILE.exists():
        return

    try:
        push_state = json.loads(_HF_PUSH_STATE_FILE.read_text())
    except Exception:
        _clear_push_state()
        return

    repo_id = push_state.get("repo_id")
    hf_token = push_state.get("hf_token")
    model_id = push_state.get("model_id", "")
    checkpoint_name = push_state.get("checkpoint_name", "")

    if not repo_id or not hf_token:
        _clear_push_state()
        return

    print(f"[HF Push] Recovering interrupted push: {checkpoint_name} -> {repo_id}", flush=True)

    if _verify_hf_push(repo_id, hf_token):
        print("[HF Push] Recovery: push already completed (verified on HF)", flush=True)
        _clear_push_state()
        return

    if _verify_hf_push(repo_id, hf_token, checkpoint_name):
        print(
            f"[HF Push] Recovery: checkpoint {checkpoint_name} already on HF (verified)", flush=True
        )
        _clear_push_state()
        return

    success = _hf_push_final_model(OUTPUT_DIR, repo_id, hf_token, model_id=model_id)
    if success:
        print("[HF Push] Recovery push succeeded", flush=True)
    else:
        print("[HF Push] Recovery push FAILED — manual intervention needed", flush=True)


def _find_best_checkpoint(output_dir: Path) -> Path | None:
    """Find the best checkpoint in the output directory.

    Prefers the checkpoint marked as best_model_checkpoint in trainer_state.json
    (written when load_best_model_at_end is enabled). Falls back to the most
    recent checkpoint by modification time.
    """
    best_from_state = _best_checkpoint_from_trainer_state(output_dir)
    if best_from_state and best_from_state.exists():
        print(f"[Checkpoint] Using best by eval metric: {best_from_state}", flush=True)
        return best_from_state

    candidates = sorted(output_dir.glob("*/checkpoint-*"), key=lambda p: p.stat().st_mtime)
    if candidates:
        return candidates[-1]
    candidates = sorted(output_dir.glob("checkpoint-*"), key=lambda p: p.stat().st_mtime)
    return candidates[-1] if candidates else None


def _best_checkpoint_from_trainer_state(output_dir: Path) -> Path | None:
    """Read trainer_state.json to find the checkpoint with the best eval metric."""
    for state_file in output_dir.rglob("trainer_state.json"):
        try:
            ts = json.loads(state_file.read_text(encoding="utf-8"))
            best = ts.get("best_model_checkpoint")
            if best:
                best_path = Path(best)
                if best_path.exists():
                    return best_path
                relative = output_dir / best_path.name
                if relative.exists():
                    return relative
        except Exception:
            continue
    return None


def _find_latest_checkpoint(output_dir: Path) -> Path | None:
    """Find the most recent checkpoint by step number across all run directories."""
    all_ckpts: list[tuple[int, Path]] = []
    for pattern in ("*/checkpoint-*", "checkpoint-*"):
        for p in output_dir.glob(pattern):
            m = re.search(r"checkpoint-(\d+)$", p.name)
            if m and p.is_dir():
                all_ckpts.append((int(m.group(1)), p))
    if not all_ckpts:
        return None
    all_ckpts.sort(key=lambda t: t[0])
    return all_ckpts[-1][1]


class _HFPushQueue:
    """Watches the output directory for new checkpoints and pushes them to HF.

    Uploads are serialized through a single worker thread to prevent concurrent
    commits to the same repo (which cause commit-race failures on HF Hub).

    Checkpoints are enqueued as soon as they are discovered so they cannot be
    deleted by the trainer (save_total_limit rotation) before the upload starts.
    The upload worker waits for the checkpoint to settle before pushing.
    """

    def __init__(
        self,
        repo_id: str,
        hf_token: str,
        model_id: str = "",
        output_dir: Path = OUTPUT_DIR,
        poll_interval: float = 15,
    ):
        self.repo_id = repo_id
        self.hf_token = hf_token
        self.model_id = model_id
        self._output_dir = output_dir
        self._poll_interval = poll_interval
        self._pushed: set[str] = set()
        self._queue: list[Path] = []
        self._queue_lock = threading.Lock()
        self._stop = threading.Event()
        self._has_work = threading.Event()

        self._watcher = threading.Thread(target=self._watch, daemon=False, name="hf-push-watcher")
        self._worker = threading.Thread(
            target=self._upload_worker, daemon=False, name="hf-push-worker"
        )
        self._watcher.start()
        self._worker.start()

    def _discover_checkpoints(self) -> list[Path]:
        """Find all checkpoint-* dirs under output_dir (any nesting depth)."""
        found = []
        for pattern in ("*/checkpoint-*", "checkpoint-*"):
            for p in self._output_dir.glob(pattern):
                if p.is_dir() and p.name not in self._pushed:
                    found.append(p)
        return sorted(found, key=lambda p: p.stat().st_mtime)

    def _watch(self):
        """Poll for new checkpoints and enqueue immediately on discovery."""
        while not self._stop.is_set():
            self._stop.wait(self._poll_interval)
            if self._stop.is_set():
                break
            for ckpt in self._discover_checkpoints():
                self._pushed.add(ckpt.name)
                self._enqueue(ckpt)

    def _enqueue(self, checkpoint_path: Path):
        with self._queue_lock:
            self._queue.append(checkpoint_path)
        self._has_work.set()
        print(f"[HF Push] Queued upload for {checkpoint_path.name}", flush=True)

    def _upload_worker(self):
        """Sequentially process queued uploads — one commit at a time.

        Waits for each checkpoint to settle (files stop changing) before
        uploading. If the checkpoint directory is deleted before it settles
        (e.g. by save_total_limit rotation), the upload is skipped.
        """
        while True:
            self._has_work.wait(timeout=5)
            self._has_work.clear()

            while True:
                with self._queue_lock:
                    if not self._queue:
                        break
                    ckpt = self._queue.pop(0)

                if not ckpt.exists():
                    print(f"[HF Push] {ckpt.name} was deleted before upload, skipping", flush=True)
                    continue

                for _ in range(12):
                    if _checkpoint_is_ready(ckpt):
                        break
                    if not ckpt.exists():
                        break
                    time.sleep(5)

                if not ckpt.exists():
                    print(f"[HF Push] {ckpt.name} was deleted before upload, skipping", flush=True)
                    continue

                _hf_push_checkpoint(
                    ckpt, self.repo_id, self.hf_token, f"Checkpoint {ckpt.name}", self.model_id
                )

            if self._stop.is_set():
                with self._queue_lock:
                    if not self._queue:
                        break

    def stop_and_wait(self, timeout: float = 600):
        """Stop watching and drain any remaining uploads."""
        self._stop.set()
        self._watcher.join(timeout=10)

        for ckpt in self._discover_checkpoints():
            self._pushed.add(ckpt.name)
            self._enqueue(ckpt)

        self._has_work.set()
        self._worker.join(timeout=timeout)
        print("[HF Push] All uploads done.", flush=True)


# ---------------------------------------------------------------------------
# Webhook helper
# ---------------------------------------------------------------------------


class _PostRedirectHandler(urllib.request.HTTPRedirectHandler):
    """Preserve POST method and body through 301/302/307/308 redirects."""

    def redirect_request(self, req, fp, code, msg, headers, newurl):
        new_req = urllib.request.Request(
            newurl,
            data=req.data,
            headers=dict(req.headers),
            method="POST",
        )
        return new_req


def _fire_webhook(url: str, secret: str, payload: dict):
    """POST JSON to the dashboard webhook."""
    if url.startswith("http://") and "localhost" not in url and "127.0.0.1" not in url:
        url = url.replace("http://", "https://", 1)
    try:
        data = json.dumps(payload).encode()
        req = urllib.request.Request(
            url,
            data=data,
            headers={
                "Content-Type": "application/json",
                "X-Webhook-Secret": secret,
            },
            method="POST",
        )
        opener = urllib.request.build_opener(_PostRedirectHandler)
        with opener.open(req, timeout=15) as resp:
            print(f"[Webhook] Fired to {url} — status {resp.status}")
    except Exception as e:
        print(f"[Webhook] Failed to fire to {url}: {e}")


def _fire_completion_webhook(config: dict, hf_push_ok: bool = False):
    """Notify the dashboard that training completed successfully.

    Only called after a real successful training run (returncode 0)
    AND after the final HF push has been attempted.
    """
    url = config.get("webhook_url")
    secret = config.get("webhook_secret") or ""
    job_id = config.get("training_job_id")
    if not url or not job_id:
        return
    _fire_webhook(
        url,
        secret,
        {
            "jobId": job_id,
            "status": "completed",
            "hfPushOk": hf_push_ok,
        },
    )


# ---------------------------------------------------------------------------
# Training state
# ---------------------------------------------------------------------------


class TrainingState:
    def __init__(self):
        self.status = "idle"
        self.started_at: str | None = None
        self.finished_at: str | None = None
        self.error: str | None = None
        self.pid: int | None = None
        self.config: dict = {}
        self.hf_push_status: str | None = None  # None, "pushing", "success", "failed"

    def to_dict(self):
        tail = ""
        if LOG_FILE.exists():
            try:
                lines = LOG_FILE.read_text().splitlines()
                tail = "\n".join(lines[-50:])
            except Exception:
                pass
        return {
            "status": self.status,
            "started_at": self.started_at,
            "finished_at": self.finished_at,
            "error": self.error,
            "pid": self.pid,
            "config": self.config,
            "log_tail": tail,
            "hf_push_status": self.hf_push_status,
            "push_in_progress": not _training_done.is_set(),
        }


state = TrainingState()


def verify_secret(authorization: str = Header(...)):
    if not API_SECRET:
        raise HTTPException(500, "API_SECRET env var not set on server")
    expected = f"Bearer {API_SECRET}"
    if authorization != expected:
        raise HTTPException(401, "Invalid or missing API secret")


def _to_snake_case(key: str) -> str:
    """Convert camelCase, PascalCase, UPPER_CASE, or kebab-case to snake_case."""
    key = key.replace("-", "_")
    key = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", key)
    key = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", key)
    return key.lower()


class TrainRequest(BaseModel):
    model: str = Field("Qwen/Qwen3.5-4B", description="HuggingFace model ID or local path")
    dataset: str | None = Field(
        None,
        description="HuggingFace dataset ID (e.g. 'tatsu-lab/alpaca') or leave empty to use uploaded JSONL",
    )
    dataset_subset: str | None = Field(None, description="Dataset subset/config name")
    val_split_ratio: float | None = Field(
        None,
        ge=0.0,
        le=0.5,
        description="Fraction of training data to hold out for validation (0.0-0.5). Ignored when val_dataset is set",
    )
    val_dataset: str | None = Field(
        None, description="HuggingFace dataset ID for validation, or leave empty"
    )
    num_epochs: int = Field(3, ge=1, le=100, description="Number of training epochs")
    batch_size: int = Field(1, ge=1, description="Per-device training batch size")
    grad_accum: int = Field(
        4,
        ge=1,
        description="Gradient accumulation steps (effective batch = batch_size * grad_accum * num_gpus)",
    )
    learning_rate: float = Field(2e-5, gt=0, description="Peak learning rate")
    max_length: int = Field(
        2048,
        ge=128,
        description="Max sequence length in tokens. Rows exceeding this are dropped. Qwen 3.5 supports up to 32768",
    )
    save_steps: int = Field(10, ge=1, description="Save a checkpoint every N steps")
    eval_steps: int | None = Field(
        None, ge=1, description="Run evaluation every N steps. Defaults to save_steps if not set"
    )
    save_total_limit: int = Field(
        2, ge=1, description="Max number of checkpoints to keep on disk (oldest are deleted)"
    )
    logging_steps: int = Field(5, ge=1, description="Log metrics every N steps")
    tuner_type: str = Field("full", description="Tuning strategy (only 'full' is supported)")
    warmup_ratio: float = Field(
        0.1,
        ge=0.0,
        le=1.0,
        description="Fraction of total steps used for linear LR warmup (0.0-1.0)",
    )
    lr_scheduler_type: str = Field(
        "cosine",
        description="LR scheduler: cosine, linear, cosine_with_restarts, polynomial, constant, constant_with_warmup, inverse_sqrt",
    )
    weight_decay: float = Field(0.1, ge=0.0, description="L2 weight decay coefficient")
    max_grad_norm: float = Field(
        1.0, ge=0.0, description="Max gradient norm for clipping (0 = no clipping)"
    )
    optimizer: str = Field(
        "adamw_torch",
        description="Optimizer: adamw_torch, adamw_torch_fused, adamw_8bit, paged_adamw_8bit, paged_adamw_32bit, adafactor, sgd",
    )
    seed: int = Field(42, ge=0, description="Random seed for reproducibility")
    neftune_alpha: float | None = Field(
        None,
        ge=0.0,
        description="NEFTune noise alpha for embedding regularization (null/0 = off, try 5-15)",
    )
    packing: bool = Field(
        False,
        description="Pack multiple samples into uniform-length sequences to reduce padding waste",
    )
    shuffle_dataset: bool = Field(
        False,
        description="Explicitly shuffle the dataset before training. The dataloader already uses a random sampler by default, so this adds an extra pre-shuffle pass",
    )
    lazy_tokenize: bool = Field(
        True,
        description="Tokenize samples on-the-fly during training instead of pre-tokenizing the entire dataset into memory. Prevents OOM on large datasets",
    )
    dataset_num_proc: int = Field(
        4,
        ge=1,
        le=128,
        description="Number of processes for dataset preprocessing. Higher values speed up tokenization but use more CPU/RAM",
    )
    attn_impl: str = Field(
        "flash_attn",
        description="Attention implementation: 'flash_attn' (recommended, O(n) memory), 'sdpa' (PyTorch native), or 'eager' (naive, O(n^2) memory). Qwen3.5 has full-attention layers that OOM without flash_attn at long sequences",
    )
    deepspeed: str | None = Field(
        None,
        description="DeepSpeed config: 'zero2', 'zero3', or null. Auto-set to 'zero2' when num_gpus > 1",
    )
    num_gpus: int | None = Field(
        None, description="Number of GPUs to use. null = auto-detect all available GPUs"
    )
    num_nodes: int | None = Field(
        None,
        ge=1,
        le=64,
        description="Number of nodes for multi-node training. null = auto-detect from NUM_NODES env var (set by RunPod Instant Clusters). 1 = single-node",
    )
    node_rank: int | None = Field(
        None,
        ge=0,
        description="This node's rank in the cluster. null = auto-detect from NODE_RANK env var. 0 = primary node",
    )
    master_addr: str | None = Field(
        None,
        description="Primary node address for distributed training. null = auto-detect from MASTER_ADDR env var",
    )
    master_port: str | None = Field(
        None,
        description="Primary node port for distributed training. null = auto-detect from MASTER_PORT env var",
    )
    gradient_checkpointing: bool = Field(
        True,
        description="Enable gradient checkpointing to reduce VRAM at the cost of ~20% slower training",
    )
    use_flash_ckpt: bool = Field(
        False,
        description="Use flash checkpointing (experimental, requires dlrover). Disabled by default due to dlrover/ms-swift compatibility issues",
    )
    resume_from_checkpoint: str | None = Field(
        None,
        description=(
            "Resume training from a checkpoint. Values: "
            "'auto' = find the latest local checkpoint automatically; "
            "a local path like '/workspace/output/v0-.../checkpoint-100'; "
            "or null to start fresh"
        ),
    )
    hf_token: str | None = Field(None, description="HuggingFace token (overrides HF_TOKEN env var)")
    hf_repo_id: str | None = Field(
        None, description="HuggingFace repo to push checkpoints/final model (e.g. 'org/model-name')"
    )
    wandb_project: str | None = Field(None, description="W&B project name (enables wandb logging)")
    wandb_entity: str | None = Field(None, description="W&B entity/team name")
    wandb_run_name: str | None = Field(None, description="W&B run name")
    wandb_api_key: str | None = Field(
        None, description="W&B API key (overrides WANDB_API_KEY env var)"
    )
    webhook_url: str | None = Field(None, description="URL to POST when training completes")
    webhook_secret: str | None = Field(None, description="Secret sent in X-Webhook-Secret header")
    training_job_id: str | None = Field(None, description="Dashboard job ID passed back in webhook")
    max_pixels: int | None = Field(
        None,
        ge=1024,
        description="Max pixels per image for multimodal training (controls image resolution/VRAM). e.g. 1003520 for ~1M pixels. null = model default",
    )
    min_pixels: int | None = Field(
        None,
        ge=256,
        description="Min pixels per image for multimodal training. null = model default",
    )
    early_stopping_patience: int | None = Field(
        None,
        ge=1,
        description="Stop training when eval loss hasn't improved for this many eval rounds. Requires validation data (val_dataset or val_split_ratio). null = disabled",
    )
    early_stopping_threshold: float = Field(
        0.0,
        ge=0.0,
        description="Minimum eval loss improvement to count as 'better' (0.0 = any improvement counts)",
    )
    extra_args: dict | None = Field(
        None,
        description='Extra args passed directly to swift sft (e.g. {"truncation_strategy": "truncation_left"})',
    )

    _VALID_LR_SCHEDULERS = frozenset(
        {
            "cosine",
            "linear",
            "cosine_with_restarts",
            "polynomial",
            "constant",
            "constant_with_warmup",
            "inverse_sqrt",
        }
    )
    _VALID_OPTIMIZERS = frozenset(
        {
            "adamw_torch",
            "adamw_torch_fused",    
            "adamw_8bit",
            "adamw_bnb_8bit",
            "paged_adamw_8bit",
            "paged_adamw_32bit",
            "adafactor",
            "sgd",
        }
    )
    _VALID_ATTN_IMPLS = frozenset(
        {
            "flash_attn",
            "flash_attention_2",
            "sdpa",
            "eager",
        }
    )

    @model_validator(mode="before")
    @classmethod
    def _normalize_keys(cls, data: Any) -> Any:
        if not isinstance(data, dict):
            return data
        known = set(cls.model_fields.keys())
        normalized: dict[str, Any] = {}
        for key, value in data.items():
            snake = _to_snake_case(key)
            if snake in known:
                normalized[snake] = value
            else:
                normalized[key] = value
        return normalized

    @model_validator(mode="after")
    def _validate_enums(self):
        if self.lr_scheduler_type not in self._VALID_LR_SCHEDULERS:
            raise ValueError(
                f"lr_scheduler_type must be one of {sorted(self._VALID_LR_SCHEDULERS)}, got '{self.lr_scheduler_type}'"
            )
        if self.optimizer not in self._VALID_OPTIMIZERS:
            raise ValueError(
                f"optimizer must be one of {sorted(self._VALID_OPTIMIZERS)}, got '{self.optimizer}'"
            )
        if self.attn_impl not in self._VALID_ATTN_IMPLS:
            raise ValueError(
                f"attn_impl must be one of {sorted(self._VALID_ATTN_IMPLS)}, got '{self.attn_impl}'"
            )
        return self


# ---------------------------------------------------------------------------
# Pre-flight checks
# ---------------------------------------------------------------------------


def _preflight_fla_check():
    """Verify flash-linear-attention is importable before training starts.

    Qwen 3.5 models use GatedDeltaNet for ~75% of their attention layers.
    Without the `fla` package these layers silently fall back to a naive
    O(n²) recurrence that uses 2-3x the VRAM — no warning, no error.
    This check fails fast so we don't burn GPU hours on a doomed run.
    """
    try:
        import fla  # noqa: F401
        from fla.ops.gated_delta_rule import fused_recurrent_gated_delta_rule  # noqa: F401

        print(
            "[Pre-flight] flash-linear-attention OK — GatedDeltaNet layers will use FLA kernels",
            flush=True,
        )
    except ImportError as e:
        print(
            f"[Pre-flight] WARNING: flash-linear-attention not importable: {e}\n"
            "  GatedDeltaNet layers will fall back to naive O(n²) recurrence.\n"
            "  This will use 2-3x more VRAM and likely OOM on sequences >4k.\n"
            "  Install: pip install git+https://github.com/fla-org/flash-linear-attention",
            flush=True,
        )
    except Exception as e:
        print(f"[Pre-flight] flash-linear-attention import issue (non-fatal): {e}", flush=True)

    try:
        import causal_conv1d  # noqa: F401

        print("[Pre-flight] causal-conv1d OK", flush=True)
    except ImportError:
        print(
            "[Pre-flight] WARNING: causal-conv1d not available — some FLA ops may be slower",
            flush=True,
        )


# ---------------------------------------------------------------------------
# Training runner
# ---------------------------------------------------------------------------


def _log_cluster_diagnostics(
    num_nodes: int,
    node_rank: int,
    master_addr: str,
    master_port: str | int,
):
    """Log network interfaces and cluster env vars for debugging."""
    import subprocess as _sp

    try:
        ifaces = _sp.check_output(["ip", "-4", "addr", "show"], timeout=5).decode()
        print(f"[Cluster] Network interfaces:\n{ifaces}", flush=True)
    except Exception as e:
        print(f"[Cluster] Could not list interfaces: {e}", flush=True)

    node_addr = os.environ.get("NODE_ADDR", "")
    primary_addr = os.environ.get("PRIMARY_ADDR", "")
    print(
        f"[Cluster] rank={node_rank}/{num_nodes}, "
        f"MASTER_ADDR={master_addr}, MASTER_PORT={master_port}, "
        f"NODE_ADDR={node_addr}, PRIMARY_ADDR={primary_addr}",
        flush=True,
    )


def _prepare_hf_dataset(snap_path: str, dataset_name: str) -> str | None:
    """Copy JSONL from a HF snapshot to /workspace/data/ with absolute image paths.

    ms-swift resolves relative image paths from the CWD, not from the JSONL
    location.  When a dataset is pulled from HuggingFace Hub the images live
    inside the snapshot cache dir, so relative paths like ``images/foo.jpg``
    break.  This function rewrites them to absolute paths and writes a local
    copy that ``swift sft`` can consume directly.

    Returns the path to the local train JSONL, or None on failure.
    """
    import json as _json
    snap = Path(snap_path)

    jsonl_files = sorted(snap.rglob("*.jsonl"))
    if not jsonl_files:
        print(f"[Dataset] No .jsonl files found in {snap_path}", flush=True)
        return None

    train_src = None
    test_src = None
    for jf in jsonl_files:
        name = jf.stem.lower()
        if "test" in name or "val" in name:
            test_src = test_src or jf
        else:
            train_src = train_src or jf

    if not train_src:
        train_src = jsonl_files[0]

    DATA_DIR.mkdir(parents=True, exist_ok=True)

    def _rewrite_jsonl(src: Path, dst: Path):
        count = 0
        with open(src) as fin, open(dst, "w") as fout:
            for line in fin:
                line = line.strip()
                if not line:
                    continue
                row = _json.loads(line)
                for key in ("images", "videos", "audios"):
                    paths = row.get(key)
                    if not paths:
                        continue
                    resolved = []
                    for p in paths:
                        if p.startswith(("http://", "https://", "data:", "/")):
                            resolved.append(p)
                        else:
                            abs_p = str(snap / p)
                            if Path(abs_p).exists():
                                resolved.append(abs_p)
                            else:
                                resolved.append(p)
                    row[key] = resolved
                fout.write(_json.dumps(row, ensure_ascii=False) + "\n")
                count += 1
        return count

    local_train = DATA_DIR / "train.jsonl"
    n = _rewrite_jsonl(train_src, local_train)
    print(f"[Dataset] Prepared {n} samples: {train_src} -> {local_train}", flush=True)

    if test_src:
        local_test = DATA_DIR / "test.jsonl"
        n_test = _rewrite_jsonl(test_src, local_test)
        print(f"[Dataset] Prepared {n_test} val samples: {test_src} -> {local_test}", flush=True)

    return str(local_train)


def _run_training(config: dict):
    global state
    push_queue: _HFPushQueue | None = None
    is_primary = True

    _training_done.clear()
    try:
        state.status = "running"
        state.started_at = datetime.now(timezone.utc).isoformat()
        state.finished_at = None
        state.error = None
        state.config = config

        print(
            f"[Training] Thread started, dataset={config.get('dataset')}, model={config.get('model')}",
            flush=True,
        )

        # Pre-flight: verify flash-linear-attention is usable.
        # Qwen 3.5 has ~75% GatedDeltaNet layers that silently fall back to
        # a naive O(n²) implementation if `fla` isn't importable, causing
        # 2-3x VRAM usage with zero warning.
        _preflight_fla_check()

        env = os.environ.copy()

        env["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
        env["HF_HUB_DISABLE_PROGRESS_BARS"] = "0"
        env["TRANSFORMERS_VERBOSITY"] = "info"
        env["TORCHELASTIC_LOG_LEVEL"] = "INFO"

        num_gpus = config.get("num_gpus") or _detect_gpu_count()

        if num_gpus > 1:
            gpu_ids = ",".join(str(i) for i in range(num_gpus))
            env["CUDA_VISIBLE_DEVICES"] = gpu_ids
            env["NPROC_PER_NODE"] = str(num_gpus)

        # Multi-node: detect from config or RunPod Instant Cluster env vars
        num_nodes = config.get("num_nodes") or int(os.environ.get("NUM_NODES", "1"))
        node_rank = config.get("node_rank")
        if node_rank is None:
            node_rank = int(os.environ.get("NODE_RANK", "0"))
        master_addr = config.get("master_addr") or os.environ.get("MASTER_ADDR", "")
        master_port = config.get("master_port") or os.environ.get("MASTER_PORT", "29500")

        if num_nodes > 1:
            if not master_addr:
                print(
                    "[Training] WARNING: num_nodes > 1 but MASTER_ADDR is empty. "
                    "RunPod Instant Clusters should set this automatically. "
                    "Distributed training will likely fail without it.",
                    flush=True,
                )
            env["NNODES"] = str(num_nodes)
            env["NODE_RANK"] = str(node_rank)
            env["MASTER_ADDR"] = master_addr
            env["MASTER_PORT"] = str(master_port)

            if not env.get("NCCL_SOCKET_IFNAME") and os.environ.get("PRIMARY_ADDR"):
                env["NCCL_SOCKET_IFNAME"] = "ens1"

            env.setdefault("NCCL_DEBUG", "INFO")
            env.setdefault("NCCL_TIMEOUT", "1800000")
            env.setdefault("TORCHELASTIC_MAX_RESTARTS", "0")

            print(
                f"[Training] Multi-node: {num_nodes} nodes, rank={node_rank}, "
                f"master={master_addr}:{master_port}, NCCL_IFNAME={env.get('NCCL_SOCKET_IFNAME', 'default')}",
                flush=True,
            )

        is_primary = node_rank == 0

        if (num_gpus > 1 or num_nodes > 1) and not config.get("deepspeed"):
            config["deepspeed"] = "zero3"

        hf_token = config.get("hf_token") or os.environ.get("HF_TOKEN", "")
        if hf_token:
            env["HF_TOKEN"] = hf_token

        wandb_key = config.get("wandb_api_key") or os.environ.get("WANDB_API_KEY", "")
        if wandb_key:
            env["WANDB_API_KEY"] = wandb_key
        if config.get("wandb_project"):
            env["WANDB_PROJECT"] = config["wandb_project"]
        if config.get("wandb_entity"):
            env["WANDB_ENTITY"] = config["wandb_entity"]
        if config.get("wandb_run_name"):
            env["WANDB_NAME"] = config["wandb_run_name"]

        hf_repo_id = config.get("hf_repo_id")
        if hf_repo_id and hf_token and is_primary:
            push_queue = _HFPushQueue(
                hf_repo_id, hf_token, model_id=config["model"], output_dir=OUTPUT_DIR
            )

        dataset_arg = config.get("dataset") or "/workspace/data/train.jsonl"

        # For HF datasets with multimodal data: download, rewrite relative
        # image/video/audio paths to absolute, and use the local copy.
        if dataset_arg and not dataset_arg.startswith("/"):
            try:
                from huggingface_hub import snapshot_download
                ds_repo = dataset_arg.split(":")[0]
                print(f"[Training] Pre-downloading dataset: {ds_repo}", flush=True)
                snap_path = snapshot_download(ds_repo, repo_type="dataset", token=hf_token or None)
                print(f"[Training] Dataset cached: {ds_repo} -> {snap_path}", flush=True)

                local_jsonl = _prepare_hf_dataset(snap_path, dataset_arg)
                if local_jsonl:
                    dataset_arg = local_jsonl
            except Exception as e:
                print(f"[Training] Dataset pre-download note: {e} (swift will retry)", flush=True)

        if config.get("dataset_subset"):
            dataset_arg = f"{dataset_arg}:{config['dataset_subset']}"

        cmd = [
            "swift",
            "sft",
            "--model",
            config["model"],
            "--dataset",
            dataset_arg,
            "--tuner_type",
            config["tuner_type"],
            "--torch_dtype",
            "bfloat16",
            "--num_train_epochs",
            str(config["num_epochs"]),
            "--per_device_train_batch_size",
            str(config["batch_size"]),
            "--per_device_eval_batch_size",
            str(config["batch_size"]),
            "--learning_rate",
            str(config["learning_rate"]),
            "--gradient_accumulation_steps",
            str(config["grad_accum"]),
            "--eval_strategy",
            "steps",
            "--eval_steps",
            str(config.get("eval_steps") or config["save_steps"]),
            "--save_steps",
            str(config["save_steps"]),
            "--save_total_limit",
            str(config.get("save_total_limit", 2)),
            "--logging_steps",
            str(config["logging_steps"]),
            "--max_length",
            str(config["max_length"]),
            "--output_dir",
            str(OUTPUT_DIR),
            "--warmup_ratio",
            str(config.get("warmup_ratio", 0.1)),
            "--lr_scheduler_type",
            config.get("lr_scheduler_type", "cosine"),
            "--weight_decay",
            str(config.get("weight_decay", 0.1)),
            "--max_grad_norm",
            str(config.get("max_grad_norm", 1.0)),
            "--optim",
            config.get("optimizer", "adamw_torch"),
            "--seed",
            str(config.get("seed", 42)),
            "--dataloader_num_workers",
            "4",
            "--lazy_tokenize",
            str(config.get("lazy_tokenize", True)),
            "--dataset_num_proc",
            str(config.get("dataset_num_proc", 4)),
            "--attn_impl",
            config.get("attn_impl", "flash_attn"),
            "--use_hf",
            "true",
        ]

        if config.get("max_pixels") is not None:
            cmd += ["--max_pixels", str(config["max_pixels"])]
        if config.get("min_pixels") is not None:
            cmd += ["--min_pixels", str(config["min_pixels"])]

        if config.get("neftune_alpha") and config["neftune_alpha"] > 0:
            cmd += ["--neftune_noise_alpha", str(config["neftune_alpha"])]

        if config.get("packing"):
            cmd += ["--packing", "true"]

        if config.get("shuffle_dataset"):
            cmd += ["--dataset_shuffle", "true"]

        if config.get("gradient_checkpointing"):
            cmd += ["--gradient_checkpointing", "true"]

        if config.get("use_flash_ckpt", False):
            cmd += ["--use_flash_ckpt", "true"]

        if config.get("deepspeed"):
            cmd += ["--deepspeed", config["deepspeed"]]

        if is_primary and (config.get("wandb_project") or wandb_key):
            cmd += ["--report_to", "wandb"]

        val = config.get("val_dataset")
        if val:
            cmd += ["--val_dataset", val]
        elif Path("/workspace/data/test.jsonl").exists():
            cmd += ["--val_dataset", "/workspace/data/test.jsonl"]

        val_split = config.get("val_split_ratio")
        if val_split and val_split > 0 and not val:
            cmd += ["--split_dataset_ratio", str(val_split)]

        has_val = (
            bool(val)
            or (val_split and val_split > 0)
            or Path("/workspace/data/test.jsonl").exists()
        )
        patience = config.get("early_stopping_patience")
        if patience and has_val:
            cmd += [
                "--load_best_model_at_end",
                "true",
                "--metric_for_best_model",
                "eval_loss",
                "--greater_is_better",
                "false",
                "--early_stopping_patience",
                str(patience),
            ]
            threshold = config.get("early_stopping_threshold", 0.0)
            if threshold > 0:
                cmd += ["--early_stopping_threshold", str(threshold)]
            save_limit = config.get("save_total_limit", 2)
            if save_limit < 2:
                cmd += ["--save_total_limit", "2"]
                print(
                    "[Training] Bumped save_total_limit to 2 (required for load_best_model_at_end)",
                    flush=True,
                )
        elif patience and not has_val:
            print(
                "[Training] WARNING: early_stopping_patience ignored — no validation data configured",
                flush=True,
            )

        resume = config.get("resume_from_checkpoint")
        if resume:
            if resume == "auto":
                ckpt = _find_latest_checkpoint(OUTPUT_DIR)
                if ckpt:
                    print(f"[Training] Auto-resume: found {ckpt}", flush=True)
                    cmd += ["--resume_from_checkpoint", str(ckpt)]
                else:
                    print("[Training] Auto-resume: no checkpoint found, starting fresh", flush=True)
            else:
                cmd += ["--resume_from_checkpoint", resume]

        if config.get("extra_args"):
            for k, v in config["extra_args"].items():
                flag = f"--{k}" if not k.startswith("--") else k
                cmd += [flag, str(v)]

        # Pre-download model so torchrun doesn't do it silently
        model_name = config["model"]
        print(f"[Training] Pre-downloading model: {model_name}", flush=True)
        try:
            from huggingface_hub import snapshot_download
            snapshot_download(model_name, token=hf_token or None)
            print(f"[Training] Model cached: {model_name}", flush=True)
        except Exception as e:
            print(f"[Training] Model pre-download note: {e} (torchrun will retry)", flush=True)

        if num_nodes > 1 and master_addr:
            _log_cluster_diagnostics(num_nodes, node_rank, master_addr, master_port)

        print(f"[Training] Running: {' '.join(cmd)}", flush=True)

        with open(LOG_FILE, "w") as log:
            proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
            state.pid = proc.pid
            for line in iter(proc.stdout.readline, b""):
                decoded = line.decode("utf-8", errors="replace")
                log.write(decoded)
                log.flush()
                print(decoded, end="", flush=True)

            proc.wait()

        if proc.returncode != 0:
            state.status = "failed"
            state.error = f"Training exited with code {proc.returncode}"
            print(f"[Training] FAILED with exit code {proc.returncode}", flush=True)
        else:
            state.status = "completed"
            print("[Training] Completed successfully", flush=True)

    except Exception as e:
        state.status = "failed"
        state.error = str(e)
        print(f"[Training] Exception: {e}", flush=True)
    finally:
        state.finished_at = datetime.now(timezone.utc).isoformat()
        state.pid = None

        if push_queue is not None:
            push_queue.stop_and_wait()

        hf_push_ok = False
        if is_primary and state.status == "completed" and hf_repo_id and hf_token:
            state.hf_push_status = "pushing"
            print(
                "[HF Push] Starting final model push — container MUST stay alive until this completes",
                flush=True,
            )
            hf_push_ok = _hf_push_final_model(
                OUTPUT_DIR,
                hf_repo_id,
                hf_token,
                model_id=config["model"],
                tuner_type=config["tuner_type"],
            )
            state.hf_push_status = "success" if hf_push_ok else "failed"
            if not hf_push_ok:
                state.error = "Training completed but final HF push failed after all retries"
            print(f"[HF Push] Final push finished (success={hf_push_ok})", flush=True)

        if is_primary and state.status == "completed":
            _fire_completion_webhook(config, hf_push_ok=hf_push_ok)
        elif not is_primary:
            print(f"[Training] Worker node (rank {config.get('node_rank', '?')}) — skipping HF push and webhook", flush=True)

        _training_done.set()
        print("[Training] Training thread fully done (push complete, safe to exit)", flush=True)


# ---------------------------------------------------------------------------
# Graceful shutdown: wait for training + HF push before allowing exit
# ---------------------------------------------------------------------------

_original_sigterm = signal.getsignal(signal.SIGTERM)
_original_sigint = signal.getsignal(signal.SIGINT)


def _graceful_shutdown(signum, frame):
    """Block container exit until the HF push is finished."""
    sig_name = "SIGTERM" if signum == signal.SIGTERM else "SIGINT"
    if not _training_done.is_set():
        print(
            f"[Shutdown] {sig_name} received but training/push still in progress — waiting up to 30 min...",
            flush=True,
        )
        finished = _training_done.wait(timeout=1800)
        if finished:
            print("[Shutdown] Training + push completed, proceeding with shutdown", flush=True)
        else:
            print(
                "[Shutdown] CRITICAL: Timed out waiting for push after 30 min, forcing exit",
                flush=True,
            )
    else:
        print(
            f"[Shutdown] {sig_name} received, no training in progress — shutting down immediately",
            flush=True,
        )

    # Re-raise to the original handler (uvicorn's) so the server actually stops
    original = _original_sigterm if signum == signal.SIGTERM else _original_sigint
    if callable(original):
        original(signum, frame)
    else:
        raise SystemExit(0)


# ---------------------------------------------------------------------------
# Startup: recover interrupted HF pushes + install signal handlers
# ---------------------------------------------------------------------------


@app.on_event("startup")
def _on_startup():
    signal.signal(signal.SIGTERM, _graceful_shutdown)
    signal.signal(signal.SIGINT, _graceful_shutdown)
    threading.Thread(target=_recover_pending_push, daemon=True, name="hf-push-recovery").start()


# ---------------------------------------------------------------------------
# API endpoints
# ---------------------------------------------------------------------------


@app.get("/health")
def health():
    gpus = _gpu_info()
    num_nodes = int(os.environ.get("NUM_NODES", "1"))
    node_rank = int(os.environ.get("NODE_RANK", "0"))
    resp: dict[str, Any] = {"status": "ok", "num_gpus": len(gpus), "gpus": gpus}
    if num_nodes > 1:
        resp["cluster"] = {
            "num_nodes": num_nodes,
            "node_rank": node_rank,
            "master_addr": os.environ.get("MASTER_ADDR", ""),
            "master_port": os.environ.get("MASTER_PORT", "29500"),
        }
    return resp


@app.get("/status", dependencies=[Depends(verify_secret)])
def get_status():
    return state.to_dict()


@app.post("/train", dependencies=[Depends(verify_secret)])
def start_training(req: TrainRequest):
    if state.status == "running":
        raise HTTPException(409, "Training already in progress")

    config = req.model_dump()
    if not config.get("dataset") and not Path("/workspace/data/train.jsonl").exists():
        raise HTTPException(
            400,
            "No dataset specified and no train.jsonl found. "
            "Either set 'dataset' to a HuggingFace ID or upload a JSONL file first.",
        )

    global _training_thread
    thread = threading.Thread(
        target=_run_training, args=(config,), daemon=False, name="training-main"
    )
    _training_thread = thread
    thread.start()
    return {"message": "Training started", "config": config}


@app.get("/train/config")
def get_train_config():
    """Return the full training configuration schema with defaults, types, and descriptions.

    No auth required so dashboards can populate forms before the user enters a secret.
    """
    schema = TrainRequest.model_json_schema()
    props = schema.get("properties", {})

    fields: list[dict] = []
    for name, info in props.items():
        field_type = info.get("type")
        any_of = info.get("anyOf")
        if not field_type and any_of:
            types = [t.get("type") for t in any_of if t.get("type") and t.get("type") != "null"]
            field_type = types[0] if types else "string"
            nullable = any(t.get("type") == "null" for t in any_of)
        else:
            nullable = False

        entry: dict[str, Any] = {
            "name": name,
            "type": field_type or "string",
            "nullable": nullable,
            "default": info.get("default"),
            "description": info.get("description", ""),
        }
        if "minimum" in info:
            entry["min"] = info["minimum"]
        if "exclusiveMinimum" in info:
            entry["exclusive_min"] = info["exclusiveMinimum"]
        if "maximum" in info:
            entry["max"] = info["maximum"]
        if "enum" in info:
            entry["options"] = info["enum"]

        fields.append(entry)

    defaults = TrainRequest().model_dump()
    return {"fields": fields, "defaults": defaults}


@app.post("/stop", dependencies=[Depends(verify_secret)])
def stop_training():
    if state.status != "running" or not state.pid:
        if not _training_done.is_set():
            return {
                "message": "Training finished but HF push still in progress — container will stay alive"
            }
        raise HTTPException(400, "No training in progress")
    try:
        os.kill(state.pid, signal.SIGTERM)
        state.status = "stopped"
        state.finished_at = datetime.now(timezone.utc).isoformat()
        return {
            "message": "Training stop signal sent (HF push will still complete before container exits)"
        }
    except ProcessLookupError:
        state.status = "idle"
        return {"message": "Process already exited"}


@app.post("/upload/dataset", dependencies=[Depends(verify_secret)])
async def upload_dataset(
    train_file: UploadFile = File(...),
    test_file: UploadFile | None = File(None),
):
    train_path = DATA_DIR / "train.jsonl"
    with open(train_path, "wb") as f:
        shutil.copyfileobj(train_file.file, f)
    result = {"train_file": str(train_path), "train_size": train_path.stat().st_size}

    if test_file:
        test_path = DATA_DIR / "test.jsonl"
        with open(test_path, "wb") as f:
            shutil.copyfileobj(test_file.file, f)
        result["test_file"] = str(test_path)
        result["test_size"] = test_path.stat().st_size

    return result


@app.post("/upload/config", dependencies=[Depends(verify_secret)])
async def upload_config(config_file: UploadFile = File(...)):
    dest = CONFIG_DIR / config_file.filename
    with open(dest, "wb") as f:
        shutil.copyfileobj(config_file.file, f)
    return {"config_file": str(dest), "size": dest.stat().st_size}


@app.get("/logs", dependencies=[Depends(verify_secret)])
def get_logs(lines: int = 100):
    if not LOG_FILE.exists():
        return {"logs": ""}
    all_lines = LOG_FILE.read_text().splitlines()
    return {"logs": "\n".join(all_lines[-lines:])}


@app.get("/checkpoints", dependencies=[Depends(verify_secret)])
def list_checkpoints():
    checkpoints = []
    for d in sorted(OUTPUT_DIR.glob("*/checkpoint-*")):
        checkpoints.append(
            {
                "path": str(d),
                "name": d.name,
                "run": d.parent.name,
            }
        )
    for d in sorted(OUTPUT_DIR.glob("checkpoint-*")):
        checkpoints.append(
            {
                "path": str(d),
                "name": d.name,
                "run": "root",
            }
        )
    return {"checkpoints": checkpoints}


def _detect_gpu_count() -> int:
    try:
        out = subprocess.check_output(["nvidia-smi", "-L"], text=True)
        count = len([l for l in out.strip().splitlines() if l.strip()])
        return max(count, 1)
    except Exception:
        return 1


def _gpu_info():
    try:
        out = subprocess.check_output(
            [
                "nvidia-smi",
                "--query-gpu=name,memory.total,memory.used,memory.free",
                "--format=csv,noheader,nounits",
            ],
            text=True,
        )
        gpus = []
        for line in out.strip().splitlines():
            parts = [p.strip() for p in line.split(",")]
            gpus.append(
                {
                    "name": parts[0],
                    "memory_total_mb": int(parts[1]),
                    "memory_used_mb": int(parts[2]),
                    "memory_free_mb": int(parts[3]),
                }
            )
        return gpus
    except Exception:
        return []