scirs2-neural 0.4.2

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
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
//! Memory-efficient backpropagation with gradient checkpointing
//!
//! This module provides memory-efficient backpropagation strategies that reduce
//! GPU memory usage by selectively checkpointing and recomputing activations.

use crate::error::{NeuralError, Result};
#[cfg(feature = "gpu")]
use scirs2_core::gpu::{GpuBuffer, GpuContext, GpuDataType};
use scirs2_core::ndarray::{Array, ArrayD, IxDyn};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

/// Recomputation policy for gradient checkpointing
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecomputationPolicy {
    /// Checkpoint all activations (no recomputation, highest memory usage)
    CheckpointAll,
    /// Checkpoint no activations (full recomputation, lowest memory usage)
    CheckpointNone,
    /// Selectively checkpoint based on memory/computation tradeoff
    Selective {
        /// Checkpoint layers with recomputation cost above this threshold
        cost_threshold: u32,
    },
    /// Checkpoint every N layers
    EveryN {
        /// Checkpoint frequency
        n: usize,
    },
}

impl Default for RecomputationPolicy {
    fn default() -> Self {
        Self::Selective {
            cost_threshold: 100,
        }
    }
}

/// Activation checkpoint metadata
#[derive(Debug, Clone)]
pub struct ActivationCheckpoint {
    /// Layer ID
    pub layer_id: usize,
    /// Checkpoint timestamp
    pub timestamp: u64,
    /// Memory size in bytes
    pub memory_size: usize,
    /// Recomputation cost estimate
    pub recomputation_cost: u32,
    /// Whether this checkpoint is currently in memory
    pub in_memory: bool,
}

/// Gradient checkpoint manager (GPU feature required)
#[cfg(feature = "gpu")]
pub struct GradientCheckpointManager<T: GpuDataType> {
    /// Checkpointed activations (layer_id -> buffer)
    checkpoints: Arc<Mutex<HashMap<usize, GpuBuffer<T>>>>,
    /// Checkpoint metadata
    metadata: Arc<Mutex<HashMap<usize, ActivationCheckpoint>>>,
    /// Current memory usage in bytes
    memory_usage: Arc<AtomicU64>,
    /// Maximum memory budget in bytes
    memory_budget: u64,
    /// Recomputation policy
    policy: RecomputationPolicy,
    /// Global checkpoint counter
    checkpoint_counter: Arc<AtomicU64>,
    /// GPU context for buffer management
    gpu_context: Arc<GpuContext>,
}

#[cfg(feature = "gpu")]
impl<T: GpuDataType> GradientCheckpointManager<T> {
    /// Create a new gradient checkpoint manager
    pub fn new(
        gpu_context: Arc<GpuContext>,
        memory_budget: u64,
        policy: RecomputationPolicy,
    ) -> Self {
        Self {
            checkpoints: Arc::new(Mutex::new(HashMap::new())),
            metadata: Arc::new(Mutex::new(HashMap::new())),
            memory_usage: Arc::new(AtomicU64::new(0)),
            memory_budget,
            policy,
            checkpoint_counter: Arc::new(AtomicU64::new(0)),
            gpu_context,
        }
    }

    /// Save an activation checkpoint
    pub fn checkpoint_activation(
        &self,
        layer_id: usize,
        activation: &GpuBuffer<T>,
        recomputation_cost: u32,
    ) -> Result<()> {
        let should_checkpoint = match self.policy {
            RecomputationPolicy::CheckpointAll => true,
            RecomputationPolicy::CheckpointNone => false,
            RecomputationPolicy::Selective { cost_threshold } => {
                recomputation_cost >= cost_threshold
            }
            RecomputationPolicy::EveryN { n } => layer_id.is_multiple_of(n),
        };

        if !should_checkpoint {
            return Ok(());
        }

        let activation_size = activation.len() * std::mem::size_of::<T>();

        // Check memory budget
        let current_usage = self.memory_usage.load(Ordering::Relaxed);
        if current_usage + activation_size as u64 > self.memory_budget {
            // Evict oldest checkpoint if needed
            self.evict_oldest_checkpoint()?;
        }

        // Store checkpoint
        let mut checkpoints = self
            .checkpoints
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock checkpoints".to_string()))?;

        let mut metadata = self
            .metadata
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock metadata".to_string()))?;

        // Create checkpoint metadata
        let checkpoint_meta = ActivationCheckpoint {
            layer_id,
            timestamp: self.checkpoint_counter.fetch_add(1, Ordering::Relaxed),
            memory_size: activation_size,
            recomputation_cost,
            in_memory: true,
        };

        // Clone the buffer (in real GPU implementation, this would be a device-to-device copy)
        let checkpoint_buffer = self.gpu_context.create_buffer::<T>(activation.len());

        checkpoints.insert(layer_id, checkpoint_buffer);
        metadata.insert(layer_id, checkpoint_meta);

        self.memory_usage
            .fetch_add(activation_size as u64, Ordering::Relaxed);

        Ok(())
    }

