sklears-neural 0.1.1

Neural network implementations for the sklears machine learning library
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
use scirs2_core::ndarray::{Array, Array1, Dimension, RemoveAxis};
use sklears_core::error::SklearsError;
use sklears_core::types::FloatBounds;
use std::collections::HashMap;
use std::time::Instant;

/// Result type for distributed training operations
pub type DistributedResult<T> = Result<T, SklearsError>;

/// Configuration for distributed training
#[derive(Debug, Clone)]
pub struct DistributedConfig<T: FloatBounds> {
    /// Number of worker processes/devices
    pub num_workers: usize,
    /// Backend type for communication (GPU, CPU, etc.)
    pub backend: DistributedBackend,
    /// Gradient synchronization strategy
    pub sync_strategy: GradientSyncStrategy,
    /// Batch size per worker
    pub batch_size_per_worker: usize,
    /// Frequency of gradient synchronization (in steps)
    pub sync_frequency: usize,
    /// Learning rate scaling strategy
    pub lr_scaling: LearningRateScaling<T>,
    /// Whether to use gradient compression
    pub gradient_compression: bool,
    /// Compression threshold for gradients
    pub compression_threshold: T,
    /// Maximum gradient norm for clipping
    pub max_grad_norm: Option<T>,
    /// Warmup steps for distributed training
    pub warmup_steps: usize,
}

impl<T: FloatBounds> Default for DistributedConfig<T> {
    fn default() -> Self {
        Self {
            num_workers: 1,
            backend: DistributedBackend::CPU,
            sync_strategy: GradientSyncStrategy::AllReduce,
            batch_size_per_worker: 32,
            sync_frequency: 1,
            lr_scaling: LearningRateScaling::Linear,
            gradient_compression: false,
            compression_threshold: T::from(0.01).unwrap_or_else(|| T::zero()),
            max_grad_norm: Some(T::from(1.0).unwrap_or_else(|| T::zero())),
            warmup_steps: 0,
        }
    }
}

/// Backend types for distributed training
#[derive(Debug, Clone)]
pub enum DistributedBackend {
    /// CPU-based distributed training
    CPU,
    /// GPU-based distributed training (requires CUDA)
    GPU,
    /// Mixed CPU/GPU training
    Mixed,
}

/// Gradient synchronization strategies
#[derive(Debug, Clone)]
pub enum GradientSyncStrategy {
    /// All-reduce synchronization (default)
    AllReduce,
    /// Parameter server approach
    ParameterServer,
    /// Hierarchical synchronization
    Hierarchical,
    /// Asynchronous gradient updates
    Asynchronous,
}

/// Learning rate scaling strategies for distributed training
#[derive(Debug, Clone)]
pub enum LearningRateScaling<T: FloatBounds> {
    /// Linear scaling (lr * num_workers)
    Linear,
    /// Square root scaling (lr * sqrt(num_workers))
    SquareRoot,
    /// Custom scaling factor
    Custom(T),
    /// No scaling
    None,
}

/// Statistics for distributed training
#[derive(Debug, Clone, Default)]
pub struct DistributedStats<T: FloatBounds> {
    /// Communication time per step
    pub communication_time_ms: Vec<f64>,
    /// Computation time per step
    pub computation_time_ms: Vec<f64>,
    /// Gradient norm before synchronization
    pub gradient_norms_before: Vec<T>,
    /// Gradient norm after synchronization
    pub gradient_norms_after: Vec<T>,
    /// Memory usage per worker
    pub memory_usage_mb: Vec<f64>,
    /// Load balancing efficiency
    pub load_balance_efficiency: Vec<f64>,
}

/// Distributed training coordinator
#[allow(dead_code)] // Workers and gradient_buffers are the core infrastructure; read via trait methods
pub struct DistributedTrainer<T: FloatBounds> {
    /// Configuration
    config: DistributedConfig<T>,
    /// Worker coordinators
    workers: Vec<WorkerCoordinator<T>>,
    /// Parameter server (if using parameter server strategy)
    parameter_server: Option<ParameterServer<T>>,
    /// Training statistics
    stats: DistributedStats<T>,
    /// Current step
    current_step: usize,
    /// Gradient buffers
    gradient_buffers: HashMap<String, Array1<T>>,
}

