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
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// Copyright (c) 2025-2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Hardware acceleration integration for TrustformeRS
//!
//! This module provides a unified interface for hardware acceleration backends,
//! automatically selecting the best available acceleration method based on system
//! capabilities and user preferences.

#![allow(unused_variables)] // Multi-backend implementation with feature gates

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

/// Hardware acceleration backend types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccelerationBackend {
    /// NVIDIA CUDA
    Cuda,
    /// AMD ROCm
    Rocm,
    /// Intel OneAPI
    Intel,
    /// Vulkan Compute
    Vulkan,
    /// Apple Metal
    Metal,
    /// CPU fallback
    Cpu,
}

/// Hardware acceleration configuration
#[derive(Debug, Clone)]
pub struct AccelerationConfig {
    /// Preferred backend (if available)
    pub preferred_backend: Option<AccelerationBackend>,
    /// Enable automatic fallback to CPU
    pub auto_fallback: bool,
    /// Memory pool size per device (MB)
    pub memory_pool_size: usize,
    /// Enable kernel caching
    pub enable_kernel_cache: bool,
    /// Enable performance monitoring
    pub enable_monitoring: bool,
}

impl Default for AccelerationConfig {
    fn default() -> Self {
        Self {
            preferred_backend: None,
            auto_fallback: true,
            memory_pool_size: 1024, // 1GB
            enable_kernel_cache: true,
            enable_monitoring: true,
        }
    }
}

/// Hardware acceleration manager
pub struct HardwareAccelerator {
    /// Active backend
    active_backend: AccelerationBackend,
    /// Configuration
    #[allow(dead_code)]
    config: AccelerationConfig,
    /// Performance statistics
    stats: AccelerationStats,
}

/// Performance statistics
#[derive(Debug, Clone, Default)]
pub struct AccelerationStats {
    /// Total operations executed
    pub total_operations: u64,
    /// Total execution time (ms)
    pub total_time_ms: f64,
    /// Memory allocated (bytes)
    pub memory_allocated: u64,
    /// Cache hits
    pub cache_hits: u64,
    /// Cache misses
    pub cache_misses: u64,
}

/// Global hardware accelerator instance
static ACCELERATOR: OnceLock<HardwareAccelerator> = OnceLock::new();

