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
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
//! Collective communication operations

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use crate::backend::ReduceOp;
use crate::process_group::ProcessGroup;
use crate::TorshResult;
use log::info;
use std::collections::HashMap;
use std::sync::Arc;
use torsh_core::dtype::FloatElement;
use torsh_tensor::Tensor;

/// Communication group for selective collective operations
#[derive(Debug, Clone)]
pub struct CommunicationGroup {
    /// Group identifier
    pub group_id: String,
    /// Ranks participating in this group
    pub ranks: Vec<u32>,
    /// Local rank within this group (0-indexed within the group)
    pub local_rank: u32,
    /// Size of this group
    pub group_size: u32,
    /// Global rank to local rank mapping
    pub global_to_local: HashMap<u32, u32>,
    /// Local rank to global rank mapping
    pub local_to_global: HashMap<u32, u32>,
}

impl CommunicationGroup {
    /// Create a new communication group
    pub fn new(group_id: String, ranks: Vec<u32>, current_global_rank: u32) -> TorshResult<Self> {
        if ranks.is_empty() {
            return Err(crate::TorshDistributedError::invalid_argument(
                "ranks",
                "Communication group cannot be empty",
                "non-empty vector of ranks",
            ));
        }

        if !ranks.contains(&current_global_rank) {
            return Err(crate::TorshDistributedError::invalid_argument(
                "current_global_rank",
                format!(
                    "Current rank {} not in group {:?}",
                    current_global_rank, ranks
                ),
                "rank that exists in the group",
            ));
        }

        let mut sorted_ranks = ranks.clone();
        sorted_ranks.sort_unstable();

        let mut global_to_local = HashMap::new();
        let mut local_to_global = HashMap::new();

        for (local_idx, &global_rank) in sorted_ranks.iter().enumerate() {
            global_to_local.insert(global_rank, local_idx as u32);
            local_to_global.insert(local_idx as u32, global_rank);
        }

        let local_rank = global_to_local[&current_global_rank];
        let group_size = sorted_ranks.len() as u32;

        Ok(Self {
            group_id,
            ranks: sorted_ranks,
            local_rank,
            group_size,
            global_to_local,
            local_to_global,
        })
    }

    /// Create a communication group for a range of ranks
    pub fn from_range(
        group_id: String,
        start_rank: u32,
        end_rank: u32,
        current_global_rank: u32,
    ) -> TorshResult<Self> {
        if start_rank >= end_rank {
            return Err(crate::TorshDistributedError::invalid_argument(
                "rank_range",
                "start_rank must be less than end_rank",
                "valid rank range where start < end",
            ));
        }

        let ranks: Vec<u32> = (start_rank..end_rank).collect();
        Self::new(group_id, ranks, current_global_rank)
    }

    /// Check if a global rank is in this group
    pub fn contains_rank(&self, global_rank: u32) -> bool {
        self.global_to_local.contains_key(&global_rank)
    }

    /// Get local rank for a global rank
    pub fn global_to_local_rank(&self, global_rank: u32) -> Option<u32> {
        self.global_to_local.get(&global_rank).copied()
    }

    /// Get global rank for a local rank
    pub fn local_to_global_rank(&self, local_rank: u32) -> Option<u32> {
        self.local_to_global.get(&local_rank).copied()
    }
}

/// Group manager for managing multiple communication groups
#[derive(Debug, Default)]
pub struct GroupManager {
    groups: HashMap<String, Arc<CommunicationGroup>>,
    current_global_rank: u32,
}

impl GroupManager {
    /// Create a new group manager
    pub fn new(current_global_rank: u32) -> Self {
        Self {
            groups: HashMap::new(),
            current_global_rank,
        }
    }

    /// Create and register a new communication group
    pub fn create_group(
        &mut self,
        group_id: String,
        ranks: Vec<u32>,
    ) -> TorshResult<Arc<CommunicationGroup>> {
        if self.groups.contains_key(&group_id) {
            return Err(crate::TorshDistributedError::invalid_argument(
                "group_id",
                format!("Group '{}' already exists", group_id),
                "unique group identifier",
            ));
        }

        let group = Arc::new(CommunicationGroup::new(
            group_id.clone(),
            ranks,
            self.current_global_rank,
        )?);
        self.groups.insert(group_id, Arc::clone(&group));
        Ok(group)
    }

