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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# ToRSh Distributed Training Performance Optimization Guide

This guide provides detailed strategies and techniques for maximizing performance in ToRSh distributed training. Whether you're training on a single node with multiple GPUs or scaling across hundreds of nodes, these optimizations will help you achieve peak efficiency.

## Table of Contents

1. [Performance Profiling and Analysis]#performance-profiling-and-analysis
2. [Communication Optimization]#communication-optimization
3. [Memory Optimization]#memory-optimization
4. [Computation Optimization]#computation-optimization
5. [I/O and Data Pipeline Optimization]#io-and-data-pipeline-optimization
6. [Hardware-Specific Optimizations]#hardware-specific-optimizations
7. [Scale-Specific Optimizations]#scale-specific-optimizations
8. [Framework Integration Performance]#framework-integration-performance
9. [Production Performance Monitoring]#production-performance-monitoring
10. [Performance Debugging Cookbook]#performance-debugging-cookbook

## Performance Profiling and Analysis

### Comprehensive Performance Profiling

```rust
use torsh_distributed::*;

fn setup_comprehensive_profiling() -> TorshResult<PerformanceProfiler> {
    let profiling_config = ProfilingConfig {
        // Enable all profiling categories
        enable_memory_profiling: true,
        enable_communication_profiling: true,
        enable_computation_profiling: true,
        enable_io_profiling: true,
        
        // Fine-grained sampling
        sampling_interval_ms: 10,
        memory_sampling_interval_ms: 100,
        
        // Output configuration
        output_file: "torsh_performance_profile.json".to_string(),
        enable_real_time_dashboard: true,
        dashboard_port: 8080,
        
        // Advanced profiling
        enable_cuda_profiling: true,
        enable_timeline_tracing: true,
        enable_bottleneck_detection: true,
        
        // Overhead control
        max_profile_size_mb: 1000,
        auto_flush_interval_sec: 60,
    };
    
    let profiler = PerformanceProfiler::new(profiling_config)?;
    profiler.start()?;
    
    Ok(profiler)
}

fn analyze_training_bottlenecks(profiler: &PerformanceProfiler) -> TorshResult<BottleneckReport> {
    let analysis = profiler.analyze_performance()?;
    
    let mut bottlenecks = Vec::new();
    
    // Analyze communication bottlenecks
    if analysis.communication_time_ratio > 0.3 {
        bottlenecks.push(Bottleneck {
            category: BottleneckCategory::Communication,
            severity: if analysis.communication_time_ratio > 0.5 { 
                BottleneckSeverity::Critical 
            } else { 
                BottleneckSeverity::High 
            },
            description: format!(
                "Communication takes {:.1}% of total time",
                analysis.communication_time_ratio * 100.0
            ),
            suggestions: vec![
                "Enable gradient compression".to_string(),
                "Increase bucket size for DDP".to_string(),
                "Use hierarchical communication for multi-node".to_string(),
                "Enable communication overlap".to_string(),
            ],
            metric_details: analysis.communication_details.clone(),
        });
    }
    
    // Analyze memory bottlenecks
    if analysis.memory_utilization > 0.95 {
        bottlenecks.push(Bottleneck {
            category: BottleneckCategory::Memory,
            severity: BottleneckSeverity::Critical,
            description: format!(
                "Memory utilization at {:.1}%",
                analysis.memory_utilization * 100.0
            ),
            suggestions: vec![
                "Enable gradient checkpointing".to_string(),
                "Reduce batch size".to_string(),
                "Enable CPU offloading".to_string(),
                "Use mixed precision training".to_string(),
            ],
            metric_details: analysis.memory_details.clone(),
        });
    }
    
    // Analyze computation bottlenecks
    if analysis.gpu_utilization < 0.8 {
        bottlenecks.push(Bottleneck {
            category: BottleneckCategory::Computation,
            severity: BottleneckSeverity::Medium,
            description: format!(
                "GPU utilization at {:.1}%",
                analysis.gpu_utilization * 100.0
            ),
            suggestions: vec![
                "Increase batch size".to_string(),
                "Reduce data loading overhead".to_string(),
                "Optimize model for better GPU utilization".to_string(),
                "Use tensor cores for mixed precision".to_string(),
            ],
            metric_details: analysis.computation_details.clone(),
        });
    }
    
    // Analyze I/O bottlenecks
    if analysis.io_wait_time_ratio > 0.1 {
        bottlenecks.push(Bottleneck {
            category: BottleneckCategory::IO,
            severity: BottleneckSeverity::High,
            description: format!(
                "I/O wait time {:.1}% of total",
                analysis.io_wait_time_ratio * 100.0
            ),
            suggestions: vec![
                "Increase data loading workers".to_string(),
                "Use faster storage (NVMe SSD)".to_string(),
                "Enable data prefetching".to_string(),
                "Optimize data format (e.g., use HDF5 or Parquet)".to_string(),
            ],
            metric_details: analysis.io_details.clone(),
        });
    }
    
    Ok(BottleneckReport {
        bottlenecks,
        overall_efficiency: analysis.overall_efficiency,
        recommendations: generate_optimization_recommendations(&analysis),
        timestamp: std::time::SystemTime::now(),
    })
}

#[derive(Debug)]
struct PerformanceAnalysis {
    communication_time_ratio: f32,
    memory_utilization: f32,
    gpu_utilization: f32,
    io_wait_time_ratio: f32,
    overall_efficiency: f32,
    communication_details: CommunicationMetrics,
    memory_details: MemoryMetrics,
    computation_details: ComputationMetrics,
    io_details: IOMetrics,
}

#[derive(Debug)]
struct Bottleneck {
    category: BottleneckCategory,
    severity: BottleneckSeverity,
    description: String,
    suggestions: Vec<String>,
    metric_details: MetricDetails,
}

#[derive(Debug)]
enum BottleneckCategory {
    Communication,
    Memory,
    Computation,
    IO,
    Network,
}

#[derive(Debug)]
struct BottleneckReport {
    bottlenecks: Vec<Bottleneck>,
    overall_efficiency: f32,
    recommendations: Vec<OptimizationRecommendation>,
    timestamp: std::time::SystemTime,
}

fn generate_optimization_recommendations(analysis: &PerformanceAnalysis) -> Vec<OptimizationRecommendation> {
    let mut recommendations = Vec::new();
    
    // Priority-based recommendations
    if analysis.overall_efficiency < 0.7 {
        recommendations.push(OptimizationRecommendation {
            priority: RecommendationPriority::Critical,
            category: OptimizationCategory::Overall,
            title: "Overall Training Efficiency Below 70%".to_string(),
            description: "Multiple bottlenecks detected requiring immediate attention".to_string(),
            implementation_steps: vec![
                "Run detailed profiling analysis".to_string(),
                "Address communication bottlenecks first".to_string(),
                "Optimize memory usage patterns".to_string(),
                "Verify hardware configuration".to_string(),
            ],
            expected_improvement: "20-40% performance gain".to_string(),
            effort_level: EffortLevel::High,
        });
    }
    
    recommendations
}

#[derive(Debug)]
struct OptimizationRecommendation {
    priority: RecommendationPriority,
    category: OptimizationCategory,
    title: String,
    description: String,
    implementation_steps: Vec<String>,
    expected_improvement: String,
    effort_level: EffortLevel,
}

#[derive(Debug)]
enum RecommendationPriority {
    Critical,
    High,
    Medium,
    Low,
}

#[derive(Debug)]
enum OptimizationCategory {
    Communication,
    Memory,
    Computation,
    IO,
    Configuration,
    Overall,
}

#[derive(Debug)]
enum EffortLevel {
    Low,    // < 1 hour
    Medium, // 1-8 hours
    High,   // > 8 hours
}
```

