Download code/w_export.py from cybertronak/layerlab-store: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/datasets/cybertronak/layerlab-store/resolve/main/code/w_export.py
- Command line
-
hf download hf://datasets/cybertronak/layerlab-store/code/w_export.py
-
curl -L -o w_export.py https://huggingface.co/datasets/cybertronak/layerlab-store/resolve/main/code/w_export.py
1.31 kB
| import sys, json | |
| from lab_common import * | |
| from transformers import AutoModelForCausalLM, BitsAndBytesConfig | |
| name, prot, out = sys.argv[1], json.loads(sys.argv[2]), sys.argv[3] | |
| skip = ["lm_head"] + [f"model.layers.{i}" for i in prot] | |
| bq = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16, bnb_4bit_use_double_quant=False, llm_int8_skip_modules=skip) | |
| m = AutoModelForCausalLM.from_pretrained(MID, token=HF, quantization_config=bq, dtype=torch.float16, device_map={"": 0}).eval() | |
| fp16, nf4, other = [], [], [] | |
| for i, ly in enumerate(m.model.layers): | |
| kinds = {type(mod).__name__ for mod in ly.modules() if isinstance(mod, torch.nn.Linear)} | |
| (fp16 if kinds == {"Linear"} else nf4 if kinds == {"Linear4bit"} else other).append(i) | |
| print("layer layout -> fp16:", fp16, "| nf4:", len(nf4), "layers | mixed/other:", other, flush=True) | |
| assert not other and fp16 == sorted(prot) and len(nf4) == len(m.model.layers) - len(prot), f"layout mismatch: wanted fp16 layers {sorted(prot)}, got {fp16}" | |
| m.save_pretrained(out); tok.save_pretrained(out) | |
| json.dump({"arm": name, "fp16_layers": sorted(prot), "other_layers": "NF4 (bitsandbytes, blocksize 64, no double quant, fp16 compute)"}, open(out + "/safecompress_layout.json", "w")) | |
| print("saved", out, flush=True) | |