File size: 27,634 Bytes
ef6446c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#!/usr/bin/env python3
# Copyright (C) 2024 Louis Chua Bean Chong
#
# This file is part of OpenLLM.
#
# OpenLLM is dual-licensed:
# 1. For open source use: GNU General Public License v3.0
# 2. For commercial use: Commercial License (contact for details)
#
# See LICENSE and docs/LICENSES.md for full license information.

"""
OpenLLM Model Evaluation Script

This script implements comprehensive evaluation for trained OpenLLM models,
including intrinsic evaluation (perplexity, loss) and text generation quality
assessment as specified in Step 5 of the training pipeline.

Usage:
    python core/src/evaluate_model.py \
        --model_dir models/openllm-medium \
        --eval_data data/clean/validation_data.txt \
        --metrics perplexity,loss

Features:
- Perplexity calculation on held-out data
- Text generation quality assessment
- Multiple evaluation metrics
- Comprehensive quality benchmarks
- JSON output for downstream analysis

Author: Louis Chua Bean Chong
License: GPLv3
"""

import argparse
import json
import math
import os
import sys
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as smp
import torch

# Add current directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from model import GPTModel, create_model


class ModelEvaluator:
    """
    Comprehensive evaluator for OpenLLM models.

    Implements intrinsic evaluation metrics and text generation quality
    assessment following the training pipeline specifications.
    """

    def __init__(self, model: GPTModel, tokenizer_path: str, device: str = "cpu"):
        """
        Initialize the model evaluator.

        Args:
            model: Trained GPT model
            tokenizer_path: Path to tokenizer model file
            device: Device to run evaluation on
        """
        self.model = model.to(device)
        self.device = device

        # Load tokenizer
        self.tokenizer = smp.SentencePieceProcessor()
        self.tokenizer.load(tokenizer_path)

        print("πŸ”§ ModelEvaluator initialized")
        print(f"  Device: {device}")
        print(f"  Model parameters: {model.get_num_params():,}")
        print(f"  Vocabulary size: {self.tokenizer.vocab_size():,}")

    def evaluate_perplexity(
        self, eval_data: List[str], max_seq_len: int = 512, batch_size: int = 1
    ) -> Dict[str, float]:
        """
        Calculate perplexity on evaluation data.

        Args:
            eval_data: List of text passages for evaluation
            max_seq_len: Maximum sequence length for evaluation
            batch_size: Batch size for evaluation

        Returns:
            Dictionary with loss and perplexity metrics
        """
        self.model.eval()
        total_loss = 0.0
        total_tokens = 0
        num_sequences = 0

        print(f"πŸ“Š Calculating perplexity on {len(eval_data)} passages...")

        with torch.no_grad():
            for i, text in enumerate(eval_data):
                if i % 100 == 0:
                    print(f"  Progress: {i}/{len(eval_data)} passages")

                # Tokenize text
                tokens = self.tokenizer.encode(text)
                if len(tokens) < 2:
                    continue

                # Truncate if too long
                if len(tokens) > max_seq_len:
                    tokens = tokens[:max_seq_len]

                # Create input and target tensors
                input_ids = torch.tensor([tokens[:-1]], dtype=torch.long, device=self.device)
                target_ids = torch.tensor([tokens[1:]], dtype=torch.long, device=self.device)

                # Forward pass
                logits, loss = self.model(input_ids, target_ids)

                # Accumulate loss
                seq_length = len(tokens) - 1
                total_loss += loss.item() * seq_length
                total_tokens += seq_length
                num_sequences += 1

        # Calculate metrics
        avg_loss = total_loss / total_tokens if total_tokens > 0 else float("inf")
        perplexity = math.exp(min(avg_loss, 10))  # Cap to prevent overflow

        return {
            "loss": avg_loss,
            "perplexity": perplexity,
            "total_tokens": total_tokens,
            "num_sequences": num_sequences,
        }

    def evaluate_text_generation(
        self,
        prompts: List[str],
        max_length: int = 256,
        temperature: float = 0.7,
        top_k: Optional[int] = 40,
        num_samples: int = 1,
    ) -> List[Dict[str, Any]]:
        """
        Evaluate text generation quality.

        Args:
            prompts: List of input prompts
            max_length: Maximum generation length
            temperature: Sampling temperature
            top_k: Top-k sampling parameter
            num_samples: Number of samples per prompt

        Returns:
            List of generation results with quality metrics
        """
        self.model.eval()
        results = []

        print(f"✍️  Evaluating text generation on {len(prompts)} prompts...")

        with torch.no_grad():
            for prompt in prompts:
                prompt_results = []

                for sample_idx in range(num_samples):
                    # Tokenize prompt
                    input_ids = self.tokenizer.encode(prompt)
                    input_tensor = torch.tensor([input_ids], dtype=torch.long, device=self.device)

                    start_time = time.time()

                    # Generate text
                    output = self.model.generate(
                        input_tensor,
                        max_new_tokens=max_length,
                        temperature=temperature,
                        top_k=top_k,
                    )

                    generation_time = time.time() - start_time

                    # Decode output
                    generated_ids = output[0].tolist()
                    full_text = self.tokenizer.decode(generated_ids)
                    generated_text = self.tokenizer.decode(generated_ids[len(input_ids) :])

                    # Calculate quality metrics
                    quality_metrics = self._assess_generation_quality(generated_text)

                    prompt_results.append(
                        {
                            "prompt": prompt,
                            "generated_text": generated_text,
                            "full_text": full_text,
                            "generation_time": generation_time,
                            "tokens_generated": len(generated_ids) - len(input_ids),
                            "tokens_per_second": (len(generated_ids) - len(input_ids))
                            / generation_time,
                            "quality_metrics": quality_metrics,
                        }
                    )

                results.extend(prompt_results)

        return results

    def _assess_generation_quality(self, text: str) -> Dict[str, float]:
        """
        Assess basic quality metrics for generated text.

        Args:
            text: Generated text to assess

        Returns:
            Dictionary of quality metrics
        """
        if not text.strip():
            return {
                "length": 0,
                "avg_word_length": 0,
                "repetition_rate": 1.0,
                "coherence_score": 0.0,
            }

        words = text.split()

        # Basic metrics
        length = len(words)
        avg_word_length = sum(len(word) for word in words) / len(words) if words else 0

        # Repetition rate (simple n-gram repetition)
        bigrams = [f"{words[i]} {words[i+1]}" for i in range(len(words) - 1)]
        unique_bigrams = len(set(bigrams))
        repetition_rate = 1 - (unique_bigrams / len(bigrams) if bigrams else 0)

        # Simple coherence score (based on sentence structure)
        sentences = text.split(".")
        valid_sentences = [s for s in sentences if len(s.strip().split()) > 3]
        coherence_score = len(valid_sentences) / len(sentences) if sentences else 0

        return {
            "length": length,
            "avg_word_length": avg_word_length,
            "repetition_rate": repetition_rate,
            "coherence_score": coherence_score,
        }

    def evaluate_downstream_tasks(self) -> Dict[str, Any]:
        """
        Evaluate model performance on downstream tasks.

        This function implements basic downstream task evaluation including:
        - Reading comprehension (simplified SQUAD-style)
        - Sentiment analysis (few-shot)
        - Common sense reasoning

        Returns:
            Dictionary of downstream task results
        """
        results = {}

        # 1. Reading Comprehension (Simplified SQUAD-style)
        results["reading_comprehension"] = self._evaluate_reading_comprehension()

        # 2. Sentiment Analysis (Few-shot learning)
        results["sentiment_analysis"] = self._evaluate_sentiment_analysis()

        # 3. Common Sense Reasoning
        results["reasoning"] = self._evaluate_reasoning()

        # 4. Text Completion Quality
        results["text_completion"] = self._evaluate_text_completion()

        return results

    def _evaluate_reading_comprehension(self) -> Dict[str, Any]:
        """Simplified reading comprehension evaluation."""
        # Sample reading comprehension tasks
        tasks = [
            {
                "context": "The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It is named after the engineer Gustave Eiffel, whose company designed and built the tower.",
                "question": "Who is the Eiffel Tower named after?",
                "expected": "Gustave Eiffel",
            },
            {
                "context": "Python is a high-level programming language. It was created by Guido van Rossum and first released in 1991.",
                "question": "When was Python first released?",
                "expected": "1991",
            },
            {
                "context": "Machine learning is a subset of artificial intelligence that enables computers to learn without being explicitly programmed.",
                "question": "What is machine learning a subset of?",
                "expected": "artificial intelligence",
            },
        ]

        correct = 0
        total = len(tasks)

        for task in tasks:
            prompt = f"Context: {task['context']}\nQuestion: {task['question']}\nAnswer:"

            # Generate answer
            input_ids = self.tokenizer.encode(prompt)
            input_tensor = torch.tensor([input_ids], dtype=torch.long, device=self.device)

            with torch.no_grad():
                output = self.model.generate(input_tensor, max_new_tokens=20, temperature=0.1)

            generated_ids = output[0].tolist()
            answer = self.tokenizer.decode(generated_ids[len(input_ids) :]).strip().lower()

            # Simple substring matching
            if task["expected"].lower() in answer:
                correct += 1

        return {
            "accuracy": correct / total,
            "correct": correct,
            "total": total,
            "score": correct / total,
        }

    def _evaluate_sentiment_analysis(self) -> Dict[str, Any]:
        """Few-shot sentiment analysis evaluation."""
        # Few-shot examples
        examples = "Examples:\nText: 'I love this movie!' Sentiment: Positive\nText: 'This is terrible.' Sentiment: Negative\nText: 'It was okay.' Sentiment: Neutral\n\n"

        # Test cases
        test_cases = [
            {"text": "This is amazing!", "expected": "positive"},
            {"text": "I hate this.", "expected": "negative"},
            {"text": "This is wonderful.", "expected": "positive"},
            {"text": "This is awful.", "expected": "negative"},
            {"text": "It was fine.", "expected": "neutral"},
        ]

        correct = 0
        total = len(test_cases)

        for case in test_cases:
            prompt = f"{examples}Text: '{case['text']}' Sentiment:"

            # Generate sentiment
            input_ids = self.tokenizer.encode(prompt)
            input_tensor = torch.tensor([input_ids], dtype=torch.long, device=self.device)

            with torch.no_grad():
                output = self.model.generate(input_tensor, max_new_tokens=5, temperature=0.1)

            generated_ids = output[0].tolist()
            sentiment = self.tokenizer.decode(generated_ids[len(input_ids) :]).strip().lower()

            # Check if expected sentiment is in the generated response
            if case["expected"] in sentiment:
                correct += 1

        return {
            "accuracy": correct / total,
            "correct": correct,
            "total": total,
            "score": correct / total,
        }

    def _evaluate_reasoning(self) -> Dict[str, Any]:
        """Simple reasoning evaluation."""
        # Basic reasoning tasks
        tasks = [
            {
                "question": "If all birds can fly and a penguin is a bird, can a penguin fly?",
                "expected": "no",  # This tests if model knows real-world facts
            },
            {
                "question": "If it is raining outside, should you take an umbrella?",
                "expected": "yes",
            },
            {"question": "What comes after Monday?", "expected": "tuesday"},
            {"question": "Is the sun larger than the earth?", "expected": "yes"},
        ]

        correct = 0
        total = len(tasks)

        for task in tasks:
            prompt = f"Question: {task['question']}\nAnswer:"

            # Generate answer
            input_ids = self.tokenizer.encode(prompt)
            input_tensor = torch.tensor([input_ids], dtype=torch.long, device=self.device)

            with torch.no_grad():
                output = self.model.generate(input_tensor, max_new_tokens=10, temperature=0.1)

            generated_ids = output[0].tolist()
            answer = self.tokenizer.decode(generated_ids[len(input_ids) :]).strip().lower()

            # Check if expected answer is in the response
            if task["expected"] in answer:
                correct += 1

        return {
            "accuracy": correct / total,
            "correct": correct,
            "total": total,
            "score": correct / total,
        }

    def _evaluate_text_completion(self) -> Dict[str, Any]:
        """Evaluate text completion quality."""
        # Common phrases that should be completed predictably
        completions = [
            {"prompt": "The capital of France is", "expected_word": "paris"},
            {"prompt": "Two plus two equals", "expected_word": "four"},
            {"prompt": "The largest planet in our solar system is", "expected_word": "jupiter"},
            {"prompt": "Water boils at", "expected_word": "100"},
        ]

        correct = 0
        total = len(completions)

        for completion in completions:
            # Generate completion
            input_ids = self.tokenizer.encode(completion["prompt"])
            input_tensor = torch.tensor([input_ids], dtype=torch.long, device=self.device)

            with torch.no_grad():
                output = self.model.generate(input_tensor, max_new_tokens=5, temperature=0.1)

            generated_ids = output[0].tolist()
            generated_text = self.tokenizer.decode(generated_ids[len(input_ids) :]).strip().lower()

            # Check if expected word appears in completion
            if completion["expected_word"] in generated_text:
                correct += 1

        return {
            "accuracy": correct / total,
            "correct": correct,
            "total": total,
            "score": correct / total,
        }

    def run_comprehensive_evaluation(
        self, eval_data_path: str, metrics: List[str] = None, generation_prompts: List[str] = None
    ) -> Dict[str, Any]:
        """
        Run comprehensive model evaluation.

        Args:
            eval_data_path: Path to evaluation text file
            metrics: List of metrics to compute
            generation_prompts: Prompts for text generation evaluation

        Returns:
            Complete evaluation results
        """
        if metrics is None:
            metrics = ["perplexity", "loss", "generation"]

        if generation_prompts is None:
            generation_prompts = [
                "The history of artificial intelligence",
                "Machine learning algorithms",
                "The future of technology",
                "In a world where",
                "Scientists have discovered",
            ]

        results = {
            "model_info": {
                "parameters": self.model.get_num_params(),
                "device": self.device,
                "vocab_size": self.tokenizer.vocab_size(),
            },
            "evaluation_timestamp": time.time(),
        }

        # Load evaluation data
        print(f"πŸ“‚ Loading evaluation data from {eval_data_path}")
        if os.path.exists(eval_data_path):
            with open(eval_data_path, "r", encoding="utf-8") as f:
                eval_texts = [line.strip() for line in f if line.strip()]
        else:
            print("⚠️  Evaluation file not found, using sample texts")
            eval_texts = [
                "Artificial intelligence is a rapidly growing field of computer science.",
                "Machine learning algorithms can learn patterns from data automatically.",
                "Natural language processing helps computers understand human language.",
                "Deep learning uses neural networks with multiple layers for complex tasks.",
                "The development of large language models has transformed AI applications.",
            ]

        # Intrinsic evaluation
        if "perplexity" in metrics or "loss" in metrics:
            perplexity_results = self.evaluate_perplexity(eval_texts)
            results["intrinsic_evaluation"] = perplexity_results

        # Text generation evaluation
        if "generation" in metrics:
            generation_results = self.evaluate_text_generation(generation_prompts)
            results["generation_evaluation"] = {
                "results": generation_results,
                "summary": self._summarize_generation_results(generation_results),
            }

        # Downstream tasks (placeholder)
        results["downstream_evaluation"] = self.evaluate_downstream_tasks()

        # Overall quality assessment
        results["quality_assessment"] = self._assess_overall_quality(results)

        return results

    def _summarize_generation_results(self, results: List[Dict[str, Any]]) -> Dict[str, float]:
        """Summarize text generation results."""
        if not results:
            return {}

        total_time = sum(r["generation_time"] for r in results)
        total_tokens = sum(r["tokens_generated"] for r in results)

        quality_metrics = [r["quality_metrics"] for r in results]

        return {
            "avg_generation_time": total_time / len(results),
            "avg_tokens_per_second": total_tokens / total_time if total_time > 0 else 0,
            "avg_length": sum(q["length"] for q in quality_metrics) / len(quality_metrics),
            "avg_repetition_rate": sum(q["repetition_rate"] for q in quality_metrics)
            / len(quality_metrics),
            "avg_coherence_score": sum(q["coherence_score"] for q in quality_metrics)
            / len(quality_metrics),
        }

    def _assess_overall_quality(self, results: Dict[str, Any]) -> Dict[str, Any]:
        """Assess overall model quality based on evaluation results."""
        assessment = {"quality_level": "unknown", "recommendations": []}

        # Check intrinsic metrics
        if "intrinsic_evaluation" in results:
            perplexity = results["intrinsic_evaluation"].get("perplexity", float("inf"))

            if perplexity < 12:
                assessment["quality_level"] = "good"
                assessment["recommendations"].append("Model shows good perplexity scores")
            elif perplexity < 50:
                assessment["quality_level"] = "fair"
                assessment["recommendations"].append(
                    "Model shows fair performance, could benefit from more training"
                )
            else:
                assessment["quality_level"] = "poor"
                assessment["recommendations"].append(
                    "Model needs significant more training or data improvements"
                )

        # Check generation quality
        if "generation_evaluation" in results:
            summary = results["generation_evaluation"].get("summary", {})
            repetition_rate = summary.get("avg_repetition_rate", 1.0)
            coherence_score = summary.get("avg_coherence_score", 0.0)

            if repetition_rate > 0.7:
                assessment["recommendations"].append(
                    "High repetition rate - consider training longer or adjusting data"
                )
            if coherence_score < 0.3:
                assessment["recommendations"].append(
                    "Low coherence - model may need more training steps"
                )

        return assessment


