File size: 13,307 Bytes
d187b57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import torch
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
import logging
from pathlib import Path
from torch.utils.data import DataLoader
import sys
import os
import wandb
from transformers import get_linear_schedule_with_warmup

# Add project root to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from model.training_config import TrainingConfig
from model.language_aware_transformer import LanguageAwareTransformer
from model.train import ToxicDataset
from transformers import XLMRobertaTokenizer

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def setup_plot_style():
    """Configure plot styling"""
    plt.style.use('seaborn-darkgrid')
    plt.rcParams['figure.figsize'] = (12, 12)
    plt.rcParams['font.size'] = 12

def setup_wandb():
    """Initialize wandb for validation tracking"""
    try:
        wandb.init(
            project="toxic-comment-classification",
            name=f"validation-analysis-{datetime.now().strftime('%Y%m%d-%H%M%S')}",
            config={
                "analysis_type": "validation_loss",
                "timestamp": datetime.now().strftime('%Y%m%d-%H%M%S')
            }
        )
        logger.info("Initialized wandb logging")
    except Exception as e:
        logger.error(f"Error initializing wandb: {str(e)}")
        raise

def load_model_and_data():
    """Load the model and validation data"""
    try:
        # Initialize config with training settings
        config = TrainingConfig(
            batch_size=16,
            num_workers=16,
            lr=2e-5,
            weight_decay=0.01,
            max_grad_norm=1.0,
            warmup_ratio=0.1,
            label_smoothing=0.01,

            mixed_precision="fp16",
            activation_checkpointing=True,
            epochs=1  # Number of validation epochs

        )
        
        # Load validation data
        logger.info("Loading validation and test data...")
        val_df = pd.read_csv("dataset/split/val.csv")
        test_df = pd.read_csv("dataset/split/test.csv")
        combined_df = pd.concat([val_df, test_df])
        tokenizer = XLMRobertaTokenizer.from_pretrained(config.model_name)
        combined_dataset = ToxicDataset(combined_df, tokenizer, config, mode='combined')
        

        # Create combined dataloader
        combined_loader = DataLoader(
            combined_dataset,
            batch_size=config.batch_size,
            shuffle=True,  # Enable shuffling
            num_workers=config.num_workers,
            pin_memory=True,
            drop_last=False  # Keep all samples
        )
        
        # Log dataloader config to wandb
        if wandb.run is not None:
            wandb.config.update({
                'shuffle': True,
                'drop_last': False,
                'total_validation_steps': len(combined_loader),
                'total_validation_samples': len(combined_dataset)
            })
        

        # Load model
        logger.info("Loading model...")
        model = LanguageAwareTransformer(
            num_labels=len(config.toxicity_labels),
            model_name=config.model_name
        )
        
        # Load latest checkpoint
        checkpoint_path = Path('weights/toxic_classifier_xlm-roberta-large/pytorch_model.bin')
        if checkpoint_path.exists():
            checkpoint = torch.load(checkpoint_path, map_location='cpu')
            model.load_state_dict(checkpoint)
            logger.info("Loaded model checkpoint")
        else:
            raise FileNotFoundError("No checkpoint found")
        
        # Move model to GPU if available
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        model = model.to(device)
        
        # Setup optimizer
        param_groups = config.get_param_groups(model)
        optimizer = torch.optim.AdamW(param_groups)
        
        # Setup scheduler
        total_steps = len(combined_loader) * config.epochs
        warmup_steps = int(total_steps * config.warmup_ratio)

        scheduler = get_linear_schedule_with_warmup(
            optimizer,
            num_warmup_steps=warmup_steps,
            num_training_steps=total_steps
        )
        
        # Initialize gradient scaler for mixed precision
        scaler = torch.cuda.amp.GradScaler(enabled=config.mixed_precision == "fp16")
        
        # Log model configuration to wandb
        if wandb.run is not None:
            wandb.config.update({
                'model_name': config.model_name,
                'batch_size': config.batch_size,
                'learning_rate': config.lr,
                'weight_decay': config.weight_decay,
                'max_grad_norm': config.max_grad_norm,
                'warmup_ratio': config.warmup_ratio,
                'label_smoothing': config.label_smoothing,
                'mixed_precision': config.mixed_precision,
                'num_workers': config.num_workers,
                'activation_checkpointing': config.activation_checkpointing,
                'validation_epochs': config.epochs
            })
        
        return model, combined_loader, device, optimizer, scheduler, scaler, config
        

    except Exception as e:
        logger.error(f"Error loading model and data: {str(e)}")
        raise

