Dev-the-dev91 commited on
Commit
a3eefab
·
verified ·
1 Parent(s): 9e16d69

Update HF Jobs training script

Browse files
Files changed (1) hide show
  1. scripts/hf_jobs_train_syllabus.py +124 -0
scripts/hf_jobs_train_syllabus.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "huggingface_hub>=0.26.0",
5
+ # ]
6
+ # ///
7
+ """Run syllabus LoRA SFT on Hugging Face Jobs (GPU).
8
+
9
+ Downloads finetune JSONL from the Hub, clones training-pipeline, runs readiness
10
+ gates, trains with OOM-safe defaults, and pushes the adapter to a Hub model repo.
11
+
12
+ Override via environment variables:
13
+ HF_DATASET_REPO (default: Dev-the-dev91/syllabus-finetune)
14
+ HF_OUTPUT_MODEL (default: Dev-the-dev91/syllabus-extractor-lora)
15
+ HF_PIPELINE_REPO (default: https://github.com/madch3m/training-pipeline.git)
16
+ HF_MAX_LENGTH (default: 2048)
17
+ HF_TRAIN_EPOCHS (default: 3)
18
+ HF_JOB_FLAVOR (informational only when run locally)
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import os
24
+ import subprocess
25
+ import sys
26
+ from pathlib import Path
27
+
28
+ from huggingface_hub import HfApi, hf_hub_download
29
+
30
+ HF_USER = os.environ.get("HF_USER", "Dev-the-dev91")
31
+ DATASET_REPO = os.environ.get("HF_DATASET_REPO", f"{HF_USER}/syllabus-finetune")
32
+ OUTPUT_MODEL = os.environ.get("HF_OUTPUT_MODEL", f"{HF_USER}/syllabus-extractor-lora")
33
+ PIPELINE_GIT = os.environ.get(
34
+ "HF_PIPELINE_REPO",
35
+ "https://github.com/madch3m/training-pipeline.git",
36
+ )
37
+ MAX_LENGTH = os.environ.get("HF_MAX_LENGTH", "2048")
38
+ NUM_EPOCHS = os.environ.get("HF_TRAIN_EPOCHS", "3")
39
+ WORK = Path(os.environ.get("HF_WORK_DIR", "/tmp/training_pipeline"))
40
+
41
+
42
+ def run(cmd: list[str], *, cwd: Path | None = None) -> None:
43
+ print("+", " ".join(cmd), flush=True)
44
+ subprocess.run(cmd, check=True, cwd=cwd)
45
+
46
+
47
+ def main() -> None:
48
+ api = HfApi()
49
+ who = api.whoami()["name"]
50
+ print(f"Hub user: {who}")
51
+ print(f"Dataset: {DATASET_REPO}")
52
+ print(f"Output model: {OUTPUT_MODEL}")
53
+
54
+ if WORK.exists():
55
+ run(["rm", "-rf", str(WORK)])
56
+ run(["git", "clone", "--depth", "1", PIPELINE_GIT, str(WORK)])
57
+ run([sys.executable, "-m", "pip", "install", "-q", "-e", ".[train]"], cwd=WORK)
58
+
59
+ data_dir = WORK / "data" / "finetune"
60
+ data_dir.mkdir(parents=True, exist_ok=True)
61
+ for name in ("train.jsonl", "valid.jsonl"):
62
+ cached = hf_hub_download(
63
+ repo_id=DATASET_REPO,
64
+ filename=name,
65
+ repo_type="dataset",
66
+ )
67
+ (data_dir / name).write_bytes(Path(cached).read_bytes())
68
+ print(f"Fetched {name} from {DATASET_REPO}")
69
+
70
+ train_path = data_dir / "train.jsonl"
71
+ valid_path = data_dir / "valid.jsonl"
72
+
73
+ run(
74
+ [
75
+ sys.executable,
76
+ "validate_training_readiness.py",
77
+ "--train-jsonl",
78
+ str(train_path),
79
+ "--valid-jsonl",
80
+ str(valid_path),
81
+ "--strict",
82
+ ],
83
+ cwd=WORK,
84
+ )
85
+
86
+ out_dir = WORK / "artifacts" / "hf_syllabus_extractor"
87
+ run(
88
+ [
89
+ sys.executable,
90
+ "train_hf_structured_extractor.py",
91
+ "--train-jsonl",
92
+ str(train_path),
93
+ "--valid-jsonl",
94
+ str(valid_path),
95
+ "--model-name",
96
+ "Qwen/Qwen2.5-0.5B-Instruct",
97
+ "--output-dir",
98
+ str(out_dir),
99
+ "--max-length",
100
+ MAX_LENGTH,
101
+ "--per-device-train-batch-size",
102
+ "1",
103
+ "--gradient-accumulation-steps",
104
+ "8",
105
+ "--num-train-epochs",
106
+ NUM_EPOCHS,
107
+ "--bf16",
108
+ "--disable-mlflow",
109
+ ],
110
+ cwd=WORK,
111
+ )
112
+
113
+ api.create_repo(OUTPUT_MODEL, repo_type="model", exist_ok=True)
114
+ api.upload_folder(
115
+ folder_path=str(out_dir),
116
+ repo_id=OUTPUT_MODEL,
117
+ repo_type="model",
118
+ commit_message="LoRA adapter from HF Jobs syllabus SFT",
119
+ )
120
+ print(f"Uploaded adapter: https://huggingface.co/{OUTPUT_MODEL}")
121
+
122
+
123
+ if __name__ == "__main__":
124
+ main()