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
//! Comprehensive integration tests for TrustformeRS Debug framework

use anyhow::Result;
use scirs2_core::ndarray::{Array, IxDyn};
use std::time::Duration;
use tokio::time::sleep;
use uuid::Uuid;

use trustformers_debug::model_diagnostics::ModelArchitectureInfo;
use trustformers_debug::*;

/// Test basic debugging session workflow
#[tokio::test]
async fn test_basic_debugging_workflow() -> Result<()> {
    let config = DebugConfig {
        enable_tensor_inspection: true,
        enable_gradient_debugging: true,
        enable_model_diagnostics: true,
        enable_visualization: false,    // Disable for testing
        enable_memory_profiling: false, // Disable for testing
        enable_computation_graph_analysis: true,
        max_tracked_tensors: 100,
        max_gradient_history: 50,
        output_dir: Some("./test_debug_output".to_string()),
        sampling_rate: 1.0,
        ..Default::default()
    };

    let mut debug_session = DebugSession::new(config);

    // Start session
    debug_session.start().await?;

    // Create test tensors with different means for MSE comparison
    let input_tensor = Array::linspace(0.0, 1.0, 100)
        .to_shape(IxDyn(&[10, 10]))
        .expect("operation failed in test")
        .to_owned();
    let weight_tensor = Array::<f32, _>::ones(IxDyn(&[10, 10])) * 0.8; // Different mean from input (0.8 vs 0.5)

    // Inspect tensors
    let input_id = debug_session.tensor_inspector_mut().inspect_tensor(
        &input_tensor,
        "input_test",
        Some("test_layer"),
        Some("forward"),
    )?;

    let weight_id = debug_session.tensor_inspector_mut().inspect_tensor(
        &weight_tensor,
        "weights_test",
        Some("test_layer"),
        Some("parameter"),
    )?;

    // Record gradient flow
    let gradient_values: Vec<f64> = weight_tensor.iter().map(|&x| x as f64 * 0.01).collect();
    let gradient_norm = (gradient_values.iter().map(|&x| x * x).sum::<f64>()).sqrt();
    let gradient_mean = gradient_values.iter().sum::<f64>() / gradient_values.len() as f64;
    let gradient_variance =
        gradient_values.iter().map(|&x| (x - gradient_mean).powi(2)).sum::<f64>()
            / gradient_values.len() as f64;
    let gradient_std = gradient_variance.sqrt();

    debug_session.gradient_debugger_mut().record_gradient_flow(
        "test_layer",
        gradient_norm,
        gradient_mean,
        gradient_std,
    )?;

    debug_session.gradient_debugger_mut().next_step();

    // Compare tensors
    let comparison = debug_session.tensor_inspector_mut().compare_tensors(input_id, weight_id)?;
    assert!(comparison.shape_match); // Same shape [10, 10]
    assert!(comparison.mse > 0.0); // Different values, MSE should be > 0

    // Generate report
    let report = debug_session.stop().await?;

    // Verify report contents
    assert!(report.tensor_report.is_some());
    assert!(report.gradient_report.is_some());

    if let Some(ref tensor_report) = report.tensor_report {
        assert_eq!(tensor_report.total_tensors, 2);
    }

    Ok(())
}

/// Test simplified debugging interface
#[tokio::test]
async fn test_simplified_debugging_interface() -> Result<()> {
    // Mock model for testing
    let mock_model = "test_model";

    // Test different debug levels
    let light_result = quick_debug(&mock_model, QuickDebugLevel::Light).await?;
    assert!(matches!(light_result, SimplifiedDebugResult::Light(_)));

    let standard_result = quick_debug(&mock_model, QuickDebugLevel::Standard).await?;
    assert!(matches!(
        standard_result,
        SimplifiedDebugResult::Standard { .. }
    ));

    let production_result = quick_debug(&mock_model, QuickDebugLevel::Production).await?;
    assert!(matches!(
        production_result,
        SimplifiedDebugResult::Production(_)
    ));

    // Test convenience function
    let debug_result = debug(&mock_model).await?;
    assert!(matches!(
        debug_result,
        SimplifiedDebugResult::Standard { .. }
    ));

    // Test result methods
    let summary = debug_result.summary();
    assert!(!summary.is_empty());

    let _recommendations = debug_result.recommendations();
    // Recommendations vector is valid

    Ok(())
}