    /// Create a communication group from a rank range
    pub fn create_group_from_range(
        &mut self,
        group_id: String,
        start_rank: u32,
        end_rank: u32,
    ) -> TorshResult<Arc<CommunicationGroup>> {
        if self.groups.contains_key(&group_id) {
            return Err(crate::TorshDistributedError::invalid_argument(
                "group_id",
                format!("Group '{}' already exists", group_id),
                "unique group identifier",
            ));
        }

        let group = Arc::new(CommunicationGroup::from_range(
            group_id.clone(),
            start_rank,
            end_rank,
            self.current_global_rank,
        )?);
        self.groups.insert(group_id, Arc::clone(&group));
        Ok(group)
    }

    /// Get a communication group by ID
    pub fn get_group(&self, group_id: &str) -> Option<Arc<CommunicationGroup>> {
        self.groups.get(group_id).cloned()
    }

    /// Remove a communication group
    pub fn remove_group(&mut self, group_id: &str) -> bool {
        self.groups.remove(group_id).is_some()
    }

    /// List all group IDs
    pub fn list_groups(&self) -> Vec<String> {
        self.groups.keys().cloned().collect()
    }

    /// Create predefined groups for common parallelism patterns
    pub fn create_standard_groups(
        &mut self,
        world_size: u32,
        data_parallel_size: u32,
        model_parallel_size: u32,
    ) -> TorshResult<()> {
        if data_parallel_size * model_parallel_size != world_size {
            return Err(crate::TorshDistributedError::invalid_argument(
                "parallelism_configuration",
                "data_parallel_size * model_parallel_size must equal world_size",
                format!(
                    "configuration where {} * {} = {}",
                    data_parallel_size, model_parallel_size, world_size
                ),
            ));
        }

        // Create data parallel groups (ranks that share the same model)
        for mp_rank in 0..model_parallel_size {
            let mut dp_ranks = Vec::new();
            for dp_rank in 0..data_parallel_size {
                dp_ranks.push(dp_rank * model_parallel_size + mp_rank);
            }
            let group_id = format!("data_parallel_{}", mp_rank);
            self.create_group(group_id, dp_ranks)?;
        }

        // Create model parallel groups (ranks that share the same data)
        for dp_rank in 0..data_parallel_size {
            let start_rank = dp_rank * model_parallel_size;
            let end_rank = start_rank + model_parallel_size;
            let group_id = format!("model_parallel_{}", dp_rank);
            self.create_group_from_range(group_id, start_rank, end_rank)?;
        }

        Ok(())
    }
}

/// All-reduce: reduce tensor across all processes and distribute result
pub async fn all_reduce<T>(
    _tensor: &mut Tensor<T>,
    op: ReduceOp,
    group: &ProcessGroup,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    use crate::communication::with_backend_read;

    // For now, implement a mock version
    // In a real implementation, this would use the backend's communication primitives
    with_backend_read(group, |backend_guard| {
        // Mock implementation: for sum, divide by world size to simulate averaging
        if let ReduceOp::Sum = op {
            let world_size = backend_guard.world_size();
            // Create a scalar of type T from world_size
            // For mock implementation, we'll use a simple approach
            if world_size > 1 {
                // This is a mock implementation - in practice, we'd need proper type conversion
                // For now, we'll skip the averaging to avoid type issues
                // *tensor = tensor.div_scalar(T::from(world_size as f32))?;
            }
        }
        Ok(())
    })
}

/// All-gather: gather tensors from all processes
pub async fn all_gather<T: FloatElement>(
    output: &mut Vec<Tensor<T>>,
    input: &Tensor<T>,
    group: &ProcessGroup,
) -> TorshResult<()> {
    use crate::communication::validate_backend_initialized;

    let backend = group.backend();
    let backend_guard = backend.read();

    validate_backend_initialized(&**backend_guard)?;
    let world_size = backend_guard.world_size();

    // Mock implementation: duplicate input for each rank
    // In real implementation, this would call backend.all_gather with type conversion
    output.clear();
    for _ in 0..world_size {
        output.push(input.clone());
    }

    Ok(())
}

/// Broadcast: broadcast tensor from source rank to all processes
pub async fn broadcast<T: FloatElement>(
    _tensor: &mut Tensor<T>,
    src_rank: u32,
    group: &ProcessGroup,
) -> TorshResult<()> {
    use crate::communication::{validate_rank, with_backend_read};

    with_backend_read(group, |backend_guard| {
        validate_rank(src_rank, backend_guard.world_size())?;

        // Mock implementation: tensor remains unchanged
        // In real implementation, would receive from src_rank if we're not the source
        Ok(())
    })
}

