torsh-backend 0.1.2

Backend abstraction layer for ToRSh
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
//! Modern WebGPU Backend Implementation for ToRSh

#[cfg(feature = "webgpu")]
use crate::webgpu::wgpu;
//!
//! This is a complete rewrite using wgpu 26.0.1 latest API patterns and best practices.
//! Features comprehensive async/await support, modern shader management (WGSL only),
//! proper memory management, and full compute pipeline implementation.

use crate::{
    Backend, BackendCapabilities, BackendCore, BackendDeviceManager, BackendExecutor,
    BackendLifecycle, BackendOperations, BackendOps, BackendResourceManager, BackendResult,
    BackendType, Buffer, BufferDescriptor, BufferHandle, CapabilityValue, Device, Kernel,
    KernelDescriptor, KernelHandle, MemoryManager, MemoryPool, MemoryStats, OperationsBundle,
    PerformanceHints, Profiler,
};
use crate::webgpu::{WebGpuError, WebGpuResult};
use async_trait::async_trait;
use futures::future::join_all;
use parking_lot::{RwLock, Mutex};
use std::{
    collections::HashMap,
    sync::Arc,
    time::{Duration, Instant},
};
use torsh_core::{device::DeviceType, dtype::DType, error::TorshError};
use wgpu::util::DeviceExt;

/// Modern WebGPU Backend Configuration
#[derive(Debug, Clone)]
pub struct ModernWebGpuConfig {
    pub device_descriptor: wgpu::DeviceDescriptor<'static>,
    pub instance_descriptor: wgpu::InstanceDescriptor,
    pub adapter_options: wgpu::RequestAdapterOptions<'static>,
    pub max_buffer_size: u64,
    pub enable_validation: bool,
    pub enable_spirv: bool,
    pub preferred_backend: Option<wgpu::Backends>,
}

impl Default for ModernWebGpuConfig {
    fn default() -> Self {
        Self {
            device_descriptor: wgpu::DeviceDescriptor {
                label: Some("ToRSh WebGPU Device"),
                required_features: wgpu::Features::empty(),
                required_limits: wgpu::Limits::downlevel_defaults(),
                memory_hints: wgpu::MemoryHints::default(),
                trace: wgpu::Trace::Off,
                experimental_features: wgpu::ExperimentalFeatures::default(),
            },
            instance_descriptor: wgpu::InstanceDescriptor {
                backends: wgpu::Backends::all(),
                flags: wgpu::InstanceFlags::default(),
                memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(),
                backend_options: wgpu::BackendOptions {
                    dx12: wgpu::Dx12BackendOptions {
                        shader_compiler: wgpu::Dx12Compiler::Fxc,
                        ..Default::default()
                    },
                    gl: wgpu::GlBackendOptions {
                        gles_minor_version: wgpu::Gles3MinorVersion::Automatic,
                        ..Default::default()
                    },
                    ..Default::default()
                },
                display: None,
            },
            adapter_options: wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            },
            max_buffer_size: 256 * 1024 * 1024, // 256MB default
            enable_validation: true,
            enable_spirv: false, // Modern wgpu prefers WGSL
            preferred_backend: None,
        }
    }
}

/// Modern WebGPU Device Context
#[derive(Debug)]
pub struct ModernWebGpuDevice {
    pub instance: wgpu::Instance,
    pub adapter: wgpu::Adapter,
    pub device: wgpu::Device,
    pub queue: wgpu::Queue,
    pub info: wgpu::AdapterInfo,
    pub limits: wgpu::Limits,
    pub features: wgpu::Features,
    config: ModernWebGpuConfig,
    shader_cache: RwLock<HashMap<String, Arc<wgpu::ShaderModule>>>,
    pipeline_cache: RwLock<HashMap<String, Arc<wgpu::ComputePipeline>>>,
    bind_group_layout_cache: RwLock<HashMap<String, Arc<wgpu::BindGroupLayout>>>,
}