/// Test utilities module functionality
#[tokio::test]
async fn test_utilities_functionality() -> Result<()> {
    // Test tensor statistics computation
    let test_tensor = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0])
        .to_shape(IxDyn(&[5]))
        .expect("operation failed in test")
        .to_owned();

    let stats = DebugUtils::compute_tensor_statistics(&test_tensor)?;

    assert_eq!(stats.count, 5);
    assert!((stats.mean - 3.0).abs() < 0.01); // Mean should be 3.0
    assert!(stats.min == 1.0);
    assert!(stats.max == 5.0);
    assert_eq!(stats.nan_count, 0);
    assert_eq!(stats.inf_count, 0);

    // Test anomaly detection
    let anomalies = DebugUtils::detect_tensor_anomalies(&stats);
    assert!(anomalies.is_empty()); // Normal tensor should have no anomalies

    // Test tensor with NaN values
    let nan_tensor = Array::from_vec(vec![1.0, f32::NAN, 3.0, 4.0, 5.0])
        .to_shape(IxDyn(&[5]))
        .expect("operation failed in test")
        .to_owned();

    let nan_stats = DebugUtils::compute_tensor_statistics(&nan_tensor)?;
    let nan_anomalies = DebugUtils::detect_tensor_anomalies(&nan_stats);

    assert_eq!(nan_stats.nan_count, 1);
    assert!(!nan_anomalies.is_empty()); // Should detect NaN anomaly

    Ok(())
}

/// Test batch tensor analysis
#[tokio::test]
async fn test_batch_tensor_analysis() -> Result<()> {
    let tensors = vec![
        Array::ones(IxDyn(&[5, 5])) * 0.5,
        Array::zeros(IxDyn(&[5, 5])),
        Array::from_elem(IxDyn(&[5, 5]), 2.0),
    ];

    let batch_analysis = DebugUtils::analyze_tensors_batch(&tensors)?;

    assert_eq!(batch_analysis.batch_size, 3);
    assert_eq!(batch_analysis.individual_results.len(), 3);

    // Check individual results
    for (i, result) in batch_analysis.individual_results.iter().enumerate() {
        assert_eq!(result.tensor_index, i);
        assert_eq!(result.shape, vec![5, 5]);
        assert_eq!(result.statistics.count, 25);
    }

    // First tensor should have mean ~0.5
    assert!((batch_analysis.individual_results[0].statistics.mean - 0.5).abs() < 0.01);

    // Second tensor should have mean ~0.0
    assert!((batch_analysis.individual_results[1].statistics.mean - 0.0).abs() < 0.01);

    // Third tensor should have mean ~2.0
    assert!((batch_analysis.individual_results[2].statistics.mean - 2.0).abs() < 0.01);

    Ok(())
}

/// Test guided debugging workflow
#[tokio::test]
async fn test_guided_debugging() -> Result<()> {
    let mut guided_debugger = GuidedDebugger::new();

    assert_eq!(guided_debugger.total_steps(), 6); // Should have 6 predefined steps
    assert_eq!(guided_debugger.progress(), 0.0);
    assert!(!guided_debugger.is_complete());

    // Execute first step
    if let Some(step) = guided_debugger.current_step() {
        assert_eq!(step.name, "Health Check");
    }

    let result = guided_debugger.execute_current_step().await?;
    assert!(matches!(result, StepResult::Health(_)));

    assert!(guided_debugger.progress() > 0.0);

    // Skip to end
    while !guided_debugger.is_complete() {
        guided_debugger.skip_current_step()?;
    }

    assert!(guided_debugger.is_complete());
    assert_eq!(guided_debugger.progress(), 100.0);

    Ok(())
}

/// Test tutorial system
#[tokio::test]
async fn test_tutorial_system() -> Result<()> {
    let mut tutorial = TutorialMode::new();

    assert!(tutorial.total_lessons() > 0);
    assert_eq!(tutorial.progress(), 0.0);
    assert!(!tutorial.is_complete());

    // Get current lesson
    if let Some(lesson) = tutorial.current_lesson() {
        assert!(!lesson.title.is_empty());
        assert!(!lesson.description.is_empty());
        assert!(!lesson.example_code.is_empty());
    }

    // Complete first lesson
    tutorial.complete_current_lesson()?;
    assert!(tutorial.progress() > 0.0);

    // Test navigation
    tutorial.goto_lesson(0)?;
    assert_eq!(
        tutorial.current_lesson().expect("operation failed in test").title,
        "Getting Started with TrustformeRS Debug"
    );

    Ok(())
}

/// Test context help system
#[tokio::test]
async fn test_context_help() -> Result<()> {
    let help_system = context_help();

    // Test getting specific help
    let debug_help = help_system.get_help("debug_session");
    assert!(debug_help.is_some());

    if let Some(help_entry) = debug_help {
        assert_eq!(help_entry.topic, "Debug Session");
        assert!(!help_entry.description.is_empty());
        assert!(!help_entry.usage.is_empty());
    }

    // Test search functionality
    let search_results = help_system.search("debug");
    assert!(!search_results.is_empty());

    // Test contextual help
    let _gradient_help = help_system.contextual_help("gradient");
    // Should return help results

    Ok(())
}