def load_model_from_directory(model_dir: str, device: str = "cpu") -> Tuple[GPTModel, str]:
    """
    Load model from directory containing checkpoints.

    Args:
        model_dir: Directory containing model files
        device: Device to load model on

    Returns:
        Tuple of (model, tokenizer_path)
    """
    model_dir = Path(model_dir)

    # Find best model checkpoint
    best_model_path = model_dir / "best_model.pt"
    if not best_model_path.exists():
        # Look for latest checkpoint
        checkpoints = list(model_dir.glob("checkpoint_step_*.pt"))
        if not checkpoints:
            raise FileNotFoundError(f"No model checkpoints found in {model_dir}")

        # Get latest checkpoint
        latest_checkpoint = max(checkpoints, key=lambda p: int(p.stem.split("_")[-1]))
        best_model_path = latest_checkpoint

    print(f"πŸ“‚ Loading model from {best_model_path}")

    # Load checkpoint
    checkpoint = torch.load(best_model_path, map_location=device)

    # Determine model size from config
    config = checkpoint.get("config", {})
    n_layer = config.get("n_layer", 12)

    if n_layer <= 6:
        model_size = "small"
    elif n_layer <= 12:
        model_size = "medium"
    else:
        model_size = "large"

    # Create and load model
    model = create_model(model_size)
    model.load_state_dict(checkpoint["model_state_dict"])

    print(f"βœ… Model loaded successfully ({model_size}, {model.get_num_params():,} parameters)")

    # Find tokenizer
    tokenizer_path = model_dir.parent / "tokenizer" / "tokenizer.model"
    if not tokenizer_path.exists():
        tokenizer_path = Path("data/tokenizer/tokenizer.model")

    if not tokenizer_path.exists():
        raise FileNotFoundError(f"Tokenizer not found at {tokenizer_path}")

    return model, str(tokenizer_path)


