entrenar 0.7.11

Training & Optimization library with autograd, LoRA, quantization, and model merging
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
//! Hansei (反省) Post-Training Report Generator
//!
//! Toyota Way principle: Reflection and continuous improvement through
//! systematic analysis of training outcomes.
//!
//! Reference: Liker, J.K. (2004). The Toyota Way: 14 Management Principles.

use super::output::PostTrainingReport;
use super::types::{IssueSeverity, MetricSummary, TrainingIssue, Trend};
use crate::monitor::{Metric, MetricStats, MetricsCollector};
use std::collections::HashMap;
use std::fmt::Write as FmtWrite;

/// Hansei report generator
pub struct HanseiAnalyzer {
    /// Threshold for loss increase to trigger warning
    pub loss_increase_threshold: f64,
    /// Threshold for gradient norm to indicate explosion
    pub gradient_explosion_threshold: f64,
    /// Threshold for gradient norm to indicate vanishing
    pub gradient_vanishing_threshold: f64,
    /// Minimum expected accuracy improvement
    pub min_accuracy_improvement: f64,
}

impl Default for HanseiAnalyzer {
    fn default() -> Self {
        Self {
            loss_increase_threshold: 0.1, // 10% increase
            gradient_explosion_threshold: 100.0,
            gradient_vanishing_threshold: 1e-7,
            min_accuracy_improvement: 0.01, // 1% improvement
        }
    }
}

impl HanseiAnalyzer {
    pub fn new() -> Self {
        Self::default()
    }

    /// Analyze a completed training run and generate a report
    pub fn analyze(
        &self,
        training_id: &str,
        collector: &MetricsCollector,
        duration_secs: f64,
    ) -> PostTrainingReport {
        let mut issues = Vec::new();
        let mut recommendations = Vec::new();
        let mut metric_summaries = HashMap::new();
        let mut final_metrics = HashMap::new();

        let summary = collector.summary();
        let total_steps = summary.values().map(|s| s.count).sum::<usize>() as u64;

        // Analyze each metric
        for (metric, stats) in &summary {
            let metric_summary = self.analyze_metric(metric, stats);
            metric_summaries.insert(metric.clone(), metric_summary.clone());
            final_metrics.insert(metric.clone(), stats.mean);

            // Check for issues based on metric type
            self.check_metric_issues(metric, &metric_summary, stats, &mut issues);
        }

        // Generate recommendations based on issues
        self.generate_recommendations(&issues, &mut recommendations);

        // Check for missing expected metrics
        self.check_missing_metrics(&summary, &mut issues);

        // Sort issues by severity (critical first)
        issues.sort_by(|a, b| b.severity.cmp(&a.severity));

        PostTrainingReport {
            training_id: training_id.to_string(),
            duration_secs,
            total_steps,
            final_metrics,
            metric_summaries,
            issues,
            recommendations,
        }
    }

    fn analyze_metric(&self, metric: &Metric, stats: &MetricStats) -> MetricSummary {
        // Determine trend based on metric type and statistics
        let trend = self.determine_trend(metric, stats);

        MetricSummary {
            initial: stats.min,      // Approximation - would need history for actual initial
            final_value: stats.mean, // Approximation - would need last value
            min: stats.min,
            max: stats.max,
            mean: stats.mean,
            std_dev: stats.std,
            trend,
        }
    }

    fn determine_trend(&self, metric: &Metric, stats: &MetricStats) -> Trend {
        let cv = coeff_of_variation(stats);
        if cv > 0.5 {
            return Trend::Oscillating;
        }
        match metric {
            Metric::Loss => range_trend(stats, true),
            Metric::Accuracy => range_trend(stats, false),
            Metric::GradientNorm => {
                if cv < 0.2 {
                    Trend::Stable
                } else {
                    Trend::Oscillating
                }
            }
            Metric::LearningRate | Metric::Epoch | Metric::Batch | Metric::Custom(_) => {
                Trend::Stable
            }
        }
    }
}