/// Test performance monitoring utilities
#[tokio::test]
async fn test_performance_monitoring() -> Result<()> {
    let mut monitor = PerformanceMonitor::new();

    // Test checkpoints
    monitor.checkpoint("operation1");
    sleep(Duration::from_millis(10)).await;
    let duration1 = monitor.end_checkpoint("operation1");
    assert!(duration1.is_some());
    assert!(duration1.expect("operation failed in test").as_millis() >= 10);

    monitor.checkpoint("operation2");
    sleep(Duration::from_millis(20)).await;
    let duration2 = monitor.end_checkpoint("operation2");
    assert!(duration2.is_some());
    assert!(duration2.expect("operation failed in test").as_millis() >= 20);

    // Test total elapsed time
    let total = monitor.total_elapsed();
    assert!(total.as_millis() >= 30);

    // Test performance report
    let report = monitor.performance_report();
    assert!(report.contains("operation1"));
    assert!(report.contains("operation2"));
    assert!(report.contains("Performance Report"));

    Ok(())
}

/// Test debug templates
#[tokio::test]
async fn test_debug_templates() -> Result<()> {
    // Test development template
    let dev_config = DebugUtils::create_debug_template(DebugTemplate::Development);
    assert!(dev_config.enable_tensor_inspection);
    assert!(dev_config.enable_gradient_debugging);
    assert!(dev_config.enable_visualization);
    assert_eq!(dev_config.sampling_rate, 1.0);

    // Test production template
    let prod_config = DebugUtils::create_debug_template(DebugTemplate::Production);
    assert!(!prod_config.enable_tensor_inspection);
    assert!(!prod_config.enable_gradient_debugging);
    assert!(!prod_config.enable_visualization);
    assert_eq!(prod_config.sampling_rate, 0.1);

    // Test training template
    let train_config = DebugUtils::create_debug_template(DebugTemplate::Training);
    assert!(train_config.enable_tensor_inspection);
    assert!(train_config.enable_gradient_debugging);
    assert!(!train_config.enable_visualization);
    assert_eq!(train_config.sampling_rate, 0.5);

    // Test research template
    let research_config = DebugUtils::create_debug_template(DebugTemplate::Research);
    assert!(research_config.enable_tensor_inspection);
    assert!(research_config.enable_gradient_debugging);
    assert!(research_config.enable_visualization);
    assert_eq!(research_config.sampling_rate, 1.0);
    assert_eq!(research_config.max_tracked_tensors, 2000);

    Ok(())
}

/// Test optimized debugging sessions
#[tokio::test]
async fn test_optimized_debugging() -> Result<()> {
    // Test ultra-low overhead session
    let mut low_overhead_session = ultra_low_overhead_session();

    // Start the session
    low_overhead_session.start().await?;

    // Check performance metrics
    let metrics = low_overhead_session.get_performance_metrics();
    assert_eq!(metrics.memory_usage_mb, 0); // Simplified implementation returns 0
    assert_eq!(metrics.cpu_usage_percentage, 0.0); // Simplified implementation returns 0

    // Check performance limits
    assert!(low_overhead_session.is_within_performance_limits());

    Ok(())
}

