File size: 10,931 Bytes
e3a8356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62a3dea
 
e3a8356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Demo script for LMCODE (Language Model with Memory CODE).

Demonstrates the dual memory system in action.
"""

import torch
import matplotlib.pyplot as plt
from model_architecture import LMCODE, LMCODEConfig
from training import MemoryAwareTrainer, MemoryDataset, create_synthetic_dataset
from utils import (
    analyze_memory_capacity, 
    compute_memory_efficiency,
    visualize_memory_flow,
    plot_training_history,
    MemoryMonitor,
    generate_memory_report
)
import numpy as np


def demo_basic_usage():
    """Demonstrate basic model usage."""
    print("=" * 60)
    print("DEMO 1: Basic Model Usage")
    print("=" * 60)
    
    # Create a small model for demonstration
    config = LMCODEConfig(
        vocab_size=1000,  # Smaller vocab for demo
        hidden_size=128,
        num_layers=3,
        num_heads=4,
        short_term_memory_size=128,
        long_term_memory_slots=1000
    )
    
    model = LMCODE(config)
    
    print(f"\nModel created with config:")
    print(f"  Vocabulary size: {config.vocab_size}")
    print(f"  Hidden size: {config.hidden_size}")
    print(f"  Number of layers: {config.num_layers}")
    print(f"  Number of heads: {config.num_heads}")
    print(f"  Short-term memory size: {config.short_term_memory_size}")
    print(f"  Long-term memory slots: {config.long_term_memory_slots}")
    
    # Count parameters
    total_params = sum(p.numel() for p in model.parameters())
    print(f"\nTotal parameters: {total_params:,}")
    
    # Forward pass
    batch_size = 2
    seq_len = 20
    input_ids = torch.randint(0, config.vocab_size, (batch_size, seq_len))
    
    print(f"\nInput shape: {input_ids.shape}")
    
    with torch.no_grad():
        outputs = model(input_ids, use_long_term_memory=True)
    
    print(f"Output logits shape: {outputs['logits'].shape}")
    print(f"Loss: {outputs['loss']}")
    
    # Test generation
    print("\nTesting text generation...")
    start_tokens = torch.randint(0, config.vocab_size, (1, 5))
    
    with torch.no_grad():
        generated = model.generate(
            start_tokens,
            max_length=30,
            temperature=1.0,
            top_k=50,
            top_p=0.9,
            use_long_term_memory=True
        )
    
    print(f"Input tokens: {start_tokens[0].tolist()}")
    print(f"Generated tokens (first 20): {generated[0][:20].tolist()}")
    print(f"Generated shape: {generated.shape}")
    
    return model, config


def demo_memory_operations(model):
    """Demonstrate memory store and retrieve operations."""
    print("\n" + "=" * 60)
    print("DEMO 2: Memory Store and Retrieve Operations")
    print("=" * 60)
    
    # Store some experiences
    experiences = [
        "The quick brown fox jumps over the lazy dog",
        "Machine learning is a subset of artificial intelligence",
        "Python is a popular programming language for data science",
        "Neural networks can learn complex patterns",
        "Transformers have revolutionized natural language processing"
    ]
    
    print("\nStoring experiences in long-term memory...")
    for exp in experiences:
        model.store_experience(exp)
        print(f"  Stored: {exp[:50]}...")
    
    # Try to retrieve
    print("\nQuerying memory...")
    queries = [
        "programming language",
        "neural networks",
        "machine learning"
    ]
    
    for query in queries:
        retrieved, indices = model.query_memory(query, top_k=3)
        print(f"\nQuery: '{query}'")
        print(f"  Retrieved shape: {retrieved.shape}")
        print(f"  Top indices: {indices[0].tolist()}")
    
    # Consolidate memories
    print("\nConsolidating memories (merging similar ones)...")
    for i, layer in enumerate(model.layers):
        before_active = (torch.sigmoid(layer.long_term_memory.memory_importance) > 0.1).sum().item()
        layer.long_term_memory.consolidate_memories(threshold=0.9)
        after_active = (torch.sigmoid(layer.long_term_memory.memory_importance) > 0.1).sum().item()
        print(f"  Layer {i}: {before_active} -> {after_active} active memories")


def demo_training():
    """Demonstrate training with memory-aware trainer."""
    print("\n" + "=" * 60)
    print("DEMO 3: Training with Memory-Aware Trainer")
    print("=" * 60)
    
    # Create model
    config = LMCODEConfig(
        vocab_size=1000,
        hidden_size=64,  # Small for fast demo
        num_layers=2,
        num_heads=4,
        short_term_memory_size=64,
        long_term_memory_slots=500
    )
    
    model = LMCODE(config)
    
    # Create dataset
    print("\nCreating synthetic dataset...")
    train_data = create_synthetic_dataset(num_samples=200, seq_len=20, vocab_size=1000)
    train_dataset = MemoryDataset(train_data, memory_sample_ratio=0.2)
    
    eval_data = create_synthetic_dataset(num_samples=50, seq_len=20, vocab_size=1000)
    eval_dataset = MemoryDataset(eval_data, memory_sample_ratio=0.2)
    
    print(f"Training samples: {len(train_data)}")
    print(f"Evaluation samples: {len(eval_data)}")
    
    # Create trainer
    trainer_config = {
        'learning_rate': 1e-3,
        'weight_decay': 0.01,
        'gradient_clip': 1.0,
        'memory_consolidation_interval': 20,
        'warmup_steps': 5,
        'total_steps': 200
    }
    
    trainer = MemoryAwareTrainer(model, trainer_config)
    
    # Train for 3 epochs
    print("\nTraining model (3 epochs, small for demo)...")
    history = trainer.train(
        train_dataset,
        num_epochs=3,
        batch_size=16,
        eval_dataset=eval_dataset
    )
    
    # Show results
    print("\nTraining complete!")
    print(f"Final train loss: {history['train_loss'][-1]:.4f}")
    if history['eval_loss']:
        print(f"Final eval loss: {history['eval_loss'][-1]:.4f}")
    
    # Save checkpoint
    trainer.save_checkpoint('demo_model.pt')
    
    return model, history


def demo_memory_analysis(model):
    """Demonstrate memory analysis tools."""
    print("\n" + "=" * 60)
    print("DEMO 4: Memory Analysis and Efficiency")
    print("=" * 60)
    
    # Create test sequences
    test_sequences = []
    for _ in range(10):
        seq = torch.randint(0, model.config.vocab_size, (1, 15))
        test_sequences.append(seq)
    
    # Analyze memory capacity
    print("\nAnalyzing memory capacity...")
    analysis = analyze_memory_capacity(model, test_sequences)
    
    print(f"Total memories stored: {analysis['total_memories']}")
    print(f"Successful retrievals: {analysis['successful_retrievals']}")
    print(f"Average similarity: {analysis.get('average_similarity', 'N/A')}")
    print(f"Capacity utilization: {analysis['capacity_utilization']:.2%}")
    
    # Compute efficiency
    print("\nComputing memory efficiency...")
    efficiency = compute_memory_efficiency(model)
    
    print(f"Total parameters: {efficiency['total_parameters']:,}")
    print(f"Memory parameters: {efficiency['memory_parameters']:,}")
    print(f"Memory parameter ratio: {efficiency['memory_parameter_ratio']:.2%}")
    print(f"Total memory slots: {efficiency['total_memory_slots']}")
    print(f"Parameters per slot: {efficiency['parameters_per_memory_slot']:.1f}")
    
    # Generate report
    print("\nGenerating memory report...")
    report = generate_memory_report(model, test_sequences, 'demo_memory_report.json')
    print(f"Report saved to demo_memory_report.json")
    
    return analysis, efficiency


def demo_visualization(model):
    """Demonstrate visualization tools."""
    print("\n" + "=" * 60)
    print("DEMO 5: Visualization Tools")
    print("=" * 60)
    
    # Create sample input
    input_seq = torch.randint(0, model.config.vocab_size, (1, 25))
    
    print("\nGenerating memory flow visualization...")
    try:
        fig = visualize_memory_flow(model, input_seq.squeeze(0))
        plt.savefig('demo_memory_flow.png', dpi=150, bbox_inches='tight')
        plt.close()
        print("Saved to demo_memory_flow.png")
    except Exception as e:
        print(f"Note: Visualization requires display (error: {e})")
    
    # Create training history plot
    print("\nGenerating training history plot...")
    history = {
        'train_loss': [2.5, 2.0, 1.5, 1.2, 1.0, 0.9, 0.85],
        'eval_loss': [2.4, 1.9, 1.4, 1.3, 1.1, 1.0, 0.95],
        'memory_stats': [
            {'layer_0_lt_active_count': i * 10} for i in range(7)
        ]
    }
    
    try:
        fig = plot_training_history(history)
        plt.savefig('demo_training_history.png', dpi=150, bbox_inches='tight')
        plt.close()
        print("Saved to demo_training_history.png")
    except Exception as e:
        print(f"Note: Visualization requires display (error: {e})")


def demo_monitor():
    """Demonstrate memory monitoring during training."""
    print("\n" + "=" * 60)
    print("DEMO 6: Memory Monitoring")
    print("=" * 60)
    
    config = LMCODEConfig(
        vocab_size=1000,
        hidden_size=64,
        num_layers=2,
        num_heads=4,
        short_term_memory_size=64,
        long_term_memory_slots=500
    )
    
    model = LMCODE(config)
    monitor = MemoryMonitor(model)
    
    print("\nSimulating training steps with monitoring...")
    for step in range(10):
        # Create dummy batch
        input_ids = torch.randint(0, 1000, (4, 20))
        labels = torch.randint(0, 1000, (4, 20))
        
        # Forward pass
        with torch.no_grad():
            outputs = model(input_ids, labels=labels, use_long_term_memory=True)
        
        # Record step
        monitor.record_step(step, outputs)
    
    # Get statistics
    print("\nMemory monitoring statistics:")
    stats = monitor.get_statistics()
    for key, val in stats.items():
        print(f"  {key}:")
        print(f"    Mean: {val['mean']:.4f}")
        print(f"    Std:  {val['std']:.4f}")
        print(f"    Latest: {val['latest']:.4f}")
    
    print("\nNote: Full visualization requires display environment")


def main():
    """Run all demos."""
    print("\n" + "=" * 60)
    print("LMCODE: Language Model with Memory CODE - Demo")
    print("=" * 60)
    
    # Demo 1: Basic usage
    model, config = demo_basic_usage()
    
    # Demo 2: Memory operations
    demo_memory_operations(model)
    
    # Demo 3: Training
    model, history = demo_training()
    
    # Demo 4: Memory analysis
    demo_memory_analysis(model)
    
    # Demo 5: Visualization
    demo_visualization(model)
    
    # Demo 6: Monitoring
    demo_monitor()
    
    print("\n" + "=" * 60)
    print("All demos completed successfully!")
    print("=" * 60)
    print("\nGenerated files:")
    print("  - demo_model.pt (trained model checkpoint)")
    print("  - demo_memory_report.json (memory analysis)")
    print("  - demo_memory_flow.png (memory flow visualization)")
    print("  - demo_training_history.png (training history)")


if __name__ == '__main__':
    main()