trustformers-core 0.1.1

Core traits and utilities for TrustformeRS
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
//! Model Parallel Support for Large Models
//!
//! This module provides infrastructure for distributing model layers and tensors
//! across multiple devices (GPUs) to enable training and inference of models
//! that are too large to fit on a single device.

#![allow(unused_variables)] // Model parallelism implementation

#[allow(unused_imports)] // Used conditionally based on feature gates
use crate::errors::{runtime_error, tensor_op_error, Result};
use crate::Tensor;
use std::sync::Arc;

/// Model parallelism strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModelParallelStrategy {
    /// Pipeline parallelism - split model by layers
    Pipeline,
    /// Tensor parallelism - split individual layers
    Tensor,
    /// Hybrid approach combining both
    Hybrid,
}

/// Configuration for model parallel execution
#[derive(Debug, Clone)]
pub struct ModelParallelConfig {
    /// Number of devices to use
    pub num_devices: usize,
    /// Parallelism strategy
    pub strategy: ModelParallelStrategy,
    /// Device IDs to use (e.g., [0, 1, 2, 3] for 4 GPUs)
    pub device_ids: Vec<usize>,
    /// Pipeline depth for pipeline parallelism
    pub pipeline_depth: Option<usize>,
    /// Tensor split dimension for tensor parallelism
    pub tensor_split_dim: Option<usize>,
    /// Enable gradient checkpointing to save memory
    pub gradient_checkpointing: bool,
    /// Communication backend
    pub comm_backend: CommunicationBackend,
}

impl Default for ModelParallelConfig {
    fn default() -> Self {
        Self {
            num_devices: 1,
            strategy: ModelParallelStrategy::Pipeline,
            device_ids: vec![0],
            pipeline_depth: None,
            tensor_split_dim: None,
            gradient_checkpointing: false,
            comm_backend: CommunicationBackend::Nccl,
        }
    }
}

/// Communication backend for model parallel
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum CommunicationBackend {
    /// NVIDIA Collective Communication Library
    Nccl,
    /// Message Passing Interface
    Mpi,
    /// Gloo (CPU communication)
    Gloo,
    /// Custom implementation
    Custom,
}

/// Distributed tensor that can be split across devices
#[derive(Debug, Clone)]
pub struct DistributedTensor {
    /// Local shard of the tensor
    pub local_shard: Tensor,
    /// Global shape of the full tensor
    pub global_shape: Vec<usize>,
    /// Partition info
    pub partition: TensorPartition,
    /// Device ID where this shard resides
    pub device_id: usize,
}

/// Information about how a tensor is partitioned
#[derive(Debug, Clone)]
pub struct TensorPartition {
    /// Dimension along which tensor is split
    pub split_dim: usize,
    /// Start index in the global tensor
    pub start_idx: usize,
    /// End index in the global tensor
    pub end_idx: usize,
    /// Total number of partitions
    pub num_partitions: usize,
    /// This partition's rank
    pub partition_rank: usize,
}

impl DistributedTensor {
    /// Create a new distributed tensor from a local shard
    pub fn new(
        local_shard: Tensor,
        global_shape: Vec<usize>,
        partition: TensorPartition,
        device_id: usize,
    ) -> Self {
        Self {
            local_shard,
            global_shape,
            partition,
            device_id,
        }
    }

    /// Get the local shape of this shard
    pub fn local_shape(&self) -> Vec<usize> {
        self.local_shard.shape()
    }

    /// Check if this tensor needs communication for operations
    pub fn requires_communication(&self) -> bool {
        self.partition.num_partitions > 1
    }
}

/// Model parallel context managing distributed execution
pub struct ModelParallelContext {
    config: ModelParallelConfig,
    rank: usize,
    world_size: usize,
    pub(crate) communicator: Arc<dyn Communicator>,
    #[allow(dead_code)]
    device_mesh: DeviceMesh,
}

impl ModelParallelContext {
    /// Initialize model parallel context
    pub fn new(config: ModelParallelConfig) -> Result<Self> {
        let world_size = config.num_devices;
        let rank = 0; // Will be set by init process

        let communicator = create_communicator(&config.comm_backend)?;
        let device_mesh = DeviceMesh::new(&config.device_ids, config.strategy)?;

        Ok(Self {
            config,
            rank,
            world_size,
            communicator,
            device_mesh,
        })
    }

    /// Get current process rank
    pub fn rank(&self) -> usize {
        self.rank
    }

