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
//! Distributed optimization for multi-process training
//!
//! This module provides comprehensive support for distributed deep learning training
//! across multiple processes, nodes, or devices. It includes various distributed
//! optimization algorithms and utilities for different distributed training scenarios.
//!
//! # Overview
//!
//! Distributed training allows you to scale deep learning to larger datasets and models
//! by parallelizing the training process across multiple workers. This module supports
//! several distributed training paradigms:
//!
//! ## Synchronous Training
//! - **Data Parallel**: Each worker processes different batches, gradients are synchronized
//! - **All-Reduce**: Efficient gradient averaging across all workers
//! - **Parameter Server**: Central parameter management with worker nodes
//!
//! ## Asynchronous Training
//! - **Async SGD**: Workers update independently without waiting for synchronization
//! - **Bounded Staleness**: Limits how stale parameter updates can be
//! - **Elastic Averaging**: Workers explore different parameter spaces while being pulled toward center
//!
//! # Key Components
//!
//! ## Core Module (`core`)
//! - `DistributedOptimizer<O>`: Wrapper that adds distributed functionality to any optimizer
//! - `DistributedConfig`: Configuration for communication backends and strategies
//! - Communication abstractions for different backends (NCCL, MPI, Gloo)
//!
//! ## Async SGD Module (`async_sgd`)
//! - `AsyncSGD`: Asynchronous stochastic gradient descent
//! - Staleness tracking and adaptive learning rates
//! - Parameter mixing for improved convergence
//!
//! ## Elastic SGD Module (`elastic_sgd`)
//! - `ElasticAveragingSGD`: Elastic averaging SGD (EASGD)
//! - Allows worker exploration while maintaining coordination
//! - Better convergence properties than vanilla distributed SGD
//!
//! ## Utilities Module (`utils`)
//! - Convenient factory functions for common setups
//! - Predefined configurations for different scenarios
//! - Monitoring and debugging utilities
//!
//! # Quick Start
//!
//! ## Basic Distributed Training
//! ```rust
//! # use torsh_tensor::creation::randn;
//! # use torsh_core::error::Result;
//! # use parking_lot::RwLock;
//! # use std::sync::Arc;
//! # fn main() -> Result<()> {
//! use torsh_optim::distributed::{utils, core::*};
//! use torsh_optim::{SGD, Optimizer};
//!
//! // Create some parameters
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//!
//! // Create distributed SGD optimizer
//! let mut optimizer = utils::distributed_sgd(
//! params,
//! 0.1, // learning rate
//! 4, // world size (4 workers)
//! 0, // rank (this is worker 0)
//! Some(0.9), // momentum
//! Some(1e-4) // weight decay
//! )?;
//!
//! // Training loop (simplified)
//! for _batch in 0..10 {
//! // Forward pass and backward pass would go here
//! // In real code: loss.backward()?;
//!
//! // Distributed optimization step (includes gradient synchronization)
//! optimizer.step()?;
//! optimizer.zero_grad();
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Asynchronous Training
//! ```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::distributed::async_sgd::AsyncSGD;
//!
//! // Create some parameters
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//!
//! let mut async_optimizer = AsyncSGD::new_async(params, 0.01);
//!
//! // Asynchronous training - workers can update at different rates (conceptual example)
//! // In practice, you would coordinate with a parameter server
//! # Ok(())
//! # }
//! ```
//!
//! ## Elastic Averaging SGD
//! ```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::distributed::elastic_sgd::ElasticAveragingSGD;
//!
//! // Create some parameters
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//!
//! let mut easgd = ElasticAveragingSGD::new_default(
//! params,
//! 0.1, // learning rate
//! 0, // worker rank
//! 4 // total workers
//! )?;
//!
//! // Training with periodic communication (conceptual example)
//! // In practice, you would implement the full training loop with communication
//! # Ok(())
//! # }
//! ```
//!
//! # Advanced Configurations
//!
//! ## Custom Communication Setup
//! ```rust
//! # use torsh_tensor::creation::randn;
//! # use torsh_core::error::Result;
//! # use parking_lot::RwLock;
//! # use std::sync::Arc;
//! # fn main() -> Result<()> {
//! use torsh_optim::distributed::{core::*, utils};
//! use torsh_optim::AdamW;
//!
//! // Create some parameters
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//! let base_optimizer = AdamW::new(params, Some(1e-4), None, None, Some(0.01), false);
//!
//! let config = DistributedConfig {
//! backend: DistributedBackend::NCCL,
//! sync_strategy: SyncStrategy::AllReduce,
//! world_size: 8,
//! rank: 0,
//! gradient_compression: true,
//! bucket_size_mb: 25.0,
//! overlap_communication: true,
//! ..Default::default()
//! };
//!
//! let optimizer = utils::distributed_optimizer(base_optimizer, config)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Large Scale Training
//! ```rust
//! # use torsh_tensor::creation::randn;
//! # use torsh_core::error::Result;
//! # use parking_lot::RwLock;
//! # use std::sync::Arc;
//! # fn main() -> Result<()> {
//! use torsh_optim::distributed::utils::{self, configs};
//! use torsh_optim::AdamW;
//!
//! // Create some parameters
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//! let base_optimizer = AdamW::new(params, Some(1e-4), None, None, Some(0.01), false);
//!
//! let worker_rank = 0;
//! // Optimized for 100+ workers
//! let config = configs::large_scale_config(128, worker_rank);
//! let optimizer = utils::distributed_optimizer(base_optimizer, config)?;
//! # Ok(())
//! # }
//! ```
//!
//! # Communication Backends
//!
//! ## NCCL (Recommended for GPUs)
//! - Highly optimized for NVIDIA GPUs
//! - Supports advanced communication patterns
//! - Best performance for GPU clusters
//!
//! ## MPI (General Purpose)
//! - Works on both CPU and GPU
//! - Widely supported across HPC systems
//! - Good for mixed CPU/GPU environments
//!
//! ## Gloo (Facebook's Backend)
//! - Cross-platform support
//! - Good fallback option
//! - Supports both CPU and GPU
//!
//! # Best Practices
//!
//! ## Choosing the Right Approach
//! - **Synchronous**: Better convergence, easier debugging, may be slower
//! - **Asynchronous**: Faster iteration, handles stragglers, may have convergence issues
//! - **Elastic Averaging**: Best of both worlds, good exploration
//!
//! ## Communication Efficiency
//! - Enable gradient compression for large clusters or limited bandwidth
//! - Use appropriate bucket sizes (smaller for CPU, larger for GPU)
//! - Enable communication overlap when possible
//!
//! ## Monitoring and Debugging
//! ```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::distributed::utils;
//!
//! // Create distributed optimizer
//! let param1 = Arc::new(RwLock::new(randn::<f32>(&[10, 20])?));
//! let params = vec![param1];
//! let optimizer = utils::distributed_adam(params, 0.001, 4, 0, None, None, None)?;
//!
//! // In practice, you would collect performance statistics using distributed monitoring tools
//! # Ok(())
//! # }
//! ```
//!
//! # Performance Tips
//!
//! 1. **Batch Size**: Scale batch size with number of workers
//! 2. **Learning Rate**: May need adjustment for distributed training
//! 3. **Communication Frequency**: Balance between convergence and efficiency
//! 4. **Gradient Compression**: Essential for large clusters
//! 5. **Memory Management**: Monitor memory usage across workers
// Re-export the main types for convenience
pub use ;
pub use ;
pub use ElasticAveragingSGD;
// Re-export utilities
pub use ;