impl ModernWebGpuDevice {
    /// Create a new modern WebGPU device
    pub async fn new(config: ModernWebGpuConfig) -> WebGpuResult<Arc<Self>> {
        // Create instance
        let instance = wgpu::Instance::new(config.instance_descriptor.clone());

        // Request adapter
        let adapter = instance
            .request_adapter(&config.adapter_options)
            .await
            .ok_or_else(|| {
                WebGpuError::InitializationError("Failed to request WebGPU adapter".to_string())
            })?;

        let info = adapter.get_info();
        log::info!("WebGPU Adapter: {} ({:?})", info.name, info.backend);

        let mut required_features = config.device_descriptor.required_features;
        let mut required_limits = config.device_descriptor.required_limits.clone();

        // Adjust limits based on adapter capabilities
        let adapter_limits = adapter.limits();
        required_limits.max_buffer_size = required_limits
            .max_buffer_size
            .min(adapter_limits.max_buffer_size)
            .min(config.max_buffer_size);

        // Enable additional features if supported
        let supported_features = adapter.features();
        if supported_features.contains(wgpu::Features::TIMESTAMP_QUERY) {
            required_features |= wgpu::Features::TIMESTAMP_QUERY;
        }
        if supported_features.contains(wgpu::Features::BUFFER_BINDING_ARRAY) {
            required_features |= wgpu::Features::BUFFER_BINDING_ARRAY;
        }
        if supported_features.contains(wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY) {
            required_features |= wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY;
        }

        let device_descriptor = wgpu::DeviceDescriptor {
            label: config.device_descriptor.label,
            required_features,
            required_limits: required_limits.clone(),
            memory_hints: config.device_descriptor.memory_hints.clone(),
            trace: wgpu::Trace::Off,
            experimental_features: wgpu::ExperimentalFeatures::default(),
        };

        // Request device and queue
        let (device, queue) = adapter
            .request_device(&device_descriptor)
            .await
            .map_err(|e| {
                WebGpuError::InitializationError(format!("Failed to request WebGPU device: {}", e))
            })?;

        Ok(Arc::new(Self {
            instance,
            adapter,
            device,
            queue,
            info,
            limits: required_limits,
            features: required_features,
            config,
            shader_cache: RwLock::new(HashMap::new()),
            pipeline_cache: RwLock::new(HashMap::new()),
            bind_group_layout_cache: RwLock::new(HashMap::new()),
        }))
    }

    /// Create or get cached shader module from WGSL source
    pub fn create_shader_module(&self, label: &str, source: &str) -> Arc<wgpu::ShaderModule> {
        let key = format!("{}_{}", label, md5::compute(source.as_bytes()));

        {
            let cache = self.shader_cache.read();
            if let Some(shader) = cache.get(&key) {
                return shader.clone();
            }
        }

        let shader = Arc::new(self.device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some(label),
            source: wgpu::ShaderSource::Wgsl(source.into()),
        }));

        self.shader_cache.write().insert(key, shader.clone());
        shader
    }

    /// Create or get cached compute pipeline
    pub fn create_compute_pipeline(
        &self,
        label: &str,
        shader: &wgpu::ShaderModule,
        entry_point: &str,
        bind_group_layouts: &[&wgpu::BindGroupLayout],
    ) -> WebGpuResult<Arc<wgpu::ComputePipeline>> {
        let key = format!("{}_{}", label, entry_point);

        {
            let cache = self.pipeline_cache.read();
            if let Some(pipeline) = cache.get(&key) {
                return Ok(pipeline.clone());
            }
        }

        let bind_group_layouts_opt: Vec<_> = bind_group_layouts.iter().map(|&l| Some(l)).collect();
        let pipeline_layout = self.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some(&format!("{}_layout", label)),
            bind_group_layouts: &bind_group_layouts_opt,
            immediate_size: 0,
        });

        let pipeline = Arc::new(
            self.device
                .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                    label: Some(label),
                    layout: Some(&pipeline_layout),
                    module: shader,
                    entry_point: Some(entry_point),
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                    cache: None,
                }),
        );

        self.pipeline_cache.write().insert(key, pipeline.clone());
        Ok(pipeline)
    }

    /// Create buffer with proper error handling
    pub fn create_buffer_with_data<T: bytemuck::Pod>(&self, label: &str, data: &[T], usage: wgpu::BufferUsages) -> wgpu::Buffer {
        self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some(label),
            contents: bytemuck::cast_slice(data),
            usage,
        })
    }

    /// Submit work and wait for completion
    pub async fn submit_and_wait(&self, commands: impl IntoIterator<Item = wgpu::CommandBuffer>) -> WebGpuResult<()> {
        let submission_index = self.queue.submit(commands);

        // Map buffer to ensure completion
        let staging_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("staging_wait_buffer"),
            size: 4,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        staging_buffer.slice(..).map_async(wgpu::MapMode::Read, |result| {
            if let Err(e) = result {
                log::error!("Buffer mapping failed during wait: {:?}", e);
            }
        });

        let _ = self.device.poll(wgpu::PollType::Wait {
            submission_index: Some(submission_index),
            timeout: None,
        });
        Ok(())
    }
}