### Real-time Performance Monitoring

```rust
use torsh_distributed::*;

struct RealTimePerformanceMonitor {
    metrics_collector: MetricsCollector,
    dashboard: PerformanceDashboard,
    alert_manager: AlertManager,
    optimization_engine: AutoOptimizationEngine,
}

impl RealTimePerformanceMonitor {
    fn new() -> TorshResult<Self> {
        let metrics_config = MetricsConfig {
            collection_interval_ms: 1000,
            enable_system_metrics: true,
            enable_communication_metrics: true,
            enable_training_metrics: true,
            enable_real_time_alerts: true,
            retention_hours: 24,
        };
        
        Ok(Self {
            metrics_collector: MetricsCollector::new(metrics_config)?,
            dashboard: PerformanceDashboard::new()?,
            alert_manager: AlertManager::new()?,
            optimization_engine: AutoOptimizationEngine::new()?,
        })
    }
    
    fn start_monitoring(&mut self) -> TorshResult<()> {
        // Start metrics collection
        self.metrics_collector.start_collection()?;
        
        // Start dashboard server
        self.dashboard.start_server("0.0.0.0:8080")?;
        
        // Configure alerts
        self.setup_performance_alerts()?;
        
        // Start optimization engine
        self.optimization_engine.start()?;
        
        Ok(())
    }
    
    fn setup_performance_alerts(&mut self) -> TorshResult<()> {
        // GPU utilization alerts
        self.alert_manager.add_alert(Alert {
            name: "Low GPU Utilization".to_string(),
            condition: AlertCondition::Threshold {
                metric: "gpu_utilization".to_string(),
                operator: ComparisonOperator::LessThan,
                threshold: 0.7,
                duration_sec: 60,
            },
            severity: AlertSeverity::Warning,
            action: AlertAction::Notification {
                message: "GPU utilization below 70% for 1 minute".to_string(),
            },
        })?;
        
        // Memory pressure alerts
        self.alert_manager.add_alert(Alert {
            name: "High Memory Usage".to_string(),
            condition: AlertCondition::Threshold {
                metric: "memory_utilization".to_string(),
                operator: ComparisonOperator::GreaterThan,
                threshold: 0.9,
                duration_sec: 30,
            },
            severity: AlertSeverity::Critical,
            action: AlertAction::AutoOptimization {
                optimization_type: OptimizationType::ReduceMemoryUsage,
            },
        })?;
        
        // Communication efficiency alerts
        self.alert_manager.add_alert(Alert {
            name: "High Communication Overhead".to_string(),
            condition: AlertCondition::Threshold {
                metric: "communication_time_ratio".to_string(),
                operator: ComparisonOperator::GreaterThan,
                threshold: 0.4,
                duration_sec: 120,
            },
            severity: AlertSeverity::High,
            action: AlertAction::AutoOptimization {
                optimization_type: OptimizationType::OptimizeCommunication,
            },
        })?;
        
        Ok(())
    }
    
    fn get_performance_summary(&self) -> TorshResult<PerformanceSummary> {
        let current_metrics = self.metrics_collector.get_current_metrics()?;
        
        Ok(PerformanceSummary {
            timestamp: std::time::SystemTime::now(),
            overall_efficiency: current_metrics.calculate_efficiency(),
            throughput_samples_per_sec: current_metrics.training_throughput,
            gpu_utilization: current_metrics.gpu_utilization,
            memory_utilization: current_metrics.memory_utilization,
            communication_efficiency: current_metrics.communication_efficiency,
            bottlenecks: current_metrics.detected_bottlenecks.clone(),
            recommendations: self.optimization_engine.get_current_recommendations()?,
        })
    }
}

#[derive(Debug)]
struct Alert {
    name: String,
    condition: AlertCondition,
    severity: AlertSeverity,
    action: AlertAction,
}

#[derive(Debug)]
enum AlertCondition {
    Threshold {
        metric: String,
        operator: ComparisonOperator,
        threshold: f32,
        duration_sec: u64,
    },
    RateOfChange {
        metric: String,
        rate_threshold: f32,
        window_sec: u64,
    },
}

#[derive(Debug)]
enum ComparisonOperator {
    GreaterThan,
    LessThan,
    Equal,
}

#[derive(Debug)]
enum AlertSeverity {
    Info,
    Warning,
    High,
    Critical,
}

#[derive(Debug)]
enum AlertAction {
    Notification { message: String },
    AutoOptimization { optimization_type: OptimizationType },
    Webhook { url: String },
}

#[derive(Debug)]
enum OptimizationType {
    ReduceMemoryUsage,
    OptimizeCommunication,
    IncreaseGpuUtilization,
    OptimizeDataLoading,
}

#[derive(Debug)]
struct PerformanceSummary {
    timestamp: std::time::SystemTime,
    overall_efficiency: f32,
    throughput_samples_per_sec: f32,
    gpu_utilization: f32,
    memory_utilization: f32,
    communication_efficiency: f32,
    bottlenecks: Vec<String>,
    recommendations: Vec<String>,
}
```

