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
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
use crate::{buffer::TensorBuffer, Device, Result, TensorError};
use scirs2_core::ndarray::ArrayD;
use std::sync::Arc;
use wgpu::util::DeviceExt;

#[cfg(feature = "gpu")]
use crate::gpu::memory_tracing::{AllocationId, GLOBAL_GPU_MEMORY_TRACKER};

/// A GPU buffer that stores tensor data on the GPU device
#[cfg(feature = "gpu")]
#[derive(Debug)]
pub struct GpuBuffer<T> {
    buffer: Arc<wgpu::Buffer>,
    pub device: Arc<wgpu::Device>,
    pub queue: Arc<wgpu::Queue>,
    device_enum: Device,
    len: usize,
    is_pinned: bool,
    #[cfg(feature = "gpu")]
    allocation_id: Option<AllocationId>,
    _phantom: std::marker::PhantomData<T>,
}

/// A view into a GPU buffer that references a portion of the original buffer
pub struct GpuBufferView<T> {
    parent_buffer: Arc<GpuBuffer<T>>,
    offset: usize,
    len: usize,
    device_enum: Device,
    _phantom: std::marker::PhantomData<T>,
}

#[cfg(feature = "gpu")]
impl<T: bytemuck::Pod + bytemuck::Zeroable + Clone + Send + Sync + 'static> TensorBuffer
    for GpuBufferView<T>
{
    type Elem = T;

    fn device(&self) -> &Device {
        &self.device_enum
    }

    fn len(&self) -> usize {
        self.len
    }

    fn size_bytes(&self) -> usize {
        self.len * std::mem::size_of::<T>()
    }

    fn view(&self, offset: usize, len: usize) -> Result<Box<dyn TensorBuffer<Elem = T>>> {
        if offset + len > self.len {
            return Err(TensorError::invalid_operation_simple(format!(
                "View out of bounds: {}+{} > {}",
                offset, len, self.len
            )));
        }
        Ok(Box::new(GpuBufferView::new(
            Arc::clone(&self.parent_buffer),
            self.offset + offset,
            len,
        )?))
    }

    fn to_cpu(&self) -> Result<Vec<T>> {
        self.parent_buffer.to_cpu()
    }

    unsafe fn as_ptr(&self) -> *const T {
        // GPU buffers don't have CPU-accessible pointers
        std::ptr::null()
    }

    unsafe fn as_mut_ptr(&mut self) -> *mut T {
        // GPU buffers don't have CPU-accessible pointers
        std::ptr::null_mut()
    }

    fn clone_buffer(&self) -> Result<Box<dyn TensorBuffer<Elem = T>>> {
        Ok(Box::new(self.clone()))
    }
}

impl<T: bytemuck::Pod + bytemuck::Zeroable + Clone + Send + Sync + 'static> GpuBufferView<T> {
    pub fn new(parent: Arc<GpuBuffer<T>>, offset: usize, len: usize) -> Result<Self> {
        if offset + len > parent.len() {
            return Err(TensorError::invalid_operation_simple(format!(
                "Buffer view out of bounds: {}+{} > {}",
                offset,
                len,
                parent.len()
            )));
        }
        Ok(Self {
            device_enum: parent.device_enum.clone(),
            parent_buffer: parent,
            offset,
            len,
            _phantom: std::marker::PhantomData,
        })
    }

    #[inline]
    pub fn parent(&self) -> &GpuBuffer<T> {
        &self.parent_buffer
    }

    #[inline]
    pub fn offset(&self) -> usize {
        self.offset
    }

    #[inline]
    pub fn buffer(&self) -> &wgpu::Buffer {
        &self.parent_buffer.buffer
    }

    #[inline]
    pub fn device(&self) -> &wgpu::Device {
        &self.parent_buffer.device
    }

    #[inline]
    pub fn queue(&self) -> &wgpu::Queue {
        &self.parent_buffer.queue
    }

    pub fn to_cpu_array(&self) -> Result<ArrayD<T>> {
        // For now, we need to copy the entire parent buffer and then slice
        // In the future, we could optimize this to only copy the view portion
        let full_data = self.parent_buffer.to_cpu()?;
        let view_data = full_data[self.offset..self.offset + self.len].to_vec();
        Ok(scirs2_core::ndarray::Array1::from(view_data).into_dyn())
    }
}

