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
//! Tensor analysis utilities and statistical functions

use anyhow::Result;
use scirs2_core::ndarray::*; // SciRS2 Integration Policy - was: use ndarray::{Array, ArrayD};
use serde::{Deserialize, Serialize};

/// Batch tensor analysis result
#[derive(Debug, Serialize, Deserialize)]
pub struct BatchTensorAnalysis {
    pub individual_results: Vec<TensorAnalysisResult>,
    pub overall_statistics: TensorStatistics,
    pub batch_size: usize,
    pub analysis_timestamp: chrono::DateTime<chrono::Utc>,
}

/// Individual tensor analysis result
#[derive(Debug, Serialize, Deserialize)]
pub struct TensorAnalysisResult {
    pub tensor_index: usize,
    pub shape: Vec<usize>,
    pub statistics: TensorStatistics,
    pub anomalies: Vec<TensorAnomaly>,
}

/// Comprehensive tensor statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TensorStatistics {
    pub count: usize,
    pub mean: f32,
    pub std_dev: f32,
    pub min: f32,
    pub max: f32,
    pub median: f32,
    pub p25: f32,
    pub p75: f32,
    pub nan_count: usize,
    pub inf_count: usize,
    pub zero_count: usize,
    pub skewness: f32,
    pub kurtosis: f32,
}

impl Default for TensorStatistics {
    fn default() -> Self {
        Self {
            count: 0,
            mean: 0.0,
            std_dev: 0.0,
            min: 0.0,
            max: 0.0,
            median: 0.0,
            p25: 0.0,
            p75: 0.0,
            nan_count: 0,
            inf_count: 0,
            zero_count: 0,
            skewness: 0.0,
            kurtosis: 0.0,
        }
    }
}

impl TensorStatistics {
    pub fn accumulate(&mut self, other: &TensorStatistics) {
        self.count += other.count;
        self.mean += other.mean;
        self.std_dev += other.std_dev;
        self.min = self.min.min(other.min);
        self.max = self.max.max(other.max);
        self.nan_count += other.nan_count;
        self.inf_count += other.inf_count;
        self.zero_count += other.zero_count;
    }

    pub fn finalize(&mut self, batch_size: usize) {
        if batch_size > 0 {
            self.mean /= batch_size as f32;
            self.std_dev /= batch_size as f32;
        }
    }
}

/// Tensor anomaly detection result
#[derive(Debug, Serialize, Deserialize)]
pub struct TensorAnomaly {
    pub anomaly_type: AnomalyType,
    pub severity: AnomalySeverity,
    pub description: String,
    pub suggested_fix: String,
}

/// Types of tensor anomalies
#[derive(Debug, Serialize, Deserialize)]
pub enum AnomalyType {
    NanValues,
    InfiniteValues,
    ExtremeSkewness,
    ExtremeKurtosis,
    DeadNeurons,
    ExtremeValues,
    Saturation,
    Outliers,
}

/// Severity levels for anomalies
#[derive(Debug, Serialize, Deserialize)]
pub enum AnomalySeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Advanced tensor analysis utilities
pub struct TensorAnalyzer;

impl TensorAnalyzer {
    /// Batch tensor analysis with statistical insights
    pub fn analyze_tensors_batch(tensors: &[ArrayD<f32>]) -> Result<BatchTensorAnalysis> {
        let mut results = Vec::new();
        let mut overall_stats = TensorStatistics::default();

        for (i, tensor) in tensors.iter().enumerate() {
            let stats = Self::compute_tensor_statistics(tensor)?;
            let anomalies = Self::detect_tensor_anomalies(&stats);

            results.push(TensorAnalysisResult {
                tensor_index: i,
                shape: tensor.shape().to_vec(),
                statistics: stats.clone(),
                anomalies,
            });

            overall_stats.accumulate(&stats);
        }

        overall_stats.finalize(tensors.len());

        Ok(BatchTensorAnalysis {
            individual_results: results,
            overall_statistics: overall_stats,
            batch_size: tensors.len(),
            analysis_timestamp: chrono::Utc::now(),
        })
    }

