File size: 14,260 Bytes
54c5666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Advanced Evaluation and Benchmarking Utilities

For comprehensive model assessment and comparison

"""

import os
import json
import time
import math
import torch
import torch.nn.functional as F
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass
import numpy as np

try:
    from rouge_score import rouge_scorer
    ROUGE_AVAILABLE = True
except ImportError:
    ROUGE_AVAILABLE = False

try:
    import sacrebleu
    BLEU_AVAILABLE = True
except ImportError:
    BLEU_AVAILABLE = False


@dataclass
class EvaluationConfig:
    """Configuration for evaluation"""
    max_eval_samples: int = 1000
    batch_size: int = 8
    max_new_tokens: int = 512
    temperature: float = 0.7
    top_k: int = 50
    top_p: float = 0.9
    do_sample: bool = True
    num_beams: int = 1
    repetition_penalty: float = 1.0
    length_penalty: float = 1.0


class PerplexityEvaluator:
    """Evaluate model perplexity on various datasets"""
    
    def __init__(self, model, tokenizer, device):
        self.model = model
        self.tokenizer = tokenizer
        self.device = device
    
    @torch.no_grad()
    def evaluate_perplexity(self, texts: List[str], max_length: int = 2048) -> Dict[str, float]:
        """Calculate perplexity on a list of texts"""
        self.model.eval()
        
        total_loss = 0.0
        total_tokens = 0
        
        for text in texts:
            tokens = self.tokenizer.encode(text)
            if len(tokens) < 2:
                continue
                
            # Split into chunks if too long
            chunks = [tokens[i:i+max_length] for i in range(0, len(tokens), max_length)]
            
            for chunk in chunks:
                if len(chunk) < 2:
                    continue
                    
                input_ids = torch.tensor([chunk[:-1]], device=self.device)
                labels = torch.tensor([chunk[1:]], device=self.device)
                
                outputs = self.model(input_ids=input_ids, labels=labels)
                loss = outputs['loss']
                
                total_loss += loss.item() * len(chunk[1:])
                total_tokens += len(chunk[1:])
        
        if total_tokens == 0:
            return {'perplexity': float('inf'), 'loss': float('inf')}
        
        avg_loss = total_loss / total_tokens
        perplexity = math.exp(min(avg_loss, 20))  # Cap for numerical stability
        
        return {
            'perplexity': perplexity,
            'loss': avg_loss,
            'total_tokens': total_tokens
        }


class GenerationEvaluator:
    """Evaluate text generation quality"""
    
    def __init__(self, model, tokenizer, device):
        self.model = model
        self.tokenizer = tokenizer
        self.device = device
    
    @torch.no_grad()
    def generate_text(

        self, 

        prompt: str, 

        config: EvaluationConfig

    ) -> str:
        """Generate text from a prompt"""
        self.model.eval()
        
        # Encode prompt
        input_ids = torch.tensor([self.tokenizer.encode(prompt)], device=self.device)
        
        # Generate
        generated = input_ids.clone()
        
        for _ in range(config.max_new_tokens):
            # Forward pass
            outputs = self.model(input_ids=generated, use_cache=False)
            logits = outputs['logits']
            
            # Get next token logits
            next_token_logits = logits[0, -1, :] / config.temperature
            
            # Apply top-k filtering
            if config.top_k > 0:
                indices_to_remove = next_token_logits < torch.topk(next_token_logits, config.top_k)[0][..., -1, None]
                next_token_logits[indices_to_remove] = float('-inf')
            
            # Apply top-p filtering
            if config.top_p < 1.0:
                sorted_logits, sorted_indices = torch.sort(next_token_logits, descending=True)
                cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
                
                sorted_indices_to_remove = cumulative_probs > config.top_p
                sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].clone()
                sorted_indices_to_remove[0] = 0
                
                indices_to_remove = sorted_indices[sorted_indices_to_remove]
                next_token_logits[indices_to_remove] = float('-inf')
            
            # Sample next token
            if config.do_sample:
                probs = F.softmax(next_token_logits, dim=-1)
                next_token = torch.multinomial(probs, num_samples=1)
            else:
                next_token = torch.argmax(next_token_logits, dim=-1, keepdim=True)
            
            # Append to sequence
            generated = torch.cat([generated, next_token.unsqueeze(0)], dim=-1)
            
            # Stop if we hit EOS or max length
            if generated.size(1) >= input_ids.size(1) + config.max_new_tokens:
                break
        
        # Decode generated text
        generated_text = self.tokenizer.decode(generated[0].cpu().tolist())
        
        # Extract only the new part
        prompt_length = len(prompt)
        return generated_text[prompt_length:]
    
    def evaluate_generation_quality(

        self, 

        prompts: List[str], 

        references: Optional[List[str]] = None,

        config: EvaluationConfig = None

    ) -> Dict[str, Any]:
        """Evaluate generation quality with various metrics"""
        if config is None:
            config = EvaluationConfig()
        
        results = {
            'generations': [],
            'metrics': {}
        }
        
        # Generate responses
        for prompt in prompts:
            generation = self.generate_text(prompt, config)
            results['generations'].append({
                'prompt': prompt,
                'generation': generation
            })
        
        # Calculate metrics if references provided
        if references and len(references) == len(prompts):
            generations = [r['generation'] for r in results['generations']]
            
            # BLEU score
            if BLEU_AVAILABLE:
                bleu_scores = []
                for gen, ref in zip(generations, references):
                    bleu = sacrebleu.sentence_bleu(gen, [ref])
                    bleu_scores.append(bleu.score)
                results['metrics']['bleu'] = np.mean(bleu_scores)
            
            # ROUGE scores
            if ROUGE_AVAILABLE:
                scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL'], use_stemmer=True)
                rouge_scores = {'rouge1': [], 'rouge2': [], 'rougeL': []}
                
                for gen, ref in zip(generations, references):
                    scores = scorer.score(ref, gen)
                    for key in rouge_scores:
                        rouge_scores[key].append(scores[key].fmeasure)
                
                for key in rouge_scores:
                    results['metrics'][key] = np.mean(rouge_scores[key])
            
            # Length statistics
            gen_lengths = [len(gen.split()) for gen in generations]
            ref_lengths = [len(ref.split()) for ref in references]
            
            results['metrics']['avg_gen_length'] = np.mean(gen_lengths)
            results['metrics']['avg_ref_length'] = np.mean(ref_lengths)
            results['metrics']['length_ratio'] = np.mean(gen_lengths) / np.mean(ref_lengths)
        
        return results


class BenchmarkEvaluator:
    """Run standardized benchmarks"""
    
    def __init__(self, model, tokenizer, device):
        self.model = model
        self.tokenizer = tokenizer
        self.device = device
        self.perplexity_evaluator = PerplexityEvaluator(model, tokenizer, device)
        self.generation_evaluator = GenerationEvaluator(model, tokenizer, device)
    
    def run_hellaswag_eval(self, dataset_path: str = None) -> Dict[str, float]:
        """Evaluate on HellaSwag dataset (common sense reasoning)"""
        # Simplified HellaSwag evaluation
        # In practice, you'd load the actual dataset
        
        examples = [
            {
                "context": "A woman is outside with a bucket and a dog. The dog is running around trying to avoid a bath. She",
                "choices": [
                    "rinses the bucket off with a hose and fills it with soap.",
                    "uses a hose to keep filling the bucket with water.",
                    "gets the dog wet, then it runs away again.",
                    "gets into the bucket."
                ],
                "correct": 2
            },
            # Add more examples...
        ]
        
        correct = 0
        total = 0
        
        for example in examples:
            context = example["context"]
            choices = example["choices"]
            correct_idx = example["correct"]
            
            # Calculate likelihood for each choice
            choice_scores = []
            for choice in choices:
                full_text = context + " " + choice
                tokens = self.tokenizer.encode(full_text)
                
                if len(tokens) < 2:
                    choice_scores.append(float('-inf'))
                    continue
                
                input_ids = torch.tensor([tokens[:-1]], device=self.device)
                labels = torch.tensor([tokens[1:]], device=self.device)
                
                with torch.no_grad():
                    outputs = self.model(input_ids=input_ids, labels=labels)
                    loss = outputs['loss']
                    choice_scores.append(-loss.item())
            
            # Check if highest scoring choice is correct
            predicted_idx = choice_scores.index(max(choice_scores))
            if predicted_idx == correct_idx:
                correct += 1
            total += 1
        
        accuracy = correct / total if total > 0 else 0.0
        return {"hellaswag_accuracy": accuracy}
    
    def run_lambada_eval(self, dataset_path: str = None) -> Dict[str, float]:
        """Evaluate on LAMBADA dataset (reading comprehension)"""
        # Simplified LAMBADA evaluation
        examples = [
            {
                "text": "George Washington was the first President of the United States. He served from 1789 to 1797. Washington was born in",
                "target": "Virginia"
            },
            # Add more examples...
        ]
        
        correct = 0
        total = 0
        
        for example in examples:
            text = example["text"]
            target = example["target"]
            
            # Generate continuation
            config = EvaluationConfig(max_new_tokens=10, temperature=0.0, do_sample=False)
            generation = self.generation_evaluator.generate_text(text, config)
            
            # Check if target word appears in generation
            if target.lower() in generation.lower():
                correct += 1
            total += 1
        
        accuracy = correct / total if total > 0 else 0.0
        return {"lambada_accuracy": accuracy}
    
    def run_comprehensive_eval(self) -> Dict[str, Any]:
        """Run comprehensive evaluation suite"""
        results = {}
        
        # Perplexity on sample texts
        sample_texts = [
            "The quick brown fox jumps over the lazy dog.",
            "Artificial intelligence is transforming the world in unprecedented ways.",
            "Climate change represents one of the most significant challenges of our time."
        ]
        
        perplexity_results = self.perplexity_evaluator.evaluate_perplexity(sample_texts)
        results.update(perplexity_results)
        
        # Common sense reasoning
        hellaswag_results = self.run_hellaswag_eval()
        results.update(hellaswag_results)
        
        # Reading comprehension
        lambada_results = self.run_lambada_eval()
        results.update(lambada_results)
        
        # Generation quality
        prompts = [
            "Explain the concept of machine learning in simple terms:",
            "Write a short story about a robot discovering emotions:",
            "Describe the benefits of renewable energy:"
        ]
        
        generation_results = self.generation_evaluator.evaluate_generation_quality(prompts)
        results['generation_examples'] = generation_results['generations']
        results.update(generation_results['metrics'])
        
        return results


def run_evaluation(model, tokenizer, device, output_dir: str = "eval_results"):
    """Run complete evaluation suite"""
    os.makedirs(output_dir, exist_ok=True)
    
    evaluator = BenchmarkEvaluator(model, tokenizer, device)
    
    print("Running comprehensive evaluation...")
    start_time = time.time()
    
    results = evaluator.run_comprehensive_eval()
    
    end_time = time.time()
    results['evaluation_time'] = end_time - start_time
    
    # Save results
    results_path = os.path.join(output_dir, "evaluation_results.json")
    with open(results_path, 'w') as f:
        json.dump(results, f, indent=2)
    
    print(f"Evaluation completed in {results['evaluation_time']:.2f} seconds")
    print(f"Results saved to {results_path}")
    
    # Print summary
    print("\n=== Evaluation Summary ===")
    if 'perplexity' in results:
        print(f"Perplexity: {results['perplexity']:.2f}")
    if 'hellaswag_accuracy' in results:
        print(f"HellaSwag Accuracy: {results['hellaswag_accuracy']:.3f}")
    if 'lambada_accuracy' in results:
        print(f"LAMBADA Accuracy: {results['lambada_accuracy']:.3f}")
    if 'bleu' in results:
        print(f"BLEU Score: {results['bleu']:.2f}")
    
    return results