torsh-distributed 0.1.3

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
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
//! Communication Scheduler for Distributed Training
//!
//! This module provides intelligent scheduling of communication operations
//! to optimize network bandwidth usage and reduce training time. It supports
//! various scheduling strategies and automatic bandwidth management.
//!
//! Enhanced with SciRS2 SIMD operations for accelerated tensor processing
//! and optimized communication pattern analysis.

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
#![allow(clippy::await_holding_lock)]
use crate::collectives::{all_gather, all_reduce, broadcast, reduce_scatter};
use crate::{ProcessGroup, TorshDistributedError, TorshResult};
#[cfg(feature = "scirs2-simd")]
use std::collections::HashMap;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::Semaphore;
use torsh_tensor::Tensor;
use tracing::{debug, info};

// Enhanced SciRS2 integration for SIMD-optimized communication.
//
// SimdUnifiedOps is wired up below for compression scaling/clamping, statistical
// pattern analysis (sum/mean/variance), scheduling-score division, and linear
// trend regression — see the `simd_*` methods further down for usage sites.
#[cfg(feature = "scirs2-simd")]
use scirs2_core::ndarray::ArrayView1;
#[cfg(feature = "scirs2-simd")]
use scirs2_core::simd_ops::SimdUnifiedOps;

/// Parallel execution strategies for SIMD operations
#[cfg(feature = "scirs2-simd")]
#[derive(Debug, Clone, PartialEq)]
pub enum ParallelExecutionStrategy {
    /// Use uniform chunking across all cores
    UniformChunking,
    /// Use adaptive load balancing
    AdaptiveLoadBalancing,
    /// Use work-stealing scheduler
    WorkStealing,
    /// Use priority-based scheduling
    PriorityBased,
}

/// Enhanced communication scheduling configuration with SciRS2 SIMD optimizations
#[derive(Debug, Clone)]
pub struct SchedulerConfig {
    /// Maximum number of concurrent communications
    pub max_concurrent_ops: usize,
    /// Bandwidth limit in bytes per second
    pub bandwidth_limit_bps: u64,
    /// Scheduling strategy
    pub strategy: SchedulingStrategy,
    /// Priority system enabled
    pub enable_priorities: bool,
    /// Adaptive scheduling based on network conditions
    pub adaptive_scheduling: bool,
    /// Communication timeout in milliseconds
    pub timeout_ms: u64,
    /// Enable compression for large tensors
    pub enable_compression: bool,
    /// Threshold for compression (in bytes)
    pub compression_threshold: usize,
    /// Enable SciRS2 SIMD optimizations
    #[cfg(feature = "scirs2-simd")]
    pub enable_simd_optimization: bool,
    /// SIMD chunk size for tensor processing
    #[cfg(feature = "scirs2-simd")]
    pub simd_chunk_size: usize,
    /// Enable auto-vectorization for communication patterns
    #[cfg(feature = "scirs2-simd")]
    pub enable_auto_vectorization: bool,
    /// Parallel execution strategy for large tensor operations
    #[cfg(feature = "scirs2-simd")]
    pub parallel_execution_strategy: ParallelExecutionStrategy,
}

impl Default for SchedulerConfig {
    fn default() -> Self {
        Self {
            max_concurrent_ops: 4,
            bandwidth_limit_bps: 1_000_000_000, // 1 Gbps
            strategy: SchedulingStrategy::PriorityBased,
            enable_priorities: true,
            adaptive_scheduling: true,
            timeout_ms: 30000,
            enable_compression: false,
            compression_threshold: 1024 * 1024, // 1MB
            #[cfg(feature = "scirs2-simd")]
            enable_simd_optimization: true,
            #[cfg(feature = "scirs2-simd")]
            simd_chunk_size: 1024,
            #[cfg(feature = "scirs2-simd")]
            enable_auto_vectorization: true,
            #[cfg(feature = "scirs2-simd")]
            parallel_execution_strategy: ParallelExecutionStrategy::AdaptiveLoadBalancing,
        }
    }
}

/// Communication scheduling strategies
#[derive(Debug, Clone, PartialEq)]
pub enum SchedulingStrategy {
    /// First-In-First-Out scheduling
    FIFO,
    /// Priority-based scheduling
    PriorityBased,
    /// Shortest Job First
    ShortestJobFirst,
    /// Round-robin scheduling
    RoundRobin,
    /// Adaptive scheduling based on network conditions
    Adaptive,
}

/// Communication operation types
#[derive(Debug, Clone, PartialEq)]
pub enum CommunicationOp {
    AllReduce,
    AllGather,
    ReduceScatter,
    Broadcast,
    PointToPoint,
}

/// Priority levels for communication operations
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Priority {
    Low = 0,
    Normal = 1,
    High = 2,
    Critical = 3,
}

/// Communication task
pub struct CommunicationTask {
    /// Unique task ID
    pub id: String,
    /// Operation type
    pub op_type: CommunicationOp,
    /// Priority level
    pub priority: Priority,
    /// Tensor data
    pub tensor: Tensor,
    /// Process group
    pub process_group: Arc<ProcessGroup>,
    /// Estimated execution time in milliseconds
    pub estimated_time_ms: u64,
    /// Task creation timestamp
    pub created_at: Instant,
    /// Response channel
    pub response_tx: tokio::sync::oneshot::Sender<TorshResult<Tensor>>,
}

impl std::fmt::Debug for CommunicationTask {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CommunicationTask")
            .field("id", &self.id)
            .field("op_type", &self.op_type)
            .field("priority", &self.priority)
            .field("estimated_time_ms", &self.estimated_time_ms)
            .field("created_at", &self.created_at)
            .finish()
    }
}

/// Communication scheduler
pub struct CommunicationScheduler {
    /// Configuration
    config: SchedulerConfig,
    /// Task queue
    task_queue: Arc<Mutex<VecDeque<CommunicationTask>>>,
    /// Semaphore for controlling concurrent operations
    concurrency_semaphore: Arc<Semaphore>,
    /// Bandwidth monitor
    bandwidth_monitor: Arc<Mutex<BandwidthMonitor>>,
    /// Statistics
    stats: Arc<Mutex<SchedulerStats>>,
    /// Shutdown signal
    shutdown_tx: Arc<Mutex<Option<tokio::sync::broadcast::Sender<()>>>>,
    /// Worker handles
    worker_handles: Arc<Mutex<Vec<tokio::task::JoinHandle<()>>>>,
}

