trustformers-debug 0.1.1

Advanced debugging tools for TrustformeRS models
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
//! Auto-debugging and automated recommendation system.
//!
//! This module provides intelligent debugging capabilities that automatically
//! analyze model behavior, identify potential issues, and generate actionable
//! recommendations for optimization and problem resolution.

use anyhow::Result;
use std::collections::{HashMap, VecDeque};

use super::types::{
    ConvergenceStatus, LayerActivationStats, ModelPerformanceMetrics, TrainingDynamics,
};

/// Auto-debugging system for intelligent model analysis.
#[derive(Debug)]
pub struct AutoDebugger {
    /// Debugging configuration
    config: AutoDebugConfig,
    /// Historical performance data for analysis
    performance_history: VecDeque<ModelPerformanceMetrics>,
    /// Layer statistics history
    layer_history: HashMap<String, VecDeque<LayerActivationStats>>,
    /// Training dynamics history
    dynamics_history: VecDeque<TrainingDynamics>,
    /// Known issue patterns and solutions
    #[allow(dead_code)]
    issue_patterns: IssuePatternDatabase,
    /// Current debugging session state
    session_state: DebuggingSession,
}

/// Configuration for auto-debugging system.
#[derive(Debug, Clone)]
pub struct AutoDebugConfig {
    /// Maximum history size for analysis
    pub max_history_size: usize,
    /// Minimum samples required for pattern detection
    pub min_samples_for_analysis: usize,
    /// Confidence threshold for recommendations
    pub recommendation_confidence_threshold: f64,
    /// Enable advanced pattern recognition
    pub enable_advanced_patterns: bool,
    /// Enable hyperparameter suggestions
    pub enable_hyperparameter_suggestions: bool,
    /// Enable architectural recommendations
    pub enable_architectural_recommendations: bool,
}

/// Current debugging session state.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DebuggingSession {
    /// Session start time
    pub session_start: chrono::DateTime<chrono::Utc>,
    /// Issues identified in current session
    pub identified_issues: Vec<IdentifiedIssue>,
    /// Recommendations generated
    pub recommendations: Vec<DebuggingRecommendation>,
    /// Session statistics
    pub session_stats: SessionStatistics,
}

/// An identified issue with diagnostic information.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct IdentifiedIssue {
    /// Issue category
    pub category: IssueCategory,
    /// Issue description
    pub description: String,
    /// Severity level
    pub severity: IssueSeverity,
    /// Confidence in identification
    pub confidence: f64,
    /// Evidence supporting the identification
    pub evidence: Vec<String>,
    /// Potential causes
    pub potential_causes: Vec<String>,
    /// When issue was identified
    pub identified_at: chrono::DateTime<chrono::Utc>,
}

/// Categories of issues that can be identified.
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum IssueCategory {
    /// Learning rate related issues
    LearningRate,
    /// Gradient flow problems
    GradientFlow,
    /// Overfitting issues
    Overfitting,
    /// Underfitting issues
    Underfitting,
    /// Data quality problems
    DataQuality,
    /// Architecture inefficiencies
    Architecture,
    /// Memory management issues
    Memory,
    /// Convergence problems
    Convergence,
    /// Numerical stability issues
    NumericalStability,
}

/// Severity levels for identified issues.
#[derive(Debug, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
pub enum IssueSeverity {
    /// Minor issue with low impact
    Minor,
    /// Moderate issue affecting performance
    Moderate,
    /// Major issue requiring attention
    Major,
    /// Critical issue requiring immediate action
    Critical,
}

/// Auto-generated debugging recommendation.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DebuggingRecommendation {
    /// Recommendation category
    pub category: RecommendationCategory,
    /// Recommendation title
    pub title: String,
    /// Detailed description
    pub description: String,
    /// Specific actions to take
    pub actions: Vec<String>,
    /// Expected impact
    pub expected_impact: String,
    /// Confidence in recommendation
    pub confidence: f64,
    /// Priority level
    pub priority: AutoDebugRecommendationPriority,
    /// Relevant hyperparameters to adjust
    pub hyperparameter_suggestions: Vec<HyperparameterSuggestion>,
}

/// Categories of recommendations.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum RecommendationCategory {
    /// Hyperparameter adjustments
    HyperparameterTuning,
    /// Architectural changes
    ArchitecturalModification,
    /// Data preprocessing recommendations
    DataPreprocessing,
    /// Training strategy changes
    TrainingStrategy,
    /// Debugging and monitoring
    DebuggingAndMonitoring,
    /// Resource optimization
    ResourceOptimization,
}

