ruvllm 2.2.1

LLM serving runtime with Ruvector integration - Paged attention, KV cache, and SONA learning
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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
#![allow(
    clippy::all,
    unused_imports,
    unused_variables,
    dead_code,
    unused_mut,
    unused_assignments,
    non_camel_case_types,
    clippy::approx_constant,
    unexpected_cfgs,
    unused_must_use,
    unused_parens
)]
//! Integration tests for v2.1 cross-platform features
//!
//! Tests cover:
//! - Platform-specific fallbacks
//! - WASM-specific detection and limitations
//! - Feature detection across platforms
//! - Graceful degradation
//! - Runtime capability checking

#![allow(non_camel_case_types)]

// =============================================================================
// Platform Types
// =============================================================================

/// Target platform
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Platform {
    MacOS,
    Linux,
    Windows,
    iOS,
    Android,
    WebAssembly,
    Unknown,
}

impl Platform {
    /// Detect current platform at compile time
    pub fn current() -> Self {
        #[cfg(target_os = "macos")]
        return Platform::MacOS;

        #[cfg(target_os = "linux")]
        return Platform::Linux;

        #[cfg(target_os = "windows")]
        return Platform::Windows;

        #[cfg(target_os = "ios")]
        return Platform::iOS;

        #[cfg(target_os = "android")]
        return Platform::Android;

        #[cfg(target_arch = "wasm32")]
        return Platform::WebAssembly;

        #[cfg(not(any(
            target_os = "macos",
            target_os = "linux",
            target_os = "windows",
            target_os = "ios",
            target_os = "android",
            target_arch = "wasm32"
        )))]
        return Platform::Unknown;
    }

    /// Check if platform supports Metal
    pub fn supports_metal(&self) -> bool {
        matches!(self, Platform::MacOS | Platform::iOS)
    }

    /// Check if platform supports CUDA
    pub fn supports_cuda(&self) -> bool {
        matches!(self, Platform::Linux | Platform::Windows)
    }

    /// Check if platform supports WebGPU
    pub fn supports_webgpu(&self) -> bool {
        matches!(
            self,
            Platform::MacOS | Platform::Linux | Platform::Windows | Platform::WebAssembly
        )
    }

    /// Check if platform supports native file I/O
    pub fn supports_native_io(&self) -> bool {
        !matches!(self, Platform::WebAssembly)
    }

    /// Check if platform supports multi-threading
    pub fn supports_threading(&self) -> bool {
        !matches!(self, Platform::WebAssembly)
    }

    /// Get maximum recommended batch size for platform
    pub fn max_recommended_batch_size(&self) -> usize {
        match self {
            Platform::MacOS | Platform::Linux | Platform::Windows => 64,
            Platform::iOS | Platform::Android => 16,
            Platform::WebAssembly => 4,
            Platform::Unknown => 1,
        }
    }
}

/// CPU architecture
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Architecture {
    X86_64,
    Aarch64,
    Wasm32,
    Unknown,
}

impl Architecture {
    /// Detect current architecture at compile time
    pub fn current() -> Self {
        #[cfg(target_arch = "x86_64")]
        return Architecture::X86_64;

        #[cfg(target_arch = "aarch64")]
        return Architecture::Aarch64;

        #[cfg(target_arch = "wasm32")]
        return Architecture::Wasm32;

        #[cfg(not(any(
            target_arch = "x86_64",
            target_arch = "aarch64",
            target_arch = "wasm32"
        )))]
        return Architecture::Unknown;
    }

    /// Check if architecture supports SIMD
    pub fn supports_simd(&self) -> bool {
        !matches!(self, Architecture::Unknown)
    }

    /// Get SIMD width in bytes
    pub fn simd_width(&self) -> usize {
        match self {
            Architecture::X86_64 => 32,  // AVX2
            Architecture::Aarch64 => 16, // NEON
            Architecture::Wasm32 => 16,  // SIMD128
            Architecture::Unknown => 0,
        }
    }
}

// =============================================================================
// CPU Features
// =============================================================================

/// CPU feature flags
#[derive(Debug, Clone, Default)]
pub struct CpuFeatures {
    // x86_64 features
    pub sse: bool,
    pub sse2: bool,
    pub sse3: bool,
    pub ssse3: bool,
    pub sse4_1: bool,
    pub sse4_2: bool,
    pub avx: bool,
    pub avx2: bool,
    pub avx512f: bool,
    pub avx512vl: bool,
    pub avx512vnni: bool,
    pub fma: bool,
    pub f16c: bool,

