arcisvlm / tests /test_dreamer.py
Hardik Sanghvi
Claude Opus 4.6 (1M context)
feat: integrate Gemma 4 E2B backbone for production-quality VLM inference
7a564e3
Raw History Blame Contribute Delete
3.54 kB
"""Tests for LatentDreamer — future frame prediction in JEPA space."""
import pytest
import torch
from model.latent_dreamer import LatentDreamer
class TestLatentDreamer:
"""Test the latent visual dreaming module."""
@pytest.fixture
def dreamer(self):
return LatentDreamer(
embed_dim=256, # Smaller for testing
n_heads=4,
n_layers=2,
max_future_steps=8,
max_context_frames=16,
)
def test_dream_single_frame(self, dreamer):
"""Dream from a single context frame."""
ctx = torch.randn(2, 256) # [B, D] — single frame
results = dreamer.dream(ctx, n_steps=3)
assert len(results) == 3
for emb, conf in results:
assert emb.shape == (2, 256)
assert conf.shape == (2, 1)
def test_dream_multi_frame(self, dreamer):
"""Dream from multiple context frames."""
ctx = torch.randn(2, 8, 256) # [B, 8, D]
results = dreamer.dream(ctx, n_steps=4)
assert len(results) == 4
for emb, conf in results:
assert emb.shape == (2, 256)
assert conf.shape == (2, 1)
def test_dream_sequence(self, dreamer):
"""Test stacked tensor output."""
ctx = torch.randn(2, 4, 256)
embs, confs = dreamer.dream_sequence(ctx, n_steps=3)
assert embs.shape == (2, 3, 256)
assert confs.shape == (2, 3, 1)
def test_confidence_range(self, dreamer):
"""Confidence should be in [0, 1]."""
ctx = torch.randn(2, 4, 256)
results = dreamer.dream(ctx, n_steps=4)
for _, conf in results:
assert (conf >= 0).all() and (conf <= 1).all()
def test_max_steps_clamped(self, dreamer):
"""Should not generate more than max_future_steps."""
ctx = torch.randn(1, 4, 256)
results = dreamer.dream(ctx, n_steps=100) # Way over max
assert len(results) == dreamer.max_future_steps
def test_dream_loss(self, dreamer):
"""Test training loss computation."""
pred = torch.randn(2, 4, 256, requires_grad=True)
actual = torch.randn(2, 4, 256)
conf = torch.sigmoid(torch.randn(2, 4, 1, requires_grad=True))
loss_dict = dreamer.compute_dream_loss(pred, actual, conf)
assert "total_loss" in loss_dict
assert "mse_loss" in loss_dict
assert "cosine_loss" in loss_dict
assert "calibration_loss" in loss_dict
assert "mean_cosine_sim" in loss_dict
assert loss_dict["total_loss"].requires_grad
def test_dream_loss_perfect_prediction(self, dreamer):
"""Loss should be near zero for identical predictions."""
x = torch.randn(2, 3, 256)
conf = torch.ones(2, 3, 1)
loss_dict = dreamer.compute_dream_loss(x, x.clone(), conf)
assert loss_dict["mse_loss"].item() < 1e-5
assert loss_dict["cosine_loss"].item() < 1e-5
def test_gradient_flow(self, dreamer):
"""Ensure gradients flow through the dreamer."""
dreamer.train()
ctx = torch.randn(1, 4, 256, requires_grad=True)
results = dreamer.dream(ctx, n_steps=2)
loss = sum(emb.sum() for emb, _ in results)
loss.backward()
# At least some dreamer parameters should have gradients
has_grads = any(
p.grad is not None and p.grad.abs().sum() > 0
for p in dreamer.parameters()
if p.requires_grad
)
assert has_grads, "No gradients flowed through the dreamer"