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
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
//! Device abstraction and management

use torsh_core::device::DeviceType as CoreDeviceType;

#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

/// Device identifier and properties
#[derive(Debug, Clone, PartialEq)]
pub struct Device {
    /// Unique device ID within the backend
    pub id: usize,

    /// Device type (CPU, CUDA, Metal, etc.)
    pub device_type: CoreDeviceType,

    /// Human-readable device name
    pub name: String,

    /// Device information and capabilities
    pub info: DeviceInfo,
}

impl Device {
    /// Create a new device
    pub fn new(id: usize, device_type: CoreDeviceType, name: String, info: DeviceInfo) -> Self {
        Self {
            id,
            device_type,
            name,
            info,
        }
    }

    /// Builder pattern for creating devices
    pub fn builder() -> DeviceBuilder {
        DeviceBuilder::new()
    }

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

    /// Get the device type
    pub const fn device_type(&self) -> CoreDeviceType {
        self.device_type
    }

    /// Get the device name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get device information
    pub fn info(&self) -> &DeviceInfo {
        &self.info
    }

    /// Check if this device supports the given feature
    pub fn supports_feature(&self, feature: DeviceFeature) -> bool {
        self.info.features.contains(&feature)
    }

    /// Create a default CPU device
    pub fn cpu() -> crate::BackendResult<Self> {
        DeviceBuilder::new()
            .with_device_type(CoreDeviceType::Cpu)
            .with_name("CPU".to_string())
            .with_vendor("Generic".to_string())
            .with_compute_units(num_cpus::get())
            .build()
    }
}

/// Detailed device information
#[derive(Debug, Clone, PartialEq)]
pub struct DeviceInfo {
    /// Vendor name (e.g., "NVIDIA", "AMD", "Apple")
    pub vendor: String,

    /// Driver version
    pub driver_version: String,

    /// Total memory in bytes
    pub total_memory: usize,

    /// Available memory in bytes
    pub available_memory: usize,

    /// Number of compute units (cores, SMs, etc.)
    pub compute_units: usize,

    /// Maximum work group size
    pub max_work_group_size: usize,

    /// Maximum work group dimensions
    pub max_work_group_dimensions: Vec<usize>,

    /// Clock frequency in MHz
    pub clock_frequency_mhz: u32,

    /// Memory bandwidth in GB/s
    pub memory_bandwidth_gbps: f32,

    /// Peak compute performance in GFLOPS
    pub peak_gflops: f32,

    /// Supported features
    pub features: Vec<DeviceFeature>,

    /// Additional vendor-specific properties
    pub properties: Vec<(String, String)>,
}

impl Default for DeviceInfo {
    fn default() -> Self {
        Self {
            vendor: "Unknown".to_string(),
            driver_version: "Unknown".to_string(),
            total_memory: 0,
            available_memory: 0,
            compute_units: 1,
            max_work_group_size: 256,
            max_work_group_dimensions: vec![256, 1, 1],
            clock_frequency_mhz: 1000,
            memory_bandwidth_gbps: 10.0,
            peak_gflops: 100.0,
            features: Vec::new(),
            properties: Vec::new(),
        }
    }
}