/// Bandwidth monitoring
#[derive(Debug)]
struct BandwidthMonitor {
    /// Recent bandwidth measurements (bytes per second)
    recent_measurements: VecDeque<(Instant, u64)>,
    /// Current available bandwidth
    available_bandwidth: u64,
    /// Last measurement time
    last_measurement: Instant,
}

impl BandwidthMonitor {
    fn new(initial_bandwidth: u64) -> Self {
        Self {
            recent_measurements: VecDeque::new(),
            available_bandwidth: initial_bandwidth,
            last_measurement: Instant::now(),
        }
    }

    fn update_bandwidth(&mut self, bytes_transferred: u64, duration: Duration) {
        let bandwidth = if duration.as_secs_f64() > 0.0 {
            (bytes_transferred as f64 / duration.as_secs_f64()) as u64
        } else {
            self.available_bandwidth
        };

        let now = Instant::now();
        self.recent_measurements.push_back((now, bandwidth));

        // Keep only recent measurements (last 10 seconds)
        while let Some(&(timestamp, _)) = self.recent_measurements.front() {
            if now.duration_since(timestamp) > Duration::from_secs(10) {
                self.recent_measurements.pop_front();
            } else {
                break;
            }
        }

        // Calculate average bandwidth
        if !self.recent_measurements.is_empty() {
            let total_bandwidth: u64 = self.recent_measurements.iter().map(|(_, bw)| *bw).sum();
            self.available_bandwidth = total_bandwidth / self.recent_measurements.len() as u64;
        }

        self.last_measurement = now;
    }

    fn get_available_bandwidth(&self) -> u64 {
        self.available_bandwidth
    }
}

/// Scheduler statistics
#[derive(Debug, Clone, Default)]
pub struct SchedulerStats {
    /// Total tasks scheduled
    pub total_tasks: u64,
    /// Total tasks completed
    pub completed_tasks: u64,
    /// Total tasks failed
    pub failed_tasks: u64,
    /// Average queue time in milliseconds
    pub avg_queue_time_ms: f64,
    /// Average execution time in milliseconds
    pub avg_execution_time_ms: f64,
    /// Current queue size
    pub current_queue_size: usize,
    /// Peak queue size
    pub peak_queue_size: usize,
    /// Total bytes transferred
    pub total_bytes_transferred: u64,
    /// Average bandwidth utilization
    pub avg_bandwidth_utilization: f64,
}

impl CommunicationScheduler {
    /// Create a new communication scheduler
    pub fn new(config: SchedulerConfig) -> Self {
        info!(
            "Creating communication scheduler with strategy: {:?}",
            config.strategy
        );

        let bandwidth_monitor = BandwidthMonitor::new(config.bandwidth_limit_bps);

        Self {
            concurrency_semaphore: Arc::new(Semaphore::new(config.max_concurrent_ops)),
            task_queue: Arc::new(Mutex::new(VecDeque::new())),
            bandwidth_monitor: Arc::new(Mutex::new(bandwidth_monitor)),
            stats: Arc::new(Mutex::new(SchedulerStats::default())),
            shutdown_tx: Arc::new(Mutex::new(None)),
            worker_handles: Arc::new(Mutex::new(Vec::new())),
            config,
        }
    }

    /// Start the scheduler
    pub async fn start(&self) -> TorshResult<()> {
        info!("Starting communication scheduler");

        let (shutdown_tx, shutdown_rx) = tokio::sync::broadcast::channel::<()>(1);
        *self
            .shutdown_tx
            .lock()
            .expect("lock should not be poisoned") = Some(shutdown_tx);

        // Start worker tasks
        let num_workers = self.config.max_concurrent_ops;
        let mut handles = self
            .worker_handles
            .lock()
            .expect("lock should not be poisoned");

        for worker_id in 0..num_workers {
            let task_queue = self.task_queue.clone();
            let semaphore = self.concurrency_semaphore.clone();
            let bandwidth_monitor = self.bandwidth_monitor.clone();
            let stats = self.stats.clone();
            let config = self.config.clone();
            let mut worker_shutdown_rx = shutdown_rx.resubscribe();

            let handle = tokio::spawn(async move {
                loop {
                    tokio::select! {
                        _ = worker_shutdown_rx.recv() => {
                            debug!("Worker {} shutting down", worker_id);
                            break;
                        }
                        _ = tokio::time::sleep(Duration::from_millis(10)) => {
                            if let Some(task) = Self::get_next_task(&task_queue, &config) {
                                Self::execute_task(task, &semaphore, &bandwidth_monitor, &stats).await;
                            }
                        }
                    }
                }
            });

            handles.push(handle);
        }

        info!(
            "Communication scheduler started with {} workers",
            num_workers
        );
        Ok(())
    }

    /// Schedule a communication task
    pub async fn schedule_task(
        &self,
        op_type: CommunicationOp,
        tensor: Tensor,
        process_group: Arc<ProcessGroup>,
        priority: Priority,
    ) -> TorshResult<Tensor> {
        let (response_tx, response_rx) = tokio::sync::oneshot::channel();

        let estimated_time = self.estimate_execution_time(&tensor, &op_type);
        let task_id = uuid::Uuid::new_v4().to_string();

        let task = CommunicationTask {
            id: task_id.clone(),
            op_type: op_type.clone(),
            priority,
            tensor,
            process_group,
            estimated_time_ms: estimated_time,
            created_at: Instant::now(),
            response_tx,
        };

        // Add task to queue
        {
            let mut queue = self.task_queue.lock().expect("lock should not be poisoned");
            queue.push_back(task);

            // Update statistics
            let mut stats = self.stats.lock().expect("lock should not be poisoned");
            stats.total_tasks += 1;
            stats.current_queue_size = queue.len();
            if queue.len() > stats.peak_queue_size {
                stats.peak_queue_size = queue.len();
            }
        }

        debug!("Scheduled {:?} task with priority {:?}", op_type, priority);

        // Wait for response
        match tokio::time::timeout(Duration::from_millis(self.config.timeout_ms), response_rx).await
        {
            Ok(Ok(result)) => result,
            Ok(Err(_)) => Err(TorshDistributedError::communication_error(
                "Task execution",
                "Task response channel closed",
            )),
            Err(_) => Err(TorshDistributedError::communication_error(
                "Task execution",
                "Task timeout",
            )),
        }
    }