    /// Get total world size
    pub fn world_size(&self) -> usize {
        self.world_size
    }

    /// Partition a tensor across devices
    pub fn partition_tensor(&self, tensor: &Tensor, split_dim: usize) -> Result<DistributedTensor> {
        let shape = tensor.shape();
        if split_dim >= shape.len() {
            return Err(tensor_op_error(
                "split_tensor",
                format!(
                    "Split dimension {} out of bounds for tensor with {} dimensions",
                    split_dim,
                    shape.len()
                ),
            ));
        }

        let dim_size = shape[split_dim];
        let chunk_size = dim_size.div_ceil(self.world_size);
        let start_idx = self.rank * chunk_size;
        let end_idx = ((self.rank + 1) * chunk_size).min(dim_size);

        // Extract local shard by slicing the tensor along the split dimension
        let local_shard = tensor.slice(split_dim, start_idx, end_idx)?;

        let partition = TensorPartition {
            split_dim,
            start_idx,
            end_idx,
            num_partitions: self.world_size,
            partition_rank: self.rank,
        };

        Ok(DistributedTensor::new(
            local_shard,
            shape.to_vec(),
            partition,
            self.config.device_ids[self.rank],
        ))
    }

    /// Gather distributed tensor to full tensor
    pub fn all_gather(&self, distributed: &DistributedTensor) -> Result<Tensor> {
        if !distributed.requires_communication() {
            return Ok(distributed.local_shard.clone());
        }

        self.communicator
            .all_gather(&distributed.local_shard, distributed.partition.split_dim)
    }

    /// Reduce scattered tensor across devices
    pub fn reduce_scatter(&self, tensor: &Tensor, split_dim: usize) -> Result<Tensor> {
        self.communicator.reduce_scatter(tensor, split_dim)
    }

    /// All-reduce operation for gradient synchronization
    pub fn all_reduce(&self, tensor: &mut Tensor) -> Result<()> {
        self.communicator.all_reduce(tensor)
    }

    /// Broadcast tensor from root rank to all other ranks
    pub fn broadcast(&self, tensor: &mut Tensor, root: usize) -> Result<()> {
        self.communicator.broadcast(tensor, root)
    }
}

/// Device mesh for organizing devices in model parallel
#[derive(Debug, Clone)]
pub struct DeviceMesh {
    /// Device IDs in the mesh
    device_ids: Vec<usize>,
    /// Topology of the mesh
    topology: MeshTopology,
}

#[derive(Debug, Clone)]
enum MeshTopology {
    /// Linear arrangement (for pipeline parallel)
    Linear,
    /// 2D mesh (for tensor parallel)
    Grid2D { rows: usize, cols: usize },
    /// 3D mesh (for hybrid parallel)
    #[allow(dead_code)]
    Grid3D { x: usize, y: usize, z: usize },
}

impl DeviceMesh {
    fn new(device_ids: &[usize], strategy: ModelParallelStrategy) -> Result<Self> {
        let topology = match strategy {
            ModelParallelStrategy::Pipeline => MeshTopology::Linear,
            ModelParallelStrategy::Tensor => {
                // For tensor parallel, try to create a balanced 2D grid
                let n = device_ids.len();
                let rows = (n as f64).sqrt().ceil() as usize;
                let cols = n.div_ceil(rows);
                MeshTopology::Grid2D { rows, cols }
            },
            ModelParallelStrategy::Hybrid => {
                // For hybrid, create a 3D mesh if possible
                // This is a simplified version
                MeshTopology::Linear
            },
        };

        Ok(Self {
            device_ids: device_ids.to_vec(),
            topology,
        })
    }

    /// Get device ID at a given coordinate
    pub fn device_at(&self, coord: &[usize]) -> Option<usize> {
        match &self.topology {
            MeshTopology::Linear => {
                coord.first().and_then(|&idx| self.device_ids.get(idx).copied())
            },
            MeshTopology::Grid2D { rows, cols } => {
                if coord.len() >= 2 {
                    let idx = coord[0] * cols + coord[1];
                    self.device_ids.get(idx).copied()
                } else {
                    None
                }
            },
            MeshTopology::Grid3D { x, y, z } => {
                if coord.len() >= 3 {
                    let idx = coord[0] * y * z + coord[1] * z + coord[2];
                    self.device_ids.get(idx).copied()
                } else {
                    None
                }
            },
        }
    }
}

