Datasets:
Download scripts/fetch_ckpts.py from Cross-Mergeability/crossarch-accuracy: direct link, hf CLI and curl.
- Browser
- Download file 3.37 kB
-
https://huggingface.co/datasets/Cross-Mergeability/crossarch-accuracy/resolve/main/scripts/fetch_ckpts.py
- Command line
-
hf download hf://datasets/Cross-Mergeability/crossarch-accuracy/scripts/fetch_ckpts.py
-
curl -L -o fetch_ckpts.py https://huggingface.co/datasets/Cross-Mergeability/crossarch-accuracy/resolve/main/scripts/fetch_ckpts.py
3.37 kB
| """Download pythia / zh-pythia revisions, re-save as fp16 safetensors, delete the HF snapshot. | |
| Keeps the on-disk footprint at ~2.8 GB per checkpoint instead of ~5.6 GB, and never holds | |
| more than one HF snapshot at a time. | |
| """ | |
| import os, sys, gc, shutil, json, glob | |
| import torch | |
| from huggingface_hub import snapshot_download | |
| from safetensors.torch import load_file, save_file | |
| CACHE = "/root/crossarch-accuracy/cache/ckpt" | |
| os.makedirs(CACHE, exist_ok=True) | |
| FAM = { | |
| "pythia": dict(repo="EleutherAI/pythia-1.4b", fmt="step{}", | |
| steps=[0,1,128,512,1000,2000,4000,8000,16000,32000,64000,96000,128000,143000]), | |
| "zhpythia": dict(repo="SJTU-CL/Zh-Pythia-1.4B", fmt="checkpoint-{}", | |
| steps=[1000,5000,10000,20000,40000,60000,80000,90000]), | |
| } | |
| def out_path(fam, step): | |
| return f"{CACHE}/{fam}_{step}.safetensors" | |
| def fetch(fam, step): | |
| op = out_path(fam, step) | |
| if os.path.exists(op): | |
| return op | |
| cfg = FAM[fam]; rev = cfg["fmt"].format(step) | |
| d = None | |
| for pats in (["*.json", "*.safetensors"], ["*.json", "pytorch_model.bin"]): | |
| try: | |
| d = snapshot_download(cfg["repo"], revision=rev, allow_patterns=pats, max_workers=4) | |
| if glob.glob(os.path.join(d, "*.safetensors")) or os.path.exists(os.path.join(d, "pytorch_model.bin")): | |
| break | |
| d = None | |
| except Exception as e: | |
| print(" try failed", pats, repr(e)[:120], flush=True) | |
| if d is None: | |
| raise RuntimeError(f"no weights for {fam} {step}") | |
| sd = {} | |
| st = sorted(glob.glob(os.path.join(d, "*.safetensors"))) | |
| if st: | |
| for f in st: sd.update(load_file(f)) | |
| else: | |
| sd = torch.load(os.path.join(d, "pytorch_model.bin"), map_location="cpu", weights_only=True) | |
| keep = {k: v.to(torch.float16).contiguous() for k, v in sd.items() | |
| if torch.is_tensor(v) and v.is_floating_point() | |
| and "rotary" not in k and "masked_bias" not in k and "attention.bias" not in k | |
| and "attn.bias" not in k} | |
| save_file(keep, op + ".tmp") | |
| os.replace(op + ".tmp", op) | |
| # keep config/tokenizer once per family | |
| meta = f"{CACHE}/{fam}_meta" | |
| if not os.path.isdir(meta): | |
| os.makedirs(meta, exist_ok=True) | |
| for f in glob.glob(os.path.join(d, "*.json")): | |
| shutil.copy(f, meta) | |
| del sd, keep; gc.collect() | |
| # purge the HF snapshot for this revision (blobs are per-revision here) | |
| try: | |
| real = os.path.realpath(d) | |
| root = real.split("/snapshots/")[0] | |
| for link in glob.glob(os.path.join(real, "*")): | |
| tgt = os.path.realpath(link) | |
| if os.path.isfile(tgt) and "/blobs/" in tgt and os.path.getsize(tgt) > 50e6: | |
| os.remove(tgt) | |
| shutil.rmtree(real, ignore_errors=True) | |
| except Exception as e: | |
| print(" purge warn", repr(e)[:120], flush=True) | |
| return op | |
| if __name__ == "__main__": | |
| shard, nshard = int(sys.argv[1]), int(sys.argv[2]) | |
| jobs = [(f, s) for f in FAM for s in FAM[f]["steps"]] | |
| for i, (f, s) in enumerate(jobs): | |
| if i % nshard != shard: continue | |
| try: | |
| p = fetch(f, s) | |
| print("OK", f, s, os.path.getsize(p) // 2**20, "MiB", flush=True) | |
| except Exception as e: | |
| print("FAIL", f, s, repr(e)[:300], flush=True) | |
| print("SHARDDONE", shard, flush=True) | |