/// Device features and capabilities
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DeviceFeature {
    /// Supports double precision floating point
    DoublePrecision,

    /// Supports half precision floating point
    HalfPrecision,

    /// Supports unified memory between host and device
    UnifiedMemory,

    /// Supports atomic operations
    AtomicOperations,

    /// Supports sub-groups/warps
    SubGroups,

    /// Supports printf in kernels
    Printf,

    /// Supports profiling and debugging
    Profiling,

    /// Supports peer-to-peer memory access
    PeerToPeer,

    /// Supports concurrent kernel execution
    ConcurrentExecution,

    /// Supports asynchronous memory operations
    AsyncMemory,

    /// Supports texture/image operations
    ImageSupport,

    /// Supports fast math optimizations
    FastMath,

    // WebGPU-specific features
    /// Supports timestamp queries for performance measurement
    TimestampQuery,

    /// Supports timestamp queries inside encoders
    TimestampQueryInsideEncoders,

    /// Supports pipeline statistics queries
    PipelineStatistics,

    /// Supports mappable primary buffers
    MappableBuffers,

    /// Supports buffer binding arrays
    BufferArrays,

    /// Supports storage resource binding arrays
    StorageArrays,

    /// Supports unsized binding arrays
    UnsizedBindingArray,

    /// Supports indirect first instance parameter
    IndirectFirstInstance,

    /// Supports 16-bit floating point in shaders
    ShaderF16,

    /// Supports 16-bit integers in shaders
    ShaderI16,

    /// Supports shader primitive index
    ShaderPrimitiveIndex,

    /// Supports early depth test in shaders
    ShaderEarlyDepthTest,

    /// Supports multi-draw indirect
    MultiDrawIndirect,

    /// Supports multi-draw indirect with count
    MultiDrawIndirectCount,

    /// Supports multisampled shading
    Multisampling,

    /// Supports texture clear operations
    ClearTexture,

    /// Supports SPIR-V shader passthrough
    SpirvShaderPassthrough,

    /// Custom vendor-specific feature
    Custom(String),
}

/// Device builder for constructing devices with validation
#[derive(Debug, Clone)]
pub struct DeviceBuilder {
    id: usize,
    device_type: Option<CoreDeviceType>,
    name: Option<String>,
    info: DeviceInfo,
}

impl DeviceBuilder {
    pub fn new() -> Self {
        Self {
            id: 0,
            device_type: None,
            name: None,
            info: DeviceInfo::default(),
        }
    }

    pub fn with_id(mut self, id: usize) -> Self {
        self.id = id;
        self
    }

    pub fn with_device_type(mut self, device_type: CoreDeviceType) -> Self {
        self.device_type = Some(device_type);
        self
    }

    pub fn with_name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }

    pub fn with_vendor(mut self, vendor: String) -> Self {
        self.info.vendor = vendor;
        self
    }

    pub fn with_driver_version(mut self, version: String) -> Self {
        self.info.driver_version = version;
        self
    }

    pub fn with_memory(mut self, total: usize, available: usize) -> Self {
        self.info.total_memory = total;
        self.info.available_memory = available;
        self
    }

    pub fn with_compute_units(mut self, units: usize) -> Self {
        self.info.compute_units = units;
        self
    }

    pub fn with_performance(mut self, gflops: f32, bandwidth_gbps: f32) -> Self {
        self.info.peak_gflops = gflops;
        self.info.memory_bandwidth_gbps = bandwidth_gbps;
        self
    }

    pub fn with_feature(mut self, feature: DeviceFeature) -> Self {
        self.info.features.push(feature);
        self
    }

    pub fn with_property(mut self, key: String, value: String) -> Self {
        self.info.properties.push((key, value));
        self
    }

    pub fn build(self) -> crate::BackendResult<Device> {
        let device_type = self.device_type.ok_or_else(|| {
            torsh_core::error::TorshError::BackendError("Device type is required".to_string())
        })?;

        let name = self.name.ok_or_else(|| {
            torsh_core::error::TorshError::BackendError("Device name is required".to_string())
        })?;

        Ok(Device {
            id: self.id,
            device_type,
            name,
            info: self.info,
        })
    }
}

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

/// Device type enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeviceType {
    /// CPU device
    Cpu,

    /// NVIDIA CUDA GPU
    Cuda,

    /// Apple Metal GPU
    Metal,

    /// WebGPU device
    WebGpu,

    /// OpenCL device
    OpenCl,

    /// Vulkan Compute device
    Vulkan,

    /// Custom device type
    Custom,
}

impl From<CoreDeviceType> for DeviceType {
    fn from(core_type: CoreDeviceType) -> Self {
        match core_type {
            CoreDeviceType::Cpu => DeviceType::Cpu,
            CoreDeviceType::Cuda(_) => DeviceType::Cuda,
            CoreDeviceType::Metal(_) => DeviceType::Metal,
            CoreDeviceType::Wgpu(_) => DeviceType::WebGpu,
        }
    }
}

