oxirag 0.1.1

A four-layer RAG engine with SMT-based logic verification and knowledge graph support
Documentation
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
//! Distillation metrics and evaluation module.
//!
//! This module provides types and functionality for evaluating distilled models,
//! comparing teacher and student model performance, and tracking training metrics.

use serde::{Deserialize, Serialize};

/// A test example for model evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestExample {
    /// Input query or prompt.
    pub input: String,
    /// Expected output or reference answer.
    pub expected_output: String,
    /// Optional metadata for the example.
    pub metadata: Option<TestExampleMetadata>,
}

/// Metadata for a test example.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TestExampleMetadata {
    /// Category or domain of the example.
    pub category: Option<String>,
    /// Difficulty level (0.0 - 1.0).
    pub difficulty: Option<f32>,
    /// Source of the example.
    pub source: Option<String>,
}

impl TestExample {
    /// Create a new test example.
    #[must_use]
    pub fn new(input: impl Into<String>, expected_output: impl Into<String>) -> Self {
        Self {
            input: input.into(),
            expected_output: expected_output.into(),
            metadata: None,
        }
    }

    /// Create a test example with metadata.
    #[must_use]
    pub fn with_metadata(mut self, metadata: TestExampleMetadata) -> Self {
        self.metadata = Some(metadata);
        self
    }

    /// Set the category for this example.
    #[must_use]
    pub fn with_category(mut self, category: impl Into<String>) -> Self {
        let meta = self
            .metadata
            .get_or_insert_with(TestExampleMetadata::default);
        meta.category = Some(category.into());
        self
    }

    /// Set the difficulty for this example.
    #[must_use]
    pub fn with_difficulty(mut self, difficulty: f32) -> Self {
        let meta = self
            .metadata
            .get_or_insert_with(TestExampleMetadata::default);
        meta.difficulty = Some(difficulty.clamp(0.0, 1.0));
        self
    }
}

/// Result of evaluating a model.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EvaluationResult {
    /// Accuracy score (0.0 - 1.0).
    pub accuracy: f32,
    /// Precision score (0.0 - 1.0).
    pub precision: f32,
    /// Recall score (0.0 - 1.0).
    pub recall: f32,
    /// F1 score (harmonic mean of precision and recall).
    pub f1_score: f32,
    /// Average latency in milliseconds.
    pub latency_ms: f64,
    /// Throughput in examples per second.
    pub throughput: f64,
    /// Model size in bytes.
    pub model_size_bytes: u64,
    /// Peak memory usage in bytes.
    pub memory_usage: u64,
}

impl EvaluationResult {
    /// Create a new evaluation result.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create an evaluation result with accuracy metrics.
    #[must_use]
    pub fn with_accuracy_metrics(accuracy: f32, precision: f32, recall: f32) -> Self {
        let f1_score = Self::calculate_f1(precision, recall);
        Self {
            accuracy,
            precision,
            recall,
            f1_score,
            ..Default::default()
        }
    }

    /// Calculate F1 score from precision and recall.
    #[must_use]
    pub fn calculate_f1(precision: f32, recall: f32) -> f32 {
        if precision + recall <= 0.0 {
            0.0
        } else {
            2.0 * precision * recall / (precision + recall)
        }
    }

    /// Set latency metrics.
    #[must_use]
    pub fn with_latency(mut self, latency_ms: f64, throughput: f64) -> Self {
        self.latency_ms = latency_ms;
        self.throughput = throughput;
        self
    }

    /// Set memory metrics.
    #[must_use]
    pub fn with_memory(mut self, model_size_bytes: u64, memory_usage: u64) -> Self {
        self.model_size_bytes = model_size_bytes;
        self.memory_usage = memory_usage;
        self
    }

    /// Check if the model meets quality thresholds.
    #[must_use]
    pub fn meets_threshold(&self, min_accuracy: f32, max_latency_ms: f64) -> bool {
        self.accuracy >= min_accuracy && self.latency_ms <= max_latency_ms
    }

    /// Calculate overall quality score (weighted combination of metrics).
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn overall_quality_score(&self) -> f64 {
        // Weighted combination: accuracy (40%), F1 (30%), normalized latency (30%)
        let accuracy_weight = 0.4;
        let f1_weight = 0.3;
        let latency_weight = 0.3;

        let normalized_latency = if self.latency_ms > 0.0 {
            (1000.0 / self.latency_ms).min(1.0)
        } else {
            1.0
        };

        f64::from(self.accuracy) * accuracy_weight
            + f64::from(self.f1_score) * f1_weight
            + normalized_latency * latency_weight
    }
}

/// Result of comparing teacher and student models.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ComparisonResult {
    /// Accuracy retention: student accuracy / teacher accuracy.
    pub accuracy_retention: f64,
    /// Speedup ratio: teacher latency / student latency.
    pub speedup_ratio: f64,
    /// Compression ratio: teacher size / student size.
    pub compression_ratio: f64,
    /// Overall quality score.
    pub quality_score: f64,
    /// Teacher evaluation result.
    pub teacher_result: EvaluationResult,
    /// Student evaluation result.
    pub student_result: EvaluationResult,
}

impl ComparisonResult {
    /// Create a new comparison result from teacher and student evaluations.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn from_evaluations(teacher: EvaluationResult, student: EvaluationResult) -> Self {
        let accuracy_retention = if teacher.accuracy > 0.0 {
            f64::from(student.accuracy) / f64::from(teacher.accuracy)
        } else {
            0.0
        };

        let speedup_ratio = if student.latency_ms > 0.0 {
            teacher.latency_ms / student.latency_ms
        } else {
            0.0
        };

