numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Model Parallelism for Large-Scale Deep Learning
//!
//! This module implements model-parallel training patterns where the model is
//! partitioned across workers, enabling training of models too large for a single device.
//!
//! # Features
//!
//! - **Layer-wise Partitioning**: Split model by layers
//! - **Pipeline Parallelism**: GPipe-style micro-batching
//! - **Tensor Parallelism**: Megatron-style intra-layer partitioning
//! - **Activation Checkpointing**: Memory-efficient backpropagation
//! - **Gradient Accumulation**: Multi-microbatch training
//!
//! # Parallelism Patterns
//!
//! ## Pipeline Parallelism (GPipe)
//! ```text
//! Time →
//! GPU0: [F0] [F1] [F2] [B0] [B1] [B2]
//! GPU1:      [F0] [F1] [F2] [B0] [B1] [B2]
//! GPU2:           [F0] [F1] [F2] [B0] [B1]
//! (F=Forward, B=Backward, numbers=microbatch)
//! ```
//!
//! ## Tensor Parallelism (Megatron)
//! ```text
//! Input → [Split] → GPU0: Linear_A
//!                  GPU1: Linear_B
//!        [Concat] → Output
//! ```
//!
//! # Example
//!
//! ```rust,no_run
//! use numrs2::distributed::model_parallel::*;
//! use numrs2::distributed::process::*;
//! use std::sync::Arc;
//!
//! # async fn example() -> Result<(), ModelParallelError> {
//! let world = init().await?;
//!
//! // Pipeline parallelism - split model into 4 stages
//! let pipeline = PipelineParallel::new(
//!     Arc::new(world.clone()),
//!     4,  // number of pipeline stages
//!     8,  // number of microbatches
//! )?;
//!
//! // Tensor parallelism - partition large layers
//! let tensor_parallel = TensorParallel::new(
//!     Arc::new(world),
//!     PartitionStrategy::ColumnWise,
//! )?;
//!
//! // Activation checkpointing for memory efficiency
//! let checkpointer = ActivationCheckpointer::new(2)?; // checkpoint every 2 layers
//! # Ok(())
//! # }
//! ```

use super::communication::{
    AsyncCommunicator, CommunicationError, CompressionStrategy, MessagePriority, TensorMessage,
};
use super::coordinator::CoordinatorError;
use super::process::{Communicator, ProcessError};
use crate::error::NumRs2Error;
use scirs2_core::ndarray::{Array1, Array2};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::{Mutex, RwLock};

/// Errors in model parallel operations
#[derive(Error, Debug)]
pub enum ModelParallelError {
    #[error("Process error: {0}")]
    Process(#[from] ProcessError),

    #[error("Communication error: {0}")]
    Communication(#[from] CommunicationError),

    #[error("Coordinator error: {0}")]
    Coordinator(#[from] CoordinatorError),

    #[error("Invalid stage assignment: stage {stage} out of {total}")]
    InvalidStage { stage: usize, total: usize },

    #[error("Partition error: {0}")]
    PartitionError(String),

    #[error("Pipeline error: {0}")]
    PipelineError(String),

    #[error("Checkpoint error: {0}")]
    CheckpointError(String),

    #[error("Invalid microbatch: {0}")]
    InvalidMicrobatch(String),
}

impl From<ModelParallelError> for NumRs2Error {
    fn from(err: ModelParallelError) -> Self {
        NumRs2Error::DistributedComputing(err.to_string())
    }
}

/// Tensor partitioning strategy
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, oxicode::Encode, oxicode::Decode,
)]
pub enum PartitionStrategy {
    /// Split along columns (for linear layers)
    ColumnWise,

    /// Split along rows (for linear layers)
    RowWise,

    /// Split along batch dimension
    BatchWise,

    /// Split along sequence dimension (for transformers)
    SequenceWise,
}

/// Pipeline stage information
#[derive(Debug, Clone)]
pub struct PipelineStage {
    /// Stage ID
    pub stage_id: usize,

    /// Total number of stages
    pub num_stages: usize,

    /// Ranks assigned to this stage
    pub ranks: Vec<usize>,

    /// Previous stage (None for first stage)
    pub prev_stage: Option<usize>,