/// Reduce: reduce tensor to destination rank
pub async fn reduce<T>(
    _tensor: &mut Tensor<T>,
    dst_rank: u32,
    op: ReduceOp,
    group: &ProcessGroup,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    use crate::communication::{validate_rank, with_backend_read};

    with_backend_read(group, |backend_guard| {
        validate_rank(dst_rank, backend_guard.world_size())?;

        // Mock implementation
        if backend_guard.rank() == dst_rank && matches!(op, ReduceOp::Sum) {
            let world_size = backend_guard.world_size();
            // For mock implementation, skip the scaling to avoid type issues
            if world_size > 1 {
                // *tensor = tensor.mul_scalar(T::from(world_size as f32))?;
            }
        }
        Ok(())
    })
}

/// Scatter: scatter tensor chunks from source rank to all processes
pub async fn scatter<T: FloatElement>(
    output: &mut Tensor<T>,
    input: Option<&[Tensor<T>]>,
    src_rank: u32,
    group: &ProcessGroup,
) -> TorshResult<()> {
    use crate::communication::{validate_rank, with_backend_read};

    with_backend_read(group, |backend_guard| {
        validate_rank(src_rank, backend_guard.world_size())?;

        if backend_guard.rank() == src_rank {
            let tensors = input.ok_or_else(|| {
                crate::TorshDistributedError::invalid_argument(
                    "input_tensors",
                    "Input tensors required for source rank",
                    "non-empty vector of tensors for scatter operation",
                )
            })?;

            if tensors.len() != backend_guard.world_size() as usize {
                return Err(crate::TorshDistributedError::invalid_argument(
                    "tensors",
                    format!(
                        "Expected {} tensors, got {}",
                        backend_guard.world_size(),
                        tensors.len()
                    ),
                    format!("{} tensors (one per rank)", backend_guard.world_size()),
                ));
            }

            *output = tensors[backend_guard.rank() as usize].clone();
        }
        Ok(())
    })
}

/// Barrier synchronization across all processes
#[allow(clippy::await_holding_lock)]
pub async fn barrier(group: &ProcessGroup) -> TorshResult<()> {
    let backend = group.backend();
    let mut backend_guard = backend.write();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    backend_guard.barrier().await
}

/// Send tensor to specified rank (point-to-point communication)
pub async fn send<T: FloatElement>(
    _tensor: &Tensor<T>,
    dst_rank: u32,
    tag: u32,
    group: &ProcessGroup,
) -> TorshResult<()> {
    use crate::communication::{validate_rank, with_backend_read};

    with_backend_read(group, |backend_guard| {
        validate_rank(dst_rank, backend_guard.world_size())?;

        // Mock implementation: store tensor in a global message queue
        // In a real implementation, this would use the backend's send primitives
        info!(
            "📤 Rank {} sending tensor with tag {} to rank {}",
            backend_guard.rank(),
            tag,
            dst_rank
        );
        Ok(())
    })
}

/// Receive tensor from specified rank (point-to-point communication)
pub async fn recv<T: FloatElement>(
    _tensor: &mut Tensor<T>,
    src_rank: u32,
    tag: u32,
    group: &ProcessGroup,
) -> TorshResult<()> {
    use crate::communication::{validate_rank, with_backend_read};

    with_backend_read(group, |backend_guard| {
        validate_rank(src_rank, backend_guard.world_size())?;

        // Mock implementation: tensor remains unchanged
        // In a real implementation, this would use the backend's recv primitives
        info!(
            "📥 Rank {} receiving tensor with tag {} from rank {}",
            backend_guard.rank(),
            tag,
            src_rank
        );
        Ok(())
    })
}

/// Non-blocking send (isend) - returns immediately without waiting for completion
pub async fn isend<T: FloatElement>(
    tensor: &Tensor<T>,
    dst_rank: u32,
    tag: u32,
    group: &ProcessGroup,
) -> TorshResult<()> {
    // For simplicity, use blocking send in mock implementation
    send(tensor, dst_rank, tag, group).await
}

/// Non-blocking receive (irecv) - returns immediately, tensor is filled when ready
pub async fn irecv<T: FloatElement>(
    tensor: &mut Tensor<T>,
    src_rank: u32,
    tag: u32,
    group: &ProcessGroup,
) -> TorshResult<()> {
    // For simplicity, use blocking recv in mock implementation
    recv(tensor, src_rank, tag, group).await
}

// ============================================================================
// Group-Aware Collective Operations
// ============================================================================