impl From<DeviceType> for CoreDeviceType {
    fn from(device_type: DeviceType) -> Self {
        match device_type {
            DeviceType::Cpu => CoreDeviceType::Cpu,
            DeviceType::Cuda => CoreDeviceType::Cuda(0), // Default to device 0
            DeviceType::Metal => CoreDeviceType::Metal(0), // Default to device 0
            DeviceType::WebGpu => CoreDeviceType::Wgpu(0), // Default to device 0
            DeviceType::OpenCl => CoreDeviceType::Cpu,   // Fallback
            DeviceType::Vulkan => CoreDeviceType::Cpu,   // Fallback
            DeviceType::Custom => CoreDeviceType::Cpu,   // Fallback
        }
    }
}

/// Device selection criteria
#[derive(Default)]
pub struct DeviceSelector {
    /// Preferred device type
    pub device_type: Option<DeviceType>,

    /// Minimum memory requirement in bytes
    pub min_memory: Option<usize>,

    /// Minimum compute units
    pub min_compute_units: Option<usize>,

    /// Required features
    pub required_features: Vec<DeviceFeature>,

    /// Preferred vendor
    pub preferred_vendor: Option<String>,

    /// Custom selection function
    #[allow(clippy::type_complexity)]
    pub custom_filter: Option<Box<dyn Fn(&Device) -> bool + Send + Sync>>,
}

impl DeviceSelector {
    /// Create a new device selector
    pub fn new() -> Self {
        Self::default()
    }

    /// Set preferred device type
    pub fn with_device_type(mut self, device_type: DeviceType) -> Self {
        self.device_type = Some(device_type);
        self
    }

    /// Set minimum memory requirement
    pub fn with_min_memory(mut self, min_memory: usize) -> Self {
        self.min_memory = Some(min_memory);
        self
    }

    /// Set minimum compute units
    pub fn with_min_compute_units(mut self, min_compute_units: usize) -> Self {
        self.min_compute_units = Some(min_compute_units);
        self
    }

    /// Add required feature
    pub fn with_feature(mut self, feature: DeviceFeature) -> Self {
        self.required_features.push(feature);
        self
    }

    /// Set preferred vendor
    pub fn with_vendor(mut self, vendor: String) -> Self {
        self.preferred_vendor = Some(vendor);
        self
    }

    /// Check if a device matches this selector
    pub fn matches(&self, device: &Device) -> bool {
        // Check device type
        if let Some(required_type) = &self.device_type {
            if device.device_type != (*required_type).into() {
                return false;
            }
        }

        // Check memory
        if let Some(min_memory) = self.min_memory {
            if device.info.total_memory < min_memory {
                return false;
            }
        }

        // Check compute units
        if let Some(min_compute_units) = self.min_compute_units {
            if device.info.compute_units < min_compute_units {
                return false;
            }
        }

        // Check required features
        for feature in &self.required_features {
            if !device.supports_feature(feature.clone()) {
                return false;
            }
        }

        // Check vendor
        if let Some(ref preferred_vendor) = self.preferred_vendor {
            if device.info.vendor != *preferred_vendor {
                return false;
            }
        }

        // Apply custom filter
        if let Some(ref filter) = self.custom_filter {
            if !filter(device) {
                return false;
            }
        }

        true
    }
}

/// Unified device management interface for all backends
pub trait DeviceManager: Send + Sync {
    /// Enumerate all available devices for this backend type
    fn enumerate_devices(&self) -> crate::BackendResult<Vec<Device>>;

    /// Get detailed device information by ID
    fn get_device_info(&self, device_id: usize) -> crate::BackendResult<DeviceInfo>;

    /// Check if a device supports specific features
    fn check_device_features(
        &self,
        device_id: usize,
        features: &[DeviceFeature],
    ) -> crate::BackendResult<Vec<bool>>;

    /// Get optimal device configuration for the backend
    fn get_optimal_device_config(
        &self,
        device_id: usize,
    ) -> crate::BackendResult<DeviceConfiguration>;