/// Communication interface for model parallel operations
pub trait Communicator: Send + Sync {
    /// All-gather operation
    fn all_gather(&self, tensor: &Tensor, split_dim: usize) -> Result<Tensor>;

    /// Reduce-scatter operation
    fn reduce_scatter(&self, tensor: &Tensor, split_dim: usize) -> Result<Tensor>;

    /// All-reduce operation
    fn all_reduce(&self, tensor: &mut Tensor) -> Result<()>;

    /// Point-to-point send
    fn send(&self, tensor: &Tensor, dest: usize) -> Result<()>;

    /// Point-to-point receive
    fn recv(&self, shape: &[usize], src: usize) -> Result<Tensor>;

    /// Broadcast from root
    fn broadcast(&self, tensor: &mut Tensor, root: usize) -> Result<()>;
}

/// Create appropriate communicator based on backend
fn create_communicator(backend: &CommunicationBackend) -> Result<Arc<dyn Communicator>> {
    match backend {
        CommunicationBackend::Nccl => {
            #[cfg(feature = "nccl")]
            {
                use super::nccl_communicator::create_nccl_communicator;
                // Default to rank 0, world_size 1, device 0 for single-process case
                // In a real distributed setup, these would come from environment or config
                let rank =
                    std::env::var("RANK").unwrap_or_else(|_| "0".to_string()).parse().unwrap_or(0);
                let world_size = std::env::var("WORLD_SIZE")
                    .unwrap_or_else(|_| "1".to_string())
                    .parse()
                    .unwrap_or(1);
                let device_id = std::env::var("LOCAL_RANK")
                    .unwrap_or_else(|_| "0".to_string())
                    .parse()
                    .unwrap_or(0);

                create_nccl_communicator(rank, world_size, device_id)
            }

            #[cfg(not(feature = "nccl"))]
            return Err(runtime_error(
                "NCCL backend requested but not compiled with nccl feature",
            ));
        },
        CommunicationBackend::Mpi => {
            use super::mpi_communicator::MpiCommunicatorImpl;
            Ok(Arc::new(MpiCommunicatorImpl::new()?))
        },
        CommunicationBackend::Gloo => {
            // Fallback to mock for now
            Ok(Arc::new(MockCommunicator::new()))
        },
        CommunicationBackend::Custom => Ok(Arc::new(MockCommunicator::new())),
    }
}

/// Mock communicator for testing
struct MockCommunicator;

impl MockCommunicator {
    fn new() -> Self {
        Self
    }
}

impl Communicator for MockCommunicator {
    fn all_gather(&self, tensor: &Tensor, _split_dim: usize) -> Result<Tensor> {
        // In mock mode, just return the tensor as-is
        Ok(tensor.clone())
    }

    fn reduce_scatter(&self, tensor: &Tensor, _split_dim: usize) -> Result<Tensor> {
        Ok(tensor.clone())
    }

    fn all_reduce(&self, _tensor: &mut Tensor) -> Result<()> {
        Ok(())
    }

    fn send(&self, _tensor: &Tensor, _dest: usize) -> Result<()> {
        Ok(())
    }

    fn recv(&self, shape: &[usize], _src: usize) -> Result<Tensor> {
        Tensor::zeros(shape)
    }

    fn broadcast(&self, _tensor: &mut Tensor, _root: usize) -> Result<()> {
        Ok(())
    }
}

/// Pipeline parallel schedule for forward/backward passes
#[derive(Debug, Clone)]
pub struct PipelineSchedule {
    /// Number of pipeline stages
    pub num_stages: usize,
    /// Number of microbatches
    pub num_microbatches: usize,
    /// Schedule type
    pub schedule_type: PipelineScheduleType,
}

#[derive(Debug, Clone, Copy)]
pub enum PipelineScheduleType {
    /// Forward then backward (simple but inefficient)
    Sequential,
    /// 1F1B schedule (one forward, one backward)
    OneForwardOneBackward,
    /// Interleaved 1F1B for better efficiency
    InterleavedOneF1B,
}

impl PipelineSchedule {
    /// Create a new pipeline schedule
    pub fn new(
        num_stages: usize,
        num_microbatches: usize,
        schedule_type: PipelineScheduleType,
    ) -> Self {
        Self {
            num_stages,
            num_microbatches,
            schedule_type,
        }
    }

