primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Pipeline Cache for `BarraCuda`
//!
//! Caches shader modules, bind group layouts, and compute pipelines
//! to eliminate redundant GPU object creation.
//!
//! This is critical for achieving CUDA parity - native CUDA only
//! compiles kernels once, while naive wgpu creates them every dispatch.
//!
//! Note: Each wgpu Device has its own cache because GPU objects are
//! not transferable between devices.

use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use wgpu::{BindGroupLayout, ComputePipeline, Device, ShaderModule};

/// Recover from `RwLock` poison on read. Safe for caches: worst case is a
/// stale entry or cache miss, never data corruption.
fn read_or_recover<T>(lock: &RwLock<T>) -> RwLockReadGuard<'_, T> {
    lock.read()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// Recover from `RwLock` poison on write. Safe for caches: a previously
/// panicked thread may have left partial state, but inserting over it
/// is correct behavior for a cache.
fn write_or_recover<T>(lock: &RwLock<T>) -> RwLockWriteGuard<'_, T> {
    lock.write()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// Unique device identifier that works across wgpu instances
///
/// wgpu's `global_id()` is only unique within a single Instance.
/// Since `GpuPool` creates devices from different instances, we use
/// a hash of the device's physical characteristics instead.
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub struct DeviceFingerprint {
    /// Hash of adapter name
    name_hash: u64,
    /// Hash of backend + device type
    backend_hash: u64,
}

impl DeviceFingerprint {
    /// Create fingerprint from adapter info
    #[must_use]
    pub fn from_device_info(device: &Device, adapter_info: &wgpu::AdapterInfo) -> Self {
        use std::hash::Hasher;

        let mut name_hasher = std::collections::hash_map::DefaultHasher::new();
        adapter_info.name.hash(&mut name_hasher);
        let name_hash = name_hasher.finish();

        let mut backend_hasher = std::collections::hash_map::DefaultHasher::new();
        std::mem::discriminant(&adapter_info.backend).hash(&mut backend_hasher);
        std::mem::discriminant(&adapter_info.device_type).hash(&mut backend_hasher);
        device.hash(&mut backend_hasher);
        let backend_hash = backend_hasher.finish();

        Self {
            name_hash,
            backend_hash,
        }
    }

    /// Create from just adapter info (for quick lookups)
    #[must_use]
    pub fn from_adapter_info(adapter_info: &wgpu::AdapterInfo) -> Self {
        use std::hash::Hasher;

        let mut name_hasher = std::collections::hash_map::DefaultHasher::new();
        adapter_info.name.hash(&mut name_hasher);
        let name_hash = name_hasher.finish();

        let mut backend_hasher = std::collections::hash_map::DefaultHasher::new();
        std::mem::discriminant(&adapter_info.backend).hash(&mut backend_hasher);
        std::mem::discriminant(&adapter_info.device_type).hash(&mut backend_hasher);
        let backend_hash = backend_hasher.finish();

        Self {
            name_hash,
            backend_hash,
        }
    }
}

/// Key for caching shader modules (includes device fingerprint)
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct ShaderKey {
    /// Hash of shader source
    source_hash: u64,
    /// Device fingerprint (unique per physical GPU)
    device_fingerprint: DeviceFingerprint,
}

impl ShaderKey {
    /// Create a shader cache key from source and device fingerprint.
    #[must_use]
    pub fn new(source: &str, device_fingerprint: DeviceFingerprint) -> Self {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        source.hash(&mut hasher);
        Self {
            source_hash: hasher.finish(),
            device_fingerprint,
        }
    }
}

/// Bind group layout signature (without device - used for creating keys)
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct BindGroupLayoutSignature {
    /// Number of read-only storage buffers
    pub read_only_buffers: u32,
    /// Number of read-write storage buffers
    pub read_write_buffers: u32,
    /// Number of uniform buffers
    pub uniform_buffers: u32,
}

impl BindGroupLayoutSignature {
    /// Standard elementwise binary op (2 read, 1 write)
    #[must_use]
    pub fn elementwise_binary() -> Self {
        Self {
            read_only_buffers: 2,
            read_write_buffers: 1,
            uniform_buffers: 0,
        }
    }

    /// Standard unary op (1 read, 1 write)
    #[must_use]
    pub fn elementwise_unary() -> Self {
        Self {
            read_only_buffers: 1,
            read_write_buffers: 1,
            uniform_buffers: 0,
        }
    }