    /// Validate device availability and readiness
    fn validate_device(&self, device_id: usize) -> crate::BackendResult<bool>;

    /// Get device performance characteristics
    fn get_performance_info(&self, device_id: usize)
        -> crate::BackendResult<DevicePerformanceInfo>;
}

/// Device configuration for optimal performance
#[derive(Debug, Clone)]
pub struct DeviceConfiguration {
    /// Optimal memory allocation size
    pub optimal_allocation_size: usize,

    /// Recommended workgroup/thread block size
    pub workgroup_size: (u32, u32, u32),

    /// Memory alignment requirements
    pub memory_alignment: usize,

    /// Concurrent operation limits
    pub max_concurrent_operations: u32,

    /// Backend-specific configuration
    pub backend_specific: std::collections::HashMap<String, crate::backend::CapabilityValue>,
}

/// Device performance characteristics
#[derive(Debug, Clone)]
pub struct DevicePerformanceInfo {
    /// Memory bandwidth in GB/s
    pub memory_bandwidth_gbps: f32,

    /// Compute throughput in GFLOPS
    pub compute_throughput_gflops: f32,

    /// Memory latency in nanoseconds
    pub memory_latency_ns: f32,

    /// Cache hierarchy information
    pub cache_hierarchy: Vec<CacheLevel>,

    /// Thermal information (if available)
    pub thermal_info: Option<ThermalInfo>,

    /// Power consumption information (if available)
    pub power_info: Option<PowerInfo>,
}

/// Cache level information
#[derive(Debug, Clone)]
pub struct CacheLevel {
    pub level: u8,
    pub size_bytes: usize,
    pub line_size_bytes: usize,
    pub associativity: Option<usize>,
}

/// Thermal monitoring information
#[derive(Debug, Clone)]
pub struct ThermalInfo {
    pub current_temperature_celsius: f32,
    pub max_temperature_celsius: f32,
    pub thermal_throttling_active: bool,
}

/// Power consumption information
#[derive(Debug, Clone)]
pub struct PowerInfo {
    pub current_power_watts: f32,
    pub max_power_watts: f32,
    pub power_limit_watts: f32,
}

/// Common device management utilities that can be shared across backends
pub struct DeviceUtils;

impl DeviceUtils {
    /// Validate device configuration parameters
    pub const fn validate_device_id(device_id: usize, max_devices: usize) -> bool {
        device_id < max_devices
    }

    /// Calculate device score for selection algorithms
    pub fn calculate_device_score(device: &Device, requirements: &DeviceRequirements) -> f32 {
        let mut score = 0.0;

        // Memory requirement scoring
        if let Some(min_memory) = requirements.min_memory {
            if device.info.total_memory >= min_memory {
                score += 20.0;
                // Bonus for having more memory than required
                score += (device.info.total_memory as f32 / min_memory as f32 - 1.0) * 5.0;
            } else {
                return 0.0; // Disqualify if insufficient memory
            }
        }

        // Compute units requirement scoring
        if let Some(min_compute_units) = requirements.min_compute_units {
            if device.info.compute_units >= min_compute_units {
                score += 15.0;
                score += (device.info.compute_units as f32 / min_compute_units as f32 - 1.0) * 3.0;
            } else {
                return 0.0;
            }
        }

        // Features requirement scoring
        for required_feature in &requirements.required_features {
            if device.supports_feature(required_feature.clone()) {
                score += 10.0;
            } else {
                return 0.0; // Disqualify if missing required feature
            }
        }

        // Performance scores
        score += device.info.peak_gflops / 1000.0; // Bonus for compute performance
        score += device.info.memory_bandwidth_gbps / 100.0; // Bonus for memory bandwidth

        // Backend preference
        match DeviceType::from(device.device_type) {
            DeviceType::Cuda => score += 15.0,  // Prefer CUDA
            DeviceType::Metal => score += 10.0, // Then Metal
            DeviceType::WebGpu => score += 5.0, // Then WebGPU
            DeviceType::Cpu => score += 1.0,    // CPU as fallback
            _ => score += 0.0,
        }

        score
    }