    /// Get the schedule for a specific stage
    pub fn get_stage_schedule(&self, stage_id: usize) -> Vec<PipelineOp> {
        match self.schedule_type {
            PipelineScheduleType::Sequential => self.sequential_schedule(stage_id),
            PipelineScheduleType::OneForwardOneBackward => self.one_f1b_schedule(stage_id),
            PipelineScheduleType::InterleavedOneF1B => self.interleaved_1f1b_schedule(stage_id),
        }
    }

    fn sequential_schedule(&self, stage_id: usize) -> Vec<PipelineOp> {
        let mut ops = Vec::new();

        // All forwards first
        for mb in 0..self.num_microbatches {
            ops.push(PipelineOp::Forward { microbatch_id: mb });
        }

        // Then all backwards
        for mb in (0..self.num_microbatches).rev() {
            ops.push(PipelineOp::Backward { microbatch_id: mb });
        }

        ops
    }

    fn one_f1b_schedule(&self, stage_id: usize) -> Vec<PipelineOp> {
        let mut ops = Vec::new();
        let num_warmup = self.num_stages - stage_id - 1;

        // Warmup phase - only forward
        for mb in 0..num_warmup.min(self.num_microbatches) {
            ops.push(PipelineOp::Forward { microbatch_id: mb });
        }

        // Steady state - 1F1B
        let steady_state_mbs = self.num_microbatches.saturating_sub(num_warmup);
        for i in 0..steady_state_mbs {
            let forward_mb = num_warmup + i;
            let backward_mb = i;

            if forward_mb < self.num_microbatches {
                ops.push(PipelineOp::Forward {
                    microbatch_id: forward_mb,
                });
            }
            ops.push(PipelineOp::Backward {
                microbatch_id: backward_mb,
            });
        }

        // Cooldown phase - only backward
        for mb in steady_state_mbs..self.num_microbatches {
            ops.push(PipelineOp::Backward { microbatch_id: mb });
        }

        ops
    }

    fn interleaved_1f1b_schedule(&self, _stage_id: usize) -> Vec<PipelineOp> {
        // Simplified version - can be optimized further
        self.one_f1b_schedule(_stage_id)
    }
}

#[derive(Debug, Clone)]
pub enum PipelineOp {
    Forward { microbatch_id: usize },
    Backward { microbatch_id: usize },
    SendActivation { to_stage: usize },
    RecvActivation { from_stage: usize },
    SendGradient { to_stage: usize },
    RecvGradient { from_stage: usize },
}

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

    #[test]
    fn test_tensor_partition() {
        let ctx = ModelParallelContext::new(ModelParallelConfig {
            num_devices: 4,
            device_ids: vec![0, 1, 2, 3],
            comm_backend: CommunicationBackend::Custom, // Use mock backend for tests
            ..Default::default()
        })
        .expect("operation failed in test");

        let tensor = Tensor::zeros(&[128, 512]).expect("Failed to create zero tensor");
        let distributed = ctx.partition_tensor(&tensor, 0).expect("tensor operation failed");

        // Verify partition metadata is correct
        assert_eq!(distributed.global_shape, vec![128, 512]);
        assert_eq!(distributed.partition.split_dim, 0);
        assert_eq!(distributed.partition.start_idx, 0);
        assert_eq!(distributed.partition.end_idx, 32);
        assert_eq!(distributed.partition.num_partitions, 4);

        // Check local tensor shape after slicing
        let local_shape = distributed.local_shard.shape();
        assert_eq!(local_shape, vec![32, 512]); // First dimension should be sliced to 32
    }

    #[test]
    fn test_device_mesh() {
        let mesh = DeviceMesh::new(&[0, 1, 2, 3], ModelParallelStrategy::Tensor)
            .expect("tensor operation failed");

        assert_eq!(mesh.device_at(&[0, 0]), Some(0));
        assert_eq!(mesh.device_at(&[0, 1]), Some(1));
        assert_eq!(mesh.device_at(&[1, 0]), Some(2));
        assert_eq!(mesh.device_at(&[1, 1]), Some(3));
    }

    #[test]
    fn test_pipeline_schedule() {
        let schedule = PipelineSchedule::new(4, 8, PipelineScheduleType::OneForwardOneBackward);
        let stage0_ops = schedule.get_stage_schedule(0);

        // Stage 0 should have 3 warmup forwards
        let forward_ops: Vec<_> = stage0_ops
            .iter()
            .filter(|op| matches!(op, PipelineOp::Forward { .. }))
            .collect();
        assert!(!forward_ops.is_empty());
    }
}