/// Test comprehensive debugging flow with all components
#[tokio::test]
async fn test_comprehensive_debugging_flow() -> Result<()> {
    let config = DebugConfig {
        enable_tensor_inspection: true,
        enable_gradient_debugging: true,
        enable_model_diagnostics: true,
        enable_visualization: false,    // Disable for testing
        enable_memory_profiling: false, // Disable for testing
        enable_computation_graph_analysis: true,
        max_tracked_tensors: 50,
        max_gradient_history: 25,
        sampling_rate: 0.5,
        ..Default::default()
    };

    let mut debug_session = DebugSession::new(config.clone());
    debug_session.start().await?;

    // Record model architecture
    let arch_info = ModelArchitectureInfo {
        total_parameters: 1_000_000,
        trainable_parameters: 1_000_000,
        model_size_mb: 4.0,
        layer_count: 12,
        layer_types: {
            let mut types = std::collections::HashMap::new();
            types.insert("Linear".to_string(), 6);
            types.insert("Attention".to_string(), 6);
            types
        },
        depth: 12,
        width: 512,
        activation_functions: {
            let mut activations = std::collections::HashMap::new();
            activations.insert("ReLU".to_string(), 6);
            activations.insert("Softmax".to_string(), 6);
            activations
        },
    };

    debug_session.model_diagnostics_mut().record_architecture(arch_info);

    // Simulate training loop
    for step in 0..5 {
        let loss = 2.0 * (-0.1 * step as f64).exp() + 0.1;
        let accuracy = 1.0 - loss / 3.0;

        let metrics = ModelPerformanceMetrics {
            training_step: step,
            loss,
            accuracy: Some(accuracy.clamp(0.0, 1.0)),
            learning_rate: 1e-4,
            batch_size: 32,
            throughput_samples_per_sec: 100.0,
            memory_usage_mb: 1024.0,
            gpu_utilization: Some(80.0),
            timestamp: chrono::Utc::now(),
        };

        let _ = debug_session.model_diagnostics_mut().record_performance(metrics);

        // Record gradients for multiple layers
        for layer_idx in 0..3 {
            let layer_name = format!("layer_{}", layer_idx);
            let gradient_values: Vec<f64> =
                (0..50).map(|i| 0.01 * (i as f64 * 0.1 + step as f64 * 0.2).sin()).collect();

            let gradient_norm = (gradient_values.iter().map(|&x| x * x).sum::<f64>()).sqrt();
            let gradient_mean = gradient_values.iter().sum::<f64>() / gradient_values.len() as f64;
            let gradient_variance =
                gradient_values.iter().map(|&x| (x - gradient_mean).powi(2)).sum::<f64>()
                    / gradient_values.len() as f64;
            let gradient_std = gradient_variance.sqrt();

            debug_session.gradient_debugger_mut().record_gradient_flow(
                &layer_name,
                gradient_norm,
                gradient_mean,
                gradient_std,
            )?;
        }

        debug_session.gradient_debugger_mut().next_step();
    }

    // Analyze training dynamics
    let training_dynamics = debug_session.model_diagnostics().analyze_training_dynamics();
    assert!(matches!(training_dynamics.convergence_status, _)); // Should have some status

    // Analyze gradient flow
    let _gradient_analysis = debug_session.gradient_debugger().analyze_gradient_conflicts();
    // Conflicts can be analyzed (may be 0 if no conflicts found)

    // Generate final report
    let report = debug_session.stop().await?;

    // Verify comprehensive report
    assert!(report.tensor_report.is_some() || !config.enable_tensor_inspection);
    assert!(report.gradient_report.is_some());
    assert!(report.diagnostics_report.is_some());
    assert!(report.architecture_analysis_report.is_some());

    let _summary = report.summary();
    // Summary contains valid issue count

    Ok(())
}

/// Test macro functionality
#[tokio::test(flavor = "multi_thread")]
async fn test_debug_macros() -> Result<()> {
    // Wrap in timeout to prevent hanging
    let test_result = tokio::time::timeout(Duration::from_secs(5), async {
        let mut debug_session = debug_session();
        debug_session.start().await?;

        // Test tensor debugging macro
        let test_tensor = Array::<f32, _>::ones(IxDyn(&[5, 5]));
        let tensor_id = debug_tensor!(debug_session, &test_tensor, "macro_test")?;
        assert!(tensor_id != Uuid::nil());

        // Test gradient debugging macro
        let gradients = vec![0.1, 0.2, 0.3, 0.4, 0.5];
        debug_gradient!(debug_session, "macro_layer", &gradients)?;

        // Give background tasks a moment to process
        sleep(Duration::from_millis(1)).await;

        debug_session.stop().await?;

        // Give background tasks time to clean up
        sleep(Duration::from_millis(1)).await;

        Ok::<(), anyhow::Error>(())
    })
    .await;

    match test_result {
        Ok(result) => result,
        Err(_) => Err(anyhow::anyhow!("Test timed out after 5s")),
    }
}

/// Integration test with error conditions
#[tokio::test]
async fn test_error_handling() -> Result<()> {
    let mut debug_session = debug_session();
    debug_session.start().await?;

    // Test with problematic tensor (NaN values)
    let problematic_tensor = Array::from_vec(vec![1.0, f32::NAN, 3.0, f32::INFINITY])
        .to_shape(IxDyn(&[2, 2]))
        .expect("operation failed in test")
        .to_owned();

    let tensor_id = debug_session.tensor_inspector_mut().inspect_tensor(
        &problematic_tensor,
        "problematic",
        Some("test_layer"),
        Some("forward"),
    )?;

    // Should still work despite problematic values
    assert!(tensor_id != Uuid::nil());

    let report = debug_session.stop().await?;

    // Should detect issues in the tensor
    if let Some(ref tensor_report) = report.tensor_report {
        assert!(tensor_report.tensors_with_issues > 0);
    }

    Ok(())
}