    /// Check if device meets minimum requirements
    pub fn meets_requirements(device: &Device, requirements: &DeviceRequirements) -> bool {
        // Check memory requirement
        if let Some(min_memory) = requirements.min_memory {
            if device.info.total_memory < min_memory {
                return false;
            }
        }

        // Check compute units requirement
        if let Some(min_compute_units) = requirements.min_compute_units {
            if device.info.compute_units < min_compute_units {
                return false;
            }
        }

        // Check required features
        for required_feature in &requirements.required_features {
            if !device.supports_feature(required_feature.clone()) {
                return false;
            }
        }

        // Check backend preference
        if let Some(preferred_backend) = requirements.preferred_backend {
            let device_backend = match DeviceType::from(device.device_type) {
                DeviceType::Cpu => crate::backend::BackendType::Cpu,
                DeviceType::Cuda => crate::backend::BackendType::Cuda,
                DeviceType::Metal => crate::backend::BackendType::Metal,
                DeviceType::WebGpu => crate::backend::BackendType::WebGpu,
                _ => return false,
            };
            if device_backend != preferred_backend {
                return false;
            }
        }

        true
    }

    /// Get optimal workgroup/thread block size for device
    pub fn get_optimal_workgroup_size(device: &Device, operation_type: &str) -> (u32, u32, u32) {
        match DeviceType::from(device.device_type) {
            DeviceType::Cuda => {
                // CUDA optimal sizes
                match operation_type {
                    "matrix_mul" => (16, 16, 1),
                    "element_wise" => (256, 1, 1),
                    "reduction" => (512, 1, 1),
                    _ => (32, 32, 1),
                }
            }
            DeviceType::Metal => {
                // Metal optimal sizes
                match operation_type {
                    "matrix_mul" => (16, 16, 1),
                    "element_wise" => (256, 1, 1),
                    "reduction" => (256, 1, 1),
                    _ => (32, 32, 1),
                }
            }
            DeviceType::WebGpu => {
                // WebGPU optimal sizes
                match operation_type {
                    "matrix_mul" => (8, 8, 1),
                    "element_wise" => (64, 1, 1),
                    "reduction" => (64, 1, 1),
                    _ => (8, 8, 1),
                }
            }
            _ => {
                // Default fallback
                (1, 1, 1)
            }
        }
    }
}

/// Common device discovery utilities
pub struct DeviceDiscovery;

impl DeviceDiscovery {
    /// Discover all available devices across all backends
    pub fn discover_all() -> crate::BackendResult<Vec<(crate::backend::BackendType, Vec<Device>)>> {
        let mut all_devices = Vec::new();

        // CPU devices (always available)
        if let Ok(cpu_devices) = Self::discover_cpu_devices() {
            all_devices.push((crate::backend::BackendType::Cpu, cpu_devices));
        }

        // CUDA devices
        #[cfg(feature = "cuda")]
        if let Ok(cuda_devices) = Self::discover_cuda_devices() {
            if !cuda_devices.is_empty() {
                all_devices.push((crate::backend::BackendType::Cuda, cuda_devices));
            }
        }

        // Metal devices
        #[cfg(all(feature = "metal", target_os = "macos"))]
        if let Ok(metal_devices) = Self::discover_metal_devices() {
            if !metal_devices.is_empty() {
                all_devices.push((crate::backend::BackendType::Metal, metal_devices));
            }
        }

        // WebGPU devices
        #[cfg(feature = "webgpu")]
        if let Ok(webgpu_devices) = Self::discover_webgpu_devices() {
            if !webgpu_devices.is_empty() {
                all_devices.push((crate::backend::BackendType::WebGpu, webgpu_devices));
            }
        }

        Ok(all_devices)
    }

