tenflowers-core 0.1.1

Core tensor operations and execution engine for TenfloweRS
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
use crate::{Device, Result, TensorError};
use std::collections::HashMap;
use std::sync::Arc;

/// Device-specific memory allocator trait
pub trait DeviceAllocator: Send + Sync {
    /// Allocate memory on the device
    fn allocate(&self, size: usize) -> Result<*mut u8>;

    /// Deallocate memory on the device
    ///
    /// # Safety
    /// The caller must ensure that:
    /// - The pointer was allocated by this allocator
    /// - The size matches the original allocation
    /// - The pointer is not used after deallocation
    unsafe fn deallocate(&self, ptr: *mut u8, size: usize);

    /// Copy memory from host to device
    fn copy_from_host(&self, dst: *mut u8, src: &[u8]) -> Result<()>;

    /// Copy memory from device to host
    fn copy_to_host(&self, dst: &mut [u8], src: *const u8) -> Result<()>;

    /// Copy memory within device
    fn copy_device_to_device(&self, dst: *mut u8, src: *const u8, size: usize) -> Result<()>;
}

/// Device-specific compute context
pub trait DeviceContext: Send + Sync {
    /// Get the device
    fn device(&self) -> &Device;

    /// Get the allocator for this device
    fn allocator(&self) -> &dyn DeviceAllocator;

    /// Synchronize device operations
    fn synchronize(&self) -> Result<()>;

    /// Create a compute stream/queue
    fn create_stream(&self) -> Result<Box<dyn DeviceStream>>;

    /// Get device properties
    fn properties(&self) -> DeviceProperties;
}

/// Device stream for async operations
pub trait DeviceStream: Send + Sync {
    /// Enqueue a kernel launch
    fn launch_kernel(&self, kernel: &dyn DeviceKernel, args: KernelArgs) -> Result<()>;

    /// Synchronize the stream
    fn synchronize(&self) -> Result<()>;

    /// Check if operations are complete
    fn is_complete(&self) -> bool;
}

/// Device kernel interface
pub trait DeviceKernel: Send + Sync {
    /// Get kernel name
    fn name(&self) -> &str;

    /// Get required shared memory size
    fn shared_memory_size(&self) -> usize;

    /// Get optimal block size
    fn optimal_block_size(&self) -> usize;
}

/// Kernel launch arguments
pub struct KernelArgs {
    pub grid_size: [u32; 3],
    pub block_size: [u32; 3],
    pub shared_memory: usize,
    pub inputs: Vec<*const u8>,
    pub outputs: Vec<*mut u8>,
    pub params: HashMap<String, KernelParam>,
}

/// Kernel parameter types
#[derive(Clone)]
pub enum KernelParam {
    I32(i32),
    U32(u32),
    F32(f32),
    F64(f64),
    Ptr(*const u8),
}

/// Device properties
#[derive(Debug, Clone)]
pub struct DeviceProperties {
    pub name: String,
    pub total_memory: usize,
    pub compute_capability: (u32, u32),
    pub max_threads_per_block: u32,
    pub max_blocks: [u32; 3],
    pub shared_memory_per_block: usize,
    pub warp_size: u32,
}

/// CPU device context implementation
pub struct CpuContext {
    allocator: CpuAllocator,
}

impl CpuContext {
    pub fn new() -> Self {
        Self {
            allocator: CpuAllocator,
        }
    }
}

impl Default for CpuContext {
    fn default() -> Self {
        Self::new()
    }
}

impl DeviceContext for CpuContext {
    fn device(&self) -> &Device {
        &Device::Cpu
    }

    fn allocator(&self) -> &dyn DeviceAllocator {
        &self.allocator
    }

    fn synchronize(&self) -> Result<()> {
        // CPU operations are synchronous
        Ok(())
    }

    fn create_stream(&self) -> Result<Box<dyn DeviceStream>> {
        Ok(Box::new(CpuStream))
    }

    fn properties(&self) -> DeviceProperties {
        DeviceProperties {
            name: "CPU".to_string(),
            total_memory: sys_info::mem_info()
                .map(|info| info.total as usize * 1024)
                .unwrap_or(0),
            compute_capability: (1, 0),
            max_threads_per_block: 1,
            max_blocks: [1, 1, 1],
            shared_memory_per_block: 0,
            warp_size: 1,
        }
    }
}

/// CPU allocator
struct CpuAllocator;