/// Priority levels for recommendations.
#[derive(Debug, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
pub enum AutoDebugRecommendationPriority {
    /// Low priority suggestion
    Low,
    /// Medium priority recommendation
    Medium,
    /// High priority action needed
    High,
    /// Urgent action required
    Urgent,
}

/// Hyperparameter adjustment suggestion.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct HyperparameterSuggestion {
    /// Parameter name
    pub parameter_name: String,
    /// Current value (if known)
    pub current_value: Option<f64>,
    /// Suggested value
    pub suggested_value: f64,
    /// Adjustment reasoning
    pub reasoning: String,
    /// Expected effect
    pub expected_effect: String,
}

/// Database of known issue patterns and solutions.
#[derive(Debug, Clone)]
pub struct IssuePatternDatabase {
    /// Learning rate patterns
    pub learning_rate_patterns: Vec<IssuePattern>,
    /// Gradient patterns
    pub gradient_patterns: Vec<IssuePattern>,
    /// Convergence patterns
    pub convergence_patterns: Vec<IssuePattern>,
    /// Layer behavior patterns
    pub layer_patterns: Vec<IssuePattern>,
}

/// Pattern for identifying specific issues.
#[derive(Debug, Clone)]
pub struct IssuePattern {
    /// Pattern name
    pub name: String,
    /// Pattern description
    pub description: String,
    /// Conditions that must be met
    pub conditions: Vec<PatternCondition>,
    /// Associated issue category
    pub issue_category: IssueCategory,
    /// Confidence weight for this pattern
    pub confidence_weight: f64,
    /// Recommended solutions
    pub solutions: Vec<String>,
}

/// Condition for pattern matching.
#[derive(Debug, Clone)]
pub struct PatternCondition {
    /// Metric name
    pub metric: String,
    /// Comparison operator
    pub operator: ComparisonOperator,
    /// Threshold value
    pub threshold: f64,
    /// Number of consecutive occurrences required
    pub consecutive_count: usize,
}

/// Comparison operators for pattern conditions.
#[derive(Debug, Clone)]
pub enum ComparisonOperator {
    /// Greater than
    GreaterThan,
    /// Less than
    LessThan,
    /// Equal to (within tolerance)
    EqualTo,
    /// Increasing trend
    Increasing,
    /// Decreasing trend
    Decreasing,
    /// Oscillating pattern
    Oscillating,
}

/// Session statistics for debugging analysis.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SessionStatistics {
    /// Total issues identified
    pub total_issues: usize,
    /// Issues by category
    pub issues_by_category: HashMap<IssueCategory, usize>,
    /// Total recommendations generated
    pub total_recommendations: usize,
    /// Average confidence of recommendations
    pub avg_recommendation_confidence: f64,
    /// Analysis time taken
    pub analysis_duration: chrono::Duration,
}

impl Default for AutoDebugConfig {
    fn default() -> Self {
        Self {
            max_history_size: 1000,
            min_samples_for_analysis: 10,
            recommendation_confidence_threshold: 0.7,
            enable_advanced_patterns: true,
            enable_hyperparameter_suggestions: true,
            enable_architectural_recommendations: true,
        }
    }
}

impl AutoDebugger {
    /// Create a new auto-debugger.
    pub fn new() -> Self {
        Self {
            config: AutoDebugConfig::default(),
            performance_history: VecDeque::new(),
            layer_history: HashMap::new(),
            dynamics_history: VecDeque::new(),
            issue_patterns: IssuePatternDatabase::new(),
            session_state: DebuggingSession::new(),
        }
    }

    /// Create auto-debugger with custom configuration.
    pub fn with_config(config: AutoDebugConfig) -> Self {
        Self {
            config,
            performance_history: VecDeque::new(),
            layer_history: HashMap::new(),
            dynamics_history: VecDeque::new(),
            issue_patterns: IssuePatternDatabase::new(),
            session_state: DebuggingSession::new(),
        }
    }

    /// Record performance metrics for analysis.
    pub fn record_performance_metrics(&mut self, metrics: ModelPerformanceMetrics) {
        self.performance_history.push_back(metrics);

        while self.performance_history.len() > self.config.max_history_size {
            self.performance_history.pop_front();
        }
    }