/// Modern WebGPU Memory Manager
#[derive(Debug)]
pub struct ModernWebGpuMemoryManager {
    device: Arc<ModernWebGpuDevice>,
    buffer_pools: RwLock<HashMap<(u64, wgpu::BufferUsages), Vec<wgpu::Buffer>>>,
    allocation_stats: RwLock<MemoryStats>,
    next_buffer_id: Mutex<u64>,
}

impl ModernWebGpuMemoryManager {
    pub fn new(device: Arc<ModernWebGpuDevice>) -> Arc<Self> {
        Arc::new(Self {
            device,
            buffer_pools: RwLock::new(HashMap::new()),
            allocation_stats: RwLock::new(MemoryStats {
                total_allocated: 0,
                total_deallocated: 0,
                current_allocated: 0,
                peak_allocated: 0,
                allocation_count: 0,
                deallocation_count: 0,
            }),
            next_buffer_id: Mutex::new(0),
        })
    }

    pub fn allocate_buffer(&self, descriptor: &BufferDescriptor) -> WebGpuResult<ModernWebGpuBuffer> {
        let usage = self.convert_buffer_usage(&descriptor.usage)?;
        let size = descriptor.size as u64;

        // Try to get from pool first
        let mut pools = self.buffer_pools.write();
        let pool_key = (size, usage);

        let buffer = if let Some(pool) = pools.get_mut(&pool_key) {
            pool.pop().unwrap_or_else(|| {
                self.device.device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some(&format!("ToRSh Buffer {}", descriptor.name.as_deref().unwrap_or("unnamed"))),
                    size,
                    usage,
                    mapped_at_creation: false,
                })
            })
        } else {
            self.device.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some(&format!("ToRSh Buffer {}", descriptor.name.as_deref().unwrap_or("unnamed"))),
                size,
                usage,
                mapped_at_creation: false,
            })
        };

        let buffer_id = {
            let mut id = self.next_buffer_id.lock();
            let current_id = *id;
            *id += 1;
            current_id
        };

        // Update stats
        {
            let mut stats = self.allocation_stats.write();
            stats.total_allocated += size as usize;
            stats.current_allocated += size as usize;
            stats.peak_allocated = stats.peak_allocated.max(stats.current_allocated);
            stats.allocation_count += 1;
        }

        Ok(ModernWebGpuBuffer {
            buffer,
            device: self.device.clone(),
            handle: BufferHandle::WebGpu {
                buffer_ptr: buffer_id,
                size: descriptor.size,
            },
            size: descriptor.size,
            usage,
            label: descriptor.name.clone(),
        })
    }

    fn convert_buffer_usage(&self, usage: &crate::buffer::BufferUsage) -> WebGpuResult<wgpu::BufferUsages> {
        use crate::buffer::BufferUsage;

        let mut wgpu_usage = wgpu::BufferUsages::empty();

        match usage {
            BufferUsage::Uniform => wgpu_usage |= wgpu::BufferUsages::UNIFORM,
            BufferUsage::Storage => wgpu_usage |= wgpu::BufferUsages::STORAGE,
            BufferUsage::Vertex => wgpu_usage |= wgpu::BufferUsages::VERTEX,
            BufferUsage::Index => wgpu_usage |= wgpu::BufferUsages::INDEX,
        }

        // Always add copy operations for data transfer
        wgpu_usage |= wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST;

        Ok(wgpu_usage)
    }

    pub fn return_buffer_to_pool(&self, buffer: ModernWebGpuBuffer) {
        let pool_key = (buffer.size as u64, buffer.usage);
        let mut pools = self.buffer_pools.write();
        pools.entry(pool_key).or_insert_with(Vec::new).push(buffer.buffer);

        // Update stats
        let mut stats = self.allocation_stats.write();
        stats.total_deallocated += buffer.size;
        stats.current_allocated = stats.current_allocated.saturating_sub(buffer.size);
        stats.deallocation_count += 1;
    }
}

/// Modern WebGPU Buffer
#[derive(Debug)]
pub struct ModernWebGpuBuffer {
    pub buffer: wgpu::Buffer,
    pub device: Arc<ModernWebGpuDevice>,
    pub handle: BufferHandle,
    pub size: usize,
    pub usage: wgpu::BufferUsages,
    pub label: Option<String>,
}

impl ModernWebGpuBuffer {
    /// Write data to buffer asynchronously
    pub async fn write_data<T: bytemuck::Pod>(&self, data: &[T]) -> WebGpuResult<()> {
        let bytes = bytemuck::cast_slice(data);
        if bytes.len() > self.size {
            return Err(WebGpuError::InvalidArgument(format!(
                "Data size {} exceeds buffer size {}",
                bytes.len(),
                self.size
            )));
        }

        self.device.queue.write_buffer(&self.buffer, 0, bytes);
        Ok(())
    }