        let compression_ratio = if student.model_size_bytes > 0 {
            teacher.model_size_bytes as f64 / student.model_size_bytes as f64
        } else {
            0.0
        };

        let quality_score =
            Self::calculate_quality_score(accuracy_retention, speedup_ratio, compression_ratio);

        Self {
            accuracy_retention,
            speedup_ratio,
            compression_ratio,
            quality_score,
            teacher_result: teacher,
            student_result: student,
        }
    }

    /// Calculate overall quality score from component metrics.
    #[must_use]
    fn calculate_quality_score(
        accuracy_retention: f64,
        speedup_ratio: f64,
        compression_ratio: f64,
    ) -> f64 {
        // Quality score combines retention, speedup, and compression
        // Higher is better for all three
        let retention_weight = 0.5;
        let speedup_weight = 0.3;
        let compression_weight = 0.2;

        // Normalize speedup and compression (cap at reasonable values)
        let normalized_speedup = speedup_ratio.min(10.0) / 10.0;
        let normalized_compression = compression_ratio.min(20.0) / 20.0;

        accuracy_retention * retention_weight
            + normalized_speedup * speedup_weight
            + normalized_compression * compression_weight
    }

    /// Check if distillation was successful based on thresholds.
    #[must_use]
    pub fn is_successful(&self, min_accuracy_retention: f64, min_speedup: f64) -> bool {
        self.accuracy_retention >= min_accuracy_retention && self.speedup_ratio >= min_speedup
    }

    /// Get a summary of the comparison.
    #[must_use]
    pub fn summary(&self) -> ComparisonSummary {
        ComparisonSummary {
            accuracy_retention_percent: self.accuracy_retention * 100.0,
            speedup_factor: self.speedup_ratio,
            compression_factor: self.compression_ratio,
            quality_score: self.quality_score,
            student_faster: self.speedup_ratio > 1.0,
            student_smaller: self.compression_ratio > 1.0,
        }
    }
}

/// A summary of the comparison result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComparisonSummary {
    /// Accuracy retention as a percentage.
    pub accuracy_retention_percent: f64,
    /// How many times faster the student is.
    pub speedup_factor: f64,
    /// How many times smaller the student is.
    pub compression_factor: f64,
    /// Overall quality score.
    pub quality_score: f64,
    /// Whether the student is faster than the teacher.
    pub student_faster: bool,
    /// Whether the student is smaller than the teacher.
    pub student_smaller: bool,
}

/// Layer-wise similarity measurement.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LayerSimilarity {
    /// Layer index (0-indexed).
    pub layer_index: usize,
    /// Layer name or identifier.
    pub layer_name: String,
    /// Cosine similarity between teacher and student layer outputs.
    pub cosine_similarity: f32,
    /// Mean squared error between layer outputs.
    pub mse: f32,
    /// Pearson correlation coefficient.
    pub correlation: f32,
}

impl LayerSimilarity {
    /// Create a new layer similarity measurement.
    #[must_use]
    pub fn new(layer_index: usize, layer_name: impl Into<String>) -> Self {
        Self {
            layer_index,
            layer_name: layer_name.into(),
            ..Default::default()
        }
    }

    /// Set similarity metrics.
    #[must_use]
    pub fn with_metrics(mut self, cosine_similarity: f32, mse: f32, correlation: f32) -> Self {
        self.cosine_similarity = cosine_similarity;
        self.mse = mse;
        self.correlation = correlation;
        self
    }

    /// Calculate an aggregate similarity score.
    #[must_use]
    pub fn aggregate_score(&self) -> f32 {
        // Combine cosine similarity and correlation (both higher is better)
        // MSE is inverted since lower is better
        let mse_score = 1.0 / (1.0 + self.mse);
        (self.cosine_similarity + self.correlation + mse_score) / 3.0
    }
}

/// Metrics for measuring knowledge transfer efficiency.
///
/// This type provides detailed metrics about the knowledge transfer process
/// during distillation, including layer-wise similarity measurements.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct KnowledgeTransferMetrics {
    /// Knowledge transfer efficiency (0.0 - 1.0).
    /// Measures how well the student learned from the teacher.
    pub knowledge_transfer_efficiency: f32,
    /// Capacity utilization (0.0 - 1.0).
    /// Measures how much of the student's capacity is being used.
    pub capacity_utilization: f32,
    /// Layer-wise similarity measurements.
    pub layer_wise_similarity: Vec<LayerSimilarity>,
    /// Average layer similarity.
    pub average_layer_similarity: f32,
    /// Training epochs completed.
    pub epochs_completed: usize,
    /// Final training loss.
    pub final_loss: f32,
}

impl KnowledgeTransferMetrics {
    /// Create new knowledge transfer metrics.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set transfer efficiency.
    #[must_use]
    pub fn with_transfer_efficiency(mut self, efficiency: f32) -> Self {
        self.knowledge_transfer_efficiency = efficiency.clamp(0.0, 1.0);
        self
    }

    /// Set capacity utilization.
    #[must_use]
    pub fn with_capacity_utilization(mut self, utilization: f32) -> Self {
        self.capacity_utilization = utilization.clamp(0.0, 1.0);
        self
    }