    /// Record layer statistics for analysis.
    pub fn record_layer_stats(&mut self, stats: LayerActivationStats) {
        let layer_name = stats.layer_name.clone();

        let layer_history = self.layer_history.entry(layer_name).or_default();
        layer_history.push_back(stats);

        while layer_history.len() > self.config.max_history_size {
            layer_history.pop_front();
        }
    }

    /// Record training dynamics for analysis.
    pub fn record_training_dynamics(&mut self, dynamics: TrainingDynamics) {
        self.dynamics_history.push_back(dynamics);

        while self.dynamics_history.len() > self.config.max_history_size {
            self.dynamics_history.pop_front();
        }
    }

    /// Perform comprehensive auto-debugging analysis.
    pub fn perform_analysis(&mut self) -> Result<DebuggingReport> {
        let analysis_start = chrono::Utc::now();

        if self.performance_history.len() < self.config.min_samples_for_analysis {
            return Err(anyhow::anyhow!("Insufficient data for analysis"));
        }

        // Clear previous session state
        self.session_state = DebuggingSession::new();

        // Analyze different aspects
        self.analyze_learning_rate_issues()?;
        self.analyze_convergence_issues()?;
        self.analyze_gradient_flow_issues()?;
        self.analyze_layer_health_issues()?;
        self.analyze_memory_issues()?;
        self.analyze_overfitting_underfitting()?;

        // Generate recommendations based on identified issues
        self.generate_recommendations()?;

        // Update session statistics
        self.session_state.session_stats.analysis_duration = chrono::Utc::now() - analysis_start;
        self.update_session_statistics();

        Ok(DebuggingReport {
            session_info: self.session_state.clone(),
            identified_issues: self.session_state.identified_issues.clone(),
            recommendations: self.session_state.recommendations.clone(),
            summary: self.generate_analysis_summary(),
        })
    }

    /// Analyze learning rate related issues.
    fn analyze_learning_rate_issues(&mut self) -> Result<()> {
        let recent_metrics: Vec<_> = self.performance_history.iter().rev().take(20).collect();
        if recent_metrics.len() < 10 {
            return Ok(());
        }

        let mut issues_to_add = Vec::new();

        // Check for loss explosion
        let recent_losses: Vec<f64> = recent_metrics.iter().map(|m| m.loss).collect();
        if let Some(max_loss) = recent_losses
            .iter()
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        {
            if let Some(min_loss) = recent_losses
                .iter()
                .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            {
                if max_loss / min_loss > 10.0 {
                    issues_to_add.push(IdentifiedIssue {
                        category: IssueCategory::LearningRate,
                        description: "Learning rate too high - loss explosion detected".to_string(),
                        severity: IssueSeverity::Critical,
                        confidence: 0.9,
                        evidence: vec![
                            format!("Loss ratio: {:.2}", max_loss / min_loss),
                            "Rapid loss increase observed".to_string(),
                        ],
                        potential_causes: vec![
                            "Learning rate set too high".to_string(),
                            "Gradient clipping disabled".to_string(),
                            "Numerical instability".to_string(),
                        ],
                        identified_at: chrono::Utc::now(),
                    });
                }
            }
        }

        // Check for learning stagnation
        let loss_variance = self.calculate_variance(&recent_losses);
        let recent_metrics_len = recent_metrics.len();
        if loss_variance < 1e-6 && recent_metrics_len >= 15 {
            issues_to_add.push(IdentifiedIssue {
                category: IssueCategory::LearningRate,
                description: "Learning rate too low - training stagnation".to_string(),
                severity: IssueSeverity::Major,
                confidence: 0.8,
                evidence: vec![
                    format!("Loss variance: {:.2e}", loss_variance),
                    "No learning progress in recent steps".to_string(),
                ],
                potential_causes: vec![
                    "Learning rate set too low".to_string(),
                    "Learning rate decay too aggressive".to_string(),
                    "Model has converged".to_string(),
                ],
                identified_at: chrono::Utc::now(),
            });
        }

        // Add all collected issues
        for issue in issues_to_add {
            self.add_issue(issue);
        }

        Ok(())
    }