    /// Read data from buffer asynchronously
    pub async fn read_data<T: bytemuck::Pod + Clone>(&self) -> WebGpuResult<Vec<T>> {
        // Create staging buffer
        let staging_buffer = self.device.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("staging_read_buffer"),
            size: self.size as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        // Copy data to staging buffer
        let mut encoder = self.device.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("buffer_read_copy"),
        });

        encoder.copy_buffer_to_buffer(
            &self.buffer,
            0,
            &staging_buffer,
            0,
            self.size as u64,
        );

        self.device.queue.submit([encoder.finish()]);

        // Map and read data
        let buffer_slice = staging_buffer.slice(..);
        let (tx, rx) = futures::channel::oneshot::channel();

        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = tx.send(result);
        });

        let _ = self.device.device.poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        });
        rx.await.expect("buffer mapping channel should not be dropped").map_err(|e| WebGpuError::RuntimeError(format!("Buffer mapping failed: {:?}", e)))?;

        let data = buffer_slice.get_mapped_range();
        let result: Vec<T> = bytemuck::cast_slice(&data).to_vec();

        drop(data);
        staging_buffer.unmap();

        Ok(result)
    }
}

/// Modern WebGPU Compute Operation
#[derive(Debug)]
pub struct ModernWebGpuCompute {
    device: Arc<ModernWebGpuDevice>,
    memory_manager: Arc<ModernWebGpuMemoryManager>,
}

impl ModernWebGpuCompute {
    pub fn new(device: Arc<ModernWebGpuDevice>) -> Self {
        let memory_manager = ModernWebGpuMemoryManager::new(device.clone());
        Self {
            device,
            memory_manager,
        }
    }

    /// Execute a compute shader with buffers
    pub async fn execute_compute(
        &self,
        shader_source: &str,
        entry_point: &str,
        buffers: &[&ModernWebGpuBuffer],
        workgroup_size: (u32, u32, u32),
    ) -> WebGpuResult<()> {
        // Create shader
        let shader = self.device.create_shader_module("compute_shader", shader_source);

        // Create bind group layout
        let mut bind_group_layout_entries = Vec::new();
        for (i, _buffer) in buffers.iter().enumerate() {
            bind_group_layout_entries.push(wgpu::BindGroupLayoutEntry {
                binding: i as u32,
                visibility: wgpu::ShaderStages::COMPUTE,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Storage { read_only: false },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            });
        }

        let bind_group_layout = self.device.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("compute_bind_group_layout"),
            entries: &bind_group_layout_entries,
        });

        // Create compute pipeline
        let pipeline = self.device.create_compute_pipeline(
            "compute_pipeline",
            &shader,
            entry_point,
            &[&bind_group_layout],
        )?;

        // Create bind group
        let mut bind_group_entries = Vec::new();
        for (i, buffer) in buffers.iter().enumerate() {
            bind_group_entries.push(wgpu::BindGroupEntry {
                binding: i as u32,
                resource: buffer.buffer.as_entire_binding(),
            });
        }

        let bind_group = self.device.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("compute_bind_group"),
            layout: &bind_group_layout,
            entries: &bind_group_entries,
        });

        // Create command encoder and dispatch
        let mut encoder = self.device.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("compute_encoder"),
        });

        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("compute_pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(&pipeline);
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups(workgroup_size.0, workgroup_size.1, workgroup_size.2);
        }

        // Submit and wait
        self.device.submit_and_wait([encoder.finish()]).await?;
        Ok(())
    }
}

/// Modern WebGPU Backend - Main Implementation
#[derive(Debug)]
pub struct ModernWebGpuBackend {
    device: Arc<ModernWebGpuDevice>,
    memory_manager: Arc<ModernWebGpuMemoryManager>,
    compute: ModernWebGpuCompute,
    config: ModernWebGpuConfig,
}

impl ModernWebGpuBackend {
    pub async fn new(config: ModernWebGpuConfig) -> WebGpuResult<Self> {
        let device = ModernWebGpuDevice::new(config.clone()).await?;
        let memory_manager = ModernWebGpuMemoryManager::new(device.clone());
        let compute = ModernWebGpuCompute::new(device.clone());

        Ok(Self {
            device,
            memory_manager,
            compute,
            config,
        })
    }

    pub fn with_default() -> impl std::future::Future<Output = WebGpuResult<Self>> {
        Self::new(ModernWebGpuConfig::default())
    }
}

// Note: Full trait implementations will be added in subsequent files
// This provides the foundation for a modern, comprehensive WebGPU backend