#!/usr/bin/env python3 """Apply (or undo) the pqhaz heretic patch IN PLACE in the official DeepSeek-V4.1-Flash-1017 shards, for engines that read attention weights straight from the official checkpoint (Morrowmake). Only the 38 `layers.N.attn.wo_b.weight` tensors change (same dtype F8_E4M3, same shape, same offsets); FP8 scales are untouched, as the patch was built. heretic_inplace.py apply OFFICIAL_DIR PATCH.safetensors BACKUP.safetensors heretic_inplace.py restore OFFICIAL_DIR PATCH.safetensors BACKUP.safetensors heretic_inplace.py status OFFICIAL_DIR PATCH.safetensors BACKUP.safetensors apply: writes an exact byte backup of every original tensor first (a safetensors file with the same names), then overwrites, fsyncs and reads back; refuses a shard with more than one hard link. restore: writes the backup back. Idempotent: a tensor already in the wanted state is skipped. Stdlib only.""" import hashlib, json, os, struct, sys def header(path): with open(path, "rb") as f: n = struct.unpack(" (dtype, shape, bytes) hdr, off = {}, 0 for k in sorted(tensors): dt, shp, data = tensors[k] hdr[k] = {"dtype": dt, "shape": shp, "data_offsets": [off, off + len(data)]} off += len(data) hb = json.dumps(hdr, separators=(",", ":")).encode() hb += b" " * ((8 - len(hb) % 8) % 8) tmp = path + ".tmp" with open(tmp, "wb") as f: f.write(struct.pack("