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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Group damage sweep (battlefield teacher).
|
| 2 |
+
|
| 3 |
+
For each tied group / singleton: baseline (everything Q5_K + pins) vs variant
|
| 4 |
+
(group dropped to Q4_K). Label = PPL(variant) - PPL(baseline) — the measured
|
| 5 |
+
damage used to train the tiny importance net.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python group_damage_sweep.py --model M.gguf --imatrix I.gguf --out models/damage.jsonl [--resume]
|
| 9 |
+
|
| 10 |
+
~4.5 min/label on 1.7B (quant + PPL). ~130 labels ≈ 10h. Run overnight.
|
| 11 |
+
"""
|
| 12 |
+
import argparse
|
| 13 |
+
import json
|
| 14 |
+
import os
|
| 15 |
+
import re
|
| 16 |
+
import subprocess
|
| 17 |
+
import sys
|
| 18 |
+
import time
|
| 19 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 20 |
+
|
| 21 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 22 |
+
|
| 23 |
+
from model_reader import read_model
|
| 24 |
+
from imatrix_reader import read_imatrix, detect_tied_groups, build_importance_table
|
| 25 |
+
from classifier import build_groups
|
| 26 |
+
from config_generator import generate_flags
|
| 27 |
+
from quantizer import run_quantization
|
| 28 |
+
|
| 29 |
+
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 30 |
+
BIN = os.environ.get("LLAMA_QUANTIZE",
|
| 31 |
+
"/home/wepiqx/llama.cpp/build/bin/llama-quantize")
|
| 32 |
+
PPL = os.environ.get("LLAMA_PPL",
|
| 33 |
+
"/home/wepiqx/llama.cpp/build/bin/llama-perplexity")
|
| 34 |
+
WIKI = os.environ.get("WIKITEXT_DATA", "/mnt/Vsio/wikitext-2-raw/wiki.test.raw")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def run_ppl(model_path: str, ctx: int = 1024, n: int = 64) -> float:
|
| 38 |
+
cmd = [PPL, "-m", model_path, "-f", WIKI, "-ngl", "99",
|
| 39 |
+
"-c", str(ctx), "-n", str(n), "-b", "512", "--seed", "7"]
|
| 40 |
+
out = subprocess.run(cmd, capture_output=True, text=True, timeout=1200)
|
| 41 |
+
for line in (out.stderr + out.stdout).splitlines():
|
| 42 |
+
if "Final estimate" in line:
|
| 43 |
+
return float(line.split("PPL =")[1].split("+/-")[0].strip())
|
| 44 |
+
raise RuntimeError(f"no PPL in output for {model_path}")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def main():
|
| 48 |
+
ap = argparse.ArgumentParser()
|
| 49 |
+
ap.add_argument("--model", required=True)
|
| 50 |
+
ap.add_argument("--imatrix", required=True)
|
| 51 |
+
ap.add_argument("--out", required=True)
|
| 52 |
+
ap.add_argument("--base-tier", default="Q5_K")
|
| 53 |
+
ap.add_argument("--drop-tier", default="Q4_K")
|
| 54 |
+
ap.add_argument("--resume", action="store_true")
|
| 55 |
+
ap.add_argument("--per-tensor", action="store_true",
|
| 56 |
+
help="v2: units are single tensors (finer Gnom labels), "
|
| 57 |
+
"not tied groups")
|
| 58 |
+
ap.add_argument("--ppl-ctx", type=int, default=1024)
|
| 59 |
+
ap.add_argument("--ppl-n", type=int, default=64,
|
| 60 |
+
help="v2: short protocol (-n 32) if damage is ctx-invariant")
|
| 61 |
+
args = ap.parse_args()
|
| 62 |
+
|
| 63 |
+
model = read_model(args.model)
|
| 64 |
+
im = read_imatrix(args.imatrix)
|
| 65 |
+
tg = detect_tied_groups(im)
|
| 66 |
+
imp_table = build_importance_table(im, model)
|
| 67 |
+
|
| 68 |
+
ne_map = {k: v["n_elements"] for k, v in model.get("tensors", {}).items()}
|
| 69 |
+
for tname, info in imp_table.items():
|
| 70 |
+
if tname not in ne_map:
|
| 71 |
+
ne_map[tname] = info["n_elements"]
|
| 72 |
+
|
| 73 |
+
# pool = everything the queue would fight over (no MTP here by construction
|
| 74 |
+
# on dense models; keep pins as fixed like production)
|
| 75 |
+
from constants import get_tensor_type, EMBD_PIN_TYPES, ROUTER_PIN_TYPES
|
| 76 |
+
pool = set(ne_map.keys())
|
| 77 |
+
for pin in (EMBD_PIN_TYPES, ROUTER_PIN_TYPES):
|
| 78 |
+
pool = {n for n in pool
|
| 79 |
+
if imp_table.get(n, {}).get("type", get_tensor_type(n)) not in pin}
|
| 80 |
+
pool = {n for n in pool if "norm" not in n}
|
| 81 |
+
|
| 82 |
+
if args.per_tensor:
|
| 83 |
+
units = [[n] for n in sorted(pool)] # v2: finer labels, no sharing
|
| 84 |
+
else:
|
| 85 |
+
groups = build_groups(tg, pool, ne_map, dict(ne_map))
|
| 86 |
+
units = [g for _, (g, _, _) in sorted(groups.items())]
|
| 87 |
+
print(f"units: {len(units)}", flush=True)
|
| 88 |
+
|
| 89 |
+
done = set()
|
| 90 |
+
if args.resume and os.path.exists(args.out):
|
| 91 |
+
with open(args.out) as f:
|
| 92 |
+
for line in f:
|
| 93 |
+
done.add(json.loads(line)["unit"])
|
| 94 |
+
|
| 95 |
+
def build_file_inner(drop: list | None, path: str):
|
| 96 |
+
assignments = {}
|
| 97 |
+
for n in ne_map:
|
| 98 |
+
assignments[n] = args.base_tier
|
| 99 |
+
if drop:
|
| 100 |
+
for n in drop:
|
| 101 |
+
assignments[n] = args.drop_tier
|
| 102 |
+
flags = generate_flags(assignments, model, args.base_tier, 100000)
|
| 103 |
+
flags["imatrix"] = args.imatrix
|
| 104 |
+
ok = run_quantization(flags, args.model, path)
|
| 105 |
+
if not ok:
|
| 106 |
+
raise RuntimeError("quant failed")
|
| 107 |
+
|
| 108 |
+
def _looks_valid(path: str) -> bool:
|
| 109 |
+
"""GGUF magic + sane size (>100MB for 1.7B quants)."""
|
| 110 |
+
try:
|
| 111 |
+
if os.path.getsize(path) < 100 * 1024 * 1024:
|
| 112 |
+
return False
|
| 113 |
+
with open(path, "rb") as f:
|
| 114 |
+
return f.read(4) == b"GGUF"
|
| 115 |
+
except OSError:
|
| 116 |
+
return False
|
| 117 |
+
|
| 118 |
+
def _wait_then_build(prev_fut, unit, path):
|
| 119 |
+
prev_fut.result()
|
| 120 |
+
build_file_inner(unit, path)
|
| 121 |
+
|
| 122 |
+
build_file = build_file_inner
|
| 123 |
+
|
| 124 |
+
tmp = os.path.join(REPO_ROOT, "models", ".sweep_tmp.gguf")
|
| 125 |
+
if "BASELINE" not in done:
|
| 126 |
+
build_file(None, tmp)
|
| 127 |
+
base_ppl = run_ppl(tmp, args.ppl_ctx, args.ppl_n)
|
| 128 |
+
with open(args.out, "a") as f:
|
| 129 |
+
f.write(json.dumps({"unit": "BASELINE", "ppl": base_ppl}) + "\n")
|
| 130 |
+
print(f"BASELINE ppl={base_ppl:.4f}", flush=True)
|
| 131 |
+
else:
|
| 132 |
+
with open(args.out) as f:
|
| 133 |
+
base_ppl = [json.loads(l)["ppl"] for l in f
|
| 134 |
+
if json.loads(l)["unit"] == "BASELINE"][0]
|
| 135 |
+
|
| 136 |
+
def unit_tag(unit):
|
| 137 |
+
return "+".join(n.replace(".weight", "").split(".")[-1] + "@" +
|
| 138 |
+
n.split(".")[1] for n in unit
|
| 139 |
+
if n.startswith("blk.")) or "global"
|
| 140 |
+
|
| 141 |
+
# Pipelined: quant(unit i+1) on CPU overlaps PPL(unit i) on GPU.
|
| 142 |
+
# UNIQUE tmp file per unit: rotating buffers raced when a slow PPL
|
| 143 |
+
# (loaded system) let a later build overwrite the file PPL still read.
|
| 144 |
+
# ≤3 files alive at once (~3.4GB), deleted right after use.
|
| 145 |
+
todo = [(unit_tag(u), u) for u in units if unit_tag(u) not in done]
|
| 146 |
+
safe = lambda tag, i: re.sub(r"[^A-Za-z0-9_@+-]", "_", tag)[:60]
|
| 147 |
+
ex = ThreadPoolExecutor(max_workers=2)
|
| 148 |
+
try:
|
| 149 |
+
if todo:
|
| 150 |
+
tag0, unit0 = todo[0]
|
| 151 |
+
buf0 = os.path.join(REPO_ROOT, "models",
|
| 152 |
+
f".sweep_0_{safe(tag0, 0)}.gguf")
|
| 153 |
+
fut_q = ex.submit(build_file, unit0, buf0)
|
| 154 |
+
pending = [(tag0, unit0, buf0, fut_q)]
|
| 155 |
+
for i, (tag, unit) in enumerate(todo[1:], start=1):
|
| 156 |
+
buf = os.path.join(REPO_ROOT, "models",
|
| 157 |
+
f".sweep_{i}_{safe(tag, i)}.gguf")
|
| 158 |
+
pending.append((tag, unit, buf,
|
| 159 |
+
ex.submit(_wait_then_build, pending[-1][3],
|
| 160 |
+
unit, buf)))
|
| 161 |
+
for idx, (tag, unit, buf, fq) in enumerate(pending):
|
| 162 |
+
try:
|
| 163 |
+
fq.result() # quant done (previous PPL overlapped it)
|
| 164 |
+
except Exception as e:
|
| 165 |
+
print(f"[{idx + 1}/{len(pending)}] {tag} QUANT failed "
|
| 166 |
+
f"({type(e).__name__}), skipped", flush=True)
|
| 167 |
+
continue
|
| 168 |
+
if not _looks_valid(buf):
|
| 169 |
+
print(f"[{idx + 1}/{len(pending)}] {tag} CORRUPT file, "
|
| 170 |
+
f"skipped", flush=True)
|
| 171 |
+
continue
|
| 172 |
+
try:
|
| 173 |
+
p = run_ppl(buf, args.ppl_ctx, args.ppl_n)
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f"[{idx + 1}/{len(pending)}] {tag} PPL failed "
|
| 176 |
+
f"({type(e).__name__}: {str(e)[:100]}), skipped",
|
| 177 |
+
flush=True)
|
| 178 |
+
continue
|
| 179 |
+
finally:
|
| 180 |
+
try:
|
| 181 |
+
os.remove(buf)
|
| 182 |
+
except OSError:
|
| 183 |
+
pass
|
| 184 |
+
with open(args.out, "a") as f:
|
| 185 |
+
f.write(json.dumps({"unit": tag, "tensors": unit,
|
| 186 |
+
"ppl": p, "damage": p - base_ppl})
|
| 187 |
+
+ "\n")
|
| 188 |
+
print(f"[{idx + 1}/{len(pending)}] {tag} "
|
| 189 |
+
f"damage={p - base_ppl:+.4f}", flush=True)
|
| 190 |
+
finally:
|
| 191 |
+
ex.shutdown(wait=False)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
if __name__ == "__main__":
|
| 195 |
+
main()
|