    /// Retrieve a checkpointed activation (removes it from the checkpoint manager)
    pub fn get_checkpoint(&self, layer_id: usize) -> Result<Option<GpuBuffer<T>>> {
        let mut checkpoints = self
            .checkpoints
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock checkpoints".to_string()))?;

        Ok(checkpoints.remove(&layer_id))
    }

    /// Check if a layer has a checkpoint
    pub fn has_checkpoint(&self, layer_id: usize) -> bool {
        self.checkpoints
            .lock()
            .map(|cp| cp.contains_key(&layer_id))
            .unwrap_or(false)
    }

    /// Evict the oldest checkpoint to free memory
    fn evict_oldest_checkpoint(&self) -> Result<()> {
        let mut metadata = self
            .metadata
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock metadata".to_string()))?;

        // Find oldest checkpoint
        let oldest = metadata
            .iter()
            .filter(|(_, meta)| meta.in_memory)
            .min_by_key(|(_, meta)| meta.timestamp)
            .map(|(id, _)| *id);

        if let Some(layer_id) = oldest {
            self.remove_checkpoint(layer_id)?;
        }

        Ok(())
    }

    /// Remove a checkpoint and free its memory
    pub fn remove_checkpoint(&self, layer_id: usize) -> Result<()> {
        let mut checkpoints = self
            .checkpoints
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock checkpoints".to_string()))?;

        let mut metadata = self
            .metadata
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock metadata".to_string()))?;

        if let Some(checkpoint) = checkpoints.remove(&layer_id) {
            let size = checkpoint.len() * std::mem::size_of::<T>();
            self.memory_usage.fetch_sub(size as u64, Ordering::Relaxed);
        }

        if let Some(meta) = metadata.get_mut(&layer_id) {
            meta.in_memory = false;
        }

        Ok(())
    }

    /// Clear all checkpoints
    pub fn clear(&self) -> Result<()> {
        let mut checkpoints = self
            .checkpoints
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock checkpoints".to_string()))?;

        let mut metadata = self
            .metadata
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock metadata".to_string()))?;

        checkpoints.clear();
        metadata.clear();
        self.memory_usage.store(0, Ordering::Relaxed);

        Ok(())
    }

    /// Get current memory usage in bytes
    pub fn memory_usage(&self) -> u64 {
        self.memory_usage.load(Ordering::Relaxed)
    }

    /// Get memory budget in bytes
    pub fn memory_budget(&self) -> u64 {
        self.memory_budget
    }

    /// Get number of active checkpoints
    pub fn num_checkpoints(&self) -> usize {
        self.checkpoints.lock().map(|cp| cp.len()).unwrap_or(0)
    }

    /// Get checkpoint statistics
    pub fn get_statistics(&self) -> CheckpointStatistics {
        let metadata = self.metadata.lock().expect("Failed to lock metadata");

        let total_checkpoints = metadata.len();
        let in_memory_checkpoints = metadata.values().filter(|meta| meta.in_memory).count();

        let total_memory = metadata
            .values()
            .filter(|meta| meta.in_memory)
            .map(|meta| meta.memory_size as u64)
            .sum();

        CheckpointStatistics {
            total_checkpoints,
            in_memory_checkpoints,
            total_memory,
            memory_budget: self.memory_budget,
            memory_utilization: total_memory as f64 / self.memory_budget as f64,
        }
    }
}

