test(core): make organism memory tests load-bearing (spec §4)

This commit is contained in:
chrnx committed 2026-09-01 23:57:02 -05:00
1 parent 0b4ee0d490
commit c940621977
1 file changed
+25 -3
+25 -3
View File
@@ -116,6 +116,12 @@ def test_organism_memory_lifecycle():
# a newborn's previous state was zero: its update equals the update from an explicit zero # 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)}) _, mem3 = org(attrs, r, ids2, (0.0, 0.0), {**mem, 99: torch.zeros(H)})
assert torch.allclose(mem2[99], mem3[99]) assert torch.allclose(mem2[99], mem3[99])
# memory must be load-bearing: zeroing a survivor's incoming state changes the outcome
survivor = ids2[0]
zeroed_mem = {i: torch.zeros(H) for i in mem}
y2_zeroed, mem2_zeroed = org(attrs, r, ids2, (0.0, 0.0), zeroed_mem)
assert not torch.allclose(y2, y2_zeroed)
assert not torch.allclose(mem2[survivor], mem2_zeroed[survivor])
def test_organism_without_memory_ignores_history(): def test_organism_without_memory_ignores_history():
@@ -138,16 +144,32 @@ def test_organism_context_defaults_to_affect():
assert not torch.allclose(y_a, y_c) assert not torch.allclose(y_a, y_c)
def test_gradient_reaches_cppn_through_memory(): def test_gradient_flows_through_memory_across_steps():
# A single step already sends gradient into the CPPN (it runs every call, memory
# or not) so that alone doesn't prove the recurrence is live. Compare the real
# 3-step rollout against one where memory is detached after every step, which
# severs the cross-step graph but leaves each step's own CPPN call intact: if
# memory actually carries gradient across time the two must disagree.
org = make_org(memory=True, kappa=1.0) org = make_org(memory=True, kappa=1.0)
attrs, coords, _, _, _ = make_inputs() attrs, coords, _, _, _ = make_inputs()
r = flat_diff(coords); ids = list(range(N)) r = flat_diff(coords); ids = list(range(N))
mem = {} mem = {}
for _ in range(3): for _ in range(3):
y, mem = org(attrs, r, ids, (0.0, 0.5), mem) y, mem = org(attrs, r, ids, (0.0, 0.5), mem)
y.pow(2).sum().backward() y.pow(2).sum().backward()
g = org.rp.cppn.l1.weight.grad g_recurrent = org.rp.cppn.l1.weight.grad.clone()
assert g is not None and g.abs().sum() > 0
org.zero_grad()
mem = {}
for _ in range(3):
y, mem = org(attrs, r, ids, (0.0, 0.5), mem)
mem = detach_memory(mem)
y.pow(2).sum().backward()
g_severed = org.rp.cppn.l1.weight.grad.clone()
assert g_recurrent.abs().sum() > 0
assert not torch.allclose(g_recurrent, g_severed)
def test_detach_memory_cuts_graph(): def test_detach_memory_cuts_graph():