File size: 22,505 Bytes
388fd6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
"""

Example demonstrating Gamma SSM's ability to learn copy and reverse tasks.



Copy Task: Given [x₁, x₂, ..., xₗ, ∅, ∅, ..., ∅], predict [∅, ∅, ..., ∅, x₁, x₂, ..., xₗ]

This tests the model's ability to hold information in memory and recall it after a delay.



Reverse Task: Given [x₁, x₂, ..., xₗ], predict [xₗ, xₗ₋₁, ..., x₁]

This tests the model's ability to process sequences bidirectionally.



These are classical synthetic benchmarks for evaluating sequence models.

"""

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
import numpy as np
from pathlib import Path
import argparse
from typing import Tuple, Dict, List
import sys

# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))

from gamma_space_model.modules.block import GammaSingleBlock


# ============================================================================
# PHASE 1: TASK DATA GENERATION
# ============================================================================

def generate_copy_task(

    seq_len: int,

    vocab_size: int,

    batch_size: int,

    device: torch.device,

) -> Tuple[torch.Tensor, torch.Tensor]:
    """

    Generate copy task data.

    

    Input:  [token₁, token₂, ..., tokenₗ, 0, 0, ..., 0]  (length: 2*seq_len)

    Target: [0,      0,      ..., 0,       token₁, token₂, ..., tokenₗ]  (length: 2*seq_len)

    

    Args:

        seq_len: Length of the token sequence

        vocab_size: Number of possible token values (excluding 0 for padding)

        batch_size: Batch size

        device: Device to create tensors on

    

    Returns:

        inputs: (batch_size, 2*seq_len) tensor of input token indices

        targets: (batch_size, 2*seq_len) tensor of target token indices

    """
    # Generate random tokens (1 to vocab_size-1; 0 is reserved for padding)
    tokens = torch.randint(1, vocab_size, (batch_size, seq_len), device=device)
    
    # Create inputs: [tokens, pad]
    padding = torch.zeros((batch_size, seq_len), dtype=torch.long, device=device)
    inputs = torch.cat([tokens, padding], dim=1)
    
    # Create targets: [pad, tokens]
    targets = torch.cat([padding, tokens], dim=1)
    
    return inputs, targets


def generate_reverse_task(

    seq_len: int,

    vocab_size: int,

    batch_size: int,

    device: torch.device,

) -> Tuple[torch.Tensor, torch.Tensor]:
    """

    Generate reverse task data.

    

    Input:  [token₁, token₂, ..., tokenₗ]

    Target: [tokenₗ, tokenₗ₋₁, ..., token₁]

    

    Args:

        seq_len: Length of the token sequence

        vocab_size: Number of possible token values (1 to vocab_size-1)

        batch_size: Batch size

        device: Device to create tensors on

    

    Returns:

        inputs: (batch_size, seq_len) tensor of input token indices

        targets: (batch_size, seq_len) tensor of target token indices

    """
    # Generate random tokens (1 to vocab_size-1)
    inputs = torch.randint(1, vocab_size, (batch_size, seq_len), device=device)
    
    # Create targets by reversing along sequence dimension
    targets = torch.flip(inputs, dims=[1])
    
    return inputs, targets


class CopyReverseDataset(Dataset):
    """Dataset for copy/reverse tasks."""
    
    def __init__(

        self,

        task_type: str,

        seq_len: int,

        vocab_size: int,

        num_samples: int,

        device: torch.device,

    ):
        """

        Args:

            task_type: 'copy' or 'reverse'

            seq_len: Sequence length

            vocab_size: Number of vocabulary tokens

            num_samples: Number of samples to generate

            device: Device to generate data on

        """
        self.task_type = task_type
        self.seq_len = seq_len
        self.vocab_size = vocab_size
        self.device = device
        
        # Pre-generate all samples
        self.inputs = []
        self.targets = []
        
        if task_type == 'copy':
            gen_fn = generate_copy_task
        elif task_type == 'reverse':
            gen_fn = generate_reverse_task
        else:
            raise ValueError(f"Unknown task type: {task_type}")
        
        # Generate samples in batches
        batch_size = min(num_samples, 256)
        for i in range(0, num_samples, batch_size):
            current_batch_size = min(batch_size, num_samples - i)
            inputs, targets = gen_fn(seq_len, vocab_size, current_batch_size, device)
            self.inputs.append(inputs)
            self.targets.append(targets)
        
        self.inputs = torch.cat(self.inputs, dim=0)
        self.targets = torch.cat(self.targets, dim=0)
    
    def __len__(self):
        return len(self.inputs)
    
    def __getitem__(self, idx):
        return self.inputs[idx], self.targets[idx]


