Larrivhan's picture
Add files using upload-large-folder tool
049fcd2 verified
Raw History Blame Contribute Delete
3.89 kB
"""Rebuild a converged-sidecar variant on any host from patch.npz + the production sidecar.
The production file is read only. The output is written into a NEW directory and is always
named chain0_level32.npz, because the frozen runner refuses any other basename
(training/fixed_view.py: SIDECAR_BASENAME = f"chain{VIEW_CHAIN}_level{VIEW_DIPLOIDS}.npz").
Variants (see RECEIPT.json for why meanfill exists):
dropin production values on every un-rebuilt row, converged refit on the rebuilt ones
meanfill converged refit on the rebuilt rows; every other row = the rebuilt-fit mean of
the converged vectors
meanfill_baseline the matched control: stored values on the rebuilt rows; every other row = the
rebuilt-fit mean of the stored vectors
Usage:
python apply_patch.py --stored /cloud/cloud-ssd1/dlcoal/sidecar109k/chain0_level32.npz \
--patch patch.npz \
--out /cloud/cloud-ssd1/dlcoal/sidecar109k_converged_20260916 \
--variant dropin --variant meanfill --variant meanfill_baseline
"""
import argparse, hashlib, json, os
import numpy as np
FEAT = 256
BASENAME = "chain0_level32.npz"
def sha256_file(path, chunk=1 << 22):
h = hashlib.sha256()
with open(path, "rb") as f:
for block in iter(lambda: f.read(chunk), b""):
h.update(block)
return h.hexdigest()
ap = argparse.ArgumentParser()
ap.add_argument("--stored", required=True)
ap.add_argument("--patch", required=True)
ap.add_argument("--out", required=True)
ap.add_argument("--variant", action="append", default=None,
choices=["dropin", "meanfill", "meanfill_baseline"])
A = ap.parse_args()
variants = A.variant or ["dropin"]
store = np.load(A.stored, allow_pickle=False)
stored = store["sidecar"].astype(np.float32)
present = store["sidecar_present"]
patch = np.load(A.patch, allow_pickle=False)
reg = patch["registry_index"].astype(np.int64)
feats = patch["features"].astype(np.float32)
pilot_fit = patch["pilot_fit"].astype(np.int64)
assert stored.ndim == 2 and stored.shape[1] == FEAT, stored.shape
assert feats.shape == (len(reg), FEAT)
assert reg.min() >= 0 and reg.max() < len(stored), "the patch does not fit this cohort"
assert len(np.unique(reg)) == len(reg)
# the patch leaves the plain half of every rebuilt row exactly as production had it
assert np.array_equal(feats[:, 0:128], stored[reg][:, 0:128]), \
"patch plain block differs from the stored file: wrong production sidecar"
built_fit = np.asarray(sorted(set(pilot_fit.tolist()) & set(reg.tolist())), dtype=np.int64)
os.makedirs(A.out, exist_ok=True)
report = {"stored": A.stored, "stored_sha256": sha256_file(A.stored), "patch": A.patch,
"patch_sha256": sha256_file(A.patch), "rows": int(len(stored)),
"rows_patched": int(len(reg)), "built_fit_rows": int(len(built_fit)), "written": {}}
for name in variants:
if name == "dropin":
values = stored.copy(); values[reg] = feats
elif name == "meanfill":
fill = feats[np.isin(reg, built_fit)].mean(axis=0)
values = np.repeat(fill[None, :], len(stored), axis=0).astype(np.float32); values[reg] = feats
else:
fill = stored[built_fit].mean(axis=0)
values = np.repeat(fill[None, :], len(stored), axis=0).astype(np.float32)
values[reg] = stored[reg]
assert np.isfinite(values).all()
d = os.path.join(A.out, name); os.makedirs(d, exist_ok=True)
p = os.path.join(d, BASENAME)
np.savez(p, sidecar=values, sidecar_present=np.asarray(present).astype(np.uint8))
report["written"][name] = {"path": p, "sha256": sha256_file(p), "bytes": os.path.getsize(p)}
print("wrote", p)
json.dump(report, open(os.path.join(A.out, "APPLY_PATCH.json"), "w"), indent=1, sort_keys=True)
print(json.dumps(report, indent=1, sort_keys=True))