numr 0.5.2

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
//! WebGPU Client implementation.
//!
//! `WgpuClient` owns the WebGPU device and queue for operation dispatch.
//!
//! # Thread Safety
//!
//! `WgpuClient` is `Clone` and the underlying wgpu::Device and wgpu::Queue
//! are already `Send + Sync` by design.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use wgpu::{Buffer, BufferDescriptor, BufferUsages, Device, Queue};

use super::WgpuRuntime;
use super::device::{WgpuDevice, WgpuError, query_adapter_info_blocking};
use super::shaders::PipelineCache;
use crate::runtime::{Allocator, RuntimeClient};

// ============================================================================
// WgpuClient
// ============================================================================

/// WebGPU Runtime Client.
///
/// Owns WebGPU device and queue for operation dispatch.
/// All tensor operations are submitted through this client's queue.
///
/// # Buffer Management
///
/// The client uses storage buffers for tensor data and staging buffers
/// for CPU<->GPU transfers. Buffers are created on-demand and can be
/// reused through the allocator.
#[derive(Clone)]
pub struct WgpuClient {
    /// GPU device identifier
    pub(crate) device_id: WgpuDevice,

    /// WebGPU device handle
    pub(crate) wgpu_device: Arc<Device>,

    /// WebGPU queue for command submission
    pub(crate) queue: Arc<Queue>,

    /// Allocator for buffer management
    pub(crate) allocator: WgpuAllocator,

    /// Raw handle for custom kernel access
    pub(crate) raw_handle: WgpuRawHandle,

    /// Pipeline cache for compute shaders
    pub(crate) pipeline_cache: Arc<PipelineCache>,
}

impl std::fmt::Debug for WgpuClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WgpuClient")
            .field("device", &self.device_id)
            .finish_non_exhaustive()
    }
}

impl WgpuClient {
    /// Create or retrieve a cached WebGPU client for a device.
    ///
    /// This returns the cached client for the given device index, creating one
    /// if it doesn't exist yet. All clients for the same device index share
    /// the same underlying `wgpu::Device` and queue, which is required because
    /// wgpu buffers are device-specific and cannot be used across devices.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - No suitable GPU adapter is found
    /// - Device creation fails
    pub fn new(device: WgpuDevice) -> Result<Self, WgpuError> {
        super::cache::get_or_create_client(&device)
    }

    /// Internal: create a fresh WebGPU client (not cached).
    ///
    /// This is called by the cache layer. External users should call `new()`
    /// which goes through the cache to ensure a single `wgpu::Device` per
    /// device index.
    pub(super) fn new_uncached(device: WgpuDevice) -> Result<Self, WgpuError> {
        let (adapter, info) = query_adapter_info_blocking(device.index)?;

        // Request device with compute features
        let (wgpu_device, queue) = pollster::block_on(async {
            let features = adapter.features();
            let required_features = if features.contains(wgpu::Features::SUBGROUP) {
                wgpu::Features::SUBGROUP
            } else {
                wgpu::Features::empty()
            };

            adapter
                .request_device(&wgpu::DeviceDescriptor {
                    label: Some("numr WebGPU Device"),
                    required_features,
                    required_limits: wgpu::Limits::default(),
                    memory_hints: wgpu::MemoryHints::Performance,
                    trace: wgpu::Trace::Off,
                    experimental_features: wgpu::ExperimentalFeatures::default(),
                })
                .await
        })
        .map_err(|e| WgpuError::DeviceError(format!("{:?}", e)))?;

        let wgpu_device = Arc::new(wgpu_device);
        let queue = Arc::new(queue);

        let allocator = WgpuAllocator {
            device: wgpu_device.clone(),
            queue: queue.clone(),
        };

        let raw_handle = WgpuRawHandle {
            device: wgpu_device.clone(),
            queue: queue.clone(),
        };

        let pipeline_cache = Arc::new(PipelineCache::new(wgpu_device.clone(), queue.clone()));

        let device_with_info = WgpuDevice::with_info(device.index, info);

        Ok(Self {
            device_id: device_with_info,
            wgpu_device,
            queue,
            allocator,
            raw_handle,
            pipeline_cache,
        })
    }

    /// Get reference to the WebGPU device.
    #[inline]
    pub fn wgpu_device(&self) -> &Device {
        &self.wgpu_device
    }

    /// Get Arc-wrapped WebGPU device.
    #[inline]
    pub fn wgpu_device_arc(&self) -> &Arc<Device> {
        &self.wgpu_device
    }

    /// Get reference to the WebGPU queue.
    #[inline]
    pub fn wgpu_queue(&self) -> &Queue {
        &self.queue
    }