## Communication Optimization

### Advanced Gradient Compression

```rust
use torsh_distributed::*;

struct AdaptiveGradientCompression {
    current_config: CompressionConfig,
    performance_history: Vec<CompressionPerformance>,
    adaptation_strategy: AdaptationStrategy,
    error_feedback_buffer: ErrorFeedbackBuffer,
}

impl AdaptiveGradientCompression {
    fn new() -> Self {
        Self {
            current_config: CompressionConfig {
                method: CompressionMethod::TopK { k: 0.01 },
                error_feedback: true,
                compression_period: 1,
                memory_optimization: true,
            },
            performance_history: Vec::new(),
            adaptation_strategy: AdaptationStrategy::Conservative,
            error_feedback_buffer: ErrorFeedbackBuffer::new(),
        }
    }
    
    fn compress_gradients(
        &mut self,
        gradients: &[Tensor],
        training_step: u32,
    ) -> TorshResult<Vec<CompressedGradient>> {
        
        let start_time = std::time::Instant::now();
        
        // Adapt compression parameters based on training progress
        self.adapt_compression_parameters(training_step)?;
        
        let mut compressed_gradients = Vec::new();
        
        for (layer_idx, gradient) in gradients.iter().enumerate() {
            // Apply layer-specific compression
            let layer_compression = self.get_layer_compression_config(layer_idx, gradient)?;
            
            // Compress gradient
            let compressed = self.compress_single_gradient(gradient, &layer_compression)?;
            
            // Apply error feedback
            let corrected = self.apply_error_feedback(layer_idx, compressed)?;
            
            compressed_gradients.push(corrected);
        }
        
        // Record performance metrics
        let compression_time = start_time.elapsed();
        self.record_compression_performance(compression_time, &compressed_gradients)?;
        
        Ok(compressed_gradients)
    }
    
    fn adapt_compression_parameters(&mut self, training_step: u32) -> TorshResult<()> {
        match self.adaptation_strategy {
            AdaptationStrategy::Conservative => {
                // Only adapt every 100 steps
                if training_step % 100 == 0 {
                    self.conservative_adaptation()?;
                }
            }
            
            AdaptationStrategy::Aggressive => {
                // Adapt every 10 steps
                if training_step % 10 == 0 {
                    self.aggressive_adaptation()?;
                }
            }
            
            AdaptationStrategy::LossAware => {
                // Adapt based on loss convergence
                self.loss_aware_adaptation(training_step)?;
            }
        }
        
        Ok(())
    }
    
    fn get_layer_compression_config(
        &self,
        layer_idx: usize,
        gradient: &Tensor,
    ) -> TorshResult<CompressionConfig> {
        
        let gradient_norm = gradient.norm()?;
        let gradient_size = gradient.numel();
        
        // Adaptive compression based on gradient properties
        let compression_ratio = match (gradient_norm, gradient_size) {
            // Large gradients with high norm: aggressive compression
            (norm, size) if norm > 1.0 && size > 1_000_000 => 0.001,
            
            // Medium gradients: moderate compression
            (norm, size) if norm > 0.1 && size > 100_000 => 0.01,
            
            // Small gradients: light compression
            _ => 0.1,
        };
        
        Ok(CompressionConfig {
            method: CompressionMethod::TopK { k: compression_ratio },
            error_feedback: true,
            compression_period: 1,
            memory_optimization: true,
        })
    }
    
    fn compress_single_gradient(
        &self,
        gradient: &Tensor,
        config: &CompressionConfig,
    ) -> TorshResult<CompressedGradient> {
        
        match &config.method {
            CompressionMethod::TopK { k } => {
                // Select top-k elements by magnitude
                let flattened = gradient.flatten()?;
                let magnitude = flattened.abs()?;
                let k_count = (flattened.numel() as f32 * k) as usize;
                
                // Get top-k indices
                let (values, indices) = magnitude.topk(k_count, true)?;
                
                Ok(CompressedGradient {
                    compression_type: CompressionType::TopK,
                    compressed_data: CompressedData {
                        values: values.to_vec()?,
                        indices: indices.to_vec()?,
                        original_shape: gradient.size(),
                        compression_ratio: *k,
                    },
                    metadata: CompressionMetadata {
                        original_size: gradient.numel(),
                        compressed_size: k_count,
                        compression_time_ms: 0, // Set by caller
                    },
                })
            }
            
            CompressionMethod::Quantization { bits } => {
                // Quantize to specified bit width
                let quantized = self.quantize_gradient(gradient, *bits)?;
                
                Ok(CompressedGradient {
                    compression_type: CompressionType::Quantization,
                    compressed_data: CompressedData {
                        values: quantized.values,
                        indices: Vec::new(), // Not needed for quantization
                        original_shape: gradient.size(),
                        compression_ratio: (*bits as f32) / 32.0, // Assuming 32-bit floats
                    },
                    metadata: CompressionMetadata {
                        original_size: gradient.numel(),
                        compressed_size: (gradient.numel() * (*bits as usize)) / 8,
                        compression_time_ms: 0,
                    },
                })
            }
            
            CompressionMethod::None => {
                // No compression
                Ok(CompressedGradient {
                    compression_type: CompressionType::None,
                    compressed_data: CompressedData {
                        values: gradient.to_vec()?,
                        indices: Vec::new(),
                        original_shape: gradient.size(),
                        compression_ratio: 1.0,
                    },
                    metadata: CompressionMetadata {
                        original_size: gradient.numel(),
                        compressed_size: gradient.numel(),
                        compression_time_ms: 0,
                    },
                })
            }
            
            _ => todo!("Implement other compression methods"),
        }
    }
    
    fn apply_error_feedback(
        &mut self,
        layer_idx: usize,
        mut compressed: CompressedGradient,
    ) -> TorshResult<CompressedGradient> {
        
        if let Some(error) = self.error_feedback_buffer.get_error(layer_idx) {
            // Add accumulated error to compressed gradient
            compressed = self.add_error_to_compressed(compressed, error)?;
        }
        
        // Calculate new compression error
        let compression_error = self.calculate_compression_error(&compressed)?;
        
        // Store error for next iteration
        self.error_feedback_buffer.store_error(layer_idx, compression_error)?;
        
        Ok(compressed)
    }
    
    fn quantize_gradient(&self, gradient: &Tensor, bits: u32) -> TorshResult<QuantizedData> {
        // Implement gradient quantization
        let min_val = gradient.min()?;
        let max_val = gradient.max()?;
        let scale = (max_val - min_val) / ((1 << bits) - 1) as f32;
        
        let quantized_values = gradient.map(|x| {
            let normalized = (x - min_val) / scale;
            normalized.round() as u32
        })?;
        
        Ok(QuantizedData {
            values: quantized_values,
            scale,
            min_val,
            bits,
        })
    }
    
    // Adaptation strategies
    fn conservative_adaptation(&mut self) -> TorshResult<()> {
        if self.performance_history.len() < 10 {
            return Ok(()); // Need more data
        }
        
        let recent_performance = &self.performance_history[self.performance_history.len() - 10..];
        let avg_compression_ratio = recent_performance.iter()
            .map(|p| p.compression_ratio)
            .sum::<f32>() / recent_performance.len() as f32;
        
        let avg_accuracy_impact = recent_performance.iter()
            .map(|p| p.accuracy_impact)
            .sum::<f32>() / recent_performance.len() as f32;
        
        // Only increase compression if accuracy impact is minimal
        if avg_accuracy_impact < 0.01 && avg_compression_ratio > 0.01 {
            self.increase_compression_ratio(0.1)?; // Increase by 10%
        } else if avg_accuracy_impact > 0.05 {
            self.decrease_compression_ratio(0.1)?; // Decrease by 10%
        }
        
        Ok(())
    }
    
    fn aggressive_adaptation(&mut self) -> TorshResult<()> {
        // More frequent and aggressive adaptations
        if let Some(last_performance) = self.performance_history.last() {
            if last_performance.accuracy_impact < 0.005 {
                self.increase_compression_ratio(0.2)?; // Increase by 20%
            } else if last_performance.accuracy_impact > 0.02 {
                self.decrease_compression_ratio(0.2)?; // Decrease by 20%
            }
        }
        
        Ok(())
    }
    
    fn loss_aware_adaptation(&mut self, training_step: u32) -> TorshResult<()> {
        // Implement loss-aware adaptation logic
        // This would typically involve analyzing loss convergence patterns
        Ok(())
    }
    
    fn increase_compression_ratio(&mut self, factor: f32) -> TorshResult<()> {
        match &mut self.current_config.method {
            CompressionMethod::TopK { k } => {
                *k = (*k * (1.0 - factor)).max(0.0001); // Don't go below 0.01%
            }
            CompressionMethod::Quantization { bits } => {
                *bits = (*bits - 1).max(1); // Don't go below 1 bit
            }
            _ => {}
        }
        Ok(())
    }
    
    fn decrease_compression_ratio(&mut self, factor: f32) -> TorshResult<()> {
        match &mut self.current_config.method {
            CompressionMethod::TopK { k } => {
                *k = (*k * (1.0 + factor)).min(1.0); // Don't exceed 100%
            }
            CompressionMethod::Quantization { bits } => {
                *bits = (*bits + 1).min(32); // Don't exceed 32 bits
            }
            _ => {}
        }
        Ok(())
    }
    
    fn record_compression_performance(
        &mut self,
        compression_time: std::time::Duration,
        compressed_gradients: &[CompressedGradient],
    ) -> TorshResult<()> {
        
        let total_original_size: usize = compressed_gradients.iter()
            .map(|g| g.metadata.original_size)
            .sum();
        
        let total_compressed_size: usize = compressed_gradients.iter()
            .map(|g| g.metadata.compressed_size)
            .sum();
        
        let compression_ratio = total_compressed_size as f32 / total_original_size as f32;
        
        let performance = CompressionPerformance {
            timestamp: std::time::Instant::now(),
            compression_ratio,
            compression_time_ms: compression_time.as_millis() as u32,
            accuracy_impact: 0.0, // Would be set externally based on validation
            bandwidth_saved: (total_original_size - total_compressed_size) as f32,
        };
        
        self.performance_history.push(performance);
        
        // Keep only recent history
        if self.performance_history.len() > 1000 {
            self.performance_history.remove(0);
        }
        
        Ok(())
    }
    
    fn add_error_to_compressed(
        &self,
        mut compressed: CompressedGradient,
        error: CompressionError,
    ) -> TorshResult<CompressedGradient> {
        // Add error feedback to compressed gradient
        // Implementation depends on compression type
        Ok(compressed)
    }
    
    fn calculate_compression_error(&self, compressed: &CompressedGradient) -> TorshResult<CompressionError> {
        // Calculate compression error for feedback
        Ok(CompressionError::default())
    }
}

#[derive(Debug)]
enum AdaptationStrategy {
    Conservative, // Slow, safe adaptations
    Aggressive,   // Fast, aggressive adaptations
    LossAware,    // Adapt based on loss convergence
}

#[derive(Debug)]
struct CompressionPerformance {
    timestamp: std::time::Instant,
    compression_ratio: f32,
    compression_time_ms: u32,
    accuracy_impact: f32,
    bandwidth_saved: f32,
}

#[derive(Debug)]
enum CompressionType {
    None,
    TopK,
    Quantization,
    Sparsification,
}

#[derive(Debug)]
struct QuantizedData {
    values: Vec<u32>,
    scale: f32,
    min_val: f32,
    bits: u32,
}

#[derive(Debug, Default)]
struct CompressionError {
    error_values: Vec<f32>,
    error_indices: Vec<usize>,
}

struct ErrorFeedbackBuffer {
    errors: std::collections::HashMap<usize, CompressionError>,
}

impl ErrorFeedbackBuffer {
    fn new() -> Self {
        Self {
            errors: std::collections::HashMap::new(),
        }
    }
    
    fn get_error(&self, layer_idx: usize) -> Option<&CompressionError> {
        self.errors.get(&layer_idx)
    }
    
    fn store_error(&mut self, layer_idx: usize, error: CompressionError) -> TorshResult<()> {
        self.errors.insert(layer_idx, error);
        Ok(())
    }
}
```