    // ARM features
    pub neon: bool,
    pub fp16: bool,
    pub dotprod: bool,
    pub i8mm: bool,
    pub sve: bool,
    pub sve2: bool,

    // WASM features
    pub simd128: bool,
    pub relaxed_simd: bool,
}

impl CpuFeatures {
    /// Detect CPU features at runtime
    pub fn detect() -> Self {
        let mut features = Self::default();

        #[cfg(target_arch = "x86_64")]
        {
            #[cfg(target_feature = "sse")]
            {
                features.sse = true;
            }
            #[cfg(target_feature = "sse2")]
            {
                features.sse2 = true;
            }
            #[cfg(target_feature = "sse3")]
            {
                features.sse3 = true;
            }
            #[cfg(target_feature = "ssse3")]
            {
                features.ssse3 = true;
            }
            #[cfg(target_feature = "sse4.1")]
            {
                features.sse4_1 = true;
            }
            #[cfg(target_feature = "sse4.2")]
            {
                features.sse4_2 = true;
            }
            #[cfg(target_feature = "avx")]
            {
                features.avx = true;
            }
            #[cfg(target_feature = "avx2")]
            {
                features.avx2 = true;
            }
            #[cfg(target_feature = "fma")]
            {
                features.fma = true;
            }
            #[cfg(target_feature = "f16c")]
            {
                features.f16c = true;
            }
        }

        #[cfg(target_arch = "aarch64")]
        {
            // NEON is always available on aarch64
            features.neon = true;

            #[cfg(target_feature = "fp16")]
            {
                features.fp16 = true;
            }
            #[cfg(target_feature = "dotprod")]
            {
                features.dotprod = true;
            }
        }

        #[cfg(target_arch = "wasm32")]
        {
            #[cfg(target_feature = "simd128")]
            {
                features.simd128 = true;
            }
            #[cfg(target_feature = "relaxed-simd")]
            {
                features.relaxed_simd = true;
            }
        }

        features
    }

    /// Create feature set for a mock x86_64 system with AVX2
    pub fn mock_x86_64_avx2() -> Self {
        Self {
            sse: true,
            sse2: true,
            sse3: true,
            ssse3: true,
            sse4_1: true,
            sse4_2: true,
            avx: true,
            avx2: true,
            fma: true,
            f16c: true,
            ..Default::default()
        }
    }

    /// Create feature set for a mock ARM system with NEON
    pub fn mock_aarch64_neon() -> Self {
        Self {
            neon: true,
            fp16: true,
            dotprod: true,
            ..Default::default()
        }
    }

    /// Create feature set for a mock WASM environment
    pub fn mock_wasm_simd() -> Self {
        Self {
            simd128: true,
            ..Default::default()
        }
    }

    /// Check if the system supports fast matrix operations
    pub fn supports_fast_matmul(&self) -> bool {
        self.avx2 || self.neon || self.simd128
    }

    /// Check if the system supports native FP16
    pub fn supports_native_fp16(&self) -> bool {
        self.f16c || self.fp16
    }

    /// Check if the system supports INT8 dot products
    pub fn supports_int8_dotprod(&self) -> bool {
        self.avx512vnni || self.dotprod || self.i8mm
    }
}

// =============================================================================
// GPU Capabilities
// =============================================================================

/// GPU backend type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GpuBackend {
    Metal,
    Cuda,
    Vulkan,
    WebGPU,
    None,
}

/// GPU capabilities
#[derive(Debug, Clone)]
pub struct GpuCapabilities {
    pub backend: GpuBackend,
    pub device_name: String,
    pub compute_units: u32,
    pub memory_bytes: u64,
    pub supports_fp16: bool,
    pub supports_int8: bool,
    pub supports_bf16: bool,
    pub max_buffer_size: u64,
    pub max_workgroup_size: u32,
    pub unified_memory: bool,
}

impl GpuCapabilities {
    /// Create mock Metal capabilities (Apple Silicon)
    pub fn mock_metal_m4() -> Self {
        Self {
            backend: GpuBackend::Metal,
            device_name: "Apple M4 Pro".to_string(),
            compute_units: 20,
            memory_bytes: 48 * 1024 * 1024 * 1024, // 48GB unified
            supports_fp16: true,
            supports_int8: true,
            supports_bf16: true,
            max_buffer_size: 48 * 1024 * 1024 * 1024,
            max_workgroup_size: 1024,
            unified_memory: true,
        }
    }