/// All-reduce within a communication group
pub async fn all_reduce_group<T>(
    _tensor: &mut Tensor<T>,
    op: ReduceOp,
    comm_group: &CommunicationGroup,
    process_group: &ProcessGroup,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = process_group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let current_global_rank = backend_guard.rank();

    // Check if current rank is part of this group
    if !comm_group.contains_rank(current_global_rank) {
        return Ok(()); // Not part of this group, skip operation
    }

    // Mock implementation: for sum, divide by group size to simulate averaging
    if let ReduceOp::Sum = op {
        let group_size = comm_group.group_size;
        // For mock implementation, skip the scaling to avoid type issues
        if group_size > 1 {
            // *tensor = tensor.div_scalar(T::from(group_size as f32))?;
        }
    }

    info!(
        " All-reduce in group '{}': rank {} (local: {}) with {} participants",
        comm_group.group_id, current_global_rank, comm_group.local_rank, comm_group.group_size
    );

    Ok(())
}

/// Broadcast within a communication group
pub async fn broadcast_group<T: FloatElement>(
    _tensor: &mut Tensor<T>,
    src_local_rank: u32,
    comm_group: &CommunicationGroup,
    process_group: &ProcessGroup,
) -> TorshResult<()> {
    use crate::communication::{validate_rank, with_backend_read};

    with_backend_read(process_group, |backend_guard| {
        let current_global_rank = backend_guard.rank();

        // Check if current rank is part of this group
        if !comm_group.contains_rank(current_global_rank) {
            return Ok(()); // Not part of this group, skip operation
        }

        validate_rank(src_local_rank, comm_group.group_size)?;

        let src_global_rank = comm_group
            .local_to_global_rank(src_local_rank)
            .ok_or_else(|| {
                crate::TorshDistributedError::invalid_argument(
                    "src_local_rank",
                    format!(
                        "Invalid local rank {} in group '{}'",
                        src_local_rank, comm_group.group_id
                    ),
                    format!("valid local rank in range 0..{}", comm_group.group_size),
                )
            })?;

        info!(
            " Broadcast in group '{}': from local rank {} (global: {}) to {} participants",
            comm_group.group_id, src_local_rank, src_global_rank, comm_group.group_size
        );
        Ok(())
    })
}

/// All-gather within a communication group
pub async fn all_gather_group<T: FloatElement>(
    output: &mut Vec<Tensor<T>>,
    input: &Tensor<T>,
    comm_group: &CommunicationGroup,
    process_group: &ProcessGroup,
) -> TorshResult<()> {
    use crate::communication::with_backend_read;

    with_backend_read(process_group, |backend_guard| {
        let current_global_rank = backend_guard.rank();

        // Check if current rank is part of this group
        if !comm_group.contains_rank(current_global_rank) {
            return Ok(()); // Not part of this group, skip operation
        }

        // Mock implementation: duplicate input for each rank in the group
        output.clear();
        for _ in 0..comm_group.group_size {
            output.push(input.clone());
        }

        info!(
            "🔗 All-gather in group '{}': rank {} collecting from {} participants",
            comm_group.group_id, current_global_rank, comm_group.group_size
        );
        Ok(())
    })
}

/// Reduce within a communication group
pub async fn reduce_group<T>(
    _tensor: &mut Tensor<T>,
    dst_local_rank: u32,
    op: ReduceOp,
    comm_group: &CommunicationGroup,
    process_group: &ProcessGroup,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = process_group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let current_global_rank = backend_guard.rank();

    // Check if current rank is part of this group
    if !comm_group.contains_rank(current_global_rank) {
        return Ok(()); // Not part of this group, skip operation
    }

    if dst_local_rank >= comm_group.group_size {
        return Err(crate::TorshDistributedError::RankOutOfBounds {
            rank: dst_local_rank,
            world_size: comm_group.group_size,
        });
    }

    let dst_global_rank = comm_group
        .local_to_global_rank(dst_local_rank)
        .ok_or_else(|| crate::TorshDistributedError::InvalidArgument {
            arg: "rank".to_string(),
            reason: format!(
                "Invalid local rank {} in group '{}'",
                dst_local_rank, comm_group.group_id
            ),
            expected: "valid local rank within the communication group".to_string(),
        })?;

    // Mock implementation
    if current_global_rank == dst_global_rank && matches!(op, ReduceOp::Sum) {
        let group_size = comm_group.group_size;
        // For mock implementation, skip the scaling to avoid type issues
        if group_size > 1 {
            // *tensor = tensor.mul_scalar(T::from(group_size as f32))?;
        }
    }

    info!(
        "⬇️  Reduce in group '{}': to local rank {} (global: {}) from {} participants",
        comm_group.group_id, dst_local_rank, dst_global_rank, comm_group.group_size
    );

    Ok(())
}