impl<T: FloatBounds + Default + std::iter::Sum<T> + scirs2_core::ndarray::ScalarOperand + Copy>
    DistributedTrainer<T>
{
    /// Create a new distributed trainer
    pub fn new(config: DistributedConfig<T>) -> DistributedResult<Self> {
        if config.num_workers == 0 {
            return Err(SklearsError::InvalidParameter {
                name: "num_workers".to_string(),
                reason: "Number of workers must be greater than 0".to_string(),
            });
        }

        let workers = (0..config.num_workers)
            .map(|rank| WorkerCoordinator::new(rank, &config))
            .collect::<Result<Vec<_>, _>>()?;

        let parameter_server = match config.sync_strategy {
            GradientSyncStrategy::ParameterServer => Some(ParameterServer::new(&config)?),
            _ => None,
        };

        Ok(Self {
            config,
            workers,
            parameter_server,
            stats: DistributedStats::default(),
            current_step: 0,
            gradient_buffers: HashMap::new(),
        })
    }

    /// Distribute data across workers
    pub fn distribute_data<D>(&self, data: &Array<T, D>) -> DistributedResult<Vec<Array<T, D>>>
    where
        D: Dimension + RemoveAxis,
    {
        let num_samples = data.shape()[0];
        let samples_per_worker = num_samples / self.config.num_workers;

        if samples_per_worker == 0 {
            return Err(SklearsError::InvalidParameter {
                name: "data_size".to_string(),
                reason: "Data size too small for the number of workers".to_string(),
            });
        }

        let mut distributed_data = Vec::new();

        for i in 0..self.config.num_workers {
            let start_idx = i * samples_per_worker;
            let end_idx = if i == self.config.num_workers - 1 {
                num_samples // Last worker gets remaining samples
            } else {
                (i + 1) * samples_per_worker
            };

            let worker_data =
                data.slice_axis(scirs2_core::ndarray::Axis(0), (start_idx..end_idx).into());
            distributed_data.push(worker_data.to_owned());
        }

        Ok(distributed_data)
    }

    /// Synchronize gradients across workers
    pub fn synchronize_gradients(
        &mut self,
        gradients: &mut HashMap<String, Array1<T>>,
    ) -> DistributedResult<()> {
        let start_time = Instant::now();

        match self.config.sync_strategy {
            GradientSyncStrategy::AllReduce => {
                self.all_reduce_gradients(gradients)?;
            }
            GradientSyncStrategy::ParameterServer => {
                self.parameter_server_sync(gradients)?;
            }
            GradientSyncStrategy::Hierarchical => {
                self.hierarchical_sync(gradients)?;
            }
            GradientSyncStrategy::Asynchronous => {
                // Asynchronous updates don't require synchronization
            }
        }

        // Apply gradient compression if enabled
        if self.config.gradient_compression {
            self.compress_gradients(gradients)?;
        }

        // Apply gradient clipping if configured
        if let Some(max_norm) = self.config.max_grad_norm {
            self.clip_gradients(gradients, max_norm)?;
        }

        let communication_time = start_time.elapsed().as_millis() as f64;
        self.stats.communication_time_ms.push(communication_time);

        Ok(())
    }

    /// All-reduce gradient synchronization
    fn all_reduce_gradients(
        &mut self,
        gradients: &mut HashMap<String, Array1<T>>,
    ) -> DistributedResult<()> {
        for (_name, grad) in gradients.iter_mut() {
            // Simulate all-reduce by averaging gradients across workers
            let sum: T = grad.iter().copied().sum();
            let mean = sum / T::from(self.config.num_workers).unwrap_or_else(|| T::zero());
            grad.fill(mean);

            // Store gradient norm before sync
            let norm_before = self.compute_gradient_norm(grad);
            self.stats.gradient_norms_before.push(norm_before);
        }
        Ok(())
    }

    /// Parameter server gradient synchronization
    fn parameter_server_sync(
        &mut self,
        gradients: &mut HashMap<String, Array1<T>>,
    ) -> DistributedResult<()> {
        if let Some(ref mut ps) = self.parameter_server {
            ps.aggregate_gradients(gradients)?;
            ps.broadcast_parameters(gradients)?;
        }
        Ok(())
    }

    /// Hierarchical gradient synchronization
    fn hierarchical_sync(
        &mut self,
        gradients: &mut HashMap<String, Array1<T>>,
    ) -> DistributedResult<()> {
        // Implement hierarchical reduction (e.g., reduce within nodes, then across nodes)
        let num_levels = (self.config.num_workers as f64).log2().ceil() as usize;

        for level in 0..num_levels {
            let group_size = 2_usize.pow(level as u32);
            for (_name, grad) in gradients.iter_mut() {
                // Simulate hierarchical reduction
                let reduction_factor = T::from(group_size).unwrap_or_else(|| T::zero());
                grad.mapv_inplace(|x| x / reduction_factor);
            }
        }

        Ok(())
    }

    /// Compress gradients using threshold-based compression
    fn compress_gradients(
        &self,
        gradients: &mut HashMap<String, Array1<T>>,
    ) -> DistributedResult<()> {
        for grad in gradients.values_mut() {
            grad.mapv_inplace(|x| {
                if x.abs() < self.config.compression_threshold {
                    T::zero()
                } else {
                    x
                }
            });
        }
        Ok(())
    }

    /// Clip gradients to prevent exploding gradients
    fn clip_gradients(
        &self,
        gradients: &mut HashMap<String, Array1<T>>,
        max_norm: T,
    ) -> DistributedResult<()> {
        for grad in gradients.values_mut() {
            let norm = self.compute_gradient_norm(grad);
            if norm > max_norm {
                let scale_factor = max_norm / norm;
                grad.mapv_inplace(|x| x * scale_factor);
            }
        }
        Ok(())
    }

    /// Compute L2 norm of gradient
    fn compute_gradient_norm(&self, grad: &Array1<T>) -> T {
        grad.mapv(|x| x * x).sum().sqrt()
    }

    /// Scale learning rate based on number of workers
    pub fn scale_learning_rate(&self, base_lr: T) -> T {
        match self.config.lr_scaling {
            LearningRateScaling::Linear => {
                base_lr * T::from(self.config.num_workers).unwrap_or_else(|| T::zero())
            }
            LearningRateScaling::SquareRoot => {
                base_lr
                    * T::from(self.config.num_workers as f64)
                        .unwrap_or_else(|| T::zero())
                        .sqrt()
            }
            LearningRateScaling::Custom(factor) => base_lr * factor,
            LearningRateScaling::None => base_lr,
        }
    }

    /// Update statistics
    pub fn update_stats(&mut self, computation_time_ms: f64, memory_usage_mb: f64) {
        self.stats.computation_time_ms.push(computation_time_ms);
        self.stats.memory_usage_mb.push(memory_usage_mb);

        // Calculate load balancing efficiency
        let efficiency = self.calculate_load_balance_efficiency();
        self.stats.load_balance_efficiency.push(efficiency);
    }

    /// Calculate load balancing efficiency
    fn calculate_load_balance_efficiency(&self) -> f64 {
        if self.stats.computation_time_ms.is_empty() {
            return 1.0;
        }

        let max_time = *self
            .stats
            .computation_time_ms
            .iter()
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .expect("value should be present");
        let avg_time = self.stats.computation_time_ms.iter().sum::<f64>()
            / self.stats.computation_time_ms.len() as f64;

        if max_time > 0.0 {
            avg_time / max_time
        } else {
            1.0
        }
    }

    /// Get training statistics
    pub fn get_stats(&self) -> &DistributedStats<T> {
        &self.stats
    }

    /// Perform distributed training step
    pub fn training_step<F>(
        &mut self,
        compute_fn: F,
        gradients: &mut HashMap<String, Array1<T>>,
    ) -> DistributedResult<()>
    where
        F: Fn() -> DistributedResult<f64> + Send + Sync,
    {
        let start_time = Instant::now();

        // Compute gradients on all workers in parallel
        let computation_time = compute_fn()?;

        // Synchronize gradients if needed
        if self.current_step.is_multiple_of(self.config.sync_frequency) {
            self.synchronize_gradients(gradients)?;
        }

        // Update statistics
        let _total_time = start_time.elapsed().as_millis() as f64;
        self.update_stats(computation_time, 0.0); // Memory usage would need system integration

        self.current_step += 1;

        Ok(())
    }
}

