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
//! Comprehensive benchmarking suite for optimizers
//!
//! This module provides a complete toolkit for evaluating and comparing optimizer performance
//! across diverse optimization scenarios. It's designed for both research and production
//! use cases to help you choose the best optimizer for your specific needs.
//!
//! ## Features
//!
//! ### Core Benchmarks
//! - **Step Performance**: Raw computational speed measurement
//! - **Convergence Speed**: How quickly optimizers reach optimal solutions
//! - **Memory Usage**: Memory consumption and scaling characteristics
//! - **Sparse Gradients**: Performance with different sparsity patterns
//!
//! ### Advanced Analytics
//! - **Side-by-side Comparisons**: Statistical comparison between optimizers
//! - **Convergence Analysis**: Detailed convergence behavior analysis
//! - **Hardware Utilization**: CPU/Memory usage tracking (when available)
//! - **Domain-specific Benchmarks**: Tests tailored for CV, NLP, and RL
//!
//! ### Export & Visualization
//! - **JSON/CSV Export**: Data export for external analysis
//! - **Statistical Tests**: Determine if performance differences are significant
//! - **Benchmark Reports**: Comprehensive HTML reports with visualizations
//! - **Performance Regression**: Track optimizer performance over time
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! # use torsh_tensor::creation::randn;
//! # use torsh_core::error::Result;
//! # use parking_lot::RwLock;
//! # use std::sync::Arc;
//! # fn main() -> Result<()> {
//! use torsh_optim::benchmarks::*;
//! use torsh_optim::Adam;
//!
//! // Create some parameters
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//! let params_clone1 = params.clone();
//! let params_clone2 = params.clone();
//! let params_clone3 = params.clone();
//!
//! // Create benchmark suite
//! let benchmarks = OptimizerBenchmarks::new();
//!
//! // Compare different configurations of the same optimizer
//! let comparison = OptimizerComparison::new()
//! .add_optimizer("Adam-1e-3", move || Ok(Adam::new(params_clone1.clone(), Some(0.001), None, None, None, false)))
//! .add_optimizer("Adam-1e-4", move || Ok(Adam::new(params_clone2.clone(), Some(0.0001), None, None, None, false)))
//! .add_optimizer("Adam-AMSGrad", move || Ok(Adam::new(params_clone3.clone(), Some(0.001), None, None, None, true)));
//!
//! // Run comprehensive benchmarks
//! let results = comparison.run_comparison_suite()?;
//! comparison.print_comparison_table(&results);
//! # Ok(())
//! # }
//! ```
//!
//! ## Domain-Specific Benchmarks
//!
//! ### Computer Vision
//! ```rust,no_run
//! # use torsh_tensor::creation::randn;
//! # use torsh_core::error::Result;
//! # use parking_lot::RwLock;
//! # use std::sync::Arc;
//! # fn main() -> Result<()> {
//! use torsh_optim::benchmarks::domain_specific::CVBenchmarks;
//! use torsh_optim::Adam;
//!
//! // Create some parameters and optimizer
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//! let optimizer = Adam::new(params, None, None, None, None, false);
//!
//! let cv_bench = CVBenchmarks::new();
//! let results = cv_bench.benchmark_resnet_training(optimizer)?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Natural Language Processing
//! ```rust,no_run
//! # use torsh_tensor::creation::randn;
//! # use torsh_core::error::Result;
//! # use parking_lot::RwLock;
//! # use std::sync::Arc;
//! # fn main() -> Result<()> {
//! use torsh_optim::benchmarks::domain_specific::NLPBenchmarks;
//! use torsh_optim::AdamW;
//!
//! // Create some parameters and optimizer
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//! let optimizer = AdamW::new(params, Some(5e-5), None, None, Some(0.01), false);
//!
//! let nlp_bench = NLPBenchmarks::new();
//! let results = nlp_bench.benchmark_transformer_training(optimizer)?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Reinforcement Learning
//! ```rust,no_run
//! # use torsh_tensor::creation::randn;
//! # use torsh_core::error::Result;
//! # use parking_lot::RwLock;
//! # use std::sync::Arc;
//! # fn main() -> Result<()> {
//! use torsh_optim::benchmarks::domain_specific::RLBenchmarks;
//! use torsh_optim::Adam;
//!
//! // Create some parameters and optimizer
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//! let optimizer = Adam::new(params, Some(3e-4), None, None, None, false);
//!
//! let rl_bench = RLBenchmarks::new();
//! let results = rl_bench.benchmark_policy_gradient(optimizer)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Performance Analysis
//!
//! The benchmark suite provides detailed statistical analysis:
//!
//! - **Execution Time**: Mean, median, std dev, confidence intervals
//! - **Convergence Metrics**: Convergence rate, final loss, iteration count
//! - **Memory Analysis**: Peak usage, growth patterns, efficiency scores
//! - **Statistical Significance**: P-values, effect sizes, confidence intervals
//!
//! ## Exporting Results
//!
//! ```rust,no_run
//! # use torsh_tensor::creation::randn;
//! # use torsh_core::error::Result;
//! # use parking_lot::RwLock;
//! # use std::sync::Arc;
//! # fn main() -> Result<()> {
//! use torsh_optim::benchmarks::*;
//! use torsh_optim::Adam;
//!
//! // Create some parameters and run benchmarks
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//! let params_clone = params.clone();
//! let comparison = OptimizerComparison::new()
//! .add_optimizer("Adam", move || Ok(Adam::new(params_clone.clone(), None, None, None, None, false)));
//! let results = comparison.run_comparison_suite()?;
//!
//! // Export results (examples - actual paths may vary)
//! // results.export_json("benchmark_results.json")?;
//! // results.export_csv("benchmark_results.csv")?;
//! // results.generate_html_report("benchmark_report.html")?;
//! # Ok(())
//! # }
//! ```
// Re-export the main types for convenience
pub use ;
pub use OptimizerBenchmarks;
pub use ;
pub use ;
pub use ;