impl HardwareAccelerator {
    /// Initialize hardware accelerator with configuration
    pub fn initialize(config: AccelerationConfig) -> Result<&'static HardwareAccelerator> {
        ACCELERATOR.get_or_init(|| {
            Self::new(config).unwrap_or_else(|_| {
                // Fallback to CPU if initialization fails
                Self::new_cpu_fallback()
            })
        });
        Ok(ACCELERATOR.get().expect("accelerator should be initialized after get_or_init"))
    }

    /// Get global hardware accelerator instance
    pub fn global() -> Result<&'static HardwareAccelerator> {
        ACCELERATOR.get().ok_or_else(|| {
            hardware_error("unknown", "Hardware accelerator not initialized")
                .with_operation("global")
                .with_suggestion("Call HardwareAccelerator::initialize() first")
        })
    }

    /// Create new hardware accelerator
    fn new(config: AccelerationConfig) -> Result<Self> {
        let backend = Self::select_backend(&config)?;

        // Initialize the selected backend
        match backend {
            AccelerationBackend::Cuda => {
                #[cfg(feature = "cuda")]
                {
                    crate::kernels::cuda_impl::api::init_cuda()?;
                }
                #[cfg(not(feature = "cuda"))]
                {
                    return Err(
                        acceleration_error("CUDA", "Support not compiled in this build")
                            .with_operation("initialization")
                            .with_suggestion("Rebuild with --features cuda to enable CUDA support"),
                    );
                }
            },
            AccelerationBackend::Rocm => {
                #[cfg(feature = "rocm")]
                {
                    crate::kernels::rocm_impl::api::init_rocm()?;
                }
                #[cfg(not(feature = "rocm"))]
                {
                    return Err(
                        acceleration_error("ROCm", "Support not compiled in this build")
                            .with_operation("initialization")
                            .with_suggestion("Rebuild with --features rocm to enable ROCm support"),
                    );
                }
            },
            AccelerationBackend::Intel => {
                #[cfg(feature = "intel")]
                {
                    crate::kernels::intel_impl::api::init_intel()?;
                }
                #[cfg(not(feature = "intel"))]
                {
                    return Err(acceleration_error(
                        "Intel OneAPI",
                        "Support not compiled in this build",
                    )
                    .with_operation("initialization")
                    .with_suggestion(
                        "Rebuild with --features intel to enable Intel OneAPI support",
                    ));
                }
            },
            AccelerationBackend::Vulkan => {
                #[cfg(feature = "vulkan")]
                {
                    // Vulkan backend initialization is handled in VulkanImpl::new()
                    let _vulkan = crate::kernels::vulkan_impl::VulkanImpl::new()?;
                }
                #[cfg(not(feature = "vulkan"))]
                {
                    return Err(
                        acceleration_error("Vulkan", "Support not compiled in this build")
                            .with_operation("initialization")
                            .with_suggestion(
                                "Rebuild with --features vulkan to enable Vulkan support",
                            ),
                    );
                }
            },
            AccelerationBackend::Metal => {
                #[cfg(all(target_os = "macos", feature = "metal"))]
                {
                    // Metal backend initialization using Metal Performance Shaders
                    let _metal = crate::kernels::metal_impl::MetalImpl::new()?;
                    log::info!(
                        "Metal backend initialized successfully for Apple Silicon acceleration"
                    );
                }
                #[cfg(not(all(target_os = "macos", feature = "metal")))]
                {
                    return Err(
                        acceleration_error("Metal", "Support not compiled in this build")
                            .with_operation("initialization")
                            .with_suggestion(
                                "Rebuild with --features metal to enable Metal support",
                            )
                            .with_suggestion("Metal backend requires macOS/iOS with Apple Silicon"),
                    );
                }
            },
            AccelerationBackend::Cpu => {
                // CPU backend is always available
            },
        }

        Ok(Self {
            active_backend: backend,
            config,
            stats: AccelerationStats::default(),
        })
    }

    /// Create CPU fallback accelerator
    fn new_cpu_fallback() -> Self {
        Self {
            active_backend: AccelerationBackend::Cpu,
            config: AccelerationConfig::default(),
            stats: AccelerationStats::default(),
        }
    }

    /// Select the best available backend
    fn select_backend(config: &AccelerationConfig) -> Result<AccelerationBackend> {
        // Try preferred backend first
        if let Some(preferred) = config.preferred_backend {
            if Self::is_backend_available(preferred) {
                return Ok(preferred);
            }
        }

        // Auto-select based on availability
        let backends = [
            AccelerationBackend::Cuda,
            AccelerationBackend::Rocm,
            AccelerationBackend::Intel,
            AccelerationBackend::Vulkan,
            AccelerationBackend::Metal,
            AccelerationBackend::Cpu,
        ];

        for backend in backends {
            if Self::is_backend_available(backend) {
                return Ok(backend);
            }
        }

        Err(
            hardware_error("system", "No acceleration backend available on this system")
                .with_operation("backend_selection")
                .with_suggestion("Install GPU drivers (NVIDIA CUDA, AMD ROCm, Intel OneAPI)")
                .with_suggestion("Ensure required features are enabled during compilation")
                .with_suggestion("CPU backend should always be available as fallback"),
        )
    }

    /// Check if backend is available
    fn is_backend_available(backend: AccelerationBackend) -> bool {
        match backend {
            AccelerationBackend::Cuda => {
                #[cfg(feature = "cuda")]
                {
                    crate::kernels::cuda_impl::api::init_cuda().is_ok()
                }
                #[cfg(not(feature = "cuda"))]
                {
                    false
                }
            },
            AccelerationBackend::Rocm => {
                #[cfg(feature = "rocm")]
                {
                    crate::kernels::rocm_impl::api::init_rocm().is_ok()
                }
                #[cfg(not(feature = "rocm"))]
                {
                    false
                }
            },
            AccelerationBackend::Intel => {
                #[cfg(feature = "intel")]
                {
                    crate::kernels::intel_impl::api::is_intel_available()
                }
                #[cfg(not(feature = "intel"))]
                {
                    false
                }
            },
            AccelerationBackend::Vulkan => {
                #[cfg(feature = "vulkan")]
                {
                    crate::kernels::vulkan_impl::VulkanImpl::new().is_ok()
                }
                #[cfg(not(feature = "vulkan"))]
                {
                    false
                }
            },
            AccelerationBackend::Metal => {
                // Check if Metal is available by attempting to create a Metal implementation
                #[cfg(all(target_os = "macos", feature = "metal"))]
                {
                    crate::kernels::metal_impl::MetalImpl::new().is_ok()
                }
                #[cfg(not(all(target_os = "macos", feature = "metal")))]
                {
                    false
                }
            },
            AccelerationBackend::Cpu => true,
        }
    }

    /// Execute matrix multiplication with hardware acceleration
    pub fn matmul(&mut self, a: &Tensor, b: &Tensor, c: &mut Tensor) -> Result<()> {
        let start_time = std::time::Instant::now();

        let result = match self.active_backend {
            AccelerationBackend::Cuda => {
                #[cfg(feature = "cuda")]
                {
                    crate::kernels::cuda_impl::api::cuda_matmul(a, b, c)
                }
                #[cfg(not(feature = "cuda"))]
                {
                    self.cpu_matmul(a, b, c)
                }
            },
            AccelerationBackend::Rocm => {
                #[cfg(feature = "rocm")]
                {
                    crate::kernels::rocm_impl::api::rocm_matmul(a, b, c)
                }
                #[cfg(not(feature = "rocm"))]
                {
                    self.cpu_matmul(a, b, c)
                }
            },
            AccelerationBackend::Intel => {
                #[cfg(feature = "intel")]
                {
                    crate::kernels::intel_impl::api::intel_matmul(a, b, c)
                }
                #[cfg(not(feature = "intel"))]
                {
                    self.cpu_matmul(a, b, c)
                }
            },
            AccelerationBackend::Vulkan => {
                #[cfg(feature = "vulkan")]
                {
                    let mut vulkan = crate::kernels::vulkan_impl::VulkanImpl::new()?;
                    vulkan.matmul(a, b, c)
                }
                #[cfg(not(feature = "vulkan"))]
                {
                    self.cpu_matmul(a, b, c)
                }
            },
            AccelerationBackend::Metal => {
                #[cfg(all(target_os = "macos", feature = "metal"))]
                {
                    let metal_impl = crate::kernels::metal_impl::MetalImpl::new()?;
                    metal_impl.matrix_multiply(a, b).and_then(|result| {
                        // Copy result to output tensor c
                        match (c, &result) {
                            (Tensor::F32(c_arr), Tensor::F32(result_arr)) => {
                                c_arr.assign(result_arr);
                                Ok(())
                            },
                            _ => Err(tensor_op_error(
                                "Tensor type mismatch in Metal matmul",
                                "matmul",
                            )),
                        }
                    })
                }
                #[cfg(not(all(target_os = "macos", feature = "metal")))]
                {
                    self.cpu_matmul(a, b, c)
                }
            },
            AccelerationBackend::Cpu => self.cpu_matmul(a, b, c),
        };

        // Update statistics
        self.stats.total_operations += 1;
        self.stats.total_time_ms += start_time.elapsed().as_millis() as f64;

        result
    }

    /// Execute Flash Attention with hardware acceleration
    pub fn flash_attention(
        &mut self,
        query: &Tensor,
        key: &Tensor,
        value: &Tensor,
        output: &mut Tensor,
    ) -> Result<()> {
        let start_time = std::time::Instant::now();

        let result = match self.active_backend {
            AccelerationBackend::Cuda => {
                #[cfg(feature = "cuda")]
                {
                    crate::kernels::cuda_impl::api::cuda_flash_attention(query, key, value, output)
                }
                #[cfg(not(feature = "cuda"))]
                {
                    self.cpu_flash_attention(query, key, value, output)
                }
            },
            AccelerationBackend::Rocm => {
                #[cfg(feature = "rocm")]
                {
                    crate::kernels::rocm_impl::api::rocm_flash_attention(query, key, value, output)
                }
                #[cfg(not(feature = "rocm"))]
                {
                    self.cpu_flash_attention(query, key, value, output)
                }
            },
            AccelerationBackend::Intel => {
                #[cfg(feature = "intel")]
                {
                    crate::kernels::intel_impl::api::intel_flash_attention(
                        query, key, value, output,
                    )
                }
                #[cfg(not(feature = "intel"))]
                {
                    self.cpu_flash_attention(query, key, value, output)
                }
            },
            AccelerationBackend::Vulkan => {
                #[cfg(feature = "vulkan")]
                {
                    let mut vulkan = crate::kernels::vulkan_impl::VulkanImpl::new()?;
                    let scale = 1.0 / (query.shape()[2] as f32).sqrt();
                    vulkan.flash_attention(query, key, value, output, scale)
                }
                #[cfg(not(feature = "vulkan"))]
                {
                    self.cpu_flash_attention(query, key, value, output)
                }
            },
            AccelerationBackend::Metal => {
                #[cfg(all(target_os = "macos", feature = "metal"))]
                {
                    let metal_impl = crate::kernels::metal_impl::MetalImpl::new()?;
                    metal_impl.flash_attention(query, key, value, output)
                }
                #[cfg(not(all(target_os = "macos", feature = "metal")))]
                {
                    self.cpu_flash_attention(query, key, value, output)
                }
            },
            AccelerationBackend::Cpu => self.cpu_flash_attention(query, key, value, output),
        };

        // Update statistics
        self.stats.total_operations += 1;
        self.stats.total_time_ms += start_time.elapsed().as_millis() as f64;

        result
    }

    /// CPU fallback for matrix multiplication
    fn cpu_matmul(&self, a: &Tensor, b: &Tensor, c: &mut Tensor) -> Result<()> {
        // Use the tensor's built-in matmul implementation
        let result = a.matmul(b)?;
        *c = result;
        Ok(())
    }

    /// CPU fallback for Flash Attention
    fn cpu_flash_attention(
        &self,
        query: &Tensor,
        key: &Tensor,
        value: &Tensor,
        output: &mut Tensor,
    ) -> Result<()> {
        // Simplified CPU implementation of Flash Attention
        let q_shape = query.shape();
        let batch_size = q_shape[0];
        let seq_len = q_shape[1];
        let head_dim = q_shape[2];

        // Compute attention scores: Q @ K^T
        let key_transposed = key.transpose(1, 2)?;
        let scores = query.matmul(&key_transposed)?;

        // Apply scaling
        let scale = 1.0 / (head_dim as f32).sqrt();
        let scaled_scores = scores.mul_scalar(scale)?;

        // Apply softmax
        let attention_weights = scaled_scores.softmax(2)?;

        // Apply attention to values: attention_weights @ V
        let result = attention_weights.matmul(value)?;

        *output = result;
        Ok(())
    }

    /// Get active backend
    pub fn active_backend(&self) -> AccelerationBackend {
        self.active_backend
    }

    /// Get performance statistics
    pub fn get_stats(&self) -> &AccelerationStats {
        &self.stats
    }

    /// Reset performance statistics
    pub fn reset_stats(&mut self) {
        self.stats = AccelerationStats::default();
    }

    /// Get device information
    pub fn device_info(&self) -> Result<String> {
        match self.active_backend {
            AccelerationBackend::Cuda => {
                #[cfg(feature = "cuda")]
                {
                    crate::kernels::cuda_impl::api::cuda_device_info()
                }
                #[cfg(not(feature = "cuda"))]
                {
                    Ok("CUDA not available".to_string())
                }
            },
            AccelerationBackend::Rocm => {
                #[cfg(feature = "rocm")]
                {
                    crate::kernels::rocm_impl::api::rocm_device_info()
                }
                #[cfg(not(feature = "rocm"))]
                {
                    Ok("ROCm not available".to_string())
                }
            },
            AccelerationBackend::Intel => {
                #[cfg(feature = "intel")]
                {
                    crate::kernels::intel_impl::api::intel_device_info()
                }
                #[cfg(not(feature = "intel"))]
                {
                    Ok("Intel OneAPI not available".to_string())
                }
            },
            AccelerationBackend::Cpu => Ok(format!("CPU: {} cores", num_cpus::get())),
            _ => Ok(format!("Backend: {:?}", self.active_backend)),
        }
    }

    /// Get memory statistics
    pub fn memory_stats(&self) -> Result<(usize, usize)> {
        match self.active_backend {
            AccelerationBackend::Cuda => {
                #[cfg(feature = "cuda")]
                {
                    crate::kernels::cuda_impl::api::cuda_memory_stats()
                }
                #[cfg(not(feature = "cuda"))]
                {
                    Ok((0, 0))
                }
            },
            AccelerationBackend::Rocm => {
                #[cfg(feature = "rocm")]
                {
                    crate::kernels::rocm_impl::api::rocm_memory_stats()
                }
                #[cfg(not(feature = "rocm"))]
                {
                    Ok((0, 0))
                }
            },
            AccelerationBackend::Intel => {
                #[cfg(feature = "intel")]
                {
                    crate::kernels::intel_impl::api::intel_memory_stats()
                }
                #[cfg(not(feature = "intel"))]
                {
                    Ok((0, 0))
                }
            },
            AccelerationBackend::Cpu => {
                Ok((0, 0)) // CPU doesn't have dedicated memory pool
            },
            _ => Ok((0, 0)),
        }
    }
}

