rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! Data parallel training implementation
//! データ並列学習実装
//!
//! This module provides data parallel training capabilities where the same model
//! is replicated across multiple devices and data is split among them.
//!
//! このモジュールは、同じモデルを複数のデバイスに複製し、
//! データをそれらの間で分割するデータ並列学習機能を提供します。

use crate::autograd::Variable;
use crate::distributed::get_distributed_state;
use crate::error::{RusTorchError, RusTorchResult};
use crate::gpu::DeviceType;
use crate::nn::Module;
use crate::tensor::Tensor;
use num_traits::Float;
use std::sync::Arc;

/// Data parallel wrapper for models
/// モデル用データ並列ラッパー
#[derive(Debug)]
pub struct DataParallel<T, M>
where
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
    M: Module<T> + Send + Sync,
{
    /// Base model to be replicated
    /// 複製されるベースモデル
    module: Arc<M>,
    /// Devices to use for parallel training
    /// 並列学習に使用するデバイス
    device_ids: Vec<DeviceType>,
    /// Gradient synchronization strategy
    /// 勾配同期戦略
    sync_strategy: GradientSyncStrategy,
    _phantom: std::marker::PhantomData<T>,
}

/// Gradient synchronization strategies
/// 勾配同期戦略
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GradientSyncStrategy {
    /// Synchronize gradients after each backward pass
    /// 各バックワードパス後に勾配を同期
    Synchronous,
    /// Asynchronous gradient updates
    /// 非同期勾配更新
    Asynchronous,
    /// Local SGD with periodic synchronization
    /// 定期同期を伴うローカルSGD
    LocalSGD {
        /// Frequency of synchronization in steps
        /// 同期の頻度(ステップ数)
        sync_frequency: usize,
    },
}

