Download quantization/fix_mtp.py from ramachandrajoshi/LoopedPhai-A1-35B: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/ramachandrajoshi/LoopedPhai-A1-35B/resolve/main/quantization/fix_mtp.py
- Command line
-
hf download hf://ramachandrajoshi/LoopedPhai-A1-35B/quantization/fix_mtp.py
-
curl -L -o fix_mtp.py https://huggingface.co/ramachandrajoshi/LoopedPhai-A1-35B/resolve/main/quantization/fix_mtp.py
2.54 kB
| import json, os, torch | |
| from safetensors import safe_open | |
| from safetensors.torch import save_file | |
| O = "out/Phai-A1-35B-1M-FP8" | |
| srcidx = json.load(open("src/model.safetensors.index.json"))["weight_map"] | |
| outidx_all = json.load(open(f"{O}/model.safetensors.index.json")); outidx = outidx_all["weight_map"] | |
| def get(d, idx, k): | |
| with safe_open(os.path.join(d, idx[k]), "pt") as f: return f.get_tensor(k) | |
| # 1) verify the split convention llm-compressor used for backbone experts (layer 0, experts 0 and 77) | |
| gu = get("src", srcidx, "model.language_model.layers.0.mlp.experts.gate_up_proj") | |
| dn = get("src", srcidx, "model.language_model.layers.0.mlp.experts.down_proj") | |
| I = gu.shape[1] // 2 | |
| for e in (0, 77): | |
| for nm, ref in (("gate_proj", gu[e, :I]), ("up_proj", gu[e, I:]), ("down_proj", dn[e])): | |
| k = f"model.language_model.layers.0.mlp.experts.{e}.{nm}" | |
| w = get(O, outidx, k + ".weight").float(); s = get(O, outidx, k + ".weight_scale").float() | |
| deq = (w.reshape(w.shape[0]//128, 128, w.shape[1]//128, 128) * s[:, None, :, None]).reshape(w.shape) | |
| rel = ((deq - ref.float()).norm() / ref.float().norm()).item() | |
| print(f"expert {e} {nm}: fp8{tuple(w.shape)} scale{tuple(s.shape)} {s.dtype} rel_err={rel:.4f}") | |
| assert rel < 0.05, "split convention mismatch" | |
| # 2) unfuse MTP experts with the same convention | |
| mf = "model_mtp.safetensors" | |
| with safe_open(os.path.join(O, mf), "pt") as f: | |
| t = {k: f.get_tensor(k) for k in f.keys()} | |
| new = {} | |
| for k, v in t.items(): | |
| if k.endswith("mlp.experts.gate_up_proj"): | |
| p = k[: -len("gate_up_proj")] | |
| for e in range(v.shape[0]): | |
| new[f"{p}{e}.gate_proj.weight"] = v[e, :I].contiguous() | |
| new[f"{p}{e}.up_proj.weight"] = v[e, I:].contiguous() | |
| elif k.endswith("mlp.experts.down_proj"): | |
| p = k[: -len("down_proj")] | |
| for e in range(v.shape[0]): | |
| new[f"{p}{e}.down_proj.weight"] = v[e].contiguous() | |
| else: | |
| new[k] = v | |
| save_file(new, os.path.join(O, mf), metadata={"format": "pt"}) | |
| for k in [k for k in outidx if k.startswith("mtp.")]: del outidx[k] | |
| for k in new: outidx[k] = mf | |
| json.dump(outidx_all, open(f"{O}/model.safetensors.index.json", "w"), indent=2) | |
| print("mtp tensors:", len(new)) | |
| # 3) keep the whole BF16 MTP head out of FP8 in serving engines | |
| cfg = json.load(open(f"{O}/config.json")) | |
| ig = cfg["quantization_config"]["ignore"] | |
| if "re:.*mtp\..*" not in ig: ig.append("re:.*mtp\..*") | |
| json.dump(cfg, open(f"{O}/config.json", "w"), indent=2) | |
| print("ignore tail:", ig[-3:]) | |