File size: 3,887 Bytes
1cb35cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""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))