File size: 11,433 Bytes
bf15d9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
NeuroLex v4 β€” Quick Test & Validation Script
=============================================
Run this to verify everything works before training.
Works on CPU (no GPU needed for testing).

Usage:
    python test_model.py
"""

import torch
import sys
import time

def test_imports():
    """Test all imports work."""
    print("Testing imports...", end=" ")
    try:
        from neurolex_v4_model import (
            NeuroLexV4, NeuroLexConfig, CharTokenizer, create_model,
            AdaptiveLayerNorm, DiTBlock, TimeEmbedding,
            DOMAINS, STYLES, LANGUAGES, DOMAIN_TO_ID, STYLE_TO_ID, LANG_TO_ID
        )
        from neurolex_v4_dataset import (
            create_dataloaders, NeuroLexDataset, StreamingNeuroLexDataset,
            LANGUAGE_WORDS, DOMAIN_NAMES, generate_blended_name,
            generate_augmented_dataset, PREFIXES, SUFFIXES
        )
        print("βœ… All imports successful")
        return True
    except Exception as e:
        print(f"❌ Import error: {e}")
        return False


def test_tokenizer():
    """Test CharTokenizer encode/decode."""
    print("Testing tokenizer...", end=" ")
    from neurolex_v4_model import CharTokenizer
    
    tok = CharTokenizer()
    
    # Test encode/decode roundtrip
    test_names = ['Nexora', 'FluxByte', 'luminara', 'Kaze-Flow', 'X42']
    for name in test_names:
        encoded = tok.encode(name, max_len=24)
        decoded = tok.decode(encoded)
        assert decoded == name, f"Roundtrip failed: '{name}' β†’ {encoded} β†’ '{decoded}'"
    
    # Test batch operations
    batch = tok.batch_encode(test_names, max_len=24)
    assert batch.shape == (5, 24), f"Batch shape wrong: {batch.shape}"
    
    decoded_batch = tok.batch_decode(batch)
    for orig, dec in zip(test_names, decoded_batch):
        assert orig == dec, f"Batch roundtrip failed: '{orig}' β†’ '{dec}'"
    
    print(f"βœ… Tokenizer works (vocab_size={tok.vocab_size})")
    return True


def test_model_forward():
    """Test model forward pass with correct shapes."""
    print("Testing model forward pass...", end=" ")
    from neurolex_v4_model import NeuroLexV4, NeuroLexConfig, CharTokenizer
    
    config = NeuroLexConfig(
        d_model=64, n_heads=4, n_layers=2, d_ff=128  # Tiny for testing
    )
    model = NeuroLexV4(config)
    tok = CharTokenizer()
    
    # Create test inputs
    B, L = 4, 24
    x = tok.batch_encode(['Nexora', 'FluxByte', 'Kaze', 'Luminara'], max_len=L)
    t = torch.rand(B)
    domain_id = torch.randint(0, config.n_domains, (B,))
    style_id = torch.randint(0, config.n_styles, (B,))
    lang_id = torch.randint(0, config.n_languages, (B,))
    length_id = torch.randint(0, config.n_lengths, (B,))
    
    # Forward pass
    logits = model(x, t, domain_id, style_id, lang_id, length_id)
    assert logits.shape == (B, L, config.vocab_size), f"Output shape wrong: {logits.shape}"
    
    # Test with CFG mask
    cfg_mask = torch.ones(B, dtype=torch.bool)
    logits_uncond = model(x, t, domain_id, style_id, lang_id, length_id, cfg_mask=cfg_mask)
    assert logits_uncond.shape == (B, L, config.vocab_size)
    
    # Verify CFG makes a difference
    diff = (logits - logits_uncond).abs().sum()
    assert diff > 0, "CFG mask should produce different logits"
    
    print(f"βœ… Forward pass works (output: {logits.shape})")
    return True


def test_loss_computation():
    """Test UDLM loss computation."""
    print("Testing loss computation...", end=" ")
    from neurolex_v4_model import NeuroLexV4, NeuroLexConfig, CharTokenizer
    
    config = NeuroLexConfig(d_model=64, n_heads=4, n_layers=2, d_ff=128)
    model = NeuroLexV4(config)
    tok = CharTokenizer()
    
    B = 8
    x = tok.batch_encode(['Test' + str(i) for i in range(B)], max_len=24)
    domain_id = torch.randint(0, config.n_domains, (B,))
    style_id = torch.randint(0, config.n_styles, (B,))
    lang_id = torch.randint(0, config.n_languages, (B,))
    length_id = torch.randint(0, config.n_lengths, (B,))
    
    # Compute loss
    loss = model.compute_loss(x, domain_id, style_id, lang_id, length_id)
    
    assert loss.dim() == 0, f"Loss should be scalar, got shape {loss.shape}"
    assert loss.item() > 0, f"Loss should be positive, got {loss.item()}"
    assert not torch.isnan(loss), "Loss is NaN!"
    assert not torch.isinf(loss), "Loss is Inf!"
    
    # Test backward pass
    loss.backward()
    
    # Check gradients exist
    has_grad = sum(1 for p in model.parameters() if p.grad is not None)
    total_params = sum(1 for p in model.parameters())
    assert has_grad == total_params, f"Only {has_grad}/{total_params} params have gradients"
    
    print(f"βœ… Loss works (loss={loss.item():.4f}, grads OK)")
    return True


def test_generation():
    """Test generation pipeline."""
    print("Testing generation...", end=" ")
    from neurolex_v4_model import NeuroLexV4, NeuroLexConfig, DOMAIN_TO_ID, STYLE_TO_ID, LANG_TO_ID
    
    config = NeuroLexConfig(d_model=64, n_heads=4, n_layers=2, d_ff=128)
    model = NeuroLexV4(config)
    model.eval()
    
    # Generate with minimal steps (fast test)
    names = model.generate(
        domain_id=DOMAIN_TO_ID['tech'],
        style_id=STYLE_TO_ID['sharp'],
        lang_id=LANG_TO_ID['english'],
        target_length=7,
        batch_size=8,
        cfg_scale=2.0,
        temperature=0.9,
        n_steps=10,  # Very few steps for testing
        odd_alpha=4.0,
        device='cpu'
    )
    
    assert len(names) > 0, "No names generated!"
    assert all(isinstance(n, str) for n in names), "Names should be strings"
    assert all(len(n) >= 3 for n in names), f"Names too short: {names}"
    
    # Test diversity (even with untrained model and few steps)
    unique = set(n.lower() for n in names)
    
    print(f"βœ… Generation works ({len(names)} names, {len(unique)} unique)")
    print(f"    Sample: {names[:5]}")
    return True


def test_dataset():
    """Test dataset generation."""
    print("Testing dataset...", end=" ")
    from neurolex_v4_dataset import (
        NeuroLexDataset, StreamingNeuroLexDataset, 
        generate_augmented_dataset, LANGUAGE_WORDS, DOMAIN_NAMES
    )
    
    # Test augmented dataset
    data = generate_augmented_dataset(n_samples=1000, seed=42)
    assert len(data) >= 1000, f"Dataset too small: {len(data)}"
    
    # Verify structure
    sample = data[0]
    assert 'name' in sample, "Missing 'name' field"
    assert 'domain' in sample, "Missing 'domain' field"
    assert 'style' in sample, "Missing 'style' field"
    assert 'language' in sample, "Missing 'language' field"
    assert 'length' in sample, "Missing 'length' field"
    
    # Test PyTorch Dataset
    ds = NeuroLexDataset(n_samples=500, seed=42)
    item = ds[0]
    assert item['input_ids'].shape == (24,), f"Wrong shape: {item['input_ids'].shape}"
    assert item['domain'].dim() == 0, "Domain should be scalar"
    
    # Test streaming dataset
    stream_ds = StreamingNeuroLexDataset(epoch_size=100)
    stream_item = stream_ds[0]
    assert stream_item['input_ids'].shape == (24,)
    
    # Check language coverage
    assert len(LANGUAGE_WORDS) >= 25, f"Only {len(LANGUAGE_WORDS)} languages"
    total_words = sum(len(v) for v in LANGUAGE_WORDS.values())
    total_brands = sum(len(v) for v in DOMAIN_NAMES.values())
    
    print(f"βœ… Dataset works ({len(data)} samples, {total_words} lang words, {total_brands} brands)")
    return True


def test_dataloader():
    """Test DataLoader integration."""
    print("Testing dataloader...", end=" ")
    from neurolex_v4_dataset import create_dataloaders
    
    train_loader, val_loader = create_dataloaders(
        batch_size=32, n_samples=500, num_workers=0
    )
    
    batch = next(iter(train_loader))
    assert batch['input_ids'].shape == (32, 24), f"Wrong batch shape: {batch['input_ids'].shape}"
    assert batch['domain'].shape == (32,)
    assert batch['style'].shape == (32,)
    assert batch['language'].shape == (32,)
    assert batch['length'].shape == (32,)
    
    print(f"βœ… Dataloader works (train={len(train_loader)} batches, val={len(val_loader)} batches)")
    return True


def test_training_step():
    """Test one complete training step."""
    print("Testing training step...", end=" ")
    from neurolex_v4_model import NeuroLexV4, NeuroLexConfig
    from neurolex_v4_dataset import create_dataloaders
    from torch.optim import AdamW
    
    config = NeuroLexConfig(d_model=64, n_heads=4, n_layers=2, d_ff=128)
    model = NeuroLexV4(config)
    optimizer = AdamW(model.parameters(), lr=1e-3)
    
    train_loader, _ = create_dataloaders(batch_size=16, n_samples=100, num_workers=0)
    batch = next(iter(train_loader))
    
    # Training step
    model.train()
    loss = model.compute_loss(
        batch['input_ids'], batch['domain'], batch['style'],
        batch['language'], batch['length']
    )
    
    optimizer.zero_grad()
    loss.backward()
    
    # Check gradients are finite
    for name, p in model.named_parameters():
        if p.grad is not None:
            assert not torch.isnan(p.grad).any(), f"NaN gradient in {name}"
            assert not torch.isinf(p.grad).any(), f"Inf gradient in {name}"
    
    optimizer.step()
    
    # Verify loss decreased after step (not guaranteed but likely)
    loss2 = model.compute_loss(
        batch['input_ids'], batch['domain'], batch['style'],
        batch['language'], batch['length']
    )
    
    print(f"βœ… Training step works (loss: {loss.item():.4f} β†’ {loss2.item():.4f})")
    return True


def test_model_sizes():
    """Test all model size presets."""
    print("Testing model sizes...", end=" ")
    from neurolex_v4_model import create_model
    
    sizes = {
        'tiny': (1_000_000, 5_000_000),    # 1-5M expected
        'small': (3_000_000, 8_000_000),   # 3-8M expected
        'base': (8_000_000, 15_000_000),   # 8-15M expected
        'large': (15_000_000, 35_000_000), # 15-35M expected
    }
    
    for size_name, (min_params, max_params) in sizes.items():
        model, config = create_model(size_name)
        n_params = model.count_parameters()
        assert min_params <= n_params <= max_params, \
            f"{size_name}: {n_params} params not in [{min_params}, {max_params}]"
    
    print(f"βœ… All model sizes valid")
    return True


def run_all_tests():
    """Run all tests."""
    print("=" * 60)
    print("  NEUROLEX v4 β€” VALIDATION TESTS")
    print("=" * 60)
    print()
    
    tests = [
        test_imports,
        test_tokenizer,
        test_model_forward,
        test_loss_computation,
        test_dataset,
        test_dataloader,
        test_training_step,
        test_generation,
        test_model_sizes,
    ]
    
    passed = 0
    failed = 0
    
    for test_fn in tests:
        try:
            if test_fn():
                passed += 1
            else:
                failed += 1
        except Exception as e:
            print(f"❌ {test_fn.__name__} FAILED: {e}")
            import traceback
            traceback.print_exc()
            failed += 1
    
    print()
    print("=" * 60)
    if failed == 0:
        print(f"  βœ… ALL {passed} TESTS PASSED β€” Ready to train!")
    else:
        print(f"  ⚠️  {passed} passed, {failed} FAILED")
    print("=" * 60)
    
    return failed == 0


if __name__ == '__main__':
    success = run_all_tests()
    sys.exit(0 if success else 1)