feat(core): gated relational perceptron and organism with per-entity memory (spec §4)

This commit is contained in:
chrnx committed 2026-09-01 23:51:43 -05:00
1 parent dbc5d18a77
commit 0b4ee0d490
3 files changed
+156

No files matched your search

+87
View File
@@ -70,3 +70,90 @@ def test_rp_mask_removes_neighbours():
masked_b = rp(attrs2, r, edge, mask2, ctx)
assert torch.allclose(masked_a[0], masked_b[0], atol=1e-5)
assert not torch.allclose(full[0], masked_a[0])
from ailed.core import GatedRelationalPerceptron, Organism, detach_memory
def test_grp_kappa_zero_ignores_arousal():
grp = GatedRelationalPerceptron(H, kappa=0.0)
h, M = torch.randn(N, H), torch.randn(N, H)
assert torch.allclose(grp(h, M, 0.0), grp(h, M, 1.0))
def test_grp_high_arousal_overwrites():
grp = GatedRelationalPerceptron(H, kappa=50.0)
h, M = torch.randn(N, H), torch.randn(N, H)
out = grp(h, M, 1.0)
cand = torch.tanh(grp.cand(torch.cat([h, M], -1)))
assert torch.allclose(out, cand, atol=1e-3)
def test_grp_gate_saturated_keeps():
grp = GatedRelationalPerceptron(H, kappa=1.0)
with torch.no_grad():
grp.gate.weight.zero_(); grp.gate.bias.fill_(50.0)
h, M = torch.randn(N, H), torch.randn(N, H)
assert torch.allclose(grp(h, M, 0.0), M, atol=1e-3)
def make_org(**kw):
return Organism(attr_dim=D_ATTR, coord_dim=K, target_dim=2, hidden_dim=H, cppn_hidden=16, **kw)
def test_organism_memory_lifecycle():
org = make_org(memory=True)
attrs, coords, _, _, _ = make_inputs()
r = flat_diff(coords)
ids = list(range(N))
y, mem = org(attrs, r, ids, (0.0, 0.0), {})
assert y.shape == (N, 2)
assert set(mem) == set(ids)
# entity 0 leaves, entity 99 is born
ids2 = ids[1:] + [99]
y2, mem2 = org(attrs, r, ids2, (0.0, 0.0), mem)
assert 0 not in mem2 and 99 in mem2
# a newborn's previous state was zero: its update equals the update from an explicit zero
_, mem3 = org(attrs, r, ids2, (0.0, 0.0), {**mem, 99: torch.zeros(H)})
assert torch.allclose(mem2[99], mem3[99])
def test_organism_without_memory_ignores_history():
org = make_org(memory=False)
attrs, coords, _, _, _ = make_inputs()
r = flat_diff(coords); ids = list(range(N))
y0, mem0 = org(attrs, r, ids, (0.0, 0.0), {})
y1, _ = org(attrs, r, ids, (0.0, 0.0), {i: torch.randn(H) for i in ids})
assert torch.allclose(y0, y1) and mem0 == {}
def test_organism_context_defaults_to_affect():
org = make_org(memory=True)
attrs, coords, _, _, _ = make_inputs()
r = flat_diff(coords); ids = list(range(N))
y_a, _ = org(attrs, r, ids, (0.3, 0.6), {})
y_b, _ = org(attrs, r, ids, (0.3, 0.6), {}, context=torch.tensor([0.3, 0.6]))
y_c, _ = org(attrs, r, ids, (0.3, 0.6), {}, context=torch.zeros(2))
assert torch.allclose(y_a, y_b)
assert not torch.allclose(y_a, y_c)
def test_gradient_reaches_cppn_through_memory():
org = make_org(memory=True, kappa=1.0)
attrs, coords, _, _, _ = make_inputs()
r = flat_diff(coords); ids = list(range(N))
mem = {}
for _ in range(3):
y, mem = org(attrs, r, ids, (0.0, 0.5), mem)
y.pow(2).sum().backward()
g = org.rp.cppn.l1.weight.grad
assert g is not None and g.abs().sum() > 0
def test_detach_memory_cuts_graph():
org = make_org(memory=True)
attrs, coords, _, _, _ = make_inputs()
r = flat_diff(coords); ids = list(range(N))
_, 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())