impl<T, M> DataParallel<T, M>
where
    T: Float + Send + Sync + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive,
    M: Module<T> + Send + Sync + 'static,
{
    /// Create a new data parallel wrapper
    /// 新しいデータ並列ラッパーを作成
    pub fn new(module: M, device_ids: Vec<DeviceType>) -> Self {
        Self {
            module: Arc::new(module),
            device_ids,
            sync_strategy: GradientSyncStrategy::Synchronous,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Set gradient synchronization strategy
    /// 勾配同期戦略を設定
    pub fn set_sync_strategy(&mut self, strategy: GradientSyncStrategy) {
        self.sync_strategy = strategy;
    }

    /// Get number of devices
    /// デバイス数を取得
    pub fn num_devices(&self) -> usize {
        self.device_ids.len()
    }

    /// Replicate input across devices
    /// 入力をデバイス間で複製
    pub fn replicate_input(&self, input: &Variable<T>) -> RusTorchResult<Vec<Variable<T>>> {
        let batch_size = input.data().read().unwrap().shape()[0];
        let chunk_size = batch_size.div_ceil(self.device_ids.len());

        let mut replicated_inputs = Vec::new();

        for (i, _device) in self.device_ids.iter().enumerate() {
            let start_idx = i * chunk_size;
            let end_idx = ((i + 1) * chunk_size).min(batch_size);

            if start_idx < batch_size {
                // Create a slice of the input for this device
                // このデバイス用の入力スライスを作成
                let chunk_shape = {
                    let input_data = input.data();
                    let data_guard = input_data.read().unwrap();
                    let mut shape = data_guard.shape().to_vec();
                    shape[0] = end_idx - start_idx;
                    shape
                };

                // Extract chunk data (simplified implementation)
                // チャンクデータを抽出(簡略化実装)
                let chunk_tensor = Tensor::zeros(&chunk_shape);
                let chunk_var = Variable::new(chunk_tensor, input.requires_grad());

                replicated_inputs.push(chunk_var);
            }
        }

        Ok(replicated_inputs)
    }

    /// Gather outputs from devices
    /// デバイスから出力を集約
    pub fn gather_outputs(&self, outputs: Vec<Variable<T>>) -> RusTorchResult<Variable<T>> {
        if outputs.is_empty() {
            return Err(RusTorchError::ProcessGroupError("No outputs to gather"));
        }

        // Calculate total output size
        // 総出力サイズを計算
        let total_batch_size: usize = outputs
            .iter()
            .map(|o| o.data().read().unwrap().shape()[0])
            .sum();
        let mut output_shape = outputs[0].data().read().unwrap().shape().to_vec();
        output_shape[0] = total_batch_size;

        // Create concatenated output
        // 連結された出力を作成
        let output_tensor = Tensor::zeros(&output_shape);
        let output_var = Variable::new(output_tensor, outputs[0].requires_grad());

        Ok(output_var)
    }

    /// Synchronize gradients across devices
    /// デバイス間で勾配を同期
    pub fn sync_gradients(&self) -> RusTorchResult<()> {
        match self.sync_strategy {
            GradientSyncStrategy::Synchronous => self.sync_gradients_sync(),
            GradientSyncStrategy::Asynchronous => self.sync_gradients_async(),
            GradientSyncStrategy::LocalSGD { sync_frequency: _ } => self.sync_gradients_local_sgd(),
        }
    }

    fn sync_gradients_sync(&self) -> RusTorchResult<()> {
        // Synchronous gradient synchronization
        // 同期勾配同期
        let state = get_distributed_state();
        let state_guard = state.lock().unwrap();

        if let Some(_pg) = &state_guard.process_group {
            // All-reduce gradients across all processes
            // 全プロセス間で勾配をall-reduce
            // Implementation would use the actual communication backend
            // 実装では実際の通信バックエンドを使用
            drop(state_guard);
            Ok(())
        } else {
            Err(RusTorchError::ProcessGroupError(
                "Process group not initialized",
            ))
        }
    }

    fn sync_gradients_async(&self) -> RusTorchResult<()> {
        // Asynchronous gradient synchronization
        // 非同期勾配同期
        Ok(())
    }

    fn sync_gradients_local_sgd(&self) -> RusTorchResult<()> {
        // Local SGD gradient synchronization
        // ローカルSGD勾配同期
        Ok(())
    }
}

impl<T, M> Module<T> for DataParallel<T, M>
where
    T: Float
        + Send
        + Sync
        + 'static
        + std::fmt::Debug
        + ndarray::ScalarOperand
        + num_traits::FromPrimitive,
    M: Module<T> + Send + Sync + 'static,
{
    fn forward(&self, input: &Variable<T>) -> Variable<T> {
        // Replicate input across devices
        // デバイス間で入力を複製
        let replicated_inputs = self
            .replicate_input(input)
            .unwrap_or_else(|_| vec![input.clone()]);

        // Forward pass on each device
        // 各デバイスでフォワードパス
        let outputs: Vec<Variable<T>> = replicated_inputs
            .iter()
            .map(|input| self.module.forward(input))
            .collect();

        // Gather outputs to output device
        // 出力デバイスに出力を収集
        self.gather_outputs(outputs)
            .unwrap_or_else(|_| input.clone())
    }

    fn parameters(&self) -> Vec<Variable<T>> {
        self.module.parameters()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// Distributed data loader for data parallel training
/// データ並列学習用分散データローダー
pub struct DistributedDataLoader<T: Float> {
    /// Training data
    /// 学習データ
    data: Vec<Tensor<T>>,
    /// Local labels
    /// ローカルラベル
    labels: Vec<Tensor<T>>,
    /// Batch size per process
    /// プロセスあたりのバッチサイズ
    batch_size: usize,
    /// Current epoch
    /// 現在のエポック
    current_epoch: usize,
    /// Shuffle data between epochs
    /// エポック間でデータをシャッフル
    shuffle: bool,
    /// Random seed for reproducibility
    /// 再現性のためのランダムシード
    seed: Option<u64>,
}

impl<T: Float + Send + Sync + 'static> DistributedDataLoader<T> {
    /// Create a new distributed data loader
    /// 新しい分散データローダーを作成
    pub fn new(
        data: Vec<Tensor<T>>,
        labels: Vec<Tensor<T>>,
        batch_size: usize,
        shuffle: bool,
        seed: Option<u64>,
    ) -> Self {
        Self {
            data,
            labels,
            batch_size,
            current_epoch: 0,
            shuffle,
            seed,
        }
    }

    /// Set epoch for deterministic shuffling
    /// 決定論的シャッフル用のエポックを設定
    pub fn set_epoch(&mut self, epoch: usize) {
        self.current_epoch = epoch;
    }

    /// Get batch iterator
    /// バッチイテレータを取得
    pub fn iter(&self) -> DistributedDataIterator<'_, T> {
        DistributedDataIterator::new(
            &self.data,
            &self.labels,
            self.batch_size,
            self.current_epoch,
            self.shuffle,
            self.seed,
        )
    }

    /// Get number of batches per epoch
    /// エポックあたりのバッチ数を取得
    pub fn len(&self) -> usize {
        self.data.len().div_ceil(self.batch_size)
    }

    /// Returns true if the dataloader has no data
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}

/// Iterator for distributed data loading
/// 分散データローディング用イテレータ
pub struct DistributedDataIterator<'a, T: Float> {
    data: &'a [Tensor<T>],
    labels: &'a [Tensor<T>],
    batch_size: usize,
    current_index: usize,
    indices: Vec<usize>,
}

impl<'a, T: Float> DistributedDataIterator<'a, T> {
    fn new(
        data: &'a [Tensor<T>],
        labels: &'a [Tensor<T>],
        batch_size: usize,
        epoch: usize,
        shuffle: bool,
        seed: Option<u64>,
    ) -> Self {
        let mut indices: Vec<usize> = (0..data.len()).collect();

        if shuffle {
            // Deterministic shuffling based on epoch and seed
            // エポックとシードに基づく決定論的シャッフル
            use rand::rngs::StdRng;
            use rand::{Rng, SeedableRng};

            let actual_seed = seed.unwrap_or(0) + epoch as u64;
            let mut rng = StdRng::seed_from_u64(actual_seed);

            for i in (1..indices.len()).rev() {
                let j = rng.gen_range(0..=i);
                indices.swap(i, j);
            }
        }

        Self {
            data,
            labels,
            batch_size,
            current_index: 0,
            indices,
        }
    }
}

impl<'a, T: Float> Iterator for DistributedDataIterator<'a, T> {
    type Item = (Vec<&'a Tensor<T>>, Vec<&'a Tensor<T>>);

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_index >= self.indices.len() {
            return None;
        }

        let end_index = (self.current_index + self.batch_size).min(self.indices.len());
        let batch_indices = &self.indices[self.current_index..end_index];

        let batch_data: Vec<&Tensor<T>> = batch_indices.iter().map(|&i| &self.data[i]).collect();

        let batch_labels: Vec<&Tensor<T>> =
            batch_indices.iter().map(|&i| &self.labels[i]).collect();

        self.current_index = end_index;

        Some((batch_data, batch_labels))
    }
}