# ============================================================================
# PHASE 2: MODEL ARCHITECTURE
# ============================================================================

class CopyReverseModel(nn.Module):
    """

    Model for copy/reverse tasks.

    

    Architecture:

        input (token indices)

        → embedding (one-hot)

        → GammaSingleBlock (SSM)

        → MLP projection

        → output logits (vocab_size)

    """
    
    def __init__(

        self,

        vocab_size: int,

        d_model: int = 32,

        hidden_dim: int = 128,

        prenorm: bool = True,

        dropout: float = 0.0,

    ):
        """

        Args:

            vocab_size: Number of vocabulary tokens

            d_model: Model dimension (embedding + SSM input dim)

            hidden_dim: SSM hidden state dimension

            prenorm: Whether to use prenorm in SSM block

            dropout: Dropout rate

        """
        super().__init__()
        
        self.vocab_size = vocab_size
        self.d_model = d_model
        self.hidden_dim = hidden_dim
        
        # Embedding layer converts token indices to d_model dimensional vectors
        self.embedding = nn.Embedding(vocab_size, d_model)
        
        # SSM block for sequence processing
        self.ssm_block = GammaSingleBlock(
            d_model=d_model,
            hidden_dim=hidden_dim,
            delta_t=0.005,
            prenorm=prenorm,
            dropout=dropout,
        )
        
        # Output MLP projection to vocab logits
        self.output_proj = nn.Sequential(
            nn.Linear(d_model, d_model),
            nn.ReLU(),
            nn.Dropout(dropout),
            nn.Linear(d_model, vocab_size),
        )
    
    def forward(

        self,

        token_indices: torch.Tensor,

        state: torch.Tensor = None,

        mask: torch.Tensor = None,

    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """

        Forward pass.

        

        Args:

            token_indices: (batch, seq_len) tensor of token indices

            state: Optional initial state for SSM

            mask: Optional mask for padding

        

        Returns:

            logits: (batch, seq_len, vocab_size) output logits

            final_state: (batch, hidden_dim) final SSM state

        """
        # Embed tokens
        embedded = self.embedding(token_indices)  # (batch, seq_len, d_model)
        
        # Process through SSM block
        ssm_output, final_state = self.ssm_block(embedded, state=state, mask=mask)
        
        # Project to vocabulary
        logits = self.output_proj(ssm_output)  # (batch, seq_len, vocab_size)
        
        return logits, final_state


# ============================================================================
# PHASE 3: TRAINING & EVALUATION
# ============================================================================

def train_on_task(

    model: nn.Module,

    train_loader: DataLoader,

    val_loader: DataLoader,

    task_type: str,

    num_epochs: int = 50,

    learning_rate: float = 0.001,

    device: torch.device = torch.device('cpu'),

    verbose: bool = True,

) -> Dict[str, List[float]]:
    """

    Train model on copy or reverse task.

    

    Args:

        model: CopyReverseModel instance

        train_loader: Training data loader

        val_loader: Validation data loader

        task_type: 'copy' or 'reverse' (for logging)

        num_epochs: Number of training epochs

        learning_rate: Learning rate for optimizer

        device: Device to train on

        verbose: Whether to print metrics

    

    Returns:

        Dictionary with 'train_loss', 'val_loss', 'train_acc', 'val_acc' lists

    """
    model = model.to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
    criterion = nn.CrossEntropyLoss()
    
    history = {
        'train_loss': [],
        'val_loss': [],
        'train_acc': [],
        'val_acc': [],
    }
    
    for epoch in range(num_epochs):
        # Training phase
        model.train()
        train_loss = 0.0
        train_correct = 0
        train_total = 0
        
        for batch_inputs, batch_targets in train_loader:
            batch_inputs = batch_inputs.to(device)
            batch_targets = batch_targets.to(device)
            
            # Forward pass
            logits, _ = model(batch_inputs)
            
            # Compute loss (flatten for CrossEntropyLoss)
            batch_size, seq_len, vocab_size = logits.shape
            loss = criterion(
                logits.reshape(-1, vocab_size),
                batch_targets.reshape(-1),
            )
            
            # Backward pass
            optimizer.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
            optimizer.step()
            
            # Metrics
            train_loss += loss.item()
            predictions = logits.argmax(dim=-1)
            train_correct += (predictions == batch_targets).sum().item()
            train_total += batch_targets.numel()
        
        train_loss /= len(train_loader)
        train_acc = 100 * train_correct / train_total
        
        # Validation phase
        model.eval()
        val_loss = 0.0
        val_correct = 0
        val_total = 0
        
        with torch.no_grad():
            for batch_inputs, batch_targets in val_loader:
                batch_inputs = batch_inputs.to(device)
                batch_targets = batch_targets.to(device)
                
                logits, _ = model(batch_inputs)
                
                batch_size, seq_len, vocab_size = logits.shape
                loss = criterion(
                    logits.reshape(-1, vocab_size),
                    batch_targets.reshape(-1),
                )
                
                val_loss += loss.item()
                predictions = logits.argmax(dim=-1)
                val_correct += (predictions == batch_targets).sum().item()
                val_total += batch_targets.numel()
        
        val_loss /= len(val_loader)
        val_acc = 100 * val_correct / val_total
        
        history['train_loss'].append(train_loss)
        history['val_loss'].append(val_loss)
        history['train_acc'].append(train_acc)
        history['val_acc'].append(val_acc)
        
        if verbose and (epoch + 1) % 10 == 0:
            print(
                f"[{task_type.upper()}] Epoch {epoch+1:3d}/{num_epochs} | "
                f"Train Loss: {train_loss:.4f} | Train Acc: {train_acc:6.2f}% | "
                f"Val Loss: {val_loss:.4f} | Val Acc: {val_acc:6.2f}%"
            )
    
    return history


def evaluate_on_task(

    model: nn.Module,

    test_loader: DataLoader,

    task_type: str,

    device: torch.device = torch.device('cpu'),

) -> Dict[str, float]:
    """

    Evaluate model on test set.

    

    Args:

        model: CopyReverseModel instance

        test_loader: Test data loader

        task_type: 'copy' or 'reverse' (for logging)

        device: Device to evaluate on

    

    Returns:

        Dictionary with 'loss', 'accuracy', 'per_position_acc' metrics

    """
    model = model.to(device)
    model.eval()
    
    criterion = nn.CrossEntropyLoss()
    total_loss = 0.0
    total_correct = 0
    total_tokens = 0
    
    # Track per-position accuracy
    all_predictions = []
    all_targets = []
    
    with torch.no_grad():
        for batch_inputs, batch_targets in test_loader:
            batch_inputs = batch_inputs.to(device)
            batch_targets = batch_targets.to(device)
            
            logits, _ = model(batch_inputs)
            
            batch_size, seq_len, vocab_size = logits.shape
            loss = criterion(
                logits.reshape(-1, vocab_size),
                batch_targets.reshape(-1),
            )
            
            total_loss += loss.item()
            predictions = logits.argmax(dim=-1)
            total_correct += (predictions == batch_targets).sum().item()
            total_tokens += batch_targets.numel()
            
            all_predictions.append(predictions.cpu())
            all_targets.append(batch_targets.cpu())
    
    avg_loss = total_loss / len(test_loader)
    accuracy = 100 * total_correct / total_tokens
    
    # Compute per-position accuracy
    all_predictions = torch.cat(all_predictions, dim=0)
    all_targets = torch.cat(all_targets, dim=0)
    
    per_pos_correct = (all_predictions == all_targets).float().mean(dim=0)
    
    return {
        'loss': avg_loss,
        'accuracy': accuracy,
        'per_position_acc': per_pos_correct.numpy(),
    }


def visualize_predictions(

    model: nn.Module,

    task_type: str,

    seq_len: int,

    vocab_size: int,

    num_examples: int = 3,

    device: torch.device = torch.device('cpu'),

):
    """

    Visualize model predictions on sample data.

    

    Args:

        model: CopyReverseModel instance

        task_type: 'copy' or 'reverse'

        seq_len: Sequence length

        vocab_size: Vocabulary size

        num_examples: Number of examples to show

        device: Device to use

    """
    model.eval()
    
    if task_type == 'copy':
        inputs, targets = generate_copy_task(seq_len, vocab_size, num_examples, device)
    else:
        inputs, targets = generate_reverse_task(seq_len, vocab_size, num_examples, device)
    
    with torch.no_grad():
        logits, _ = model(inputs)
        predictions = logits.argmax(dim=-1)
    
    print(f"\n{'='*80}")
    print(f"Sample Predictions for {task_type.upper()} Task (seq_len={seq_len}, vocab_size={vocab_size})")
    print(f"{'='*80}")
    
    for idx in range(num_examples):
        print(f"\nExample {idx + 1}:")
        print(f"  Input:      {inputs[idx].cpu().tolist()}")
        print(f"  Target:     {targets[idx].cpu().tolist()}")
        print(f"  Predicted:  {predictions[idx].cpu().tolist()}")
        
        # Compute accuracy for this example
        correct = (predictions[idx] == targets[idx]).sum().item()
        acc = 100 * correct / len(targets[idx])
        print(f"  Accuracy:   {acc:.2f}%")


# ============================================================================
# PHASE 4: MAIN ENTRY POINT
# ============================================================================

def main(args):
    """Main training and evaluation script."""
    
    # Setup device
    device = torch.device(args.device if torch.cuda.is_available() or args.device == 'cpu' else 'cpu')
    print(f"Using device: {device}")
    
    # Create datasets
    print(f"\nCreating datasets...")
    print(f"  Sequence length: {args.seq_len}")
    print(f"  Vocabulary size: {args.vocab_size}")
    print(f"  Batch size: {args.batch_size}")
    
    # Copy task
    print(f"\n{'='*80}")
    print(f"COPY TASK")
    print(f"{'='*80}")
    
    copy_train_ds = CopyReverseDataset(
        'copy', args.seq_len, args.vocab_size, args.train_samples, device
    )
    copy_val_ds = CopyReverseDataset(
        'copy', args.seq_len, args.vocab_size, args.val_samples, device
    )
    copy_test_ds = CopyReverseDataset(
        'copy', args.seq_len, args.vocab_size, args.test_samples, device
    )
    
    copy_train_loader = DataLoader(copy_train_ds, batch_size=args.batch_size, shuffle=True)
    copy_val_loader = DataLoader(copy_val_ds, batch_size=args.batch_size, shuffle=False)
    copy_test_loader = DataLoader(copy_test_ds, batch_size=args.batch_size, shuffle=False)
    
    # Reverse task
    print(f"\n{'='*80}")
    print(f"REVERSE TASK")
    print(f"{'='*80}")
    
    rev_train_ds = CopyReverseDataset(
        'reverse', args.seq_len, args.vocab_size, args.train_samples, device
    )
    rev_val_ds = CopyReverseDataset(
        'reverse', args.seq_len, args.vocab_size, args.val_samples, device
    )
    rev_test_ds = CopyReverseDataset(
        'reverse', args.seq_len, args.vocab_size, args.test_samples, device
    )
    
    rev_train_loader = DataLoader(rev_train_ds, batch_size=args.batch_size, shuffle=True)
    rev_val_loader = DataLoader(rev_val_ds, batch_size=args.batch_size, shuffle=False)
    rev_test_loader = DataLoader(rev_test_ds, batch_size=args.batch_size, shuffle=False)
    
    # Train copy model
    print(f"\nTraining COPY task model...")
    copy_model = CopyReverseModel(
        vocab_size=args.vocab_size,
        d_model=args.d_model,
        hidden_dim=args.hidden_dim,
        prenorm=args.prenorm,
        dropout=args.dropout,
    )
    copy_history = train_on_task(
        copy_model,
        copy_train_loader,
        copy_val_loader,
        'copy',
        num_epochs=args.num_epochs,
        learning_rate=args.lr,
        device=device,
        verbose=True,
    )
    
    # Train reverse model
    print(f"\n\nTraining REVERSE task model...")
    reverse_model = CopyReverseModel(
        vocab_size=args.vocab_size,
        d_model=args.d_model,
        hidden_dim=args.hidden_dim,
        prenorm=args.prenorm,
        dropout=args.dropout,
    )
    rev_history = train_on_task(
        reverse_model,
        rev_train_loader,
        rev_val_loader,
        'reverse',
        num_epochs=args.num_epochs,
        learning_rate=args.lr,
        device=device,
        verbose=True,
    )
    
    # Evaluate on test sets
    print(f"\n\n{'='*80}")
    print(f"EVALUATION ON TEST SET")
    print(f"{'='*80}")
    
    copy_eval = evaluate_on_task(copy_model, copy_test_loader, 'copy', device)
    print(f"\nCOPY Task Test Results:")
    print(f"  Loss:     {copy_eval['loss']:.4f}")
    print(f"  Accuracy: {copy_eval['accuracy']:.2f}%")
    
    rev_eval = evaluate_on_task(reverse_model, rev_test_loader, 'reverse', device)
    print(f"\nREVERSE Task Test Results:")
    print(f"  Loss:     {rev_eval['loss']:.4f}")
    print(f"  Accuracy: {rev_eval['accuracy']:.2f}%")
    
    # Print comparison
    print(f"\n{'='*80}")
    print(f"COMPARISON")
    print(f"{'='*80}")
    print(f"Copy accuracy is {'higher' if copy_eval['accuracy'] > rev_eval['accuracy'] else 'lower'} than reverse")
    print(f"Difference: {abs(copy_eval['accuracy'] - rev_eval['accuracy']):.2f} percentage points")
    
    # Visualize predictions
    if args.visualize:
        visualize_predictions(copy_model, 'copy', args.seq_len, args.vocab_size, device=device)
        visualize_predictions(reverse_model, 'reverse', args.seq_len, args.vocab_size, device=device)
    
    print(f"\n{'='*80}")
    print(f"Training complete!")
    print(f"{'='*80}\n")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Test Gamma SSM on copy and reverse synthetic tasks.'
    )
    
    # Task parameters
    parser.add_argument(
        '--seq-len',
        type=int,
        default=20,
        help='Sequence length for tasks (default: 20)',
    )
    parser.add_argument(
        '--vocab-size',
        type=int,
        default=8,
        help='Vocabulary size (including 0 for padding, default: 8)',
    )
    
    # Dataset parameters
    parser.add_argument(
        '--train-samples',
        type=int,
        default=500,
        help='Number of training samples (default: 500)',
    )
    parser.add_argument(
        '--val-samples',
        type=int,
        default=100,
        help='Number of validation samples (default: 100)',
    )
    parser.add_argument(
        '--test-samples',
        type=int,
        default=100,
        help='Number of test samples (default: 100)',
    )
    parser.add_argument(
        '--batch-size',
        type=int,
        default=32,
        help='Batch size (default: 32)',
    )
    
    # Model parameters
    parser.add_argument(
        '--d-model',
        type=int,
        default=32,
        help='Model dimension (default: 32)',
    )
    parser.add_argument(
        '--hidden-dim',
        type=int,
        default=128,
        help='SSM hidden dimension (default: 128)',
    )
    parser.add_argument(
        '--prenorm',
        type=bool,
        default=True,
        help='Use prenorm in SSM block (default: True)',
    )
    parser.add_argument(
        '--dropout',
        type=float,
        default=0.0,
        help='Dropout rate (default: 0.0)',
    )
    
    # Training parameters
    parser.add_argument(
        '--num-epochs',
        type=int,
        default=500,
        help='Number of training epochs (default: 500)',
    )
    parser.add_argument(
        '--lr',
        type=float,
        default=0.001,
        help='Learning rate (default: 0.001)',
    )
    
    # Other parameters
    parser.add_argument(
        '--device',
        type=str,
        default='cuda',
        choices=['cpu', 'cuda'],
        help='Device to use (default: cuda)',
    )
    parser.add_argument(
        '--visualize',
        action='store_true',
        help='Visualize predictions on sample data',
    )
    
    args = parser.parse_args()
    main(args)