/// Public API for hardware acceleration
pub mod api {
    use super::*;

    /// Initialize hardware acceleration with default configuration
    pub fn init_hardware_acceleration() -> Result<()> {
        HardwareAccelerator::initialize(AccelerationConfig::default())?;
        Ok(())
    }

    /// Initialize hardware acceleration with custom configuration
    pub fn init_hardware_acceleration_with_config(config: AccelerationConfig) -> Result<()> {
        HardwareAccelerator::initialize(config)?;
        Ok(())
    }

    /// Execute accelerated matrix multiplication
    pub fn accelerated_matmul(a: &Tensor, b: &Tensor, c: &mut Tensor) -> Result<()> {
        let accelerator = HardwareAccelerator::global()?;

        // Since we can't get a mutable reference from the static,
        // we need to handle this differently for now
        let result = a.matmul(b)?;
        *c = result;
        Ok(())
    }

    /// Execute accelerated Flash Attention
    pub fn accelerated_flash_attention(
        query: &Tensor,
        key: &Tensor,
        value: &Tensor,
        output: &mut Tensor,
    ) -> Result<()> {
        let accelerator = HardwareAccelerator::global()?;

        // Since we can't get a mutable reference from the static,
        // we need to handle this differently for now
        // Simplified CPU implementation of Flash Attention
        let q_shape = query.shape();
        let head_dim = q_shape[q_shape.len() - 1];

        // Compute attention scores: Q @ K^T
        let key_transposed = key.transpose(q_shape.len() - 2, q_shape.len() - 1)?;
        let scores = query.matmul(&key_transposed)?;

        // Apply scaling
        let scale = 1.0 / (head_dim as f32).sqrt();
        let scaled_scores = scores.mul_scalar(scale)?;

        // Apply softmax
        let attention_weights = scaled_scores.softmax((q_shape.len() - 1) as i32)?;

        // Apply attention to values: attention_weights @ V
        let result = attention_weights.matmul(value)?;

        *output = result;
        Ok(())
    }

