TrustformeRS Debug
Version: 0.2.0 | Status: Alpha | Tests: ~899 | SLoC: ~101,000 | Updated: 2026-07-02
Advanced debugging and analysis tools for TrustformeRS machine learning models.
Feature Flags
| Feature | Description |
|---|---|
visual |
Enable graphical plot output via Plotters (opt-in — see note below) |
video |
Enable video frame export for training animations |
gif |
Enable animated GIF export for visualization sequences |
wasm |
Enable WebAssembly-compatible debugging (no filesystem I/O) |
atomics |
Enable atomic counters for lock-free profiling in multi-threaded contexts |
headless |
Enable terminal/ASCII visualization for server environments (Ratatui) |
cuda |
Reserved for a future NVIDIA CUDA GPU backend (see note below) |
rocm |
Reserved for a future AMD ROCm GPU backend (see note below) |
tpu |
Reserved for a future Google TPU backend (see note below) |
default = [] — this crate is Pure-Rust by default (COOLJAPAN Pure-Rust policy). visual is deliberately not part of default: it pulls in plotters → image → png → flate2/miniz_oxide, and compression crates other than oxiarc-archive are policy-banned in default builds. Enable it explicitly with --features visual when graphical (PNG/SVG) plot output is needed.
cuda/rocm/tpu are placeholder flags for future GPU/TPU backend work — no GPU/TPU kernels are compiled or executed anywhere in the crate today. The only place they currently have any effect is the Performance Profiler's performance_tuning::PerformanceTuner, whose compile-time detect_hardware() check picks which hardware-specific tuning recommendations to surface (HardwareType::NvidiaGpu/AmdGpu/Tpu vs. the Cpu/AppleSilicon defaults).
Overview
TrustformeRS Debug provides comprehensive debugging capabilities for deep learning models, including tensor inspection, gradient flow analysis, model diagnostics, performance profiling, automated debugging hooks, model interpretability (SHAP/LIME/attention analysis), and simulation-based robustness testing. These tools help identify training issues, performance bottlenecks, and model problems early in the development process.
Features
Tensor Inspector
- Real-time Analysis: Inspect tensor statistics, distributions, and patterns
- Anomaly Detection: Automatically detect NaN, infinite, and extreme values
- Memory Tracking: Monitor memory usage and identify memory leaks
- Comparison Tools: Compare tensors across different training steps or layers
Gradient Debugger
- Flow Analysis: Track gradient flow through model layers
- Problem Detection: Identify vanishing and exploding gradients
- Dead Neuron Detection: Find layers with inactive neurons
- Trend Analysis: Monitor gradient patterns over training steps
Model Diagnostics
- Training Dynamics: Analyze convergence, stability, and learning efficiency
- Architecture Analysis: Evaluate model structure and parameter efficiency
- Performance Monitoring: Track training metrics and detect anomalies
- Health Assessment: Overall model health scoring and recommendations
Debugging Hooks
- Automated Monitoring: Set up conditional debugging triggers
- Layer-specific Tracking: Monitor specific layers or operations
- Custom Callbacks: Implement custom debugging logic
- Alert System: Real-time notifications for debugging events
Visualization
Multiple backends depending on enabled features:
- Plotters (
visualfeature): Static PNG/SVG plots, histograms, scatter charts, line graphs - Ratatui (
headlessfeature): Terminal-based TUI dashboards and ASCII plots for headless server environments - TensorBoard: Event file export compatible with TensorBoard scalar/histogram/graph viewers
- GIF export (
giffeature): Animated training progress visualizations - Video export (
videofeature): MP4-compatible frame sequences for training recordings
Memory Profiling
Deadlock-safe memory profiling using scoped mutex guards:
- Peak memory tracking with RAII-scoped lock acquisition (no
unwrap, no lock poisoning) - Memory allocation timeline with per-layer attribution
- Fragmentation analysis and leak detection
- Thread-safe snapshot API with bounded scope to prevent deadlock across async boundaries
Performance Profiler
- Execution Timing: Profile layer and operation execution times
- Memory Profiling: Track memory usage patterns with deadlock-safe mutex scoping
- Bottleneck Detection: Identify performance bottlenecks automatically
- Optimization Suggestions: Get recommendations for performance improvements
- Flame Graphs: Generate Inferno-compatible flamegraph data for call stack visualization
AI Code Analysis
Automated model code analysis powered by built-in heuristics:
- Pattern matching for common anti-patterns (gradient checkpoint misuse, redundant recomputation)
- Architecture smell detection (excessive depth without skip connections, degenerate attention heads)
- Actionable suggestions with line-level annotations compatible with VS Code diagnostics
VS Code Integration
Language Server Protocol (LSP)-compatible diagnostic output:
- Emit structured JSON diagnostics consumable by the VS Code Rust Analyzer extension
- Breakpoint-compatible debug session with DAP (Debug Adapter Protocol) event emission
- In-editor tensor shape annotations via hover provider protocol
Interpretability
Feature-attribution and explainability toolkit (InterpretabilityAnalyzer) for model predictions:
- SHAP Analysis: Shapley-value feature contributions with background-dataset sampling
- LIME Analysis: local surrogate-model explanations via perturbation-based neighborhood sampling
- Feature Attribution: Integrated Gradients, Gradient×Input, SmoothGrad, Gradient SHAP, DeepLIFT, LRP, Guided Backprop, Grad-CAM/Grad-CAM++/Score-CAM, Expected Gradients, Attention Rollout, Path Integrated Gradients
- Counterfactual Analysis: minimal-change counterfactual generation, feature-sensitivity and decision-boundary analysis, actionable insights
- Attention Pattern Analysis: per-layer/per-head attention statistics, head-specialization classification, attention-flow tracing
Simulation & Robustness Testing
Systematic model-behavior probing (SimulationAnalyzer in simulation_tools):
- What-If Analysis: scenario generation, impact analysis, feature-sensitivity and decision-boundary exploration
- Perturbation Testing: robustness scoring across perturbation intensities, sensitivity-hotspot identification, failure-mode analysis
- Adversarial Probing: adversarial-example generation (FGSM/PGD/CW/DeepFool), attack-success analysis, certified-robustness estimation, defense recommendations
- Edge Case Discovery: automated edge-case search, classification, coverage analysis, and risk assessment
Guided Debugging
Step-by-step debugging wizard (GuidedDebugger) that walks through health check, gradient analysis, architecture analysis, memory profiling, performance profiling, and anomaly detection in sequence, with progress tracking and per-step skip/reset control.
Interactive Tutorial
Lesson-based walkthrough mode (TutorialMode) for onboarding new users to the debugging toolkit, with per-lesson objectives, example code, tips, and progress tracking.
Export and Integration
- Netron Export: ONNX-compatible export for the Netron model visualizer
- Excel (.xlsx) Export: genuine Office Open XML (OOXML) workbook generation —
[Content_Types].xml, relationships, workbook, and worksheet parts written via COOLJAPAN's pure-Rustoxiarc-archivecrate (oxiarc_archive::zip::ZipWriter), replacing the previous CSV-with-.xlsx-extension placeholder
Quick Start
Basic Usage
use ;
async
Advanced Configuration
use ;
let config = DebugConfig ;
let mut debug_session = new;
Using Debugging Hooks
use ;
let mut debug_session = debug_session;
// Create a hook to monitor specific layers
let hook = new
.trigger
.action
.layer_patterns
.max_executions
.build;
let hook_id = debug_session.hooks_mut.register_hook?;
// Execute hooks during model execution
let tensor_data = vec!;
let results = debug_session.hooks_mut.execute_hooks;
Visualization
use ;
let config = VisualizationConfig ;
let mut visualizer = new;
// Plot tensor distribution
let values = vec!;
visualizer.plot_tensor_distribution?;
// Plot gradient flow over time
let steps = vec!;
let gradients = vec!;
visualizer.plot_gradient_flow?;
// Create dashboard
let plot_names = visualizer.get_plot_names;
let dashboard = visualizer.create_dashboard?;
Flame Graph Generation
use FlameGraphBuilder;
let mut builder = new;
builder.record_frame;
builder.record_frame;
builder.record_frame;
// Write Inferno-compatible collapsed stack format
builder.write_to_file?;
// Run: inferno-flamegraph < /tmp/trustformers_flame.txt > flame.svg
Interpretability Analysis (SHAP)
use HashMap;
use ;
async
Key Components
Tensor Inspector
The tensor inspector provides detailed analysis of tensor values:
- Statistical Analysis: Mean, standard deviation, min/max, norms
- Distribution Analysis: Histograms, percentiles, outlier detection
- Quality Checks: NaN/infinite value detection, sparsity analysis
- Memory Tracking: Memory usage per tensor, leak detection
let tensor_id = debug_session.tensor_inspector_mut
.inspect_tensor?;
// Get tensor information
let info = debug_session.tensor_inspector.get_tensor_info;
println!;
Gradient Debugger
Monitor gradient flow to identify training problems:
- Flow Tracking: Record gradient norms, means, and patterns
- Problem Detection: Vanishing/exploding gradients, dead neurons
- Trend Analysis: Gradient evolution over training steps
- Health Assessment: Overall gradient flow health scoring
// Record gradients for a layer
debug_session.gradient_debugger_mut
.record_gradient_flow?;
// Analyze gradient health
let analysis = debug_session.gradient_debugger.analyze_gradient_flow;
println!;
Model Diagnostics
Comprehensive model-level analysis:
- Training Dynamics: Convergence analysis, stability assessment
- Architecture Evaluation: Parameter efficiency, layer analysis
- Performance Tracking: Training metrics, memory usage
- Anomaly Detection: Performance degradation, instability detection
// Record model architecture
let arch_info = ModelArchitectureInfo ;
debug_session.model_diagnostics_mut.record_architecture;
// Record training metrics
let metrics = ModelPerformanceMetrics ;
debug_session.model_diagnostics_mut.record_performance;
Performance Profiler
Identify performance bottlenecks:
- Execution Timing: Layer and operation profiling
- Memory Analysis: Memory usage patterns and leaks (deadlock-safe scoped locks)
- Bottleneck Detection: Automatic performance issue identification
- Flame Graph Export: Inferno-compatible collapsed stack format
- Optimization Recommendations: Actionable performance suggestions
let profiler = debug_session.profiler_mut;
// Profile layer execution
profiler.start_timer;
// ... layer computation ...
let duration = profiler.end_timer;
// Record detailed layer metrics
profiler.record_layer_execution;
Terminal Visualization
For headless environments (headless feature), use Ratatui-based TUI dashboards or ASCII-based visualizations:
use TerminalVisualizer;
let terminal_viz = new;
// ASCII histogram
let histogram = terminal_viz.ascii_histogram;
println!;
// ASCII line plot
let line_plot = terminal_viz.ascii_line_plot;
println!;
Integration with Training
Integrate debugging seamlessly into your training loop:
use ;
let mut debug_session = debug_session;
debug_session.start.await?;
for epoch in 0..num_epochs
// Generate comprehensive debug report
let report = debug_session.stop.await?;
Best Practices
Performance Considerations
- Sampling: Use sampling rate < 1.0 for expensive operations
- Selective Monitoring: Monitor only critical layers during training
- Batch Processing: Process debug operations in batches when possible
- Memory Management: Clear debug data periodically for long training runs
- Feature Flags: Enable only needed features to minimize binary size and overhead
Debugging Workflow
- Start Simple: Begin with basic tensor inspection and gradient monitoring
- Add Hooks: Use automated hooks for continuous monitoring
- Analyze Patterns: Look for trends in gradient flow and model metrics
- Visualize Results: Create dashboards for comprehensive analysis
- Act on Insights: Apply recommendations from debug reports
Common Use Cases
- Training Instability: Use gradient debugger to identify vanishing/exploding gradients
- Memory Issues: Monitor tensor memory usage with deadlock-safe profiling
- Performance Optimization: Profile layer execution and generate flame graphs
- Model Health: Track training dynamics and convergence patterns
- Debugging New Architectures: Comprehensive monitoring during model development
- Headless Servers: Use
headlessfeature with Ratatui for terminal dashboards
Examples
See the examples/ directory for comprehensive demonstrations:
debug_session_demo.rs: Complete debugging workflow examplesflame_graph_demo.rs: Generating and viewing flame graphsheadless_dashboard_demo.rs: Terminal TUI dashboard with Ratatuivs_code_integration_demo.rs: VS Code diagnostic JSON output
Contributing
We welcome contributions to improve TrustformeRS Debug! Please see the main repository contributing guidelines.
License
Licensed under Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0).