Datasets:
Download scripts/model_io.py from Cross-Mergeability/crossarch-accuracy: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/datasets/Cross-Mergeability/crossarch-accuracy/resolve/main/scripts/model_io.py
- Command line
-
hf download hf://datasets/Cross-Mergeability/crossarch-accuracy/scripts/model_io.py
-
curl -L -o model_io.py https://huggingface.co/datasets/Cross-Mergeability/crossarch-accuracy/resolve/main/scripts/model_io.py
1.53 kB
| import os, json, glob, gc | |
| import torch | |
| from safetensors.torch import load_file | |
| from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer | |
| CACHE = "/root/crossarch-accuracy/cache/ckpt" | |
| def meta_dir(fam): return f"{CACHE}/{fam}_meta" | |
| def sd_path(fam, step): return f"{CACHE}/{fam}_{step}.safetensors" | |
| def load_sd(fam, step, device="cpu", dtype=torch.float32): | |
| return {k: v.to(device, dtype) for k, v in load_file(sd_path(fam, step)).items()} | |
| def build_model(fam, device): | |
| cfg = AutoConfig.from_pretrained(meta_dir(fam)) | |
| m = AutoModelForCausalLM.from_config(cfg, dtype=torch.float16) | |
| m = m.to(device).eval() | |
| tok = AutoTokenizer.from_pretrained(meta_dir(fam)) | |
| if tok.pad_token is None: tok.pad_token = tok.eos_token | |
| return m, tok | |
| ALIAS = {"embed_out.weight": "lm_head.weight", "lm_head.weight": "embed_out.weight"} | |
| def install(model, sd): | |
| """Copy a state dict into a live model, tolerating the transformers v4/v5 head rename.""" | |
| params = dict(model.named_parameters()); bufs = dict(model.named_buffers()) | |
| miss = [] | |
| for k, v in sd.items(): | |
| cands = [k, ALIAS.get(k), | |
| k.replace("gpt_neox.", "") if k.startswith("gpt_neox.") else "gpt_neox." + k] | |
| t = None | |
| for c in cands: | |
| if c is None: continue | |
| t = params.get(c, bufs.get(c)) | |
| if t is not None: break | |
| if t is None or t.shape != v.shape: | |
| miss.append(k); continue | |
| t.copy_(v.to(t.dtype)) | |
| return miss | |