ghostflow-core 1.1.0

Core tensor operations for GhostFlow ML framework - optimized for maximum performance
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
//! Hardware abstraction layer
//!
//! Provides unified interface for different hardware backends:
//! - CUDA (NVIDIA GPUs)
//! - ROCm (AMD GPUs)
//! - Metal (Apple Silicon)
//! - TPU (Google TPUs)
//! - CPU with SIMD (AVX, NEON)

use crate::tensor::Tensor;
use crate::error::{GhostError, Result};

/// Hardware backend type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HardwareBackend {
    /// CPU with optional SIMD
    CPU,
    /// NVIDIA CUDA
    CUDA,
    /// AMD ROCm
    ROCm,
    /// Apple Metal
    Metal,
    /// Google TPU
    TPU,
}

/// Hardware device information
#[derive(Debug, Clone)]
pub struct HardwareDevice {
    /// Backend type
    pub backend: HardwareBackend,
    /// Device ID
    pub device_id: usize,
    /// Device name
    pub name: String,
    /// Total memory in bytes
    pub total_memory: usize,
    /// Available memory in bytes
    pub available_memory: usize,
    /// Compute capability (for CUDA/ROCm)
    pub compute_capability: Option<(u32, u32)>,
}

impl HardwareDevice {
    /// Create a CPU device
    pub fn cpu() -> Self {
        HardwareDevice {
            backend: HardwareBackend::CPU,
            device_id: 0,
            name: "CPU".to_string(),
            total_memory: 0,
            available_memory: 0,
            compute_capability: None,
        }
    }
    
    /// Create a CUDA device
    pub fn cuda(device_id: usize) -> Result<Self> {
        #[cfg(feature = "cuda")]
        {
            // Query CUDA device properties
            Ok(HardwareDevice {
                backend: HardwareBackend::CUDA,
                device_id,
                name: format!("CUDA Device {}", device_id),
                total_memory: 0, // Would query actual memory
                available_memory: 0,
                compute_capability: Some((7, 5)), // Example
            })
        }
        #[cfg(not(feature = "cuda"))]
        {
            Err(GhostError::DeviceError("CUDA support not compiled".to_string()))
        }
    }
    
    /// Create a ROCm device
    pub fn rocm(device_id: usize) -> Result<Self> {
        #[cfg(feature = "rocm")]
        {
            Ok(HardwareDevice {
                backend: HardwareBackend::ROCm,
                device_id,
                name: format!("ROCm Device {}", device_id),
                total_memory: 0,
                available_memory: 0,
                compute_capability: None,
            })
        }
        #[cfg(not(feature = "rocm"))]
        {
            Err(GhostError::DeviceError("ROCm support not compiled".to_string()))
        }
    }
    
    /// Create a Metal device
    pub fn metal(device_id: usize) -> Result<Self> {
        #[cfg(feature = "metal")]
        {
            Ok(HardwareDevice {
                backend: HardwareBackend::Metal,
                device_id,
                name: format!("Metal Device {}", device_id),
                total_memory: 0,
                available_memory: 0,
                compute_capability: None,
            })
        }
        #[cfg(not(feature = "metal"))]
        {
            Err(GhostError::DeviceError("Metal support not compiled".to_string()))
        }
    }
    
    /// Create a TPU device
    pub fn tpu(device_id: usize) -> Result<Self> {
        #[cfg(feature = "tpu")]
        {
            Ok(HardwareDevice {
                backend: HardwareBackend::TPU,
                device_id,
                name: format!("TPU Device {}", device_id),
                total_memory: 0,
                available_memory: 0,
                compute_capability: None,
            })
        }
        #[cfg(not(feature = "tpu"))]
        {
            Err(GhostError::DeviceError("TPU support not compiled".to_string()))
        }
    }
}

/// List available devices
pub fn list_devices() -> Vec<HardwareDevice> {
    let mut devices = vec![HardwareDevice::cpu()];
    
    // Check for CUDA devices
    #[cfg(feature = "cuda")]
    {
        match crate::cuda::get_device_count() {
            Ok(count) => {
                for i in 0..count {
                    if let Ok(device) = HardwareDevice::cuda(i) {
                        devices.push(device);
                    }
                }
            }
            Err(_) => {}
        }
    }
    
    // Check for ROCm devices
    #[cfg(feature = "rocm")]
    {
        match crate::rocm::RocmDevice::device_count() {
            Ok(count) => {
                for i in 0..count {
                    if let Ok(device) = HardwareDevice::rocm(i) {
                        devices.push(device);
                    }
                }
            }
            Err(_) => {}
        }
    }
    
    // Check for Metal devices
    #[cfg(feature = "metal")]
    {
        match crate::metal::MetalDevice::device_count() {
            Ok(count) => {
                for i in 0..count {
                    if let Ok(device) = HardwareDevice::metal(i) {
                        devices.push(device);
                    }
                }
            }
            Err(_) => {}
        }
    }
    
    // Check for TPU devices
    #[cfg(feature = "tpu")]
    {
        match crate::tpu::TpuDevice::device_count() {
            Ok(count) => {
                for i in 0..count {
                    if let Ok(device) = HardwareDevice::tpu(i) {
                        devices.push(device);
                    }
                }
            }
            Err(_) => {}
        }
    }
    
    devices
}