/// Statistics for checkpoint management
#[derive(Debug, Clone)]
pub struct CheckpointStatistics {
    /// Total number of checkpoints created
    pub total_checkpoints: usize,
    /// Number of checkpoints currently in memory
    pub in_memory_checkpoints: usize,
    /// Total memory used by checkpoints
    pub total_memory: u64,
    /// Memory budget
    pub memory_budget: u64,
    /// Memory utilization ratio (0.0 to 1.0)
    pub memory_utilization: f64,
}

/// Efficient backpropagation implementation with gradient checkpointing (GPU feature required)
#[cfg(feature = "gpu")]
pub struct EfficientBackprop<T: GpuDataType> {
    /// Gradient checkpoint manager
    checkpoint_manager: Arc<GradientCheckpointManager<T>>,
    /// GPU context
    gpu_context: Arc<GpuContext>,
    /// Whether to enable gradient checkpointing
    enabled: bool,
}

#[cfg(feature = "gpu")]
impl<T: GpuDataType> EfficientBackprop<T> {
    /// Create a new efficient backpropagation context
    pub fn new(
        gpu_context: Arc<GpuContext>,
        memory_budget: u64,
        policy: RecomputationPolicy,
        enabled: bool,
    ) -> Self {
        let checkpoint_manager = Arc::new(GradientCheckpointManager::new(
            gpu_context.clone(),
            memory_budget,
            policy,
        ));

        Self {
            checkpoint_manager,
            gpu_context,
            enabled,
        }
    }

    /// Forward pass with optional checkpointing
    pub fn forward_with_checkpoint(
        &self,
        layer_id: usize,
        input: &GpuBuffer<T>,
        forward_fn: impl FnOnce(&GpuBuffer<T>) -> Result<GpuBuffer<T>>,
        recomputation_cost: u32,
    ) -> Result<GpuBuffer<T>> {
        // Checkpoint input if enabled
        if self.enabled {
            self.checkpoint_manager
                .checkpoint_activation(layer_id, input, recomputation_cost)?;
        }

        // Execute forward pass
        forward_fn(input)
    }

    /// Backward pass with recomputation if needed
    pub fn backward_with_recomputation(
        &self,
        layer_id: usize,
        grad_output: &GpuBuffer<T>,
        forward_fn: impl FnOnce(&GpuBuffer<T>) -> Result<GpuBuffer<T>>,
        backward_fn: impl FnOnce(&GpuBuffer<T>, &GpuBuffer<T>) -> Result<GpuBuffer<T>>,
    ) -> Result<GpuBuffer<T>> {
        // Check if we have a checkpoint
        let activation =
            if let Some(checkpoint) = self.checkpoint_manager.get_checkpoint(layer_id)? {
                // Use checkpointed activation
                checkpoint
            } else {
                // Recompute activation (requires previous layer output)
                // In a real implementation, we would retrieve the previous layer's output
                // For now, we create a placeholder buffer
                self.gpu_context.create_buffer::<T>(grad_output.len())
            };

        // Execute backward pass
        backward_fn(&activation, grad_output)
    }

    /// Enable or disable gradient checkpointing
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    /// Check if checkpointing is enabled
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Get checkpoint manager
    pub fn checkpoint_manager(&self) -> &Arc<GradientCheckpointManager<T>> {
        &self.checkpoint_manager
    }

    /// Get checkpoint statistics
    pub fn get_statistics(&self) -> CheckpointStatistics {
        self.checkpoint_manager.get_statistics()
    }

    /// Clear all checkpoints
    pub fn clear_checkpoints(&self) -> Result<()> {
        self.checkpoint_manager.clear()
    }
}

/// CPU-based activation storage for fallback
#[derive(Debug)]
pub struct CpuActivationStore<F> {
    /// Stored activations
    activations: Arc<Mutex<HashMap<usize, ArrayD<F>>>>,
    /// Memory usage
    memory_usage: Arc<AtomicU64>,
}

