File size: 12,158 Bytes
fee179c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env python3
"""
============================================================================
LatentRecurrentFlow (LRF) β€” End-to-End Test Script
============================================================================

Tests the full pipeline on CPU:
1. Model creation and parameter counting
2. VAE forward pass
3. Flow matching forward pass  
4. Recursive latent core forward pass
5. Full training loop (few steps)
6. Sample generation
7. Checkpoint save/load

Run: python test_lrf.py
"""

import sys
import os
import time
import torch
import traceback

# Add project root
sys.path.insert(0, '/app')

def test_model_creation():
    """Test model creation with different configs."""
    print("\n[TEST 1] Model Creation")
    print("-" * 40)
    
    from lrf.model import LatentRecurrentFlow
    
    # Test tiny config
    model = LatentRecurrentFlow(LatentRecurrentFlow.tiny_config())
    counts = model.count_parameters()
    print("Tiny config parameters:")
    for name, count in counts.items():
        print(f"  {name}: {count:,}")
    assert counts['total'] > 0, "Model has no parameters!"
    
    # Test default config
    model_default = LatentRecurrentFlow(LatentRecurrentFlow.default_config())
    counts_default = model_default.count_parameters()
    print("\nDefault config parameters:")
    for name, count in counts_default.items():
        print(f"  {name}: {count:,}")
    assert counts_default['total'] > counts['total'], "Default should be larger than tiny"
    
    print("βœ“ Model creation passed")
    return True


def test_vae():
    """Test VAE forward and backward."""
    print("\n[TEST 2] VAE Forward/Backward")
    print("-" * 40)
    
    from lrf.model import CompactVAE
    
    vae = CompactVAE(in_channels=3, latent_channels=16, encoder_base_ch=32, decoder_base_ch=64)
    
    # Count params
    enc_params = sum(p.numel() for p in vae.encoder.parameters())
    dec_params = sum(p.numel() for p in vae.decoder.parameters())
    print(f"Encoder params: {enc_params:,}")
    print(f"Decoder params: {dec_params:,}")
    
    # Forward
    x = torch.randn(2, 3, 64, 64)
    recon, mean, logvar = vae(x)
    print(f"Input shape:   {x.shape}")
    print(f"Latent shape:  {mean.shape}")
    print(f"Recon shape:   {recon.shape}")
    
    assert recon.shape == x.shape, f"Reconstruction shape mismatch: {recon.shape} != {x.shape}"
    assert mean.shape[1] == 16, f"Latent channels mismatch: {mean.shape[1]}"
    
    # Backward
    loss = F.l1_loss(recon, x) - 0.5 * torch.mean(1 + logvar - mean.pow(2) - logvar.exp()) * 1e-6
    loss.backward()
    
    # Check gradients
    grad_ok = all(p.grad is not None for p in vae.parameters() if p.requires_grad)
    print(f"Gradients computed: {grad_ok}")
    
    print("βœ“ VAE test passed")
    return True


def test_gla():
    """Test Gated Linear Attention."""
    print("\n[TEST 3] Gated Linear Attention")
    print("-" * 40)
    
    from lrf.model import GatedLinearAttention
    
    gla = GatedLinearAttention(dim=64, num_heads=4, head_dim=16)
    
    B, H, W, D = 2, 8, 8, 64
    x = torch.randn(B, H * W, D)
    
    t0 = time.time()
    out = gla(x, h=H, w=W)
    dt = time.time() - t0
    
    print(f"Input:  {x.shape}")
    print(f"Output: {out.shape}")
    print(f"Time:   {dt*1000:.1f}ms")
    
    assert out.shape == x.shape, f"Shape mismatch: {out.shape}"
    
    # Test with larger sequence
    B, H, W, D = 1, 32, 32, 64
    x_large = torch.randn(B, H * W, D)
    t0 = time.time()
    out_large = gla(x_large, h=H, w=W)
    dt_large = time.time() - t0
    print(f"\nLarger input (32x32={H*W} tokens):")
    print(f"  Time: {dt_large*1000:.1f}ms")
    
    print("βœ“ GLA test passed")
    return True