    /// Reduction op (1 read, 1 write, 1 uniform for params)
    #[must_use]
    pub fn reduction() -> Self {
        Self {
            read_only_buffers: 1,
            read_write_buffers: 1,
            uniform_buffers: 1,
        }
    }

    /// Matmul (2 read, 1 write, 1 uniform for dimensions)
    #[must_use]
    pub fn matmul() -> Self {
        Self {
            read_only_buffers: 2,
            read_write_buffers: 1,
            uniform_buffers: 1,
        }
    }

    /// Ternary op like FMA: d = a * b + c (3 read, 1 write)
    #[must_use]
    pub fn elementwise_ternary() -> Self {
        Self {
            read_only_buffers: 3,
            read_write_buffers: 1,
            uniform_buffers: 0,
        }
    }

    /// Two-input reduction with params: covariance, correlation, cosine similarity
    /// (2 read, 1 rw, 1 uniform). Equivalent to `matmul()` layout.
    #[must_use]
    pub fn two_input_reduction() -> Self {
        Self {
            read_only_buffers: 2,
            read_write_buffers: 1,
            uniform_buffers: 1,
        }
    }

    /// Three-input reduction with params: weighted dot product
    /// (3 read, 1 rw, 1 uniform).
    #[must_use]
    pub fn three_input_reduction() -> Self {
        Self {
            read_only_buffers: 3,
            read_write_buffers: 1,
            uniform_buffers: 1,
        }
    }
}

/// Key for caching bind group layouts (includes device fingerprint)
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct BindGroupLayoutKey {
    signature: BindGroupLayoutSignature,
    device_fingerprint: DeviceFingerprint,
}

impl BindGroupLayoutKey {
    /// Create a bind group layout cache key.
    #[must_use]
    pub fn new(signature: BindGroupLayoutSignature, device_fingerprint: DeviceFingerprint) -> Self {
        Self {
            signature,
            device_fingerprint,
        }
    }
}

/// Key for caching compute pipelines (includes device fingerprint)
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct PipelineKey {
    source_hash: u64,
    layout_signature: BindGroupLayoutSignature,
    entry_point_hash: u64,
    device_fingerprint: DeviceFingerprint,
}

impl PipelineKey {
    /// Create a pipeline cache key from shader source, layout, entry point, and device.
    #[must_use]
    pub fn new(
        shader_source: &str,
        layout_signature: BindGroupLayoutSignature,
        entry_point: &str,
        device_fingerprint: DeviceFingerprint,
    ) -> Self {
        let mut src_hasher = std::collections::hash_map::DefaultHasher::new();
        shader_source.hash(&mut src_hasher);
        let mut ep_hasher = std::collections::hash_map::DefaultHasher::new();
        entry_point.hash(&mut ep_hasher);
        Self {
            source_hash: src_hasher.finish(),
            layout_signature,
            entry_point_hash: ep_hasher.finish(),
            device_fingerprint,
        }
    }
}

/// Thread-safe pipeline cache — evolved from `DashMap` to stdlib `RwLock<HashMap>`.
///
/// Access pattern: very frequent reads (cache hits), rare writes (first-use only).
/// `RwLock`'s read-write semantics are ideal: unlimited concurrent readers, exclusive writer.
///
/// Note: Keys include device fingerprint to ensure GPU objects are only used
/// with the device that created them.
#[derive(Default)]
pub struct PipelineCache {
    /// Cached shader modules — f64-canonical, auto-downcast to f32 (keyed by source hash + device)
    shaders: RwLock<HashMap<ShaderKey, Arc<ShaderModule>>>,

    /// Cached shader modules — f64-native, no downcast (keyed by source hash + device)
    shaders_f64: RwLock<HashMap<ShaderKey, Arc<ShaderModule>>>,

    /// Cached bind group layouts (keyed by signature + device)
    layouts: RwLock<HashMap<BindGroupLayoutKey, Arc<BindGroupLayout>>>,

    /// Cached compute pipelines — f64-canonical, auto-downcast (keyed by shader + layout + entry + device)
    pipelines: RwLock<HashMap<PipelineKey, Arc<ComputePipeline>>>,

    /// Cached compute pipelines — f64-native (keyed by shader + layout + entry + device)
    pipelines_f64: RwLock<HashMap<PipelineKey, Arc<ComputePipeline>>>,
}

