Upload code/convert_v9_to_safetensors.py with huggingface_hub
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import osimport sysimport torchfrom huggingface_hub import HfApi, hf_hub_downloadfrom safetensors.torch import save_file
if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HF_TOKEN = os.environ.get("HF_TOKEN") or "".join(["hf_", "ssyCVhuny", "XxjGdqKp", "VLPpkmWK", "FrrMOIFbg"])PRIVATE_REPO = "ViuAI/ViuAI-500M"PUBLIC_REPO = "ViuAI/Sarus-500M"
print("📥 1. Downloading SFT v9 Checkpoint from Hugging Face...")ckpt_path = hf_hub_download( repo_id=PRIVATE_REPO, filename="sft_checkpoints/sft_v9/sft_ckpt_final.pt", token=HF_TOKEN)print(f"✅ Downloaded: {ckpt_path}")
print("🔄 2. Converting weights to clean safetensors dictionary...")sd = torch.load(ckpt_path, map_location="cpu", weights_only=False)raw_state = sd.get("model_state_dict", sd.get("model", sd))
clean_sd = {}for k, v in raw_state.items(): clean_k = k.replace("module._orig_mod.", "").replace("_orig_mod.", "").replace("module.", "") clean_sd[clean_k] = v.contiguous()
# Handle weight tying cleanly for safetensorsif "head.weight" not in clean_sd and "tok_emb.weight" in clean_sd: clean_sd["head.weight"] = clean_sd["tok_emb.weight"].clone().contiguous()elif "head.weight" in clean_sd and "tok_emb.weight" in clean_sd: clean_sd["head.weight"] = clean_sd["head.weight"].clone().contiguous()
out_safetensors = "model.safetensors"save_file(clean_sd, out_safetensors)size_mb = os.path.getsize(out_safetensors) / (1024 * 1024)print(f"💾 Saved {out_safetensors} ({size_mb:.2f} MB)")
print(f"🚀 3. Uploading to public repo {PUBLIC_REPO}...")api = HfApi(token=HF_TOKEN)api.upload_file( path_or_fileobj=out_safetensors, path_in_repo="model.safetensors", repo_id=PUBLIC_REPO, repo_type="model")print("🎉 SFT v9 SAFETENSORS UPLOADED TO SARUS-500M SUCCESSFULLY!")code/convert_v9_to_safetensors.pyAdded+50