def test_recursive_core():
    """Test the Recursive Latent Core."""
    print("\n[TEST 4] Recursive Latent Core")
    print("-" * 40)
    
    from lrf.model import RecursiveLatentCore
    
    core = RecursiveLatentCore(
        dim=32,
        cond_dim=64,
        num_blocks=2,
        num_heads=2,
        head_dim=16,
        T_inner=2,
        T_outer=1,
        use_ift_training=False,
    )
    
    params = sum(p.numel() for p in core.parameters())
    print(f"Core params: {params:,}")
    
    B, C, H, W = 2, 32, 4, 4
    z_t = torch.randn(B, C, H, W)
    t = torch.rand(B)
    text_emb = torch.randn(B, 10, 64)
    text_global = torch.randn(B, 64)
    
    # Forward
    t0 = time.time()
    v = core(z_t, t, text_emb, text_global)
    dt = time.time() - t0
    
    print(f"Input shape:  {z_t.shape}")
    print(f"Output shape: {v.shape}")
    print(f"Time:         {dt*1000:.1f}ms")
    
    assert v.shape == z_t.shape, f"Shape mismatch: {v.shape}"
    
    # Backward
    loss = v.pow(2).mean()
    loss.backward()
    
    grad_ok = sum(1 for p in core.parameters() if p.grad is not None and p.requires_grad)
    total_params = sum(1 for p in core.parameters() if p.requires_grad)
    print(f"Params with grad: {grad_ok}/{total_params}")
    
    print("βœ“ Recursive core test passed")
    return True


def test_ift_training():
    """Test IFT (Implicit Function Theorem) training mode."""
    print("\n[TEST 5] IFT Training Mode")
    print("-" * 40)
    
    from lrf.model import RecursiveLatentCore
    
    # Test with IFT enabled
    core_ift = RecursiveLatentCore(
        dim=32, cond_dim=64, num_blocks=2, num_heads=2, head_dim=16,
        T_inner=3, T_outer=2, use_ift_training=True,
    )
    core_ift.train()
    
    z_t = torch.randn(2, 32, 4, 4, requires_grad=True)
    t = torch.rand(2)
    
    v = core_ift(z_t, t)
    loss = v.pow(2).mean()
    loss.backward()
    
    print(f"IFT mode: loss={loss.item():.4f}")
    print(f"  T_outer={core_ift.T_outer}, T_inner={core_ift.T_inner}")
    print(f"  Effective depth: {core_ift.T_outer * core_ift.T_inner * core_ift.num_blocks} layers")
    print(f"  Actual blocks: {core_ift.num_blocks}")
    
    print("βœ“ IFT training test passed")
    return True


def test_flow_matching():
    """Test flow matching scheduler."""
    print("\n[TEST 6] Flow Matching Scheduler")
    print("-" * 40)
    
    from lrf.training import RectifiedFlowScheduler
    
    scheduler = RectifiedFlowScheduler(shift=1.0)
    
    z_0 = torch.randn(2, 16, 4, 4)
    noise = torch.randn_like(z_0)
    t = torch.tensor([0.0, 0.5])
    
    z_t = scheduler.add_noise(z_0, noise, t)
    v_target = scheduler.get_velocity_target(z_0, noise)
    
    print(f"z_0 shape: {z_0.shape}")
    print(f"z_t shape: {z_t.shape}")
    print(f"v_target shape: {v_target.shape}")
    
    # At t=0, z_t should equal z_0
    t_zero = torch.tensor([0.0, 0.0])
    z_t_zero = scheduler.add_noise(z_0, noise, t_zero)
    diff = (z_t_zero - z_0).abs().max().item()
    print(f"At t=0, |z_t - z_0| max = {diff:.6f}")
    assert diff < 1e-5, f"At t=0, z_t should equal z_0, got diff={diff}"
    
    # At t=1, z_t should equal noise
    t_one = torch.tensor([1.0, 1.0])
    z_t_one = scheduler.add_noise(z_0, noise, t_one)
    diff_one = (z_t_one - noise).abs().max().item()
    print(f"At t=1, |z_t - noise| max = {diff_one:.6f}")
    assert diff_one < 1e-5, f"At t=1, z_t should equal noise, got diff={diff_one}"
    
    print("βœ“ Flow matching test passed")
    return True