    /// Find the best device based on requirements
    pub fn find_best_device(
        requirements: &DeviceRequirements,
    ) -> crate::BackendResult<(crate::backend::BackendType, Device)> {
        let all_devices = Self::discover_all()?;

        let mut best_device = None;
        let mut best_score = 0.0;

        for (backend_type, devices) in all_devices {
            for device in devices {
                let score = Self::score_device(&device, requirements);
                if score > best_score {
                    best_score = score;
                    best_device = Some((backend_type, device));
                }
            }
        }

        best_device.ok_or_else(|| {
            torsh_core::error::TorshError::BackendError(
                "No suitable device found for requirements".to_string(),
            )
        })
    }

    /// Score a device based on requirements
    fn score_device(device: &Device, requirements: &DeviceRequirements) -> f32 {
        DeviceUtils::calculate_device_score(device, requirements)
    }

    /// Discover CPU devices
    fn discover_cpu_devices() -> crate::BackendResult<Vec<Device>> {
        let cpu_device = crate::cpu::CpuDevice::new(0, num_cpus::get())?;
        Ok(vec![cpu_device.to_device()])
    }

    /// Discover CUDA devices
    #[cfg(feature = "cuda")]
    fn discover_cuda_devices() -> crate::BackendResult<Vec<Device>> {
        // Implementation would query CUDA runtime for available devices
        // For now, return empty vector
        Ok(vec![])
    }

    /// Discover Metal devices
    #[cfg(all(feature = "metal", target_os = "macos"))]
    fn discover_metal_devices() -> crate::BackendResult<Vec<Device>> {
        // Implementation would query Metal framework for available devices
        // For now, return empty vector
        Ok(vec![])
    }

    /// Discover WebGPU devices
    #[cfg(feature = "webgpu")]
    fn discover_webgpu_devices() -> crate::BackendResult<Vec<Device>> {
        // Implementation would query WebGPU for available adapters
        // For now, return empty vector
        Ok(vec![])
    }
}

/// Device requirements for selection
#[derive(Debug, Clone, Default)]
pub struct DeviceRequirements {
    pub min_memory: Option<usize>,
    pub min_compute_units: Option<usize>,
    pub required_features: Vec<DeviceFeature>,
    pub preferred_backend: Option<crate::backend::BackendType>,
    pub max_power_consumption: Option<f32>,
    pub max_temperature: Option<f32>,
}

impl DeviceRequirements {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_min_memory(mut self, memory: usize) -> Self {
        self.min_memory = Some(memory);
        self
    }

    pub fn with_min_compute_units(mut self, units: usize) -> Self {
        self.min_compute_units = Some(units);
        self
    }

    pub fn with_feature(mut self, feature: DeviceFeature) -> Self {
        self.required_features.push(feature);
        self
    }

    pub fn with_preferred_backend(mut self, backend: crate::backend::BackendType) -> Self {
        self.preferred_backend = Some(backend);
        self
    }
}

// Manual implementations for Device to work around f32 fields in DeviceInfo
impl Eq for Device {}