impl DeviceAllocator for CpuAllocator {
    fn allocate(&self, size: usize) -> Result<*mut u8> {
        if size == 0 {
            return Ok(std::ptr::null_mut());
        }

        let layout = std::alloc::Layout::from_size_align(size, 64)
            .map_err(|e| TensorError::allocation_error_simple(e.to_string()))?;

        let ptr = unsafe { std::alloc::alloc(layout) };
        if ptr.is_null() {
            Err(TensorError::allocation_error_simple(format!(
                "Failed to allocate {size} bytes"
            )))
        } else {
            Ok(ptr)
        }
    }

    unsafe fn deallocate(&self, ptr: *mut u8, size: usize) {
        if !ptr.is_null() && size > 0 {
            let layout = std::alloc::Layout::from_size_align_unchecked(size, 64);
            std::alloc::dealloc(ptr, layout);
        }
    }

    fn copy_from_host(&self, dst: *mut u8, src: &[u8]) -> Result<()> {
        unsafe {
            std::ptr::copy_nonoverlapping(src.as_ptr(), dst, src.len());
        }
        Ok(())
    }

    fn copy_to_host(&self, dst: &mut [u8], src: *const u8) -> Result<()> {
        unsafe {
            std::ptr::copy_nonoverlapping(src, dst.as_mut_ptr(), dst.len());
        }
        Ok(())
    }

    fn copy_device_to_device(&self, dst: *mut u8, src: *const u8, size: usize) -> Result<()> {
        unsafe {
            std::ptr::copy_nonoverlapping(src, dst, size);
        }
        Ok(())
    }
}

/// CPU stream (synchronous)
struct CpuStream;

impl DeviceStream for CpuStream {
    fn launch_kernel(&self, _kernel: &dyn DeviceKernel, _args: KernelArgs) -> Result<()> {
        // CPU kernels are executed directly, not through stream
        Ok(())
    }

    fn synchronize(&self) -> Result<()> {
        Ok(())
    }

    fn is_complete(&self) -> bool {
        true
    }
}

/// Device manager for context creation and caching
pub struct DeviceManager {
    contexts: std::sync::RwLock<HashMap<Device, Arc<dyn DeviceContext>>>,
}

impl DeviceManager {
    pub fn new() -> Self {
        Self {
            contexts: std::sync::RwLock::new(HashMap::new()),
        }
    }
}

impl Default for DeviceManager {
    fn default() -> Self {
        Self::new()
    }
}

impl DeviceManager {
    /// Get or create a device context
    pub fn get_context(&self, device: &Device) -> Result<Arc<dyn DeviceContext>> {
        // Check cache first
        {
            let contexts = self
                .contexts
                .read()
                .expect("read lock should not be poisoned");
            if let Some(ctx) = contexts.get(device) {
                return Ok(Arc::clone(ctx));
            }
        }

        // Create new context
        let context: Arc<dyn DeviceContext> = match device {
            Device::Cpu => Arc::new(CpuContext::new()),
            #[cfg(feature = "gpu")]
            Device::Gpu(id) => Arc::new(GpuContext::new(*id)?),
            #[cfg(feature = "rocm")]
            Device::Rocm(id) => Arc::new(GpuContext::new(*id)?), // Use same GPU context for ROCm
        };

        // Cache it
        {
            let mut contexts = self
                .contexts
                .write()
                .expect("write lock should not be poisoned");
            contexts.insert(*device, Arc::clone(&context));
        }

        Ok(context)
    }
}

// Global device manager
lazy_static::lazy_static! {
    pub static ref DEVICE_MANAGER: DeviceManager = DeviceManager::new();
}

/// GPU device context implementation
#[cfg(feature = "gpu")]
pub struct GpuContext {
    device: Arc<wgpu::Device>,
    queue: Arc<wgpu::Queue>,
    adapter: wgpu::Adapter,
    allocator: GpuAllocator,
    device_enum: Device,
}

#[cfg(feature = "gpu")]
impl GpuContext {
    pub fn new(device_id: usize) -> Result<Self> {
        pollster::block_on(Self::new_async(device_id))
    }

    async fn new_async(device_id: usize) -> Result<Self> {
        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());

        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await
            .map_err(|_e| {
                TensorError::device_error_simple("Failed to find suitable GPU adapter".to_string())
            })?;

        let (device, queue) = adapter
            .request_device(&wgpu::DeviceDescriptor {
                label: Some(&format!("TenfloweRS GPU Device {device_id}")),
                required_features: wgpu::Features::empty(),
                required_limits: wgpu::Limits::default(),
                memory_hints: wgpu::MemoryHints::Performance,
                experimental_features: wgpu::ExperimentalFeatures::default(),
                trace: wgpu::Trace::default(),
            })
            .await
            .map_err(|e| {
                TensorError::device_error_simple(format!("Failed to create GPU device: {e}"))
            })?;