    /// Next stage (None for last stage)
    pub next_stage: Option<usize>,
}

impl PipelineStage {
    /// Create new pipeline stage
    pub fn new(stage_id: usize, num_stages: usize, ranks: Vec<usize>) -> Self {
        let prev_stage = if stage_id > 0 {
            Some(stage_id - 1)
        } else {
            None
        };

        let next_stage = if stage_id < num_stages - 1 {
            Some(stage_id + 1)
        } else {
            None
        };

        Self {
            stage_id,
            num_stages,
            ranks,
            prev_stage,
            next_stage,
        }
    }

    /// Check if this is the first stage
    pub fn is_first(&self) -> bool {
        self.stage_id == 0
    }

    /// Check if this is the last stage
    pub fn is_last(&self) -> bool {
        self.stage_id == self.num_stages - 1
    }
}

/// Microbatch for pipeline parallelism
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Microbatch<T> {
    /// Microbatch ID
    pub id: usize,

    /// Data
    pub data: Vec<T>,

    /// Shape
    pub shape: Vec<usize>,

    /// Stage where this microbatch is currently
    pub current_stage: usize,
}

impl<T: Clone> Microbatch<T> {
    /// Create new microbatch
    pub fn new(id: usize, data: Vec<T>, shape: Vec<usize>) -> Self {
        Self {
            id,
            data,
            shape,
            current_stage: 0,
        }
    }

    /// Move to next stage
    pub fn advance_stage(&mut self) {
        self.current_stage += 1;
    }
}

/// Pipeline parallel coordinator
pub struct PipelineParallel {
    /// Communicator
    communicator: Arc<Communicator>,

    /// Async communicator
    async_comm: AsyncCommunicator,

    /// This worker's pipeline stage
    stage: PipelineStage,

    /// Number of microbatches
    num_microbatches: usize,

    /// Forward activation buffer
    forward_buffer: Arc<Mutex<HashMap<usize, Vec<f32>>>>,

    /// Backward gradient buffer
    backward_buffer: Arc<Mutex<HashMap<usize, Vec<f32>>>>,

    /// Pipeline schedule
    schedule: PipelineSchedule,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PipelineSchedule {
    /// GPipe: forward all microbatches, then backward all
    GPipe,

    /// PipeDream: interleaved 1F1B (one forward, one backward)
    OneFOneB,
}

impl PipelineParallel {
    /// Create new pipeline parallel coordinator
    pub fn new(
        communicator: Arc<Communicator>,
        num_stages: usize,
        num_microbatches: usize,
    ) -> Result<Self, ModelParallelError> {
        let async_comm = AsyncCommunicator::new(communicator.clone())?;

        let rank = communicator.rank();
        let world_size = communicator.size();

        // Assign ranks to stages
        let ranks_per_stage = world_size.div_ceil(num_stages);
        let stage_id = rank / ranks_per_stage;

        if stage_id >= num_stages {
            return Err(ModelParallelError::InvalidStage {
                stage: stage_id,
                total: num_stages,
            });
        }

        // Calculate ranks in this stage
        let stage_start = stage_id * ranks_per_stage;
        let stage_end = (stage_start + ranks_per_stage).min(world_size);
        let ranks: Vec<usize> = (stage_start..stage_end).collect();

        let stage = PipelineStage::new(stage_id, num_stages, ranks);

        Ok(Self {
            communicator,
            async_comm,
            stage,
            num_microbatches,
            forward_buffer: Arc::new(Mutex::new(HashMap::new())),
            backward_buffer: Arc::new(Mutex::new(HashMap::new())),
            schedule: PipelineSchedule::GPipe,
        })
    }

    /// Send forward activations to next stage
    pub async fn send_forward(
        &self,
        microbatch_id: usize,
        activations: &[f32],
    ) -> Result<(), ModelParallelError> {
        if let Some(next_stage) = self.stage.next_stage {
            let next_rank = next_stage * self.communicator.size().div_ceil(self.stage.num_stages);

            let msg = TensorMessage::new(
                activations.to_vec(),
                CompressionStrategy::None,
                MessagePriority::High,
            )
            .with_tag(microbatch_id as u32);

            self.async_comm.isend(msg, next_rank).await?;
        }

        Ok(())
    }