    /// Set layer-wise similarity measurements.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn with_layer_similarity(mut self, similarities: Vec<LayerSimilarity>) -> Self {
        if similarities.is_empty() {
            self.average_layer_similarity = 0.0;
        } else {
            let sum: f32 = similarities
                .iter()
                .map(LayerSimilarity::aggregate_score)
                .sum();
            self.average_layer_similarity = sum / similarities.len() as f32;
        }
        self.layer_wise_similarity = similarities;
        self
    }

    /// Add a layer similarity measurement.
    #[allow(clippy::cast_precision_loss)]
    pub fn add_layer_similarity(&mut self, similarity: LayerSimilarity) {
        self.layer_wise_similarity.push(similarity);
        // Recalculate average
        let sum: f32 = self
            .layer_wise_similarity
            .iter()
            .map(LayerSimilarity::aggregate_score)
            .sum();
        self.average_layer_similarity = sum / self.layer_wise_similarity.len() as f32;
    }

    /// Get the overall distillation quality score.
    #[must_use]
    pub fn overall_score(&self) -> f32 {
        // Weighted combination of metrics
        let transfer_weight = 0.4;
        let capacity_weight = 0.3;
        let similarity_weight = 0.3;

        self.knowledge_transfer_efficiency * transfer_weight
            + self.capacity_utilization * capacity_weight
            + self.average_layer_similarity * similarity_weight
    }

    /// Check if distillation quality meets thresholds.
    #[must_use]
    pub fn meets_quality_threshold(&self, min_score: f32) -> bool {
        self.overall_score() >= min_score
    }
}

/// Metrics for a single training epoch with comprehensive tracking.
///
/// This type provides detailed per-epoch metrics for tracking training progress
/// during distillation, including loss, accuracy, and timing information.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TrainingEpochMetrics {
    /// Epoch number (1-indexed).
    pub epoch: usize,
    /// Training loss.
    pub train_loss: f32,
    /// Validation loss.
    pub val_loss: f32,
    /// Training accuracy.
    pub train_accuracy: f32,
    /// Validation accuracy.
    pub val_accuracy: f32,
    /// Learning rate used in this epoch.
    pub learning_rate: f64,
    /// Temperature used for distillation.
    pub temperature: f32,
    /// Duration of the epoch in seconds.
    pub duration_secs: f64,
    /// Optional additional metrics.
    pub extra_metrics: Option<ExtraEpochMetrics>,
}

/// Additional epoch metrics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExtraEpochMetrics {
    /// Gradient norm.
    pub gradient_norm: Option<f32>,
    /// Number of training samples processed.
    pub samples_processed: Option<usize>,
    /// Memory usage in bytes.
    pub memory_usage: Option<u64>,
}

impl TrainingEpochMetrics {
    /// Create new epoch metrics.
    #[must_use]
    pub fn new(epoch: usize) -> Self {
        Self {
            epoch,
            ..Default::default()
        }
    }

    /// Set loss values.
    #[must_use]
    pub fn with_loss(mut self, train_loss: f32, val_loss: f32) -> Self {
        self.train_loss = train_loss;
        self.val_loss = val_loss;
        self
    }

    /// Set accuracy values.
    #[must_use]
    pub fn with_accuracy(mut self, train_accuracy: f32, val_accuracy: f32) -> Self {
        self.train_accuracy = train_accuracy;
        self.val_accuracy = val_accuracy;
        self
    }

    /// Set training parameters.
    #[must_use]
    pub fn with_params(mut self, learning_rate: f64, temperature: f32) -> Self {
        self.learning_rate = learning_rate;
        self.temperature = temperature;
        self
    }

    /// Set duration.
    #[must_use]
    pub fn with_duration(mut self, duration_secs: f64) -> Self {
        self.duration_secs = duration_secs;
        self
    }

    /// Check if this epoch shows improvement over a previous epoch.
    #[must_use]
    pub fn improved_over(&self, other: &Self) -> bool {
        self.val_loss < other.val_loss
    }

    /// Check if the model is overfitting (train loss much lower than val loss).
    #[must_use]
    pub fn is_overfitting(&self, threshold: f32) -> bool {
        self.train_loss > 0.0 && (self.val_loss - self.train_loss) / self.train_loss > threshold
    }
}

/// Data for plotting training progress.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PlotData {
    /// Epoch numbers.
    pub epochs: Vec<usize>,
    /// Training loss values.
    pub train_loss: Vec<f32>,
    /// Validation loss values.
    pub val_loss: Vec<f32>,
    /// Training accuracy values.
    pub train_accuracy: Vec<f32>,
    /// Validation accuracy values.
    pub val_accuracy: Vec<f32>,
    /// Learning rate values.
    pub learning_rate: Vec<f64>,
}

impl PlotData {
    /// Create new plot data.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create plot data from epoch metrics.
    #[must_use]
    pub fn from_metrics(metrics: &[TrainingEpochMetrics]) -> Self {
        let mut plot_data = Self::new();
        for m in metrics {
            plot_data.add_epoch(m);
        }
        plot_data
    }

    /// Add an epoch to the plot data.
    pub fn add_epoch(&mut self, metrics: &TrainingEpochMetrics) {
        self.epochs.push(metrics.epoch);
        self.train_loss.push(metrics.train_loss);
        self.val_loss.push(metrics.val_loss);
        self.train_accuracy.push(metrics.train_accuracy);
        self.val_accuracy.push(metrics.val_accuracy);
        self.learning_rate.push(metrics.learning_rate);
    }

    /// Get the number of epochs.
    #[must_use]
    pub fn len(&self) -> usize {
        self.epochs.len()
    }