def main():
    """Main evaluation function."""
    parser = argparse.ArgumentParser(
        description="Evaluate OpenLLM model performance",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Basic evaluation
  python core/src/evaluate_model.py \\
    --model_dir models/small-extended-4k \\
    --eval_data data/clean/training_data.txt

  # Specific metrics
  python core/src/evaluate_model.py \\
    --model_dir models/small-extended-4k \\
    --metrics perplexity,generation \\
    --output results.json
        """,
    )

    parser.add_argument("--model_dir", required=True, help="Directory containing trained model")

    parser.add_argument(
        "--eval_data", help="Path to evaluation text file (default: use sample texts)"
    )

    parser.add_argument(
        "--metrics",
        default="perplexity,loss,generation",
        help="Comma-separated list of metrics to evaluate (default: perplexity,loss,generation)",
    )

    parser.add_argument("--output", help="Output JSON file for results (default: print to console)")

    parser.add_argument(
        "--device",
        choices=["cpu", "cuda", "auto"],
        default="auto",
        help="Device for evaluation (default: auto)",
    )

    parser.add_argument(
        "--generation_prompts", help="File containing prompts for text generation evaluation"
    )

    args = parser.parse_args()

    print("πŸ“Š OpenLLM Model Evaluation")
    print("=" * 50)

    # Determine device
    if args.device == "auto":
        device = "cuda" if torch.cuda.is_available() else "cpu"
    else:
        device = args.device

    print(f"Using device: {device}")

    try:
        # Load model
        model, tokenizer_path = load_model_from_directory(args.model_dir, device)

        # Create evaluator
        evaluator = ModelEvaluator(model, tokenizer_path, device)

        # Parse metrics
        metrics = [m.strip() for m in args.metrics.split(",")]

        # Load generation prompts if specified
        generation_prompts = None
        if args.generation_prompts and os.path.exists(args.generation_prompts):
            with open(args.generation_prompts, "r", encoding="utf-8") as f:
                generation_prompts = [line.strip() for line in f if line.strip()]

        # Run evaluation
        eval_data_path = args.eval_data or "data/clean/training_data.txt"
        results = evaluator.run_comprehensive_evaluation(
            eval_data_path, metrics, generation_prompts
        )

        # Output results
        if args.output:
            with open(args.output, "w", encoding="utf-8") as f:
                json.dump(results, f, indent=2)
            print(f"\nπŸ’Ύ Results saved to {args.output}")
        else:
            print("\nπŸ“Š Evaluation Results:")
            print("=" * 50)

            # Print key metrics
            if "intrinsic_evaluation" in results:
                intrinsic = results["intrinsic_evaluation"]
                print("πŸ“ˆ Intrinsic Metrics:")
                print(f"  Loss: {intrinsic['loss']:.4f}")
                print(f"  Perplexity: {intrinsic['perplexity']:.2f}")
                print(f"  Sequences evaluated: {intrinsic['num_sequences']:,}")

            if "generation_evaluation" in results:
                gen_summary = results["generation_evaluation"]["summary"]
                print("\n✍️  Generation Quality:")
                print(
                    f"  Avg generation speed: {gen_summary['avg_tokens_per_second']:.1f} tokens/sec"
                )
                print(f"  Avg text length: {gen_summary['avg_length']:.1f} words")
                print(f"  Repetition rate: {gen_summary['avg_repetition_rate']:.3f}")
                print(f"  Coherence score: {gen_summary['avg_coherence_score']:.3f}")

            # Quality assessment
            if "quality_assessment" in results:
                assessment = results["quality_assessment"]
                print("\n🎯 Overall Assessment:")
                print(f"  Quality Level: {assessment['quality_level'].upper()}")
                for rec in assessment["recommendations"]:
                    print(f"  β€’ {rec}")

        print("\nπŸŽ‰ Evaluation completed successfully!")

    except Exception as e:
        print(f"\n❌ Evaluation failed: {e}")
        import traceback

        traceback.print_exc()
        return False

    return True


if __name__ == "__main__":
    main()