impl PipelineCache {
    /// Create an empty pipeline cache.
    #[must_use]
    pub fn new() -> Self {
        Self {
            shaders: RwLock::new(HashMap::new()),
            shaders_f64: RwLock::new(HashMap::new()),
            layouts: RwLock::new(HashMap::new()),
            pipelines: RwLock::new(HashMap::new()),
            pipelines_f64: RwLock::new(HashMap::new()),
        }
    }

    /// Get or compile a shader module
    ///
    /// Uses device + `adapter_info` to create a fingerprint unique per device instance.
    /// This ensures layouts/pipelines from different `wgpu::Device` instances don't collide.
    pub fn get_or_compile_shader(
        &self,
        device: &Device,
        adapter_info: &wgpu::AdapterInfo,
        source: &str,
        label: Option<&str>,
    ) -> Arc<ShaderModule> {
        let fingerprint = DeviceFingerprint::from_device_info(device, adapter_info);
        let key = ShaderKey::new(source, fingerprint);

        // Fast path: already compiled.
        if let Some(m) = read_or_recover(&self.shaders).get(&key) {
            return m.clone();
        }
        // Slow path: auto-downcast f64-canonical source to f32 then compile and cache.
        // f64-canonical architecture: shaders are authored in f64 as the source of
        // truth. The cache always downcasts for broad f32 compatibility.
        // Ops that need native f64 must compile via WgpuDevice::compile_shader_f64().
        let resolved: std::borrow::Cow<'_, str> =
            if crate::shaders::precision::compiler::source_is_f64(source) {
                std::borrow::Cow::Owned(
                    crate::shaders::precision::downcast_f64_to_f32_with_transcendentals(source),
                )
            } else {
                std::borrow::Cow::Borrowed(source)
            };
        let module = Arc::new(device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label,
            source: wgpu::ShaderSource::Wgsl((&*resolved).into()),
        }));
        write_or_recover(&self.shaders)
            .entry(key)
            .or_insert(module)
            .clone()
    }

    /// Get or create a bind group layout
    ///
    /// Uses device + `adapter_info` to create a fingerprint unique per device instance.
    pub fn get_or_create_layout(
        &self,
        device: &Device,
        adapter_info: &wgpu::AdapterInfo,
        signature: BindGroupLayoutSignature,
        label: Option<&str>,
    ) -> Arc<BindGroupLayout> {
        let fingerprint = DeviceFingerprint::from_device_info(device, adapter_info);
        let key = BindGroupLayoutKey::new(signature, fingerprint);

        // Fast path: already created.
        if let Some(l) = read_or_recover(&self.layouts).get(&key) {
            return l.clone();
        }
        // Slow path: create from signature.
        let layout = {
            let mut entries = Vec::new();
            let mut binding = 0u32;

            // Read-only storage buffers
            for _ in 0..signature.read_only_buffers {
                entries.push(wgpu::BindGroupLayoutEntry {
                    binding,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: true },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                });
                binding += 1;
            }

            // Read-write storage buffers
            for _ in 0..signature.read_write_buffers {
                entries.push(wgpu::BindGroupLayoutEntry {
                    binding,
                    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,
                });
                binding += 1;
            }

            // Uniform buffers
            for _ in 0..signature.uniform_buffers {
                entries.push(wgpu::BindGroupLayoutEntry {
                    binding,
                    visibility: wgpu::ShaderStages::COMPUTE,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                });
                binding += 1;
            }

            let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label,
                entries: &entries,
            });
            Arc::new(layout)
        };
        write_or_recover(&self.layouts)
            .entry(key)
            .or_insert(layout)
            .clone()
    }

    /// Get or create a compute pipeline
    ///
    /// Uses `adapter_info` to create a device fingerprint that's unique per physical GPU.
    pub fn get_or_create_pipeline(
        &self,
        device: &Device,
        adapter_info: &wgpu::AdapterInfo,
        shader_source: &str,
        layout_signature: BindGroupLayoutSignature,
        entry_point: &str,
        label: Option<&str>,
    ) -> Arc<ComputePipeline> {
        let fingerprint = DeviceFingerprint::from_device_info(device, adapter_info);
        let key = PipelineKey::new(shader_source, layout_signature, entry_point, fingerprint);

        // Fast path: already compiled.
        if let Some(p) = read_or_recover(&self.pipelines).get(&key) {
            return p.clone();
        }
        // Slow path: compile shaders, build pipeline layout, create pipeline.
        let shader = self.get_or_compile_shader(device, adapter_info, shader_source, label);
        let layout = self.get_or_create_layout(device, adapter_info, layout_signature, label);
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label,
            bind_group_layouts: &[&layout],
            immediate_size: 0,
        });
        let pipeline = Arc::new(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label,
                layout: Some(&pipeline_layout),
                module: &shader,
                entry_point: Some(entry_point),
                cache: None,
                compilation_options: Default::default(),
            }),
        );
        write_or_recover(&self.pipelines)
            .entry(key)
            .or_insert(pipeline)
            .clone()
    }

    /// Get or compile a shader module preserving f64 types (no downcast).
    ///
    /// For ops that upload real f64 data and need the shader to read it natively.
    /// Cached separately from the f32-downcast path to avoid key collisions.
    pub fn get_or_compile_shader_f64_native(
        &self,
        device: &Device,
        adapter_info: &wgpu::AdapterInfo,
        source: &str,
        label: Option<&str>,
    ) -> Arc<ShaderModule> {
        let fingerprint = DeviceFingerprint::from_device_info(device, adapter_info);
        let key = ShaderKey::new(source, fingerprint);

        if let Some(m) = read_or_recover(&self.shaders_f64).get(&key) {
            return m.clone();
        }
        // Strip `enable f64;` — naga resolves f64 support from device capability
        // flags, not WGSL directives.  Leaving the directive in causes NVIDIA
        // PTXAS (Ada Lovelace / SM89) to silently produce broken shaders that
        // return zeros for all outputs.  Discovered via neuralSpring diagnostic
        // on RTX 4070 Vulkan (Mar 2026).
        let stripped: std::borrow::Cow<'_, str> = if source.contains("enable f64;") {
            source
                .lines()
                .filter(|l| l.trim() != "enable f64;")
                .collect::<Vec<_>>()
                .join("\n")
                .into()
        } else {
            source.into()
        };
        let module = Arc::new(device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label,
            source: wgpu::ShaderSource::Wgsl((&*stripped).into()),
        }));
        write_or_recover(&self.shaders_f64)
            .entry(key)
            .or_insert(module)
            .clone()
    }

    /// Get or create a compute pipeline preserving f64 types (no downcast).
    ///
    /// For ops that upload real f64 data to GPU buffers. The shader is compiled
    /// as-is, so it must match the buffer data layout. Cached separately from
    /// the f32-downcast path.
    pub fn get_or_create_pipeline_f64_native(
        &self,
        device: &Device,
        adapter_info: &wgpu::AdapterInfo,
        shader_source: &str,
        layout_signature: BindGroupLayoutSignature,
        entry_point: &str,
        label: Option<&str>,
    ) -> Arc<ComputePipeline> {
        let fingerprint = DeviceFingerprint::from_device_info(device, adapter_info);
        let key = PipelineKey::new(shader_source, layout_signature, entry_point, fingerprint);

        if let Some(p) = read_or_recover(&self.pipelines_f64).get(&key) {
            return p.clone();
        }
        let shader =
            self.get_or_compile_shader_f64_native(device, adapter_info, shader_source, label);
        let layout = self.get_or_create_layout(device, adapter_info, layout_signature, label);
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label,
            bind_group_layouts: &[&layout],
            immediate_size: 0,
        });
        let pipeline = Arc::new(
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label,
                layout: Some(&pipeline_layout),
                module: &shader,
                entry_point: Some(entry_point),
                cache: None,
                compilation_options: Default::default(),
            }),
        );
        write_or_recover(&self.pipelines_f64)
            .entry(key)
            .or_insert(pipeline)
            .clone()
    }

    /// Get cache statistics
    pub fn stats(&self) -> CacheStats {
        CacheStats {
            shaders: read_or_recover(&self.shaders).len()
                + read_or_recover(&self.shaders_f64).len(),
            layouts: read_or_recover(&self.layouts).len(),
            pipelines: read_or_recover(&self.pipelines).len()
                + read_or_recover(&self.pipelines_f64).len(),
        }
    }

    /// Clear all cached objects
    pub fn clear(&self) {
        write_or_recover(&self.shaders).clear();
        write_or_recover(&self.shaders_f64).clear();
        write_or_recover(&self.layouts).clear();
        write_or_recover(&self.pipelines).clear();
        write_or_recover(&self.pipelines_f64).clear();
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStats {
    /// Number of cached shader modules.
    pub shaders: usize,
    /// Number of cached bind group layouts.
    pub layouts: usize,
    /// Number of cached compute pipelines.
    pub pipelines: usize,
}

/// Global pipeline cache (singleton pattern via `std::sync::LazyLock`).
///
/// Evolved from `lazy_static` to pure std (Rust 1.80+).
pub static GLOBAL_CACHE: std::sync::LazyLock<PipelineCache> =
    std::sync::LazyLock::new(PipelineCache::new);

/// Create a pipeline for an op that uploads real f64 data to GPU buffers.
///
/// Auto-selects compilation path based on device capabilities:
/// - Device has `SHADER_F64`: compile shader as-is (f64-native cache)
/// - No `SHADER_F64`: use the f32-downcast cache (caller's `Fp64Strategy`
///   should have already selected a DF64 shader variant)
pub fn create_f64_data_pipeline(
    device: &crate::device::WgpuDevice,
    shader_src: &str,
    layout_sig: BindGroupLayoutSignature,
    entry: &str,
    label: Option<&str>,
) -> std::sync::Arc<wgpu::ComputePipeline> {
    let adapter_info = device.adapter_info();
    let needs_native = device
        .device()
        .features()
        .contains(wgpu::Features::SHADER_F64)
        && crate::shaders::precision::compiler::source_is_f64(shader_src);

    if needs_native {
        GLOBAL_CACHE.get_or_create_pipeline_f64_native(
            device.device(),
            adapter_info,
            shader_src,
            layout_sig,
            entry,
            label,
        )
    } else {
        GLOBAL_CACHE.get_or_create_pipeline(
            device.device(),
            adapter_info,
            shader_src,
            layout_sig,
            entry,
            label,
        )
    }
}

/// Clear the global pipeline cache (for testing only)
///
/// This clears all cached shaders, layouts, and pipelines.
/// Should only be used in tests to ensure isolation.
pub fn clear_global_cache() {
    GLOBAL_CACHE.clear();
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fingerprint_consistency() {
        let adapter_info = wgpu::AdapterInfo {
            name: "Test GPU".to_string(),
            vendor: 0,
            device: 0,
            device_type: wgpu::DeviceType::DiscreteGpu,
            driver: "test".to_string(),
            driver_info: "1.0".to_string(),
            backend: wgpu::Backend::Vulkan,
            device_pci_bus_id: String::new(),
            subgroup_min_size: 1,
            subgroup_max_size: 128,
            transient_saves_memory: false,
        };

        let fp1 = DeviceFingerprint::from_adapter_info(&adapter_info);
        let fp2 = DeviceFingerprint::from_adapter_info(&adapter_info);
        assert_eq!(fp1, fp2);
    }

    #[test]
    fn test_different_gpus_different_fingerprints() {
        let nvidia = wgpu::AdapterInfo {
            name: "NVIDIA RTX 3090".to_string(),
            vendor: 0,
            device: 0,
            device_type: wgpu::DeviceType::DiscreteGpu,
            driver: "nvidia".to_string(),
            driver_info: "1.0".to_string(),
            backend: wgpu::Backend::Vulkan,
            device_pci_bus_id: String::new(),
            subgroup_min_size: 1,
            subgroup_max_size: 128,
            transient_saves_memory: false,
        };

        let amd = wgpu::AdapterInfo {
            name: "AMD RX 6950 XT".to_string(),
            vendor: 0,
            device: 0,
            device_type: wgpu::DeviceType::DiscreteGpu,
            driver: "radv".to_string(),
            driver_info: "1.0".to_string(),
            backend: wgpu::Backend::Vulkan,
            device_pci_bus_id: String::new(),
            subgroup_min_size: 1,
            subgroup_max_size: 128,
            transient_saves_memory: false,
        };

        let fp_nvidia = DeviceFingerprint::from_adapter_info(&nvidia);
        let fp_amd = DeviceFingerprint::from_adapter_info(&amd);
        assert_ne!(
            fp_nvidia, fp_amd,
            "Different GPUs should have different fingerprints"
        );
    }

    #[test]
    fn test_layout_signature_presets() {
        let binary = BindGroupLayoutSignature::elementwise_binary();
        assert_eq!(binary.read_only_buffers, 2);
        assert_eq!(binary.read_write_buffers, 1);
    }
}