impl std::hash::Hash for Device {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state);
        self.device_type.hash(state);
        self.name.hash(state);
        // Skip DeviceInfo fields that contain f32 since they don't implement Hash
        self.info.vendor.hash(state);
        self.info.driver_version.hash(state);
        self.info.total_memory.hash(state);
        self.info.available_memory.hash(state);
        self.info.compute_units.hash(state);
        self.info.max_work_group_size.hash(state);
        self.info.max_work_group_dimensions.hash(state);
        self.info.clock_frequency_mhz.hash(state);
        // Skip memory_bandwidth_gbps and peak_gflops (f32 fields)
        self.info.features.hash(state);
        self.info.properties.hash(state);
    }
}

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

    fn create_test_device_info() -> DeviceInfo {
        DeviceInfo {
            vendor: "Test Vendor".to_string(),
            driver_version: "1.0.0".to_string(),
            total_memory: 8 * 1024 * 1024 * 1024,     // 8GB
            available_memory: 6 * 1024 * 1024 * 1024, // 6GB
            compute_units: 32,
            max_work_group_size: 1024,
            max_work_group_dimensions: vec![1024, 1024, 64],
            clock_frequency_mhz: 1500,
            memory_bandwidth_gbps: 500.0,
            peak_gflops: 10000.0,
            features: vec![
                DeviceFeature::DoublePrecision,
                DeviceFeature::UnifiedMemory,
                DeviceFeature::AtomicOperations,
            ],
            properties: vec![
                ("compute_capability".to_string(), "7.5".to_string()),
                ("warp_size".to_string(), "32".to_string()),
            ],
        }
    }

    #[test]
    fn test_device_creation() {
        let info = create_test_device_info();
        let device = Device::new(
            0,
            CoreDeviceType::Cuda(0),
            "Test GPU".to_string(),
            info.clone(),
        );

        assert_eq!(device.id(), 0);
        assert_eq!(device.name(), "Test GPU");
        assert_eq!(device.device_type(), CoreDeviceType::Cuda(0));
        assert_eq!(device.info().vendor, "Test Vendor");
        assert_eq!(device.info().compute_units, 32);
    }

    #[test]
    fn test_device_feature_support() {
        let info = create_test_device_info();
        let device = Device::new(1, CoreDeviceType::Cpu, "Test CPU".to_string(), info);

        assert!(device.supports_feature(DeviceFeature::DoublePrecision));
        assert!(device.supports_feature(DeviceFeature::UnifiedMemory));
        assert!(device.supports_feature(DeviceFeature::AtomicOperations));
        assert!(!device.supports_feature(DeviceFeature::HalfPrecision));
        assert!(!device.supports_feature(DeviceFeature::SubGroups));
    }

    #[test]
    fn test_device_info_default() {
        let info = DeviceInfo::default();

        assert_eq!(info.vendor, "Unknown");
        assert_eq!(info.driver_version, "Unknown");
        assert_eq!(info.total_memory, 0);
        assert_eq!(info.available_memory, 0);
        assert_eq!(info.compute_units, 1);
        assert_eq!(info.max_work_group_size, 256);
        assert_eq!(info.max_work_group_dimensions, vec![256, 1, 1]);
        assert_eq!(info.clock_frequency_mhz, 1000);
        assert_eq!(info.memory_bandwidth_gbps, 10.0);
        assert_eq!(info.peak_gflops, 100.0);
        assert!(info.features.is_empty());
        assert!(info.properties.is_empty());
    }

    #[test]
    fn test_device_type_conversion() {
        assert_eq!(DeviceType::from(CoreDeviceType::Cpu), DeviceType::Cpu);
        assert_eq!(DeviceType::from(CoreDeviceType::Cuda(0)), DeviceType::Cuda);
        assert_eq!(
            DeviceType::from(CoreDeviceType::Metal(0)),
            DeviceType::Metal
        );
        assert_eq!(
            DeviceType::from(CoreDeviceType::Wgpu(0)),
            DeviceType::WebGpu
        );

        assert_eq!(CoreDeviceType::from(DeviceType::Cpu), CoreDeviceType::Cpu);
        assert_eq!(
            CoreDeviceType::from(DeviceType::Cuda),
            CoreDeviceType::Cuda(0)
        );
        assert_eq!(
            CoreDeviceType::from(DeviceType::Metal),
            CoreDeviceType::Metal(0)
        );
        assert_eq!(
            CoreDeviceType::from(DeviceType::WebGpu),
            CoreDeviceType::Wgpu(0)
        );

        // Fallback conversions
        assert_eq!(
            CoreDeviceType::from(DeviceType::OpenCl),
            CoreDeviceType::Cpu
        );
        assert_eq!(
            CoreDeviceType::from(DeviceType::Vulkan),
            CoreDeviceType::Cpu
        );
        assert_eq!(
            CoreDeviceType::from(DeviceType::Custom),
            CoreDeviceType::Cpu
        );
    }

    #[test]
    fn test_device_feature_variants() {
        let features = [
            DeviceFeature::DoublePrecision,
            DeviceFeature::HalfPrecision,
            DeviceFeature::UnifiedMemory,
            DeviceFeature::AtomicOperations,
            DeviceFeature::SubGroups,
            DeviceFeature::Printf,
            DeviceFeature::Profiling,
            DeviceFeature::PeerToPeer,
            DeviceFeature::ConcurrentExecution,
            DeviceFeature::AsyncMemory,
            DeviceFeature::ImageSupport,
            DeviceFeature::FastMath,
            DeviceFeature::Custom("CustomFeature".to_string()),
        ];

        // Ensure all features are distinct
        for (i, feature1) in features.iter().enumerate() {
            for (j, feature2) in features.iter().enumerate() {
                if i != j {
                    assert_ne!(feature1, feature2);
                }
            }
        }
    }

    #[test]
    fn test_device_selector_creation() {
        let selector = DeviceSelector::new();

        assert_eq!(selector.device_type, None);
        assert_eq!(selector.min_memory, None);
        assert_eq!(selector.min_compute_units, None);
        assert!(selector.required_features.is_empty());
        assert_eq!(selector.preferred_vendor, None);
        assert!(selector.custom_filter.is_none());
    }

    #[test]
    fn test_device_selector_builder() {
        let selector = DeviceSelector::new()
            .with_device_type(DeviceType::Cuda)
            .with_min_memory(4 * 1024 * 1024 * 1024) // 4GB
            .with_min_compute_units(16)
            .with_feature(DeviceFeature::DoublePrecision)
            .with_feature(DeviceFeature::AtomicOperations)
            .with_vendor("NVIDIA".to_string());

        assert_eq!(selector.device_type, Some(DeviceType::Cuda));
        assert_eq!(selector.min_memory, Some(4 * 1024 * 1024 * 1024));
        assert_eq!(selector.min_compute_units, Some(16));
        assert_eq!(selector.required_features.len(), 2);
        assert!(selector
            .required_features
            .contains(&DeviceFeature::DoublePrecision));
        assert!(selector
            .required_features
            .contains(&DeviceFeature::AtomicOperations));
        assert_eq!(selector.preferred_vendor, Some("NVIDIA".to_string()));
    }

    #[test]
    fn test_device_selector_matching() {
        let mut info = create_test_device_info();
        info.vendor = "NVIDIA".to_string();
        info.total_memory = 8 * 1024 * 1024 * 1024; // 8GB
        info.compute_units = 32;

        let device = Device::new(0, CoreDeviceType::Cuda(0), "RTX 4090".to_string(), info);

        // Should match
        let selector1 = DeviceSelector::new()
            .with_device_type(DeviceType::Cuda)
            .with_min_memory(4 * 1024 * 1024 * 1024) // 4GB
            .with_min_compute_units(16)
            .with_feature(DeviceFeature::DoublePrecision)
            .with_vendor("NVIDIA".to_string());

        assert!(selector1.matches(&device));

        // Should not match - insufficient memory
        let selector2 = DeviceSelector::new().with_min_memory(16 * 1024 * 1024 * 1024); // 16GB

        assert!(!selector2.matches(&device));

        // Should not match - missing feature
        let selector3 = DeviceSelector::new().with_feature(DeviceFeature::HalfPrecision);

        assert!(!selector3.matches(&device));

        // Should not match - wrong vendor
        let selector4 = DeviceSelector::new().with_vendor("AMD".to_string());

        assert!(!selector4.matches(&device));
    }

    #[test]
    fn test_custom_device_feature() {
        let custom_feature1 = DeviceFeature::Custom("TensorCores".to_string());
        let custom_feature2 = DeviceFeature::Custom("TensorCores".to_string());
        let custom_feature3 = DeviceFeature::Custom("RTCores".to_string());

        assert_eq!(custom_feature1, custom_feature2);
        assert_ne!(custom_feature1, custom_feature3);

        let mut info = DeviceInfo::default();
        info.features.push(custom_feature1.clone());

        let device = Device::new(0, CoreDeviceType::Cuda(0), "Custom GPU".to_string(), info);
        assert!(device.supports_feature(custom_feature1));
        assert!(!device.supports_feature(custom_feature3));
    }
}