def test_full_training():
    """Test full training pipeline."""
    print("\n[TEST 7] Full Training Pipeline")
    print("-" * 40)
    
    from lrf.model import LatentRecurrentFlow
    from lrf.training import LRFTrainer, SyntheticImageTextDataset
    from torch.utils.data import DataLoader
    
    config = LatentRecurrentFlow.tiny_config()
    model = LatentRecurrentFlow(config)
    
    trainer = LRFTrainer(model, torch.device('cpu'), '/app/test_checkpoints')
    
    dataset = SyntheticImageTextDataset(num_samples=16, image_size=64, max_text_length=32)
    dataloader = DataLoader(dataset, batch_size=4, shuffle=True)
    
    # Stage 1: VAE
    print("  Training VAE...")
    vae_opt = torch.optim.AdamW(model.vae.parameters(), lr=1e-3)
    for i, batch in enumerate(dataloader):
        if i >= 3:
            break
        losses = trainer.train_vae_step(batch['image'], vae_opt)
        print(f"    VAE step {i}: loss={losses['total']:.4f}")
    
    # Stage 2: Flow matching
    print("  Training flow matching...")
    for p in model.vae.parameters():
        p.requires_grad = False
    
    flow_params = list(model.core.parameters()) + list(model.text_encoder.parameters())
    flow_opt = torch.optim.AdamW(flow_params, lr=1e-3)
    
    for i, batch in enumerate(dataloader):
        if i >= 3:
            break
        losses = trainer.train_flow_step(
            batch['image'], batch['token_ids'], batch['attention_mask'],
            flow_opt,
        )
        print(f"    Flow step {i}: loss={losses['flow_loss']:.4f}")
    
    # Generate
    print("  Generating samples...")
    sample_tokens = torch.randint(1, 31999, (2, 32))
    sample_mask = torch.ones(2, 32)
    
    images = trainer.generate(
        sample_tokens, sample_mask,
        num_steps=5, cfg_scale=1.0,
        latent_h=4, latent_w=4,
    )
    print(f"  Generated: {images.shape}, range=[{images.min():.3f}, {images.max():.3f}]")
    
    # Save/load checkpoint
    print("  Saving checkpoint...")
    trainer.save_checkpoint('/app/test_checkpoints/test.pt', 'test', 0)
    trainer.load_checkpoint('/app/test_checkpoints/test.pt')
    
    print("βœ“ Full training pipeline test passed")
    return True


def test_memory_estimate():
    """Estimate memory usage for different configs."""
    print("\n[TEST 8] Memory Estimation")
    print("-" * 40)
    
    from lrf.model import LatentRecurrentFlow
    
    configs = {
        'tiny': LatentRecurrentFlow.tiny_config(),
        'default': LatentRecurrentFlow.default_config(),
    }
    
    for name, config in configs.items():
        model = LatentRecurrentFlow(config)
        counts = model.count_parameters()
        
        # Estimate memory
        param_bytes = counts['total'] * 4  # float32
        param_mb = param_bytes / (1024 * 1024)
        
        # INT8 deployment
        param_int8_mb = counts['total'] * 1 / (1024 * 1024)
        
        print(f"\n{name} config:")
        print(f"  Total params: {counts['total']:,}")
        print(f"  FP32 size: {param_mb:.1f} MB")
        print(f"  INT8 size: {param_int8_mb:.1f} MB")
        
        # Estimate activation memory for 256x256 generation
        latent_h = 256 // 16
        latent_w = 256 // 16
        latent_tokens = latent_h * latent_w
        act_bytes = 2 * latent_tokens * config['latent_channels'] * 4  # Conservative
        act_mb = act_bytes / (1024 * 1024)
        print(f"  Est. activation memory (256x256): {act_mb:.1f} MB")
        
        del model
    
    print("\nβœ“ Memory estimation passed")
    return True


# Import F for backward test
import torch.nn.functional as F

def main():
    """Run all tests."""
    print("=" * 60)
    print("LatentRecurrentFlow (LRF) - End-to-End Tests")
    print("=" * 60)
    
    tests = [
        ("Model Creation", test_model_creation),
        ("VAE", test_vae),
        ("GLA", test_gla),
        ("Recursive Core", test_recursive_core),
        ("IFT Training", test_ift_training),
        ("Flow Matching", test_flow_matching),
        ("Full Training", test_full_training),
        ("Memory Estimate", test_memory_estimate),
    ]
    
    results = []
    for name, test_fn in tests:
        try:
            passed = test_fn()
            results.append((name, passed))
        except Exception as e:
            print(f"\nβœ— {name} FAILED: {e}")
            traceback.print_exc()
            results.append((name, False))
    
    print("\n" + "=" * 60)
    print("Test Summary")
    print("=" * 60)
    
    all_passed = True
    for name, passed in results:
        status = "βœ“ PASS" if passed else "βœ— FAIL"
        print(f"  {status}: {name}")
        if not passed:
            all_passed = False
    
    if all_passed:
        print("\nβœ“ ALL TESTS PASSED!")
    else:
        print("\nβœ— SOME TESTS FAILED!")
        sys.exit(1)
    
    return all_passed


if __name__ == '__main__':
    main()