LuffyTheFox

Untitled

Mar 26th, 2026
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.70 KB | None | 0 0
  1. # ============================================================================
  2. # GGUF Add-Difference Merge — Disk-Optimized
  3. #
  4. # output = A + (B - C) * weight
  5. #
  6. # Pipeline:
  7. # 1) Download B and C
  8. # 2) Compute delta = B - C → GGUF (only actually changed tensors)
  9. # 3) Delete B and C
  10. # 4) Download A
  11. # 5) Apply output = A + delta * weight
  12. # 6) Delete A, delta (keep only merged output)
  13. #
  14. # • Delta is standard GGUF in original quantization format
  15. # • Only tensors where raw bytes B ≠ C are included
  16. # • Two-pass delta: pass1=raw byte compare, pass2=compute+write diffs
  17. # • Streaming GGUF write: one tensor in RAM at a time
  18. # ============================================================================
  19.  
  20. import subprocess, sys, os, gc, time, shutil
  21. import numpy as np
  22.  
  23. WORK_DIR = "/content/gguf_workspace"
  24. os.makedirs(WORK_DIR, exist_ok=True)
  25.  
  26. def install():
  27. for pkg in ["huggingface_hub[hf_xet]", "gguf"]:
  28. subprocess.check_call(
  29. [sys.executable, "-m", "pip", "install", "-q", pkg],
  30. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
  31. )
  32. print("[OK] Dependencies installed.")
  33.  
  34. install()
  35.  
  36. from huggingface_hub import hf_hub_download
  37. from gguf import GGUFReader, GGUFWriter
  38. from gguf.constants import GGMLQuantizationType, GGML_QUANT_SIZES
  39. from gguf import quants as gguf_quants
  40.  
  41. # ── Patch IQ block sizes ─────────────────────────────────────────────────
  42. _IQ = {
  43. "IQ1_S":(256,50),"IQ1_M":(256,56),"IQ2_XXS":(256,66),"IQ2_XS":(256,74),
  44. "IQ2_S":(256,82),"IQ3_XXS":(256,98),"IQ3_S":(256,110),
  45. "IQ4_NL":(32,18),"IQ4_XS":(256,136),
  46. }
  47. for _n,_g in _IQ.items():
  48. _qt = getattr(GGMLQuantizationType, _n, None)
  49. if _qt is not None and _qt not in GGML_QUANT_SIZES:
  50. GGML_QUANT_SIZES[_qt] = _g
  51.  
  52. # ── Probe quantize support ──────────────────────────────────────────────
  53. _QUANTIZE_SUPPORTED = set()
  54.  
  55. def _probe_quantize():
  56. for qt in GGMLQuantizationType:
  57. if qt in (GGMLQuantizationType.F32, GGMLQuantizationType.F16,
  58. GGMLQuantizationType.BF16):
  59. _QUANTIZE_SUPPORTED.add(qt)
  60. elif qt in GGML_QUANT_SIZES:
  61. be, _ = GGML_QUANT_SIZES[qt]
  62. try:
  63. r = gguf_quants.quantize(np.zeros(be, np.float32), qt)
  64. if r is not None and len(r.ravel().tobytes()) > 0:
  65. _QUANTIZE_SUPPORTED.add(qt)
  66. except Exception: pass
  67.  
  68. _probe_quantize()
  69. print(f"[INFO] quantize() supported: {sorted([q.name for q in _QUANTIZE_SUPPORTED])}")
  70.  
  71. # ── Check streaming GGUF write ──────────────────────────────────────────
  72. _STREAMING = (
  73. hasattr(GGUFWriter, 'add_tensor_info') and
  74. hasattr(GGUFWriter, 'write_tensor_data') and
  75. hasattr(GGUFWriter, 'write_ti_data_to_file')
  76. )
  77. print(f"[INFO] GGUF streaming write: {'YES' if _STREAMING else 'NO (buffered)'}")
  78.  
  79. # ============================================================================
  80. # CONFIGURATION
  81. # ============================================================================
  82. MODEL_A = {
  83. "repo": "Jackrong/Qwen3.5-27B-Claude-4.6-Opus-Reasoning-Distilled-v2-GGUF",
  84. "file": "Qwen3.5-27B.Q8_0.gguf",
  85. }
  86. MODEL_B = {
  87. "repo": "HauhauCS/Qwen3.5-27B-Uncensored-HauhauCS-Aggressive",
  88. "file": "Qwen3.5-27B-Uncensored-HauhauCS-Aggressive-Q8_0.gguf",
  89. }
  90. MODEL_C = {
  91. "repo": "unsloth/Qwen3.5-27B-GGUF",
  92. "file": "Qwen3.5-27B-Q8_0.gguf",
  93. }
  94.  
  95. MERGE_WEIGHT = 1.0
  96. OUTPUT_NAME = "Qwen3.5-27B-Q8_0-merged.gguf"
  97. OUTPUT_PATH = os.path.join(WORK_DIR, OUTPUT_NAME)
  98. DELTA_PATH = os.path.join(WORK_DIR, "_delta.gguf")
  99. CHUNK_BYTES = 4 * 1024 * 1024
  100.  
  101. # ============================================================================
  102. # TENSOR CLASSIFICATION
  103. # ============================================================================
  104. FINETUNE_SUFFIXES = (
  105. "attn_q.weight","attn_k.weight","attn_v.weight","attn_output.weight",
  106. "ssm_in.weight","ssm_out.weight","ssm_a.weight",
  107. "ssm_d.weight","ssm_dt.weight",
  108. "ffn_gate.weight","ffn_up.weight","ffn_down.weight",
  109. "ffn_gate_exps.weight","ffn_up_exps.weight","ffn_down_exps.weight",
  110. "ffn_gate_inp.weight","ffn_gate_shexp.weight",
  111. "ffn_up_shexp.weight","ffn_down_shexp.weight",
  112. "attn_gate.weight",
  113. )
  114.  
  115. PROTECTED_SUBSTRINGS = (
  116. "token_embd","output.weight","_norm",".bias",
  117. "rope_freqs","pos_embd",
  118. "v.blk.","v.patch_embd","v.position_embd",
  119. "v.post_ln","v.pre_ln","v.output_norm","v.proj",
  120. "mm.","mtp.",
  121. )
  122.  
  123. FLOAT_QTYPES = {
  124. GGMLQuantizationType.F32, GGMLQuantizationType.F16, GGMLQuantizationType.BF16,
  125. }
  126.  
  127. # ============================================================================
  128. # UTILITIES
  129. # ============================================================================
  130.  
  131. def disk_free_gb(p=WORK_DIR):
  132. st = os.statvfs(p); return (st.f_bavail * st.f_frsize) / 1073741824
  133.  
  134. def file_size_gb(p):
  135. return os.path.getsize(p) / 1073741824 if os.path.exists(p) else 0.0
  136.  
  137. def fmt_bytes(n):
  138. for u in ("B","KB","MB","GB"):
  139. if n < 1024: return f"{n:.1f} {u}"
  140. n /= 1024
  141. return f"{n:.1f} TB"
  142.  
  143. def safe_remove(path, protect=None):
  144. try:
  145. a = os.path.abspath(path)
  146. if protect:
  147. if isinstance(protect, str): protect = [protect]
  148. for p in protect:
  149. if a == os.path.abspath(p): return
  150. if os.path.isfile(a):
  151. sz = file_size_gb(a)
  152. os.remove(a)
  153. print(f"[CLEANUP] Removed {os.path.basename(a)} ({sz:.2f} GB)")
  154. except OSError: pass
  155.  
  156. def flush_and_close(f, label="file"):
  157. f.flush(); os.fsync(f.fileno()); f.close()
  158. print(f"[FLUSH] {label}: flush + fsync done.")
  159.  
  160. def block_align(qt):
  161. if qt in FLOAT_QTYPES:
  162. return {GGMLQuantizationType.F32:4,GGMLQuantizationType.F16:2,
  163. GGMLQuantizationType.BF16:2}[qt]
  164. if qt in GGML_QUANT_SIZES: return GGML_QUANT_SIZES[qt][1]
  165. return 1
  166.  
  167. def align_down(v, a):
  168. return (v // a) * a if a > 0 else v
  169.  
  170. def print_workspace():
  171. print(f"\n[FS] {WORK_DIR}:")
  172. for fn in sorted(os.listdir(WORK_DIR)):
  173. fp = os.path.join(WORK_DIR, fn)
  174. if os.path.isfile(fp): print(f" {fn} ({file_size_gb(fp):.2f} GB)")
  175. print(f" Disk free: {disk_free_gb():.1f} GB")
  176.  
  177. # ============================================================================
  178. # CLASSIFICATION
  179. # ============================================================================
  180.  
  181. def is_finetune_weight(name):
  182. return any(name.endswith(s) for s in FINETUNE_SUFFIXES)
  183.  
  184. def is_protected(name):
  185. return any(p in name for p in PROTECTED_SUBSTRINGS)
  186.  
  187. def classify_bc(name, ti_b, ti_c):
  188. if is_protected(name): return "protect", "structural/norm/embd"
  189. if not is_finetune_weight(name): return "protect", "not fine-tunable"
  190. if ti_c is None: return "skip", "missing in C"
  191. if ti_b["size"] != ti_c["size"]:
  192. return "skip", f"size mismatch B={ti_b['size']:,} C={ti_c['size']:,}"
  193. if ti_b["qtype_name"] != ti_c["qtype_name"]:
  194. return "skip", f"type mismatch B={ti_b['qtype_name']} C={ti_c['qtype_name']}"
  195. if ti_b["n_elements"] != ti_c["n_elements"]:
  196. return "skip", f"elem mismatch"
  197. if ti_b["qtype"] not in _QUANTIZE_SUPPORTED:
  198. return "skip", f"{ti_b['qtype_name']} no quantize()"
  199. return "merge", "fine-tunable"
  200.  
  201. # ============================================================================
  202. # GGUF PARSING
  203. # ============================================================================
  204.  
  205. def parse_gguf(path):
  206. reader = GGUFReader(path)
  207. tensors = {}
  208. for t in reader.tensors:
  209. tensors[t.name] = {
  210. "offset": int(t.data_offset), "size": int(t.n_bytes),
  211. "qtype": t.tensor_type, "qtype_name": t.tensor_type.name,
  212. "n_elements": int(t.n_elements), "shape": list(t.shape),
  213. }
  214. n = len(tensors); del reader; gc.collect()
  215. return tensors, n
  216.  
  217. # ============================================================================
  218. # RAW I/O
  219. # ============================================================================
  220.  
  221. def read_chunk(f, ti, off, sz):
  222. actual = min(sz, ti["size"] - off)
  223. if actual <= 0: return b""
  224. f.seek(ti["offset"] + off); return f.read(actual)
  225.  
  226. def write_chunk(f, ti, off, data):
  227. f.seek(ti["offset"] + off); f.write(data)
  228.  
  229. def raw_equal(f1, t1, f2, t2):
  230. if t1["size"] != t2["size"]: return False
  231. sz = t1["size"]; off = 0
  232. while off < sz:
  233. n = min(CHUNK_BYTES, sz - off)
  234. f1.seek(t1["offset"]+off); d1 = f1.read(n)
  235. f2.seek(t2["offset"]+off); d2 = f2.read(n)
  236. if d1 != d2: return False
  237. off += n
  238. return True
  239.  
  240. # ============================================================================
  241. # DEQUANT / QUANT
  242. # ============================================================================
  243.  
  244. def to_f32(raw, qt):
  245. if qt == GGMLQuantizationType.F32:
  246. return np.frombuffer(raw, np.float32).copy()
  247. if qt == GGMLQuantizationType.F16:
  248. return np.frombuffer(raw, np.float16).astype(np.float32)
  249. if qt == GGMLQuantizationType.BF16:
  250. u = np.frombuffer(raw, np.uint16).copy()
  251. return (u.astype(np.uint32) << 16).view(np.float32).copy()
  252. return gguf_quants.dequantize(np.frombuffer(raw, np.uint8).copy(), qt
  253. ).ravel().astype(np.float32)
  254.  
  255. def from_f32(f, qt, expected):
  256. f = np.nan_to_num(f, nan=0., posinf=0., neginf=0.)
  257. if qt == GGMLQuantizationType.F32:
  258. r = f.astype(np.float32).tobytes()
  259. elif qt == GGMLQuantizationType.F16:
  260. r = f.astype(np.float16).tobytes()
  261. elif qt == GGMLQuantizationType.BF16:
  262. r = (f.astype(np.float32).view(np.uint32) >> 16).astype(np.uint16).tobytes()
  263. elif qt in _QUANTIZE_SUPPORTED:
  264. be, _ = GGML_QUANT_SIZES[qt]
  265. n = align_down(len(f), be)
  266. if n == 0: return None
  267. try: r = gguf_quants.quantize(f[:n], qt).ravel().tobytes()
  268. except Exception: return None
  269. else: return None
  270. return r if len(r) == expected else None
  271.  
  272. def quant_nd(f, qt):
  273. f = np.nan_to_num(f, nan=0., posinf=0., neginf=0.).astype(np.float32)
  274. if qt == GGMLQuantizationType.F32: return f.view(np.uint8).copy()
  275. if qt == GGMLQuantizationType.F16: return f.astype(np.float16).view(np.uint8).copy()
  276. if qt == GGMLQuantizationType.BF16:
  277. return (f.view(np.uint32) >> 16).astype(np.uint16).view(np.uint8).copy()
  278. if qt in _QUANTIZE_SUPPORTED:
  279. be, _ = GGML_QUANT_SIZES[qt]; n = align_down(len(f), be)
  280. if n == 0: return np.zeros(0, np.uint8)
  281. return gguf_quants.quantize(f[:n], qt).ravel().copy()
  282. return np.zeros(0, np.uint8)
  283.  
  284. # ============================================================================
  285. # DOWNLOAD
  286. # ============================================================================
  287.  
  288. def download(spec, label):
  289. print(f"[DL] {label}: {spec['repo']} / {spec['file']}")
  290. p = hf_hub_download(spec["repo"], spec["file"], local_dir=WORK_DIR)
  291. print(f"[DL] {label}: {file_size_gb(p):.2f} GB -> {p}"); return p
  292.  
  293. # ============================================================================
  294. # COMPUTE ONE TENSOR DIFF → quantized uint8 ndarray
  295. # ============================================================================
  296.  
  297. def tensor_diff(f_b, tb, f_c, tc, qt):
  298. sz = tb["size"]; ba = block_align(qt)
  299. cb = align_down(CHUNK_BYTES, ba)
  300. if cb <= 0: cb = ba
  301. parts = []; off = 0
  302. while off < sz:
  303. act = align_down(min(cb, sz - off), ba)
  304. if act <= 0: break
  305. rb = read_chunk(f_b, tb, off, act)
  306. rc = read_chunk(f_c, tc, off, act)
  307. if len(rb) != act or len(rc) != act: break
  308. try:
  309. d = to_f32(rb, qt) - to_f32(rc, qt)
  310. parts.append(np.nan_to_num(d, nan=0., posinf=0., neginf=0.))
  311. except Exception:
  312. if qt in FLOAT_QTYPES:
  313. eb = {GGMLQuantizationType.F32:4,GGMLQuantizationType.F16:2,
  314. GGMLQuantizationType.BF16:2}[qt]
  315. parts.append(np.zeros(act//eb, np.float32))
  316. elif qt in GGML_QUANT_SIZES:
  317. be, bs = GGML_QUANT_SIZES[qt]
  318. parts.append(np.zeros((act//bs)*be, np.float32))
  319. del rb, rc; off += act
  320. full = np.concatenate(parts) if parts else np.zeros(tb["n_elements"], np.float32)
  321. del parts; r = quant_nd(full, qt); del full; return r
  322.  
  323. # ============================================================================
  324. # STAGE 1: COMPUTE DELTA = B - C → GGUF (only changed tensors)
  325. # ============================================================================
  326.  
  327. def compute_delta(path_b, path_c, delta_path):
  328. t0 = time.time()
  329. print("\n[DELTA] Parsing B and C...")
  330. ib, nb = parse_gguf(path_b); ic, nc = parse_gguf(path_c)
  331. print(f"[DELTA] B: {nb} tensors C: {nc} tensors")
  332.  
  333. plan = {}; mergeable = []
  334. for nm in sorted(ib.keys()):
  335. a, r = classify_bc(nm, ib[nm], ic.get(nm))
  336. plan[nm] = (a, r)
  337. if a == "merge": mergeable.append(nm)
  338.  
  339. np_ = sum(1 for v in plan.values() if v[0]=="protect")
  340. ns = sum(1 for v in plan.values() if v[0]=="skip")
  341. print(f"[DELTA] Mergeable={len(mergeable)} Protected={np_} Skipped={ns}")
  342. for nm,(a,r) in sorted(plan.items()):
  343. if a=="skip": print(f" SKIP: {nm:<48} {r}")
  344.  
  345. if not mergeable:
  346. w = GGUFWriter(delta_path, arch="delta")
  347. w.add_description("Empty delta")
  348. w.write_header_to_file(); w.write_kv_data_to_file()
  349. w.write_ti_data_to_file(); w.close()
  350. print("[DELTA] Empty delta created."); return {}
  351.  
  352. # ── Pass 1: raw byte compare ─────────────────────────────────────────
  353. print(f"\n[DELTA] Pass 1: raw byte compare ({len(mergeable)} tensors)...")
  354. fb = open(path_b,"rb"); fc = open(path_c,"rb")
  355. changed=[]; same=[]
  356. for i,nm in enumerate(mergeable):
  357. (changed if not raw_equal(fb, ib[nm], fc, ic[nm]) else same).append(nm)
  358. if (i+1)%100==0 or i+1==len(mergeable):
  359. print(f" {i+1}/{len(mergeable)} changed={len(changed)} same={len(same)}")
  360.  
  361. print(f"[DELTA] Result: {len(changed)} changed, {len(same)} identical")
  362.  
  363. if not changed:
  364. fb.close(); fc.close()
  365. w = GGUFWriter(delta_path, arch="delta")
  366. w.add_description("Empty delta: B==C")
  367. w.write_header_to_file(); w.write_kv_data_to_file()
  368. w.write_ti_data_to_file(); w.close()
  369. print("[DELTA] Empty delta."); return {"changed":0,"unchanged":len(same)}
  370.  
  371. # ── Write delta GGUF ─────────────────────────────────────────────────
  372. w = GGUFWriter(delta_path, arch="delta")
  373. w.add_description(f"Delta (B-C): {len(changed)} changed tensors. "
  374. f"B={MODEL_B['repo']}/{MODEL_B['file']} "
  375. f"C={MODEL_C['repo']}/{MODEL_C['file']}")
  376. w.add_uint32("delta.changed_count", len(changed))
  377. w.add_uint32("delta.unchanged_count", len(same))
  378.  
  379. if _STREAMING:
  380. for nm in changed:
  381. ti = ib[nm]
  382. w.add_tensor_info(name=nm, tensor_shape=ti["shape"],
  383. tensor_dtype=np.float32,
  384. tensor_nbytes=ti["size"], raw_dtype=ti["qtype"])
  385. w.write_header_to_file()
  386. w.write_kv_data_to_file()
  387. w.write_ti_data_to_file()
  388.  
  389. print(f"\n[DELTA] Pass 2: compute + stream {len(changed)} deltas...")
  390. print(f"{'#':>4} {'Tensor':<52} {'Type':<10} {'Size':>9}")
  391. print("-"*85)
  392. for i,nm in enumerate(changed):
  393. d = tensor_diff(fb, ib[nm], fc, ic[nm], ib[nm]["qtype"])
  394. w.write_tensor_data(d)
  395. print(f"{i+1:>4} {nm:<52} {ib[nm]['qtype_name']:<10} "
  396. f"{fmt_bytes(ib[nm]['size']):>9}")
  397. del d
  398. if (i+1)%30==0: gc.collect()
  399. else:
  400. print(f"\n[DELTA] Pass 2: compute + buffer {len(changed)} deltas...")
  401. print("[DELTA] WARNING: buffered mode — high RAM usage!")
  402. print(f"{'#':>4} {'Tensor':<52} {'Type':<10} {'Size':>9}")
  403. print("-"*85)
  404. for i,nm in enumerate(changed):
  405. ti = ib[nm]
  406. d = tensor_diff(fb, ti, fc, ic[nm], ti["qtype"])
  407. w.add_tensor(nm, d, raw_shape=np.array(ti["shape"],np.uint64),
  408. raw_dtype=ti["qtype"])
  409. print(f"{i+1:>4} {nm:<52} {ti['qtype_name']:<10} "
  410. f"{fmt_bytes(ti['size']):>9}")
  411. del d
  412. if (i+1)%30==0: gc.collect()
  413. w.write_header_to_file()
  414. w.write_kv_data_to_file()
  415. w.write_tensors_to_file()
  416.  
  417. fb.close(); fc.close(); w.close()
  418. el = time.time()-t0
  419. print(f"\n[DELTA] Done: {len(changed)} tensors in delta GGUF "
  420. f"({file_size_gb(delta_path):.2f} GB, {el:.1f}s)")
  421. return {"changed":len(changed),"unchanged":len(same)}
  422.  
  423. # ============================================================================
  424. # STAGE 2: APPLY DELTA TO A → output = A + delta * weight
  425. # ============================================================================
  426.  
  427. def apply_delta(path_a, delta_path, weight, output_path):
  428. t0 = time.time()
  429. print("\n[APPLY] Parsing delta GGUF...")
  430. id_, nd = parse_gguf(delta_path)
  431. print(f"[APPLY] Delta: {nd} changed tensors")
  432.  
  433. if nd == 0:
  434. print("[APPLY] Empty delta → copying A as output.")
  435. shutil.copy2(path_a, output_path); return {}
  436.  
  437. print("[APPLY] Parsing A...")
  438. ia, na = parse_gguf(path_a)
  439. sa = os.path.getsize(path_a)
  440. print(f"[APPLY] A: {na} tensors ({sa:,} B)")
  441.  
  442. miss = [n for n in id_ if n not in ia]
  443. if miss:
  444. print(f"[APPLY] WARNING: {len(miss)} delta tensors missing in A")
  445. for n in miss[:5]: print(f" {n}")
  446.  
  447. free = disk_free_gb(); need = file_size_gb(path_a)
  448. print(f"[APPLY] Disk: free={free:.1f}GB need={need:.2f}GB")
  449. if free < need * 1.05: sys.exit("[APPLY] ERROR: Not enough disk.")
  450.  
  451. print(f"[APPLY] Copying A → {os.path.basename(output_path)}...")
  452. shutil.copy2(path_a, output_path)
  453. assert os.path.getsize(output_path) == sa, "Copy size mismatch"
  454. print(f"[APPLY] Copy OK ({time.time()-t0:.1f}s)")
  455.  
  456. fo = open(output_path, "r+b")
  457. fd = open(delta_path, "rb")
  458.  
  459. # Architecture info
  460. attn=set(); ssm=set(); ffn=set()
  461. for nm in ia:
  462. p = nm.split(".")
  463. if len(p)>=3 and p[0]=="blk":
  464. b = f"blk.{p[1]}"
  465. if ".attn_q." in nm: attn.add(b)
  466. if ".ssm_in." in nm: ssm.add(b)
  467. if ".ffn_gate." in nm: ffn.add(b)
  468. print(f"\n[APPLY] Arch: attn={len(attn)} deltanet={len(ssm)} ffn={len(ffn)}")
  469.  
  470. names = sorted(ia.keys())
  471. print(f"\n[APPLY] output = A + Δ * {weight} ({nd} deltas in {na} tensors)")
  472. print(f"{'#':>4} {'Tensor':<52} {'Type':<10} {'Size':>9} {'Action':<10} Detail")
  473. print("-"*125)
  474.  
  475. st = {"merged":0,"protected":0,"skipped":0,"total_changed":0,
  476. "identical":0,"failed":0,"attn":0,"ssm":0,"ffn":0}
  477.  
  478. for idx, nm in enumerate(names):
  479. ta = ia[nm]; qn = ta["qtype_name"]; sz = ta["size"]
  480. if nm not in id_:
  481. if is_protected(nm) or not is_finetune_weight(nm):
  482. st["protected"]+=1; lbl="PROTECT"
  483. rsn = "structural/norm/embd" if is_protected(nm) else "not fine-tunable"
  484. else:
  485. st["skipped"]+=1; lbl="KEEP"; rsn="unchanged (not in delta)"
  486. print(f"{idx+1:>4} {nm:<52} {qn:<10} {fmt_bytes(sz):>9} {lbl:<10} {rsn}")
  487. continue
  488.  
  489. td = id_[nm]
  490. if ta["qtype"] != td["qtype"]:
  491. st["skipped"]+=1
  492. print(f"{idx+1:>4} {nm:<52} {qn:<10} {fmt_bytes(sz):>9} {'SKIP':<10} "
  493. f"qtype mismatch A={ta['qtype_name']} Δ={td['qtype_name']}"); continue
  494. if ta["size"] != td["size"]:
  495. st["skipped"]+=1
  496. print(f"{idx+1:>4} {nm:<52} {qn:<10} {fmt_bytes(sz):>9} {'SKIP':<10} "
  497. f"size mismatch A={ta['size']} Δ={td['size']}"); continue
  498.  
  499. qt = ta["qtype"]; ds = ta["size"]
  500. ba = block_align(qt); cb = align_down(CHUNK_BYTES, ba)
  501. if cb<=0: cb=ba
  502.  
  503. chg=0; fail=0; off=0
  504. while off < ds:
  505. act = align_down(min(cb, ds-off), ba)
  506. if act<=0: break
  507. ra = read_chunk(fo, ta, off, act)
  508. rd = read_chunk(fd, td, off, act)
  509. if len(ra)!=act or len(rd)!=act: break
  510. try:
  511. fa = to_f32(ra, qt); fdd = to_f32(rd, qt)
  512. except Exception:
  513. fail+=1; del ra,rd; off+=act; continue
  514. n = min(len(fa), len(fdd))
  515. m = fa[:n] + fdd[:n] * np.float32(weight)
  516. del fa, fdd
  517. nr = from_f32(m, qt, len(ra)); del m
  518. if nr is not None:
  519. oa = np.frombuffer(ra, np.uint8)
  520. na_ = np.frombuffer(nr, np.uint8)
  521. chg += int(np.sum(oa != na_))
  522. write_chunk(fo, ta, off, nr)
  523. del oa, na_, nr
  524. else: fail+=1
  525. del ra, rd; off += act
  526.  
  527. st["merged"]+=1; st["total_changed"]+=chg; st["failed"]+=fail
  528. if ".attn_" in nm and "_norm" not in nm: st["attn"]+=1
  529. elif ".ssm_" in nm and "_norm" not in nm: st["ssm"]+=1
  530. elif ".ffn_" in nm and "_norm" not in nm: st["ffn"]+=1
  531.  
  532. if chg>0:
  533. pct=chg/sz*100; fs=f" ({fail} kept)" if fail else ""
  534. print(f"{idx+1:>4} {nm:<52} {qn:<10} {fmt_bytes(sz):>9} {'MERGED':<10} "
  535. f"{chg:,}B ({pct:.2f}%){fs}")
  536. else:
  537. st["identical"]+=1
  538. print(f"{idx+1:>4} {nm:<52} {qn:<10} {fmt_bytes(sz):>9} {'MERGED':<10} "
  539. f"(identical)")
  540. if (idx+1)%50==0: gc.collect()
  541.  
  542. flush_and_close(fo, "output"); fd.close()
  543. el = time.time()-t0
  544. print(f"\n{'='*75}\nAPPLY SUMMARY\n{'='*75}")
  545. print(f"Protected: {st['protected']}")
  546. print(f"Merged: {st['merged']} (attn={st['attn']} ssm={st['ssm']} ffn={st['ffn']})")
  547. print(f" Identical: {st['identical']}")
  548. print(f" Changed: {st['total_changed']:,} bytes")
  549. if st['failed']: print(f" Failed chk: {st['failed']}")
  550. print(f"Kept/Skipped: {st['skipped']}")
  551. print(f"Time: {el:.1f}s")
  552. print(f"Output: {os.path.basename(output_path)} ({file_size_gb(output_path):.2f} GB)")
  553. return st
  554.  
  555. # ============================================================================
  556. # VERIFY
  557. # ============================================================================
  558.  
  559. def verify(path):
  560. print("\n[VERIFY] Checking output...")
  561. try: info, n = parse_gguf(path); print(f"[VERIFY] Parsed {n} tensors.")
  562. except Exception as e: print(f"[VERIFY] ERROR: {e}"); return False
  563. f = open(path,"rb"); ok=w=0
  564. for nm in sorted(info.keys())[:30]:
  565. ti = info[nm]; cs = min(4096, ti["size"])
  566. raw = read_chunk(f, ti, 0, cs)
  567. a = np.frombuffer(raw, np.uint8)
  568. if a.sum()==0 and cs>64: w+=1; print(f" WARNING: '{nm}' all zeros!")
  569. else: ok+=1
  570. del raw, a
  571. f.close()
  572. if w: print(f"[VERIFY] {w} zero tensors.")
  573. print(f"[VERIFY] {ok} tensors OK."); return w==0
  574.  
  575. # ============================================================================
  576. # MAIN
  577. # ============================================================================
  578.  
  579. def main():
  580. print("="*75)
  581. print("GGUF ADD-DIFFERENCE MERGE — Disk-Optimized")
  582. print("output = A + (B - C) * weight")
  583. print()
  584. print("Pipeline:")
  585. print(" 1) Download B and C")
  586. print(" 2) Compute delta = B - C → GGUF (only changed tensors)")
  587. print(" 3) Delete B and C")
  588. print(" 4) Download A")
  589. print(" 5) Apply output = A + delta * weight")
  590. print(" 6) Delete A, delta")
  591. print()
  592. print(f"WORK_DIR: {WORK_DIR}")
  593. print(f"OUTPUT: {OUTPUT_PATH}")
  594. print(f"DELTA: {DELTA_PATH}")
  595. print(f"WEIGHT: {MERGE_WEIGHT}")
  596. print("="*75)
  597.  
  598. # 1) Download B and C
  599. print("\n"+"="*75+"\nSTEP 1: Download B and C\n"+"="*75)
  600. pb = download(MODEL_B, "B (fine-tuned)")
  601. pc = download(MODEL_C, "C (reference)")
  602. print_workspace()
  603.  
  604. # 2) Compute delta
  605. print("\n"+"="*75+"\nSTEP 2: Compute delta = B - C → GGUF\n"+"="*75)
  606. compute_delta(pb, pc, DELTA_PATH)
  607. gc.collect(); print_workspace()
  608.  
  609. # 3) Delete B and C
  610. print("\n"+"="*75+"\nSTEP 3: Delete B and C\n"+"="*75)
  611. for p in set([os.path.abspath(pb), os.path.abspath(pc)]):
  612. safe_remove(p, protect=[OUTPUT_PATH, DELTA_PATH])
  613. gc.collect(); print_workspace()
  614.  
  615. # 4) Download A
  616. print("\n"+"="*75+"\nSTEP 4: Download A\n"+"="*75)
  617. pa = download(MODEL_A, "A (base)")
  618. print_workspace()
  619.  
  620. # 5) Apply delta
  621. print("\n"+"="*75+"\nSTEP 5: Apply output = A + delta * weight\n"+"="*75)
  622. apply_delta(pa, DELTA_PATH, MERGE_WEIGHT, OUTPUT_PATH)
  623. gc.collect()
  624.  
  625. # 6) Cleanup
  626. print("\n"+"="*75+"\nSTEP 6: Cleanup\n"+"="*75)
  627. safe_remove(pa, protect=OUTPUT_PATH)
  628. safe_remove(DELTA_PATH, protect=OUTPUT_PATH)
  629. gc.collect(); print_workspace()
  630.  
  631. # Verify
  632. print("\n"+"="*75+"\nVERIFY\n"+"="*75)
  633. verify(OUTPUT_PATH)
  634.  
  635. # Done
  636. print("\n"+"="*75+"\nDONE\n"+"="*75)
  637. print_workspace()
  638. print(f"\n Final: {OUTPUT_PATH}")
  639. print(f" Size: {file_size_gb(OUTPUT_PATH):.2f} GB")
  640. print(f" Disk free: {disk_free_gb():.1f} GB")
  641.  
  642. main()
Advertisement
Add Comment
Please, Sign In to add comment