    /// Compute comprehensive statistics for a tensor
    pub fn compute_tensor_statistics(tensor: &ArrayD<f32>) -> Result<TensorStatistics> {
        let data: Vec<f32> = tensor.iter().cloned().collect();
        let count = data.len();

        if count == 0 {
            return Ok(TensorStatistics::default());
        }

        // Basic statistics
        let sum: f32 = data.iter().sum();
        let mean = sum / count as f32;

        let variance = data.iter().map(|x| (x - mean).powi(2)).sum::<f32>() / count as f32;
        let std_dev = variance.sqrt();

        // Min/max
        let min = data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
        let max = data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));

        // Percentiles
        let mut sorted_data = data.clone();
        sorted_data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let median = Self::percentile(&sorted_data, 50.0);
        let p25 = Self::percentile(&sorted_data, 25.0);
        let p75 = Self::percentile(&sorted_data, 75.0);

        // Count special values
        let nan_count = data.iter().filter(|&&x| x.is_nan()).count();
        let inf_count = data.iter().filter(|&&x| x.is_infinite()).count();
        let zero_count = data.iter().filter(|&&x| x == 0.0).count();

        // Higher order moments
        let skewness = Self::compute_skewness(&data, mean, std_dev);
        let kurtosis = Self::compute_kurtosis(&data, mean, std_dev);

        Ok(TensorStatistics {
            count,
            mean,
            std_dev,
            min,
            max,
            median,
            p25,
            p75,
            nan_count,
            inf_count,
            zero_count,
            skewness,
            kurtosis,
        })
    }

    /// Detect anomalies in tensor statistics
    pub fn detect_tensor_anomalies(stats: &TensorStatistics) -> Vec<TensorAnomaly> {
        let mut anomalies = Vec::new();

        // Check for NaN values
        if stats.nan_count > 0 {
            anomalies.push(TensorAnomaly {
                anomaly_type: AnomalyType::NanValues,
                severity: AnomalySeverity::Critical,
                description: format!("Found {} NaN values in tensor", stats.nan_count),
                suggested_fix: "Check for division by zero or invalid operations".to_string(),
            });
        }

        // Check for infinite values
        if stats.inf_count > 0 {
            anomalies.push(TensorAnomaly {
                anomaly_type: AnomalyType::InfiniteValues,
                severity: AnomalySeverity::High,
                description: format!("Found {} infinite values in tensor", stats.inf_count),
                suggested_fix: "Check for overflow or division by zero".to_string(),
            });
        }

        // Check for extreme skewness
        if stats.skewness.abs() > 3.0 {
            anomalies.push(TensorAnomaly {
                anomaly_type: AnomalyType::ExtremeSkewness,
                severity: AnomalySeverity::Medium,
                description: format!("Extreme skewness detected: {:.2}", stats.skewness),
                suggested_fix: "Consider data normalization or outlier removal".to_string(),
            });
        }

        // Check for extreme kurtosis
        if stats.kurtosis > 10.0 {
            anomalies.push(TensorAnomaly {
                anomaly_type: AnomalyType::ExtremeKurtosis,
                severity: AnomalySeverity::Medium,
                description: format!("High kurtosis detected: {:.2}", stats.kurtosis),
                suggested_fix: "Check for outliers or distribution issues".to_string(),
            });
        }

        // Check for dead neurons (too many zeros)
        let zero_ratio = stats.zero_count as f32 / stats.count as f32;
        if zero_ratio > 0.5 {
            anomalies.push(TensorAnomaly {
                anomaly_type: AnomalyType::DeadNeurons,
                severity: AnomalySeverity::High,
                description: format!("High zero ratio: {:.2}%", zero_ratio * 100.0),
                suggested_fix:
                    "Check learning rate, weight initialization, or activation functions"
                        .to_string(),
            });
        }

        // Check for extreme values
        let range = stats.max - stats.min;
        if range > 1000.0 || stats.max.abs() > 100.0 || stats.min.abs() > 100.0 {
            anomalies.push(TensorAnomaly {
                anomaly_type: AnomalyType::ExtremeValues,
                severity: AnomalySeverity::Medium,
                description: format!("Extreme value range: [{:.2}, {:.2}]", stats.min, stats.max),
                suggested_fix: "Consider gradient clipping or weight regularization".to_string(),
            });
        }

        anomalies
    }

    /// Calculate percentile of sorted data
    fn percentile(sorted_data: &[f32], percentile: f32) -> f32 {
        if sorted_data.is_empty() {
            return 0.0;
        }

        let index = (percentile / 100.0) * (sorted_data.len() - 1) as f32;
        let lower_index = index.floor() as usize;
        let upper_index = (index.ceil() as usize).min(sorted_data.len() - 1);

        if lower_index == upper_index {
            sorted_data[lower_index]
        } else {
            let weight = index - lower_index as f32;
            sorted_data[lower_index] * (1.0 - weight) + sorted_data[upper_index] * weight
        }
    }

    /// Compute skewness
    fn compute_skewness(data: &[f32], mean: f32, std_dev: f32) -> f32 {
        if std_dev == 0.0 || data.len() < 3 {
            return 0.0;
        }

        let n = data.len() as f32;
        let skewness = data.iter().map(|&x| ((x - mean) / std_dev).powi(3)).sum::<f32>() / n;

        skewness
    }

    /// Compute kurtosis
    fn compute_kurtosis(data: &[f32], mean: f32, std_dev: f32) -> f32 {
        if std_dev == 0.0 || data.len() < 4 {
            return 0.0;
        }

        let n = data.len() as f32;
        let kurtosis = data.iter().map(|&x| ((x - mean) / std_dev).powi(4)).sum::<f32>() / n;

        kurtosis - 3.0 // Excess kurtosis
    }

    /// Compare tensors for drift detection
    pub fn compare_tensors(
        baseline: &ArrayD<f32>,
        current: &ArrayD<f32>,
    ) -> Result<TensorComparisonResult> {
        let baseline_stats = Self::compute_tensor_statistics(baseline)?;
        let current_stats = Self::compute_tensor_statistics(current)?;

        // Calculate various drift metrics
        let mean_drift = (current_stats.mean - baseline_stats.mean).abs();
        let std_drift = (current_stats.std_dev - baseline_stats.std_dev).abs();
        let distribution_shift = Self::compute_distribution_shift(&baseline_stats, &current_stats);

        let drift_severity = if mean_drift > 1.0 || std_drift > 1.0 || distribution_shift > 0.5 {
            TensorDriftSeverity::High
        } else if mean_drift > 0.5 || std_drift > 0.5 || distribution_shift > 0.3 {
            TensorDriftSeverity::Medium
        } else {
            TensorDriftSeverity::Low
        };

        Ok(TensorComparisonResult {
            baseline_stats,
            current_stats,
            mean_drift,
            std_drift,
            distribution_shift,
            drift_severity: drift_severity.clone(),
            recommendations: Self::generate_drift_recommendations(
                drift_severity,
                mean_drift,
                std_drift,
            ),
        })
    }

    /// Compute distribution shift between two sets of statistics
    fn compute_distribution_shift(baseline: &TensorStatistics, current: &TensorStatistics) -> f32 {
        // Simple distribution shift metric based on statistical differences
        let mean_diff = ((current.mean - baseline.mean) / (baseline.std_dev + 1e-8)).abs();
        let std_diff = ((current.std_dev - baseline.std_dev) / (baseline.std_dev + 1e-8)).abs();
        let skew_diff = (current.skewness - baseline.skewness).abs();

        (mean_diff + std_diff + skew_diff * 0.5) / 2.5
    }

    /// Generate recommendations based on drift severity
    fn generate_drift_recommendations(
        severity: TensorDriftSeverity,
        mean_drift: f32,
        std_drift: f32,
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        match severity {
            TensorDriftSeverity::High => {
                recommendations.push("Significant tensor drift detected".to_string());
                if mean_drift > 1.0 {
                    recommendations.push("Consider retraining or data rebalancing".to_string());
                }
                if std_drift > 1.0 {
                    recommendations.push("Check for changes in data preprocessing".to_string());
                }
            },
            TensorDriftSeverity::Medium => {
                recommendations.push("Moderate tensor drift detected".to_string());
                recommendations.push("Monitor closely for further changes".to_string());
            },
            TensorDriftSeverity::Low => {
                recommendations.push("Minimal tensor drift - within acceptable range".to_string());
            },
        }

        recommendations
    }
}

/// Result of tensor comparison for drift detection
#[derive(Debug, Serialize, Deserialize)]
pub struct TensorComparisonResult {
    pub baseline_stats: TensorStatistics,
    pub current_stats: TensorStatistics,
    pub mean_drift: f32,
    pub std_drift: f32,
    pub distribution_shift: f32,
    pub drift_severity: TensorDriftSeverity,
    pub recommendations: Vec<String>,
}

/// Drift severity levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TensorDriftSeverity {
    Low,
    Medium,
    High,
}