Files
digital-drosophila/models/male-cns-lif/normal-conditioning-v1/test_normal_plan.py
T
2026-09-30 21:00:33 +08:00

161 lines
9.0 KiB
Python

"""CPU-only fixed inventory, authority, and dependency regression tests."""
from collections import Counter, defaultdict
import importlib.util
from pathlib import Path
import random
import sys
import unittest
from unittest.mock import patch
_SPEC = importlib.util.spec_from_file_location("normal_plan_test_subject", Path(__file__).with_name("normal_plan.py"))
P = importlib.util.module_from_spec(_SPEC)
_SPEC.loader.exec_module(P)
class NormalPlanTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Deterministic recipe construction, NOT neural random sampling.
cls.trial = P._FROZEN["trial_plan"].build_trial_plan(P.MASTER)
cls.evaluation = P.expand_pinned_evaluation(cls.trial)
with patch.object(random, "random", side_effect=AssertionError("RNG draw")), \
patch.object(random, "Random", side_effect=AssertionError("RNG instance")):
cls.plan = P.build_normal_plan(cls.trial, cls.evaluation)
def test_complete_inventory_and_order(self):
plan = self.plan
self.assertEqual(Counter(t["kind"] for t in plan["tasks"]), {"train": 30000, "probe": 3920})
self.assertEqual(sum(t["steps"] for t in plan["tasks"]), 40704000)
self.assertEqual(len(plan["blocks"]), 40)
self.assertEqual(Counter((b["mode"], b["dan_type"], b["reinforced_cue"]) for b in plan["blocks"]),
{(m, d, c): 5 for m in ("physiological", "matched_energy")
for d in ("PAM15", "PPL103") for c in ("A", "B")})
groups = defaultdict(list)
for row in plan["tasks"]:
groups[(row["block_id"], row["arm"])].append(row)
self.assertEqual(list(groups), [(b["block_id"], a) for b in self.trial["plan"]["blocks"]
for a in P._FROZEN["trial_plan"].ARMS])
for (_, arm), rows in groups.items():
self.assertEqual([r["kind"] for r in rows[:3]], ["probe", "probe", "train"])
self.assertEqual([r["recipe"]["cue"] for r in rows[:2]], ["A", "B"])
self.assertEqual(len(rows), 164 if arm == "unpaired" else 114)
self.assertEqual([r["checkpoint"] for r in rows if r["kind"] == "probe"],
[n for n in (0, 1, 2, 5, 10, 20, 50) for _ in range(2)])
self.assertEqual(len(groups), 280)
def test_every_dependency_is_earlier_same_arm_delta_and_probes_do_not_feed_back(self):
previous, completed = {}, Counter()
for row in self.plan["tasks"]:
key = row["block_id"], row["arm"]
expected = previous.get(key, {"kind": "zero", "source": None})
self.assertEqual(row["delta_dependency"], expected)
self.assertTrue(row["artifact_validation_required"])
if row["kind"] == "train":
self.assertEqual(row["epoch_index"], completed[key])
completed[key] += 1
self.assertEqual(row["carry_between_epochs"], ["plastic_delta_only"])
self.assertEqual(row["neural_state_policy"], "frozen_reset_including_KC_trace_and_both_delay_queues")
self.assertTrue(row["original_E_required"] and row["cpu_validation_required"])
previous[key] = {"kind": "epoch_delta_after", "source": row["delta_output_id"]}
else:
self.assertEqual(row["completed_epochs"], completed[key])
self.assertEqual(row["snapshot_binding"]["completed_epochs"], completed[key])
self.assertFalse(row["feedback_to_training_or_idle"])
self.assertFalse(row["original_E_required"] or row["cpu_validation_required"])
self.assertFalse(row["recipe"]["US_requested"] or row["recipe"]["weight_updates_enabled"])
self.assertEqual(row["recipe"]["D_command"], 0.0)
self.assertIsNone(row["delta_output_id"])
def test_frozen_control_recipes_not_nominal_boundaries(self):
actual = {r["task_id"]: r for r in self.plan["tasks"]}
for block in self.trial["plan"]["blocks"]:
normal = next(s for s in block["suites"] if s["assay"] == "normal")
for arm in normal["arms"]:
for recipe in arm["epochs"]:
row = actual[recipe["epoch_id"]]
self.assertEqual(row["recipe"], recipe)
self.assertEqual(row["source_binding"]["record_sha256"], P._sha(recipe))
self.assertEqual(row["source_binding"]["payload_sha256"], self.trial["payload_sha256"])
self.assertEqual(sum(e["us"]["requested"] for e in arm["epochs"]),
0 if arm["arm"] == "cs_only" else 50)
if arm["arm"] == "unpaired":
self.assertTrue(all(not (e["cs"] and e["us"]["requested"]) for e in arm["epochs"]))
for original in self.evaluation["probes"]:
if original["assay"] == "normal":
self.assertEqual(actual[original["probe_id"]]["recipe"], original)
self.assertTrue(any(r["kind"] == "probe" and r["arm"] == "unpaired" and
r["checkpoint"] == 50 and r["completed_epochs"] == 150
for r in actual.values()))
def test_evaluation_inputs_reused_not_redrawn_or_counted_as_replicates(self):
inputs = self.plan["evaluation_inputs"]
self.assertEqual(len(inputs), 80)
self.assertEqual(len({r["stream_id"] for r in inputs}), 80)
seeds = {r["seed"] for r in inputs}
training = {r["recipe"]["cs"]["seed"] for r in self.plan["tasks"]
if r["kind"] == "train" and r["recipe"]["cs"] is not None}
self.assertFalse(seeds & training)
self.assertTrue(all(type(s) is int and 0 <= s < 2**63 for s in seeds | training))
reuse = Counter(r["evaluation_input_id"] for r in self.plan["tasks"] if r["kind"] == "probe")
self.assertEqual(set(reuse.values()), {49})
self.assertEqual(set(reuse), {r["input_id"] for r in inputs})
self.assertTrue(all(not r["recipe"]["adds_training_replicate"] for r in self.plan["tasks"] if r["kind"] == "probe"))
self.assertEqual(self.plan["new_seed_streams"], 0)
self.assertFalse(self.plan["learning_pass_claimed"] or self.plan["execution_authorized"])
def test_no_mutable_alias_to_authorities(self):
row = next(r for r in self.plan["tasks"] if r["kind"] == "train")
before = row["recipe"]["D_command"]
try:
row["recipe"]["D_command"] = 999
original = self.trial["plan"]["blocks"][0]["suites"][0]["arms"][0]["epochs"][0]
self.assertEqual(original["D_command"], before)
finally:
row["recipe"]["D_command"] = before
self.assertIsNot(self.plan["provenance"]["frozen_source_sha256"], P.SOURCE_PINS)
def test_private_loader_ignores_ordinary_module_cache_and_fails_closed_on_source_pin(self):
sentinels = {name: object() for name in ("seed_registry", "trial_plan", "evaluation_plan", "evaluation_policy", "R", "E")}
with patch.dict(sys.modules, sentinels):
loaded = P._load_frozen()
self.assertIs(loaded["trial_plan"].seed_registry, loaded["seed_registry"])
self.assertIs(loaded["evaluation_policy"].evaluation_plan, loaded["evaluation_plan"])
self.assertTrue(all(sys.modules[name] is value for name, value in sentinels.items()))
with patch.object(Path, "read_bytes", return_value=b"unregistered mutation"):
with self.assertRaisesRegex(ValueError, "Frozen source pin mismatch"):
P._load_frozen()
def test_original_trial_and_policy_mutation_rejected(self):
master = self.trial["plan"]["master"]
try:
self.trial["plan"]["master"] = master + 1
with self.assertRaisesRegex(ValueError, "master"):
P.build_normal_plan(self.trial, self.evaluation)
finally:
self.trial["plan"]["master"] = master
binding = self.evaluation["config"]["snapshot_bindings"][1]
count = binding["completed_epochs"]
try:
binding["completed_epochs"] += 1
with self.assertRaisesRegex(ValueError, "exactly expand"):
P.build_normal_plan(self.trial, self.evaluation)
finally:
binding["completed_epochs"] = count
def test_validates_exact_reconstruction_and_rejects_rehashed_task_mutation(self):
self.assertTrue(P.validate_normal_plan(self.plan, self.trial, self.evaluation))
row, original_hash = self.plan["tasks"][0], self.plan["payload_sha256"]
try:
row["feedback_to_training_or_idle"] = True
self.plan["payload_sha256"] = P._sha({k: v for k, v in self.plan.items() if k != "payload_sha256"})
with self.assertRaisesRegex(ValueError, "exact reconstruction"):
P.validate_normal_plan(self.plan, self.trial, self.evaluation)
finally:
row["feedback_to_training_or_idle"] = False
self.plan["payload_sha256"] = original_hash
if __name__ == "__main__":
unittest.main()