rfann 0.1.0

A pure Rust implementation of the Fast Artificial Neural Network (FANN) 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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! GPU-accelerated training algorithms using WebGPU
//!
//! This module provides GPU-accelerated versions of training algorithms for dramatic
//! performance improvements. Expected benefits:
//! - 10-50x speedup for matrix operations
//! - Parallel batch processing
//! - Efficient memory management with buffer pooling
//! - Automatic fallback to CPU when GPU is unavailable

use super::*;
use crate::webgpu::backend::ComputeBackend;
use crate::webgpu::{ComputeContext, ComputeError};
use num_traits::Float;
use std::collections::HashMap;
use std::sync::Arc;

/// GPU-accelerated Adam optimizer
/// Provides dramatic speedup over CPU implementation for larger networks
#[cfg(feature = "gpu")]
pub struct GpuAdam<T: Float + Send + Sync + Default + std::fmt::Debug + 'static> {
    learning_rate: T,
    beta1: T,
    beta2: T,
    epsilon: T,
    weight_decay: T,
    error_function: Box<dyn ErrorFunction<T>>,

    /// GPU compute context for operations
    _compute_context: ComputeContext<T>,

    /// WebGPU backend for actual GPU operations
    webgpu_backend: Option<Arc<dyn ComputeBackend<T>>>,

    /// GPU moment estimates (stored on GPU)
    m_weights_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,
    v_weights_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,
    m_biases_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,
    v_biases_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,

    /// Step counter for bias correction
    step: usize,

    /// Performance statistics
    gpu_stats: GpuPerformanceStats,

    /// CPU-side moment estimates (temporary until full GPU implementation)
    moment_estimates: Option<HashMap<String, T>>,

    callback: Option<TrainingCallback<T>>,
}

/// GPU-accelerated AdamW optimizer
/// AdamW with decoupled weight decay, optimized for GPU execution
///
/// TODO: This is a placeholder for future GPU implementation of AdamW
#[cfg(feature = "gpu")]
#[allow(dead_code)]
pub struct GpuAdamW<T: Float + Send + Sync + Default + std::fmt::Debug + 'static> {
    learning_rate: T,
    beta1: T,
    beta2: T,
    epsilon: T,
    weight_decay: T,
    error_function: Box<dyn ErrorFunction<T>>,

    /// GPU compute context for operations
    _compute_context: ComputeContext<T>,

    /// WebGPU backend for actual GPU operations
    webgpu_backend: Option<Arc<dyn ComputeBackend<T>>>,

    /// GPU moment estimates (stored on GPU)
    m_weights_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,
    v_weights_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,
    m_biases_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,
    v_biases_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,

    /// Step counter for bias correction
    step: usize,

    /// Performance statistics
    gpu_stats: GpuPerformanceStats,

    callback: Option<TrainingCallback<T>>,
}

/// GPU-accelerated batch backpropagation
/// Processes entire batches on GPU for maximum parallelism
///
/// TODO: This is a placeholder for future GPU implementation of batch backpropagation
#[cfg(feature = "gpu")]
#[allow(dead_code)]
pub struct GpuBatchBackprop<T: Float + Send + Sync + Default + std::fmt::Debug + 'static> {
    learning_rate: T,
    momentum: T,
    error_function: Box<dyn ErrorFunction<T>>,

    /// GPU compute context for operations
    _compute_context: ComputeContext<T>,

    /// WebGPU backend for actual GPU operations
    webgpu_backend: Option<Arc<dyn ComputeBackend<T>>>,

    /// GPU momentum buffers
    momentum_weights_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,
    momentum_biases_gpu: Option<Vec<crate::webgpu::memory::BufferHandle>>,

    /// Performance statistics
    gpu_stats: GpuPerformanceStats,

    callback: Option<TrainingCallback<T>>,
}