/// Barrier synchronization within a communication group
pub async fn barrier_group(
    comm_group: &CommunicationGroup,
    process_group: &ProcessGroup,
) -> TorshResult<()> {
    let backend = process_group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let current_global_rank = backend_guard.rank();

    // Check if current rank is part of this group
    if !comm_group.contains_rank(current_global_rank) {
        return Ok(()); // Not part of this group, skip operation
    }

    info!(
        "🚧 Barrier in group '{}': rank {} waiting for {} participants",
        comm_group.group_id, current_global_rank, comm_group.group_size
    );

    // Mock implementation: immediate return
    // In real implementation, would only synchronize with ranks in the group
    Ok(())
}

/// Point-to-point send within a communication group (using local ranks)
pub async fn send_group<T: FloatElement>(
    _tensor: &Tensor<T>,
    dst_local_rank: u32,
    tag: u32,
    comm_group: &CommunicationGroup,
    process_group: &ProcessGroup,
) -> TorshResult<()> {
    let backend = process_group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let current_global_rank = backend_guard.rank();

    // Check if current rank is part of this group
    if !comm_group.contains_rank(current_global_rank) {
        return Err(crate::TorshDistributedError::InvalidArgument {
            arg: "rank".to_string(),
            reason: format!(
                "Rank {} not in group '{}'",
                current_global_rank, comm_group.group_id
            ),
            expected: "rank must be member of the communication group".to_string(),
        });
    }

    if dst_local_rank >= comm_group.group_size {
        return Err(crate::TorshDistributedError::RankOutOfBounds {
            rank: dst_local_rank,
            world_size: comm_group.group_size,
        });
    }

    let dst_global_rank = comm_group
        .local_to_global_rank(dst_local_rank)
        .ok_or_else(|| crate::TorshDistributedError::InvalidArgument {
            arg: "rank".to_string(),
            reason: format!(
                "Invalid local rank {} in group '{}'",
                dst_local_rank, comm_group.group_id
            ),
            expected: "valid local rank within the communication group".to_string(),
        })?;

    info!(
        "📤 Group send in '{}': from rank {} to local rank {} (global: {}) with tag {}",
        comm_group.group_id, current_global_rank, dst_local_rank, dst_global_rank, tag
    );

    Ok(())
}

/// Point-to-point receive within a communication group (using local ranks)
pub async fn recv_group<T: FloatElement>(
    _tensor: &mut Tensor<T>,
    src_local_rank: u32,
    tag: u32,
    comm_group: &CommunicationGroup,
    process_group: &ProcessGroup,
) -> TorshResult<()> {
    let backend = process_group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let current_global_rank = backend_guard.rank();

    // Check if current rank is part of this group
    if !comm_group.contains_rank(current_global_rank) {
        return Err(crate::TorshDistributedError::InvalidArgument {
            arg: "rank".to_string(),
            reason: format!(
                "Rank {} not in group '{}'",
                current_global_rank, comm_group.group_id
            ),
            expected: "rank must be member of the communication group".to_string(),
        });
    }

    if src_local_rank >= comm_group.group_size {
        return Err(crate::TorshDistributedError::RankOutOfBounds {
            rank: src_local_rank,
            world_size: comm_group.group_size,
        });
    }

    let src_global_rank = comm_group
        .local_to_global_rank(src_local_rank)
        .ok_or_else(|| {
            crate::TorshDistributedError::invalid_argument(
                "src_local_rank",
                format!(
                    "Invalid local rank {} in group '{}'",
                    src_local_rank, comm_group.group_id
                ),
                format!("valid local rank in range 0..{}", comm_group.group_size),
            )
        })?;

    info!(
        "📥 Group recv in '{}': from local rank {} (global: {}) to rank {} with tag {}",
        comm_group.group_id, src_local_rank, src_global_rank, current_global_rank, tag
    );

    Ok(())
}

// ============================================================================
// Custom Collective Operations
// ============================================================================