def collect_validation_losses(model, combined_loader, device, optimizer, scheduler, scaler, config):
    """Run validation and collect step losses across multiple epochs"""
    try:
        logger.warning("This is an analysis run on combined val+test data - model will not be saved or updated")
        # Ensure we're in eval mode and no gradients are computed
        model.eval()
        for param in model.parameters():
            param.requires_grad = False
            
        all_losses = []
        epoch_losses = []
        
        for epoch in range(config.epochs):
            logger.info(f"\nStarting validation epoch {epoch+1}/{config.epochs}")
            total_loss = 0
            num_batches = len(combined_loader)
            epoch_start_time = datetime.now()

            with torch.no_grad():  # Extra safety to ensure no gradients
                for step, batch in enumerate(combined_loader):
                    # Move batch to device
                    batch = {k: v.to(device) if isinstance(v, torch.Tensor) else v 
                            for k, v in batch.items()}
                    
                    # Forward pass with mixed precision
                    with torch.cuda.amp.autocast(enabled=config.mixed_precision != "no"):
                        outputs = model(**batch)
                        loss = outputs['loss'].item()
                    
                    total_loss += loss
                    
                    # Calculate running averages
                    avg_loss = total_loss / (step + 1)
                    
                    # Get learning rates
                    lrs = [group['lr'] for group in optimizer.param_groups]
                    
                    # Log to wandb
                    wandb.log({
                        'val/step_loss': loss,
                        'val/running_avg_loss': avg_loss,
                        'val/progress': (step + 1) / num_batches * 100,
                        'val/learning_rate': lrs[0],  # Base learning rate
                        'val/batch_size': config.batch_size,
                        'val/epoch': epoch + 1,
                        'val/global_step': epoch * num_batches + step
                    })
                    
                    # Log progress
                    if step % 10 == 0:
                        elapsed_time = datetime.now() - epoch_start_time
                        steps_per_sec = (step + 1) / elapsed_time.total_seconds()
                        remaining_steps = num_batches - (step + 1)
                        eta_seconds = remaining_steps / steps_per_sec if steps_per_sec > 0 else 0
                        
                        logger.info(
                            f"Epoch [{epoch+1}/{config.epochs}] "
                            f"Step [{step+1}/{num_batches}] "
                            f"Loss: {loss:.4f} "
                            f"Avg Loss: {avg_loss:.4f} "
                            f"LR: {lrs[0]:.2e} "
                            f"ETA: {int(eta_seconds)}s"
                        )
            
            # Calculate epoch statistics
            epoch_avg_loss = total_loss / num_batches
            epoch_losses.append({
                'epoch': epoch + 1,
                'avg_loss': epoch_avg_loss,
                'elapsed_time': (datetime.now() - epoch_start_time).total_seconds()
            })
            
            # Log epoch metrics to wandb
            wandb.log({
                'val/epoch_avg_loss': epoch_avg_loss,
                'val/epoch_number': epoch + 1,
                'val/epoch_time': epoch_losses[-1]['elapsed_time']
            })
            
            # Clear GPU memory after each epoch
            torch.cuda.empty_cache()
        
        return epoch_losses
        
    except Exception as e:
        logger.error(f"Error collecting validation losses: {str(e)}")
        raise

def plot_validation_losses(epoch_losses):
    """Plot validation epoch losses"""
    try:
        setup_plot_style()
        
        # Create figure
        fig, ax = plt.subplots()
        
        # Extract data
        epochs = [d['epoch'] for d in epoch_losses]
        losses = [d['avg_loss'] for d in epoch_losses]
        
        # Plot epoch losses
        ax.plot(epochs, losses, 'go-', label='Epoch Average Loss', linewidth=2, markersize=8)
        
        # Add trend line
        z = np.polyfit(epochs, losses, 1)
        p = np.poly1d(z)
        ax.plot(epochs, p(epochs), "r--", alpha=0.8, label='Loss Trend')
        
        # Customize plot
        ax.set_title('Validation Epoch Losses')
        ax.set_xlabel('Epoch')
        ax.set_ylabel('Average Loss')
        ax.legend()
        ax.grid(True, linestyle='--', alpha=0.7)
        
        # Adjust layout
        plt.tight_layout()
        
        # Create output directory if it doesn't exist
        output_dir = Path('analysis/plots')
        output_dir.mkdir(parents=True, exist_ok=True)
        
        # Save plot
        timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
        output_path = output_dir / f'validation_losses_{timestamp}.png'
        plt.savefig(output_path, dpi=300, bbox_inches='tight')
        logger.info(f"Plot saved to {output_path}")
        
        # Log plot to wandb
        wandb.log({
            "val/loss_plot": wandb.Image(str(output_path))
        })
        
        # Show plot
        plt.show()
        
    except Exception as e:
        logger.error(f"Error plotting validation losses: {str(e)}")
        raise

def calculate_loss_statistics(epoch_losses):
    """Calculate and print loss statistics"""
    try:
        losses = [d['avg_loss'] for d in epoch_losses]
        
        stats = {
            'Mean Loss': np.mean(losses),
            'Std Loss': np.std(losses),
            'Min Loss': np.min(losses),
            'Max Loss': np.max(losses),
            'Best Epoch': epoch_losses[np.argmin(losses)]['epoch']
        }
        
        # Log statistics to wandb
        wandb.log({
            'val/mean_loss': stats['Mean Loss'],
            'val/std_loss': stats['Std Loss'],
            'val/min_loss': stats['Min Loss'],
            'val/max_loss': stats['Max Loss'],
            'val/best_epoch': stats['Best Epoch']
        })
        
        # Print statistics
        print("\nValidation Loss Statistics:")
        for metric_name, value in stats.items():
            if metric_name == 'Best Epoch':
                print(f"{metric_name}: {int(value)}")
            else:
                print(f"{metric_name}: {value:.4f}")
        
        return stats
        
    except Exception as e:
        logger.error(f"Error calculating statistics: {str(e)}")
        raise

def main():
    try:
        # Initialize wandb
        setup_wandb()
        
        # Load model and data
        logger.info("Loading model and data...")
        model, combined_loader, device, optimizer, scheduler, scaler, config = load_model_and_data()
        

        # Collect validation losses
        logger.info("Collecting validation losses...")
        epoch_losses = collect_validation_losses(
            model, combined_loader, device, optimizer, scheduler, scaler, config
        )
        

        # Plot losses
        logger.info("Plotting validation losses...")
        plot_validation_losses(epoch_losses)
        
        # Calculate and print statistics
        logger.info("Calculating statistics...")
        calculate_loss_statistics(epoch_losses)
        
    except Exception as e:
        logger.error(f"Error in main: {str(e)}")
        raise
    finally:
        # Clean up
        torch.cuda.empty_cache()
        # Finish wandb run
        wandb.finish()

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        logger.error(f"Script failed: {str(e)}")
        raise