    /// Check if the plot data is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.epochs.is_empty()
    }

    /// Get min and max values for loss.
    #[must_use]
    pub fn loss_range(&self) -> Option<(f32, f32)> {
        let all_loss: Vec<f32> = self
            .train_loss
            .iter()
            .chain(self.val_loss.iter())
            .copied()
            .collect();

        if all_loss.is_empty() {
            return None;
        }

        let min = all_loss.iter().copied().fold(f32::INFINITY, f32::min);
        let max = all_loss.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        Some((min, max))
    }
}

/// Tracks metrics across training epochs.
#[derive(Debug, Clone, Default)]
pub struct MetricsTracker {
    /// History of epoch metrics.
    history: Vec<TrainingEpochMetrics>,
    /// Best epoch index based on validation loss.
    best_epoch_idx: Option<usize>,
    /// Best validation loss seen.
    best_val_loss: f32,
}

impl MetricsTracker {
    /// Create a new metrics tracker.
    #[must_use]
    pub fn new() -> Self {
        Self {
            history: Vec::new(),
            best_epoch_idx: None,
            best_val_loss: f32::INFINITY,
        }
    }

    /// Record metrics for an epoch.
    pub fn record_epoch(&mut self, metrics: TrainingEpochMetrics) {
        // Check if this is the best epoch
        if metrics.val_loss < self.best_val_loss {
            self.best_val_loss = metrics.val_loss;
            self.best_epoch_idx = Some(self.history.len());
        }

        self.history.push(metrics);
    }

    /// Get the history of all epoch metrics.
    #[must_use]
    pub fn get_history(&self) -> Vec<TrainingEpochMetrics> {
        self.history.clone()
    }

    /// Get a reference to the history.
    #[must_use]
    pub fn history(&self) -> &[TrainingEpochMetrics] {
        &self.history
    }

    /// Get the best epoch (epoch number and metrics).
    #[must_use]
    pub fn best_epoch(&self) -> Option<(usize, TrainingEpochMetrics)> {
        self.best_epoch_idx
            .and_then(|idx| self.history.get(idx).map(|m| (m.epoch, m.clone())))
    }

    /// Get the best epoch index.
    #[must_use]
    pub fn best_epoch_index(&self) -> Option<usize> {
        self.best_epoch_idx
    }

    /// Get data formatted for plotting.
    #[must_use]
    pub fn plot_data(&self) -> PlotData {
        PlotData::from_metrics(&self.history)
    }

    /// Get the number of epochs recorded.
    #[must_use]
    pub fn epoch_count(&self) -> usize {
        self.history.len()
    }

    /// Get the latest epoch metrics.
    #[must_use]
    pub fn latest(&self) -> Option<&TrainingEpochMetrics> {
        self.history.last()
    }

    /// Check if training has converged (validation loss not improving).
    #[must_use]
    pub fn has_converged(&self, patience: usize) -> bool {
        if self.history.len() < patience {
            return false;
        }

        let recent = &self.history[self.history.len() - patience..];
        let first_val_loss = recent.first().map_or(f32::INFINITY, |m| m.val_loss);

        // Converged if no improvement in the last `patience` epochs
        recent.iter().all(|m| m.val_loss >= first_val_loss)
    }

    /// Calculate average training time per epoch.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn avg_epoch_duration(&self) -> f64 {
        if self.history.is_empty() {
            return 0.0;
        }

        let total: f64 = self.history.iter().map(|m| m.duration_secs).sum();
        total / self.history.len() as f64
    }

    /// Get the improvement rate (average decrease in validation loss per epoch).
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn improvement_rate(&self) -> f32 {
        if self.history.len() < 2 {
            return 0.0;
        }

        let first_loss = self.history.first().map_or(0.0, |m| m.val_loss);
        let last_loss = self.history.last().map_or(0.0, |m| m.val_loss);
        let epochs = self.history.len() as f32 - 1.0;

        (first_loss - last_loss) / epochs
    }

    /// Clear all recorded metrics.
    pub fn clear(&mut self) {
        self.history.clear();
        self.best_epoch_idx = None;
        self.best_val_loss = f32::INFINITY;
    }

    /// Get summary statistics.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn summary(&self) -> TrackerSummary {
        let avg_train_loss = if self.history.is_empty() {
            0.0
        } else {
            self.history.iter().map(|m| m.train_loss).sum::<f32>() / self.history.len() as f32
        };

        let avg_val_loss = if self.history.is_empty() {
            0.0
        } else {
            self.history.iter().map(|m| m.val_loss).sum::<f32>() / self.history.len() as f32
        };

        TrackerSummary {
            total_epochs: self.history.len(),
            best_epoch: self
                .best_epoch_idx
                .and_then(|idx| self.history.get(idx).map(|m| m.epoch)),
            best_val_loss: self.best_val_loss,
            final_val_loss: self.history.last().map(|m| m.val_loss),
            avg_train_loss,
            avg_val_loss,
            total_duration_secs: self.history.iter().map(|m| m.duration_secs).sum(),
        }
    }
}

/// Summary of the metrics tracker state.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TrackerSummary {
    /// Total number of epochs recorded.
    pub total_epochs: usize,
    /// Best epoch number.
    pub best_epoch: Option<usize>,
    /// Best validation loss achieved.
    pub best_val_loss: f32,
    /// Final validation loss.
    pub final_val_loss: Option<f32>,
    /// Average training loss across all epochs.
    pub avg_train_loss: f32,
    /// Average validation loss across all epochs.
    pub avg_val_loss: f32,
    /// Total training duration in seconds.
    pub total_duration_secs: f64,
}

/// Trait for student models that can be evaluated for text generation.
///
/// This trait provides a text-generation interface for student models,
/// distinct from the vector-based interface in `teacher_student` module.
pub trait EvalStudentModel: Send + Sync {
    /// Generate output for a given input.
    fn generate(&self, input: &str) -> String;