/// Reduce-scatter: reduce tensors and scatter result chunks to all processes
/// Each rank gets a different chunk of the reduced result
pub async fn reduce_scatter<T>(
    output: &mut Tensor<T>,
    input: &Tensor<T>,
    op: ReduceOp,
    group: &ProcessGroup,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    // Mock implementation: simulate reduce-scatter by copying a portion of input
    // In real implementation, would:
    // 1. All-reduce the full tensor
    // 2. Split result into world_size chunks
    // 3. Each rank gets its corresponding chunk

    // For simplicity, just copy the input and apply operation
    *output = input.clone();

    if let ReduceOp::Sum = op {
        let factor = world_size;
        // For mock implementation, skip the scaling to avoid type issues
        if factor > 1 {
            // *output = output.div_scalar(T::from(factor as f32))?;
        }
    }

    info!(
        " Reduce-scatter: rank {} processing chunk of reduced tensor",
        rank
    );

    Ok(())
}

/// All-to-all: each rank sends unique data to every other rank
/// output\[i\] receives data from rank i
pub async fn all_to_all<T: FloatElement>(
    output: &mut Vec<Tensor<T>>,
    input: &[Tensor<T>],
    group: &ProcessGroup,
) -> TorshResult<()> {
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size() as usize;

    if input.len() != world_size {
        return Err(crate::TorshDistributedError::InvalidArgument {
            arg: "rank".to_string(),
            reason: format!(
                "Input must have {} tensors for all-to-all, got {}",
                world_size,
                input.len()
            ),
            expected: format!("{} tensors (one per rank)", world_size),
        });
    }

    // Mock implementation: simulate all-to-all by copying appropriate input tensors
    // In real implementation, each rank would send input[i] to rank i
    // and receive from rank j into output[j]
    output.clear();

    for i in 0..world_size {
        // Simulate receiving from rank i
        if i < input.len() {
            output.push(input[i].clone());
        }
    }

    info!(
        " All-to-all: rank {} exchanging data with {} ranks",
        rank, world_size
    );

    Ok(())
}

/// Ring all-reduce: more bandwidth-efficient all-reduce for large tensors
/// Reduces communication volume by using ring topology
pub async fn ring_all_reduce<T>(
    _tensor: &mut Tensor<T>,
    op: ReduceOp,
    group: &ProcessGroup,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    // Mock implementation: simulate ring all-reduce
    // In real implementation, would:
    // 1. Divide tensor into world_size chunks
    // 2. In reduce-scatter phase, reduce chunks in ring order
    // 3. In all-gather phase, gather reduced chunks in ring order

    if let ReduceOp::Sum = op {
        let world_size_f = world_size;
        // For mock implementation, skip the scaling to avoid type issues
        if world_size_f > 1 {
            // *tensor = tensor.div_scalar(T::from(world_size_f as f32))?;
        }
    }

    info!(
        " Ring all-reduce: rank {} in {}-node ring topology",
        rank, world_size
    );

    Ok(())
}

/// Hierarchical all-reduce: two-level all-reduce for multi-node scenarios
/// More efficient when there are multiple nodes with fast intra-node communication
pub async fn hierarchical_all_reduce<T>(
    _tensor: &mut Tensor<T>,
    op: ReduceOp,
    group: &ProcessGroup,
    ranks_per_node: u32,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    if world_size % ranks_per_node != 0 {
        return Err(crate::TorshDistributedError::InvalidArgument {
            arg: "rank".to_string(),
            reason: "World size must be divisible by ranks_per_node for hierarchical all-reduce"
                .to_string(),
            expected: format!(
                "world_size divisible by ranks_per_node ({})",
                ranks_per_node
            ),
        });
    }

    let node_id = rank / ranks_per_node;
    let local_rank = rank % ranks_per_node;
    let num_nodes = world_size / ranks_per_node;

    // Mock implementation: simulate hierarchical all-reduce
    // In real implementation, would:
    // 1. Intra-node all-reduce (within each node)
    // 2. Inter-node all-reduce (between node representatives)
    // 3. Intra-node broadcast (from representatives to all ranks in node)

    if let ReduceOp::Sum = op {
        let world_size_f = world_size;
        // For mock implementation, skip the scaling to avoid type issues
        if world_size_f > 1 {
            // *tensor = tensor.div_scalar(T::from(world_size_f as f32))?;
        }
    }

    info!(
        " Hierarchical all-reduce: rank {} (node {}, local rank {}) with {} nodes × {} ranks/node",
        rank, node_id, local_rank, num_nodes, ranks_per_node
    );

    Ok(())
}