/// Distributed sampler for ensuring each process gets different data
/// 各プロセスが異なるデータを取得することを保証する分散サンプラー
pub struct DistributedSampler {
    /// Total number of samples
    /// サンプル総数
    num_samples: usize,
    /// Number of processes
    /// プロセス数
    num_replicas: usize,
    /// Current process rank
    /// 現在のプロセスランク
    rank: usize,
    /// Current epoch
    /// 現在のエポック
    epoch: usize,
    /// Whether to drop last incomplete batch
    /// 最後の不完全なバッチを削除するかどうか
    drop_last: bool,
    /// Random seed
    /// ランダムシード
    seed: u64,
}

impl DistributedSampler {
    /// Create a new distributed sampler
    /// 新しい分散サンプラーを作成
    pub fn new(
        num_samples: usize,
        num_replicas: Option<usize>,
        rank: Option<usize>,
        drop_last: bool,
        seed: u64,
    ) -> RusTorchResult<Self> {
        let state = get_distributed_state();
        let state_guard = state.lock().unwrap();

        let num_replicas = num_replicas
            .or_else(|| state_guard.world_size())
            .unwrap_or(1);
        let rank = rank.or_else(|| state_guard.rank()).unwrap_or(0);

        if rank >= num_replicas {
            return Err(RusTorchError::ProcessGroupError(format!(
                "Rank {} is greater than or equal to num_replicas {}",
                rank, num_replicas
            )));
        }

        Ok(Self {
            num_samples,
            num_replicas,
            rank,
            epoch: 0,
            drop_last,
            seed,
        })
    }

    /// Set epoch for deterministic sampling
    /// 決定論的サンプリング用のエポックを設定
    pub fn set_epoch(&mut self, epoch: usize) {
        self.epoch = epoch;
    }

