Download code/tests/test_intervene_units.py from bryan7264/lorapop: direct link, hf CLI and curl.
- Browser
- Download file 5.79 kB
-
https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/tests/test_intervene_units.py
- Command line
-
hf download hf://datasets/bryan7264/lorapop/code/tests/test_intervene_units.py
-
curl -L -o test_intervene_units.py https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/tests/test_intervene_units.py
5.79 kB
| # MIT License, Bryan Cheng, 2026. | |
| """Work units of intervene.py: shared c = 0 references, per-direction backgrounds, generation halves (D-031, plan 4.5).""" | |
| import os | |
| import sys | |
| import numpy as np | |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) | |
| from src.run.intervene import combos_of, make_units, restrict_gen_half # noqa: E402 | |
| PLAN = {"backgrounds": ["a", "b", "c"], "doses_base": [-3.0, -1.0, 0.0, 1.0, 3.0], "doses_bg": [-1.0, 0.0, 1.0], | |
| "directions": [{"id": "loc_1", "kind": "locus", "module": "1.q_proj", "source": "naive", "knockout_carriers": [["x", 2.0], ["y", -1.5]]}, | |
| {"id": "rnd_1", "kind": "random", "module": "1.q_proj", "backgrounds": ["a"]}, | |
| {"id": "loc_0", "kind": "locus", "module": "0.q_proj", "source": "mm_fd", "knockout_carriers": [["x", 1.2]]}]} | |
| def test_units_without_shared_zero_measure_c0_everywhere(): | |
| U = make_units(PLAN, False) | |
| assert [u["id"] for u in U] == ["loc_1", "loc_0", "rnd_1"] # loci first, each kind in sha1 order of the id (D-047) | |
| by = {u["id"]: u for u in U} | |
| c = combos_of(by["loc_1"], PLAN, False) | |
| assert len(c) == 5 + 3 * 3 + 2 * 2 and ("KO::x", 0.0) in c and ("KO::x", -2.0) in c | |
| assert combos_of(by["rnd_1"], PLAN, False) == [("__base__", d) for d in PLAN["doses_base"]] + [("a", -1.0), ("a", 0.0), ("a", 1.0)] | |
| def test_shared_zero_moves_every_c0_to_reference_units(): | |
| U = make_units(PLAN, True, ref_size=3) | |
| refs = [u for u in U if u["kind"] == "ref"] | |
| zero = [c for u in refs for c in combos_of(u, PLAN, True)] | |
| assert zero == [("__base__", 0.0), ("a", 0.0), ("b", 0.0), ("c", 0.0), ("KO::x", 0.0), ("KO::y", 0.0)] | |
| assert [u["id"][:10] for u in refs] == ["__ref__000", "__ref__001"] | |
| assert all(len(u["id"]) == 19 for u in refs) # __ref__NNN_<8 hex> | |
| for u in U: | |
| if u["kind"] != "ref": | |
| assert all(c != 0 for _, c in combos_of(u, PLAN, True)) | |
| full = sum(len(combos_of(u, PLAN, False)) for u in make_units(PLAN, False)) | |
| shared = sum(len(combos_of(u, PLAN, True)) for u in U) | |
| # removed: c = 0 on base + backgrounds per direction (4 + 4 + 2) and KO c = 0 (3); added: 6 references | |
| assert full - shared == (4 + 4 + 2 + 3) - 6 | |
| def test_restrict_gen_half_keeps_aligned_prompts_and_meta(): | |
| items = {"gen_x": {"kind": "gen", "prompts": list("abcdef"), "meta": list(range(6)), "half": np.array([0, 1, 1, 0, 0, 1])}, | |
| "mc_y": {"kind": "mc", "items": list("uvw"), "half": np.array([0, 1, 0])}} | |
| restrict_gen_half(items, 0) | |
| assert items["gen_x"]["prompts"] == ["a", "d", "e"] and items["gen_x"]["meta"] == [0, 3, 4] | |
| assert items["gen_x"]["half"].tolist() == [0, 0, 0] and items["mc_y"]["items"] == list("uvw") | |
| def test_claim_is_exclusive_and_takes_over_stale_locks(tmp_path): | |
| from src.run.intervene import claim, unlock | |
| lk = str(tmp_path / "u.json.lock") | |
| assert claim(lk, 3600) and not claim(lk, 3600) | |
| os.utime(lk, (0, 0)) # very old -> stale | |
| assert claim(lk, 3600) | |
| held = [lk] | |
| unlock(held) | |
| assert not os.path.exists(lk) and held == [] | |
| def test_spikein_itemset_of_maps_traits_to_battery_sets(): | |
| from src.run.spikein import itemset_of | |
| items = {k: {} for k in ("gen_general", "gen_ifeval", "gen_json", "mmlu", "tqa_mc1", "refusal_harm", | |
| "mwe_air_myopic-reward")} | |
| assert itemset_of("gen.lang_en", items) == "gen_general" and itemset_of("ife.strict", items) == "gen_ifeval" | |
| assert itemset_of("json.valid", items) == "gen_json" and itemset_of("mmlu.acc_stem", items) == "mmlu" | |
| assert itemset_of("mmlu.ece", items) == "mmlu" and itemset_of("tqa_mc1.p", items) == "tqa_mc1" | |
| assert itemset_of("refusal_harm", items) == "refusal_harm" | |
| assert itemset_of("mwe_air_myopic-reward.acc", items) == "mwe_air_myopic-reward" | |
| def test_reference_units_are_tagged_by_hardware_configuration(): | |
| from src.run.intervene import hw_tag | |
| assert hw_tag("NVIDIA H200", 128, 256) == "H200_b128g256" | |
| assert hw_tag("NVIDIA RTX PRO 6000 Blackwell Server Edition", 128, 256) == "RTX_PRO_6000_Blackwell_Server_Edition_b128g256" | |
| assert hw_tag("cpu", 8, 16) == "cpu_b8g16" | |
| U = make_units(PLAN, True, ref_size=3, hw="H200_b128g256") | |
| assert [u["id"][:-9] for u in U if u["kind"] == "ref"] == ["__ref__H200_b128g256__000", "__ref__H200_b128g256__001"] | |
| # a different plan (other backgrounds) gives different ids for the same position | |
| other = {**PLAN, "backgrounds": ["z", "b", "c"]} | |
| assert make_units(other, True, ref_size=3, hw="H200_b128g256")[0]["id"] != U[0]["id"] | |
| # directions are hardware-independent units (shared across hardware types) | |
| assert [u["id"] for u in U if u["kind"] != "ref"] == ["loc_1", "loc_0", "rnd_1"] | |
| def test_reference_units_cover_only_backgrounds_in_use(): | |
| plan = {**PLAN, "directions": [{"id": "r1", "kind": "locus", "module": "1.q_proj", "backgrounds": ["b"]}]} | |
| refs = [c for u in make_units(plan, True) if u["kind"] == "ref" for c in u["combos"]] | |
| assert refs == [("__base__", 0.0), ("b", 0.0)] | |
| def test_direction_order_is_tested_loci_first_and_independent_of_plan_order(): | |
| """D-047: tested loci (kind locus with a source) before controls, including high-variance control loci (kind locus, | |
| no source); the order depends only on the ids, so every job and worker agrees.""" | |
| hv = {"id": "hv_0.q_proj_5", "kind": "locus", "module": "0.q_proj", "control_of": ["loc_0"], "backgrounds": ["a"]} | |
| plan = dict(PLAN, directions=PLAN["directions"] + [hv]) | |
| rev = dict(plan, directions=plan["directions"][::-1]) | |
| a = [u["id"] for u in make_units(plan, False)] | |
| assert a == [u["id"] for u in make_units(rev, False)] | |
| assert set(a[:2]) == {"loc_0", "loc_1"} and set(a[2:]) == {"rnd_1", "hv_0.q_proj_5"} | |