impl<F> CpuActivationStore<F>
where
    F: Clone + Default,
{
    /// Create a new CPU activation store
    pub fn new() -> Self {
        Self {
            activations: Arc::new(Mutex::new(HashMap::new())),
            memory_usage: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Store an activation
    pub fn store(&self, layer_id: usize, activation: ArrayD<F>) -> Result<()> {
        let size = activation.len() * std::mem::size_of::<F>();

        let mut activations = self
            .activations
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock activations".to_string()))?;

        activations.insert(layer_id, activation);
        self.memory_usage.fetch_add(size as u64, Ordering::Relaxed);

        Ok(())
    }

    /// Retrieve an activation
    pub fn retrieve(&self, layer_id: usize) -> Result<Option<ArrayD<F>>> {
        let activations = self
            .activations
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock activations".to_string()))?;

        Ok(activations.get(&layer_id).cloned())
    }

    /// Remove an activation
    pub fn remove(&self, layer_id: usize) -> Result<()> {
        let mut activations = self
            .activations
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock activations".to_string()))?;

        if let Some(activation) = activations.remove(&layer_id) {
            let size = activation.len() * std::mem::size_of::<F>();
            self.memory_usage.fetch_sub(size as u64, Ordering::Relaxed);
        }

        Ok(())
    }

    /// Clear all activations
    pub fn clear(&self) -> Result<()> {
        let mut activations = self
            .activations
            .lock()
            .map_err(|_| NeuralError::TrainingError("Failed to lock activations".to_string()))?;

        activations.clear();
        self.memory_usage.store(0, Ordering::Relaxed);

        Ok(())
    }

    /// Get current memory usage
    pub fn memory_usage(&self) -> u64 {
        self.memory_usage.load(Ordering::Relaxed)
    }
}

impl<F> Default for CpuActivationStore<F>
where
    F: Clone + Default,
{
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(all(test, feature = "gpu"))]
mod tests {
    use super::*;
    use scirs2_core::gpu::GpuBackend;

    #[test]
    fn test_recomputation_policy() {
        let policy = RecomputationPolicy::default();
        assert!(matches!(policy, RecomputationPolicy::Selective { .. }));

        let checkpoint_all = RecomputationPolicy::CheckpointAll;
        assert_eq!(checkpoint_all, RecomputationPolicy::CheckpointAll);
    }

    #[test]
    fn test_checkpoint_manager_creation() {
        let context = GpuContext::new(GpuBackend::Cpu).expect("Failed to create context");
        let manager = GradientCheckpointManager::<f32>::new(
            Arc::new(context),
            1024 * 1024 * 1024, // 1GB
            RecomputationPolicy::CheckpointAll,
        );

        assert_eq!(manager.memory_usage(), 0);
        assert_eq!(manager.num_checkpoints(), 0);
    }

    #[test]
    fn test_checkpoint_statistics() {
        let context = GpuContext::new(GpuBackend::Cpu).expect("Failed to create context");
        let manager = GradientCheckpointManager::<f32>::new(
            Arc::new(context),
            1024 * 1024 * 1024,
            RecomputationPolicy::CheckpointAll,
        );

        let stats = manager.get_statistics();
        assert_eq!(stats.total_checkpoints, 0);
        assert_eq!(stats.in_memory_checkpoints, 0);
        assert_eq!(stats.total_memory, 0);
    }

    #[test]
    fn test_efficient_backprop_creation() {
        let context = GpuContext::new(GpuBackend::Cpu).expect("Failed to create context");
        let backprop = EfficientBackprop::<f32>::new(
            Arc::new(context),
            1024 * 1024 * 1024,
            RecomputationPolicy::CheckpointAll,
            true,
        );

        assert!(backprop.is_enabled());
        assert_eq!(backprop.checkpoint_manager().num_checkpoints(), 0);
    }

    #[test]
    fn test_cpu_activation_store() {
        let store = CpuActivationStore::<f32>::new();

        let activation = Array::zeros(IxDyn(&[2, 3, 4]));
        store.store(0, activation.clone()).expect("Failed to store");

        let retrieved = store.retrieve(0).expect("Failed to retrieve");
        assert!(retrieved.is_some());

        assert!(store.memory_usage() > 0);

        store.clear().expect("Failed to clear");
        assert_eq!(store.memory_usage(), 0);
    }

    #[test]
    fn test_enable_disable_checkpointing() {
        let context = GpuContext::new(GpuBackend::Cpu).expect("Failed to create context");
        let mut backprop = EfficientBackprop::<f32>::new(
            Arc::new(context),
            1024 * 1024 * 1024,
            RecomputationPolicy::CheckpointAll,
            true,
        );

        assert!(backprop.is_enabled());

        backprop.set_enabled(false);
        assert!(!backprop.is_enabled());

        backprop.set_enabled(true);
        assert!(backprop.is_enabled());
    }
}