    /// Create mock CUDA capabilities
    pub fn mock_cuda_4090() -> Self {
        Self {
            backend: GpuBackend::Cuda,
            device_name: "NVIDIA GeForce RTX 4090".to_string(),
            compute_units: 128,
            memory_bytes: 24 * 1024 * 1024 * 1024, // 24GB VRAM
            supports_fp16: true,
            supports_int8: true,
            supports_bf16: true,
            max_buffer_size: 24 * 1024 * 1024 * 1024,
            max_workgroup_size: 1024,
            unified_memory: false,
        }
    }

    /// Create mock WebGPU capabilities
    pub fn mock_webgpu() -> Self {
        Self {
            backend: GpuBackend::WebGPU,
            device_name: "WebGPU Device".to_string(),
            compute_units: 8,
            memory_bytes: 4 * 1024 * 1024 * 1024, // 4GB typical
            supports_fp16: true,
            supports_int8: false,
            supports_bf16: false,
            max_buffer_size: 2 * 1024 * 1024 * 1024, // 2GB buffer limit
            max_workgroup_size: 256,
            unified_memory: false,
        }
    }

    /// Create capabilities when no GPU is available
    pub fn none() -> Self {
        Self {
            backend: GpuBackend::None,
            device_name: "CPU Only".to_string(),
            compute_units: 0,
            memory_bytes: 0,
            supports_fp16: false,
            supports_int8: false,
            supports_bf16: false,
            max_buffer_size: 0,
            max_workgroup_size: 0,
            unified_memory: false,
        }
    }

    /// Check if GPU is available
    pub fn is_available(&self) -> bool {
        self.backend != GpuBackend::None
    }

    /// Calculate maximum model size that fits in memory
    pub fn max_model_size(&self) -> u64 {
        if self.unified_memory {
            self.memory_bytes * 9 / 10 // 90% of unified memory
        } else {
            self.memory_bytes * 8 / 10 // 80% of VRAM
        }
    }
}

// =============================================================================
// System Capabilities
// =============================================================================

/// Complete system capabilities
#[derive(Debug, Clone)]
pub struct SystemCapabilities {
    pub platform: Platform,
    pub architecture: Architecture,
    pub cpu_features: CpuFeatures,
    pub gpu: GpuCapabilities,
    pub system_memory_bytes: u64,
    pub cpu_cores: usize,
}

impl SystemCapabilities {
    /// Detect system capabilities
    pub fn detect() -> Self {
        Self {
            platform: Platform::current(),
            architecture: Architecture::current(),
            cpu_features: CpuFeatures::detect(),
            gpu: GpuCapabilities::none(), // Would need async detection
            system_memory_bytes: 0,       // Would need system calls
            cpu_cores: 1,                 // Would need system calls
        }
    }

    /// Create mock capabilities for Apple Silicon Mac
    pub fn mock_mac_m4() -> Self {
        Self {
            platform: Platform::MacOS,
            architecture: Architecture::Aarch64,
            cpu_features: CpuFeatures::mock_aarch64_neon(),
            gpu: GpuCapabilities::mock_metal_m4(),
            system_memory_bytes: 48 * 1024 * 1024 * 1024,
            cpu_cores: 14,
        }
    }

    /// Create mock capabilities for Linux with CUDA
    pub fn mock_linux_cuda() -> Self {
        Self {
            platform: Platform::Linux,
            architecture: Architecture::X86_64,
            cpu_features: CpuFeatures::mock_x86_64_avx2(),
            gpu: GpuCapabilities::mock_cuda_4090(),
            system_memory_bytes: 64 * 1024 * 1024 * 1024,
            cpu_cores: 16,
        }
    }

    /// Create mock capabilities for WebAssembly
    pub fn mock_wasm() -> Self {
        Self {
            platform: Platform::WebAssembly,
            architecture: Architecture::Wasm32,
            cpu_features: CpuFeatures::mock_wasm_simd(),
            gpu: GpuCapabilities::mock_webgpu(),
            system_memory_bytes: 4 * 1024 * 1024 * 1024, // Limited in browser
            cpu_cores: 4,                                // Typical worker count
        }
    }

