torsh-distributed 0.1.2

Distributed training and inference for ToRSh
Documentation
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Gradient synchronization for 3D parallelism
//!
//! This module manages gradient synchronization across data, tensor,
//! and pipeline parallelism dimensions with various optimization strategies.

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use crate::TorshResult;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use torsh_tensor::Tensor;

use super::{
    config::{CommunicationStrategy, RankMapping, ThreeDParallelismConfig},
    model_shards::ModelShards,
};

/// Gradient synchronization coordinator for 3D parallelism
pub struct GradientSynchronizer {
    /// Configuration
    config: ThreeDParallelismConfig,
    /// Rank mapping
    rank_mapping: RankMapping,
    /// Gradient buffers for accumulation
    gradient_buffers: Arc<Mutex<HashMap<String, GradientBuffer>>>,
    /// Synchronization statistics
    sync_stats: Arc<Mutex<SyncStatistics>>,
    /// Gradient compression settings
    compression_config: GradientCompressionConfig,
    /// Bucket configuration for gradient bucketing
    bucket_config: GradientBucketingConfig,
}

impl GradientSynchronizer {
    /// Create new gradient synchronizer
    pub fn new(config: &ThreeDParallelismConfig, rank_mapping: &RankMapping) -> TorshResult<Self> {
        let gradient_buffers = Arc::new(Mutex::new(HashMap::new()));
        let sync_stats = Arc::new(Mutex::new(SyncStatistics::new()));

        // Configure gradient compression based on settings
        let compression_config = GradientCompressionConfig {
            enable_compression: true,
            compression_ratio: 0.1,
            error_feedback: true,
            quantization_bits: 8,
        };

        // Configure gradient bucketing for efficiency
        let bucket_config = GradientBucketingConfig {
            bucket_size_mb: 25.0,
            max_buckets: 16,
            overlap_communication: true,
        };

        Ok(Self {
            config: config.clone(),
            rank_mapping: rank_mapping.clone(),
            gradient_buffers,
            sync_stats,
            compression_config,
            bucket_config,
        })
    }

    /// Synchronize gradients across all parallelism dimensions
    pub async fn synchronize_gradients(&self, model_shards: &ModelShards) -> TorshResult<()> {
        let start_time = Instant::now();

        // Step 1: Synchronize gradients within tensor parallel groups
        self.synchronize_tensor_parallel_gradients(model_shards)
            .await?;

        // Step 2: Synchronize gradients across pipeline parallel stages
        self.synchronize_pipeline_parallel_gradients(model_shards)
            .await?;

        // Step 3: Synchronize gradients across data parallel replicas
        self.synchronize_data_parallel_gradients(model_shards)
            .await?;

        // Update statistics
        let mut stats = self.sync_stats.lock().expect("lock should not be poisoned");
        stats.total_sync_operations += 1;
        stats.total_sync_time += start_time.elapsed();

        Ok(())
    }

    /// Synchronize gradients within tensor parallel groups
    async fn synchronize_tensor_parallel_gradients(
        &self,
        model_shards: &ModelShards,
    ) -> TorshResult<()> {
        // Only synchronize if we have tensor parallelism
        if self.config.tp_size <= 1 {
            return Ok(());
        }

        let _tp_ranks = self.rank_mapping.tp_group_ranks();

        // For each layer in our pipeline stages
        for (stage_idx, stage_layers) in model_shards.pipeline_stages.iter().enumerate() {
            if stage_idx != self.rank_mapping.pp_rank {
                continue; // Skip stages not owned by this rank
            }

            for layer_shard in stage_layers {
                // All-reduce gradients across tensor parallel dimension
                if let Some(ref grad_weight) = layer_shard.grad_weight {
                    self.all_reduce_tensor_parallel(grad_weight).await?;
                }

                if let Some(ref grad_bias) = layer_shard.grad_bias {
                    self.all_reduce_tensor_parallel(grad_bias).await?;
                }
            }
        }

        Ok(())
    }

