TrustformeRS Debug
Version: 0.1.0 | Status: Alpha | Tests: 216 | SLoC: 61,841 | Updated: 2026-03-21
Advanced debugging and analysis tools for TrustformeRS machine learning models.
Feature Flags
| Feature | Description |
|---|---|
visual |
Enable graphical plot output via Plotters |
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) |
Overview
TrustformeRS Debug provides comprehensive debugging capabilities for deep learning models, including tensor inspection, gradient flow analysis, model diagnostics, performance profiling, and automated debugging hooks. 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
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
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).