Upload kaggle/notebook_director_lora.py with huggingface_hub
Browse files
kaggle/notebook_director_lora.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Kaggle notebook body: train the director LoRA.
|
| 2 |
+
|
| 3 |
+
The HF bundle holds only two files needed here, so fetch them by URL instead
|
| 4 |
+
of snapshot_download: this avoids importing huggingface_hub before the
|
| 5 |
+
transformers 5.x upgrade (a stale already-imported hub breaks transformers'
|
| 6 |
+
`from huggingface_hub.utils import httpx`).
|
| 7 |
+
|
| 8 |
+
Output lands in /kaggle/working/director_lora_out: adapter/, model_fp16/,
|
| 9 |
+
sample.txt, train_meta.json. The ONNX export is a separate step.
|
| 10 |
+
"""
|
| 11 |
+
import os
|
| 12 |
+
import shutil
|
| 13 |
+
import subprocess
|
| 14 |
+
import sys
|
| 15 |
+
import time
|
| 16 |
+
import urllib.request
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
ROOT = Path("/kaggle/working")
|
| 20 |
+
REPO = os.environ.get("BUNDLE_REPO", "idefinity/webfm-eval")
|
| 21 |
+
BASE = f"https://huggingface.co/datasets/{REPO}/resolve/main"
|
| 22 |
+
SFT = ROOT / "director_lora" / "sft.jsonl" # new protocol, not the old radio set
|
| 23 |
+
TRAIN = ROOT / "kaggle" / "director_lora_train.py"
|
| 24 |
+
OUT = ROOT / "director_lora_out"
|
| 25 |
+
|
| 26 |
+
FILES = {"director_lora/sft.jsonl": SFT, "kaggle/director_lora_train.py": TRAIN}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def log(msg: str) -> None:
|
| 30 |
+
print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def fetch(url: str, dst: Path) -> None:
|
| 34 |
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
| 35 |
+
for attempt in range(3):
|
| 36 |
+
try:
|
| 37 |
+
with urllib.request.urlopen(url, timeout=180) as r, open(dst, "wb") as f:
|
| 38 |
+
shutil.copyfileobj(r, f)
|
| 39 |
+
size = dst.stat().st_size
|
| 40 |
+
if size < 1000:
|
| 41 |
+
raise RuntimeError(f"only {size} bytes")
|
| 42 |
+
log(f"fetched {url} -> {dst} ({size/1e6:.2f} MB)")
|
| 43 |
+
return
|
| 44 |
+
except Exception as exc:
|
| 45 |
+
log(f"fetch attempt {attempt + 1} failed: {exc}")
|
| 46 |
+
time.sleep(4)
|
| 47 |
+
raise SystemExit(f"cannot fetch {url}")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def main() -> None:
|
| 51 |
+
for rel, dst in FILES.items():
|
| 52 |
+
fetch(f"{BASE}/{rel}", dst)
|
| 53 |
+
|
| 54 |
+
cmd = ("pip install -q --upgrade 'transformers>=5' "
|
| 55 |
+
"'huggingface_hub>=1.31,<2' peft accelerate")
|
| 56 |
+
log(f"$ {cmd}")
|
| 57 |
+
if subprocess.run(cmd, shell=True).returncode != 0:
|
| 58 |
+
raise SystemExit("dependency install failed")
|
| 59 |
+
|
| 60 |
+
env = dict(os.environ, WORK_ROOT=str(ROOT), DATA=str(SFT), LORA_OUT=str(OUT),
|
| 61 |
+
EPOCHS=os.environ.get("EPOCHS", "2"),
|
| 62 |
+
LR=os.environ.get("LR", "1e-4"),
|
| 63 |
+
R=os.environ.get("R", "16"),
|
| 64 |
+
ALPHA=os.environ.get("ALPHA", "32"),
|
| 65 |
+
MAX_LEN=os.environ.get("MAX_LEN", "2048"),
|
| 66 |
+
ACCUM=os.environ.get("ACCUM", "4"),
|
| 67 |
+
GRAD_CKPT=os.environ.get("GRAD_CKPT", "0"))
|
| 68 |
+
log(f"training on {SFT}")
|
| 69 |
+
rc = subprocess.run([sys.executable, str(TRAIN)], env=env).returncode
|
| 70 |
+
log(f"train rc={rc}; outputs in {OUT}")
|
| 71 |
+
if rc != 0:
|
| 72 |
+
raise SystemExit("director LoRA training failed")
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
main()
|