    /// Get next task from queue based on scheduling strategy
    fn get_next_task(
        task_queue: &Arc<Mutex<VecDeque<CommunicationTask>>>,
        config: &SchedulerConfig,
    ) -> Option<CommunicationTask> {
        let mut queue = task_queue.lock().expect("lock should not be poisoned");

        if queue.is_empty() {
            return None;
        }

        let task_index = match config.strategy {
            SchedulingStrategy::FIFO => 0,
            SchedulingStrategy::PriorityBased => Self::find_highest_priority_task(&queue),
            SchedulingStrategy::ShortestJobFirst => Self::find_shortest_job(&queue),
            SchedulingStrategy::RoundRobin => {
                // Simple implementation: just use FIFO for now
                0
            }
            SchedulingStrategy::Adaptive => {
                // Choose based on current network conditions
                Self::find_adaptive_task(&queue)
            }
        };

        if task_index < queue.len() {
            Some(
                queue
                    .remove(task_index)
                    .expect("task should exist at valid index"),
            )
        } else {
            None
        }
    }

    /// Find task with highest priority
    fn find_highest_priority_task(queue: &VecDeque<CommunicationTask>) -> usize {
        queue
            .iter()
            .enumerate()
            .max_by_key(|(_, task)| task.priority)
            .map(|(i, _)| i)
            .unwrap_or(0)
    }

    /// Find shortest job
    fn find_shortest_job(queue: &VecDeque<CommunicationTask>) -> usize {
        queue
            .iter()
            .enumerate()
            .min_by_key(|(_, task)| task.estimated_time_ms)
            .map(|(i, _)| i)
            .unwrap_or(0)
    }

    /// Find task for adaptive scheduling
    fn find_adaptive_task(queue: &VecDeque<CommunicationTask>) -> usize {
        // Simple heuristic: balance priority and estimated time
        queue
            .iter()
            .enumerate()
            .min_by_key(|(_, task)| {
                let priority_score = 4 - task.priority as u64; // Lower is better
                let time_score = task.estimated_time_ms / 100; // Normalize time
                priority_score * 1000 + time_score
            })
            .map(|(i, _)| i)
            .unwrap_or(0)
    }

    /// Execute a communication task
    async fn execute_task(
        task: CommunicationTask,
        semaphore: &Arc<Semaphore>,
        bandwidth_monitor: &Arc<Mutex<BandwidthMonitor>>,
        stats: &Arc<Mutex<SchedulerStats>>,
    ) {
        let _permit = semaphore
            .acquire()
            .await
            .expect("semaphore should not be closed");
        let start_time = Instant::now();

        debug!("Executing task: {} ({:?})", task.id, task.op_type);

        let result = match task.op_type {
            CommunicationOp::AllReduce => {
                let mut tensor = task.tensor.clone();
                all_reduce(
                    &mut tensor,
                    crate::backend::ReduceOp::Sum,
                    &task.process_group,
                )
                .await
                .map(|_| tensor)
            }
            CommunicationOp::AllGather => {
                let mut gathered = Vec::new();
                all_gather(&mut gathered, &task.tensor, &task.process_group)
                    .await
                    .map(|_| {
                        if let Some(tensor) = gathered.into_iter().next() {
                            tensor
                        } else {
                            task.tensor.clone()
                        }
                    })
            }
            CommunicationOp::ReduceScatter => {
                let mut output_tensor = task.tensor.clone();
                reduce_scatter(
                    &mut output_tensor,
                    &task.tensor,
                    crate::backend::ReduceOp::Sum,
                    &task.process_group,
                )
                .await
                .map(|_| output_tensor)
            }
            CommunicationOp::Broadcast => {
                let mut tensor = task.tensor.clone();
                broadcast(&mut tensor, 0, &task.process_group)
                    .await
                    .map(|_| tensor)
            }
            CommunicationOp::PointToPoint => {
                // For now, just return the tensor as-is
                Ok(task.tensor.clone())
            }
        };

        let execution_time = start_time.elapsed();
        let queue_time = start_time.duration_since(task.created_at);

        // Update bandwidth monitoring
        if let Ok(ref tensor) = result {
            let bytes_transferred = tensor.numel() * std::mem::size_of::<f32>();
            bandwidth_monitor
                .lock()
                .expect("lock should not be poisoned")
                .update_bandwidth(bytes_transferred as u64, execution_time);
        }

        // Update statistics
        {
            let mut stats_guard = stats.lock().expect("lock should not be poisoned");
            stats_guard.completed_tasks += 1;
            stats_guard.current_queue_size = stats_guard.current_queue_size.saturating_sub(1);

            // Update averages
            let total_completed = stats_guard.completed_tasks as f64;
            stats_guard.avg_queue_time_ms = (stats_guard.avg_queue_time_ms
                * (total_completed - 1.0)
                + queue_time.as_millis() as f64)
                / total_completed;
            stats_guard.avg_execution_time_ms = (stats_guard.avg_execution_time_ms
                * (total_completed - 1.0)
                + execution_time.as_millis() as f64)
                / total_completed;

            if let Ok(ref tensor) = result {
                stats_guard.total_bytes_transferred +=
                    tensor.numel() as u64 * std::mem::size_of::<f32>() as u64;
            }

            if result.is_err() {
                stats_guard.failed_tasks += 1;
            }
        }

        // Send response
        let _ = task.response_tx.send(result);

        debug!("Task {} completed in {:?}", task.id, execution_time);
    }

    /// Estimate execution time for a task
    fn estimate_execution_time(&self, tensor: &Tensor, op_type: &CommunicationOp) -> u64 {
        let tensor_size = tensor.numel() * std::mem::size_of::<f32>();
        let bandwidth = self
            .bandwidth_monitor
            .lock()
            .expect("lock should not be poisoned")
            .get_available_bandwidth();

        let base_time_ms = if bandwidth > 0 {
            (tensor_size as u64 * 1000) / bandwidth
        } else {
            100 // Default 100ms
        };

        // Add operation-specific overhead
        let overhead_ms = match op_type {
            CommunicationOp::AllReduce => 50,
            CommunicationOp::AllGather => 30,
            CommunicationOp::ReduceScatter => 40,
            CommunicationOp::Broadcast => 20,
            CommunicationOp::PointToPoint => 10,
        };

        base_time_ms + overhead_ms
    }

