Download code/work/edit_hw.py from bryan7264/lorapop: direct link, hf CLI and curl.
- Browser
- Download file 6.68 kB
-
https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/work/edit_hw.py
- Command line
-
hf download hf://datasets/bryan7264/lorapop/code/work/edit_hw.py
-
curl -L -o edit_hw.py https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/work/edit_hw.py
6.68 kB
| # MIT License, Bryan Cheng, 2026. One-off source edit (D-035): hardware-tagged shared c = 0 references. | |
| import re | |
| p = "src/run/intervene.py" | |
| s = open(p).read() | |
| s = s.replace('''--shared_zero (D-031): c = 0 is not re-measured inside every direction; reference units __ref__NNN measure the base, every | |
| background and every knockout carrier once (c = 0 has zero weight in the OLS dose slopes; calibrate.py reads the | |
| references for knockout differences and base curves). Without it, c = 0 is measured in every direction.''', | |
| '''--shared_zero (D-031): c = 0 is not re-measured inside every direction; reference units __ref__<hw>__NNN measure the base, | |
| every background and every knockout carrier once per hardware configuration hw = GPU model + batch sizes (D-035), so | |
| that c = 0 and the dosed combos it is compared with come from the same hardware (c = 0 has zero weight in the OLS dose | |
| slopes; calibrate.py reads the references for knockout differences and base curves). Without it, c = 0 is measured in | |
| every direction.''') | |
| s = s.replace('''def make_units(plan, shared_zero, ref_size=48): | |
| """Work units in execution order: c = 0 reference units (shared_zero) then directions sorted by module."""''', | |
| '''def hw_tag(gpu, bs, gbs): | |
| """Hardware configuration tag (D-035): GPU model (alphanumerics) + scoring and generation batch sizes.""" | |
| return re.sub(r"[^A-Za-z0-9]+", "_", gpu.replace("NVIDIA ", "")).strip("_") + f"_b{bs}g{gbs}" | |
| def make_units(plan, shared_zero, ref_size=48, hw=""): | |
| """Work units in execution order: c = 0 reference units for hardware tag hw (shared_zero), then directions sorted by | |
| module."""''') | |
| s = s.replace('''units = [{"id": f"__ref__{i // ref_size:03d}", "kind": "ref"''', | |
| '''units = [{"id": f"__ref__{hw + '__' if hw else ''}{i // ref_size:03d}", "kind": "ref"''') | |
| s = s.replace("import os\nimport signal", "import os\nimport re\nimport signal") | |
| s = s.replace(''' dirs = make_units(plan, a.shared_zero)[a.shard::a.nshards] | |
| fcache = {} | |
| gpu = torch.cuda.get_device_name(0) if dev == "cuda" else "cpu" | |
| print("GPU", gpu, "directions", len(dirs), flush=True)''', | |
| ''' gpu = torch.cuda.get_device_name(0) if dev == "cuda" else "cpu" | |
| hw = hw_tag(gpu, a.bs, a.gbs) | |
| dirs = make_units(plan, a.shared_zero, hw=hw)[a.shard::a.nshards] | |
| fcache = {} | |
| print("GPU", gpu, "hw", hw, "units", len(dirs), flush=True)''') | |
| s = s.replace(''' "gpu": gpu, "sizes": a.sizes, "gen_half": a.gen_half}''', | |
| ''' "gpu": gpu, "hw": hw, "sizes": a.sizes, "gen_half": a.gen_half, "peak_mem_gb": peak_gb()}''') | |
| s = s.replace('''"seconds": time.time() - t0, "gpu": gpu, | |
| "sizes": a.sizes,''', '''"seconds": time.time() - t0, "gpu": gpu, | |
| "hw": hw, "peak_mem_gb": peak_gb(), "sizes": a.sizes,''') | |
| # exception-safe unlock: wrap the per-unit body | |
| s = s.replace(''' signal.signal(signal.SIGTERM, release) | |
| for d in dirs:''', ''' signal.signal(signal.SIGTERM, release) | |
| def peak_gb(): | |
| return round(torch.cuda.max_memory_allocated() / 2 ** 30, 2) if dev == "cuda" else None | |
| for d in dirs:''') | |
| open(p, "w").write(s) | |
| assert s.count("peak_gb()") == 3 and "hw_tag(gpu" in s and "import re" in s, "edit failed" | |
| p = "src/analysis/calibrate.py" | |
| s = open(p).read() | |
| s = s.replace('''carrier that a direction did not measure itself, only when the reference used the same generation half.''', | |
| '''carrier that a direction did not measure itself, only when the reference used the same generation half and the same hardware | |
| configuration (record "hw", D-035); directions without a matching reference contribute no c = 0 points (counted).''') | |
| s = s.replace(''' refs = {} # gen_half -> background -> combo (D-031) | |
| for f in sorted(glob.glob(os.path.join(a.interv, "__ref__*.json"))): | |
| r = json.load(open(f)) | |
| for cmb in r["combos"]: | |
| refs.setdefault(r.get("gen_half", -1), {})[cmb["background"]] = cmb | |
| n_ref_used = 0''', ''' refs = {} # (gen_half, hw) -> background -> combo (D-031, D-035) | |
| for f in sorted(glob.glob(os.path.join(a.interv, "__ref__*.json"))): | |
| r = json.load(open(f)) | |
| for cmb in r["combos"]: | |
| refs.setdefault((r.get("gen_half", -1), r.get("hw", "")), {})[cmb["background"]] = cmb | |
| n_ref_used, n_ref_missing = 0, 0''') | |
| s = s.replace(''' ref = refs.get(rec.get("gen_half", -1), {})''', | |
| ''' ref = refs.get((rec.get("gen_half", -1), rec.get("hw", "")), {})''') | |
| s = s.replace(''' for b in list(by): | |
| if b in ref and not any(c == 0 for c, _ in by[b]): | |
| by[b].append((0.0, z(ref[b]))) | |
| n_ref_used += 1''', ''' for b in list(by): | |
| if not any(c == 0 for c, _ in by[b]): | |
| if b in ref: | |
| by[b].append((0.0, z(ref[b]))) | |
| n_ref_used += 1 | |
| else: | |
| n_ref_missing += 1''') | |
| s = s.replace('''"n_zero_points_from_refs": n_ref_used,''', '''"n_zero_points_from_refs": n_ref_used, | |
| "n_zero_points_missing": n_ref_missing,''') | |
| open(p, "w").write(s) | |
| assert "n_ref_missing += 1" in s and '"n_zero_points_missing"' in s and 'r.get("hw", "")' in s, "calibrate edit failed" | |
| print("ok") | |
| p = "src/run/intervene.py" | |
| s = open(p).read() | |
| s = s.replace('''def claim(lk, stale):''', '''HELD = [] # lock files held by this process (released on exit, exception or SIGTERM) | |
| def claim(lk, stale):''') | |
| s = s.replace(''' held = [] | |
| def release(*_):''', ''' held = HELD | |
| def release(*_):''') | |
| s = s.replace('''if __name__ == "__main__": | |
| main()''', '''if __name__ == "__main__": | |
| try: | |
| main() | |
| finally: | |
| unlock(HELD)''') | |
| open(p, "w").write(s) | |
| assert "held = HELD" in s and "unlock(HELD)" in s | |
| print("ok2") | |
| p = "src/run/cross.py" | |
| s = open(p).read() | |
| s = s.replace("from src.run.intervene import claim, unlock # noqa: E402", "from src.run.intervene import HELD, claim, unlock # noqa: E402") | |
| s = s.replace(" held = []\n", " held = HELD\n gpu = torch.cuda.get_device_name(0) if dev == \"cuda\" else \"cpu\"\n", 1) | |
| s = s.replace('''"ppl_ratio": float(np.exp(nll - base_nll)), "seconds": sec}''', | |
| '''"ppl_ratio": float(np.exp(nll - base_nll)), "seconds": sec, "gpu": gpu, | |
| "bs": [a.bs, a.gbs]}''') | |
| s = s.replace(" cmd_make(a) if a.cmd == \"make\" else cmd_run(a)", ''' try: | |
| cmd_make(a) if a.cmd == "make" else cmd_run(a) | |
| finally: | |
| unlock(HELD)''') | |
| open(p, "w").write(s) | |
| assert "held = HELD" in s and '"gpu": gpu' in s and "unlock(HELD)" in s | |
| print("ok3") | |