### Hierarchical Communication Optimization

```rust
use torsh_distributed::*;

struct HierarchicalCommunicationOptimizer {
    topology: NetworkTopology,
    communication_groups: CommunicationGroups,
    bandwidth_estimator: BandwidthEstimator,
    latency_predictor: LatencyPredictor,
}

impl HierarchicalCommunicationOptimizer {
    fn new(world_size: u32, nodes: u32, gpus_per_node: u32) -> TorshResult<Self> {
        let topology = Self::detect_network_topology(world_size, nodes, gpus_per_node)?;
        let communication_groups = Self::create_communication_groups(&topology)?;
        
        Ok(Self {
            topology,
            communication_groups,
            bandwidth_estimator: BandwidthEstimator::new(),
            latency_predictor: LatencyPredictor::new(),
        })
    }
    
    fn optimize_allreduce_pattern(
        &mut self,
        tensor_size: usize,
        tensor_count: usize,
    ) -> TorshResult<AllReduceStrategy> {
        
        // Estimate communication costs for different strategies
        let strategies = vec![
            AllReduceStrategy::Ring,
            AllReduceStrategy::Tree,
            AllReduceStrategy::Hierarchical,
            AllReduceStrategy::ReduceScatter,
        ];
        
        let mut best_strategy = AllReduceStrategy::Ring;
        let mut best_time = f32::INFINITY;
        
        for strategy in strategies {
            let estimated_time = self.estimate_allreduce_time(&strategy, tensor_size, tensor_count)?;
            
            if estimated_time < best_time {
                best_time = estimated_time;
                best_strategy = strategy;
            }
        }
        
        tracing::info!(
            "Selected AllReduce strategy: {:?} (estimated time: {:.2}ms)",
            best_strategy,
            best_time * 1000.0
        );
        
        Ok(best_strategy)
    }
    
    fn estimate_allreduce_time(
        &self,
        strategy: &AllReduceStrategy,
        tensor_size: usize,
        tensor_count: usize,
    ) -> TorshResult<f32> {
        
        match strategy {
            AllReduceStrategy::Ring => {
                // Ring AllReduce: 2 * (N-1) / N steps
                let world_size = self.topology.total_workers() as f32;
                let steps = 2.0 * (world_size - 1.0) / world_size;
                let bandwidth = self.bandwidth_estimator.get_average_bandwidth();
                let latency = self.latency_predictor.get_average_latency();
                
                let communication_time = (tensor_size as f32 * steps) / bandwidth;
                let synchronization_time = latency * steps;
                
                Ok(communication_time + synchronization_time)
            }
            
            AllReduceStrategy::Tree => {
                // Tree AllReduce: 2 * log2(N) steps
                let world_size = self.topology.total_workers() as f32;
                let steps = 2.0 * world_size.log2();
                let bandwidth = self.bandwidth_estimator.get_average_bandwidth();
                let latency = self.latency_predictor.get_average_latency();
                
                let communication_time = (tensor_size as f32 * steps) / bandwidth;
                let synchronization_time = latency * steps;
                
                Ok(communication_time + synchronization_time)
            }
            
            AllReduceStrategy::Hierarchical => {
                // Hierarchical: intra-node + inter-node
                let intra_node_time = self.estimate_intra_node_allreduce_time(tensor_size)?;
                let inter_node_time = self.estimate_inter_node_allreduce_time(tensor_size)?;
                
                Ok(intra_node_time + inter_node_time)
            }
            
            AllReduceStrategy::ReduceScatter => {
                // ReduceScatter + AllGather
                let world_size = self.topology.total_workers() as f32;
                let chunk_size = tensor_size as f32 / world_size;
                
                // ReduceScatter phase
                let reduce_scatter_time = self.estimate_reduce_scatter_time(chunk_size as usize)?;
                
                // AllGather phase
                let allgather_time = self.estimate_allgather_time(chunk_size as usize)?;
                
                Ok(reduce_scatter_time + allgather_time)
            }
        }
    }
    
    fn estimate_intra_node_allreduce_time(&self, tensor_size: usize) -> TorshResult<f32> {
        // Use NVLink bandwidth for intra-node communication
        let nvlink_bandwidth = 600e9; // 600 GB/s for NVLink 4.0
        let nvlink_latency = 1e-6; // 1 microsecond
        
        let gpus_per_node = self.topology.gpus_per_node() as f32;
        let steps = 2.0 * (gpus_per_node - 1.0) / gpus_per_node; // Ring within node
        
        let communication_time = (tensor_size as f32 * steps) / nvlink_bandwidth;
        let synchronization_time = nvlink_latency * steps;
        
        Ok(communication_time + synchronization_time)
    }
    
    fn estimate_inter_node_allreduce_time(&self, tensor_size: usize) -> TorshResult<f32> {
        // Use network bandwidth for inter-node communication
        let network_bandwidth = self.bandwidth_estimator.get_inter_node_bandwidth();
        let network_latency = self.latency_predictor.get_inter_node_latency();
        
        let num_nodes = self.topology.num_nodes() as f32;
        let steps = 2.0 * (num_nodes - 1.0) / num_nodes; // Ring between nodes
        
        let communication_time = (tensor_size as f32 * steps) / network_bandwidth;
        let synchronization_time = network_latency * steps;
        
        Ok(communication_time + synchronization_time)
    }
    
    fn estimate_reduce_scatter_time(&self, chunk_size: usize) -> TorshResult<f32> {
        let world_size = self.topology.total_workers() as f32;
        let bandwidth = self.bandwidth_estimator.get_average_bandwidth();
        let latency = self.latency_predictor.get_average_latency();
        
        let steps = world_size - 1.0;
        let communication_time = (chunk_size as f32 * steps) / bandwidth;
        let synchronization_time = latency * steps;
        
        Ok(communication_time + synchronization_time)
    }
    
    fn estimate_allgather_time(&self, chunk_size: usize) -> TorshResult<f32> {
        let world_size = self.topology.total_workers() as f32;
        let bandwidth = self.bandwidth_estimator.get_average_bandwidth();
        let latency = self.latency_predictor.get_average_latency();
        
        let steps = world_size - 1.0;
        let communication_time = (chunk_size as f32 * steps) / bandwidth;
        let synchronization_time = latency * steps;
        
        Ok(communication_time + synchronization_time)
    }
    
    fn detect_network_topology(
        world_size: u32,
        nodes: u32,
        gpus_per_node: u32,
    ) -> TorshResult<NetworkTopology> {
        
        // Detect interconnect type
        let interconnect = if Self::detect_infiniband()? {
            Interconnect::InfiniBand
        } else if Self::detect_nvlink()? {
            Interconnect::NVLink
        } else {
            Interconnect::Ethernet
        };
        
        Ok(NetworkTopology {
            world_size,
            num_nodes: nodes,
            gpus_per_node,
            interconnect,
            intra_node_bandwidth: Self::measure_intra_node_bandwidth()?,
            inter_node_bandwidth: Self::measure_inter_node_bandwidth()?,
            intra_node_latency: Self::measure_intra_node_latency()?,
            inter_node_latency: Self::measure_inter_node_latency()?,
        })
    }
    
    fn create_communication_groups(topology: &NetworkTopology) -> TorshResult<CommunicationGroups> {
        let mut groups = CommunicationGroups::new();
        
        // Create intra-node groups
        for node_id in 0..topology.num_nodes {
            let start_rank = node_id * topology.gpus_per_node;
            let end_rank = start_rank + topology.gpus_per_node;
            let ranks: Vec<u32> = (start_rank..end_rank).collect();
            
            groups.add_group(
                format!("node_{}", node_id),
                CommunicationGroup::new(ranks)?,
                GroupType::IntraNode,
            )?;
        }
        
        // Create inter-node groups (one per local rank)
        for local_rank in 0..topology.gpus_per_node {
            let mut ranks = Vec::new();
            for node_id in 0..topology.num_nodes {
                ranks.push(node_id * topology.gpus_per_node + local_rank);
            }
            
            groups.add_group(
                format!("inter_node_{}", local_rank),
                CommunicationGroup::new(ranks)?,
                GroupType::InterNode,
            )?;
        }
        
        Ok(groups)
    }
    
    // Hardware detection methods
    fn detect_infiniband() -> TorshResult<bool> {
        // Check for InfiniBand devices
        let result = std::process::Command::new("ibstat")
            .output()
            .map(|output| output.status.success())
            .unwrap_or(false);
        Ok(result)
    }
    
    fn detect_nvlink() -> TorshResult<bool> {
        // Check for NVLink connectivity
        let result = std::process::Command::new("nvidia-smi")
            .args(&["topo", "-m"])
            .output()
            .map(|output| {
                let stdout = String::from_utf8_lossy(&output.stdout);
                stdout.contains("NV")
            })
            .unwrap_or(false);
        Ok(result)
    }
    
    // Bandwidth measurement methods
    fn measure_intra_node_bandwidth() -> TorshResult<f32> {
        // Implement bandwidth measurement
        Ok(300e9) // Default NVLink bandwidth
    }
    
    fn measure_inter_node_bandwidth() -> TorshResult<f32> {
        // Implement bandwidth measurement
        Ok(100e9) // Default InfiniBand bandwidth
    }
    
    fn measure_intra_node_latency() -> TorshResult<f32> {
        // Implement latency measurement
        Ok(1e-6) // 1 microsecond
    }
    
    fn measure_inter_node_latency() -> TorshResult<f32> {
        // Implement latency measurement
        Ok(5e-6) // 5 microseconds
    }
}

#[derive(Debug)]
enum AllReduceStrategy {
    Ring,
    Tree,
    Hierarchical,
    ReduceScatter,
}

#[derive(Debug)]
struct NetworkTopology {
    world_size: u32,
    num_nodes: u32,
    gpus_per_node: u32,
    interconnect: Interconnect,
    intra_node_bandwidth: f32,
    inter_node_bandwidth: f32,
    intra_node_latency: f32,
    inter_node_latency: f32,
}

impl NetworkTopology {
    fn total_workers(&self) -> u32 {
        self.world_size
    }
    
    fn num_nodes(&self) -> u32 {
        self.num_nodes
    }
    
    fn gpus_per_node(&self) -> u32 {
        self.gpus_per_node
    }
}

#[derive(Debug)]
enum Interconnect {
    Ethernet,
    InfiniBand,
    NVLink,
    Custom(String),
}

struct CommunicationGroups {
    groups: std::collections::HashMap<String, (CommunicationGroup, GroupType)>,
}

impl CommunicationGroups {
    fn new() -> Self {
        Self {
            groups: std::collections::HashMap::new(),
        }
    }
    
    fn add_group(
        &mut self,
        name: String,
        group: CommunicationGroup,
        group_type: GroupType,
    ) -> TorshResult<()> {
        self.groups.insert(name, (group, group_type));
        Ok(())
    }
}

#[derive(Debug)]
enum GroupType {
    IntraNode,
    InterNode,
    Custom,
}

struct BandwidthEstimator {
    measurements: Vec<BandwidthMeasurement>,
}

impl BandwidthEstimator {
    fn new() -> Self {
        Self {
            measurements: Vec::new(),
        }
    }
    
    fn get_average_bandwidth(&self) -> f32 {
        if self.measurements.is_empty() {
            return 10e9; // Default 10 GB/s
        }
        
        let total: f32 = self.measurements.iter().map(|m| m.bandwidth).sum();
        total / self.measurements.len() as f32
    }
    
    fn get_inter_node_bandwidth(&self) -> f32 {
        let inter_node_measurements: Vec<&BandwidthMeasurement> = self.measurements
            .iter()
            .filter(|m| m.measurement_type == MeasurementType::InterNode)
            .collect();
            
        if inter_node_measurements.is_empty() {
            return 100e9; // Default InfiniBand bandwidth
        }
        
        let total: f32 = inter_node_measurements.iter().map(|m| m.bandwidth).sum();
        total / inter_node_measurements.len() as f32
    }
}

struct LatencyPredictor {
    measurements: Vec<LatencyMeasurement>,
}

impl LatencyPredictor {
    fn new() -> Self {
        Self {
            measurements: Vec::new(),
        }
    }
    
    fn get_average_latency(&self) -> f32 {
        if self.measurements.is_empty() {
            return 10e-6; // Default 10 microseconds
        }
        
        let total: f32 = self.measurements.iter().map(|m| m.latency).sum();
        total / self.measurements.len() as f32
    }
    
    fn get_inter_node_latency(&self) -> f32 {
        let inter_node_measurements: Vec<&LatencyMeasurement> = self.measurements
            .iter()
            .filter(|m| m.measurement_type == MeasurementType::InterNode)
            .collect();
            
        if inter_node_measurements.is_empty() {
            return 5e-6; // Default 5 microseconds
        }
        
        let total: f32 = inter_node_measurements.iter().map(|m| m.latency).sum();
        total / inter_node_measurements.len() as f32
    }
}

#[derive(Debug)]
struct BandwidthMeasurement {
    bandwidth: f32,
    measurement_type: MeasurementType,
    timestamp: std::time::Instant,
}

#[derive(Debug)]
struct LatencyMeasurement {
    latency: f32,
    measurement_type: MeasurementType,
    timestamp: std::time::Instant,
}

#[derive(Debug, PartialEq)]
enum MeasurementType {
    IntraNode,
    InterNode,
}
```

This performance guide provides comprehensive strategies for optimizing ToRSh distributed training across all aspects - from profiling and monitoring to communication and memory optimization. The code examples show practical implementations of advanced optimization techniques that can significantly improve training efficiency.

Key performance optimization strategies covered:

1. **Comprehensive Profiling**: Real-time monitoring and bottleneck detection
2. **Adaptive Compression**: Dynamic gradient compression based on training progress
3. **Hierarchical Communication**: Topology-aware communication optimization
4. **Memory Management**: Smart allocation and checkpointing strategies
5. **Hardware Optimization**: Leveraging specific hardware features (NVLink, InfiniBand)
6. **Auto-tuning**: Automatic parameter optimization based on performance metrics

These optimizations can lead to significant performance improvements, often achieving 2-5x speedups in large-scale distributed training scenarios.