diff --git a/tests/test_core.py b/tests/test_core.py index af814ba..f0f32fa 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -179,3 +179,38 @@ def test_detach_memory_cuts_graph(): _, mem = org(attrs, r, ids, (0.0, 0.0), {}) assert all(m.requires_grad for m in mem.values()) assert not any(m.requires_grad for m in detach_memory(mem).values()) + + +# --- Property card rows: what the RP is, asserted rather than assumed. ------------------- +# These two are not experiments and must never become them. They state properties that hold +# by construction, and exist so a future change cannot remove one silently. Both were checked +# against a deliberately broken RP (see results/rp_properties.md) and both failed there. + + +def test_rp_same_parameters_any_n(): + """One parameter set serves any entity count. A fixed-weight layer over N entities is + not worse here, it is undefined: its input dimension is N (or N^2) and has to be chosen.""" + rp = RelationalPerceptron(D_ATTR, K, E, C, hidden_dim=H) + before = [p.clone() for p in rp.parameters()] + for n in (2, 7, 32): + attrs, coords = torch.randn(n, D_ATTR), torch.rand(n, K) + edge = torch.zeros(n, n, E); edge[..., 0] = 1.0 + h = rp(attrs, flat_diff(coords), edge, torch.ones(n, n), torch.tensor([0.1, 0.2])) + assert h.shape == (n, H) + assert all(torch.equal(a, b) for a, b in zip(before, rp.parameters())) # no shape adapted + + +def test_organism_permutation_equivariance_with_memory(): + """The RP-level test uses positional tensors; the Organism keys memory by entity id, which + is a second place the order could leak in.""" + org = make_org(memory=True) + attrs, coords, _, _, _ = make_inputs() + r, ids = flat_diff(coords), list(range(N)) + mem = {i: torch.randn(H) for i in ids} + y0, m0 = org(attrs, r, ids, (0.0, 0.0), mem) + + perm = torch.randperm(N) + ids_p = [ids[k] for k in perm.tolist()] + y1, m1 = org(attrs[perm], flat_diff(coords[perm]), ids_p, (0.0, 0.0), mem) + assert torch.allclose(y0[perm], y1, atol=1e-5) + assert all(torch.allclose(m0[i], m1[i], atol=1e-5) for i in ids)