    /// Create mock capabilities for CPU-only system
    pub fn mock_cpu_only() -> Self {
        Self {
            platform: Platform::Linux,
            architecture: Architecture::X86_64,
            cpu_features: CpuFeatures::mock_x86_64_avx2(),
            gpu: GpuCapabilities::none(),
            system_memory_bytes: 32 * 1024 * 1024 * 1024,
            cpu_cores: 8,
        }
    }

    /// Get the best available compute backend
    pub fn best_backend(&self) -> ComputeBackend {
        if self.gpu.is_available() {
            match self.gpu.backend {
                GpuBackend::Metal => ComputeBackend::Metal,
                GpuBackend::Cuda => ComputeBackend::Cuda,
                GpuBackend::WebGPU => ComputeBackend::WebGPU,
                _ => ComputeBackend::Cpu,
            }
        } else {
            ComputeBackend::Cpu
        }
    }
}

/// Compute backend selection
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ComputeBackend {
    Metal,
    Cuda,
    WebGPU,
    Cpu,
}

// =============================================================================
// Fallback System
// =============================================================================

/// Backend fallback chain
pub struct FallbackChain {
    backends: Vec<ComputeBackend>,
}

impl FallbackChain {
    /// Create a fallback chain for the given capabilities
    pub fn for_capabilities(caps: &SystemCapabilities) -> Self {
        let mut backends = Vec::new();

        // Add GPU backend if available
        if caps.gpu.is_available() {
            backends.push(caps.best_backend());
        }

        // Add CPU as final fallback
        if !backends.contains(&ComputeBackend::Cpu) {
            backends.push(ComputeBackend::Cpu);
        }

        Self { backends }
    }

    /// Get the primary backend
    pub fn primary(&self) -> ComputeBackend {
        self.backends
            .first()
            .copied()
            .unwrap_or(ComputeBackend::Cpu)
    }

    /// Get all backends in order
    pub fn all(&self) -> &[ComputeBackend] {
        &self.backends
    }

    /// Check if a backend is available
    pub fn has(&self, backend: ComputeBackend) -> bool {
        self.backends.contains(&backend)
    }

    /// Get fallback for a failed backend
    pub fn fallback_for(&self, failed: ComputeBackend) -> Option<ComputeBackend> {
        let pos = self.backends.iter().position(|&b| b == failed)?;
        self.backends.get(pos + 1).copied()
    }
}

// =============================================================================
// WASM-Specific Utilities
// =============================================================================

/// WASM-specific limitations and workarounds
pub struct WasmLimitations {
    /// Maximum memory in bytes (due to 32-bit address space)
    pub max_memory: u64,
    /// Whether SharedArrayBuffer is available (for threading)
    pub has_shared_memory: bool,
    /// Whether SIMD128 is available
    pub has_simd: bool,
    /// Whether atomics are available
    pub has_atomics: bool,
    /// Maximum single allocation size
    pub max_allocation: u64,
}

impl WasmLimitations {
    /// Create with typical browser limitations
    pub fn typical_browser() -> Self {
        Self {
            max_memory: 4 * 1024 * 1024 * 1024, // 4GB
            has_shared_memory: false,           // Requires COOP/COEP headers
            has_simd: true,
            has_atomics: false,
            max_allocation: 2 * 1024 * 1024 * 1024, // 2GB single alloc
        }
    }

    /// Create with enhanced browser limitations (with headers)
    pub fn enhanced_browser() -> Self {
        Self {
            max_memory: 4 * 1024 * 1024 * 1024,
            has_shared_memory: true,
            has_simd: true,
            has_atomics: true,
            max_allocation: 2 * 1024 * 1024 * 1024,
        }
    }

    /// Create for Node.js environment
    pub fn nodejs() -> Self {
        Self {
            max_memory: 4 * 1024 * 1024 * 1024,
            has_shared_memory: true,
            has_simd: true,
            has_atomics: true,
            max_allocation: 2 * 1024 * 1024 * 1024,
        }
    }

    /// Check if multi-threading is possible
    pub fn can_multithread(&self) -> bool {
        self.has_shared_memory && self.has_atomics
    }

    /// Get recommended thread count
    pub fn recommended_threads(&self) -> usize {
        if self.can_multithread() {
            4 // Typical worker count in browsers
        } else {
            1
        }
    }

    /// Calculate maximum model size given limitations
    pub fn max_model_size(&self) -> u64 {
        // Leave headroom for runtime and other allocations
        self.max_memory * 7 / 10 // 70% of max memory
    }
}