    /// Get the model size in bytes.
    fn model_size_bytes(&self) -> u64;

    /// Get model name or identifier.
    fn name(&self) -> &str;
}

/// Trait for teacher models that can be evaluated for text generation.
///
/// This trait provides a text-generation interface for teacher models,
/// distinct from the vector-based interface in `teacher_student` module.
pub trait EvalTeacherModel: Send + Sync {
    /// Generate output for a given input.
    fn generate(&self, input: &str) -> String;

    /// Get the model size in bytes.
    fn model_size_bytes(&self) -> u64;

    /// Get model name or identifier.
    fn name(&self) -> &str;
}

/// Evaluator for distilled models.
#[derive(Debug, Clone, Default)]
pub struct DistillationEvaluator {
    /// Configuration for evaluation.
    config: EvaluatorConfig,
}

/// Configuration for the evaluator.
#[derive(Debug, Clone)]
pub struct EvaluatorConfig {
    /// Minimum samples required for evaluation.
    pub min_samples: usize,
    /// Timeout per sample in milliseconds.
    pub timeout_ms: u64,
    /// Whether to measure memory usage.
    pub measure_memory: bool,
    /// Similarity threshold for considering outputs as matching.
    pub similarity_threshold: f32,
}

impl Default for EvaluatorConfig {
    fn default() -> Self {
        Self {
            min_samples: 10,
            timeout_ms: 5000,
            measure_memory: true,
            similarity_threshold: 0.8,
        }
    }
}

impl DistillationEvaluator {
    /// Create a new evaluator with default configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create an evaluator with custom configuration.
    #[must_use]
    pub fn with_config(config: EvaluatorConfig) -> Self {
        Self { config }
    }

    /// Get the evaluator configuration.
    #[must_use]
    pub fn config(&self) -> &EvaluatorConfig {
        &self.config
    }

    /// Evaluate a student model on test data.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn evaluate_model(
        &self,
        model: &dyn EvalStudentModel,
        test_data: &[TestExample],
    ) -> EvaluationResult {
        if test_data.is_empty() {
            return EvaluationResult::default();
        }

        let mut correct = 0;
        let mut total_latency_ms = 0.0;
        let mut true_positives = 0;
        let mut false_positives = 0;
        let mut false_negatives = 0;

        for example in test_data {
            let start = std::time::Instant::now();
            let output = model.generate(&example.input);
            let latency = start.elapsed().as_secs_f64() * 1000.0;
            total_latency_ms += latency;

            let is_match = self.outputs_match(&output, &example.expected_output);

            if is_match {
                correct += 1;
                true_positives += 1;
            } else if !output.is_empty() {
                false_positives += 1;
            }

            if !is_match && !example.expected_output.is_empty() {
                false_negatives += 1;
            }
        }

        let num_examples = test_data.len() as f32;
        let accuracy = correct as f32 / num_examples;
        let avg_latency = total_latency_ms / test_data.len() as f64;
        let throughput = if avg_latency > 0.0 {
            1000.0 / avg_latency
        } else {
            0.0
        };

        let precision = if true_positives + false_positives > 0 {
            true_positives as f32 / (true_positives + false_positives) as f32
        } else {
            0.0
        };

        let recall = if true_positives + false_negatives > 0 {
            true_positives as f32 / (true_positives + false_negatives) as f32
        } else {
            0.0
        };

        let f1_score = EvaluationResult::calculate_f1(precision, recall);

        EvaluationResult {
            accuracy,
            precision,
            recall,
            f1_score,
            latency_ms: avg_latency,
            throughput,
            model_size_bytes: model.model_size_bytes(),
            memory_usage: 0, // Would need actual memory measurement
        }
    }

    /// Compare a teacher and student model on the same test data.
    #[must_use]
    pub fn compare_models(
        &self,
        teacher: &dyn EvalTeacherModel,
        student: &dyn EvalStudentModel,
        data: &[TestExample],
    ) -> ComparisonResult {
        // Create a wrapper to evaluate teacher model
        let teacher_wrapper = TeacherModelWrapper { model: teacher };
        let teacher_result = self.evaluate_model(&teacher_wrapper, data);
        let student_result = self.evaluate_model(student, data);

        ComparisonResult::from_evaluations(teacher_result, student_result)
    }

    /// Check if two outputs match based on similarity.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    fn outputs_match(&self, output: &str, expected: &str) -> bool {
        if output == expected {
            return true;
        }

        // Normalize and compare
        let normalized_output = output.trim().to_lowercase();
        let normalized_expected = expected.trim().to_lowercase();

        if normalized_output == normalized_expected {
            return true;
        }

        // Calculate simple word overlap similarity
        let output_words: std::collections::HashSet<_> =
            normalized_output.split_whitespace().collect();
        let expected_words: std::collections::HashSet<_> =
            normalized_expected.split_whitespace().collect();

        if output_words.is_empty() || expected_words.is_empty() {
            return false;
        }

        let intersection = output_words.intersection(&expected_words).count();
        let union = output_words.union(&expected_words).count();

        if union == 0 {
            return false;
        }

        let similarity = intersection as f32 / union as f32;
        similarity >= self.config.similarity_threshold
    }
}

/// Internal wrapper to evaluate teacher models using the `StudentModel` trait.
struct TeacherModelWrapper<'a> {
    model: &'a dyn EvalTeacherModel,
}