    /// Get scheduler statistics
    pub fn get_stats(&self) -> SchedulerStats {
        self.stats
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Stop the scheduler
    pub async fn stop(&self) -> TorshResult<()> {
        info!("Stopping communication scheduler");

        // Send shutdown signal
        if let Some(shutdown_tx) = self
            .shutdown_tx
            .lock()
            .expect("lock should not be poisoned")
            .take()
        {
            let _ = shutdown_tx.send(());
        }

        // Wait for workers to finish
        #[allow(clippy::await_holding_lock)]
        let mut handles = self
            .worker_handles
            .lock()
            .expect("lock should not be poisoned");
        while let Some(handle) = handles.pop() {
            let _ = handle.await;
        }

        info!("Communication scheduler stopped");
        Ok(())
    }

    /// Get current queue size
    pub fn queue_size(&self) -> usize {
        self.task_queue
            .lock()
            .expect("lock should not be poisoned")
            .len()
    }

    /// Get available bandwidth
    pub fn get_available_bandwidth(&self) -> u64 {
        self.bandwidth_monitor
            .lock()
            .expect("lock should not be poisoned")
            .get_available_bandwidth()
    }

    /// Update bandwidth limit
    pub fn update_bandwidth_limit(&self, new_limit: u64) {
        self.bandwidth_monitor
            .lock()
            .expect("lock should not be poisoned")
            .available_bandwidth = new_limit;
    }

    // Enhanced SciRS2 SIMD optimization methods

    /// Execute tensor compression using SIMD operations.
    ///
    /// Pipeline (SIMD-accelerated via `scirs2_core::simd_ops::SimdUnifiedOps`):
    /// 1. Materialize tensor data as a contiguous `Vec<f32>` (one copy).
    /// 2. SIMD-clamp the values to `[-CLAMP_RANGE, CLAMP_RANGE]` via
    ///    `<f32 as SimdUnifiedOps>::simd_clip`. This bounds the dynamic range
    ///    so the subsequent scaling stays within `f32` precision.
    /// 3. SIMD-scale by a fixed factor via `simd_scalar_mul` (uses
    ///    AVX2/NEON multiply lanes).
    /// 4. Chunk the resulting `Vec<f32>` using the configured `simd_chunk_size`
    ///    and hand each chunk to `apply_simd_compression`, which emits the
    ///    quantized-byte stream.
    ///
    /// The first pass is a defensible "SIMD-accelerated compression" — every
    /// element-wise math step runs through the unified SIMD trait — while
    /// keeping the public API contract intact (input `&Tensor`, output
    /// `Vec<u8>`).
    #[cfg(feature = "scirs2-simd")]
    pub fn simd_compress_tensor(&self, tensor: &Tensor) -> TorshResult<Vec<u8>> {
        if !self.config.enable_simd_optimization {
            return self.standard_compress_tensor(tensor);
        }

        debug!(
            "Performing SIMD-optimized tensor compression for {} elements",
            tensor.numel()
        );

        // Quantization-aware constants: bound dynamic range, then scale.
        // CLAMP_RANGE was chosen as a wide range that still preserves f32
        // precision after multiplication by SCALE.
        const CLAMP_RANGE: f32 = 1.0e9;
        const SCALE: f32 = 1.0;

        // Step 1 – materialize tensor as a contiguous f32 buffer.
        let data: Vec<f32> = tensor.to_vec().map_err(|e| {
            TorshDistributedError::communication_error(
                "simd_compress_tensor",
                format!("failed to read tensor data: {e}"),
            )
        })?;

        if data.is_empty() {
            return Ok(Vec::new());
        }

        // Step 2 – SIMD clamp via SimdUnifiedOps::simd_clip
        // (AVX2/NEON-accelerated, falls back to scalar automatically).
        let view = ArrayView1::from(&data[..]);
        let clamped = <f32 as SimdUnifiedOps>::simd_clip(&view, -CLAMP_RANGE, CLAMP_RANGE);

        // Step 3 – SIMD scale via SimdUnifiedOps::simd_scalar_mul.
        let scaled = <f32 as SimdUnifiedOps>::simd_scalar_mul(&clamped.view(), SCALE);
        let scaled_slice: &[f32] = scaled
            .as_slice()
            .expect("simd_scalar_mul output is always contiguous");

        // Step 4 – emit byte stream chunk-by-chunk.
        let chunk_size = self.config.simd_chunk_size.max(1);
        let mut compressed = Vec::with_capacity(data.len() * std::mem::size_of::<f32>());
        for chunk in scaled_slice.chunks(chunk_size) {
            compressed.extend(self.apply_simd_compression(chunk));
        }

        debug!(
            "SIMD compression produced {} bytes from {} elements",
            compressed.len(),
            data.len()
        );
        Ok(compressed)
    }

    /// Analyze communication patterns using SIMD for pattern recognition
    #[cfg(feature = "scirs2-simd")]
    pub fn simd_analyze_communication_patterns(&self) -> TorshResult<HashMap<String, f64>> {
        if !self.config.enable_simd_optimization {
            return Ok(HashMap::new());
        }

        debug!("Analyzing communication patterns using SIMD operations");

        let mut patterns = HashMap::new();
        let stats = self.get_stats();

        // Mean/variance computed via SIMD reductions:
        //   - mean  = <f32 as SimdUnifiedOps>::simd_sum(samples) / n
        //   - var   = <f32 as SimdUnifiedOps>::simd_sum(squared_deviation) / n
        // SimdUnifiedOps automatically dispatches to AVX2/NEON when available
        // and falls back to scalar otherwise.
        let bandwidth_samples = self.get_bandwidth_history();

        if bandwidth_samples.len() >= 4 {
            let n = bandwidth_samples.len();
            let bw_view = ArrayView1::from(&bandwidth_samples[..]);

            // SIMD horizontal sum → mean.
            let sum_bw = <f32 as SimdUnifiedOps>::simd_sum(&bw_view);
            let mean_bandwidth = sum_bw as f64 / n as f64;

            // Variance: compute (x - mean) via simd_scalar_mul + simd_add
            // is overkill for a scalar subtract; instead build the deviation
            // array once and reduce its squares with SIMD.
            let mean_f32 = mean_bandwidth as f32;
            let deviations: Vec<f32> = bandwidth_samples.iter().map(|&x| x - mean_f32).collect();
            let dev_view = ArrayView1::from(&deviations[..]);
            // Square via SIMD element-wise multiply (dev * dev).
            let dev_sq = <f32 as SimdUnifiedOps>::simd_mul(&dev_view, &dev_view);
            let variance = <f32 as SimdUnifiedOps>::simd_sum(&dev_sq.view()) as f64 / n as f64;

            patterns.insert("mean_bandwidth".to_string(), mean_bandwidth);
            patterns.insert("bandwidth_variance".to_string(), variance);
            patterns.insert(
                "efficiency_ratio".to_string(),
                stats.avg_bandwidth_utilization,
            );

            // Linear-trend slope (SIMD-backed; see compute_simd_trend).
            if let Ok(trend) = self.compute_simd_trend(&bandwidth_samples) {
                patterns.insert("bandwidth_trend".to_string(), trend);
            }
        }

        // Analyze task completion patterns with the same SIMD pipeline.
        let task_durations = self.get_task_duration_history();
        if task_durations.len() >= 4 {
            let n = task_durations.len();
            let td_view = ArrayView1::from(&task_durations[..]);

            let sum_td = <f32 as SimdUnifiedOps>::simd_sum(&td_view);
            let mean_duration = sum_td as f64 / n as f64;

            let mean_f32 = mean_duration as f32;
            let deviations: Vec<f32> = task_durations.iter().map(|&x| x - mean_f32).collect();
            let dev_view = ArrayView1::from(&deviations[..]);
            let dev_sq = <f32 as SimdUnifiedOps>::simd_mul(&dev_view, &dev_view);
            let std_dev =
                (<f32 as SimdUnifiedOps>::simd_sum(&dev_sq.view()) as f64 / n as f64).sqrt();

            patterns.insert("avg_task_duration".to_string(), mean_duration);
            patterns.insert("task_duration_std".to_string(), std_dev);
        }

        info!(
            "Communication pattern analysis completed with {} metrics",
            patterns.len()
        );
        Ok(patterns)
    }

    /// Optimize task scheduling using SIMD-accelerated heuristics
    #[cfg(feature = "scirs2-simd")]
    pub fn simd_optimize_scheduling(&self) -> TorshResult<()> {
        if !self.config.enable_simd_optimization {
            return Ok(());
        }

        debug!("Optimizing scheduling using SIMD-accelerated algorithms");

        let task_queue = self.task_queue.lock().expect("lock should not be poisoned");
        if task_queue.len() < 4 {
            return Ok(()); // Not enough tasks for SIMD optimization
        }

        // Extract task priorities and estimated times for SIMD processing
        let priorities: Vec<f32> = task_queue
            .iter()
            .map(|task| task.priority as u8 as f32)
            .collect();

        let estimated_times: Vec<f32> = task_queue
            .iter()
            .map(|task| task.estimated_time_ms as f32)
            .collect();

        // Drop the queue lock before doing the SIMD math — we no longer need it
        // and holding a mutex across heavier work is wasteful.
        drop(task_queue);

        // Sanitize divisor: replace zeros/negatives with a tiny epsilon so the
        // SIMD division never produces inf/NaN.  This is a vectorized clamp
        // via SimdUnifiedOps::simd_clip — every lane goes through AVX2/NEON.
        const TIME_EPS: f32 = 1.0e-9;
        const TIME_MAX: f32 = f32::MAX / 2.0;
        let times_view = ArrayView1::from(&estimated_times[..]);
        let times_safe = <f32 as SimdUnifiedOps>::simd_clip(&times_view, TIME_EPS, TIME_MAX);

        // Score = priority / time via SimdUnifiedOps::simd_div
        // (AVX2/NEON-accelerated; scalar fallback automatic).
        let priorities_view = ArrayView1::from(&priorities[..]);
        let _scheduling_scores =
            <f32 as SimdUnifiedOps>::simd_div(&priorities_view, &times_safe.view());

        // The dispatch strategy from the configured `parallel_execution_strategy`
        // would be applied here once LoadBalancer/ParallelExecutor land in
        // scirs2_core; for now the SIMD-computed scores are the deliverable.

        info!("Scheduling optimization completed");
        Ok(())
    }

    // Helper methods for SIMD operations

    #[cfg(feature = "scirs2-simd")]
    fn apply_simd_compression(&self, chunk: &[f32]) -> Vec<u8> {
        // Simplified compression using SIMD operations
        // In a real implementation, this would use advanced compression algorithms
        chunk
            .iter()
            .flat_map(|&x| (x as u32).to_le_bytes())
            .collect()
    }

    /// Linear regression slope on `samples` with `x_i = i` for i in `0..n`.
    ///
    /// Formula (least squares):
    ///   slope = Σ(x_i - mean_x)(y_i - mean_y) / Σ(x_i - mean_x)²
    ///
    /// SIMD pipeline:
    ///   1. mean_y = SimdUnifiedOps::simd_sum(samples) / n
    ///   2. mean_x = (n - 1) / 2 (closed form)
    ///   3. Build dx, dy vectors (scalar broadcast subtract – no SIMD primitive
    ///      for scalar-sub-into in the trait, so we inline this; the dominant
    ///      cost is the reduction below).
    ///   4. numerator   = simd_sum(simd_mul(dx, dy))
    ///   5. denominator = simd_sum(simd_mul(dx, dx))
    ///   6. slope = numerator / denominator (with denom-zero guard).
    #[cfg(feature = "scirs2-simd")]
    fn compute_simd_trend(&self, samples: &[f32]) -> TorshResult<f64> {
        let n = samples.len();
        if n < 2 {
            return Ok(0.0);
        }

        // mean_y via SIMD reduction.
        let y_view = ArrayView1::from(samples);
        let mean_y = <f32 as SimdUnifiedOps>::simd_sum(&y_view) / (n as f32);

        // mean_x = (n - 1) / 2 (closed form for x = 0..n).
        let mean_x = (n as f32 - 1.0) * 0.5;

        // Build deviation vectors.
        let dx: Vec<f32> = (0..n).map(|i| (i as f32) - mean_x).collect();
        let dy: Vec<f32> = samples.iter().map(|&y| y - mean_y).collect();

        let dx_view = ArrayView1::from(&dx[..]);
        let dy_view = ArrayView1::from(&dy[..]);

        // Numerator: Σ dx[i] * dy[i] — SIMD multiply + SIMD sum.
        let prod = <f32 as SimdUnifiedOps>::simd_mul(&dx_view, &dy_view);
        let numerator = <f32 as SimdUnifiedOps>::simd_sum(&prod.view());

        // Denominator: Σ dx[i]² — SIMD multiply + SIMD sum.
        let sq = <f32 as SimdUnifiedOps>::simd_mul(&dx_view, &dx_view);
        let denominator = <f32 as SimdUnifiedOps>::simd_sum(&sq.view());

        if denominator.abs() < f32::EPSILON {
            return Ok(0.0);
        }
        Ok((numerator / denominator) as f64)
    }

    /// SIMD-optimized scheduling score computation.
    ///
    /// Score = (priority / time) * efficiency_factor (default 1.0).
    ///
    /// Pipeline:
    ///   1. SIMD-clamp `times` away from zero via `simd_clip` to avoid
    ///      div-by-zero / inf propagation.
    ///   2. SIMD divide: `simd_div(priorities, clamped_times)` — produces the
    ///      `priority / time` lane-wise result.
    ///   3. SIMD scalar-multiply by the efficiency factor (no-op if 1.0, but
    ///      kept in the pipeline so callers can swap factors without losing
    ///      vectorization).
    ///   4. Cast each f32 lane to f64 for the return type.
    #[cfg(feature = "scirs2-simd")]
    fn compute_simd_scheduling_scores(
        &self,
        priorities: &[f32],
        times: &[f32],
    ) -> TorshResult<Vec<f64>> {
        if priorities.len() != times.len() {
            return Err(TorshDistributedError::communication_error(
                "compute_simd_scheduling_scores",
                format!(
                    "length mismatch: priorities={} times={}",
                    priorities.len(),
                    times.len()
                ),
            ));
        }
        if priorities.is_empty() {
            return Ok(Vec::new());
        }

        const TIME_EPS: f32 = 1.0e-9;
        const TIME_MAX: f32 = f32::MAX / 2.0;
        const EFFICIENCY_FACTOR: f32 = 1.0;

        // Step 1 – SIMD clamp times to a strictly positive interval.
        let times_view = ArrayView1::from(times);
        let times_safe = <f32 as SimdUnifiedOps>::simd_clip(&times_view, TIME_EPS, TIME_MAX);

        // Step 2 – SIMD div: priority / time.
        let priorities_view = ArrayView1::from(priorities);
        let ratio = <f32 as SimdUnifiedOps>::simd_div(&priorities_view, &times_safe.view());

        // Step 3 – SIMD scalar multiply by efficiency factor.
        let scored = <f32 as SimdUnifiedOps>::simd_scalar_mul(&ratio.view(), EFFICIENCY_FACTOR);

        // Step 4 – cast to f64 result vector.
        Ok(scored.iter().map(|&x| x as f64).collect())
    }

    #[cfg(feature = "scirs2-simd")]
    fn get_bandwidth_history(&self) -> Vec<f32> {
        // Simplified bandwidth history - in real implementation would track actual values
        vec![1000.0, 1100.0, 950.0, 1200.0, 1050.0, 1150.0, 980.0, 1300.0]
    }

    #[cfg(feature = "scirs2-simd")]
    fn get_task_duration_history(&self) -> Vec<f32> {
        // Simplified task duration history - in real implementation would track actual values
        vec![100.0, 150.0, 80.0, 200.0, 120.0, 90.0, 180.0, 110.0]
    }

    #[cfg(feature = "scirs2-simd")]
    fn standard_compress_tensor(&self, tensor: &Tensor) -> TorshResult<Vec<u8>> {
        // Lossless fallback serialization (used when SIMD optimization is off).
        // This is the real, self-describing wire encoding — NOT a placeholder.
        debug!(
            "Using standard tensor serialization for {} elements (SIMD disabled)",
            tensor.numel()
        );
        serialize_tensor_le(tensor)
    }
}

/// Number of bytes in the fixed prefix of the standard tensor wire format:
/// `magic(4) + version(1) + dtype(1) + ndim(4)`.
const TENSOR_WIRE_HEADER_LEN: usize = 10;

/// Magic bytes identifying a ToRSh standard-serialized tensor stream (`b"TSHT"`).
///
/// Used to reject malformed / foreign / empty input on deserialization instead
/// of silently producing a wrong or empty tensor.
const TENSOR_WIRE_MAGIC: [u8; 4] = *b"TSHT";

/// Version of the standard tensor wire format produced by [`serialize_tensor_le`].
const TENSOR_WIRE_VERSION: u8 = 1;

/// Map a [`torsh_core::dtype::DType`] to its stable 1-byte wire tag.
fn dtype_to_wire_tag(dtype: torsh_core::dtype::DType) -> u8 {
    use torsh_core::dtype::DType;
    match dtype {
        DType::U8 => 0,
        DType::I8 => 1,
        DType::I16 => 2,
        DType::I32 => 3,
        DType::U32 => 4,
        DType::I64 => 5,
        DType::U64 => 6,
        DType::F16 => 7,
        DType::F32 => 8,
        DType::F64 => 9,
        DType::Bool => 10,
        DType::BF16 => 11,
        DType::C64 => 12,
        DType::C128 => 13,
        DType::QInt8 => 14,
        DType::QUInt8 => 15,
        DType::QInt32 => 16,
    }
}

/// Map a 1-byte wire tag back to its [`torsh_core::dtype::DType`].
///
/// Returns `None` for unknown tags so the caller can reject malformed input.
fn wire_tag_to_dtype(tag: u8) -> Option<torsh_core::dtype::DType> {
    use torsh_core::dtype::DType;
    Some(match tag {
        0 => DType::U8,
        1 => DType::I8,
        2 => DType::I16,
        3 => DType::I32,
        4 => DType::U32,
        5 => DType::I64,
        6 => DType::U64,
        7 => DType::F16,
        8 => DType::F32,
        9 => DType::F64,
        10 => DType::Bool,
        11 => DType::BF16,
        12 => DType::C64,
        13 => DType::C128,
        14 => DType::QInt8,
        15 => DType::QUInt8,
        16 => DType::QInt32,
        _ => return None,
    })
}

/// Serialize an `f32` tensor into a self-describing little-endian byte stream.
///
/// This is the real inter-node wire encoding for the communication scheduler's
/// non-SIMD path. It is lossless and round-trips exactly through
/// [`deserialize_tensor_le`].
///
/// # Byte layout (all multi-byte integers little-endian)
///
/// | Offset      | Size        | Field                                          |
/// |-------------|-------------|------------------------------------------------|
/// | `0`         | `4`         | magic = `b"TSHT"`                              |
/// | `4`         | `1`         | format version (`1`)                           |
/// | `5`         | `1`         | dtype tag (see [`dtype_to_wire_tag`]; `8`=F32) |
/// | `6`         | `4`         | `ndim` (`u32`)                                 |
/// | `10`        | `ndim * 8`  | shape dims (`u64` each)                        |
/// | `10+ndim*8` | `8`         | `numel` (`u64`, must equal product of dims)    |
/// | …           | `numel * 4` | raw element bytes (`f32` little-endian)        |
///
/// The device is intentionally *not* encoded: a device handle is node-local, so
/// received tensors are materialized on [`torsh_core::device::DeviceType::Cpu`].
pub fn serialize_tensor_le(tensor: &Tensor) -> TorshResult<Vec<u8>> {
    let data = tensor.to_vec().map_err(|e| {
        TorshDistributedError::serialization_error(format!(
            "standard tensor serialization: failed to read tensor data: {e}"
        ))
    })?;

    let shape = tensor.shape();
    let dims = shape.dims();
    let dtype = tensor.dtype();
    let element_size = dtype.size();

    let mut out =
        Vec::with_capacity(TENSOR_WIRE_HEADER_LEN + dims.len() * 8 + 8 + data.len() * element_size);
    out.extend_from_slice(&TENSOR_WIRE_MAGIC);
    out.push(TENSOR_WIRE_VERSION);
    out.push(dtype_to_wire_tag(dtype));
    out.extend_from_slice(&(dims.len() as u32).to_le_bytes());
    for &dim in dims {
        out.extend_from_slice(&(dim as u64).to_le_bytes());
    }
    out.extend_from_slice(&(data.len() as u64).to_le_bytes());
    for &value in &data {
        out.extend_from_slice(&value.to_le_bytes());
    }

    Ok(out)
}

/// Reconstruct an `f32` tensor from a stream produced by [`serialize_tensor_le`].
///
/// Validates the magic bytes, version, dtype tag, declared `ndim`/`numel`, and
/// the exact payload length. Any mismatch (including empty or truncated input,
/// or trailing garbage) yields a [`TorshDistributedError::SerializationError`]
/// rather than a silently wrong or empty tensor.
pub fn deserialize_tensor_le(bytes: &[u8]) -> TorshResult<Tensor> {
    if bytes.len() < TENSOR_WIRE_HEADER_LEN {
        return Err(TorshDistributedError::serialization_error(format!(
            "standard tensor deserialization: input too short ({} bytes, need at least {})",
            bytes.len(),
            TENSOR_WIRE_HEADER_LEN
        )));
    }

    let magic = [bytes[0], bytes[1], bytes[2], bytes[3]];
    if magic != TENSOR_WIRE_MAGIC {
        return Err(TorshDistributedError::serialization_error(
            "standard tensor deserialization: bad magic bytes (not a ToRSh tensor stream)",
        ));
    }

    let version = bytes[4];
    if version != TENSOR_WIRE_VERSION {
        return Err(TorshDistributedError::serialization_error(format!(
            "standard tensor deserialization: unsupported wire version {version} (expected {TENSOR_WIRE_VERSION})"
        )));
    }

    let dtype_tag = bytes[5];
    let dtype = wire_tag_to_dtype(dtype_tag).ok_or_else(|| {
        TorshDistributedError::serialization_error(format!(
            "standard tensor deserialization: unknown dtype tag {dtype_tag}"
        ))
    })?;
    if dtype != torsh_core::dtype::DType::F32 {
        return Err(TorshDistributedError::serialization_error(format!(
            "standard tensor deserialization: dtype {dtype:?} unsupported (decoder produces f32 only)"
        )));
    }

    let ndim = u32::from_le_bytes([bytes[6], bytes[7], bytes[8], bytes[9]]) as usize;

    let dims_byte_len = ndim
        .checked_mul(8)
        .ok_or_else(|| TorshDistributedError::serialization_error("ndim overflow"))?;
    let dims_start = TENSOR_WIRE_HEADER_LEN;
    let dims_end = dims_start
        .checked_add(dims_byte_len)
        .ok_or_else(|| TorshDistributedError::serialization_error("header length overflow"))?;
    let numel_end = dims_end
        .checked_add(8)
        .ok_or_else(|| TorshDistributedError::serialization_error("header length overflow"))?;
    if bytes.len() < numel_end {
        return Err(TorshDistributedError::serialization_error(format!(
            "standard tensor deserialization: truncated header (have {} bytes, need {} for {}-D shape)",
            bytes.len(),
            numel_end,
            ndim
        )));
    }

    let mut dims = Vec::with_capacity(ndim);
    let mut product: usize = 1;
    for chunk in bytes[dims_start..dims_end].chunks_exact(8) {
        let dim = u64::from_le_bytes([
            chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7],
        ]) as usize;
        product = product
            .checked_mul(dim)
            .ok_or_else(|| TorshDistributedError::serialization_error("numel product overflow"))?;
        dims.push(dim);
    }

