SoulFireMage's picture
Study 2 blinding script for the key-holder
d4be42f verified
Raw History Blame Contribute Delete
4.93 kB
"""study2/blind2.py -- STUDY 2. Run by the KEY-HOLDER (Richard), never by Claude.
What it does:
1. Downloads the 18 raw study-2 networks (16 trained + 2 random-init) from the private repo.
2. Picks 2 trained networks at random and makes shuffled copies (entries permuted
within each tensor), giving 20 networks in total.
3. Writes each network as plain text with a random ID, in a random order.
4. Writes key2.csv (KEEP THIS PRIVATE) with a random salt line, so its hash can't be
brute-forced.
5. Uploads the 20 blinded text files plus KEY_HASH.txt to the PUBLIC repo.
Randomness comes from your operating system, so Claude cannot predict it.
Setup (once): pip install numpy huggingface_hub
hf auth login (or: huggingface-cli login)
Run: python blind2.py (writes key2.csv; keep it private)
"""
import csv, hashlib, io, os, secrets, sys
import numpy as np
from huggingface_hub import HfApi, hf_hub_download
RAW = "SoulFireMage/weight-reading-probe-raw"
PUB = "SoulFireMage/weight-reading-probe"
TRAINED = [f"{g}_s{s}" for g in ["catch", "dodge", "squash", "avoid"] for s in (0, 1, 2, 3)]
RANDOM = ["random_init_0", "random_init_1"]
ORDER = ["fc1.weight", "fc1.bias", "fc2.weight", "fc2.bias", "out.weight", "out.bias"]
SHAPES = {"fc1.weight": (32, 500), "fc1.bias": (32,), "fc2.weight": (32, 32),
"fc2.bias": (32,), "out.weight": (5, 32), "out.bias": (5,)}
CH = ["agent", "moving", "targets", "obstacles", "prev_frame"]
def fmt(x):
return " ".join(f"{v:.4g}" for v in x)
def serialise(w):
out = io.StringIO()
out.write("# 17253 parameters. Values to 4 significant figures.\n")
W1 = w["fc1.weight"]
out.write("\n## fc1.weight shape [32 x 500] (hidden1 unit j <- input i)\n")
out.write("## each unit's 500 inputs are laid out as 5 channels x 10 rows x 10 columns\n")
for j in range(32):
out.write(f"\n### fc1 unit {j}\n")
g = W1[j].reshape(5, 10, 10)
for c in range(5):
out.write(f"channel {c} ({CH[c]}):\n")
for r in range(10):
out.write(f" r{r}: {fmt(g[c, r])}\n")
out.write("\n## fc1.bias shape [32]\n" + fmt(w["fc1.bias"]) + "\n")
out.write("\n## fc2.weight shape [32 x 32] (row = hidden2 unit, column = hidden1 unit)\n")
for j in range(32):
out.write(f"u{j}: {fmt(w['fc2.weight'][j])}\n")
out.write("\n## fc2.bias shape [32]\n" + fmt(w["fc2.bias"]) + "\n")
out.write("\n## out.weight shape [5 x 32] (row = action, column = hidden2 unit)\n")
for a, name in enumerate(["noop", "up", "down", "left", "right"]):
out.write(f"a{a} ({name}): {fmt(w['out.weight'][a])}\n")
out.write("\n## out.bias shape [5]\n" + fmt(w["out.bias"]) + "\n")
return out.getvalue()
def main():
rng = np.random.default_rng(secrets.randbits(128))
nets = {}
for name in TRAINED + RANDOM:
p = hf_hub_download(RAW, f"study2/raw/{name}.npz", repo_type="dataset")
z = np.load(p)
w = {k: z[k].astype(np.float64) for k in ORDER}
for k in ORDER:
assert w[k].shape == SHAPES[k], (name, k, w[k].shape)
nets[name] = ("random_init" if name in RANDOM else "trained", name, w)
for src in rng.choice(TRAINED, 2, replace=False):
w = nets[src][2]
sh = {k: rng.permutation(w[k].ravel()).reshape(w[k].shape) for k in ORDER}
nets[f"shuffled_{src}"] = ("shuffled", src, sh)
ids = set()
while len(ids) < len(nets):
ids.add("s2-" + secrets.token_hex(3))
ids = list(ids)
keys = list(nets)
rng.shuffle(keys)
os.makedirs("blinded2", exist_ok=True)
rows = []
for nid, k in zip(ids, keys):
cond, src, w = nets[k]
with open(f"blinded2/{nid}.txt", "w", newline="\n") as f:
f.write(serialise(w))
game = src.split("_s")[0] if cond != "random_init" else ""
rows.append([nid, cond, game, src])
rows.sort()
with open("key2.csv", "w", newline="") as f:
cw = csv.writer(f)
cw.writerow(["salt", secrets.token_hex(16), "", ""])
cw.writerow(["id", "condition", "game", "source"])
cw.writerows(rows)
h = hashlib.sha256(open("key2.csv", "rb").read()).hexdigest()
with open("blinded2/KEY_HASH.txt", "w") as f:
f.write(f"SHA-256 of key2.csv (held privately by the key-holder): {h}\n")
print("key2.csv SHA-256:", h)
ans = input(f"Upload {len(ids)} blinded files + KEY_HASH.txt to {PUB}? [y/N] ")
if ans.strip().lower() == "y":
HfApi().upload_folder(folder_path="blinded2", path_in_repo="study2/blinded", repo_id=PUB,
repo_type="dataset", commit_message="Study 2: blinded networks + key hash")
print("Uploaded. KEEP key2.csv PRIVATE and do not send it to Claude until unblinding.")
else:
print("Not uploaded. Files are in ./blinded2")
if __name__ == "__main__":
main()