File size: 9,525 Bytes
18a82fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import numpy as np
import random
import os
from pathlib import Path
import json
import yaml
from typing import Dict, Any, Optional, List
import hashlib
import shutil
from datetime import datetime


def set_seed(seed: int = 42):
    """
    Set random seed for reproducibility

    Args:
        seed: Random seed value
    """
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    os.environ['PYTHONHASHSEED'] = str(seed)


def get_device(gpu_id: Optional[int] = None) -> torch.device:
    """
    Get torch device

    Args:
        gpu_id: Specific GPU ID to use

    Returns:
        torch.device
    """
    if torch.cuda.is_available():
        if gpu_id is not None:
            device = torch.device(f'cuda:{gpu_id}')
        else:
            device = torch.device('cuda')
    else:
        device = torch.device('cpu')

    return device


def count_parameters(model: torch.nn.Module) -> Dict[str, int]:
    """
    Count model parameters

    Args:
        model: PyTorch model

    Returns:
        Dictionary with parameter counts
    """
    total_params = sum(p.numel() for p in model.parameters())
    trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    non_trainable_params = total_params - trainable_params

    return {
        'total': total_params,
        'trainable': trainable_params,
        'non_trainable': non_trainable_params,
        'total_mb': total_params * 4 / 1024 / 1024,  # Assuming float32
        'trainable_mb': trainable_params * 4 / 1024 / 1024
    }


def save_config(config: Dict[str, Any], save_path: Path, format: str = 'json'):
    """
    Save configuration to file

    Args:
        config: Configuration dictionary
        save_path: Path to save file
        format: File format ('json' or 'yaml')
    """
    save_path = Path(save_path)
    save_path.parent.mkdir(parents=True, exist_ok=True)

    if format == 'json':
        with open(save_path, 'w') as f:
            json.dump(config, f, indent=2)
    elif format == 'yaml':
        with open(save_path, 'w') as f:
            yaml.dump(config, f, default_flow_style=False)
    else:
        raise ValueError(f"Unknown format: {format}")


def load_config(config_path: Path) -> Dict[str, Any]:
    """
    Load configuration from file

    Args:
        config_path: Path to config file

    Returns:
        Configuration dictionary
    """
    config_path = Path(config_path)

    if config_path.suffix == '.json':
        with open(config_path, 'r') as f:
            config = json.load(f)
    elif config_path.suffix in ['.yaml', '.yml']:
        with open(config_path, 'r') as f:
            config = yaml.safe_load(f)
    else:
        raise ValueError(f"Unknown config format: {config_path.suffix}")

    return config


def merge_configs(base_config: Dict, update_config: Dict) -> Dict:
    """
    Recursively merge two configuration dictionaries

    Args:
        base_config: Base configuration
        update_config: Configuration with updates

    Returns:
        Merged configuration
    """
    merged = base_config.copy()

    for key, value in update_config.items():
        if key in merged and isinstance(merged[key], dict) and isinstance(value, dict):
            merged[key] = merge_configs(merged[key], value)
        else:
            merged[key] = value

    return merged


def get_timestamp() -> str:
    """Get current timestamp string"""
    return datetime.now().strftime('%Y%m%d_%H%M%S')


def get_experiment_name(
        model_name: str,
        dataset_name: str = 'decade',
        timestamp: bool = True
) -> str:
    """
    Generate experiment name

    Args:
        model_name: Name of the model
        dataset_name: Name of the dataset
        timestamp: Whether to include timestamp

    Returns:
        Experiment name
    """
    name_parts = [model_name, dataset_name]

    if timestamp:
        name_parts.append(get_timestamp())

    return '_'.join(name_parts)


def compute_file_hash(file_path: Path, hash_algo: str = 'md5') -> str:
    """
    Compute hash of a file

    Args:
        file_path: Path to file
        hash_algo: Hash algorithm to use

    Returns:
        Hex digest of file hash
    """
    hash_func = getattr(hashlib, hash_algo)()

    with open(file_path, 'rb') as f:
        for chunk in iter(lambda: f.read(4096), b''):
            hash_func.update(chunk)

    return hash_func.hexdigest()


