Skip to main content

trustformers_debug/utilities/
mod.rs

1//! Utilities module for common debugging operations and helper functions
2//!
3//! This module provides a comprehensive set of debugging utilities organized into focused submodules:
4//!
5//! - `health`: Health checking and diagnostic utilities
6//! - `tensor_analysis`: Tensor analysis and statistical functions
7//! - `performance`: Performance monitoring and profiling tools
8//! - More modules to be added as the utilities.rs file is further refactored
9
10pub mod health;
11pub mod performance;
12pub mod tensor_analysis;
13pub mod weight_analysis;
14
15// Re-export main types and functions for backward compatibility
16pub use health::*;
17pub use performance::*;
18pub use tensor_analysis::*;
19pub use weight_analysis::*;
20
21use anyhow::Result;
22use scirs2_core::ndarray; // SciRS2 Integration Policy
23
24/// Common debugging utilities and helper functions
25pub struct DebugUtils;
26
27impl DebugUtils {
28    /// Quick model health check with automatic issue detection
29    pub async fn quick_health_check<T>(model: &T) -> Result<HealthCheckResult> {
30        HealthChecker::quick_health_check(model).await
31    }
32
33    /// Convert health score to status string
34    pub fn score_to_status(score: f64) -> String {
35        HealthChecker::score_to_status(score)
36    }
37
38    /// Generate debug report summary
39    pub fn generate_debug_summary(
40        config: &crate::DebugConfig,
41        results: &[crate::SimplifiedDebugResult],
42    ) -> DebugSummary {
43        HealthChecker::generate_debug_summary(config, results)
44    }
45
46    /// Export debug data in various formats
47    pub async fn export_debug_data(
48        session: &crate::DebugSession,
49        format: ExportFormat,
50        output_path: &str,
51    ) -> Result<String> {
52        HealthChecker::export_debug_data(session, format, output_path).await
53    }
54
55    /// Create a debug session template for common use cases
56    pub fn create_debug_template(template_type: DebugTemplate) -> crate::DebugConfig {
57        HealthChecker::create_debug_template(template_type)
58    }
59
60    /// Batch tensor analysis with statistical insights
61    pub fn analyze_tensors_batch(tensors: &[ndarray::ArrayD<f32>]) -> Result<BatchTensorAnalysis> {
62        TensorAnalyzer::analyze_tensors_batch(tensors)
63    }
64
65    /// Compute comprehensive statistics for a tensor
66    pub fn compute_tensor_statistics(tensor: &ndarray::ArrayD<f32>) -> Result<TensorStatistics> {
67        TensorAnalyzer::compute_tensor_statistics(tensor)
68    }
69
70    /// Detect anomalies in tensor statistics
71    pub fn detect_tensor_anomalies(stats: &TensorStatistics) -> Vec<TensorAnomaly> {
72        TensorAnalyzer::detect_tensor_anomalies(stats)
73    }
74
75    /// Compare tensors for drift detection
76    pub fn compare_tensors(
77        baseline: &ndarray::ArrayD<f32>,
78        current: &ndarray::ArrayD<f32>,
79    ) -> Result<TensorComparisonResult> {
80        TensorAnalyzer::compare_tensors(baseline, current)
81    }
82
83    /// Hash configuration for tracking
84    pub fn hash_config(config: &crate::DebugConfig) -> String {
85        use std::collections::hash_map::DefaultHasher;
86        use std::hash::{Hash, Hasher};
87
88        let mut hasher = DefaultHasher::new();
89        format!("{:?}", config).hash(&mut hasher);
90        format!("{:x}", hasher.finish())
91    }
92}
93
94/// Debugging convenience macros
95#[macro_export]
96macro_rules! debug_tensor {
97    ($session:expr, $tensor:expr, $name:expr) => {
98        $session.debug_tensor($tensor, $name)
99    };
100    ($tensor:expr) => {
101        trustformers_debug::utilities::TensorAnalyzer::compute_tensor_statistics($tensor)
102    };
103}
104
105#[macro_export]
106macro_rules! debug_gradient {
107    ($session:expr, $layer_name:expr, $gradients:expr) => {
108        $session.debug_gradients($layer_name, $gradients)
109    };
110}
111
112#[macro_export]
113macro_rules! quick_debug {
114    ($model:expr, $level:expr) => {
115        trustformers_debug::quick_debug($model, $level).await
116    };
117}
118
119#[macro_export]
120macro_rules! perf_checkpoint {
121    ($monitor:expr, $name:expr) => {
122        $monitor.checkpoint($name)
123    };
124}
125
126#[macro_export]
127macro_rules! perf_end_checkpoint {
128    ($monitor:expr, $name:expr) => {
129        $monitor.end_checkpoint($name)
130    };
131}