#!/usr/bin/env python """Splice replacement tensors into a split GGUF, writing a new shard set. Pure Python, no numpy. Every tensor of the original shards is copied byte-for-byte unless a tensor of the same name exists in the replacement GGUF, in which case the replacement's shape/type/data is used instead. Shard 1 (metadata only in gguf-split layouts) is copied verbatim under the new name; shards with tensors get their split.* keys regenerated and tensor offsets recomputed. The originals are never modified. Usage: python gguf_splice.py -> writes -0000N-of-0000M.gguf next to the original shards Example (2026-09-03): python gguf_splice.py Qwen3.8-Flash-Next-UD-Q4_K_XL-00001-of-00004.gguf build-downs-iq4nl.gguf Qwen3.8-Flash-Next-UD-Q4_K_XL-dn4 Verify afterwards with gguf_tensor_map.py on the new first shard (tensor count must match split.tensors.count). """ import os, re, shutil, struct, sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from gguf_extract import Reader, nbytes, wstr, shards_of, ALIGN def write_shard(out_path, kv, tensors, sources): """kv: list of (key, type, raw); tensors: list of (name, shape, type); sources: name -> (Reader, src_off).""" hdr = b'GGUF' + struct.pack(' {os.path.basename(out_path)}') r.f.close() print(f'done: {replaced} tensors replaced, {total / 1e9:.2f} GB of tensor data written') if replaced != len(repl_map): print(f'WARNING: {len(repl_map) - replaced} replacement tensors were not found in the original') if __name__ == '__main__': main()