    /// Get reference to the pipeline cache.
    #[inline]
    pub fn pipeline_cache(&self) -> &PipelineCache {
        &self.pipeline_cache
    }

    /// Create a storage buffer for tensor data.
    ///
    /// Storage buffers can be read/written by compute shaders.
    pub fn create_storage_buffer(&self, label: &str, size: u64) -> Buffer {
        self.wgpu_device.create_buffer(&BufferDescriptor {
            label: Some(label),
            size,
            usage: BufferUsages::STORAGE | BufferUsages::COPY_DST | BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        })
    }

    /// Create a staging buffer for CPU readback.
    ///
    /// Staging buffers are used to copy data from GPU to CPU.
    pub fn create_staging_buffer(&self, label: &str, size: u64) -> Buffer {
        self.wgpu_device.create_buffer(&BufferDescriptor {
            label: Some(label),
            size,
            usage: BufferUsages::MAP_READ | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        })
    }

    /// Create a uniform buffer for shader parameters.
    pub fn create_uniform_buffer(&self, label: &str, size: u64) -> Buffer {
        self.wgpu_device.create_buffer(&BufferDescriptor {
            label: Some(label),
            size,
            usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        })
    }

    /// Write data to a buffer.
    pub fn write_buffer<T: bytemuck::Pod>(&self, buffer: &Buffer, data: &[T]) {
        self.queue
            .write_buffer(buffer, 0, bytemuck::cast_slice(data));
    }

    /// Submit commands and wait for completion.
    pub fn submit_and_wait(&self, encoder: wgpu::CommandEncoder) {
        let submission = self.queue.submit(std::iter::once(encoder.finish()));
        let _ = self.wgpu_device.poll(wgpu::PollType::Wait {
            submission_index: Some(submission),
            timeout: Some(Duration::from_secs(60)),
        });
    }

    /// Read buffer data back to CPU (blocking).
    pub fn read_buffer<T: bytemuck::Pod>(
        &self,
        staging: &Buffer,
        output: &mut [T],
    ) -> crate::error::Result<()> {
        let slice = staging.slice(..);

        let (sender, receiver) = std::sync::mpsc::channel();
        slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });

        self.wgpu_device
            .poll(wgpu::PollType::Wait {
                submission_index: None,
                timeout: Some(Duration::from_secs(60)),
            })
            .map_err(|e| {
                crate::error::Error::Backend(format!("GPU poll failed during buffer read: {e}"))
            })?;

        // Check map_async result
        let map_result = receiver.recv().map_err(|_| {
            crate::error::Error::Backend(
                "map_async callback was not invoked during buffer read".into(),
            )
        })?;
        map_result.map_err(|e| {
            crate::error::Error::Backend(format!("map_async failed during buffer read: {e}"))
        })?;

        {
            let data = slice.get_mapped_range();
            let src: &[T] = bytemuck::cast_slice(&data);
            output.copy_from_slice(&src[..output.len()]);
        }

        staging.unmap();
        Ok(())
    }
}

impl RuntimeClient<WgpuRuntime> for WgpuClient {
    fn device(&self) -> &WgpuDevice {
        &self.device_id
    }

    fn synchronize(&self) {
        // Wait for all GPU work to complete
        let _ = self.wgpu_device.poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: Some(Duration::from_secs(60)),
        });
    }

    fn allocator(&self) -> &WgpuAllocator {
        &self.allocator
    }
}

// ============================================================================
// WgpuAllocator
// ============================================================================

/// WebGPU buffer allocator.
///
/// Allocates storage buffers for tensor data. Buffers are identified by
/// their GPU address (buffer ID cast to u64).
///
/// # Implementation Note
///
/// WebGPU doesn't expose raw pointers like CUDA. Instead, we use a buffer
/// registry that maps allocation IDs to wgpu::Buffer objects.
#[derive(Clone)]
pub struct WgpuAllocator {
    device: Arc<Device>,
    #[allow(dead_code)] // Reserved for future compute shader allocations
    queue: Arc<Queue>,
}

/// Global buffer registry for mapping allocation IDs to buffers.
///
/// This is necessary because WebGPU doesn't expose raw GPU pointers.
/// We use a global registry to map u64 IDs to Buffer objects.
static BUFFER_REGISTRY: std::sync::OnceLock<parking_lot::Mutex<HashMap<u64, Arc<Buffer>>>> =
    std::sync::OnceLock::new();

/// Counter for generating unique buffer IDs.
static BUFFER_ID_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);

fn get_buffer_registry() -> &'static parking_lot::Mutex<HashMap<u64, Arc<Buffer>>> {
    BUFFER_REGISTRY.get_or_init(|| parking_lot::Mutex::new(HashMap::new()))
}