    /// Synchronize gradients across pipeline parallel stages
    async fn synchronize_pipeline_parallel_gradients(
        &self,
        model_shards: &ModelShards,
    ) -> TorshResult<()> {
        // Pipeline parallelism typically doesn't require gradient synchronization
        // because each stage has different parameters. However, we may need to
        // handle shared parameters or embedding layers.

        if self.config.pp_size <= 1 {
            return Ok(());
        }

        // Handle shared embeddings if they exist
        if self.rank_mapping.is_pp_head() || self.rank_mapping.is_pp_tail() {
            // First and last stages might share embedding parameters
            self.synchronize_shared_embeddings(model_shards).await?;
        }

        Ok(())
    }

    /// Synchronize gradients across data parallel replicas
    async fn synchronize_data_parallel_gradients(
        &self,
        model_shards: &ModelShards,
    ) -> TorshResult<()> {
        // Only synchronize if we have data parallelism
        if self.config.dp_size <= 1 {
            return Ok(());
        }

        // Create gradient buckets for efficient communication
        let gradient_buckets = self.create_gradient_buckets(model_shards).await?;

        // Synchronize each bucket
        for bucket in gradient_buckets {
            self.synchronize_gradient_bucket(&bucket).await?;
        }

        Ok(())
    }

    /// All-reduce gradients across tensor parallel dimension
    async fn all_reduce_tensor_parallel(&self, gradient: &Tensor<f32>) -> TorshResult<()> {
        match self.config.comm_strategy {
            CommunicationStrategy::AllReduce => self.standard_all_reduce_tp(gradient).await,
            CommunicationStrategy::HierarchicalAllReduce => {
                self.hierarchical_all_reduce_tp(gradient).await
            }
            CommunicationStrategy::RingAllReduce => self.ring_all_reduce_tp(gradient).await,
            CommunicationStrategy::TreeAllReduce => self.tree_all_reduce_tp(gradient).await,
            CommunicationStrategy::Adaptive => {
                // Choose strategy based on gradient size
                let gradient_size = gradient.numel() * std::mem::size_of::<f32>();
                if gradient_size < 1024 * 1024 {
                    self.tree_all_reduce_tp(gradient).await
                } else {
                    self.ring_all_reduce_tp(gradient).await
                }
            }
        }
    }

    /// Standard all-reduce for tensor parallel gradients
    async fn standard_all_reduce_tp(&self, _gradient: &Tensor<f32>) -> TorshResult<()> {
        // Simplified implementation
        tokio::time::sleep(Duration::from_micros(50)).await;
        Ok(())
    }

    /// Hierarchical all-reduce for tensor parallel gradients
    async fn hierarchical_all_reduce_tp(&self, _gradient: &Tensor<f32>) -> TorshResult<()> {
        tokio::time::sleep(Duration::from_micros(40)).await;
        Ok(())
    }

    /// Ring all-reduce for tensor parallel gradients
    async fn ring_all_reduce_tp(&self, _gradient: &Tensor<f32>) -> TorshResult<()> {
        tokio::time::sleep(Duration::from_micros(60)).await;
        Ok(())
    }

    /// Tree all-reduce for tensor parallel gradients
    async fn tree_all_reduce_tp(&self, _gradient: &Tensor<f32>) -> TorshResult<()> {
        tokio::time::sleep(Duration::from_micros(30)).await;
        Ok(())
    }

    /// Synchronize shared embeddings across pipeline stages
    async fn synchronize_shared_embeddings(&self, model_shards: &ModelShards) -> TorshResult<()> {
        // Find embedding layers and synchronize their gradients
        for stage_layers in model_shards.pipeline_stages.iter() {
            for layer_shard in stage_layers {
                if matches!(
                    layer_shard.layer_type,
                    super::model_shards::LayerType::Embedding
                ) {
                    if let Some(ref grad_weight) = layer_shard.grad_weight {
                        self.all_reduce_pipeline_parallel(grad_weight).await?;
                    }
                }
            }
        }
        Ok(())
    }

    /// All-reduce gradients across pipeline parallel dimension
    async fn all_reduce_pipeline_parallel(&self, _gradient: &Tensor<f32>) -> TorshResult<()> {
        // Simplified implementation
        tokio::time::sleep(Duration::from_micros(100)).await;
        Ok(())
    }