// Placeholder functions for device counting
#[cfg(feature = "cuda")]
fn cuda_device_count() -> Result<usize> {
    // Would use CUDA API
    Ok(1)
}

#[cfg(feature = "rocm")]
fn rocm_device_count() -> Result<usize> {
    // Would use ROCm API
    Ok(1)
}

#[cfg(feature = "metal")]
fn metal_device_count() -> Result<usize> {
    // Would use Metal API
    Ok(1)
}

#[cfg(feature = "tpu")]
fn tpu_device_count() -> Result<usize> {
    // Would use TPU API
    Ok(1)
}

/// Hardware-accelerated operations trait
pub trait HardwareOps {
    /// Matrix multiplication on hardware
    fn matmul_hw(&self, other: &Tensor, device: &HardwareDevice) -> Result<Tensor>;
    
    /// Convolution on hardware
    fn conv2d_hw(&self, kernel: &Tensor, device: &HardwareDevice) -> Result<Tensor>;
    
    /// Element-wise operations on hardware
    fn elementwise_hw(&self, op: ElementwiseOp, device: &HardwareDevice) -> Result<Tensor>;
}

#[derive(Debug, Clone, Copy)]
pub enum ElementwiseOp {
    Add,
    Mul,
    ReLU,
    Sigmoid,
    Tanh,
}

impl HardwareOps for Tensor {
    fn matmul_hw(&self, other: &Tensor, device: &HardwareDevice) -> Result<Tensor> {
        match device.backend {
            HardwareBackend::CPU => {
                // Use optimized CPU implementation with SIMD if available
                #[cfg(target_arch = "aarch64")]
                {
                    // Use NEON on ARM
                    let a_data = self.data_f32();
                    let b_data = other.data_f32();
                    let dims_a = self.dims();
                    let dims_b = other.dims();
                    
                    if dims_a.len() != 2 || dims_b.len() != 2 {
                        return Err(GhostError::InvalidShape("matmul requires 2D tensors".to_string()));
                    }
                    
                    let (m, k) = (dims_a[0], dims_a[1]);
                    let (k2, n) = (dims_b[0], dims_b[1]);
                    
                    if k != k2 {
                        return Err(GhostError::ShapeMismatch {
                            expected: vec![k],
                            got: vec![k2],
                        });
                    }
                    
                    let mut result = vec![0.0f32; m * n];
                    crate::neon::matmul_neon(&a_data, &b_data, &mut result, m, n, k);
                    Tensor::from_slice(&result, &[m, n])
                }
                #[cfg(not(target_arch = "aarch64"))]
                {
                    self.matmul(other)
                }
            }
            HardwareBackend::CUDA => {
                #[cfg(feature = "cuda")]
                {
                    crate::cuda::ops::matmul_cuda(self, other, device.device_id)
                }
                #[cfg(not(feature = "cuda"))]
                {
                    Err(GhostError::DeviceError("CUDA not available".to_string()))
                }
            }
            HardwareBackend::ROCm => {
                #[cfg(feature = "rocm")]
                {
                    crate::rocm::ops::matmul_rocm(self, other, device.device_id)
                }
                #[cfg(not(feature = "rocm"))]
                {
                    Err(GhostError::DeviceError("ROCm not available".to_string()))
                }
            }
            HardwareBackend::Metal => {
                #[cfg(feature = "metal")]
                {
                    crate::metal::mps::matmul_mps(self, other, device.device_id)
                }
                #[cfg(not(feature = "metal"))]
                {
                    Err(GhostError::DeviceError("Metal not available".to_string()))
                }
            }
            HardwareBackend::TPU => {
                #[cfg(feature = "tpu")]
                {
                    crate::tpu::ops::matmul_tpu(self, other, device.device_id)
                }
                #[cfg(not(feature = "tpu"))]
                {
                    Err(GhostError::DeviceError("TPU not available".to_string()))
                }
            }
        }
    }
    