impl EvalStudentModel for TeacherModelWrapper<'_> {
    fn generate(&self, input: &str) -> String {
        self.model.generate(input)
    }

    fn model_size_bytes(&self) -> u64 {
        self.model.model_size_bytes()
    }

    fn name(&self) -> &str {
        self.model.name()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Mock student model for testing
    struct MockStudentModel {
        name: String,
        size_bytes: u64,
        responses: std::collections::HashMap<String, String>,
    }

    impl MockStudentModel {
        fn new(name: &str, size_bytes: u64) -> Self {
            Self {
                name: name.to_string(),
                size_bytes,
                responses: std::collections::HashMap::new(),
            }
        }

        fn with_response(mut self, input: &str, output: &str) -> Self {
            self.responses.insert(input.to_string(), output.to_string());
            self
        }
    }

    impl EvalStudentModel for MockStudentModel {
        fn generate(&self, input: &str) -> String {
            self.responses
                .get(input)
                .cloned()
                .unwrap_or_else(|| "default response".to_string())
        }

        fn model_size_bytes(&self) -> u64 {
            self.size_bytes
        }

        fn name(&self) -> &str {
            &self.name
        }
    }

    // Mock teacher model for testing
    struct MockTeacherModel {
        name: String,
        size_bytes: u64,
        responses: std::collections::HashMap<String, String>,
    }

    impl MockTeacherModel {
        fn new(name: &str, size_bytes: u64) -> Self {
            Self {
                name: name.to_string(),
                size_bytes,
                responses: std::collections::HashMap::new(),
            }
        }

        fn with_response(mut self, input: &str, output: &str) -> Self {
            self.responses.insert(input.to_string(), output.to_string());
            self
        }
    }

    impl EvalTeacherModel for MockTeacherModel {
        fn generate(&self, input: &str) -> String {
            self.responses
                .get(input)
                .cloned()
                .unwrap_or_else(|| "teacher response".to_string())
        }

        fn model_size_bytes(&self) -> u64 {
            self.size_bytes
        }

        fn name(&self) -> &str {
            &self.name
        }
    }

    #[test]
    fn test_test_example_creation() {
        let example = TestExample::new("What is Rust?", "A programming language.");
        assert_eq!(example.input, "What is Rust?");
        assert_eq!(example.expected_output, "A programming language.");
        assert!(example.metadata.is_none());
    }

    #[test]
    fn test_test_example_with_metadata() {
        let example = TestExample::new("input", "output")
            .with_category("test")
            .with_difficulty(0.5);

        assert!(example.metadata.is_some());
        let meta = example.metadata.unwrap();
        assert_eq!(meta.category, Some("test".to_string()));
        assert!((meta.difficulty.unwrap() - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn test_evaluation_result_f1_calculation() {
        let f1 = EvaluationResult::calculate_f1(0.8, 0.6);
        let expected = 2.0 * 0.8 * 0.6 / (0.8 + 0.6);
        assert!((f1 - expected).abs() < f32::EPSILON);

        // Test zero case
        let f1_zero = EvaluationResult::calculate_f1(0.0, 0.0);
        assert!((f1_zero - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_evaluation_result_quality_score() {
        let result = EvaluationResult {
            accuracy: 0.9,
            f1_score: 0.85,
            latency_ms: 100.0,
            ..Default::default()
        };

        let score = result.overall_quality_score();
        assert!(score > 0.0 && score <= 1.0);
    }

    #[test]
    fn test_evaluation_result_meets_threshold() {
        let result = EvaluationResult {
            accuracy: 0.9,
            latency_ms: 50.0,
            ..Default::default()
        };

        assert!(result.meets_threshold(0.8, 100.0));
        assert!(!result.meets_threshold(0.95, 100.0));
        assert!(!result.meets_threshold(0.8, 30.0));
    }

    #[test]
    fn test_comparison_result_from_evaluations() {
        let teacher = EvaluationResult {
            accuracy: 0.95,
            latency_ms: 200.0,
            model_size_bytes: 1_000_000,
            ..Default::default()
        };

        let student = EvaluationResult {
            accuracy: 0.90,
            latency_ms: 50.0,
            model_size_bytes: 100_000,
            ..Default::default()
        };

        let comparison = ComparisonResult::from_evaluations(teacher, student);

        // Accuracy retention: 0.90 / 0.95 ~ 0.947
        assert!((comparison.accuracy_retention - 0.947).abs() < 0.01);

        // Speedup: 200 / 50 = 4.0
        assert!((comparison.speedup_ratio - 4.0).abs() < 0.01);

        // Compression: 1_000_000 / 100_000 = 10.0
        assert!((comparison.compression_ratio - 10.0).abs() < 0.01);
    }

    #[test]
    fn test_comparison_result_is_successful() {
        let comparison = ComparisonResult {
            accuracy_retention: 0.95,
            speedup_ratio: 3.0,
            ..Default::default()
        };

        assert!(comparison.is_successful(0.9, 2.0));
        assert!(!comparison.is_successful(0.98, 2.0));
        assert!(!comparison.is_successful(0.9, 5.0));
    }

    #[test]
    fn test_layer_similarity() {
        let layer = LayerSimilarity::new(0, "layer_0").with_metrics(0.9, 0.1, 0.85);

        assert_eq!(layer.layer_index, 0);
        assert!((layer.cosine_similarity - 0.9).abs() < f32::EPSILON);

        let score = layer.aggregate_score();
        assert!(score > 0.0 && score <= 1.0);
    }

    #[test]
    fn test_knowledge_transfer_metrics() {
        let metrics = KnowledgeTransferMetrics::new()
            .with_transfer_efficiency(0.9)
            .with_capacity_utilization(0.8);

        assert!((metrics.knowledge_transfer_efficiency - 0.9).abs() < f32::EPSILON);
        assert!((metrics.capacity_utilization - 0.8).abs() < f32::EPSILON);

        let score = metrics.overall_score();
        assert!(score > 0.0 && score <= 1.0);
    }

    #[test]
    fn test_knowledge_transfer_metrics_with_layers() {
        let layers = vec![
            LayerSimilarity::new(0, "layer_0").with_metrics(0.9, 0.1, 0.85),
            LayerSimilarity::new(1, "layer_1").with_metrics(0.85, 0.15, 0.8),
        ];

        let metrics = KnowledgeTransferMetrics::new().with_layer_similarity(layers);

        assert_eq!(metrics.layer_wise_similarity.len(), 2);
        assert!(metrics.average_layer_similarity > 0.0);
    }

    #[test]
    fn test_training_epoch_metrics() {
        let metrics = TrainingEpochMetrics::new(1)
            .with_loss(0.5, 0.6)
            .with_accuracy(0.8, 0.75)
            .with_params(0.001, 4.0)
            .with_duration(120.0);

        assert_eq!(metrics.epoch, 1);
        assert!((metrics.train_loss - 0.5).abs() < f32::EPSILON);
        assert!((metrics.val_loss - 0.6).abs() < f32::EPSILON);
        assert!((metrics.train_accuracy - 0.8).abs() < f32::EPSILON);
        assert!((metrics.learning_rate - 0.001).abs() < f64::EPSILON);
    }

    #[test]
    fn test_training_epoch_metrics_improved_over() {
        let epoch1 = TrainingEpochMetrics::new(1).with_loss(0.5, 0.6);
        let epoch2 = TrainingEpochMetrics::new(2).with_loss(0.4, 0.55);

        assert!(epoch2.improved_over(&epoch1));
        assert!(!epoch1.improved_over(&epoch2));
    }

    #[test]
    fn test_training_epoch_metrics_overfitting() {
        let overfitting = TrainingEpochMetrics::new(1).with_loss(0.1, 0.5);
        let normal = TrainingEpochMetrics::new(1).with_loss(0.3, 0.35);

        assert!(overfitting.is_overfitting(1.0));
        assert!(!normal.is_overfitting(1.0));
    }

    #[test]
    fn test_metrics_tracker_record_epoch() {
        let mut tracker = MetricsTracker::new();

        tracker.record_epoch(TrainingEpochMetrics::new(1).with_loss(0.5, 0.6));
        tracker.record_epoch(TrainingEpochMetrics::new(2).with_loss(0.4, 0.5));
        tracker.record_epoch(TrainingEpochMetrics::new(3).with_loss(0.45, 0.55));

        assert_eq!(tracker.epoch_count(), 3);

        let best = tracker.best_epoch();
        assert!(best.is_some());
        let (epoch, _) = best.unwrap();
        assert_eq!(epoch, 2); // Epoch 2 has lowest val_loss
    }

    #[test]
    fn test_metrics_tracker_history() {
        let mut tracker = MetricsTracker::new();

        tracker.record_epoch(TrainingEpochMetrics::new(1).with_loss(0.5, 0.6));
        tracker.record_epoch(TrainingEpochMetrics::new(2).with_loss(0.4, 0.5));

        let history = tracker.get_history();
        assert_eq!(history.len(), 2);
    }

    #[test]
    fn test_metrics_tracker_plot_data() {
        let mut tracker = MetricsTracker::new();

        tracker.record_epoch(
            TrainingEpochMetrics::new(1)
                .with_loss(0.5, 0.6)
                .with_accuracy(0.7, 0.65),
        );
        tracker.record_epoch(
            TrainingEpochMetrics::new(2)
                .with_loss(0.4, 0.5)
                .with_accuracy(0.8, 0.75),
        );

        let plot_data = tracker.plot_data();

        assert_eq!(plot_data.len(), 2);
        assert_eq!(plot_data.epochs, vec![1, 2]);
        assert_eq!(plot_data.train_loss.len(), 2);
        assert_eq!(plot_data.val_accuracy.len(), 2);
    }

    #[test]
    fn test_metrics_tracker_convergence() {
        let mut tracker = MetricsTracker::new();

        // Simulate converged training (no improvement)
        tracker.record_epoch(TrainingEpochMetrics::new(1).with_loss(0.5, 0.5));
        tracker.record_epoch(TrainingEpochMetrics::new(2).with_loss(0.45, 0.5));
        tracker.record_epoch(TrainingEpochMetrics::new(3).with_loss(0.4, 0.5));

        assert!(tracker.has_converged(3));
        assert!(!tracker.has_converged(4));
    }

    #[test]
    fn test_metrics_tracker_summary() {
        let mut tracker = MetricsTracker::new();

        tracker.record_epoch(
            TrainingEpochMetrics::new(1)
                .with_loss(0.5, 0.6)
                .with_duration(60.0),
        );
        tracker.record_epoch(
            TrainingEpochMetrics::new(2)
                .with_loss(0.4, 0.5)
                .with_duration(55.0),
        );

        let summary = tracker.summary();

        assert_eq!(summary.total_epochs, 2);
        assert_eq!(summary.best_epoch, Some(2));
        assert!((summary.best_val_loss - 0.5).abs() < f32::EPSILON);
        assert!((summary.total_duration_secs - 115.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_metrics_tracker_clear() {
        let mut tracker = MetricsTracker::new();

        tracker.record_epoch(TrainingEpochMetrics::new(1).with_loss(0.5, 0.6));
        tracker.clear();

        assert_eq!(tracker.epoch_count(), 0);
        assert!(tracker.best_epoch().is_none());
    }

    #[test]
    fn test_metrics_tracker_latest() {
        let mut tracker = MetricsTracker::new();

        assert!(tracker.latest().is_none());

        tracker.record_epoch(TrainingEpochMetrics::new(1).with_loss(0.5, 0.6));
        tracker.record_epoch(TrainingEpochMetrics::new(2).with_loss(0.4, 0.5));

        let latest = tracker.latest();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap().epoch, 2);
    }

    #[test]
    fn test_metrics_tracker_improvement_rate() {
        let mut tracker = MetricsTracker::new();

        tracker.record_epoch(TrainingEpochMetrics::new(1).with_loss(0.0, 1.0));
        tracker.record_epoch(TrainingEpochMetrics::new(2).with_loss(0.0, 0.8));
        tracker.record_epoch(TrainingEpochMetrics::new(3).with_loss(0.0, 0.6));

        let rate = tracker.improvement_rate();
        // (1.0 - 0.6) / 2 = 0.2
        assert!((rate - 0.2).abs() < 0.01);
    }

    #[test]
    fn test_plot_data_from_metrics() {
        let metrics = vec![
            TrainingEpochMetrics::new(1).with_loss(0.5, 0.6),
            TrainingEpochMetrics::new(2).with_loss(0.4, 0.5),
        ];

        let plot_data = PlotData::from_metrics(&metrics);

        assert_eq!(plot_data.len(), 2);
        assert_eq!(plot_data.epochs, vec![1, 2]);
    }

    #[test]
    fn test_plot_data_loss_range() {
        let mut plot_data = PlotData::new();
        plot_data.add_epoch(&TrainingEpochMetrics::new(1).with_loss(0.5, 0.6));
        plot_data.add_epoch(&TrainingEpochMetrics::new(2).with_loss(0.3, 0.7));

        let range = plot_data.loss_range();
        assert!(range.is_some());
        let (min, max) = range.unwrap();
        assert!((min - 0.3).abs() < f32::EPSILON);
        assert!((max - 0.7).abs() < f32::EPSILON);
    }

    #[test]
    fn test_distillation_evaluator_creation() {
        let evaluator = DistillationEvaluator::new();
        assert_eq!(evaluator.config().min_samples, 10);
    }

    #[test]
    fn test_distillation_evaluator_custom_config() {
        let config = EvaluatorConfig {
            min_samples: 5,
            timeout_ms: 1000,
            measure_memory: false,
            similarity_threshold: 0.9,
        };

        let evaluator = DistillationEvaluator::with_config(config);
        assert_eq!(evaluator.config().min_samples, 5);
        assert_eq!(evaluator.config().timeout_ms, 1000);
    }

    #[test]
    fn test_distillation_evaluator_evaluate_model() {
        let model = MockStudentModel::new("test-student", 50_000)
            .with_response("What is Rust?", "A programming language.");

        let test_data = vec![TestExample::new("What is Rust?", "A programming language.")];

        let evaluator = DistillationEvaluator::new();
        let result = evaluator.evaluate_model(&model, &test_data);

        assert!((result.accuracy - 1.0).abs() < f32::EPSILON);
        assert_eq!(result.model_size_bytes, 50_000);
    }

    #[test]
    fn test_distillation_evaluator_compare_models() {
        let teacher =
            MockTeacherModel::new("teacher", 1_000_000).with_response("test", "teacher answer");

        let student =
            MockStudentModel::new("student", 100_000).with_response("test", "student answer");

        let test_data = vec![TestExample::new("test", "teacher answer")];

        let evaluator = DistillationEvaluator::new();
        let comparison = evaluator.compare_models(&teacher, &student, &test_data);

        // Teacher should have perfect accuracy, student should be close
        assert!(comparison.teacher_result.accuracy > 0.0);
        assert!(comparison.compression_ratio > 1.0); // Student is smaller
    }

    #[test]
    fn test_distillation_evaluator_empty_data() {
        let model = MockStudentModel::new("test", 1000);
        let test_data: Vec<TestExample> = vec![];

        let evaluator = DistillationEvaluator::new();
        let result = evaluator.evaluate_model(&model, &test_data);

        assert!((result.accuracy - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_comparison_summary() {
        let comparison = ComparisonResult {
            accuracy_retention: 0.95,
            speedup_ratio: 4.0,
            compression_ratio: 10.0,
            quality_score: 0.8,
            ..Default::default()
        };

        let summary = comparison.summary();

        assert!((summary.accuracy_retention_percent - 95.0).abs() < 0.01);
        assert!(summary.student_faster);
        assert!(summary.student_smaller);
    }

    #[test]
    fn test_evaluator_config_default() {
        let config = EvaluatorConfig::default();

        assert_eq!(config.min_samples, 10);
        assert_eq!(config.timeout_ms, 5000);
        assert!(config.measure_memory);
        assert!((config.similarity_threshold - 0.8).abs() < f32::EPSILON);
    }

    #[test]
    fn test_tracker_avg_epoch_duration() {
        let mut tracker = MetricsTracker::new();

        tracker.record_epoch(TrainingEpochMetrics::new(1).with_duration(60.0));
        tracker.record_epoch(TrainingEpochMetrics::new(2).with_duration(50.0));
        tracker.record_epoch(TrainingEpochMetrics::new(3).with_duration(70.0));

        let avg = tracker.avg_epoch_duration();
        assert!((avg - 60.0).abs() < 0.01);
    }
}