    /// Get active acceleration backend
    pub fn get_active_backend() -> Result<AccelerationBackend> {
        Ok(HardwareAccelerator::global()?.active_backend())
    }

    /// Get device information
    pub fn get_device_info() -> Result<String> {
        HardwareAccelerator::global()?.device_info()
    }

    /// Get performance statistics
    pub fn get_performance_stats() -> Result<AccelerationStats> {
        Ok(HardwareAccelerator::global()?.get_stats().clone())
    }

    /// Get memory statistics
    pub fn get_memory_stats() -> Result<(usize, usize)> {
        HardwareAccelerator::global()?.memory_stats()
    }

    /// Check if a specific backend is available
    pub fn is_backend_available(backend: AccelerationBackend) -> bool {
        HardwareAccelerator::is_backend_available(backend)
    }

    /// List all available backends
    pub fn list_available_backends() -> Vec<AccelerationBackend> {
        let all_backends = [
            AccelerationBackend::Cuda,
            AccelerationBackend::Rocm,
            AccelerationBackend::Intel,
            AccelerationBackend::Vulkan,
            AccelerationBackend::Metal,
            AccelerationBackend::Cpu,
        ];

        all_backends
            .into_iter()
            .filter(|&backend| HardwareAccelerator::is_backend_available(backend))
            .collect()
    }
}

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

    #[test]
    fn test_hardware_acceleration_initialization() {
        let config = AccelerationConfig::default();
        assert!(HardwareAccelerator::initialize(config).is_ok());
    }

    #[test]
    fn test_backend_selection() {
        let available = api::list_available_backends();
        assert!(!available.is_empty());
        assert!(available.contains(&AccelerationBackend::Cpu));
    }

    #[test]
    fn test_accelerated_matmul() {
        let _ = api::init_hardware_acceleration();

        let a = Tensor::ones(&[4, 4]).expect("Failed to create ones tensor");
        let b = Tensor::ones(&[4, 4]).expect("Failed to create ones tensor");
        let mut c = Tensor::zeros(&[4, 4]).expect("Failed to create zero tensor");

        let result = api::accelerated_matmul(&a, &b, &mut c);
        assert!(result.is_ok());

        // Result should be all 4s
        let data = c.data().expect("operation failed in test");
        assert!(data.iter().all(|&x| (x - 4.0).abs() < 1e-6));
    }

    #[test]
    fn test_device_info() {
        let _ = api::init_hardware_acceleration();
        let info = api::get_device_info();
        assert!(info.is_ok());
    }

    #[test]
    fn test_performance_stats() {
        let _ = api::init_hardware_acceleration();
        let stats = api::get_performance_stats();
        assert!(stats.is_ok());
    }

    #[test]
    fn test_memory_stats() {
        let _ = api::init_hardware_acceleration();
        let stats = api::get_memory_stats();
        assert!(stats.is_ok());
    }
}