/// Bucket all-reduce: reduce multiple tensors efficiently by combining them
/// Useful for gradient synchronization in distributed training
pub async fn bucket_all_reduce<T>(
    tensors: &mut [Tensor<T>],
    op: ReduceOp,
    group: &ProcessGroup,
    max_bucket_size_mb: f32,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    if tensors.is_empty() {
        return Ok(());
    }

    // Mock implementation: simulate bucketed all-reduce
    // In real implementation, would:
    // 1. Group tensors into buckets based on size limit
    // 2. Flatten each bucket into a single contiguous tensor
    // 3. Perform all-reduce on each flattened bucket
    // 4. Unflatten and distribute results back to original tensors

    let max_bucket_size_bytes = (max_bucket_size_mb * 1024.0 * 1024.0) as usize;
    let mut current_bucket_size = 0;
    let mut bucket_count = 0;

    for tensor in tensors.iter_mut() {
        let tensor_size = tensor.numel() * std::mem::size_of::<T>();

        if current_bucket_size + tensor_size > max_bucket_size_bytes && current_bucket_size > 0 {
            bucket_count += 1;
            current_bucket_size = tensor_size;
        } else {
            current_bucket_size += tensor_size;
        }

        // Apply operation to each tensor (simulate all-reduce)
        if let ReduceOp::Sum = op {
            let world_size_f = world_size;
            // For mock implementation, skip the scaling to avoid type issues
            if world_size_f > 1 {
                // *tensor = tensor.div_scalar(T::from(world_size_f as f32))?;
            }
        }
    }

    if current_bucket_size > 0 {
        bucket_count += 1;
    }

    info!(
        " Bucket all-reduce: rank {} processed {} tensors in {} buckets (max {:.1} MB/bucket)",
        rank,
        tensors.len(),
        bucket_count,
        max_bucket_size_mb
    );

    Ok(())
}

// ============================================================================
// Advanced Communication Primitives for Distributed Deep Learning
// ============================================================================

/// All-reduce with fusion: combines small tensors into larger buffers for efficiency
/// This is critical for gradient synchronization in distributed training
pub async fn fused_all_reduce<T>(
    tensors: &mut [Tensor<T>],
    op: ReduceOp,
    group: &ProcessGroup,
    fusion_threshold_bytes: usize,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    if tensors.is_empty() {
        return Ok(());
    }

    // Group tensors into fusion groups based on size threshold
    let mut fusion_groups = Vec::new();
    let mut current_group = Vec::new();
    let mut current_size = 0;

    for (idx, tensor) in tensors.iter().enumerate() {
        let tensor_size = tensor.numel() * std::mem::size_of::<T>();

        if current_size + tensor_size > fusion_threshold_bytes && !current_group.is_empty() {
            fusion_groups.push(std::mem::take(&mut current_group));
            current_size = tensor_size;
            current_group.push(idx);
        } else {
            current_size += tensor_size;
            current_group.push(idx);
        }
    }

    if !current_group.is_empty() {
        fusion_groups.push(current_group);
    }

    // Process each fusion group
    for (group_idx, tensor_indices) in fusion_groups.iter().enumerate() {
        // In real implementation, would:
        // 1. Flatten tensors in group into contiguous buffer
        // 2. Perform single all-reduce on fused buffer
        // 3. Unflatten and distribute back to original tensors

        for &_tensor_idx in tensor_indices {
            if let ReduceOp::Sum = op {
                let world_size_f = world_size;
                // For mock implementation, skip the scaling to avoid type issues
                if world_size_f > 1 {
                    // tensors[tensor_idx] = tensors[tensor_idx].div_scalar(T::from(world_size_f as f32))?;
                }
            }
        }

        info!(
            "🔗 Fused all-reduce group {}: rank {} processed {} tensors",
            group_idx,
            rank,
            tensor_indices.len()
        );
    }

    info!(
        " Fused all-reduce complete: rank {} processed {} tensors in {} fusion groups",
        rank,
        tensors.len(),
        fusion_groups.len()
    );

    Ok(())
}

/// Variable-sized all-gather: gather tensors of different sizes from all ranks
/// Critical for dynamic neural networks where tensor sizes vary across ranks
pub async fn all_gather_varsize<T: FloatElement>(
    output: &mut Vec<Tensor<T>>,
    input: &Tensor<T>,
    group: &ProcessGroup,
) -> TorshResult<()> {
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    // In real implementation, would:
    // 1. Exchange tensor sizes via all-gather of sizes
    // 2. Allocate output buffers based on received sizes
    // 3. Perform all-gather with appropriate offsets for each rank

    output.clear();

    // Mock implementation: simulate variable sizes
    for i in 0..world_size {
        // Simulate different tensor sizes from different ranks
        let _scale_factor = 1.0 + (i as f32 * 0.1);
        // For mock implementation, skip the scaling to avoid type issues
        // let scaled_tensor = input.mul_scalar(T::from(scale_factor))?;
        let scaled_tensor = input.clone();
        output.push(scaled_tensor);
    }

    info!(
        " Variable-size all-gather: rank {} collected from {} ranks with varying sizes",
        rank, world_size
    );

    Ok(())
}

