#!/usr/bin/env python3 """Write config.json + copy the auxiliary files for the re-packed checkpoint. The weight re-packer only emits safetensors and an index. This adds everything else a consumer needs, and -- importantly -- writes a quantization_config that DESCRIBES what we actually did, because no released engine reads `deepseek_v41` yet and we are therefore defining the convention rather than matching one. Changes vs upstream config.json: expert_dtype "fp4" -> "nvfp4" experts are now block-16 E2M1 + E4M3 scale + FP32 per-tensor global scale expert_block_size (new) 16 expert_scale_fmt (new) "e4m3" engram_dtype (new) "fp4" Engram tables are now E2M1 engram_block_size (new) 32 keeping the source's own block/scale layout engram_scale_fmt (new) "ue8m0" Everything else (dense fp8 block 32x32 / ue8m0) is untouched and left exactly as shipped. """ import glob, json, os, shutil, struct, sys src, out = sys.argv[1], sys.argv[2] def rebuild_index(out_dir): """Rebuild model.safetensors.index.json by reading every output shard's header. The re-packer is run in more than one pass (experts, then the two 94.5 GiB Engram shards), and each pass writes an index covering only its own shards -- so the last pass would otherwise clobber the rest. Reading the headers back is also a real check that every shard on disk is a valid safetensors file. """ DT = {"I8": 1, "U8": 1, "F8_E4M3": 1, "F8_E8M0": 1, "BF16": 2, "F16": 2, "F32": 4, "F64": 8, "I32": 4, "I64": 8, "BOOL": 1} wmap, total = {}, 0 shards = sorted(glob.glob(os.path.join(out_dir, "*.safetensors"))) for path in shards: with open(path, "rb") as fh: n = struct.unpack(" output tensors {len(oi):,}") missing = [k for k in si if k not in oi and not k.endswith(".scale")] extra = [k for k in oi if k not in si and not k.endswith((".weight_scale", ".weight_scale_2"))] print(f"missing (non-scale): {len(missing)} {missing[:3]}") print(f"unexpected new: {len(extra)} {extra[:3]}") assert not missing, "output is missing tensors the source had"