fn coeff_of_variation(stats: &MetricStats) -> f64 {
    if stats.mean.abs() > 1e-10 {
        stats.std / stats.mean.abs()
    } else {
        0.0
    }
}

/// Determine trend based on whether mean is above or below midpoint.
/// `lower_is_better` = true for loss, false for accuracy.
fn range_trend(stats: &MetricStats, lower_is_better: bool) -> Trend {
    if stats.max - stats.min < stats.std * 0.5 {
        return Trend::Stable;
    }
    let mid = f64::midpoint(stats.min, stats.max);
    let improving = if lower_is_better { stats.mean < mid } else { stats.mean > mid };
    if improving {
        Trend::Improving
    } else {
        Trend::Degrading
    }
}

impl HanseiAnalyzer {
    fn check_metric_issues(
        &self,
        metric: &Metric,
        summary: &MetricSummary,
        stats: &MetricStats,
        issues: &mut Vec<TrainingIssue>,
    ) {
        match metric {
            Metric::Loss => self.check_loss_issues(summary, stats, issues),
            Metric::Accuracy => self.check_accuracy_issues(summary, stats, issues),
            Metric::GradientNorm => self.check_gradient_issues(stats, issues),
            Metric::LearningRate => self.check_lr_issues(summary, issues),
            Metric::Epoch | Metric::Batch | Metric::Custom(_) => {}
        }
    }

