File size: 6,621 Bytes
1f39ae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script to verify training optimizations work correctly
"""

import torch
import os
import sys
from pathlib import Path

def test_mixed_precision():
    """Test mixed precision training setup"""
    print("Testing Mixed Precision Training...")
    
    try:
        from torch.cuda.amp import GradScaler, autocast
        
        # Test scaler creation
        scaler = GradScaler(enabled=True)
        print("βœ“ GradScaler created successfully")
        
        # Test autocast context
        with autocast():
            x = torch.randn(10, 10)
            y = torch.randn(10, 10)
            z = x @ y
        print("βœ“ Autocast context works")
        
        return True
    except Exception as e:
        print(f"βœ— Mixed precision test failed: {e}")
        return False

def test_optimized_dataset():
    """Test optimized dataset functionality"""
    print("\nTesting Optimized Dataset...")
    
    try:
        from morphological_dataset import MorphologicalDataset, build_vocabulary
        
        # Create dummy data files
        os.makedirs("test_data", exist_ok=True)
        
        with open("test_data/test.src", "w") as f:
            f.write("hello world\n")
            f.write("test sequence\n")
        
        with open("test_data/test.tgt", "w") as f:
            f.write("hola mundo\n")
            f.write("secuencia prueba\n")
        
        # Test vocabulary building
        src_vocab = build_vocabulary(["test_data/test.src"])
        tgt_vocab = build_vocabulary(["test_data/test.tgt"])
        print("βœ“ Vocabulary building works")
        
        # Test dataset creation
        dataset = MorphologicalDataset("test_data/test.src", "test_data/test.tgt", 
                                     src_vocab, tgt_vocab, max_length=10)
        print("βœ“ Dataset creation works")
        
        # Test data loading
        item = dataset[0]
        print(f"βœ“ Dataset item shape: {len(item)}")
        
        # Cleanup
        import shutil
        shutil.rmtree("test_data")
        
        return True
    except Exception as e:
        print(f"βœ— Dataset test failed: {e}")
        return False

def test_optimized_dataloader():
    """Test optimized DataLoader configuration"""
    print("\nTesting Optimized DataLoader...")
    
    try:
        from torch.utils.data import DataLoader
        from morphological_dataset import MorphologicalDataset, build_vocabulary, collate_fn
        
        # Create test dataset
        os.makedirs("test_data", exist_ok=True)
        
        with open("test_data/test.src", "w") as f:
            f.write("hello world\n")
            f.write("test sequence\n")
        
        with open("test_data/test.tgt", "w") as f:
            f.write("hola mundo\n")
            f.write("secuencia prueba\n")
        
        src_vocab = build_vocabulary(["test_data/test.src"])
        tgt_vocab = build_vocabulary(["test_data/test.tgt"])
        dataset = MorphologicalDataset("test_data/test.src", "test_data/test.tgt", 
                                     src_vocab, tgt_vocab, max_length=10)
        
        # Test optimized DataLoader
        dataloader = DataLoader(
            dataset,
            batch_size=2,
            shuffle=True,
            collate_fn=lambda batch: collate_fn(batch, src_vocab, tgt_vocab, 10),
            num_workers=0,  # Use 0 for testing
            pin_memory=False,  # Disable for testing
            persistent_workers=False,  # Disable for testing
            drop_last=True
        )
        
        # Test iteration
        for batch in dataloader:
            src, src_mask, tgt, tgt_mask = batch
            print(f"βœ“ Batch shapes - src: {src.shape}, tgt: {tgt.shape}")
            break
        
        # Cleanup
        import shutil
        shutil.rmtree("test_data")
        
        return True
    except Exception as e:
        print(f"βœ— DataLoader test failed: {e}")
        return False

def test_cuda_optimizations():
    """Test CUDA optimizations"""
    print("\nTesting CUDA Optimizations...")
    
    if not torch.cuda.is_available():
        print("⚠ CUDA not available, skipping CUDA tests")
        return True
    
    try:
        # Test CUDA optimizations
        torch.backends.cudnn.benchmark = True
        torch.backends.cudnn.deterministic = False
        print("βœ“ CUDA optimizations enabled")
        
        # Test non-blocking transfers
        x = torch.randn(100, 100)
        y = x.cuda(non_blocking=True)
        print("βœ“ Non-blocking CUDA transfer works")
        
        return True
    except Exception as e:
        print(f"βœ— CUDA test failed: {e}")
        return False

def test_model_creation():
    """Test model creation with optimizations"""
    print("\nTesting Model Creation...")
    
    try:
        from transformer import TagTransformer
        
        # Test model creation
        model = TagTransformer(
            src_vocab_size=1000,
            trg_vocab_size=1000,
            embed_dim=256,
            nb_heads=4,
            src_hid_size=1024,
            src_nb_layers=2,
            trg_hid_size=1024,
            trg_nb_layers=2,
            dropout_p=0.1,
            tie_trg_embed=True,
            label_smooth=0.1,
            nb_attr=5,
            src_c2i={},
            trg_c2i={},
            attr_c2i={}
        )
        
        print("βœ“ Model creation works")
        
        # Test parameter count
        param_count = model.count_nb_params()
        print(f"βœ“ Model has {param_count:,} parameters")
        
        return True
    except Exception as e:
        print(f"βœ— Model test failed: {e}")
        return False

def run_all_tests():
    """Run all optimization tests"""
    print("=== Testing Training Optimizations ===\n")
    
    tests = [
        test_mixed_precision,
        test_optimized_dataset,
        test_optimized_dataloader,
        test_cuda_optimizations,
        test_model_creation
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        try:
            if test():
                passed += 1
        except Exception as e:
            print(f"βœ— Test {test.__name__} failed with exception: {e}")
    
    print(f"\n=== Test Results ===")
    print(f"Passed: {passed}/{total}")
    
    if passed == total:
        print("πŸŽ‰ All tests passed! Optimizations are working correctly.")
        return True
    else:
        print("❌ Some tests failed. Check the errors above.")
        return False

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