    fn conv2d_hw(&self, kernel: &Tensor, device: &HardwareDevice) -> Result<Tensor> {
        // Similar dispatch logic for convolution
        match device.backend {
            HardwareBackend::CPU => {
                // Use CPU implementation
                Err(GhostError::NotImplemented("CPU conv2d".to_string()))
            }
            _ => Err(GhostError::NotImplemented("Hardware conv2d".to_string())),
        }
    }
    
    fn elementwise_hw(&self, op: ElementwiseOp, device: &HardwareDevice) -> Result<Tensor> {
        match device.backend {
            HardwareBackend::CPU => {
                #[cfg(target_arch = "aarch64")]
                {
                    // Use NEON optimizations on ARM
                    match op {
                        ElementwiseOp::ReLU => Ok(self.relu_neon()),
                        ElementwiseOp::Sigmoid => {
                            let mut data = self.data_f32();
                            crate::neon::sigmoid_neon(&mut data);
                            Tensor::from_slice(&data, self.dims())
                        }
                        ElementwiseOp::Tanh => Ok(self.tanh()),
                        ElementwiseOp::Add | ElementwiseOp::Mul => {
                            Err(GhostError::InvalidOperation("Binary op requires two tensors".to_string()))
                        }
                    }
                }
                #[cfg(not(target_arch = "aarch64"))]
                {
                    match op {
                        ElementwiseOp::ReLU => Ok(self.relu()),
                        ElementwiseOp::Sigmoid => Ok(self.sigmoid()),
                        ElementwiseOp::Tanh => Ok(self.tanh()),
                        ElementwiseOp::Add | ElementwiseOp::Mul => {
                            Err(GhostError::InvalidOperation("Binary op requires two tensors".to_string()))
                        }
                    }
                }
            }
            HardwareBackend::CUDA => {
                #[cfg(feature = "cuda")]
                {
                    match op {
                        ElementwiseOp::ReLU => crate::cuda::ops::relu_cuda(self, device.device_id),
                        _ => Err(GhostError::NotImplemented("CUDA elementwise op".to_string())),
                    }
                }
                #[cfg(not(feature = "cuda"))]
                {
                    Err(GhostError::DeviceError("CUDA not available".to_string()))
                }
            }
            HardwareBackend::ROCm => {
                #[cfg(feature = "rocm")]
                {
                    match op {
                        ElementwiseOp::ReLU => crate::rocm::ops::relu_rocm(self, device.device_id),
                        _ => Err(GhostError::NotImplemented("ROCm elementwise op".to_string())),
                    }
                }
                #[cfg(not(feature = "rocm"))]
                {
                    Err(GhostError::DeviceError("ROCm not available".to_string()))
                }
            }
            HardwareBackend::Metal => {
                #[cfg(feature = "metal")]
                {
                    match op {
                        ElementwiseOp::ReLU => crate::metal::mps::relu_mps(self, device.device_id),
                        _ => Err(GhostError::NotImplemented("Metal elementwise op".to_string())),
                    }
                }
                #[cfg(not(feature = "metal"))]
                {
                    Err(GhostError::DeviceError("Metal not available".to_string()))
                }
            }
            HardwareBackend::TPU => {
                Err(GhostError::NotImplemented("TPU elementwise ops".to_string()))
            }
        }
    }
}

// Placeholder implementations for hardware-specific operations
#[cfg(feature = "cuda")]
fn cuda_matmul(_a: &Tensor, _b: &Tensor, _device_id: usize) -> Result<Tensor> {
    Err(GhostError::NotImplemented("CUDA matmul".to_string()))
}

#[cfg(feature = "rocm")]
fn rocm_matmul(_a: &Tensor, _b: &Tensor, _device_id: usize) -> Result<Tensor> {
    Err(GhostError::NotImplemented("ROCm matmul".to_string()))
}

#[cfg(feature = "metal")]
fn metal_matmul(_a: &Tensor, _b: &Tensor, _device_id: usize) -> Result<Tensor> {
    Err(GhostError::NotImplemented("Metal matmul".to_string()))
}

#[cfg(feature = "tpu")]
fn tpu_matmul(_a: &Tensor, _b: &Tensor, _device_id: usize) -> Result<Tensor> {
    Err(GhostError::NotImplemented("TPU matmul".to_string()))
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_list_devices() {
        let devices = list_devices();
        assert!(!devices.is_empty());
        assert_eq!(devices[0].backend, HardwareBackend::CPU);
    }
    
    #[test]
    fn test_cpu_device() {
        let device = HardwareDevice::cpu();
        assert_eq!(device.backend, HardwareBackend::CPU);
        assert_eq!(device.device_id, 0);
    }
}