    /// Create gradient buckets for efficient data parallel synchronization
    async fn create_gradient_buckets(
        &self,
        model_shards: &ModelShards,
    ) -> TorshResult<Vec<GradientBucket>> {
        let mut buckets = Vec::new();
        let mut current_bucket = GradientBucket::new();
        let bucket_size_bytes = (self.bucket_config.bucket_size_mb * 1024.0 * 1024.0) as usize;

        // Iterate through all gradients in our pipeline stages
        for (stage_idx, stage_layers) in model_shards.pipeline_stages.iter().enumerate() {
            if stage_idx != self.rank_mapping.pp_rank {
                continue; // Skip stages not owned by this rank
            }

            for layer_shard in stage_layers {
                // Add weight gradients to bucket
                if let Some(ref grad_weight) = layer_shard.grad_weight {
                    let gradient_size = grad_weight.numel() * std::mem::size_of::<f32>();

                    if current_bucket.size_bytes + gradient_size > bucket_size_bytes
                        && !current_bucket.gradients.is_empty()
                    {
                        // Start new bucket
                        buckets.push(current_bucket);
                        current_bucket = GradientBucket::new();
                    }

                    current_bucket.add_gradient(
                        format!("layer_{}_weight", layer_shard.layer_id),
                        grad_weight.clone(),
                    );
                }

                // Add bias gradients to bucket
                if let Some(ref grad_bias) = layer_shard.grad_bias {
                    let gradient_size = grad_bias.numel() * std::mem::size_of::<f32>();

                    if current_bucket.size_bytes + gradient_size > bucket_size_bytes
                        && !current_bucket.gradients.is_empty()
                    {
                        buckets.push(current_bucket);
                        current_bucket = GradientBucket::new();
                    }

                    current_bucket.add_gradient(
                        format!("layer_{}_bias", layer_shard.layer_id),
                        grad_bias.clone(),
                    );
                }
            }
        }

        // Add the last bucket if it has gradients
        if !current_bucket.gradients.is_empty() {
            buckets.push(current_bucket);
        }

        Ok(buckets)
    }

    /// Synchronize a gradient bucket across data parallel replicas
    async fn synchronize_gradient_bucket(&self, bucket: &GradientBucket) -> TorshResult<()> {
        let start_time = Instant::now();

        // Apply gradient compression if enabled
        let compressed_gradients = if self.compression_config.enable_compression {
            self.compress_gradients(&bucket.gradients).await?
        } else {
            bucket.gradients.clone()
        };

        // Perform all-reduce across data parallel dimension
        for gradient in compressed_gradients.values() {
            self.all_reduce_data_parallel(gradient).await?;
        }

        // Decompress gradients if compression was used
        if self.compression_config.enable_compression {
            self.decompress_gradients(&compressed_gradients).await?;
        }

        // Update statistics
        let mut stats = self.sync_stats.lock().expect("lock should not be poisoned");
        stats.total_buckets_synced += 1;
        stats.total_bucket_sync_time += start_time.elapsed();

        Ok(())
    }

    /// All-reduce gradients across data parallel dimension
    async fn all_reduce_data_parallel(&self, _gradient: &Tensor<f32>) -> TorshResult<()> {
        // Use the configured communication strategy
        match self.config.comm_strategy {
            CommunicationStrategy::AllReduce => {
                tokio::time::sleep(Duration::from_micros(80)).await;
            }
            CommunicationStrategy::HierarchicalAllReduce => {
                tokio::time::sleep(Duration::from_micros(60)).await;
            }
            CommunicationStrategy::RingAllReduce => {
                tokio::time::sleep(Duration::from_micros(100)).await;
            }
            CommunicationStrategy::TreeAllReduce => {
                tokio::time::sleep(Duration::from_micros(50)).await;
            }
            CommunicationStrategy::Adaptive => {
                tokio::time::sleep(Duration::from_micros(70)).await;
            }
        }

        // Scale gradients by the number of data parallel replicas
        // gradient /= self.config.dp_size as f32;

        Ok(())
    }

