Upload kaggle/notebook_director_lora.py with huggingface_hub
Browse files- kaggle/notebook_director_lora.py +49 -22
kaggle/notebook_director_lora.py
CHANGED
|
@@ -5,8 +5,8 @@ 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/,
|
| 9 |
-
sample.txt, train_meta.json. The ONNX export is a separate step.
|
| 10 |
"""
|
| 11 |
import os
|
| 12 |
import shutil
|
|
@@ -18,58 +18,85 @@ from pathlib import Path
|
|
| 18 |
|
| 19 |
ROOT = Path("/kaggle/working")
|
| 20 |
REPO = os.environ.get("BUNDLE_REPO", "idefinity/webfm-eval")
|
| 21 |
-
|
|
|
|
| 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(
|
| 34 |
dst.parent.mkdir(parents=True, exist_ok=True)
|
| 35 |
-
for attempt in range(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def main() -> None:
|
|
|
|
| 51 |
for rel, dst in FILES.items():
|
| 52 |
-
fetch(
|
| 53 |
|
| 54 |
-
|
| 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__":
|
|
|
|
| 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/, adapter.zip,
|
| 9 |
+
model_fp16/, sample.txt, train_meta.json. The ONNX export is a separate step.
|
| 10 |
"""
|
| 11 |
import os
|
| 12 |
import shutil
|
|
|
|
| 18 |
|
| 19 |
ROOT = Path("/kaggle/working")
|
| 20 |
REPO = os.environ.get("BUNDLE_REPO", "idefinity/webfm-eval")
|
| 21 |
+
BASES = [f"https://huggingface.co/datasets/{REPO}/resolve/main",
|
| 22 |
+
f"https://hf-mirror.com/datasets/{REPO}/resolve/main"]
|
| 23 |
SFT = ROOT / "director_lora" / "sft.jsonl" # new protocol, not the old radio set
|
| 24 |
TRAIN = ROOT / "kaggle" / "director_lora_train.py"
|
| 25 |
OUT = ROOT / "director_lora_out"
|
| 26 |
|
| 27 |
FILES = {"director_lora/sft.jsonl": SFT, "kaggle/director_lora_train.py": TRAIN}
|
| 28 |
|
| 29 |
+
# Pinned: 5.22.2 is the version whose Qwen3_5 auto mapping and module names
|
| 30 |
+
# were checked. hub 1.x is what transformers 5.22.2 requires.
|
| 31 |
+
PIP = ("pip install -q --upgrade "
|
| 32 |
+
"'transformers==5.22.2' 'huggingface_hub>=1.31,<2' "
|
| 33 |
+
"'peft==0.21.2' 'accelerate>=1.1'")
|
| 34 |
+
CHECK = (sys.executable + " -c \""
|
| 35 |
+
"import torch, transformers, peft, huggingface_hub; "
|
| 36 |
+
"print('torch', torch.__version__, 'cuda', torch.version.cuda, "
|
| 37 |
+
"'gpus', torch.cuda.device_count()); "
|
| 38 |
+
"print('transformers', transformers.__version__, "
|
| 39 |
+
"'peft', peft.__version__, 'hub', huggingface_hub.__version__)\"")
|
| 40 |
+
|
| 41 |
|
| 42 |
def log(msg: str) -> None:
|
| 43 |
print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)
|
| 44 |
|
| 45 |
|
| 46 |
+
def fetch(rel: str, dst: Path) -> None:
|
| 47 |
dst.parent.mkdir(parents=True, exist_ok=True)
|
| 48 |
+
for attempt in range(6):
|
| 49 |
+
for base in BASES:
|
| 50 |
+
url = f"{base}/{rel}"
|
| 51 |
+
try:
|
| 52 |
+
with urllib.request.urlopen(url, timeout=180) as r, open(dst, "wb") as f:
|
| 53 |
+
shutil.copyfileobj(r, f)
|
| 54 |
+
size = dst.stat().st_size
|
| 55 |
+
if size < 1000:
|
| 56 |
+
raise RuntimeError(f"only {size} bytes")
|
| 57 |
+
log(f"fetched {url} -> {dst} ({size/1e6:.2f} MB)")
|
| 58 |
+
return
|
| 59 |
+
except Exception as exc:
|
| 60 |
+
log(f"fetch attempt {attempt + 1} via {base} failed: {exc}")
|
| 61 |
+
time.sleep(4)
|
| 62 |
+
raise SystemExit(f"cannot fetch {rel}")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def run(cmd: str, env=None) -> int:
|
| 66 |
+
log(f"$ {cmd[:220]}")
|
| 67 |
+
return subprocess.run(cmd, shell=True, env=env).returncode
|
| 68 |
|
| 69 |
|
| 70 |
def main() -> None:
|
| 71 |
+
log(f"python {sys.version.split()[0]}")
|
| 72 |
for rel, dst in FILES.items():
|
| 73 |
+
fetch(rel, dst)
|
| 74 |
|
| 75 |
+
if run(PIP) != 0:
|
|
|
|
|
|
|
|
|
|
| 76 |
raise SystemExit("dependency install failed")
|
| 77 |
+
if run(CHECK) != 0:
|
| 78 |
+
raise SystemExit("environment check failed (torch/hub mismatch?)")
|
| 79 |
|
| 80 |
env = dict(os.environ, WORK_ROOT=str(ROOT), DATA=str(SFT), LORA_OUT=str(OUT),
|
| 81 |
+
CUDA_VISIBLE_DEVICES="0", HF_HUB_DISABLE_XET="1",
|
| 82 |
EPOCHS=os.environ.get("EPOCHS", "2"),
|
| 83 |
LR=os.environ.get("LR", "1e-4"),
|
| 84 |
R=os.environ.get("R", "16"),
|
| 85 |
ALPHA=os.environ.get("ALPHA", "32"),
|
| 86 |
MAX_LEN=os.environ.get("MAX_LEN", "2048"),
|
| 87 |
ACCUM=os.environ.get("ACCUM", "4"),
|
| 88 |
+
DTYPE=os.environ.get("DTYPE", "auto"),
|
| 89 |
GRAD_CKPT=os.environ.get("GRAD_CKPT", "0"))
|
| 90 |
log(f"training on {SFT}")
|
| 91 |
rc = subprocess.run([sys.executable, str(TRAIN)], env=env).returncode
|
| 92 |
log(f"train rc={rc}; outputs in {OUT}")
|
| 93 |
+
if OUT.exists():
|
| 94 |
+
for p in sorted(OUT.rglob("*")):
|
| 95 |
+
if p.is_file():
|
| 96 |
+
log(f"artifact {p.relative_to(OUT)} {p.stat().st_size/1e6:.2f} MB")
|
| 97 |
if rc != 0:
|
| 98 |
raise SystemExit("director LoRA training failed")
|
| 99 |
+
log("DONE")
|
| 100 |
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|