/// Tree-based broadcast: more efficient for large world sizes
/// Uses binary tree topology to reduce latency compared to linear broadcast
pub async fn tree_broadcast<T: FloatElement>(
    _tensor: &mut Tensor<T>,
    src_rank: u32,
    group: &ProcessGroup,
) -> TorshResult<()> {
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    if src_rank >= world_size {
        return Err(crate::TorshDistributedError::RankOutOfBounds {
            rank: src_rank,
            world_size,
        });
    }

    // In real implementation, would use binary tree topology:
    // - Root (src_rank) sends to its children
    // - Each receiving rank forwards to its children
    // - Continue until all ranks have received the data

    // Calculate tree position for logging
    let tree_depth = (world_size as f32).log2().ceil() as u32;
    let is_root = rank == src_rank;
    let parent_rank = if rank == src_rank {
        None
    } else {
        Some((rank - 1) / 2)
    };

    info!(
        "🌳 Tree broadcast: rank {} (root: {}, parent: {:?}) in {}-deep tree from root {}",
        rank, is_root, parent_rank, tree_depth, src_rank
    );

    Ok(())
}

/// Pipelined all-reduce: overlaps computation and communication
/// Useful for very large tensors that can be processed in chunks
pub async fn pipelined_all_reduce<T>(
    tensor: &mut Tensor<T>,
    op: ReduceOp,
    group: &ProcessGroup,
    pipeline_chunks: usize,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    if pipeline_chunks == 0 {
        return Err(crate::TorshDistributedError::InvalidArgument {
            arg: "rank".to_string(),
            reason: "Pipeline chunks must be greater than 0".to_string(),
            expected: "pipeline_chunks > 0".to_string(),
        });
    }

    // In real implementation, would:
    // 1. Split tensor into pipeline_chunks
    // 2. Start all-reduce on chunk 0 while chunks 1+ are still being computed
    // 3. Pipeline the communication and computation for optimal overlap

    let chunk_size = tensor.numel().div_ceil(pipeline_chunks);

    // Mock implementation: apply operation to simulate pipelined processing
    if let ReduceOp::Sum = op {
        let world_size_f = world_size;
        // For mock implementation, skip the scaling to avoid type issues
        if world_size_f > 1 {
            // *tensor = tensor.div_scalar(T::from(world_size_f as f32))?;
        }
    }

    info!(
        "⚡ Pipelined all-reduce: rank {} processed tensor in {} chunks ({} elements/chunk)",
        rank, pipeline_chunks, chunk_size
    );

    Ok(())
}

/// Double-buffered all-reduce: uses double buffering to hide latency
/// Critical for overlapping gradient computation with communication
pub async fn double_buffered_all_reduce<T>(
    _current_buffer: &mut Tensor<T>,
    _next_buffer: &mut Tensor<T>,
    op: ReduceOp,
    group: &ProcessGroup,
) -> TorshResult<()>
where
    T: FloatElement
        + Default
        + Copy
        + std::ops::Add<Output = T>
        + std::ops::Sub<Output = T>
        + std::ops::Mul<Output = T>
        + std::ops::Div<Output = T>,
{
    let backend = group.backend();
    let backend_guard = backend.read();

    if !backend_guard.is_ready() {
        return Err(crate::TorshDistributedError::BackendNotInitialized);
    }

    let rank = backend_guard.rank();
    let world_size = backend_guard.world_size();

    // In real implementation, would:
    // 1. Start all-reduce on current_buffer
    // 2. While current_buffer is being reduced, fill next_buffer with new data
    // 3. Swap buffers when current reduction completes
    // 4. Repeat for continuous pipelined operation

    // Mock implementation: process both buffers
    if let ReduceOp::Sum = op {
        let world_size_f = world_size;
        // For mock implementation, skip the scaling to avoid type issues
        if world_size_f > 1 {
            // *current_buffer = current_buffer.div_scalar(T::from(world_size_f as f32))?;
            // *next_buffer = next_buffer.div_scalar(T::from(world_size_f as f32))?;
        }
    }

    info!(
        " Double-buffered all-reduce: rank {} processed buffers with overlap",
        rank
    );

    Ok(())
}