        let device = Arc::new(device);
        let queue = Arc::new(queue);
        let allocator = GpuAllocator::new(Arc::clone(&device), Arc::clone(&queue));

        Ok(Self {
            device,
            queue,
            adapter,
            allocator,
            device_enum: Device::Gpu(device_id),
        })
    }
}

#[cfg(feature = "gpu")]
impl DeviceContext for GpuContext {
    fn device(&self) -> &Device {
        &self.device_enum
    }

    fn allocator(&self) -> &dyn DeviceAllocator {
        &self.allocator
    }

    fn synchronize(&self) -> Result<()> {
        self.device.poll(wgpu::PollType::wait_indefinitely()).ok();
        Ok(())
    }

    fn create_stream(&self) -> Result<Box<dyn DeviceStream>> {
        Ok(Box::new(GpuStream::new(
            Arc::clone(&self.device),
            Arc::clone(&self.queue),
        )))
    }

    fn properties(&self) -> DeviceProperties {
        let info = self.adapter.get_info();
        let limits = self.adapter.limits();

        DeviceProperties {
            name: info.name,
            total_memory: 0,            // WGPU doesn't expose memory info
            compute_capability: (1, 0), // Generic capability
            max_threads_per_block: limits.max_compute_workgroup_size_x,
            max_blocks: [
                limits.max_compute_workgroups_per_dimension,
                limits.max_compute_workgroups_per_dimension,
                limits.max_compute_workgroups_per_dimension,
            ],
            shared_memory_per_block: limits.max_compute_workgroup_storage_size as usize,
            warp_size: 32, // Common warp size
        }
    }
}

/// GPU allocator implementation
#[cfg(feature = "gpu")]
struct GpuAllocator {
    device: Arc<wgpu::Device>,
    queue: Arc<wgpu::Queue>,
}

#[cfg(feature = "gpu")]
impl GpuAllocator {
    fn new(device: Arc<wgpu::Device>, queue: Arc<wgpu::Queue>) -> Self {
        Self { device, queue }
    }
}

#[cfg(feature = "gpu")]
impl DeviceAllocator for GpuAllocator {
    fn allocate(&self, size: usize) -> Result<*mut u8> {
        // WGPU doesn't expose raw pointers, return a placeholder
        // Real GPU allocation happens through wgpu::Buffer
        Ok(size as *mut u8) // Placeholder pointer
    }

    unsafe fn deallocate(&self, _ptr: *mut u8, _size: usize) {
        // GPU buffers are managed by WGPU automatically
    }

    fn copy_from_host(&self, _dst: *mut u8, _src: &[u8]) -> Result<()> {
        // GPU memory operations are handled by buffer operations
        Ok(())
    }

    fn copy_to_host(&self, _dst: &mut [u8], _src: *const u8) -> Result<()> {
        // GPU memory operations are handled by buffer operations
        Ok(())
    }

    fn copy_device_to_device(&self, _dst: *mut u8, _src: *const u8, _size: usize) -> Result<()> {
        // GPU memory operations are handled by buffer operations
        Ok(())
    }
}

/// GPU stream implementation
#[cfg(feature = "gpu")]
struct GpuStream {
    device: Arc<wgpu::Device>,
    queue: Arc<wgpu::Queue>,
}

#[cfg(feature = "gpu")]
impl GpuStream {
    fn new(device: Arc<wgpu::Device>, queue: Arc<wgpu::Queue>) -> Self {
        Self { device, queue }
    }
}

#[cfg(feature = "gpu")]
impl DeviceStream for GpuStream {
    fn launch_kernel(&self, _kernel: &dyn DeviceKernel, _args: KernelArgs) -> Result<()> {
        // Kernel execution will be implemented with compute shaders
        Ok(())
    }

    fn synchronize(&self) -> Result<()> {
        self.device.poll(wgpu::PollType::wait_indefinitely()).ok();
        Ok(())
    }

    fn is_complete(&self) -> bool {
        // For WGPU, operations are generally synchronous
        true
    }
}

/// Helper function to get GPU context with backend selection
#[cfg(feature = "gpu")]
pub fn get_gpu_context(device_id: usize) -> Result<GpuContextInfo> {
    let device = Device::Gpu(device_id);
    let ctx = DEVICE_MANAGER.get_context(&device)?;

    // Create a new GPU context if needed (simple approach)
    let gpu_ctx = GpuContext::new(device_id)?;
    Ok(GpuContextInfo {
        device: gpu_ctx.device.clone(),
        queue: gpu_ctx.queue.clone(),
    })
}