    /// Analyze convergence related issues.
    fn analyze_convergence_issues(&mut self) -> Result<()> {
        if let Some(latest_dynamics) = self.dynamics_history.back() {
            match latest_dynamics.convergence_status {
                ConvergenceStatus::Diverging => {
                    self.add_issue(IdentifiedIssue {
                        category: IssueCategory::Convergence,
                        description: "Training is diverging".to_string(),
                        severity: IssueSeverity::Critical,
                        confidence: 0.95,
                        evidence: vec!["Convergence status: Diverging".to_string()],
                        potential_causes: vec![
                            "Learning rate too high".to_string(),
                            "Gradient explosion".to_string(),
                            "Numerical instability".to_string(),
                        ],
                        identified_at: chrono::Utc::now(),
                    });
                },
                ConvergenceStatus::Plateau => {
                    if let Some(plateau_info) = &latest_dynamics.plateau_detection {
                        if plateau_info.duration_steps > 100 {
                            self.add_issue(IdentifiedIssue {
                                category: IssueCategory::Convergence,
                                description: "Training has plateaued".to_string(),
                                severity: IssueSeverity::Moderate,
                                confidence: 0.8,
                                evidence: vec![
                                    format!(
                                        "Plateau duration: {} steps",
                                        plateau_info.duration_steps
                                    ),
                                    format!("Plateau value: {:.4}", plateau_info.plateau_value),
                                ],
                                potential_causes: vec![
                                    "Learning rate too low".to_string(),
                                    "Model capacity insufficient".to_string(),
                                    "Local minimum reached".to_string(),
                                ],
                                identified_at: chrono::Utc::now(),
                            });
                        }
                    }
                },
                ConvergenceStatus::Oscillating => {
                    self.add_issue(IdentifiedIssue {
                        category: IssueCategory::NumericalStability,
                        description: "Training is oscillating".to_string(),
                        severity: IssueSeverity::Moderate,
                        confidence: 0.7,
                        evidence: vec!["Convergence status: Oscillating".to_string()],
                        potential_causes: vec![
                            "Learning rate too high".to_string(),
                            "Batch size too small".to_string(),
                            "Momentum settings suboptimal".to_string(),
                        ],
                        identified_at: chrono::Utc::now(),
                    });
                },
                _ => {},
            }
        }

        Ok(())
    }

    /// Analyze gradient flow issues.
    fn analyze_gradient_flow_issues(&mut self) -> Result<()> {
        let mut issues_to_add = Vec::new();

        // Check layer statistics for gradient flow problems
        for (layer_name, layer_history) in &self.layer_history {
            if let Some(latest_stats) = layer_history.back() {
                // Check for dead neurons
                if latest_stats.dead_neurons_ratio > 0.5 {
                    issues_to_add.push(IdentifiedIssue {
                        category: IssueCategory::GradientFlow,
                        description: format!("High dead neuron ratio in layer {}", layer_name),
                        severity: IssueSeverity::Major,
                        confidence: 0.85,
                        evidence: vec![
                            format!(
                                "Dead neurons: {:.1}%",
                                latest_stats.dead_neurons_ratio * 100.0
                            ),
                            format!("Layer: {}", layer_name),
                        ],
                        potential_causes: vec![
                            "Dying ReLU problem".to_string(),
                            "Poor weight initialization".to_string(),
                            "Learning rate too high".to_string(),
                        ],
                        identified_at: chrono::Utc::now(),
                    });
                }

                // Check for activation saturation
                if latest_stats.saturated_neurons_ratio > 0.3 {
                    issues_to_add.push(IdentifiedIssue {
                        category: IssueCategory::GradientFlow,
                        description: format!("High activation saturation in layer {}", layer_name),
                        severity: IssueSeverity::Moderate,
                        confidence: 0.8,
                        evidence: vec![
                            format!(
                                "Saturated neurons: {:.1}%",
                                latest_stats.saturated_neurons_ratio * 100.0
                            ),
                            format!("Layer: {}", layer_name),
                        ],
                        potential_causes: vec![
                            "Vanishing gradient problem".to_string(),
                            "Poor activation function choice".to_string(),
                            "Input normalization issues".to_string(),
                        ],
                        identified_at: chrono::Utc::now(),
                    });
                }
            }
        }

        // Add all collected issues
        for issue in issues_to_add {
            self.add_issue(issue);
        }

        Ok(())
    }