    /// Receive forward activations from previous stage
    pub async fn recv_forward(&self, microbatch_id: usize) -> Result<Vec<f32>, ModelParallelError> {
        if let Some(prev_stage) = self.stage.prev_stage {
            let prev_rank = prev_stage * self.communicator.size().div_ceil(self.stage.num_stages);

            // Check buffer first
            let mut buffer = self.forward_buffer.lock().await;
            if let Some(data) = buffer.remove(&microbatch_id) {
                return Ok(data);
            }
            drop(buffer);

            // Would receive from previous stage in real implementation
            let _ = prev_rank;
            Ok(vec![0.0; 10]) // Placeholder
        } else {
            Err(ModelParallelError::PipelineError(
                "No previous stage to receive from".to_string(),
            ))
        }
    }

    /// Send backward gradients to previous stage
    pub async fn send_backward(
        &self,
        microbatch_id: usize,
        gradients: &[f32],
    ) -> Result<(), ModelParallelError> {
        if let Some(prev_stage) = self.stage.prev_stage {
            let prev_rank = prev_stage * self.communicator.size().div_ceil(self.stage.num_stages);

            let msg = TensorMessage::new(
                gradients.to_vec(),
                CompressionStrategy::None,
                MessagePriority::High,
            )
            .with_tag(microbatch_id as u32);

            self.async_comm.isend(msg, prev_rank).await?;
        }

        Ok(())
    }

    /// Receive backward gradients from next stage
    pub async fn recv_backward(
        &self,
        microbatch_id: usize,
    ) -> Result<Vec<f32>, ModelParallelError> {
        if let Some(next_stage) = self.stage.next_stage {
            let next_rank = next_stage * self.communicator.size().div_ceil(self.stage.num_stages);

            // Check buffer first
            let mut buffer = self.backward_buffer.lock().await;
            if let Some(data) = buffer.remove(&microbatch_id) {
                return Ok(data);
            }
            drop(buffer);

            // Would receive from next stage in real implementation
            let _ = next_rank;
            Ok(vec![0.0; 10]) // Placeholder
        } else {
            Err(ModelParallelError::PipelineError(
                "No next stage to receive from".to_string(),
            ))
        }
    }

    /// Get stage information
    pub fn stage(&self) -> &PipelineStage {
        &self.stage
    }

    /// Get number of microbatches
    pub fn num_microbatches(&self) -> usize {
        self.num_microbatches
    }
}

/// Tensor parallel coordinator
pub struct TensorParallel {
    /// Communicator
    communicator: Arc<Communicator>,

    /// Async communicator
    async_comm: AsyncCommunicator,

    /// Partition strategy
    strategy: PartitionStrategy,

    /// Tensor parallel group size
    tp_size: usize,

    /// Rank within tensor parallel group
    tp_rank: usize,
}

impl TensorParallel {
    /// Create new tensor parallel coordinator
    pub fn new(
        communicator: Arc<Communicator>,
        strategy: PartitionStrategy,
    ) -> Result<Self, ModelParallelError> {
        let async_comm = AsyncCommunicator::new(communicator.clone())?;
        let tp_size = communicator.size();
        let tp_rank = communicator.rank();

        Ok(Self {
            communicator,
            async_comm,
            strategy,
            tp_size,
            tp_rank,
        })
    }

    /// Partition tensor according to strategy
    pub fn partition(
        &self,
        tensor: &[f32],
        shape: &[usize],
    ) -> Result<Vec<f32>, ModelParallelError> {
        match self.strategy {
            PartitionStrategy::ColumnWise => {
                if shape.len() != 2 {
                    return Err(ModelParallelError::PartitionError(
                        "ColumnWise partition requires 2D tensor".to_string(),
                    ));
                }

                let cols = shape[1];
                let cols_per_rank = cols.div_ceil(self.tp_size);
                let start_col = self.tp_rank * cols_per_rank;
                let end_col = (start_col + cols_per_rank).min(cols);

                // Extract columns for this rank
                let mut partition = Vec::new();
                for row in 0..shape[0] {
                    for col in start_col..end_col {
                        let idx = row * cols + col;
                        if idx < tensor.len() {
                            partition.push(tensor[idx]);
                        }
                    }
                }

                Ok(partition)
            }

            PartitionStrategy::RowWise => {
                if shape.len() != 2 {
                    return Err(ModelParallelError::PartitionError(
                        "RowWise partition requires 2D tensor".to_string(),
                    ));
                }

                let rows = shape[0];
                let rows_per_rank = rows.div_ceil(self.tp_size);
                let start_row = self.tp_rank * rows_per_rank;
                let end_row = (start_row + rows_per_rank).min(rows);

                let cols = shape[1];
                let mut partition = Vec::new();

                for row in start_row..end_row {
                    for col in 0..cols {
                        let idx = row * cols + col;
                        if idx < tensor.len() {
                            partition.push(tensor[idx]);
                        }
                    }
                }

                Ok(partition)
            }

            PartitionStrategy::BatchWise | PartitionStrategy::SequenceWise => {
                // Simple block partitioning
                let chunk_size = tensor.len().div_ceil(self.tp_size);
                let start = self.tp_rank * chunk_size;
                let end = (start + chunk_size).min(tensor.len());

                Ok(tensor[start..end].to_vec())
            }
        }
    }

