Skip to main content

torsh_utils/
lib.rs

1//! # ToRSh Utilities
2//!
3//! `torsh-utils` provides essential utility functions and tools for the ToRSh deep learning framework.
4//! This crate contains functionality for development, deployment, profiling, and model management.
5//!
6//! ## Features
7//!
8//! ### 🔧 Development Tools
9//! - **Benchmarking**: Comprehensive model performance analysis with timing and memory tracking
10//! - **Profiling**: Advanced bottleneck detection with flame graphs, memory profiling, and GPU analysis
11//! - **Environment Collection**: System and framework information gathering for debugging
12//!
13//! ### 📊 Monitoring & Visualization
14//! - **TensorBoard Integration**: Compatible logging for training metrics, histograms, and graphs
15//! - **Interactive Visualizations**: D3.js-based model architecture visualization
16//! - **Real-time Monitoring**: Live performance metrics and memory tracking
17//!
18//! ### 🚀 Deployment Tools
19//! - **Mobile Optimization**: Model compression, quantization, and platform-specific optimizations
20//! - **C++ Extensions**: Build custom CUDA kernels and operations
21//! - **Model Zoo**: Pre-trained model management with HuggingFace Hub integration
22//!
23//! ## Quick Start
24//!
25//! Add to your `Cargo.toml`:
26//! ```toml
27//! [dependencies]
28//! torsh-utils = { version = "0.1.1", features = ["tensorboard", "collect_env"] }
29//! ```
30//!
31//! ### Basic Usage Examples
32//!
33//! #### Benchmarking a Model
34//!
35//! ```rust,no_run
36//! use torsh_utils::prelude::*;
37//!
38//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
39//! // Configure benchmarking
40//! let config = BenchmarkConfig {
41//!     warmup_iterations: 10,
42//!     benchmark_iterations: 100,
43//!     batch_sizes: vec![1, 8, 16, 32],
44//!     input_shapes: vec![vec![3, 224, 224]],
45//!     profile_memory: true,
46//!     ..Default::default()
47//! };
48//!
49//! // Benchmark your model (model would be your actual neural network)
50//! // let model = MyModel::new();
51//! // let results = benchmark_model(&model, config)?;
52//! // print_benchmark_results(&results);
53//! # Ok(())
54//! # }
55//! ```
56//!
57//! #### Profiling Bottlenecks
58//!
59//! ```rust,no_run
60//! use torsh_utils::prelude::*;
61//!
62//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
63//! // Profile model performance
64//! // let model = MyModel::new();
65//! // let report = profile_bottlenecks(
66//! //     &model,
67//! //     &[1, 3, 224, 224],
68//! //     100,
69//! //     true  // profile backward pass
70//! // )?;
71//! //
72//! // // Print recommendations
73//! // print_bottleneck_report(&report);
74//! # Ok(())
75//! # }
76//! ```
77//!
78//! #### TensorBoard Logging
79//!
80//! ```rust,no_run
81//! # #[cfg(feature = "tensorboard")]
82//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
83//! use torsh_utils::prelude::*;
84//!
85//! let mut writer = SummaryWriter::new("runs/experiment1")?;
86//!
87//! // Log training metrics
88//! for epoch in 0..10 {
89//!     let loss = 0.5 / (epoch + 1) as f32; // Example loss
90//!     writer.add_scalar("loss/train", loss, Some(epoch as i64))?;
91//! }
92//! # Ok(())
93//! # }
94//! ```
95//!
96//! #### Mobile Optimization
97//!
98//! ```rust,no_run
99//! use torsh_utils::prelude::*;
100//!
101//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
102//! // Configure mobile optimization
103//! let config = MobileOptimizerConfig {
104//!     quantize: true,
105//!     fuse_ops: true,
106//!     remove_dropout: true,
107//!     fold_bn: true,
108//!     backend: MobileBackend::Cpu,
109//!     ..Default::default()
110//! };
111//!
112//! // Optimize model for mobile (model would be your actual neural network)
113//! // let optimized = optimize_for_mobile(&model, config)?;
114//! # Ok(())
115//! # }
116//! ```
117//!
118//! #### Model Zoo Usage
119//!
120//! ```rust,no_run
121//! use torsh_utils::prelude::*;
122//! # use std::path::PathBuf;
123//!
124//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
125//! // Create model zoo
126//! let mut zoo = ModelZoo::new("~/.torsh/models")?;
127//!
128//! // Search for models
129//! // let models = zoo.search_models("resnet", Some(ModelSearchQuery::new()))?;
130//!
131//! // Download a model
132//! // let model_info = zoo.download_model("resnet50-imagenet", None)?;
133//! # Ok(())
134//! # }
135//! ```
136//!
137//! ## Module Overview
138//!
139//! - [`benchmark`]: Model performance benchmarking with comprehensive metrics
140//! - [`bottleneck`]: Advanced profiling for performance bottleneck detection
141//! - [`collect_env`]: System and environment information collection
142//! - [`cpp_extension`]: C++ and CUDA extension building utilities
143//! - [`mobile_optimizer`]: Mobile deployment optimization tools
144//! - [`model_zoo`]: Pre-trained model repository and management
145//! - [`tensorboard`]: TensorBoard-compatible logging and visualization
146//!
147//! ## Feature Flags
148//!
149//! - `tensorboard` (default): Enable TensorBoard integration
150//! - `collect_env` (default): Enable environment information collection
151//!
152//! ## Architecture
153//!
154//! This crate is designed to work seamlessly with the ToRSh ecosystem:
155//! - Integrates with `torsh-core` for device abstraction
156//! - Uses `torsh-tensor` for tensor operations
157//! - Compatible with `torsh-nn` for neural network modules
158//! - Leverages `torsh-profiler` for performance profiling
159//!
160//! ## Best Practices
161//!
162//! 1. **Benchmarking**: Always use warmup iterations to avoid cold start overhead
163//! 2. **Profiling**: Profile with representative workloads
164//! 3. **Mobile Optimization**: Test on actual target devices
165//! 4. **TensorBoard**: Use meaningful tag names for easier analysis
166//! 5. **Model Zoo**: Verify checksums for downloaded models
167//!
168//! ## PyTorch Migration
169//!
170//! For users migrating from PyTorch:
171//! - `SummaryWriter` is compatible with `torch.utils.tensorboard.SummaryWriter`
172//! - `benchmark_model` provides similar functionality to `torch.utils.benchmark`
173//! - Mobile optimization is similar to `torch.utils.mobile_optimizer`
174//!
175//! ## Examples
176//!
177//! See the `examples/` directory for complete working examples:
178//! - `benchmark_model.rs`: Complete benchmarking workflow
179//! - `profile_bottlenecks.rs`: Performance profiling example
180//! - `mobile_optimization.rs`: Model optimization for mobile deployment
181//! - `tensorboard_logging.rs`: Training visualization with TensorBoard
182
183pub mod benchmark;
184pub mod bottleneck;
185pub mod collect_env;
186pub mod cpp_extension;
187pub mod mobile_optimizer;
188pub mod model_zoo;
189
190#[cfg(feature = "tensorboard")]
191pub mod tensorboard;
192
193/// Re-export commonly used items for convenience.
194///
195/// This module provides quick access to the most frequently used types and functions
196/// from the torsh-utils crate. Import this module to get started quickly:
197///
198/// ```rust
199/// use torsh_utils::prelude::*;
200/// ```
201///
202/// # Included Items
203///
204/// ## TensorBoard (with `tensorboard` feature)
205/// - [`SummaryWriter`](crate::tensorboard::SummaryWriter): Main TensorBoard writer
206/// - [`TensorBoardWriter`](crate::tensorboard::TensorBoardWriter): Enhanced writer with advanced features
207///
208/// ## Benchmarking
209/// - [`benchmark_model`](crate::benchmark::benchmark_model): Benchmark a model
210/// - [`BenchmarkConfig`](crate::benchmark::BenchmarkConfig): Benchmarking configuration
211/// - [`BenchmarkResult`](crate::benchmark::BenchmarkResult): Benchmark results
212///
213/// ## Profiling
214/// - [`profile_bottlenecks`](crate::bottleneck::profile_bottlenecks): Profile performance bottlenecks
215/// - [`BottleneckReport`](crate::bottleneck::BottleneckReport): Profiling report
216///
217/// ## Environment
218/// - [`collect_env`](crate::collect_env::collect_env): Collect environment information
219/// - [`EnvironmentInfo`](crate::collect_env::EnvironmentInfo): Environment details
220///
221/// ## C++ Extensions
222/// - [`build_cpp_extension`](crate::cpp_extension::build_cpp_extension): Build C++ extensions
223/// - [`CppExtensionConfig`](crate::cpp_extension::CppExtensionConfig): Extension build configuration
224///
225/// ## Mobile Optimization
226/// - [`optimize_for_mobile`](crate::mobile_optimizer::optimize_for_mobile): Optimize model for mobile
227/// - [`ExportFormat`](crate::mobile_optimizer::ExportFormat): Mobile export formats
228/// - [`MobileBackend`](crate::mobile_optimizer::MobileBackend): Target mobile backend
229/// - [`MobileOptimizerConfig`](crate::mobile_optimizer::MobileOptimizerConfig): Optimization configuration
230///
231/// ## Model Zoo
232/// - [`ModelInfo`](crate::model_zoo::ModelInfo): Model metadata
233/// - [`ModelZoo`](crate::model_zoo::ModelZoo): Model repository manager
234pub mod prelude {
235    #[cfg(feature = "tensorboard")]
236    pub use crate::tensorboard::{SummaryWriter, TensorBoardWriter};
237
238    pub use crate::benchmark::{benchmark_model, BenchmarkConfig, BenchmarkResult};
239    pub use crate::bottleneck::{profile_bottlenecks, BottleneckReport};
240    pub use crate::collect_env::{collect_env, EnvironmentInfo};
241    pub use crate::cpp_extension::{build_cpp_extension, CppExtensionConfig};
242    pub use crate::mobile_optimizer::{
243        optimize_for_mobile, ExportFormat, MobileBackend, MobileOptimizerConfig,
244    };
245    pub use crate::model_zoo::{ModelInfo, ModelZoo};
246}