    /// Analyze layer health issues.
    fn analyze_layer_health_issues(&mut self) -> Result<()> {
        let mut issues_to_add = Vec::new();

        for (layer_name, layer_history) in &self.layer_history {
            if layer_history.len() >= 5 {
                let recent_stats: Vec<_> = layer_history.iter().rev().take(5).collect();

                // Check for activation variance trends
                let variances: Vec<f64> = recent_stats.iter().map(|s| s.std_activation).collect();
                let avg_variance = variances.iter().sum::<f64>() / variances.len() as f64;

                if avg_variance < 0.01 {
                    issues_to_add.push(IdentifiedIssue {
                        category: IssueCategory::Architecture,
                        description: format!("Low activation variance in layer {}", layer_name),
                        severity: IssueSeverity::Minor,
                        confidence: 0.6,
                        evidence: vec![
                            format!("Average variance: {:.4}", avg_variance),
                            format!("Layer: {}", layer_name),
                        ],
                        potential_causes: vec![
                            "Poor weight initialization".to_string(),
                            "Input normalization too aggressive".to_string(),
                            "Activation function saturation".to_string(),
                        ],
                        identified_at: chrono::Utc::now(),
                    });
                }
            }
        }

        // Add all collected issues
        for issue in issues_to_add {
            self.add_issue(issue);
        }

        Ok(())
    }

    /// Analyze memory usage issues.
    fn analyze_memory_issues(&mut self) -> Result<()> {
        if self.performance_history.len() >= 10 {
            let recent_memory: Vec<f64> = self
                .performance_history
                .iter()
                .rev()
                .take(10)
                .map(|m| m.memory_usage_mb)
                .collect();

            // Check for memory leaks
            let memory_trend = self.calculate_trend(&recent_memory);
            if memory_trend > 10.0 {
                // MB per step
                self.add_issue(IdentifiedIssue {
                    category: IssueCategory::Memory,
                    description: "Memory leak detected".to_string(),
                    severity: IssueSeverity::Critical,
                    confidence: 0.9,
                    evidence: vec![
                        format!("Memory growth rate: {:.2} MB/step", memory_trend),
                        "Increasing memory usage trend".to_string(),
                    ],
                    potential_causes: vec![
                        "Gradient accumulation without clearing".to_string(),
                        "Cached tensors not being released".to_string(),
                        "Memory fragmentation".to_string(),
                    ],
                    identified_at: chrono::Utc::now(),
                });
            }

            // Check for excessive memory usage
            if let Some(max_memory) = recent_memory
                .iter()
                .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            {
                if *max_memory > 16384.0 {
                    // 16GB
                    self.add_issue(IdentifiedIssue {
                        category: IssueCategory::Memory,
                        description: "Excessive memory usage detected".to_string(),
                        severity: IssueSeverity::Major,
                        confidence: 0.8,
                        evidence: vec![
                            format!("Peak memory: {:.0} MB", max_memory),
                            "High memory consumption".to_string(),
                        ],
                        potential_causes: vec![
                            "Batch size too large".to_string(),
                            "Model too large for available memory".to_string(),
                            "Inefficient memory allocation".to_string(),
                        ],
                        identified_at: chrono::Utc::now(),
                    });
                }
            }
        }

        Ok(())
    }

    /// Analyze overfitting and underfitting issues.
    fn analyze_overfitting_underfitting(&mut self) -> Result<()> {
        let mut issues_to_add = Vec::new();

        if let Some(latest_dynamics) = self.dynamics_history.back() {
            // Check for overfitting indicators
            for indicator in &latest_dynamics.overfitting_indicators {
                if let super::types::OverfittingIndicator::TrainValidationGap { gap } = indicator {
                    if *gap > 0.1 {
                        issues_to_add.push(IdentifiedIssue {
                            category: IssueCategory::Overfitting,
                            description: "Large training-validation gap detected".to_string(),
                            severity: IssueSeverity::Major,
                            confidence: 0.85,
                            evidence: vec![
                                format!("Train-validation gap: {:.3}", gap),
                                "Overfitting indicator present".to_string(),
                            ],
                            potential_causes: vec![
                                "Model complexity too high".to_string(),
                                "Insufficient regularization".to_string(),
                                "Training set too small".to_string(),
                            ],
                            identified_at: chrono::Utc::now(),
                        });
                    }
                }
            }

            // Check for underfitting indicators
            for indicator in &latest_dynamics.underfitting_indicators {
                match indicator {
                    super::types::UnderfittingIndicator::HighTrainingLoss { loss, threshold } => {
                        issues_to_add.push(IdentifiedIssue {
                            category: IssueCategory::Underfitting,
                            description: "High training loss indicates underfitting".to_string(),
                            severity: IssueSeverity::Moderate,
                            confidence: 0.7,
                            evidence: vec![
                                format!("Training loss: {:.3}", loss),
                                format!("Threshold: {:.3}", threshold),
                            ],
                            potential_causes: vec![
                                "Model capacity too low".to_string(),
                                "Learning rate too low".to_string(),
                                "Insufficient training time".to_string(),
                            ],
                            identified_at: chrono::Utc::now(),
                        });
                    },
                    super::types::UnderfittingIndicator::SlowConvergence {
                        steps_taken,
                        expected,
                    } => {
                        issues_to_add.push(IdentifiedIssue {
                            category: IssueCategory::Underfitting,
                            description: "Slow convergence detected".to_string(),
                            severity: IssueSeverity::Minor,
                            confidence: 0.6,
                            evidence: vec![
                                format!("Steps taken: {}", steps_taken),
                                format!("Expected: {}", expected),
                            ],
                            potential_causes: vec![
                                "Learning rate too conservative".to_string(),
                                "Optimizer choice suboptimal".to_string(),
                                "Poor initialization".to_string(),
                            ],
                            identified_at: chrono::Utc::now(),
                        });
                    },
                    _ => {},
                }
            }
        }

        // Add all collected issues
        for issue in issues_to_add {
            self.add_issue(issue);
        }

        Ok(())
    }