    /// All-gather partitioned tensors
    pub async fn gather(&self, local_tensor: &[f32]) -> Result<Vec<f32>, ModelParallelError> {
        // Would perform actual all-gather in real implementation
        // For now, just return local tensor
        Ok(local_tensor.to_vec())
    }

    /// Get partition strategy
    pub fn strategy(&self) -> PartitionStrategy {
        self.strategy
    }

    /// Get tensor parallel size
    pub fn tp_size(&self) -> usize {
        self.tp_size
    }

    /// Get tensor parallel rank
    pub fn tp_rank(&self) -> usize {
        self.tp_rank
    }
}

/// Activation checkpointer for memory-efficient training
pub struct ActivationCheckpointer {
    /// Checkpoint interval (checkpoint every N layers)
    interval: usize,

    /// Stored checkpoints (layer_id -> activations)
    checkpoints: Arc<RwLock<HashMap<usize, Vec<f32>>>>,

    /// Recomputation count
    recomputation_count: Arc<Mutex<usize>>,
}

impl ActivationCheckpointer {
    /// Create new activation checkpointer
    pub fn new(interval: usize) -> Result<Self, ModelParallelError> {
        Ok(Self {
            interval,
            checkpoints: Arc::new(RwLock::new(HashMap::new())),
            recomputation_count: Arc::new(Mutex::new(0)),
        })
    }

    /// Check if layer should be checkpointed
    pub fn should_checkpoint(&self, layer_id: usize) -> bool {
        layer_id.is_multiple_of(self.interval)
    }

    /// Store checkpoint for layer
    pub async fn checkpoint(&self, layer_id: usize, activations: Vec<f32>) {
        let mut checkpoints = self.checkpoints.write().await;
        checkpoints.insert(layer_id, activations);
    }

    /// Retrieve checkpoint
    pub async fn get_checkpoint(&self, layer_id: usize) -> Option<Vec<f32>> {
        let checkpoints = self.checkpoints.read().await;
        checkpoints.get(&layer_id).cloned()
    }

    /// Clear all checkpoints
    pub async fn clear(&self) {
        let mut checkpoints = self.checkpoints.write().await;
        checkpoints.clear();
    }

    /// Get recomputation count
    pub async fn recomputation_count(&self) -> usize {
        *self.recomputation_count.lock().await
    }

    /// Increment recomputation count
    pub async fn increment_recomputation(&self) {
        let mut count = self.recomputation_count.lock().await;
        *count += 1;
    }