/// Performance statistics for GPU training
#[derive(Debug, Default, Clone)]
pub struct GpuPerformanceStats {
    /// Total GPU compute time in milliseconds
    pub total_gpu_time_ms: f64,
    /// Memory transfer time (CPU <-> GPU)
    pub memory_transfer_time_ms: f64,
    /// Number of GPU kernel launches
    pub kernel_launches: u64,
    /// Average batch processing time
    pub avg_batch_time_ms: f64,
    /// GPU memory usage in bytes
    pub gpu_memory_used_bytes: u64,
    /// Speedup factor vs CPU
    pub speedup_vs_cpu: f64,
}

#[cfg(feature = "gpu")]
impl<T: Float + Send + Sync + Default + std::fmt::Debug + 'static> GpuAdam<T> {
    /// Create a new GPU Adam optimizer
    pub fn new(learning_rate: T) -> Result<Self, ComputeError> {
        let compute_context = ComputeContext::new()?;

        // Initialize WebGPU backend if available
        let webgpu_backend = Self::initialize_webgpu_backend()?;

        Ok(Self {
            learning_rate,
            beta1: T::from(0.9).unwrap(),
            beta2: T::from(0.999).unwrap(),
            epsilon: T::from(1e-8).unwrap(),
            weight_decay: T::zero(),
            error_function: Box::new(MseError),
            _compute_context: compute_context,
            webgpu_backend,
            m_weights_gpu: None,
            v_weights_gpu: None,
            m_biases_gpu: None,
            v_biases_gpu: None,
            step: 0,
            gpu_stats: GpuPerformanceStats::default(),
            moment_estimates: None,
            callback: None,
        })
    }

    /// Initialize WebGPU backend if available
    fn initialize_webgpu_backend() -> Result<Option<Arc<dyn ComputeBackend<T>>>, ComputeError> {
        // Try to initialize WebGPU backend with graceful fallback
        match crate::webgpu::WebGPUBackend::<T>::new() {
            Ok(backend) => {
                log::info!("GPU acceleration enabled via WebGPU");
                Ok(Some(Arc::new(backend) as Arc<dyn ComputeBackend<T>>))
            }
            Err(e) => {
                log::warn!("GPU initialization failed: {e}, falling back to CPU");
                Ok(None)
            }
        }
    }

    /// Check if GPU is available and initialized
    pub fn is_gpu_available(&self) -> bool {
        self.webgpu_backend.is_some()
    }

    /// Set beta1 parameter
    pub fn with_beta1(mut self, beta1: T) -> Self {
        self.beta1 = beta1;
        self
    }

    /// Set beta2 parameter
    pub fn with_beta2(mut self, beta2: T) -> Self {
        self.beta2 = beta2;
        self
    }

    /// Set epsilon for numerical stability
    pub fn with_epsilon(mut self, epsilon: T) -> Self {
        self.epsilon = epsilon;
        self
    }

    /// Set weight decay (L2 regularization)
    pub fn with_weight_decay(mut self, weight_decay: T) -> Self {
        self.weight_decay = weight_decay;
        self
    }

    /// Set error function
    pub fn with_error_function(mut self, error_function: Box<dyn ErrorFunction<T>>) -> Self {
        self.error_function = error_function;
        self
    }

    /// Get GPU performance statistics
    pub fn get_performance_stats(&self) -> &GpuPerformanceStats {
        &self.gpu_stats
    }

    /// Initialize GPU buffers for moment estimates
    fn initialize_gpu_buffers(&mut self, network: &Network<T>) -> Result<(), ComputeError> {
        if self.m_weights_gpu.is_none() && self.webgpu_backend.is_some() {
            // Clone the Arc to avoid borrow checker issues
            let backend = self.webgpu_backend.clone().unwrap();
            let memory_manager = backend.memory_manager();

            // Initialize weight moment buffers
            let mut m_weights = Vec::new();
            let mut v_weights = Vec::new();

            for layer in network.layers.iter().skip(1) {
                let num_weights = layer
                    .neurons
                    .iter()
                    .filter(|n| !n.is_bias)
                    .map(|n| n.connections.len())
                    .sum::<usize>();

                if num_weights > 0 {
                    let m_buffer =
                        memory_manager.allocate_buffer(num_weights * std::mem::size_of::<T>())?;
                    let v_buffer =
                        memory_manager.allocate_buffer(num_weights * std::mem::size_of::<T>())?;

                    // Initialize to zero
                    let zeros = vec![T::zero(); num_weights];
                    memory_manager.upload_data(m_buffer, &zeros)?;
                    memory_manager.upload_data(v_buffer, &zeros)?;

                    m_weights.push(m_buffer);
                    v_weights.push(v_buffer);
                }
            }

            // Initialize bias moment buffers
            let mut m_biases = Vec::new();
            let mut v_biases = Vec::new();

            for layer in network.layers.iter().skip(1) {
                let num_biases = layer.neurons.iter().filter(|n| !n.is_bias).count();

                if num_biases > 0 {
                    let m_buffer =
                        memory_manager.allocate_buffer(num_biases * std::mem::size_of::<T>())?;
                    let v_buffer =
                        memory_manager.allocate_buffer(num_biases * std::mem::size_of::<T>())?;

                    // Initialize to zero
                    let zeros = vec![T::zero(); num_biases];
                    memory_manager.upload_data(m_buffer, &zeros)?;
                    memory_manager.upload_data(v_buffer, &zeros)?;

                    m_biases.push(m_buffer);
                    v_biases.push(v_buffer);
                }
            }

            self.m_weights_gpu = Some(m_weights);
            self.v_weights_gpu = Some(v_weights);
            self.m_biases_gpu = Some(m_biases);
            self.v_biases_gpu = Some(v_biases);
        }

        Ok(())
    }

    /// Perform GPU-accelerated training step with batch processing
    fn gpu_train_step(
        &mut self,
        network: &mut Network<T>,
        data: &TrainingData<T>,
    ) -> Result<T, ComputeError> {
        let start_time = std::time::Instant::now();

        // Initialize GPU buffers if needed
        self.initialize_gpu_buffers(network)?;

        self.step += 1;

        // Check if we have WebGPU backend
        if let Some(backend) = self.webgpu_backend.clone() {
            // Use batch training for efficiency
            use super::gpu_batch_training::gpu_batch_train_step;

            // Process entire batch in one GPU operation
            let total_error = gpu_batch_train_step(network, data, backend.clone(), self)?;

            // Update performance statistics
            let elapsed = start_time.elapsed();
            self.gpu_stats.total_gpu_time_ms += elapsed.as_secs_f64() * 1000.0;
            // Much fewer kernel launches with batch processing!
            self.gpu_stats.kernel_launches += 1; // One batch operation instead of thousands
            self.gpu_stats.avg_batch_time_ms = elapsed.as_secs_f64() * 1000.0;

            Ok(total_error)
        } else {
            // Fallback to CPU implementation
            Err(ComputeError::GpuUnavailable)
        }
    }

    /// Extract weights from a layer for GPU operations
    fn extract_layer_weights(&self, layer: &crate::Layer<T>) -> Vec<T> {
        let mut weights = Vec::new();

        for neuron in layer.neurons.iter().filter(|n| !n.is_bias) {
            // Skip bias connection (first connection)
            for connection in neuron.connections.iter().skip(1) {
                weights.push(connection.weight);
            }
        }

        weights
    }

    /// Extract biases from a layer for GPU operations
    fn extract_layer_biases(&self, layer: &crate::Layer<T>) -> Vec<T> {
        let mut biases = Vec::new();

        for neuron in layer.neurons.iter().filter(|n| !n.is_bias) {
            // First connection is bias
            let bias = if !neuron.connections.is_empty() {
                neuron.connections[0].weight
            } else {
                T::zero()
            };
            biases.push(bias);
        }

        biases
    }

    /// Apply Adam updates using real gradients computed via backpropagation
    pub(super) fn apply_adam_updates_with_gradients(
        &mut self,
        network: &mut Network<T>,
        weight_gradients: &[Vec<T>],
        bias_gradients: &[Vec<T>],
    ) -> Result<(), ComputeError> {
        // Initialize moment estimates if not already done
        if self.m_weights_gpu.is_none() {
            self.initialize_moment_estimates(network)?;
        }

        // Bias correction for Adam
        let lr_t = self.learning_rate * (T::one() - self.beta2.powi(self.step as i32)).sqrt()
            / (T::one() - self.beta1.powi(self.step as i32));

        // Apply Adam updates to each layer
        for (layer_idx, layer) in network.layers.iter_mut().skip(1).enumerate() {
            let weight_grads = &weight_gradients[layer_idx];
            let bias_grads = &bias_gradients[layer_idx];

            let mut weight_idx = 0;
            for (neuron_idx, neuron) in layer.neurons.iter_mut().filter(|n| !n.is_bias).enumerate()
            {
                // Update bias (first connection)
                if !neuron.connections.is_empty() {
                    let bias_grad = bias_grads[neuron_idx];
                    self.update_adam_parameter(
                        &mut neuron.connections[0].weight,
                        bias_grad,
                        lr_t,
                        layer_idx,
                        neuron_idx,
                        true, // is_bias
                    );
                }

                // Update weights (remaining connections)
                for connection in neuron.connections.iter_mut().skip(1) {
                    if weight_idx < weight_grads.len() {
                        let weight_grad = weight_grads[weight_idx];
                        self.update_adam_parameter(
                            &mut connection.weight,
                            weight_grad,
                            lr_t,
                            layer_idx,
                            weight_idx,
                            false, // is_bias
                        );
                        weight_idx += 1;
                    }
                }
            }
        }

        Ok(())
    }

    /// Update a single parameter using Adam algorithm
    fn update_adam_parameter(
        &mut self,
        param: &mut T,
        gradient: T,
        lr_t: T,
        layer_idx: usize,
        param_idx: usize,
        is_bias: bool,
    ) {
        // Get moment estimates (in real GPU implementation, these would be GPU buffers)
        // For now, we'll use CPU-side tracking
        let m_key = format!("{}_{}_{}_{}", layer_idx, param_idx, is_bias, "m");
        let v_key = format!("{}_{}_{}_{}", layer_idx, param_idx, is_bias, "v");

        // Retrieve or initialize moments
        let m = self.get_moment(&m_key).unwrap_or(T::zero());
        let v = self.get_moment(&v_key).unwrap_or(T::zero());

        // Update biased first moment estimate
        let new_m = self.beta1 * m + (T::one() - self.beta1) * gradient;

        // Update biased second raw moment estimate
        let new_v = self.beta2 * v + (T::one() - self.beta2) * gradient * gradient;

        // Store updated moments
        self.set_moment(&m_key, new_m);
        self.set_moment(&v_key, new_v);

        // Update parameter
        *param = *param - lr_t * new_m / (new_v.sqrt() + self.epsilon);

        // Apply weight decay if specified
        if self.weight_decay > T::zero() && !is_bias {
            *param = *param - self.learning_rate * self.weight_decay * *param;
        }
    }

    /// Initialize moment estimates for Adam optimizer
    fn initialize_moment_estimates(&mut self, _network: &Network<T>) -> Result<(), ComputeError> {
        // In a full GPU implementation, this would allocate GPU buffers
        // For now, we use a simple HashMap for CPU tracking
        self.moment_estimates = Some(HashMap::new());
        Ok(())
    }

    /// Get moment estimate (CPU fallback)
    fn get_moment(&self, key: &str) -> Option<T> {
        self.moment_estimates.as_ref()?.get(key).copied()
    }

    /// Set moment estimate (CPU fallback)
    fn set_moment(&mut self, key: &str, value: T) {
        if let Some(moments) = self.moment_estimates.as_mut() {
            moments.insert(key.to_string(), value);
        }
    }
}