    /// Generate recommendations based on identified issues.
    fn generate_recommendations(&mut self) -> Result<()> {
        for issue in &self.session_state.identified_issues {
            let recommendations = self.generate_recommendations_for_issue(issue);
            self.session_state.recommendations.extend(recommendations);
        }

        // Sort recommendations by priority and confidence
        self.session_state.recommendations.sort_by(|a, b| {
            b.priority
                .partial_cmp(&a.priority)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then(b.confidence.partial_cmp(&a.confidence).unwrap_or(std::cmp::Ordering::Equal))
        });

        Ok(())
    }

    /// Generate specific recommendations for an issue.
    fn generate_recommendations_for_issue(
        &self,
        issue: &IdentifiedIssue,
    ) -> Vec<DebuggingRecommendation> {
        match issue.category {
            IssueCategory::LearningRate => {
                if issue.description.contains("too high") {
                    vec![DebuggingRecommendation {
                        category: RecommendationCategory::HyperparameterTuning,
                        title: "Reduce Learning Rate".to_string(),
                        description: "Lower the learning rate to stabilize training".to_string(),
                        actions: vec![
                            "Reduce learning rate by factor of 2-10".to_string(),
                            "Enable gradient clipping".to_string(),
                            "Consider learning rate scheduling".to_string(),
                        ],
                        expected_impact: "Stabilized training with reduced loss oscillations"
                            .to_string(),
                        confidence: 0.9,
                        priority: AutoDebugRecommendationPriority::High,
                        hyperparameter_suggestions: vec![HyperparameterSuggestion {
                            parameter_name: "learning_rate".to_string(),
                            current_value: None,
                            suggested_value: 0.0001,
                            reasoning: "Reduce to prevent loss explosion".to_string(),
                            expected_effect: "More stable training".to_string(),
                        }],
                    }]
                } else if issue.description.contains("too low") {
                    vec![DebuggingRecommendation {
                        category: RecommendationCategory::HyperparameterTuning,
                        title: "Increase Learning Rate".to_string(),
                        description: "Increase learning rate to improve convergence speed"
                            .to_string(),
                        actions: vec![
                            "Increase learning rate by factor of 2-5".to_string(),
                            "Use learning rate warmup".to_string(),
                            "Consider adaptive learning rate methods".to_string(),
                        ],
                        expected_impact: "Faster convergence and better final performance"
                            .to_string(),
                        confidence: 0.8,
                        priority: AutoDebugRecommendationPriority::Medium,
                        hyperparameter_suggestions: vec![HyperparameterSuggestion {
                            parameter_name: "learning_rate".to_string(),
                            current_value: None,
                            suggested_value: 0.001,
                            reasoning: "Increase to improve learning speed".to_string(),
                            expected_effect: "Faster convergence".to_string(),
                        }],
                    }]
                } else {
                    Vec::new()
                }
            },
            IssueCategory::Memory => {
                vec![DebuggingRecommendation {
                    category: RecommendationCategory::ResourceOptimization,
                    title: "Optimize Memory Usage".to_string(),
                    description: "Implement memory optimization strategies".to_string(),
                    actions: vec![
                        "Reduce batch size".to_string(),
                        "Enable gradient checkpointing".to_string(),
                        "Clear cached tensors regularly".to_string(),
                        "Use mixed precision training".to_string(),
                    ],
                    expected_impact: "Reduced memory consumption and stable training".to_string(),
                    confidence: 0.85,
                    priority: AutoDebugRecommendationPriority::High,
                    hyperparameter_suggestions: vec![HyperparameterSuggestion {
                        parameter_name: "batch_size".to_string(),
                        current_value: None,
                        suggested_value: 16.0,
                        reasoning: "Reduce to lower memory usage".to_string(),
                        expected_effect: "Lower memory consumption".to_string(),
                    }],
                }]
            },
            IssueCategory::Overfitting => {
                vec![DebuggingRecommendation {
                    category: RecommendationCategory::TrainingStrategy,
                    title: "Address Overfitting".to_string(),
                    description: "Implement regularization strategies to reduce overfitting"
                        .to_string(),
                    actions: vec![
                        "Add dropout layers".to_string(),
                        "Increase weight decay".to_string(),
                        "Use data augmentation".to_string(),
                        "Reduce model complexity".to_string(),
                        "Implement early stopping".to_string(),
                    ],
                    expected_impact: "Better generalization and validation performance".to_string(),
                    confidence: 0.8,
                    priority: AutoDebugRecommendationPriority::Medium,
                    hyperparameter_suggestions: vec![HyperparameterSuggestion {
                        parameter_name: "dropout_rate".to_string(),
                        current_value: None,
                        suggested_value: 0.1,
                        reasoning: "Add regularization to reduce overfitting".to_string(),
                        expected_effect: "Better generalization".to_string(),
                    }],
                }]
            },
            IssueCategory::GradientFlow => {
                vec![DebuggingRecommendation {
                    category: RecommendationCategory::ArchitecturalModification,
                    title: "Improve Gradient Flow".to_string(),
                    description: "Address gradient flow issues in the network".to_string(),
                    actions: vec![
                        "Use different activation functions (e.g., Leaky ReLU, Swish)".to_string(),
                        "Add batch normalization".to_string(),
                        "Implement residual connections".to_string(),
                        "Adjust weight initialization".to_string(),
                    ],
                    expected_impact: "Better gradient flow and training stability".to_string(),
                    confidence: 0.75,
                    priority: AutoDebugRecommendationPriority::Medium,
                    hyperparameter_suggestions: Vec::new(),
                }]
            },
            _ => Vec::new(),
        }
    }