/// Get a buffer by its ID.
pub fn get_buffer(id: u64) -> Option<Arc<Buffer>> {
    if id == 0 {
        return None;
    }
    let registry = get_buffer_registry();
    let guard = registry.lock();
    guard.get(&id).cloned()
}

impl Allocator for WgpuAllocator {
    /// Allocate a storage buffer and return its ID.
    ///
    /// The returned ID can be used with `get_buffer()` to retrieve the
    /// actual wgpu::Buffer for operations.
    fn allocate(&self, size_bytes: usize) -> crate::error::Result<u64> {
        if size_bytes == 0 {
            return Ok(0);
        }

        // WebGPU requires buffer sizes to be aligned to 4 bytes
        let aligned_size = size_bytes.div_ceil(4) * 4;

        let buffer = self.device.create_buffer(&BufferDescriptor {
            label: Some("numr tensor buffer"),
            size: aligned_size as u64,
            usage: BufferUsages::STORAGE | BufferUsages::COPY_DST | BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        // Generate unique ID and register buffer
        let id = BUFFER_ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let registry = get_buffer_registry();
        let mut guard = registry.lock();
        guard.insert(id, Arc::new(buffer));

        Ok(id)
    }

    fn deallocate(&self, ptr: u64, _size_bytes: usize) {
        if ptr == 0 {
            return;
        }

        // Remove buffer from registry (drop will release GPU memory)
        let registry = get_buffer_registry();
        let mut guard = registry.lock();
        guard.remove(&ptr);
    }

    fn is_frozen(&self) -> bool {
        false // WebGPU allocator doesn't support freeze
    }

    fn freeze(&self) -> bool {
        true // No-op, always succeeds
    }

    fn unfreeze(&self) {
        // No-op
    }
}

// ============================================================================
// WgpuRawHandle
// ============================================================================

/// Raw handle for custom kernel launching.
///
/// Provides access to the WebGPU device and queue for users who want
/// to launch their own compute shaders outside of numr's operation system.
///
/// # Example
///
/// ```ignore
/// let client = WgpuRuntime::default_client(&device);
/// let handle = WgpuRuntime::raw_handle(&client);
///
/// // Use handle.device to create pipelines
/// // Use handle.queue for command submission
/// ```
#[derive(Clone)]
pub struct WgpuRawHandle {
    /// WebGPU device for pipeline creation
    pub device: Arc<Device>,
    /// WebGPU queue for command submission
    pub queue: Arc<Queue>,
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime::{Allocator, Device, RuntimeClient};

    #[test]
    fn test_wgpu_client_creation() {
        let device = WgpuDevice::new(0);
        match WgpuClient::new(device) {
            Ok(client) => {
                println!("Client created for: {}", client.device().name());
                assert_eq!(client.device().id(), 0);
            }
            Err(e) => {
                println!("No GPU available, skipping test: {}", e);
            }
        }
    }

    #[test]
    fn test_wgpu_allocator() {
        let device = WgpuDevice::new(0);
        match WgpuClient::new(device) {
            Ok(client) => {
                let allocator = client.allocator();

                // Allocate buffer
                let id = allocator.allocate(1024).expect("allocation should succeed");
                assert_ne!(id, 0);

                // Verify buffer exists
                let buffer = get_buffer(id);
                assert!(buffer.is_some());

                // Deallocate
                allocator.deallocate(id, 1024);

                // Buffer should be gone
                let buffer = get_buffer(id);
                assert!(buffer.is_none());
            }
            Err(e) => {
                println!("No GPU available, skipping test: {}", e);
            }
        }
    }

    #[test]
    fn test_wgpu_buffer_roundtrip() {
        let device = WgpuDevice::new(0);
        match WgpuClient::new(device) {
            Ok(client) => {
                // Create test data
                let data: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];
                let size = data.len() * std::mem::size_of::<f32>();

                // Create storage buffer
                let storage = client.create_storage_buffer("test", size as u64);

                // Write data to GPU
                client.write_buffer(&storage, &data);

                // Create staging buffer for readback
                let staging = client.create_staging_buffer("staging", size as u64);

                // Copy storage -> staging
                let mut encoder =
                    client
                        .wgpu_device
                        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                            label: Some("copy"),
                        });
                encoder.copy_buffer_to_buffer(&storage, 0, &staging, 0, size as u64);
                client.submit_and_wait(encoder);

                // Read back
                let mut result = vec![0.0f32; data.len()];
                client
                    .read_buffer(&staging, &mut result)
                    .expect("readback should succeed");

                assert_eq!(data, result);
                println!("Buffer roundtrip successful: {:?}", result);
            }
            Err(e) => {
                println!("No GPU available, skipping test: {}", e);
            }
        }
    }
}