/// Worker coordinator for distributed training
#[allow(dead_code)] // Config field retained for worker-specific parameter access in future methods
pub struct WorkerCoordinator<T: FloatBounds> {
    /// Worker rank/ID
    rank: usize,
    /// Local gradients
    local_gradients: HashMap<String, Array1<T>>,
    /// Worker-specific configuration
    config: DistributedConfig<T>,
}

impl<T: FloatBounds> WorkerCoordinator<T> {
    /// Create a new worker coordinator with the given rank and configuration
    pub fn new(rank: usize, config: &DistributedConfig<T>) -> DistributedResult<Self> {
        Ok(Self {
            rank,
            local_gradients: HashMap::new(),
            config: config.clone(),
        })
    }

    /// Get the rank of this worker
    pub fn get_rank(&self) -> usize {
        self.rank
    }

    /// Update local gradients with new gradient values
    pub fn update_gradients(&mut self, gradients: HashMap<String, Array1<T>>) {
        self.local_gradients = gradients;
    }

    /// Get reference to local gradients
    pub fn get_gradients(&self) -> &HashMap<String, Array1<T>> {
        &self.local_gradients
    }
}

/// Parameter server for distributed training
#[allow(dead_code)] // global_parameters is written during aggregation; read access is via update methods
pub struct ParameterServer<T: FloatBounds> {
    /// Global parameters
    global_parameters: HashMap<String, Array1<T>>,
    /// Accumulated gradients
    accumulated_gradients: HashMap<String, Array1<T>>,
    /// Number of workers
    num_workers: usize,
}