    /// Add an issue to the current session.
    fn add_issue(&mut self, issue: IdentifiedIssue) {
        self.session_state.identified_issues.push(issue);
    }

    /// Calculate variance of a sequence of values.
    fn calculate_variance(&self, values: &[f64]) -> f64 {
        if values.len() < 2 {
            return 0.0;
        }

        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance =
            values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (values.len() - 1) as f64;

        variance
    }

    /// Calculate trend (slope) of a sequence of values.
    fn calculate_trend(&self, values: &[f64]) -> f64 {
        if values.len() < 2 {
            return 0.0;
        }

        let n = values.len() as f64;
        let x_mean = (n - 1.0) / 2.0;
        let y_mean = values.iter().sum::<f64>() / n;

        let numerator: f64 = values
            .iter()
            .enumerate()
            .map(|(i, &y)| (i as f64 - x_mean) * (y - y_mean))
            .sum();

        let denominator: f64 = (0..values.len()).map(|i| (i as f64 - x_mean).powi(2)).sum();

        if denominator == 0.0 {
            0.0
        } else {
            numerator / denominator
        }
    }

    /// Update session statistics.
    fn update_session_statistics(&mut self) {
        let mut issues_by_category = HashMap::new();
        for issue in &self.session_state.identified_issues {
            *issues_by_category.entry(issue.category.clone()).or_insert(0) += 1;
        }

        let avg_confidence = if self.session_state.recommendations.is_empty() {
            0.0
        } else {
            self.session_state.recommendations.iter().map(|r| r.confidence).sum::<f64>()
                / self.session_state.recommendations.len() as f64
        };

        self.session_state.session_stats = SessionStatistics {
            total_issues: self.session_state.identified_issues.len(),
            issues_by_category,
            total_recommendations: self.session_state.recommendations.len(),
            avg_recommendation_confidence: avg_confidence,
            analysis_duration: self.session_state.session_stats.analysis_duration,
        };
    }