impl<T> Clone for GpuBufferView<T> {
    fn clone(&self) -> Self {
        Self {
            parent_buffer: Arc::clone(&self.parent_buffer),
            offset: self.offset,
            len: self.len,
            device_enum: self.device_enum.clone(),
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T> Clone for GpuBuffer<T> {
    fn clone(&self) -> Self {
        Self {
            buffer: Arc::clone(&self.buffer),
            device: Arc::clone(&self.device),
            queue: Arc::clone(&self.queue),
            device_enum: self.device_enum.clone(),
            len: self.len,
            is_pinned: self.is_pinned,
            #[cfg(feature = "gpu")]
            allocation_id: self.allocation_id,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T: bytemuck::Pod + bytemuck::Zeroable + Clone + Send + Sync + 'static> GpuBuffer<T> {
    /// Track a new GPU allocation
    #[cfg(feature = "gpu")]
    fn track_allocation(
        size_bytes: usize,
        device_id: usize,
        operation: &str,
    ) -> Option<AllocationId> {
        if let Ok(mut tracker) = GLOBAL_GPU_MEMORY_TRACKER.lock() {
            Some(tracker.track_allocation(
                size_bytes,
                device_id,
                operation.to_string(),
                None, // shape
                Some(std::any::type_name::<T>().to_string()),
            ))
        } else {
            None
        }
    }

    /// Get the allocation ID for this buffer
    #[cfg(feature = "gpu")]
    pub fn allocation_id(&self) -> Option<AllocationId> {
        self.allocation_id
    }

    /// Create a new GPU buffer filled with zeros
    pub fn zeros(len: usize, device_id: usize) -> Result<Self> {
        use wgpu::util::DeviceExt;

        // Get the global GPU context
        let context = crate::gpu::GpuContext::global()?;
        let device = &context.device;
        let queue = &context.queue;

        // Create a buffer filled with zeros
        let zeros_data = vec![T::zeroed(); len];
        let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("gpu_buffer_zeros"),
            contents: bytemuck::cast_slice(&zeros_data),
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
        });

        #[cfg(feature = "gpu")]
        let allocation_id =
            Self::track_allocation(len * std::mem::size_of::<T>(), device_id, "zeros");

        Ok(Self {
            buffer: Arc::new(buffer),
            device: Arc::clone(&context.device),
            queue: Arc::clone(&context.queue),
            device_enum: Device::Gpu(device_id),
            len,
            is_pinned: false,
            #[cfg(feature = "gpu")]
            allocation_id,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Create a GpuBuffer from an existing wgpu::Buffer
    pub fn from_wgpu_buffer(
        buffer: wgpu::Buffer,
        device: Arc<wgpu::Device>,
        queue: Arc<wgpu::Queue>,
        device_enum: Device,
        len: usize,
    ) -> Self {
        #[cfg(feature = "gpu")]
        let allocation_id = if let Device::Gpu(device_id) = device_enum {
            Self::track_allocation(
                len * std::mem::size_of::<T>(),
                device_id,
                "from_wgpu_buffer",
            )
        } else {
            None
        };

        Self {
            buffer: Arc::new(buffer),
            device,
            queue,
            device_enum,
            len,
            is_pinned: false,
            #[cfg(feature = "gpu")]
            allocation_id,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Create a GpuBuffer from an existing raw wgpu::Buffer (alias for from_wgpu_buffer)
    pub fn from_raw_buffer(
        buffer: wgpu::Buffer,
        device: Arc<wgpu::Device>,
        queue: Arc<wgpu::Queue>,
        device_enum: Device,
        len: usize,
    ) -> Self {
        Self::from_wgpu_buffer(buffer, device, queue, device_enum, len)
    }

    /// Create a GpuBuffer from an Arc<wgpu::Buffer>
    pub fn from_shared_buffer(
        buffer: Arc<wgpu::Buffer>,
        device: Arc<wgpu::Device>,
        queue: Arc<wgpu::Queue>,
        device_enum: Device,
        len: usize,
    ) -> Self {
        #[cfg(feature = "gpu")]
        let allocation_id = if let Device::Gpu(device_id) = device_enum {
            Self::track_allocation(
                len * std::mem::size_of::<T>(),
                device_id,
                "from_shared_buffer",
            )
        } else {
            None
        };

        Self {
            buffer,
            device,
            queue,
            device_enum,
            len,
            is_pinned: false,
            #[cfg(feature = "gpu")]
            allocation_id,
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn from_cpu_array(array: &ArrayD<T>, device_id: usize) -> Result<Self> {
        use wgpu::util::DeviceExt;

        // Get the global GPU context
        let context = crate::gpu::GpuContext::global()?;
        let device = &context.device;
        let queue = &context.queue;

        // Convert array to slice - handle both contiguous and non-contiguous arrays
        let slice = if array.is_standard_layout() {
            array.as_slice().ok_or_else(|| {
                TensorError::invalid_operation_simple(
                    "Cannot convert non-contiguous array to slice".to_string(),
                )
            })?
        } else {
            // For non-contiguous arrays, we need to collect into a vector first
            let data: Vec<T> = array.iter().cloned().collect();
            return Self::from_slice(&data, &Device::Gpu(device_id));
        };

        // Create GPU buffer from slice
        let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("gpu_buffer_from_cpu_array"),
            contents: bytemuck::cast_slice(slice),
            usage: wgpu::BufferUsages::STORAGE
                | wgpu::BufferUsages::COPY_DST
                | wgpu::BufferUsages::COPY_SRC,
        });

        #[cfg(feature = "gpu")]
        let allocation_id = Self::track_allocation(
            array.len() * std::mem::size_of::<T>(),
            device_id,
            "from_cpu_array",
        );

        Ok(Self {
            buffer: Arc::new(buffer),
            device: Arc::clone(&context.device),
            queue: Arc::clone(&context.queue),
            device_enum: Device::Gpu(device_id),
            len: array.len(),
            is_pinned: false,
            #[cfg(feature = "gpu")]
            allocation_id,
            _phantom: std::marker::PhantomData,
        })
    }

    pub fn to_cpu_array(&self) -> Result<ArrayD<T>> {
        let data = self.to_cpu()?;
        Ok(scirs2_core::ndarray::Array1::from(data).into_dyn())
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn size_bytes(&self) -> usize {
        self.len * std::mem::size_of::<T>()
    }

    pub fn from_slice(slice: &[T], device: &Device) -> Result<Self> {
        match device {
            Device::Gpu(device_id) => {
                // Get the global GPU context
                let context = crate::gpu::GpuContext::global()?;
                let gpu_device = &context.device;
                let queue = &context.queue;

                let buffer = gpu_device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
                    label: Some("tensor_buffer_from_slice"),
                    contents: bytemuck::cast_slice(slice),
                    usage: wgpu::BufferUsages::STORAGE
                        | wgpu::BufferUsages::COPY_DST
                        | wgpu::BufferUsages::COPY_SRC,
                });

                #[cfg(feature = "gpu")]
                let allocation_id =
                    Self::track_allocation(std::mem::size_of_val(slice), *device_id, "from_slice");

                Ok(Self {
                    buffer: Arc::new(buffer),
                    device: Arc::clone(&context.device),
                    queue: Arc::clone(&context.queue),
                    device_enum: device.clone(),
                    len: slice.len(),
                    is_pinned: false,
                    #[cfg(feature = "gpu")]
                    allocation_id,
                    _phantom: std::marker::PhantomData,
                })
            }
            _ => Err(TensorError::invalid_operation_simple(
                "Expected GPU device".to_string(),
            )),
        }
    }

    pub fn transfer_to_device(&self, target_device: &Device) -> Result<Self> {
        match target_device {
            Device::Gpu(_device_id) => {
                // Transfer data via CPU for now - could be optimized for peer-to-peer transfer
                let cpu_data = self.to_cpu()?;
                Self::from_slice(&cpu_data, target_device)
            }
            _ => Err(TensorError::invalid_operation_simple(
                "Expected GPU device".to_string(),
            )),
        }
    }

    pub fn to_cpu(&self) -> Result<Vec<T>> {
        // Create a staging buffer for reading data back
        let staging_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("staging_buffer"),
            size: self.size_bytes() as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Copy from GPU buffer to staging buffer
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("gpu_to_cpu_encoder"),
            });
        encoder.copy_buffer_to_buffer(
            &self.buffer,
            0,
            &staging_buffer,
            0,
            self.size_bytes() as u64,
        );
        self.queue.submit(Some(encoder.finish()));

        // Map and read the staging buffer
        let buffer_slice = staging_buffer.slice(..);
        let (sender, receiver) = futures::channel::oneshot::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });

        self.device.poll(wgpu::PollType::wait_indefinitely()).ok();

        match futures::executor::block_on(receiver) {
            Ok(Ok(())) => {
                let data = buffer_slice.get_mapped_range();
                let result = bytemuck::cast_slice(&data).to_vec();
                drop(data);
                staging_buffer.unmap();
                Ok(result)
            }
            _ => Err(TensorError::invalid_operation_simple(
                "Failed to read GPU buffer".to_string(),
            )),
        }
    }

    pub fn buffer(&self) -> &wgpu::Buffer {
        &self.buffer
    }

    pub fn buffer_arc(&self) -> Arc<wgpu::Buffer> {
        Arc::clone(&self.buffer)
    }

    pub fn device_enum(&self) -> Device {
        self.device_enum.clone()
    }

    pub fn device(&self) -> &wgpu::Device {
        &self.device
    }

    #[inline]
    pub fn queue(&self) -> &wgpu::Queue {
        &self.queue
    }

    #[inline]
    pub fn is_pinned(&self) -> bool {
        self.is_pinned
    }

    pub fn from_cpu_array_pinned(array: &ArrayD<T>, device_id: usize) -> Result<Self> {
        let mut buffer = Self::from_cpu_array(array, device_id)?;
        buffer.is_pinned = true;
        Ok(buffer)
    }

    pub fn zeros_pinned(len: usize, device_id: usize) -> Result<Self> {
        let mut buffer = Self::zeros(len, device_id)?;
        buffer.is_pinned = true;
        Ok(buffer)
    }
}

// Implement Drop to track GPU buffer frees
#[cfg(feature = "gpu")]
impl<T> Drop for GpuBuffer<T> {
    fn drop(&mut self) {
        // Only track the free if this is the last reference to the buffer
        if Arc::strong_count(&self.buffer) == 1 {
            if let Some(alloc_id) = self.allocation_id {
                if let Ok(mut tracker) = GLOBAL_GPU_MEMORY_TRACKER.lock() {
                    tracker.track_free(alloc_id);
                }
            }
        }
    }
}

#[cfg(feature = "gpu")]
impl<T: bytemuck::Pod + bytemuck::Zeroable + Clone + Send + Sync + 'static> TensorBuffer
    for GpuBuffer<T>
{
    type Elem = T;

    fn device(&self) -> &Device {
        &self.device_enum
    }

    fn len(&self) -> usize {
        self.len
    }

    fn size_bytes(&self) -> usize {
        self.len * std::mem::size_of::<T>()
    }

    fn view(&self, offset: usize, len: usize) -> Result<Box<dyn TensorBuffer<Elem = T>>> {
        if offset + len > self.len {
            return Err(TensorError::invalid_operation_simple(format!(
                "View out of bounds: {}+{} > {}",
                offset, len, self.len
            )));
        }
        Ok(Box::new(GpuBufferView::new(
            Arc::new(self.clone()),
            offset,
            len,
        )?))
    }

    fn to_cpu(&self) -> Result<Vec<T>> {
        // Call the actual GpuBuffer::to_cpu method
        GpuBuffer::to_cpu(self)
    }

    unsafe fn as_ptr(&self) -> *const T {
        // GPU buffers don't have CPU-accessible pointers
        std::ptr::null()
    }

    unsafe fn as_mut_ptr(&mut self) -> *mut T {
        // GPU buffers don't have CPU-accessible pointers
        std::ptr::null_mut()
    }

    fn clone_buffer(&self) -> Result<Box<dyn TensorBuffer<Elem = T>>> {
        Ok(Box::new(self.clone()))
    }
}
/// Trait for GPU buffer operations
pub trait GpuBufferOps<T> {
    fn create_buffer(&self, size: usize) -> Result<GpuBuffer<T>>;
    fn copy_from_host(&self, data: &[T]) -> Result<GpuBuffer<T>>;
    fn copy_to_host(&self, buffer: &GpuBuffer<T>) -> Result<Vec<T>>;
}

/// Manager for GPU buffer allocation and memory management
pub struct BufferManager {
    device: Arc<wgpu::Device>,
    queue: Arc<wgpu::Queue>,
}

impl BufferManager {
    pub fn new(device: Arc<wgpu::Device>, queue: Arc<wgpu::Queue>) -> Self {
        Self { device, queue }
    }

    pub fn allocate<T>(&self, size: usize) -> Result<GpuBuffer<T>>
    where
        T: bytemuck::Pod + bytemuck::Zeroable + Clone + Send + Sync + 'static,
    {
        GpuBuffer::zeros(size, 0)
    }
}

#[cfg(all(test, feature = "gpu"))]
mod tests {
    use super::*;
    use crate::gpu::memory_tracing::{current_gpu_memory_usage, GLOBAL_GPU_MEMORY_TRACKER};

    #[test]
    fn test_gpu_buffer_memory_tracking() {
        // Reset tracker state
        if let Ok(mut tracker) = GLOBAL_GPU_MEMORY_TRACKER.lock() {
            tracker.reset();
        }

        // Check initial usage
        let initial_usage = current_gpu_memory_usage();

        // Create a GPU buffer
        let buffer = GpuBuffer::<f32>::zeros(1024, 0);

        // Verify allocation was tracked if buffer creation succeeded
        if let Ok(buf) = buffer {
            let usage_after_alloc = current_gpu_memory_usage();
            assert!(
                usage_after_alloc >= initial_usage,
                "Memory usage should increase after allocation"
            );

            // Verify allocation ID was assigned
            assert!(
                buf.allocation_id().is_some(),
                "Buffer should have an allocation ID"
            );

            // Drop the buffer and check that memory is freed
            drop(buf);

            // Note: Memory might not be immediately freed due to Arc references
            // So we just verify the tracking system is working
            let final_usage = current_gpu_memory_usage();
            assert!(
                final_usage <= usage_after_alloc,
                "Memory usage should not increase after dropping"
            );
        }
    }
}