    let numel = u64::from_le_bytes([
        bytes[dims_end],
        bytes[dims_end + 1],
        bytes[dims_end + 2],
        bytes[dims_end + 3],
        bytes[dims_end + 4],
        bytes[dims_end + 5],
        bytes[dims_end + 6],
        bytes[dims_end + 7],
    ]) as usize;
    if numel != product {
        return Err(TorshDistributedError::serialization_error(format!(
            "standard tensor deserialization: numel mismatch (header says {numel}, dims product is {product})"
        )));
    }

    let element_size = dtype.size();
    let data_byte_len = numel
        .checked_mul(element_size)
        .ok_or_else(|| TorshDistributedError::serialization_error("payload length overflow"))?;
    let data_start = numel_end;
    let data_end = data_start
        .checked_add(data_byte_len)
        .ok_or_else(|| TorshDistributedError::serialization_error("payload length overflow"))?;
    if bytes.len() != data_end {
        return Err(TorshDistributedError::serialization_error(format!(
            "standard tensor deserialization: payload length mismatch (stream has {} bytes, expected {} = {}-byte header + {} payload)",
            bytes.len(),
            data_end,
            data_start,
            data_byte_len
        )));
    }

    let mut values = Vec::with_capacity(numel);
    for chunk in bytes[data_start..data_end].chunks_exact(element_size) {
        values.push(f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
    }

    Tensor::from_data(values, dims, torsh_core::device::DeviceType::Cpu).map_err(|e| {
        TorshDistributedError::serialization_error(format!(
            "standard tensor deserialization: failed to reconstruct tensor: {e}"
        ))
    })
}

/// Utility functions for communication scheduling
pub mod utils {
    use super::*;