// =============================================================================
// Configuration Generator
// =============================================================================

/// Optimal configuration for a given system
#[derive(Debug, Clone)]
pub struct OptimalConfig {
    pub backend: ComputeBackend,
    pub batch_size: usize,
    pub context_length: usize,
    pub thread_count: usize,
    pub quantization: QuantizationType,
    pub use_flash_attention: bool,
    pub use_kv_cache: bool,
    pub memory_mapped_weights: bool,
}

/// Quantization type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuantizationType {
    F32,
    F16,
    BF16,
    Q8_0,
    Q4_0,
    Q4_K,
}

impl OptimalConfig {
    /// Generate optimal configuration for given capabilities
    pub fn for_capabilities(caps: &SystemCapabilities, model_size_bytes: u64) -> Self {
        let backend = caps.best_backend();

        // Determine quantization based on model size and memory
        let available_memory = if caps.gpu.is_available() {
            caps.gpu.max_model_size()
        } else {
            caps.system_memory_bytes * 7 / 10
        };

        let quantization = if model_size_bytes <= available_memory {
            if caps.cpu_features.supports_native_fp16() || caps.gpu.supports_fp16 {
                QuantizationType::F16
            } else {
                QuantizationType::F32
            }
        } else if model_size_bytes / 2 <= available_memory {
            QuantizationType::Q8_0
        } else {
            QuantizationType::Q4_K
        };

        // Determine batch size
        let batch_size = caps.platform.max_recommended_batch_size();

        // Context length based on memory
        let context_length = match backend {
            ComputeBackend::Metal => 8192,
            ComputeBackend::Cuda => 8192,
            ComputeBackend::WebGPU => 2048,
            ComputeBackend::Cpu => 4096,
        };

        // Thread count
        let thread_count = if caps.platform.supports_threading() {
            caps.cpu_cores.min(8)
        } else {
            1
        };

        // Flash attention availability
        let use_flash_attention = matches!(backend, ComputeBackend::Metal | ComputeBackend::Cuda);

        // Memory mapping (not available in WASM)
        let memory_mapped_weights = caps.platform.supports_native_io();

        Self {
            backend,
            batch_size,
            context_length,
            thread_count,
            quantization,
            use_flash_attention,
            use_kv_cache: true,
            memory_mapped_weights,
        }
    }