    /// Compress gradients for efficient communication
    async fn compress_gradients(
        &self,
        gradients: &HashMap<String, Tensor<f32>>,
    ) -> TorshResult<HashMap<String, Tensor<f32>>> {
        let mut compressed = HashMap::new();

        for (name, gradient) in gradients {
            // Apply quantization-based compression
            let compressed_gradient = self.quantize_gradient(gradient).await?;
            compressed.insert(name.clone(), compressed_gradient);
        }

        Ok(compressed)
    }

    /// Decompress gradients after communication
    async fn decompress_gradients(
        &self,
        gradients: &HashMap<String, Tensor<f32>>,
    ) -> TorshResult<()> {
        // Apply error feedback and dequantization
        for gradient in gradients.values() {
            self.dequantize_gradient(gradient).await?;
        }
        Ok(())
    }

    /// Quantize gradient for compression
    async fn quantize_gradient(&self, gradient: &Tensor<f32>) -> TorshResult<Tensor<f32>> {
        // Simplified quantization to lower precision
        tokio::time::sleep(Duration::from_micros(10)).await;
        Ok(gradient.clone()) // Would apply actual quantization
    }

    /// Dequantize gradient after communication
    async fn dequantize_gradient(&self, _gradient: &Tensor<f32>) -> TorshResult<()> {
        tokio::time::sleep(Duration::from_micros(5)).await;
        Ok(()) // Would apply actual dequantization
    }

    /// Get synchronization statistics
    pub fn get_sync_stats(&self) -> SyncStatistics {
        self.sync_stats
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Update gradient compression configuration
    pub fn update_compression_config(&mut self, config: GradientCompressionConfig) {
        self.compression_config = config;
    }

    /// Clear gradient buffers
    pub fn clear_buffers(&self) {
        let mut buffers = self
            .gradient_buffers
            .lock()
            .expect("lock should not be poisoned");
        buffers.clear();
    }
}

/// Gradient bucket for batching communications
#[derive(Debug, Clone)]
struct GradientBucket {
    gradients: HashMap<String, Tensor<f32>>,
    size_bytes: usize,
}

impl GradientBucket {
    fn new() -> Self {
        Self {
            gradients: HashMap::new(),
            size_bytes: 0,
        }
    }

    fn add_gradient(&mut self, name: String, gradient: Tensor<f32>) {
        let gradient_size = gradient.numel() * std::mem::size_of::<f32>();
        self.size_bytes += gradient_size;
        self.gradients.insert(name, gradient);
    }
}

/// Gradient buffer for accumulation
#[derive(Debug, Clone)]
struct GradientBuffer {
    accumulated_gradient: Tensor<f32>,
    accumulation_count: usize,
}

/// Synchronization statistics
#[derive(Debug, Clone)]
pub struct SyncStatistics {
    pub total_sync_operations: u64,
    pub total_sync_time: Duration,
    pub total_buckets_synced: u64,
    pub total_bucket_sync_time: Duration,
    pub average_sync_time_ms: f64,
    pub communication_efficiency: f64,
}

impl SyncStatistics {
    fn new() -> Self {
        Self {
            total_sync_operations: 0,
            total_sync_time: Duration::ZERO,
            total_buckets_synced: 0,
            total_bucket_sync_time: Duration::ZERO,
            average_sync_time_ms: 0.0,
            communication_efficiency: 1.0,
        }
    }

    /// Update average sync time
    pub fn update_average_sync_time(&mut self) {
        if self.total_sync_operations > 0 {
            self.average_sync_time_ms =
                self.total_sync_time.as_secs_f64() * 1000.0 / self.total_sync_operations as f64;
        }
    }
}

/// Gradient compression configuration
#[derive(Debug, Clone)]
pub struct GradientCompressionConfig {
    pub enable_compression: bool,
    pub compression_ratio: f32,
    pub error_feedback: bool,
    pub quantization_bits: u32,
}

/// Gradient bucketing configuration
#[derive(Debug, Clone)]
pub struct GradientBucketingConfig {
    pub bucket_size_mb: f32,
    pub max_buckets: usize,
    pub overlap_communication: bool,
}