#[cfg(feature = "gpu")]
impl<T: Float + Send + Sync + Default + std::fmt::Debug + 'static> TrainingAlgorithm<T>
    for GpuAdam<T>
{
    fn train_epoch(
        &mut self,
        network: &mut Network<T>,
        data: &TrainingData<T>,
    ) -> Result<T, TrainingError> {
        match self.gpu_train_step(network, data) {
            Ok(error) => Ok(error),
            Err(ComputeError::GpuUnavailable) => {
                // Fallback to CPU Adam
                let mut cpu_adam = super::Adam::new(self.learning_rate)
                    .with_beta1(self.beta1)
                    .with_beta2(self.beta2)
                    .with_epsilon(self.epsilon)
                    .with_weight_decay(self.weight_decay);

                println!("GPU not available, falling back to CPU Adam");
                cpu_adam.train_epoch(network, data)
            }
            Err(e) => Err(TrainingError::TrainingFailed(format!(
                "GPU training failed: {e}"
            ))),
        }
    }

    fn calculate_error(&self, network: &Network<T>, data: &TrainingData<T>) -> T {
        // Use GPU for error calculation when available
        // Clone the Arc to avoid borrow checker issues
        if let Some(backend) = self.webgpu_backend.clone() {
            let mut total_error = T::zero();

            for (input, desired_output) in data.inputs.iter().zip(data.outputs.iter()) {
                // Run forward pass using GPU
                let mut current_input = input.clone();

                for layer in network.layers.iter().skip(1) {
                    let weights = self.extract_layer_weights(layer);
                    let biases = self.extract_layer_biases(layer);

                    if let Ok(layer_output) = backend.matrix_vector_multiply(
                        &weights,
                        &current_input,
                        biases.len(),
                        current_input.len(),
                    ) {
                        let mut activated_output = Vec::new();
                        for (i, &output) in layer_output.iter().enumerate() {
                            let with_bias = output + biases[i];
                            activated_output.push(with_bias);
                        }

                        let activation_function = layer
                            .neurons
                            .iter()
                            .find(|n| !n.is_bias)
                            .map(|n| n.activation_function)
                            .unwrap_or(crate::ActivationFunction::Sigmoid);

                        if let Ok(activated) = backend.apply_activation_function(
                            &activated_output,
                            activation_function,
                            T::one(),
                        ) {
                            current_input = activated;
                        }
                    }
                }

                total_error = total_error
                    + self
                        .error_function
                        .calculate(&current_input, desired_output);
            }

            total_error / T::from(data.inputs.len()).unwrap()
        } else {
            // Fallback to CPU calculation
            let mut total_error = T::zero();
            let mut network_clone = network.clone();

            for (input, desired_output) in data.inputs.iter().zip(data.outputs.iter()) {
                let output = network_clone.run(input);
                total_error = total_error + self.error_function.calculate(&output, desired_output);
            }

            total_error / T::from(data.inputs.len()).unwrap()
        }
    }

    fn count_bit_fails(
        &self,
        network: &Network<T>,
        data: &TrainingData<T>,
        bit_fail_limit: T,
    ) -> usize {
        let mut bit_fails = 0;
        let mut network_clone = network.clone();

        for (input, desired_output) in data.inputs.iter().zip(data.outputs.iter()) {
            let output = network_clone.run(input);
            for (&actual, &desired) in output.iter().zip(desired_output.iter()) {
                if (actual - desired).abs() > bit_fail_limit {
                    bit_fails += 1;
                }
            }
        }

        bit_fails
    }

    fn save_state(&self) -> TrainingState<T> {
        let mut state = HashMap::new();
        state.insert("learning_rate".to_string(), vec![self.learning_rate]);
        state.insert("beta1".to_string(), vec![self.beta1]);
        state.insert("beta2".to_string(), vec![self.beta2]);
        state.insert("epsilon".to_string(), vec![self.epsilon]);
        state.insert("weight_decay".to_string(), vec![self.weight_decay]);
        state.insert("step".to_string(), vec![T::from(self.step).unwrap()]);

        TrainingState {
            epoch: 0,
            best_error: T::from(f32::MAX).unwrap(),
            algorithm_specific: state,
        }
    }

    fn restore_state(&mut self, state: TrainingState<T>) {
        if let Some(lr) = state.algorithm_specific.get("learning_rate") {
            if !lr.is_empty() {
                self.learning_rate = lr[0];
            }
        }
        if let Some(b1) = state.algorithm_specific.get("beta1") {
            if !b1.is_empty() {
                self.beta1 = b1[0];
            }
        }
        if let Some(b2) = state.algorithm_specific.get("beta2") {
            if !b2.is_empty() {
                self.beta2 = b2[0];
            }
        }
        if let Some(eps) = state.algorithm_specific.get("epsilon") {
            if !eps.is_empty() {
                self.epsilon = eps[0];
            }
        }
        if let Some(wd) = state.algorithm_specific.get("weight_decay") {
            if !wd.is_empty() {
                self.weight_decay = wd[0];
            }
        }
        if let Some(s) = state.algorithm_specific.get("step") {
            if !s.is_empty() {
                self.step = s[0].to_usize().unwrap_or(0);
            }
        }
    }

    fn set_callback(&mut self, callback: TrainingCallback<T>) {
        self.callback = Some(callback);
    }

    fn call_callback(
        &mut self,
        epoch: usize,
        network: &Network<T>,
        data: &TrainingData<T>,
    ) -> bool {
        let error = self.calculate_error(network, data);
        if let Some(ref mut callback) = self.callback {
            callback(epoch, error)
        } else {
            true
        }
    }
}