    /// Generate WASM-specific configuration
    pub fn for_wasm(limits: &WasmLimitations, model_size_bytes: u64) -> Self {
        let quantization = if model_size_bytes <= limits.max_model_size() {
            QuantizationType::F16
        } else if model_size_bytes / 2 <= limits.max_model_size() {
            QuantizationType::Q8_0
        } else {
            QuantizationType::Q4_K
        };

        Self {
            backend: ComputeBackend::WebGPU,
            batch_size: 4,
            context_length: 2048,
            thread_count: limits.recommended_threads(),
            quantization,
            use_flash_attention: false,
            use_kv_cache: true,
            memory_mapped_weights: false, // Not available in WASM
        }
    }
}

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

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

    // -------------------------------------------------------------------------
    // Platform Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_platform_detection() {
        let platform = Platform::current();
        // Just verify it returns something valid
        assert!(matches!(
            platform,
            Platform::MacOS
                | Platform::Linux
                | Platform::Windows
                | Platform::iOS
                | Platform::Android
                | Platform::WebAssembly
                | Platform::Unknown
        ));
    }

    #[test]
    fn test_platform_metal_support() {
        assert!(Platform::MacOS.supports_metal());
        assert!(Platform::iOS.supports_metal());
        assert!(!Platform::Linux.supports_metal());
        assert!(!Platform::Windows.supports_metal());
        assert!(!Platform::WebAssembly.supports_metal());
    }

    #[test]
    fn test_platform_cuda_support() {
        assert!(Platform::Linux.supports_cuda());
        assert!(Platform::Windows.supports_cuda());
        assert!(!Platform::MacOS.supports_cuda());
        assert!(!Platform::WebAssembly.supports_cuda());
    }

    #[test]
    fn test_platform_webgpu_support() {
        assert!(Platform::MacOS.supports_webgpu());
        assert!(Platform::Linux.supports_webgpu());
        assert!(Platform::Windows.supports_webgpu());
        assert!(Platform::WebAssembly.supports_webgpu());
        assert!(!Platform::iOS.supports_webgpu());
    }

    #[test]
    fn test_platform_native_io() {
        assert!(Platform::MacOS.supports_native_io());
        assert!(Platform::Linux.supports_native_io());
        assert!(!Platform::WebAssembly.supports_native_io());
    }

    #[test]
    fn test_platform_threading() {
        assert!(Platform::MacOS.supports_threading());
        assert!(Platform::Linux.supports_threading());
        assert!(!Platform::WebAssembly.supports_threading());
    }

    #[test]
    fn test_platform_batch_sizes() {
        assert!(Platform::MacOS.max_recommended_batch_size() >= 32);
        assert!(Platform::iOS.max_recommended_batch_size() <= 32);
        assert!(Platform::WebAssembly.max_recommended_batch_size() <= 8);
    }

    // -------------------------------------------------------------------------
    // Architecture Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_architecture_detection() {
        let arch = Architecture::current();
        assert!(matches!(
            arch,
            Architecture::X86_64
                | Architecture::Aarch64
                | Architecture::Wasm32
                | Architecture::Unknown
        ));
    }

    #[test]
    fn test_architecture_simd_support() {
        assert!(Architecture::X86_64.supports_simd());
        assert!(Architecture::Aarch64.supports_simd());
        assert!(Architecture::Wasm32.supports_simd());
        assert!(!Architecture::Unknown.supports_simd());
    }

    #[test]
    fn test_architecture_simd_width() {
        assert_eq!(Architecture::X86_64.simd_width(), 32); // AVX2
        assert_eq!(Architecture::Aarch64.simd_width(), 16); // NEON
        assert_eq!(Architecture::Wasm32.simd_width(), 16); // SIMD128
        assert_eq!(Architecture::Unknown.simd_width(), 0);
    }

    // -------------------------------------------------------------------------
    // CPU Features Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_cpu_features_x86_64_mock() {
        let features = CpuFeatures::mock_x86_64_avx2();
        assert!(features.sse);
        assert!(features.sse2);
        assert!(features.avx);
        assert!(features.avx2);
        assert!(features.fma);
    }

    #[test]
    fn test_cpu_features_aarch64_mock() {
        let features = CpuFeatures::mock_aarch64_neon();
        assert!(features.neon);
        assert!(features.fp16);
        assert!(features.dotprod);
    }

    #[test]
    fn test_cpu_features_wasm_mock() {
        let features = CpuFeatures::mock_wasm_simd();
        assert!(features.simd128);
        assert!(!features.avx2);
        assert!(!features.neon);
    }

    #[test]
    fn test_cpu_features_fast_matmul() {
        let x86 = CpuFeatures::mock_x86_64_avx2();
        assert!(x86.supports_fast_matmul());

        let arm = CpuFeatures::mock_aarch64_neon();
        assert!(arm.supports_fast_matmul());

        let wasm = CpuFeatures::mock_wasm_simd();
        assert!(wasm.supports_fast_matmul());

        let none = CpuFeatures::default();
        assert!(!none.supports_fast_matmul());
    }

    #[test]
    fn test_cpu_features_native_fp16() {
        let x86 = CpuFeatures::mock_x86_64_avx2();
        assert!(x86.supports_native_fp16()); // f16c

        let arm = CpuFeatures::mock_aarch64_neon();
        assert!(arm.supports_native_fp16()); // fp16

        let wasm = CpuFeatures::mock_wasm_simd();
        assert!(!wasm.supports_native_fp16());
    }

    // -------------------------------------------------------------------------
    // GPU Capabilities Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_gpu_metal_mock() {
        let gpu = GpuCapabilities::mock_metal_m4();
        assert_eq!(gpu.backend, GpuBackend::Metal);
        assert!(gpu.unified_memory);
        assert!(gpu.supports_fp16);
        assert!(gpu.supports_bf16);
    }

    #[test]
    fn test_gpu_cuda_mock() {
        let gpu = GpuCapabilities::mock_cuda_4090();
        assert_eq!(gpu.backend, GpuBackend::Cuda);
        assert!(!gpu.unified_memory);
        assert!(gpu.supports_fp16);
    }

    #[test]
    fn test_gpu_webgpu_mock() {
        let gpu = GpuCapabilities::mock_webgpu();
        assert_eq!(gpu.backend, GpuBackend::WebGPU);
        assert!(gpu.supports_fp16);
        assert!(!gpu.supports_int8); // Typically not supported
    }

    #[test]
    fn test_gpu_none() {
        let gpu = GpuCapabilities::none();
        assert_eq!(gpu.backend, GpuBackend::None);
        assert!(!gpu.is_available());
    }

    #[test]
    fn test_gpu_max_model_size() {
        let metal = GpuCapabilities::mock_metal_m4();
        let cuda = GpuCapabilities::mock_cuda_4090();

        // Unified memory allows larger models
        assert!(metal.max_model_size() > cuda.max_model_size());
    }

    // -------------------------------------------------------------------------
    // System Capabilities Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_system_capabilities_mac() {
        let caps = SystemCapabilities::mock_mac_m4();
        assert_eq!(caps.platform, Platform::MacOS);
        assert_eq!(caps.architecture, Architecture::Aarch64);
        assert_eq!(caps.best_backend(), ComputeBackend::Metal);
    }

    #[test]
    fn test_system_capabilities_linux_cuda() {
        let caps = SystemCapabilities::mock_linux_cuda();
        assert_eq!(caps.platform, Platform::Linux);
        assert_eq!(caps.architecture, Architecture::X86_64);
        assert_eq!(caps.best_backend(), ComputeBackend::Cuda);
    }

    #[test]
    fn test_system_capabilities_wasm() {
        let caps = SystemCapabilities::mock_wasm();
        assert_eq!(caps.platform, Platform::WebAssembly);
        assert_eq!(caps.architecture, Architecture::Wasm32);
        assert_eq!(caps.best_backend(), ComputeBackend::WebGPU);
    }

    #[test]
    fn test_system_capabilities_cpu_only() {
        let caps = SystemCapabilities::mock_cpu_only();
        assert_eq!(caps.best_backend(), ComputeBackend::Cpu);
    }

    // -------------------------------------------------------------------------
    // Fallback Chain Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_fallback_chain_metal() {
        let caps = SystemCapabilities::mock_mac_m4();
        let chain = FallbackChain::for_capabilities(&caps);

        assert_eq!(chain.primary(), ComputeBackend::Metal);
        assert!(chain.has(ComputeBackend::Cpu));
        assert_eq!(
            chain.fallback_for(ComputeBackend::Metal),
            Some(ComputeBackend::Cpu)
        );
    }

    #[test]
    fn test_fallback_chain_cpu_only() {
        let caps = SystemCapabilities::mock_cpu_only();
        let chain = FallbackChain::for_capabilities(&caps);

        assert_eq!(chain.primary(), ComputeBackend::Cpu);
        assert_eq!(chain.all().len(), 1);
        assert_eq!(chain.fallback_for(ComputeBackend::Cpu), None);
    }

    #[test]
    fn test_fallback_chain_order() {
        let caps = SystemCapabilities::mock_linux_cuda();
        let chain = FallbackChain::for_capabilities(&caps);

        let backends = chain.all();
        assert_eq!(backends[0], ComputeBackend::Cuda);
        assert_eq!(backends[1], ComputeBackend::Cpu);
    }

    // -------------------------------------------------------------------------
    // WASM Limitations Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_wasm_limitations_typical() {
        let limits = WasmLimitations::typical_browser();
        assert!(!limits.has_shared_memory);
        assert!(!limits.can_multithread());
        assert_eq!(limits.recommended_threads(), 1);
    }

    #[test]
    fn test_wasm_limitations_enhanced() {
        let limits = WasmLimitations::enhanced_browser();
        assert!(limits.has_shared_memory);
        assert!(limits.has_atomics);
        assert!(limits.can_multithread());
        assert!(limits.recommended_threads() > 1);
    }

    #[test]
    fn test_wasm_limitations_nodejs() {
        let limits = WasmLimitations::nodejs();
        assert!(limits.can_multithread());
        assert!(limits.has_simd);
    }

    #[test]
    fn test_wasm_max_model_size() {
        let limits = WasmLimitations::typical_browser();
        let max_size = limits.max_model_size();
        assert!(max_size < limits.max_memory);
        assert!(max_size > 0);
    }

    // -------------------------------------------------------------------------
    // Optimal Configuration Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_optimal_config_mac() {
        let caps = SystemCapabilities::mock_mac_m4();
        let model_size = 7 * 1024 * 1024 * 1024; // 7B model (~7GB)

        let config = OptimalConfig::for_capabilities(&caps, model_size);

        assert_eq!(config.backend, ComputeBackend::Metal);
        assert!(config.use_flash_attention);
        assert!(config.memory_mapped_weights);
        assert!(config.thread_count > 1);
    }

    #[test]
    fn test_optimal_config_cuda() {
        let caps = SystemCapabilities::mock_linux_cuda();
        let model_size = 13 * 1024 * 1024 * 1024; // 13B model

        let config = OptimalConfig::for_capabilities(&caps, model_size);

        assert_eq!(config.backend, ComputeBackend::Cuda);
        assert!(config.use_flash_attention);
    }

    #[test]
    fn test_optimal_config_quantization_fallback() {
        let caps = SystemCapabilities::mock_cpu_only();
        let model_size = 70 * 1024 * 1024 * 1024; // 70B model - too large

        let config = OptimalConfig::for_capabilities(&caps, model_size);

        // Should fall back to aggressive quantization
        assert!(matches!(
            config.quantization,
            QuantizationType::Q4_0 | QuantizationType::Q4_K | QuantizationType::Q8_0
        ));
    }

    #[test]
    fn test_optimal_config_wasm() {
        let limits = WasmLimitations::typical_browser();
        let model_size = 2 * 1024 * 1024 * 1024; // 2B model

        let config = OptimalConfig::for_wasm(&limits, model_size);

        assert_eq!(config.backend, ComputeBackend::WebGPU);
        assert!(!config.use_flash_attention);
        assert!(!config.memory_mapped_weights);
        assert!(config.context_length <= 4096);
        assert!(config.batch_size <= 8);
    }

    #[test]
    fn test_optimal_config_small_model() {
        let caps = SystemCapabilities::mock_mac_m4();
        let model_size = 1 * 1024 * 1024 * 1024; // 1GB model

        let config = OptimalConfig::for_capabilities(&caps, model_size);

        // Small model should use FP16, not quantized
        assert!(matches!(
            config.quantization,
            QuantizationType::F16 | QuantizationType::F32
        ));
    }

    // -------------------------------------------------------------------------
    // Integration Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_full_detection_pipeline() {
        // Test the full detection -> configuration pipeline
        let caps = SystemCapabilities::detect();

        // Should always return valid values
        assert!(caps.cpu_cores == 0 || caps.cpu_cores >= 1);

        let chain = FallbackChain::for_capabilities(&caps);
        assert!(!chain.all().is_empty());

        // Generate config for a 7B model
        let config = OptimalConfig::for_capabilities(&caps, 7 * 1024 * 1024 * 1024);
        assert!(config.batch_size >= 1);
        assert!(config.context_length >= 512);
    }

    #[test]
    fn test_platform_specific_defaults() {
        // Test that each platform gets sensible defaults
        let platforms = vec![
            SystemCapabilities::mock_mac_m4(),
            SystemCapabilities::mock_linux_cuda(),
            SystemCapabilities::mock_wasm(),
            SystemCapabilities::mock_cpu_only(),
        ];

        for caps in platforms {
            let config = OptimalConfig::for_capabilities(&caps, 4 * 1024 * 1024 * 1024);

            // Basic sanity checks
            assert!(config.batch_size >= 1);
            assert!(config.context_length >= 512);
            assert!(config.thread_count >= 1);
            assert!(config.use_kv_cache); // Always enabled
        }
    }

    #[test]
    fn test_graceful_degradation() {
        // Start with high-end system
        let mut caps = SystemCapabilities::mock_linux_cuda();

        // Remove GPU
        caps.gpu = GpuCapabilities::none();

        let config = OptimalConfig::for_capabilities(&caps, 7 * 1024 * 1024 * 1024);

        // Should fall back to CPU
        assert_eq!(config.backend, ComputeBackend::Cpu);
        assert!(!config.use_flash_attention); // Not available on CPU
    }

    #[test]
    fn test_memory_constrained_config() {
        // Very limited memory
        let mut caps = SystemCapabilities::mock_cpu_only();
        caps.system_memory_bytes = 8 * 1024 * 1024 * 1024; // 8GB only

        // Try to load a large model
        let model_size = 30 * 1024 * 1024 * 1024; // 30GB

        let config = OptimalConfig::for_capabilities(&caps, model_size);

        // Should use aggressive quantization
        assert!(matches!(
            config.quantization,
            QuantizationType::Q4_0 | QuantizationType::Q4_K
        ));
    }
}