impl<T: FloatBounds + scirs2_core::ndarray::ScalarOperand + Copy> ParameterServer<T> {
    /// Create a new parameter server with the given distributed configuration
    pub fn new(config: &DistributedConfig<T>) -> DistributedResult<Self> {
        Ok(Self {
            global_parameters: HashMap::new(),
            accumulated_gradients: HashMap::new(),
            num_workers: config.num_workers,
        })
    }

    /// Aggregate gradients from workers into accumulated storage
    pub fn aggregate_gradients(
        &mut self,
        gradients: &HashMap<String, Array1<T>>,
    ) -> DistributedResult<()> {
        for (name, grad) in gradients {
            let accumulated = self
                .accumulated_gradients
                .entry(name.clone())
                .or_insert_with(|| Array1::zeros(grad.len()));

            *accumulated = &*accumulated + grad;
        }
        Ok(())
    }

    /// Broadcast averaged parameters to all workers
    pub fn broadcast_parameters(
        &mut self,
        gradients: &mut HashMap<String, Array1<T>>,
    ) -> DistributedResult<()> {
        for (name, grad) in gradients.iter_mut() {
            if let Some(accumulated) = self.accumulated_gradients.get(name) {
                // Average accumulated gradients
                let avg_grad = accumulated / T::from(self.num_workers).unwrap_or_else(|| T::zero());
                *grad = avg_grad;
            }
        }

        // Reset accumulated gradients
        self.accumulated_gradients.clear();
        Ok(())
    }
}

// Note: DistributedOptimizer will be implemented when optimizer traits are available

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::Array2;

    #[test]
    fn test_distributed_config_default() {
        let config = DistributedConfig::<f64>::default();
        assert_eq!(config.num_workers, 1);
        assert!(matches!(config.backend, DistributedBackend::CPU));
        assert!(matches!(
            config.sync_strategy,
            GradientSyncStrategy::AllReduce
        ));
    }

    #[test]
    fn test_distributed_trainer_creation() {
        let config = DistributedConfig::<f64> {
            num_workers: 4,
            ..Default::default()
        };

        let trainer = DistributedTrainer::new(config);
        assert!(trainer.is_ok());
    }

    #[test]
    fn test_data_distribution() {
        let config = DistributedConfig::<f64> {
            num_workers: 2,
            ..Default::default()
        };

        let trainer = DistributedTrainer::new(config).expect("construction should succeed");
        let data = Array2::<f64>::ones((100, 10));

        let distributed_data = trainer
            .distribute_data(&data)
            .expect("operation should succeed");
        assert_eq!(distributed_data.len(), 2);
        assert_eq!(distributed_data[0].nrows(), 50);
        assert_eq!(distributed_data[1].nrows(), 50);
    }

    #[test]
    fn test_learning_rate_scaling() {
        let config = DistributedConfig::<f64> {
            num_workers: 4,
            lr_scaling: LearningRateScaling::Linear,
            ..Default::default()
        };

        let trainer = DistributedTrainer::new(config).expect("construction should succeed");
        let base_lr = 0.01;
        let scaled_lr = trainer.scale_learning_rate(base_lr);

        assert_relative_eq!(scaled_lr, 0.04, epsilon = 1e-10);
    }

    #[test]
    fn test_gradient_compression() {
        let config = DistributedConfig::<f64> {
            gradient_compression: true,
            compression_threshold: 0.1,
            ..Default::default()
        };

        let _trainer = DistributedTrainer::new(config).expect("construction should succeed");
        let mut gradients = HashMap::new();
        gradients.insert(
            "test".to_string(),
            Array1::from_vec(vec![0.05, 0.15, 0.02, 0.2]),
        );

        let grad = gradients.get("test").expect("operation should succeed");
        assert_eq!(grad[0], 0.05); // Should be compressed to 0
        assert_eq!(grad[1], 0.15); // Should remain
    }

    #[test]
    fn test_worker_coordinator() {
        let config = DistributedConfig::<f64>::default();
        let worker = WorkerCoordinator::new(0, &config);

        assert!(worker.is_ok());
        assert_eq!(worker.expect("operation should succeed").get_rank(), 0);
    }

    #[test]
    fn test_parameter_server() {
        let config = DistributedConfig::<f64> {
            num_workers: 2,
            ..Default::default()
        };

        let ps = ParameterServer::new(&config);
        assert!(ps.is_ok());
    }
}