"""The held-out cells are the experiment. If one leaks into training, the result is decorative. That leak is silent: every arm still trains, every number still prints, and `unseen` quietly becomes `seen`. So it gets the test, and the test was checked against a broken mask. """ import torch import unseen def _held(kind="random", frac=0.25, seed=0): return unseen.holdout_cells(kind, frac, seed) def test_training_masks_out_every_held_cell(): held = _held() gen = torch.Generator().manual_seed(0) for _a, r, mask in unseen.train_batch(held, gen, size=64): assert not held[unseen.cell_of(r)][mask.bool()].any() # nothing held survives assert (mask.bool() | held[unseen.cell_of(r)]).all() # nothing seen was dropped def test_a_masked_pair_cannot_move_the_output(): # The mask is only meaningful if it actually severs the message. Perturb a masked # neighbour's attributes and the receiver must not notice. held = _held() model = unseen.make_model("rp", 0) gen = torch.Generator().manual_seed(3) for a, r, mask in unseen.train_batch(held, gen, size=40): if mask.min() == 1: continue i, j = (mask == 0).nonzero()[0].tolist() a2 = a.clone(); a2[j] += 7.0 y0 = unseen.run_model(model, a, r, mask) y1 = unseen.run_model(model, a2, r, mask) assert torch.allclose(y0[i], y1[i], atol=1e-6) return raise AssertionError("no masked pair was generated") def test_eval_pairs_are_wholly_seen_or_wholly_held(): held = _held() gen = torch.Generator().manual_seed(1) off = ~torch.eye(2, dtype=torch.bool) for _a, r, _m in unseen.pair_batch(held, True, gen, size=32): assert held[unseen.cell_of(r)][off].all() for _a, r, _m in unseen.pair_batch(held, False, gen, size=32): assert not held[unseen.cell_of(r)][off].any() def test_centre_cell_is_never_held(): # r = 0 is every entity's message to itself, so it appears in every configuration. Holding # it out would mask a pathway that has nothing to do with the relation under test. centre = unseen.R * unseen.G + unseen.R for kind in ("random", "band"): for seed in range(4): assert not _held(kind, 0.25, seed)[centre] def test_both_holdouts_withhold_a_real_fraction(): for kind in ("random", "band"): n = int(_held(kind, 0.25, 0).sum()) assert 0.15 * unseen.CELLS < n < 0.35 * unseen.CELLS def test_table_kernel_matches_cppn_call_signature(): # The table arm is swapped into RelationalPerceptron in place, so a signature drift would # show up as a confusing shape error inside the RP rather than here. k = unseen.TableKernel() r = torch.randint(-3, 4, (4, 4, 2)).float() assert k(r, torch.ones(4, 4, 1), torch.zeros(2)).shape == (4, 4, unseen.D) def test_diffusion_fills_only_from_trained_cells(): # The control is only a control if the filled values come from cells that actually saw # gradient. Trained cells all hold 1, held cells hold 0 (their "init"): after filling, # every held cell must be 1 -- a 0 anywhere means an untrained entry was used as a source. held = _held() w = torch.ones(unseen.CELLS, unseen.D) w[held] = 0.0 unseen.fill_by_diffusion(w, held) assert torch.allclose(w, torch.ones_like(w), atol=1e-6) def test_diffusion_reaches_every_held_cell_in_both_holdouts(): for kind in ("random", "band"): held = _held(kind) w = torch.zeros(unseen.CELLS, unseen.D) w[~held] = 1.0 unseen.fill_by_diffusion(w, held) assert (w[held] > 0).all() # no held cell was left stranded at its init