ConsDrive / tests /test_pim_core.py
ZenoNing's picture
Upload ConsDrive v2 source (part 2)
314ecfb verified
Raw History Blame Contribute Delete
970 Bytes
import torch
from models.pim import PersistentInteractionMemory
def test_identity_and_persistence():
torch.manual_seed(1)
m = PersistentInteractionMemory(16, slots=2, memory_dim=8, heads=2)
z, a = torch.randn(1, 2, 16), torch.randn(1, 3, 16)
with torch.no_grad():
m.visibility.weight.zero_()
m.visibility.bias.fill_(10)
m.release[-1].weight.zero_()
m.release[-1].bias.fill_(-10)
out, state, _ = m(z, a, torch.tensor([0.]))
assert torch.equal(out, a)
old = state.features.clone()
with torch.no_grad():
m.visibility.bias.fill_(-10)
_, state, info = m(z, a, torch.tensor([5.]), state)
assert state.valid.all()
assert torch.equal(state.features, old)
assert torch.equal(info['age'], torch.full((1, 2), 5.))
assert torch.equal(state.last_observed, torch.zeros(1, 2))
_, state, _ = m(z, a, torch.tensor([0.]), state, reset=torch.tensor([True]))
assert not state.valid.any()