// Placeholder implementations for CPU fallback when GPU not available
#[cfg(not(feature = "gpu"))]
pub type GpuAdam<T> = super::Adam<T>;

#[cfg(not(feature = "gpu"))]
pub type GpuAdamW<T> = super::AdamW<T>;

#[cfg(not(feature = "gpu"))]
pub type GpuBatchBackprop<T> = super::BatchBackprop<T>;

#[cfg(not(feature = "gpu"))]
pub type GpuPerformanceStats = ();

/// Check if GPU training is available
pub fn is_gpu_available() -> bool {
    // Skip GPU availability check when running under Miri
    #[cfg(miri)]
    return false;

    #[cfg(feature = "gpu")]
    {
        crate::webgpu::WebGPUBackend::<f32>::is_available()
    }

    #[cfg(not(feature = "gpu"))]
    false
}

/// Get GPU capabilities summary
pub fn get_gpu_capabilities() -> String {
    // Skip GPU capabilities check when running under Miri
    #[cfg(miri)]
    return "GPU unavailable under Miri".to_string();

    #[cfg(feature = "gpu")]
    {
        if is_gpu_available() {
            match crate::webgpu::WebGPUBackend::<f32>::new() {
                Ok(backend) => {
                    let caps = backend.capabilities();
                    format!(
                        "GPU Capabilities: Max Buffer {} MB, Compute Units: {}, F16: {}, Bandwidth: {} GB/s",
                        caps.max_buffer_size / (1024 * 1024),
                        caps.max_compute_units,
                        if caps.supports_f16 { "Yes" } else { "No" },
                        caps.memory_bandwidth_gbps
                    )
                }
                Err(_) => "GPU unavailable".to_string(),
            }
        } else {
            "No GPU adapter found".to_string()
        }
    }

    #[cfg(not(feature = "gpu"))]
    {
        "GPU support not compiled (enable with --features gpu)".to_string()
    }
}

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

    #[test]
    #[cfg_attr(miri, ignore = "Miri cannot handle WebGPU FFI calls")]
    fn test_gpu_availability() {
        println!("GPU available: {}", is_gpu_available());
        println!("GPU capabilities: {}", get_gpu_capabilities());
    }

    #[cfg(feature = "gpu")]
    #[test]
    #[cfg_attr(miri, ignore = "Miri cannot handle WebGPU FFI calls")]
    fn test_gpu_adam_creation() {
        // Skip if no GPU available (CI/headless environments)
        if !is_gpu_available() {
            println!("GPU not available, skipping GPU Adam creation test");
            return;
        }

        let result = GpuAdam::new(0.001f32);
        match result {
            Ok(optimizer) => {
                assert_eq!(optimizer.learning_rate, 0.001);
                assert_eq!(optimizer.beta1, 0.9);
                assert_eq!(optimizer.beta2, 0.999);
                assert_eq!(optimizer.step, 0);
            }
            Err(e) => {
                println!("GPU Adam creation failed (expected in CI): {e}");
            }
        }
    }
}