    /// Generate sample indices for current process
    /// 現在のプロセス用のサンプルインデックスを生成
    pub fn sample_indices(&self) -> Vec<usize> {
        use rand::rngs::StdRng;
        use rand::{Rng, SeedableRng};

        let mut rng = StdRng::seed_from_u64(self.seed + self.epoch as u64);
        let mut indices: Vec<usize> = (0..self.num_samples).collect();

        // Shuffle indices
        // インデックスをシャッフル
        for i in (1..indices.len()).rev() {
            let j = rng.gen_range(0..=i);
            indices.swap(i, j);
        }

        // Pad indices to make it evenly divisible by num_replicas
        // num_replicasで均等に分割できるようにインデックスをパディング
        let total_size = if self.drop_last {
            (self.num_samples / self.num_replicas) * self.num_replicas
        } else {
            self.num_samples.div_ceil(self.num_replicas) * self.num_replicas
        };

        while indices.len() < total_size {
            let remaining = total_size - indices.len();
            let copy_len = std::cmp::min(indices.len(), remaining);
            let to_copy = indices[..copy_len].to_vec();
            indices.extend_from_slice(&to_copy);
        }
        indices.truncate(total_size);

        // Subsample for current rank
        // 現在のランク用にサブサンプル
        let num_samples_per_replica = total_size / self.num_replicas;
        let start = self.rank * num_samples_per_replica;
        let end = start + num_samples_per_replica;

        indices[start..end].to_vec()
    }

    /// Get number of samples for current process
    /// 現在のプロセスのサンプル数を取得
    pub fn len(&self) -> usize {
        if self.drop_last {
            self.num_samples / self.num_replicas
        } else {
            self.num_samples.div_ceil(self.num_replicas)
        }
    }

    /// Returns true if there are no samples
    pub fn is_empty(&self) -> bool {
        self.num_samples == 0
    }
}

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

    #[test]
    fn test_gradient_sync_strategy() {
        let strategies = [
            GradientSyncStrategy::Synchronous,
            GradientSyncStrategy::Asynchronous,
            GradientSyncStrategy::LocalSGD { sync_frequency: 10 },
        ];

        for strategy in &strategies {
            match strategy {
                GradientSyncStrategy::Synchronous => {} // Synchronous strategy valid
                GradientSyncStrategy::Asynchronous => {} // Asynchronous strategy valid
                GradientSyncStrategy::LocalSGD { sync_frequency } => assert!(*sync_frequency > 0),
            }
        }
    }

    #[test]
    fn test_distributed_data_loader() {
        let data = vec![
            Tensor::<f32>::ones(&[10, 5]),
            Tensor::<f32>::ones(&[10, 5]),
            Tensor::<f32>::ones(&[10, 5]),
        ];
        let labels = vec![
            Tensor::<f32>::zeros(&[10, 1]),
            Tensor::<f32>::zeros(&[10, 1]),
            Tensor::<f32>::zeros(&[10, 1]),
        ];

        let loader = DistributedDataLoader::new(data, labels, 2, true, Some(42).into());
        assert_eq!(loader.len(), 2); // ceil(3/2) = 2 batches

        let mut iter = loader.iter();
        let batch = iter.next();
        assert!(batch.is_some());

        let (batch_data, batch_labels) = batch.unwrap();
        assert_eq!(batch_data.len(), 2);
        assert_eq!(batch_labels.len(), 2);
    }

    #[test]
    fn test_distributed_sampler() {
        let sampler = DistributedSampler::new(100, Some(4), Some(0), false, 42);
        assert!(sampler.is_ok());

        let sampler = sampler.unwrap();
        let indices = sampler.sample_indices();
        assert_eq!(indices.len(), 25); // 100 / 4 = 25 samples per replica

        // Check that indices are within valid range
        // インデックスが有効な範囲内にあることを確認
        for &idx in &indices {
            assert!(idx < 100);
        }
    }

    #[test]
    fn test_data_parallel_creation() {
        let linear = Linear::<f32>::new(10, 5);
        let devices = vec![DeviceType::Cpu, DeviceType::Cpu]; // Use CPU for testing

        let dp = DataParallel::new(linear, devices);
        assert_eq!(dp.num_devices(), 2);
    }
}