def create_experiment_structure(base_dir: Path, experiment_name: str) -> Dict[str, Path]:
    """
    Create directory structure for an experiment

    Args:
        base_dir: Base directory for experiments
        experiment_name: Name of the experiment

    Returns:
        Dictionary mapping directory names to paths
    """
    exp_dir = base_dir / experiment_name

    dirs = {
        'root': exp_dir,
        'checkpoints': exp_dir / 'checkpoints',
        'logs': exp_dir / 'logs',
        'visualizations': exp_dir / 'visualizations',
        'predictions': exp_dir / 'predictions',
        'configs': exp_dir / 'configs'
    }

    for dir_path in dirs.values():
        dir_path.mkdir(parents=True, exist_ok=True)

    return dirs


def backup_code(src_dir: Path, backup_dir: Path, extensions: List[str] = None):
    """
    Backup source code to experiment directory

    Args:
        src_dir: Source directory
        backup_dir: Backup destination
        extensions: List of file extensions to backup
    """
    if extensions is None:
        extensions = ['.py', '.yaml', '.yml', '.json', '.txt', '.md']

    backup_dir = Path(backup_dir)
    backup_dir.mkdir(parents=True, exist_ok=True)

    for file_path in src_dir.rglob('*'):
        if file_path.is_file() and file_path.suffix in extensions:
            relative_path = file_path.relative_to(src_dir)
            backup_path = backup_dir / relative_path
            backup_path.parent.mkdir(parents=True, exist_ok=True)
            shutil.copy2(file_path, backup_path)


def format_time(seconds: float) -> str:
    """
    Format time in seconds to human-readable string

    Args:
        seconds: Time in seconds

    Returns:
        Formatted time string
    """
    hours = int(seconds // 3600)
    minutes = int((seconds % 3600) // 60)
    seconds = int(seconds % 60)

    if hours > 0:
        return f"{hours}h {minutes}m {seconds}s"
    elif minutes > 0:
        return f"{minutes}m {seconds}s"
    else:
        return f"{seconds}s"


def get_gpu_memory_usage() -> Dict[str, float]:
    """
    Get GPU memory usage

    Returns:
        Dictionary with memory usage in MB
    """
    if not torch.cuda.is_available():
        return {'allocated': 0, 'reserved': 0}

    return {
        'allocated': torch.cuda.memory_allocated() / 1024 / 1024,
        'reserved': torch.cuda.memory_reserved() / 1024 / 1024
    }


def clean_checkpoint(checkpoint_path: Path, keep_keys: List[str] = None):
    """
    Clean checkpoint file by keeping only specified keys

    Args:
        checkpoint_path: Path to checkpoint
        keep_keys: Keys to keep (default: model_state_dict only)
    """
    if keep_keys is None:
        keep_keys = ['model_state_dict']

    checkpoint = torch.load(checkpoint_path, map_location='cpu')
    cleaned_checkpoint = {k: v for k, v in checkpoint.items() if k in keep_keys}

    # Save cleaned checkpoint
    output_path = checkpoint_path.parent / f"{checkpoint_path.stem}_cleaned.pth"
    torch.save(cleaned_checkpoint, output_path)

    # Print size reduction
    original_size = checkpoint_path.stat().st_size / 1024 / 1024
    new_size = output_path.stat().st_size / 1024 / 1024
    print(f"Cleaned checkpoint: {original_size:.1f}MB → {new_size:.1f}MB")

    return output_path


class AverageMeter:
    """Computes and stores the average and current value"""

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count


class EarlyStopping:
    """Early stopping to stop training when validation loss doesn't improve"""

    def __init__(self, patience=7, verbose=False, delta=0):
        self.patience = patience
        self.verbose = verbose
        self.counter = 0
        self.best_score = None
        self.early_stop = False
        self.val_loss_min = np.Inf
        self.delta = delta

    def __call__(self, val_loss, model=None):
        score = -val_loss

        if self.best_score is None:
            self.best_score = score
            self.save_checkpoint(val_loss, model)
        elif score < self.best_score + self.delta:
            self.counter += 1
            if self.verbose:
                print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
            if self.counter >= self.patience:
                self.early_stop = True
        else:
            self.best_score = score
            self.save_checkpoint(val_loss, model)
            self.counter = 0

    def save_checkpoint(self, val_loss, model):
        """Saves model when validation loss decrease"""
        if self.verbose:
            print(f'Validation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f})')
        self.val_loss_min = val_loss