    /// Check loss metric for NaN/Inf, degrading trend, and oscillation.
    fn check_loss_issues(
        &self,
        summary: &MetricSummary,
        stats: &MetricStats,
        issues: &mut Vec<TrainingIssue>,
    ) {
        if stats.has_nan {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Critical,
                category: "Numerical Stability".to_string(),
                description: "NaN values detected in loss".to_string(),
                recommendation:
                    "Reduce learning rate, add gradient clipping, or check data preprocessing"
                        .to_string(),
            });
        }
        if stats.has_inf {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Critical,
                category: "Numerical Stability".to_string(),
                description: "Infinity values detected in loss".to_string(),
                recommendation: "Check for division by zero, reduce learning rate".to_string(),
            });
        }
        if summary.trend == Trend::Degrading {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Warning,
                category: "Convergence".to_string(),
                description: "Loss appears to be increasing over training".to_string(),
                recommendation: "Consider reducing learning rate or checking data quality"
                    .to_string(),
            });
        }
        if summary.trend == Trend::Oscillating {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Warning,
                category: "Stability".to_string(),
                description: "Loss is oscillating significantly".to_string(),
                recommendation: "Reduce learning rate or increase batch size".to_string(),
            });
        }
    }

    /// Check accuracy metric for low values and stagnation.
    fn check_accuracy_issues(
        &self,
        summary: &MetricSummary,
        stats: &MetricStats,
        issues: &mut Vec<TrainingIssue>,
    ) {
        if summary.final_value < 0.5 && stats.count > 100 {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Warning,
                category: "Performance".to_string(),
                description: format!("Final accuracy is low: {:.2}%", summary.final_value * 100.0),
                recommendation: "Consider model architecture changes or hyperparameter tuning"
                    .to_string(),
            });
        }
        if summary.trend == Trend::Stable
            && summary.max - summary.min < self.min_accuracy_improvement
        {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Info,
                category: "Convergence".to_string(),
                description: "Accuracy shows minimal improvement".to_string(),
                recommendation: "Model may have converged or may be stuck in local minimum"
                    .to_string(),
            });
        }
    }

    /// Check gradient norms for explosion and vanishing.
    fn check_gradient_issues(&self, stats: &MetricStats, issues: &mut Vec<TrainingIssue>) {
        if stats.max > self.gradient_explosion_threshold {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Error,
                category: "Gradient Health".to_string(),
                description: format!("Gradient explosion detected: max norm = {:.2e}", stats.max),
                recommendation: "Enable gradient clipping (e.g., max_norm=1.0)".to_string(),
            });
        }
        if stats.mean < self.gradient_vanishing_threshold && stats.count > 10 {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Warning,
                category: "Gradient Health".to_string(),
                description: format!(
                    "Possible vanishing gradients: mean norm = {:.2e}",
                    stats.mean
                ),
                recommendation:
                    "Consider using residual connections or different activation functions"
                        .to_string(),
            });
        }
    }

    /// Check learning rate schedule for high variance.
    fn check_lr_issues(&self, summary: &MetricSummary, issues: &mut Vec<TrainingIssue>) {
        if summary.std_dev > summary.mean * 0.5 {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Info,
                category: "Hyperparameters".to_string(),
                description: "Learning rate schedule shows high variance".to_string(),
                recommendation: "Review learning rate schedule configuration".to_string(),
            });
        }
    }

    fn check_missing_metrics(
        &self,
        metrics: &HashMap<Metric, MetricStats>,
        issues: &mut Vec<TrainingIssue>,
    ) {
        // Check for essential metrics
        if !metrics.contains_key(&Metric::Loss) {
            issues.push(TrainingIssue {
                severity: IssueSeverity::Warning,
                category: "Observability".to_string(),
                description: "No loss metric recorded".to_string(),
                recommendation: "Ensure loss is being tracked for proper monitoring".to_string(),
            });
        }
    }

    fn generate_recommendations(
        &self,
        issues: &[TrainingIssue],
        recommendations: &mut Vec<String>,
    ) {
        let has_numerical_issues = issues.iter().any(|i| i.category == "Numerical Stability");
        let has_gradient_issues = issues.iter().any(|i| i.category == "Gradient Health");
        let has_convergence_issues = issues.iter().any(|i| i.category == "Convergence");

        if has_numerical_issues {
            recommendations.push(
                "Priority 1: Address numerical stability before continuing training".to_string(),
            );
        }

        if has_gradient_issues {
            recommendations.push("Enable gradient clipping in optimizer configuration".to_string());
        }

        if has_convergence_issues {
            recommendations.push(
                "Consider hyperparameter search for learning rate and batch size".to_string(),
            );
        }

        if issues.is_empty() {
            recommendations.push(
                "Training completed without detected issues. Consider running validation tests."
                    .to_string(),
            );
        }
    }

    /// Generate a human-readable report
    pub fn format_report(&self, report: &PostTrainingReport) -> String {
        let mut output = String::new();

        // Writing to String never fails, so we ignore the Result
        let _ = writeln!(output, "═══════════════════════════════════════════════════════════════");
        let _ =
            writeln!(output, "                    HANSEI POST-TRAINING REPORT                 ");
        let _ = writeln!(output, "═══════════════════════════════════════════════════════════════");
        let _ = writeln!(output);
        let _ = writeln!(output, "Training ID: {}", report.training_id);
        let _ = writeln!(output, "Duration: {:.2}s", report.duration_secs);
        let _ = writeln!(output, "Total Steps: {}", report.total_steps);
        let _ = writeln!(output);

        // Metric summaries
        let _ =
            writeln!(output, "─── Metric Summaries ───────────────────────────────────────────");
        for (metric_type, summary) in &report.metric_summaries {
            let _ = writeln!(output, "\n{metric_type:?}:");
            let _ = writeln!(output, "  Mean: {:.6}  Std: {:.6}", summary.mean, summary.std_dev);
            let _ = writeln!(output, "  Min: {:.6}   Max: {:.6}", summary.min, summary.max);
            let _ = writeln!(output, "  Trend: {}", summary.trend);
        }
        let _ = writeln!(output);

        // Issues
        if !report.issues.is_empty() {
            let _ = writeln!(
                output,
                "─── Issues Detected ────────────────────────────────────────────"
            );
            for issue in &report.issues {
                let _ = writeln!(output, "\n[{}] {}", issue.severity, issue.category);
                let _ = writeln!(output, "  {}", issue.description);
                let _ = writeln!(output, "{}", issue.recommendation);
            }
            let _ = writeln!(output);
        }

        // Recommendations
        let _ =
            writeln!(output, "─── Recommendations ────────────────────────────────────────────");
        for (i, rec) in report.recommendations.iter().enumerate() {
            let _ = writeln!(output, "{}. {}", i + 1, rec);
        }
        let _ = writeln!(output);

        let _ = writeln!(output, "═══════════════════════════════════════════════════════════════");

        output
    }
}

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

    #[test]
    fn test_determine_trend_all_metric_variants() {
        let analyzer = HanseiAnalyzer::default();

        // Stable stats (low CV, narrow range)
        let stable_stats = MetricStats {
            count: 100,
            mean: 1.0,
            std: 0.01,
            min: 0.99,
            max: 1.01,
            sum: 100.0,
            has_nan: false,
            has_inf: false,
        };

        // Syntactic match covering all arms from determine_trend
        let metrics = [
            Metric::Loss,
            Metric::Accuracy,
            Metric::GradientNorm,
            Metric::LearningRate,
            Metric::Epoch,
            Metric::Batch,
            Metric::Custom("custom_metric".to_string()),
        ];

        for metric in &metrics {
            let trend = analyzer.determine_trend(metric, &stable_stats);
            match metric {
                Metric::Loss => {
                    assert!(matches!(
                        trend,
                        Trend::Stable | Trend::Improving | Trend::Degrading | Trend::Oscillating
                    ));
                }
                Metric::Accuracy => {
                    assert!(matches!(
                        trend,
                        Trend::Stable | Trend::Improving | Trend::Degrading | Trend::Oscillating
                    ));
                }
                Metric::GradientNorm => {
                    assert!(matches!(trend, Trend::Stable | Trend::Oscillating));
                }
                Metric::LearningRate | Metric::Epoch | Metric::Batch | Metric::Custom(_) => {
                    assert_eq!(trend, Trend::Stable);
                }
            }
        }
    }

    #[test]
    fn test_check_metric_issues_all_metric_variants() {
        let analyzer = HanseiAnalyzer::default();

        let stats = MetricStats {
            count: 200,
            mean: 0.5,
            std: 0.1,
            min: 0.3,
            max: 0.7,
            sum: 100.0,
            has_nan: false,
            has_inf: false,
        };

        let summary = MetricSummary {
            initial: 0.3,
            final_value: 0.5,
            min: 0.3,
            max: 0.7,
            mean: 0.5,
            std_dev: 0.1,
            trend: Trend::Stable,
        };

        let metrics = [
            Metric::Loss,
            Metric::Accuracy,
            Metric::GradientNorm,
            Metric::LearningRate,
            Metric::Epoch,
            Metric::Batch,
            Metric::Custom("test".to_string()),
        ];

        for metric in &metrics {
            let mut issues = Vec::new();
            analyzer.check_metric_issues(metric, &summary, &stats, &mut issues);

            // Syntactic match covering all arms from check_metric_issues
            match metric {
                Metric::Loss => {
                    // Loss branch checks NaN, Inf, trend
                }
                Metric::Accuracy => {
                    // Accuracy branch checks low accuracy, no improvement
                }
                Metric::GradientNorm => {
                    // GradientNorm branch checks explosion, vanishing
                }
                Metric::LearningRate => {
                    // LearningRate branch checks variance
                }
                Metric::Epoch | Metric::Batch | Metric::Custom(_) => {
                    // No-op branch
                    assert!(issues.is_empty(), "Epoch/Batch/Custom should produce no issues");
                }
            }
        }
    }

    #[test]
    fn test_analyzer_default() {
        let analyzer = HanseiAnalyzer::default();
        assert!((analyzer.loss_increase_threshold - 0.1).abs() < 1e-10);
        assert!((analyzer.gradient_explosion_threshold - 100.0).abs() < 1e-10);
        assert!((analyzer.gradient_vanishing_threshold - 1e-7).abs() < 1e-15);
        assert!((analyzer.min_accuracy_improvement - 0.01).abs() < 1e-10);
    }

    #[test]
    fn test_analyzer_new() {
        let analyzer = HanseiAnalyzer::new();
        assert!((analyzer.loss_increase_threshold - 0.1).abs() < 1e-10);
    }
}