Upload folder using huggingface_hub
Browse files117 unmodified lines1181191201211221231241251 unmodified line127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177117 unmodified linesreturn 4, 32 # 4 micro batch * 32 accum = 128 effective# --- 5. Self-Contained Cloud Download Helper ---def ensure_cloud_data_and_checkpoint(stage: str, data_dir: str, ckpt_path: str, token: str = None):stage_subfolder = "sft_v15_stage1" if stage == "stage1" else "sft_v15_stage2"target_data_folder = os.path.join(data_dir, stage_subfolder)1 unmodified line"train_tokens.npy", "train_labels.npy", "train_offsets.npy", "train_domains.npy","val_tokens.npy", "val_labels.npy", "val_offsets.npy", "val_domains.npy"]missing_data = any(not os.path.exists(os.path.join(target_data_folder, f)) for f in needed_data_files)if missing_data:print(f"\n🌐 Dataset missing locally for {stage_subfolder}. Downloading from Hugging Face Hub (ViuAI/viuai-500m-sft-tokenized)...")os.makedirs(target_data_folder, exist_ok=True)for f in needed_data_files + ["train.jsonl", "val.jsonl", "metadata.json"]:try:hf_hub_download(repo_id="ViuAI/viuai-500m-sft-tokenized",subfolder=stage_subfolder,filename=f,local_dir=data_dir,repo_type="dataset",token=token)except Exception as e:passprint(f"✅ Downloaded {stage_subfolder} dataset successfully!")if not os.path.exists(ckpt_path):print(f"\n🌐 Initial Checkpoint missing at {ckpt_path}. Downloading from Hugging Face Hub (ViuAI/ViuAI-500M)...")os.makedirs(os.path.dirname(ckpt_path), exist_ok=True)if stage == "stage1":hf_hub_download(repo_id="ViuAI/ViuAI-500M",filename="checkpoints/ckpt_latest.pt",local_dir=os.path.dirname(os.path.dirname(ckpt_path)),token=token)else:try:hf_hub_download(repo_id="ViuAI/ViuAI-500M",filename="sft_checkpoints/sft_v15/sft_v15_stage1.pt",local_dir=os.path.dirname(os.path.dirname(os.path.dirname(ckpt_path))),token=token)except Exception:print(" ⚠️ Stage 1 checkpoint not found in cloud, falling back to base checkpoint.")hf_hub_download(repo_id="ViuAI/ViuAI-500M",filename="checkpoints/ckpt_latest.pt",local_dir=os.path.dirname(os.path.dirname(ckpt_path)),token=token)# --- 6. Main Training Pipeline ---117 unmodified lines1181191201211221231241251261271281291301311321331341351361371381391401411 unmodified line143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204117 unmodified linesreturn 4, 32 # 4 micro batch * 32 accum = 128 effective# --- 5. Self-Contained Cloud Download Helper (Git LFS Pointer Resistant) ---def is_valid_checkpoint(path: str) -> bool:if not os.path.exists(path):return False# A Git LFS pointer text file is ~130 bytes; a 500M checkpoint is ~1.0 GBif os.path.getsize(path) < 10 * 1024 * 1024: # Under 10MB is a pointer or corrupt filereturn Falsereturn Truedef is_valid_data_file(path: str) -> bool:if not os.path.exists(path):return Falseif os.path.getsize(path) < 1000: # Under 1KB is a pointerreturn Falsereturn Truedef ensure_cloud_data_and_checkpoint(stage: str, data_dir: str, ckpt_path: str, token: str = None):import shutilstage_subfolder = "sft_v15_stage1" if stage == "stage1" else "sft_v15_stage2"target_data_folder = os.path.join(data_dir, stage_subfolder)1 unmodified line"train_tokens.npy", "train_labels.npy", "train_offsets.npy", "train_domains.npy","val_tokens.npy", "val_labels.npy", "val_offsets.npy", "val_domains.npy"]missing_data = any(not is_valid_data_file(os.path.join(target_data_folder, f)) for f in needed_data_files)if missing_data:print(f"\n🌐 Dataset missing locally for {stage_subfolder}. Downloading from Hugging Face Hub (ViuAI/viuai-500m-sft-tokenized)...")os.makedirs(target_data_folder, exist_ok=True)for f in needed_data_files + ["train.jsonl", "val.jsonl", "metadata.json"]:try:downloaded = hf_hub_download(repo_id="ViuAI/viuai-500m-sft-tokenized",subfolder=stage_subfolder,filename=f,repo_type="dataset",token=token)dest = os.path.join(target_data_folder, f)if not os.path.exists(dest) or not is_valid_data_file(dest):shutil.copy(downloaded, dest)except Exception as e:passprint(f"✅ Downloaded {stage_subfolder} dataset successfully!")if not is_valid_checkpoint(ckpt_path):if os.path.exists(ckpt_path):print(f" ⚠️ Existing checkpoint at {ckpt_path} is a Git LFS pointer text file ({os.path.getsize(ckpt_path)} bytes). Replacing with real binary...")try:os.remove(ckpt_path)except Exception:passprint(f"\n🌐 Downloading Checkpoint from Hugging Face Hub (ViuAI/ViuAI-500M)...")os.makedirs(os.path.dirname(ckpt_path), exist_ok=True)if stage == "stage1":downloaded = hf_hub_download(repo_id="ViuAI/ViuAI-500M",filename="checkpoints/ckpt_latest.pt",token=token)shutil.copy(downloaded, ckpt_path)print(f"✅ Downloaded Base Checkpoint ({os.path.getsize(ckpt_path)/(1024**3):.2f} GB) to {ckpt_path}")else:try:downloaded = hf_hub_download(repo_id="ViuAI/ViuAI-500M",filename="sft_checkpoints/sft_v15/sft_v15_stage1.pt",token=token)shutil.copy(downloaded, ckpt_path)print(f"✅ Downloaded Stage 1 Checkpoint ({os.path.getsize(ckpt_path)/(1024**3):.2f} GB) to {ckpt_path}")except Exception:print(" ⚠️ Stage 1 checkpoint not found in cloud, falling back to base checkpoint.")downloaded = hf_hub_download(repo_id="ViuAI/ViuAI-500M",filename="checkpoints/ckpt_latest.pt",token=token)shutil.copy(downloaded, ckpt_path)print(f"✅ Downloaded Base Checkpoint ({os.path.getsize(ckpt_path)/(1024**3):.2f} GB) to {ckpt_path}")# --- 6. Main Training Pipeline ---
code/train_sft.py+39-12