    /// Get checkpoint interval
    pub fn interval(&self) -> usize {
        self.interval
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_partition_strategy_serialization() {
        let strategies = vec![
            PartitionStrategy::ColumnWise,
            PartitionStrategy::RowWise,
            PartitionStrategy::BatchWise,
            PartitionStrategy::SequenceWise,
        ];

        for strategy in strategies {
            let serialized = oxicode::encode_to_vec(&strategy);
            assert!(serialized.is_ok());

            let bytes = serialized.expect("serialization failed");
            let result = oxicode::decode_from_slice::<PartitionStrategy>(&bytes);
            assert!(result.is_ok());
            let (deserialized, _) = result.expect("deserialization failed");
            assert_eq!(
                std::mem::discriminant(&strategy),
                std::mem::discriminant(&deserialized)
            );
        }
    }

    #[test]
    fn test_pipeline_stage_creation() {
        let stage = PipelineStage::new(1, 4, vec![2, 3]);

        assert_eq!(stage.stage_id, 1);
        assert_eq!(stage.num_stages, 4);
        assert_eq!(stage.ranks, vec![2, 3]);
        assert_eq!(stage.prev_stage, Some(0));
        assert_eq!(stage.next_stage, Some(2));
        assert!(!stage.is_first());
        assert!(!stage.is_last());
    }

    #[test]
    fn test_pipeline_stage_first() {
        let stage = PipelineStage::new(0, 4, vec![0]);

        assert!(stage.is_first());
        assert!(!stage.is_last());
        assert_eq!(stage.prev_stage, None);
        assert_eq!(stage.next_stage, Some(1));
    }

    #[test]
    fn test_pipeline_stage_last() {
        let stage = PipelineStage::new(3, 4, vec![6, 7]);

        assert!(!stage.is_first());
        assert!(stage.is_last());
        assert_eq!(stage.prev_stage, Some(2));
        assert_eq!(stage.next_stage, None);
    }

    #[test]
    fn test_microbatch_creation() {
        let data = vec![1.0, 2.0, 3.0, 4.0];
        let shape = vec![2, 2];
        let mb = Microbatch::new(0, data.clone(), shape.clone());

        assert_eq!(mb.id, 0);
        assert_eq!(mb.data, data);
        assert_eq!(mb.shape, shape);
        assert_eq!(mb.current_stage, 0);
    }

    #[test]
    fn test_microbatch_advance() {
        let mut mb = Microbatch::new(0, vec![1.0], vec![1]);

        assert_eq!(mb.current_stage, 0);
        mb.advance_stage();
        assert_eq!(mb.current_stage, 1);
        mb.advance_stage();
        assert_eq!(mb.current_stage, 2);
    }

    #[test]
    fn test_activation_checkpointer_should_checkpoint() {
        let checkpointer = ActivationCheckpointer::new(2).expect("checkpointer creation failed");

        assert!(checkpointer.should_checkpoint(0));
        assert!(!checkpointer.should_checkpoint(1));
        assert!(checkpointer.should_checkpoint(2));
        assert!(!checkpointer.should_checkpoint(3));
        assert!(checkpointer.should_checkpoint(4));
    }

    #[tokio::test]
    async fn test_activation_checkpointer_store_retrieve() {
        let checkpointer = ActivationCheckpointer::new(1).expect("checkpointer creation failed");

        let activations = vec![1.0, 2.0, 3.0];
        checkpointer.checkpoint(0, activations.clone()).await;

        let retrieved = checkpointer.get_checkpoint(0).await;
        assert!(retrieved.is_some());
        assert_eq!(retrieved.expect("checkpoint retrieval failed"), activations);
    }

    #[tokio::test]
    async fn test_activation_checkpointer_clear() {
        let checkpointer = ActivationCheckpointer::new(1).expect("checkpointer creation failed");

        checkpointer.checkpoint(0, vec![1.0, 2.0]).await;
        checkpointer.checkpoint(1, vec![3.0, 4.0]).await;

        checkpointer.clear().await;

        assert_eq!(checkpointer.get_checkpoint(0).await, None);
        assert_eq!(checkpointer.get_checkpoint(1).await, None);
    }

    #[tokio::test]
    async fn test_activation_checkpointer_recomputation_count() {
        let checkpointer = ActivationCheckpointer::new(2).expect("checkpointer creation failed");

        assert_eq!(checkpointer.recomputation_count().await, 0);

        checkpointer.increment_recomputation().await;
        assert_eq!(checkpointer.recomputation_count().await, 1);

        checkpointer.increment_recomputation().await;
        assert_eq!(checkpointer.recomputation_count().await, 2);
    }

    #[test]
    fn test_activation_checkpointer_interval() {
        let checkpointer = ActivationCheckpointer::new(3).expect("checkpointer creation failed");

        assert_eq!(checkpointer.interval(), 3);
    }

    #[test]
    fn test_partition_strategy_equality() {
        assert_eq!(PartitionStrategy::ColumnWise, PartitionStrategy::ColumnWise);
        assert_ne!(PartitionStrategy::ColumnWise, PartitionStrategy::RowWise);
    }
}