    /// Generate analysis summary.
    fn generate_analysis_summary(&self) -> String {
        let critical_issues = self
            .session_state
            .identified_issues
            .iter()
            .filter(|i| i.severity == IssueSeverity::Critical)
            .count();

        let major_issues = self
            .session_state
            .identified_issues
            .iter()
            .filter(|i| i.severity == IssueSeverity::Major)
            .count();

        let high_priority_recommendations = self
            .session_state
            .recommendations
            .iter()
            .filter(|r| r.priority == AutoDebugRecommendationPriority::High)
            .count();

        format!(
            "Auto-debugging analysis completed. Found {} critical issues, {} major issues. \
            Generated {} recommendations with {} high-priority actions. \
            Average recommendation confidence: {:.2}",
            critical_issues,
            major_issues,
            self.session_state.recommendations.len(),
            high_priority_recommendations,
            self.session_state.session_stats.avg_recommendation_confidence
        )
    }
}

/// Comprehensive debugging report.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DebuggingReport {
    /// Session information
    pub session_info: DebuggingSession,
    /// All identified issues
    pub identified_issues: Vec<IdentifiedIssue>,
    /// Generated recommendations
    pub recommendations: Vec<DebuggingRecommendation>,
    /// Analysis summary
    pub summary: String,
}

impl IssuePatternDatabase {
    /// Create a new pattern database with default patterns.
    pub fn new() -> Self {
        Self {
            learning_rate_patterns: Self::create_learning_rate_patterns(),
            gradient_patterns: Self::create_gradient_patterns(),
            convergence_patterns: Self::create_convergence_patterns(),
            layer_patterns: Self::create_layer_patterns(),
        }
    }

    /// Create default learning rate patterns.
    fn create_learning_rate_patterns() -> Vec<IssuePattern> {
        vec![IssuePattern {
            name: "Loss Explosion".to_string(),
            description: "Rapid increase in loss indicating learning rate too high".to_string(),
            conditions: vec![PatternCondition {
                metric: "loss".to_string(),
                operator: ComparisonOperator::Increasing,
                threshold: 2.0,
                consecutive_count: 3,
            }],
            issue_category: IssueCategory::LearningRate,
            confidence_weight: 0.9,
            solutions: vec![
                "Reduce learning rate by factor of 10".to_string(),
                "Enable gradient clipping".to_string(),
            ],
        }]
    }

    /// Create default gradient patterns.
    fn create_gradient_patterns() -> Vec<IssuePattern> {
        vec![]
    }

    /// Create default convergence patterns.
    fn create_convergence_patterns() -> Vec<IssuePattern> {
        vec![]
    }

    /// Create default layer patterns.
    fn create_layer_patterns() -> Vec<IssuePattern> {
        vec![]
    }
}

impl DebuggingSession {
    /// Create a new debugging session.
    fn new() -> Self {
        Self {
            session_start: chrono::Utc::now(),
            identified_issues: Vec::new(),
            recommendations: Vec::new(),
            session_stats: SessionStatistics {
                total_issues: 0,
                issues_by_category: HashMap::new(),
                total_recommendations: 0,
                avg_recommendation_confidence: 0.0,
                analysis_duration: chrono::Duration::zero(),
            },
        }
    }
}

impl Default for AutoDebugger {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_auto_debugger_creation() {
        let debugger = AutoDebugger::new();
        assert_eq!(debugger.performance_history.len(), 0);
        assert_eq!(debugger.layer_history.len(), 0);
    }

    #[test]
    fn test_issue_identification() {
        let mut debugger = AutoDebugger::new();

        let issue = IdentifiedIssue {
            category: IssueCategory::LearningRate,
            description: "Test issue".to_string(),
            severity: IssueSeverity::Major,
            confidence: 0.8,
            evidence: vec!["Test evidence".to_string()],
            potential_causes: vec!["Test cause".to_string()],
            identified_at: chrono::Utc::now(),
        };

        debugger.add_issue(issue);
        assert_eq!(debugger.session_state.identified_issues.len(), 1);
    }

    #[test]
    fn test_variance_calculation() {
        let debugger = AutoDebugger::new();
        let values = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let variance = debugger.calculate_variance(&values);
        assert!(variance > 0.0);
    }

    #[test]
    fn test_trend_calculation() {
        let debugger = AutoDebugger::new();
        let increasing_values = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let trend = debugger.calculate_trend(&increasing_values);
        assert!(trend > 0.0);
    }
}