Upload folder using huggingface_hub
Browse files- modal_train.py +10 -0
- prep.py +10 -11
- train.py +15 -3
modal_train.py
CHANGED
|
@@ -52,6 +52,7 @@ def train_on_l4(hours: float = 20.0, ckpt_every: int = 250,
|
|
| 52 |
size: str = "large", batch: int = None, scale: float = 3.0):
|
| 53 |
import subprocess
|
| 54 |
import sys
|
|
|
|
| 55 |
|
| 56 |
# ---- pull latest code from HF (use huggingface_hub, not the `hf` CLI) ----
|
| 57 |
from huggingface_hub import snapshot_download
|
|
@@ -65,6 +66,15 @@ def train_on_l4(hours: float = 20.0, ckpt_every: int = 250,
|
|
| 65 |
os.makedirs(data_dir, exist_ok=True)
|
| 66 |
os.makedirs(ckpt_dir, exist_ok=True)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# ---- ensure data (generate in-container; fast HF egress) ----
|
| 69 |
if not os.path.exists(os.path.join(data_dir, "train.bin")):
|
| 70 |
print(f"[modal] train.bin missing; generating data locally (scale={scale}) ...")
|
|
|
|
| 52 |
size: str = "large", batch: int = None, scale: float = 3.0):
|
| 53 |
import subprocess
|
| 54 |
import sys
|
| 55 |
+
import shutil
|
| 56 |
|
| 57 |
# ---- pull latest code from HF (use huggingface_hub, not the `hf` CLI) ----
|
| 58 |
from huggingface_hub import snapshot_download
|
|
|
|
| 66 |
os.makedirs(data_dir, exist_ok=True)
|
| 67 |
os.makedirs(ckpt_dir, exist_ok=True)
|
| 68 |
|
| 69 |
+
# reuse the canonical tokenizer / RAG corpus already fetched with the code
|
| 70 |
+
for name in ("tokenizer.json", "meta.json", "rag_corpus.txt"):
|
| 71 |
+
src = os.path.join("/root/clanker/data", name)
|
| 72 |
+
if os.path.exists(src):
|
| 73 |
+
shutil.copy(src, os.path.join(data_dir, name))
|
| 74 |
+
|
| 75 |
+
os.makedirs(data_dir, exist_ok=True)
|
| 76 |
+
os.makedirs(ckpt_dir, exist_ok=True)
|
| 77 |
+
|
| 78 |
# ---- ensure data (generate in-container; fast HF egress) ----
|
| 79 |
if not os.path.exists(os.path.join(data_dir, "train.bin")):
|
| 80 |
print(f"[modal] train.bin missing; generating data locally (scale={scale}) ...")
|
prep.py
CHANGED
|
@@ -41,7 +41,7 @@ TOK_BUDGET_RAG = 150_000_000 # scaled by --scale
|
|
| 41 |
# --------------------------------------------------------------------------
|
| 42 |
# 1) Tokenizer training
|
| 43 |
# --------------------------------------------------------------------------
|
| 44 |
-
def train_tokenizer():
|
| 45 |
print("[prep] streaming FineWeb-edu to collect tokenizer training docs ...")
|
| 46 |
ds = load_dataset("HuggingFaceFW/fineweb-edu", "sample-10BT",
|
| 47 |
streaming=True, split="train")
|
|
@@ -53,7 +53,7 @@ def train_tokenizer():
|
|
| 53 |
print(f"[prep] collected {len(texts)} docs for tokenizer")
|
| 54 |
tok = YKTokenizer().train(
|
| 55 |
iter(texts), vocab_size=32768,
|
| 56 |
-
save_path=os.path.join(
|
| 57 |
print(f"[prep] tokenizer trained: vocab={tok.vocab_size}")
|
| 58 |
return tok
|
| 59 |
|
|
@@ -248,18 +248,17 @@ def main():
|
|
| 248 |
# try to reuse the canonical tokenizer from HF (keeps all runs compatible)
|
| 249 |
try:
|
| 250 |
print("[prep] no local tokenizer; downloading canonical one from HF ...")
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
break
|
| 259 |
tok = YKTokenizer.load(tok_path)
|
| 260 |
except Exception as e:
|
| 261 |
print(f"[prep] HF tokenizer download failed ({e}); training a new one.")
|
| 262 |
-
tok = train_tokenizer()
|
| 263 |
|
| 264 |
bin_path = os.path.join(out_dir, "train.bin")
|
| 265 |
if os.path.exists(bin_path):
|
|
|
|
| 41 |
# --------------------------------------------------------------------------
|
| 42 |
# 1) Tokenizer training
|
| 43 |
# --------------------------------------------------------------------------
|
| 44 |
+
def train_tokenizer(out_dir=DATADIR):
|
| 45 |
print("[prep] streaming FineWeb-edu to collect tokenizer training docs ...")
|
| 46 |
ds = load_dataset("HuggingFaceFW/fineweb-edu", "sample-10BT",
|
| 47 |
streaming=True, split="train")
|
|
|
|
| 53 |
print(f"[prep] collected {len(texts)} docs for tokenizer")
|
| 54 |
tok = YKTokenizer().train(
|
| 55 |
iter(texts), vocab_size=32768,
|
| 56 |
+
save_path=os.path.join(out_dir, "tokenizer.json"))
|
| 57 |
print(f"[prep] tokenizer trained: vocab={tok.vocab_size}")
|
| 58 |
return tok
|
| 59 |
|
|
|
|
| 248 |
# try to reuse the canonical tokenizer from HF (keeps all runs compatible)
|
| 249 |
try:
|
| 250 |
print("[prep] no local tokenizer; downloading canonical one from HF ...")
|
| 251 |
+
from huggingface_hub import hf_hub_download
|
| 252 |
+
tok_path = hf_hub_download(
|
| 253 |
+
repo_id="coderofpears/clankerDiffusion-base",
|
| 254 |
+
filename="data/tokenizer.json",
|
| 255 |
+
repo_type="model",
|
| 256 |
+
local_dir=out_dir,
|
| 257 |
+
token=os.environ.get("HF_TOKEN"))
|
|
|
|
| 258 |
tok = YKTokenizer.load(tok_path)
|
| 259 |
except Exception as e:
|
| 260 |
print(f"[prep] HF tokenizer download failed ({e}); training a new one.")
|
| 261 |
+
tok = train_tokenizer(out_dir)
|
| 262 |
|
| 263 |
bin_path = os.path.join(out_dir, "train.bin")
|
| 264 |
if os.path.exists(bin_path):
|
train.py
CHANGED
|
@@ -44,12 +44,24 @@ def build_cfg(args):
|
|
| 44 |
|
| 45 |
|
| 46 |
def _push_hf(path, repo):
|
| 47 |
-
"""Upload a single checkpoint file to HF
|
| 48 |
if not repo:
|
| 49 |
return
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
print(f"[hf] pushed {os.path.basename(path)} -> {repo}", flush=True)
|
| 54 |
except Exception as e:
|
| 55 |
print(f"[hf] push failed for {path}: {e}", flush=True)
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
def _push_hf(path, repo):
|
| 47 |
+
"""Upload a single checkpoint file to HF (background thread)."""
|
| 48 |
if not repo:
|
| 49 |
return
|
| 50 |
try:
|
| 51 |
+
from huggingface_hub import HfApi
|
| 52 |
+
token = os.environ.get("HF_TOKEN")
|
| 53 |
+
if not token:
|
| 54 |
+
for p in (os.path.join(HERE, ".env"),
|
| 55 |
+
os.path.join(os.path.dirname(HERE), ".env"),
|
| 56 |
+
os.path.join(os.path.expanduser("~"), ".env")):
|
| 57 |
+
if os.path.exists(p):
|
| 58 |
+
for line in open(p, encoding="utf-8"):
|
| 59 |
+
if line.strip().startswith("HF_TOKEN"):
|
| 60 |
+
token = line.split("=", 1)[1].strip().strip('"').strip("'")
|
| 61 |
+
api = HfApi(token=token)
|
| 62 |
+
api.upload_file(path_or_fileobj=path,
|
| 63 |
+
path_in_repo=os.path.basename(path),
|
| 64 |
+
repo_id=repo, repo_type="model")
|
| 65 |
print(f"[hf] pushed {os.path.basename(path)} -> {repo}", flush=True)
|
| 66 |
except Exception as e:
|
| 67 |
print(f"[hf] push failed for {path}: {e}", flush=True)
|