    /// Create a scheduler with predefined configurations
    pub fn create_high_throughput_scheduler() -> CommunicationScheduler {
        let config = SchedulerConfig {
            max_concurrent_ops: 8,
            strategy: SchedulingStrategy::ShortestJobFirst,
            enable_compression: true,
            compression_threshold: 512 * 1024, // 512KB
            ..Default::default()
        };
        CommunicationScheduler::new(config)
    }

    /// Create a scheduler optimized for low latency
    pub fn create_low_latency_scheduler() -> CommunicationScheduler {
        let config = SchedulerConfig {
            max_concurrent_ops: 2,
            strategy: SchedulingStrategy::PriorityBased,
            adaptive_scheduling: true,
            timeout_ms: 5000,
            ..Default::default()
        };
        CommunicationScheduler::new(config)
    }

    /// Create a bandwidth-aware scheduler
    pub fn create_bandwidth_aware_scheduler(bandwidth_limit: u64) -> CommunicationScheduler {
        let config = SchedulerConfig {
            bandwidth_limit_bps: bandwidth_limit,
            strategy: SchedulingStrategy::Adaptive,
            adaptive_scheduling: true,
            enable_compression: true,
            ..Default::default()
        };
        CommunicationScheduler::new(config)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{init_process_group, BackendType};

    #[test]
    fn test_scheduler_config() {
        let config = SchedulerConfig::default();
        assert_eq!(config.max_concurrent_ops, 4);
        assert_eq!(config.strategy, SchedulingStrategy::PriorityBased);
        assert!(config.enable_priorities);
    }

    #[test]
    fn test_priority_ordering() {
        assert!(Priority::Critical > Priority::High);
        assert!(Priority::High > Priority::Normal);
        assert!(Priority::Normal > Priority::Low);
    }

    #[tokio::test]
    async fn test_scheduler_creation() {
        let config = SchedulerConfig::default();
        let scheduler = CommunicationScheduler::new(config);

        assert_eq!(scheduler.queue_size(), 0);
        assert!(scheduler.get_available_bandwidth() > 0);
    }

    #[tokio::test]
    async fn test_bandwidth_monitor() {
        let mut monitor = BandwidthMonitor::new(1_000_000_000);

        assert_eq!(monitor.get_available_bandwidth(), 1_000_000_000);

        monitor.update_bandwidth(1024, Duration::from_millis(1));
        // Should update bandwidth measurement
        assert!(monitor.get_available_bandwidth() > 0);
    }

    #[tokio::test]
    async fn test_task_scheduling() -> TorshResult<()> {
        let config = SchedulerConfig {
            max_concurrent_ops: 1,
            timeout_ms: 1000,
            ..Default::default()
        };
        let scheduler = CommunicationScheduler::new(config);

        let process_group =
            Arc::new(init_process_group(BackendType::Gloo, 0, 1, "127.0.0.1", 12345).await?);

        let tensor = torsh_tensor::creation::ones(&[4, 4])?;

        // Start scheduler
        scheduler.start().await?;

        // Schedule a task
        let result = scheduler
            .schedule_task(
                CommunicationOp::AllReduce,
                tensor.clone(),
                process_group,
                Priority::Normal,
            )
            .await;

        // In single-process mode, the operation should complete
        assert!(result.is_ok());

        // Stop scheduler
        scheduler.stop().await?;

        Ok(())
    }

    #[test]
    fn test_utils_schedulers() {
        let high_throughput = utils::create_high_throughput_scheduler();
        assert_eq!(high_throughput.config.max_concurrent_ops, 8);

        let low_latency = utils::create_low_latency_scheduler();
        assert_eq!(low_latency.config.max_concurrent_ops, 2);

        let bandwidth_aware = utils::create_bandwidth_aware_scheduler(500_000_000);
        assert_eq!(bandwidth_aware.config.bandwidth_limit_bps, 500_000_000);
    }

    #[tokio::test]
    async fn test_scheduler_stats() -> TorshResult<()> {
        let scheduler = CommunicationScheduler::new(SchedulerConfig::default());
        let stats = scheduler.get_stats();

        assert_eq!(stats.total_tasks, 0);
        assert_eq!(stats.completed_tasks, 0);
        assert_eq!(stats.current_queue_size, 0);

        Ok(())
    }

    #[test]
    fn test_standard_tensor_serialization_roundtrip() -> TorshResult<()> {
        // Distinctive shape and exactly-representable f32 values so equality is
        // bit-exact (no floating-point tolerance needed).
        let values = vec![1.5_f32, -2.25, 3.75, 0.0, 100.5, -0.125];
        let shape = vec![2_usize, 3];
        let tensor = Tensor::from_data(
            values.clone(),
            shape.clone(),
            torsh_core::device::DeviceType::Cpu,
        )?;

        let bytes = serialize_tensor_le(&tensor)?;

        // Must be real bytes, not the old empty / all-zero placeholder.
        assert!(!bytes.is_empty(), "serialized tensor must not be empty");
        let expected_len = TENSOR_WIRE_HEADER_LEN + shape.len() * 8 + 8 + values.len() * 4;
        assert_eq!(
            bytes.len(),
            expected_len,
            "serialized length must match the documented layout"
        );
        let magic = [bytes[0], bytes[1], bytes[2], bytes[3]];
        assert_eq!(
            magic, TENSOR_WIRE_MAGIC,
            "stream must start with the TSHT magic"
        );

        // Round-trip: shape and every element must survive exactly.
        let restored = deserialize_tensor_le(&bytes)?;
        let restored_shape = restored.shape();
        assert_eq!(
            restored_shape.dims(),
            shape.as_slice(),
            "shape must round-trip exactly"
        );
        let restored_values = restored.to_vec()?;
        assert_eq!(
            restored_values.len(),
            values.len(),
            "element count mismatch"
        );
        for (idx, (&orig, &got)) in values.iter().zip(restored_values.iter()).enumerate() {
            assert_eq!(
                orig.to_bits(),
                got.to_bits(),
                "element {idx} mismatch: {orig} != {got}"
            );
        }

        // A same-length all-zero payload (the old placeholder shape) must NOT
        // decode back to the original data — it is rejected outright here.
        let zero_payload = vec![0u8; bytes.len()];
        if let Ok(bogus) = deserialize_tensor_le(&zero_payload) {
            assert_ne!(
                bogus.to_vec()?,
                values,
                "all-zero bytes must never reproduce the original data"
            );
        }

        // Malformed / empty / truncated / corrupted input must error loudly,
        // never silently produce a wrong or empty tensor.
        assert!(
            deserialize_tensor_le(&[]).is_err(),
            "empty input must be rejected"
        );
        assert!(
            deserialize_tensor_le(&bytes[..TENSOR_WIRE_HEADER_LEN - 1]).is_err(),
            "short header must be rejected"
        );
        assert!(
            deserialize_tensor_le(&bytes[..bytes.len() - 1]).is_err(),
            "truncated payload must be rejected"
        );

        let mut bad_magic = bytes.clone();
        bad_magic[0] ^= 0xFF;
        assert!(
            deserialize_tensor_le(&bad_magic).is_err(),
            "corrupted magic must be rejected"
        );

        let mut trailing = bytes.clone();
        trailing.push(0u8);
        assert!(
            deserialize_tensor_le(&trailing).is_err(),
            "trailing garbage must be rejected"
        );

        Ok(())
    }
}