/// Enhanced GPU context with backend selection capabilities
#[cfg(any(feature = "gpu", feature = "cudnn"))]
pub fn get_enhanced_gpu_context(device_id: usize) -> Result<EnhancedGpuContext> {
    // Check if cuDNN is available and requested
    #[cfg(all(feature = "cudnn", any(target_os = "linux", target_os = "windows")))]
    {
        if crate::gpu::cudnn::CudnnContext::is_available() {
            let mut cudnn_ctx = crate::gpu::cudnn::global_cudnn_context();
            let cudnn_handle = cudnn_ctx.get_handle(device_id)?;

            // Also get WGPU context for fallback
            let wgpu_ctx = get_gpu_context(device_id)?;

            return Ok(EnhancedGpuContext {
                device_id,
                backend: GpuBackend::CuDNN(cudnn_handle),
                wgpu_fallback: Some(wgpu_ctx),
            });
        }
    }

    // Fall back to WGPU
    #[cfg(feature = "gpu")]
    {
        let wgpu_ctx = get_gpu_context(device_id)?;
        Ok(EnhancedGpuContext {
            device_id,
            backend: GpuBackend::WGPU(wgpu_ctx),
            wgpu_fallback: None,
        })
    }
    #[cfg(not(feature = "gpu"))]
    {
        Err(TensorError::unsupported_operation_simple(
            "No GPU backend available",
        ))
    }
}

#[cfg(feature = "gpu")]
#[derive(Debug, Clone)]
pub struct GpuContextInfo {
    pub device: Arc<wgpu::Device>,
    pub queue: Arc<wgpu::Queue>,
}

/// GPU backend selection enum
#[cfg(any(feature = "gpu", feature = "cudnn"))]
#[derive(Debug, Clone)]
pub enum GpuBackend {
    #[cfg(feature = "gpu")]
    WGPU(GpuContextInfo),
    #[cfg(all(feature = "cudnn", any(target_os = "linux", target_os = "windows")))]
    CuDNN(Arc<crate::gpu::cudnn::CudnnHandle>),
}

/// Enhanced GPU context with backend selection capabilities
#[cfg(any(feature = "gpu", feature = "cudnn"))]
#[derive(Debug, Clone)]
pub struct EnhancedGpuContext {
    pub device_id: usize,
    pub backend: GpuBackend,
    pub wgpu_fallback: Option<GpuContextInfo>,
}

#[cfg(any(feature = "gpu", feature = "cudnn"))]
impl EnhancedGpuContext {
    /// Check if cuDNN backend is being used
    #[cfg(all(feature = "cudnn", any(target_os = "linux", target_os = "windows")))]
    pub fn is_cudnn(&self) -> bool {
        matches!(self.backend, GpuBackend::CuDNN(_))
    }

    /// Check if WGPU backend is being used
    #[cfg(feature = "gpu")]
    pub fn is_wgpu(&self) -> bool {
        matches!(self.backend, GpuBackend::WGPU(_))
    }

    /// Get cuDNN handle if available
    #[cfg(all(feature = "cudnn", any(target_os = "linux", target_os = "windows")))]
    pub fn get_cudnn_handle(&self) -> Option<&Arc<crate::gpu::cudnn::CudnnHandle>> {
        match &self.backend {
            GpuBackend::CuDNN(handle) => Some(handle),
            _ => None,
        }
    }

    /// Get WGPU context if available
    #[cfg(feature = "gpu")]
    pub fn get_wgpu_context(&self) -> Option<&GpuContextInfo> {
        match &self.backend {
            GpuBackend::WGPU(ctx) => Some(ctx),
            #[cfg(all(feature = "cudnn", any(target_os = "linux", target_os = "windows")))]
            _ => self.wgpu_fallback.as_ref(),
        }
    }

    /// Get device ID
    pub fn device_id(&self) -> usize {
        self.device_id
    }
}

// Add sys-info dependency detection
#[cfg(not(target_arch = "wasm32"))]
extern crate sys_info;

#[cfg(target_arch = "wasm32")]
mod sys_info {
    pub struct MemInfo {
        pub total: u64,
    }

    pub fn mem_info() -> Result<MemInfo, &'static str> {
        Ok(MemInfo {
            total: 1024 * 1024 * 1024,
        }) // 1GB default for WASM
    }
}