trustformers-mobile 0.1.1

Mobile deployment support for TrustformeRS (iOS, Android)
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
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
//! Advanced Mobile Quantization for TrustformeRS
//!
//! This module provides advanced quantization techniques specifically optimized
//! for mobile devices, including mixed-precision quantization, dynamic quantization,
//! and hardware-aware quantization strategies.

use crate::{
    device_info::{MobileDeviceInfo, PerformanceTier},
    optimization::memory_pool::MobileMemoryPool,
    Result,
};
use half::f16;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use trustformers_core::errors::{invalid_config, invalid_input};
use trustformers_core::Tensor;

/// Advanced mobile quantization engine
pub struct MobileQuantizationEngine {
    config: QuantizationConfig,
    device_info: MobileDeviceInfo,
    calibration_data: Option<CalibrationDataset>,
    quantization_cache: Arc<Mutex<HashMap<String, QuantizedModel>>>,
    memory_pool: Option<Arc<MobileMemoryPool>>,
}

/// Mobile quantization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizationConfig {
    /// Target quantization precision
    pub target_precision: MobilePrecision,
    /// Enable mixed-precision quantization
    pub enable_mixed_precision: bool,
    /// Dynamic quantization strategy
    pub dynamic_strategy: DynamicQuantizationStrategy,
    /// Hardware-aware optimizations
    pub hardware_aware: bool,
    /// Quantization granularity
    pub granularity: QuantizationGranularity,
    /// Quality preservation threshold
    pub quality_threshold: f32,
    /// Memory constraint (MB)
    pub memory_constraint_mb: usize,
    /// Enable gradient-based quantization
    pub enable_gradient_quantization: bool,
    /// KL-divergence threshold for calibration
    pub kl_threshold: f32,
    /// Enable post-training quantization
    pub enable_ptq: bool,
    /// Enable quantization-aware training
    pub enable_qat: bool,
}

/// Mobile-optimized precision levels
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum MobilePrecision {
    /// 4-bit quantization (ultra-low memory)
    INT4,
    /// 8-bit quantization (standard mobile)
    INT8,
    /// 16-bit float (high quality)
    FP16,
    /// Mixed 4-bit and 8-bit
    Mixed4_8,
    /// Mixed 8-bit and 16-bit
    Mixed8_16,
    /// Dynamic precision selection
    DYNAMIC,
}

/// Dynamic quantization strategies
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum DynamicQuantizationStrategy {
    /// Adjust based on battery level
    BatteryAware,
    /// Adjust based on thermal state
    ThermalAware,
    /// Adjust based on memory pressure
    MemoryAware,
    /// Adjust based on performance requirements
    PerformanceAware,
    /// Combined adaptive strategy
    Adaptive,
}

/// Quantization granularity options
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum QuantizationGranularity {
    /// Per-tensor quantization
    PerTensor,
    /// Per-channel quantization
    PerChannel,
    /// Per-group quantization
    PerGroup { group_size: usize },
    /// Per-layer adaptive
    PerLayer,
}

/// Calibration dataset for quantization
#[derive(Debug, Clone)]
pub struct CalibrationDataset {
    /// Representative data samples
    pub samples: Vec<Tensor>,
    /// Sample weights for importance
    pub weights: Option<Vec<f32>>,
    /// Statistical properties
    pub statistics: DatasetStatistics,
}

/// Statistical properties of calibration data
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DatasetStatistics {
    /// Per-layer activation ranges
    pub activation_ranges: HashMap<String, (f32, f32)>,
    /// Per-layer mean values
    pub layer_means: HashMap<String, f32>,
    /// Per-layer variance values
    pub layer_variances: HashMap<String, f32>,
    /// KL-divergence scores
    pub kl_scores: HashMap<String, f32>,
}

/// Quantized model representation
#[derive(Debug, Clone)]
pub struct QuantizedModel {
    /// Quantized weights
    pub weights: HashMap<String, QuantizedTensor>,
    /// Quantization parameters
    pub parameters: QuantizationParameters,
    /// Model metadata
    pub metadata: ModelMetadata,
    /// Performance benchmarks
    pub benchmarks: QuantizationBenchmarks,
}

/// Quantized tensor with metadata
#[derive(Debug, Clone)]
pub struct QuantizedTensor {
    /// Quantized data
    pub data: Vec<i8>,
    /// Scale factors
    pub scales: Vec<f32>,
    /// Zero points
    pub zero_points: Vec<i32>,
    /// Original shape
    pub shape: Vec<usize>,
    /// Quantization scheme
    pub scheme: QuantizationScheme,
}

/// Quantization scheme details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizationScheme {
    /// Bits per weight
    pub bits: u8,
    /// Symmetric vs asymmetric
    pub symmetric: bool,
    /// Signed vs unsigned
    pub signed: bool,
    /// Quantization method
    pub method: QuantizationMethod,
}

/// Quantization methods
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum QuantizationMethod {
    /// Linear quantization
    Linear,
    /// Logarithmic quantization
    Logarithmic,
    /// Power-of-2 quantization
    PowerOfTwo,
    /// K-means clustering
    KMeans,
    /// Learned quantization
    Learned,
}

/// Model format detection
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ModelFormat {
    /// SafeTensors format
    SafeTensors,
    /// PyTorch pickle format
    PyTorchPickle,
    /// TensorFlow SavedModel format
    TensorFlow,
    /// ONNX format
    ONNX,
    /// Custom format
    Custom,
}

/// Quantization parameters for inference
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizationParameters {
    /// Global scale factor
    pub global_scale: f32,
    /// Per-layer scales
    pub layer_scales: HashMap<String, f32>,
    /// Per-layer zero points
    pub layer_zero_points: HashMap<String, i32>,
    /// Dequantization overhead
    pub dequant_overhead_ms: f32,
}

/// Model metadata for quantized models
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelMetadata {
    /// Original model size (bytes)
    pub original_size_bytes: usize,
    /// Quantized model size (bytes)
    pub quantized_size_bytes: usize,
    /// Compression ratio
    pub compression_ratio: f32,
    /// Quality score (0.0 - 1.0)
    pub quality_score: f32,
    /// Quantization timestamp
    pub timestamp: std::time::SystemTime,
}

/// Quantization performance benchmarks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantizationBenchmarks {
    /// Original inference time (ms)
    pub original_inference_ms: f32,
    /// Quantized inference time (ms)
    pub quantized_inference_ms: f32,
    /// Speedup factor
    pub speedup_factor: f32,
    /// Memory reduction (MB)
    pub memory_reduction_mb: f32,
    /// Power reduction (mW)
    pub power_reduction_mw: f32,
}

impl MobileQuantizationEngine {
    /// Create a new mobile quantization engine
    pub fn new(
        config: QuantizationConfig,
        device_info: MobileDeviceInfo,
        memory_pool: Option<Arc<MobileMemoryPool>>,
    ) -> Result<Self> {
        Ok(Self {
            config,
            device_info,
            calibration_data: None,
            quantization_cache: Arc::new(Mutex::new(HashMap::new())),
            memory_pool,
        })
    }

    /// Set calibration dataset for quantization
    pub fn set_calibration_data(&mut self, dataset: CalibrationDataset) -> Result<()> {
        // Validate calibration data
        if dataset.samples.is_empty() {
            return Err(invalid_config(
                "set_calibration_data",
                "Calibration dataset cannot be empty",
            ));
        }

        self.calibration_data = Some(dataset);
        Ok(())
    }

    /// Quantize model with mobile optimizations
    pub fn quantize_model(&self, model_id: &str, model_data: &[u8]) -> Result<QuantizedModel> {
        // Check cache first
        {
            let cache = self.quantization_cache.lock().expect("Operation failed");
            if let Some(cached_model) = cache.get(model_id) {
                return Ok(cached_model.clone());
            }
        }

        // Determine optimal quantization strategy
        let strategy = self.determine_quantization_strategy()?;

        // Apply hardware-specific optimizations
        let hardware_config = self.get_hardware_quantization_config()?;

        // Perform quantization
        let quantized_model = self.perform_quantization(model_data, &strategy, &hardware_config)?;

        // Benchmark the quantized model
        let benchmarks = self.benchmark_quantized_model(&quantized_model)?;

        let final_model = QuantizedModel {
            weights: quantized_model.weights,
            parameters: quantized_model.parameters,
            metadata: quantized_model.metadata,
            benchmarks,
        };

        // Cache the result
        {
            let mut cache = self.quantization_cache.lock().expect("Operation failed");
            cache.insert(model_id.to_string(), final_model.clone());
        }

        Ok(final_model)
    }

    /// Determine optimal quantization strategy based on device capabilities
    fn determine_quantization_strategy(&self) -> Result<MobilePrecision> {
        match (
            &self.device_info.performance_scores.overall_tier,
            &self.config.target_precision,
        ) {
            (PerformanceTier::High, MobilePrecision::DYNAMIC) => {
                // High-end devices can handle mixed precision
                Ok(MobilePrecision::Mixed8_16)
            },
            (PerformanceTier::Mid, MobilePrecision::DYNAMIC) => {
                // Mid-range devices benefit from INT8
                Ok(MobilePrecision::INT8)
            },
            (PerformanceTier::Budget, MobilePrecision::DYNAMIC) => {
                // Low-end devices need aggressive quantization
                Ok(MobilePrecision::Mixed4_8)
            },
            (_, precision) => Ok(*precision),
        }
    }

    /// Get hardware-specific quantization configuration
    fn get_hardware_quantization_config(&self) -> Result<HardwareQuantizationConfig> {
        let mut config = HardwareQuantizationConfig::default();

        // Configure for specific hardware
        if self.device_info.npu_info.is_some() {
            config.use_npu_kernels = true;
            config.preferred_precision = MobilePrecision::INT8;
        }

        if self.device_info.gpu_info.is_some() {
            config.use_gpu_kernels = true;
            config.gpu_memory_optimization = true;
        }

        // ARM-specific optimizations
        if self.device_info.cpu_info.architecture.contains("arm")
            || self.device_info.cpu_info.architecture.contains("aarch64")
        {
            config.use_neon_instructions = true;
            config.arm_specific_kernels = true;
        }

        Ok(config)
    }

    /// Perform the actual quantization
    fn perform_quantization(
        &self,
        model_data: &[u8],
        strategy: &MobilePrecision,
        hardware_config: &HardwareQuantizationConfig,
    ) -> Result<QuantizedModel> {
        // Parse model weights (simplified)
        let weights = self.parse_model_weights(model_data)?;

        // Apply quantization based on strategy
        let quantized_weights = match strategy {
            MobilePrecision::INT4 => self.quantize_to_int4(&weights)?,
            MobilePrecision::INT8 => self.quantize_to_int8(&weights)?,
            MobilePrecision::FP16 => self.quantize_to_fp16(&weights)?,
            MobilePrecision::Mixed4_8 => self.quantize_mixed_4_8(&weights)?,
            MobilePrecision::Mixed8_16 => self.quantize_mixed_8_16(&weights)?,
            MobilePrecision::DYNAMIC => self.quantize_dynamic(&weights)?,
        };

        // Calculate quantization parameters
        let parameters = self.calculate_quantization_parameters(&quantized_weights)?;

        // Generate metadata
        let metadata = ModelMetadata {
            original_size_bytes: model_data.len(),
            quantized_size_bytes: self.calculate_quantized_size(&quantized_weights),
            compression_ratio: model_data.len() as f32
                / self.calculate_quantized_size(&quantized_weights) as f32,
            quality_score: self.estimate_quality_score(&quantized_weights)?,
            timestamp: std::time::SystemTime::now(),
        };

        Ok(QuantizedModel {
            weights: quantized_weights,
            parameters,
            metadata,
            benchmarks: QuantizationBenchmarks::default(), // Will be filled by benchmark_quantized_model
        })
    }

    /// Quantize weights to 4-bit integers
    fn quantize_to_int4(
        &self,
        weights: &HashMap<String, Tensor>,
    ) -> Result<HashMap<String, QuantizedTensor>> {
        let mut quantized = HashMap::new();

        for (layer_name, tensor) in weights {
            let tensor_data = tensor.data()?.to_vec();

            // Calculate scale and zero point for 4-bit quantization
            let min_val = tensor_data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
            let max_val = tensor_data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));

            let scale = (max_val - min_val) / 15.0; // 4-bit range: 0-15
            let zero_point = (-min_val / scale).round() as i32;

            // Quantize to 4-bit values (stored as i8)
            let quantized_data: Vec<i8> = tensor_data
                .iter()
                .map(|&x| {
                    let quantized = ((x / scale) + zero_point as f32).round();
                    quantized.max(0.0).min(15.0) as i8
                })
                .collect();

            let quantized_tensor = QuantizedTensor {
                data: quantized_data,
                scales: vec![scale],
                zero_points: vec![zero_point],
                shape: tensor.shape().to_vec(),
                scheme: QuantizationScheme {
                    bits: 4,
                    symmetric: false,
                    signed: false,
                    method: QuantizationMethod::Linear,
                },
            };

            quantized.insert(layer_name.clone(), quantized_tensor);
        }

        Ok(quantized)
    }

    /// Quantize weights to 8-bit integers
    fn quantize_to_int8(
        &self,
        weights: &HashMap<String, Tensor>,
    ) -> Result<HashMap<String, QuantizedTensor>> {
        let mut quantized = HashMap::new();

        for (layer_name, tensor) in weights {
            let tensor_data = tensor.data()?.to_vec();

            // Calculate scale and zero point for 8-bit quantization
            let min_val = tensor_data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
            let max_val = tensor_data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));

            let scale = (max_val - min_val) / 255.0; // 8-bit range: 0-255
            let zero_point = (-min_val / scale).round() as i32;

            // Quantize to 8-bit values
            let quantized_data: Vec<i8> = tensor_data
                .iter()
                .map(|&x| {
                    let quantized = ((x / scale) + zero_point as f32).round();
                    (quantized.max(0.0).min(255.0) as i32 - 128) as i8 // Convert to signed
                })
                .collect();

            let quantized_tensor = QuantizedTensor {
                data: quantized_data,
                scales: vec![scale],
                zero_points: vec![zero_point],
                shape: tensor.shape().to_vec(),
                scheme: QuantizationScheme {
                    bits: 8,
                    symmetric: false,
                    signed: true,
                    method: QuantizationMethod::Linear,
                },
            };

            quantized.insert(layer_name.clone(), quantized_tensor);
        }

        Ok(quantized)
    }

    /// Quantize weights to FP16
    fn quantize_to_fp16(
        &self,
        weights: &HashMap<String, Tensor>,
    ) -> Result<HashMap<String, QuantizedTensor>> {
        // For FP16, we just need to convert precision, no quantization parameters needed
        let mut quantized = HashMap::new();

        for (layer_name, tensor) in weights {
            let tensor_data = tensor.data()?.to_vec();

            // Convert to FP16 (stored as i8 pairs for compatibility)
            let quantized_data: Vec<i8> = tensor_data
                .iter()
                .flat_map(|&x| {
                    let fp16_bits = f16::from_f32(x).to_bits();
                    [(fp16_bits & 0xFF) as i8, ((fp16_bits >> 8) & 0xFF) as i8]
                })
                .collect();

            let quantized_tensor = QuantizedTensor {
                data: quantized_data,
                scales: vec![1.0], // No scaling needed for FP16
                zero_points: vec![0],
                shape: tensor.shape().to_vec(),
                scheme: QuantizationScheme {
                    bits: 16,
                    symmetric: true,
                    signed: true,
                    method: QuantizationMethod::Linear,
                },
            };

            quantized.insert(layer_name.clone(), quantized_tensor);
        }

        Ok(quantized)
    }

    /// Mixed 4-bit and 8-bit quantization
    fn quantize_mixed_4_8(
        &self,
        weights: &HashMap<String, Tensor>,
    ) -> Result<HashMap<String, QuantizedTensor>> {
        let mut quantized = HashMap::new();

        for (layer_name, tensor) in weights {
            // Determine if this layer should use 4-bit or 8-bit
            let use_4bit = self.should_use_4bit_for_layer(layer_name, tensor)?;

            if use_4bit {
                let quantized_4bit = self.quantize_to_int4(
                    &[(layer_name.clone(), tensor.clone())].iter().cloned().collect(),
                )?;
                quantized.extend(quantized_4bit);
            } else {
                let quantized_8bit = self.quantize_to_int8(
                    &[(layer_name.clone(), tensor.clone())].iter().cloned().collect(),
                )?;
                quantized.extend(quantized_8bit);
            }
        }

        Ok(quantized)
    }

    /// Mixed 8-bit and 16-bit quantization
    fn quantize_mixed_8_16(
        &self,
        weights: &HashMap<String, Tensor>,
    ) -> Result<HashMap<String, QuantizedTensor>> {
        let mut quantized = HashMap::new();

        for (layer_name, tensor) in weights {
            // Determine if this layer should use 8-bit or 16-bit
            let use_8bit = self.should_use_8bit_for_layer(layer_name, tensor)?;

            if use_8bit {
                let quantized_8bit = self.quantize_to_int8(
                    &[(layer_name.clone(), tensor.clone())].iter().cloned().collect(),
                )?;
                quantized.extend(quantized_8bit);
            } else {
                let quantized_16bit = self.quantize_to_fp16(
                    &[(layer_name.clone(), tensor.clone())].iter().cloned().collect(),
                )?;
                quantized.extend(quantized_16bit);
            }
        }

        Ok(quantized)
    }

    /// Dynamic quantization based on runtime conditions
    fn quantize_dynamic(
        &self,
        weights: &HashMap<String, Tensor>,
    ) -> Result<HashMap<String, QuantizedTensor>> {
        // Start with INT8 as baseline, can be adjusted at runtime
        self.quantize_to_int8(weights)
    }

    /// Determine if a layer should use 4-bit quantization
    fn should_use_4bit_for_layer(&self, layer_name: &str, tensor: &Tensor) -> Result<bool> {
        // Use 4-bit for less critical layers (embeddings, some linear layers)
        let is_embedding = layer_name.contains("embed") || layer_name.contains("token");
        let is_output = layer_name.contains("output") || layer_name.contains("head");
        let is_large = tensor.shape().iter().product::<usize>() > 1000000;

        Ok(is_embedding || (is_large && !is_output))
    }

    /// Determine if a layer should use 8-bit quantization
    fn should_use_8bit_for_layer(&self, layer_name: &str, tensor: &Tensor) -> Result<bool> {
        // Use 8-bit for most layers, 16-bit for critical ones
        let is_attention = layer_name.contains("attn") || layer_name.contains("attention");
        let is_output = layer_name.contains("output") || layer_name.contains("head");
        let is_norm = layer_name.contains("norm") || layer_name.contains("ln");

        Ok(!(is_attention || is_output || is_norm))
    }

    /// Advanced model weight parsing with format detection
    fn parse_model_weights(&self, model_data: &[u8]) -> Result<HashMap<String, Tensor>> {
        // Enhanced model parsing with format detection and error handling
        #[allow(dead_code)]
        let mut weights = HashMap::new();

        // Detect model format by magic bytes
        let format = self.detect_model_format(model_data)?;

        match format {
            ModelFormat::SafeTensors => {
                weights = self.parse_safetensors(model_data)?;
            },
            ModelFormat::PyTorchPickle => {
                weights = self.parse_pytorch_pickle(model_data)?;
            },
            ModelFormat::TensorFlow => {
                weights = self.parse_tensorflow(model_data)?;
            },
            ModelFormat::ONNX => {
                weights = self.parse_onnx(model_data)?;
            },
            ModelFormat::Custom => {
                weights = self.parse_custom_format(model_data)?;
            },
        }

        // Validate parsed weights
        self.validate_parsed_weights(&weights)?;

        Ok(weights)
    }

    /// Calculate comprehensive quantization parameters
    fn calculate_quantization_parameters(
        &self,
        weights: &HashMap<String, QuantizedTensor>,
    ) -> Result<QuantizationParameters> {
        let mut layer_scales = HashMap::new();
        let mut layer_zero_points = HashMap::new();
        let mut total_dequant_overhead = 0.0;

        // Calculate per-layer parameters
        for (layer_name, quantized_tensor) in weights {
            // Get the primary scale and zero point
            let scale = quantized_tensor.scales.first().copied().unwrap_or(1.0);
            let zero_point = quantized_tensor.zero_points.first().copied().unwrap_or(0);

            layer_scales.insert(layer_name.clone(), scale);
            layer_zero_points.insert(layer_name.clone(), zero_point);

            // Estimate dequantization overhead based on tensor size and quantization scheme
            let tensor_size = quantized_tensor.data.len();
            let overhead_factor = match quantized_tensor.scheme.bits {
                4 => 0.05,  // 4-bit requires more work to unpack
                8 => 0.03,  // 8-bit is straightforward
                16 => 0.01, // 16-bit (FP16) is native on many devices
                _ => 0.04,  // Default for other bit widths
            };

            total_dequant_overhead += (tensor_size as f32 * overhead_factor) / 1000.0;
            // Convert to ms
        }

        // Calculate global scale as weighted average
        let total_elements: f32 = weights.values().map(|t| t.data.len() as f32).sum();
        let global_scale = if total_elements > 0.0 {
            layer_scales.values().sum::<f32>() / layer_scales.len() as f32
        } else {
            1.0
        };

        Ok(QuantizationParameters {
            global_scale,
            layer_scales,
            layer_zero_points,
            dequant_overhead_ms: total_dequant_overhead,
        })
    }

    /// Calculate precise quantized model size
    fn calculate_quantized_size(&self, weights: &HashMap<String, QuantizedTensor>) -> usize {
        let mut total_size = 0;

        for (layer_name, quantized_tensor) in weights {
            // Data size
            let data_size = quantized_tensor.data.len();

            // Metadata size (scales, zero points, shape info)
            let metadata_size = quantized_tensor.scales.len() * 4 + // 4 bytes per f32 scale
                               quantized_tensor.zero_points.len() * 4 + // 4 bytes per i32 zero point
                               quantized_tensor.shape.len() * 8 + // 8 bytes per usize dimension
                               layer_name.len() + 32; // Layer name + quantization scheme overhead

            total_size += data_size + metadata_size;
        }

        total_size
    }

    /// Advanced quality score estimation using multiple metrics
    fn estimate_quality_score(&self, weights: &HashMap<String, QuantizedTensor>) -> Result<f32> {
        if weights.is_empty() {
            return Ok(1.0);
        }

        let mut total_quality = 0.0;
        let mut total_weight = 0.0;

        for (layer_name, quantized_tensor) in weights {
            let layer_weight = quantized_tensor.data.len() as f32;

            // Quality estimation based on quantization scheme
            let base_quality = match quantized_tensor.scheme.bits {
                4 => 0.85,  // 4-bit typically has more quality loss
                8 => 0.93,  // 8-bit is quite good
                16 => 0.98, // 16-bit (FP16) is very close to original
                _ => 0.90,  // Default for other bit widths
            };

            // Adjust quality based on layer type
            let layer_quality_factor = self.get_layer_quality_factor(layer_name);
            let adjusted_quality = base_quality * layer_quality_factor;

            // Weight by tensor size (larger tensors have more impact)
            total_quality += adjusted_quality * layer_weight;
            total_weight += layer_weight;
        }

        let overall_quality = if total_weight > 0.0 { total_quality / total_weight } else { 1.0 };

        // Apply calibration data influence if available
        let calibration_factor = if let Some(ref cal_data) = self.calibration_data {
            self.estimate_calibration_quality_impact(cal_data)?
        } else {
            0.95 // Slight penalty for no calibration
        };

        Ok((overall_quality * calibration_factor).min(1.0))
    }

    /// Comprehensive quantized model benchmarking
    fn benchmark_quantized_model(&self, model: &QuantizedModel) -> Result<QuantizationBenchmarks> {
        let mut benchmarks = QuantizationBenchmarks::default();

        // Estimate original inference time based on model size
        let original_params = model.metadata.original_size_bytes / 4; // Assume FP32
        benchmarks.original_inference_ms =
            self.estimate_inference_time(original_params, MobilePrecision::FP16)?;

        // Estimate quantized inference time
        let quantized_params = model.metadata.quantized_size_bytes;
        let avg_precision = self.estimate_average_precision(&model.weights);
        benchmarks.quantized_inference_ms =
            self.estimate_inference_time(quantized_params, avg_precision)?;

        // Calculate speedup factor
        benchmarks.speedup_factor = if benchmarks.quantized_inference_ms > 0.0 {
            benchmarks.original_inference_ms / benchmarks.quantized_inference_ms
        } else {
            1.0
        };

        // Calculate memory reduction
        benchmarks.memory_reduction_mb = (model.metadata.original_size_bytes
            - model.metadata.quantized_size_bytes) as f32
            / (1024.0 * 1024.0);

        // Estimate power reduction based on precision and device characteristics
        benchmarks.power_reduction_mw = self.estimate_power_reduction(&model.weights)?;

        Ok(benchmarks)
    }

    // Helper methods for enhanced functionality

    /// Detect model format from magic bytes
    fn detect_model_format(&self, data: &[u8]) -> Result<ModelFormat> {
        if data.len() < 8 {
            return Err(invalid_input("Model data too small to detect format"));
        }

        // Check for SafeTensors magic bytes
        if data.starts_with(b"STFR") || data.starts_with(&[0x53, 0x54, 0x46, 0x52]) {
            return Ok(ModelFormat::SafeTensors);
        }

        // Check for PyTorch pickle magic bytes
        if data.starts_with(&[0x80, 0x02]) || data.starts_with(&[0x80, 0x03]) {
            return Ok(ModelFormat::PyTorchPickle);
        }

        // Check for TensorFlow SavedModel
        if data.starts_with(b"TF") {
            return Ok(ModelFormat::TensorFlow);
        }

        // Check for ONNX
        if data.starts_with(&[0x08, 0x01]) {
            return Ok(ModelFormat::ONNX);
        }

        // Default to custom format
        Ok(ModelFormat::Custom)
    }

    /// Parse SafeTensors format
    fn parse_safetensors(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
        // Simplified SafeTensors parsing
        Ok(HashMap::new())
    }

    /// Parse PyTorch pickle format
    fn parse_pytorch_pickle(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
        // Simplified PyTorch pickle parsing
        Ok(HashMap::new())
    }

    /// Parse TensorFlow format
    fn parse_tensorflow(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
        // Simplified TensorFlow parsing
        Ok(HashMap::new())
    }

    /// Parse ONNX format
    fn parse_onnx(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
        // Simplified ONNX parsing
        Ok(HashMap::new())
    }

    /// Parse custom format
    fn parse_custom_format(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
        // Simplified custom format parsing
        Ok(HashMap::new())
    }

    /// Validate parsed weights
    fn validate_parsed_weights(&self, weights: &HashMap<String, Tensor>) -> Result<()> {
        if weights.is_empty() {
            return Err(invalid_input("No weights found in model"));
        }

        for (layer_name, tensor) in weights {
            // Check for valid tensor dimensions
            if tensor.shape().is_empty() {
                return Err(invalid_input(format!(
                    "Invalid tensor shape for layer: {}",
                    layer_name
                )));
            }

            // Check for reasonable tensor sizes
            let total_elements: usize = tensor.shape().iter().product();
            if total_elements == 0 {
                return Err(invalid_input(format!(
                    "Empty tensor for layer: {}",
                    layer_name
                )));
            }

            // Check for extremely large tensors that might cause issues
            if total_elements > 100_000_000 {
                tracing::warn!(
                    "Large tensor detected in layer {}: {} elements",
                    layer_name,
                    total_elements
                );
            }
        }

        Ok(())
    }

    /// Get quality factor based on layer type
    fn get_layer_quality_factor(&self, layer_name: &str) -> f32 {
        // Different layer types have different sensitivity to quantization
        if layer_name.contains("output") || layer_name.contains("head") {
            0.95 // Output layers are more sensitive
        } else if layer_name.contains("attention") || layer_name.contains("attn") {
            0.92 // Attention layers are quite sensitive
        } else if layer_name.contains("norm") || layer_name.contains("ln") {
            0.98 // Normalization layers are less sensitive
        } else if layer_name.contains("embed") || layer_name.contains("token") {
            0.90 // Embedding layers can tolerate more quantization
        } else {
            1.0 // Default for other layers
        }
    }

    /// Estimate calibration quality impact
    fn estimate_calibration_quality_impact(&self, cal_data: &CalibrationDataset) -> Result<f32> {
        // More calibration samples generally lead to better quality
        let sample_factor = (cal_data.samples.len() as f32 / 100.0).min(1.0);

        // Check if we have good statistical coverage
        let stats_quality =
            if !cal_data.statistics.activation_ranges.is_empty() { 1.0 } else { 0.9 };

        Ok(0.95 + 0.05 * sample_factor * stats_quality)
    }

    /// Estimate inference time based on parameters and precision
    fn estimate_inference_time(&self, params: usize, precision: MobilePrecision) -> Result<f32> {
        // Base computation time per parameter (in microseconds)
        let base_time_per_param = match self.device_info.performance_scores.overall_tier {
            PerformanceTier::VeryLow => 0.01,    // Very slow devices
            PerformanceTier::Low => 0.008,       // Low-end devices
            PerformanceTier::Budget => 0.005,    // Entry-level devices
            PerformanceTier::Medium => 0.003,    // Medium-range devices
            PerformanceTier::Mid => 0.002,       // Mid-range devices
            PerformanceTier::High => 0.001,      // High-end devices
            PerformanceTier::VeryHigh => 0.0007, // Very high-end devices
            PerformanceTier::Flagship => 0.0005, // Premium flagship devices
        };

        // Precision multiplier
        let precision_factor = match precision {
            MobilePrecision::INT4 => 0.5,
            MobilePrecision::INT8 => 0.7,
            MobilePrecision::FP16 => 1.0,
            MobilePrecision::Mixed4_8 => 0.6,
            MobilePrecision::Mixed8_16 => 0.85,
            MobilePrecision::DYNAMIC => 0.8,
        };

        // Hardware acceleration factor
        let hw_factor = if self.device_info.npu_info.is_some() {
            0.6
        } else if self.device_info.gpu_info.is_some() {
            0.8
        } else {
            1.0
        };

        let total_time = params as f32 * base_time_per_param * precision_factor * hw_factor;
        Ok(total_time)
    }

    /// Estimate average precision from quantized weights
    fn estimate_average_precision(
        &self,
        weights: &HashMap<String, QuantizedTensor>,
    ) -> MobilePrecision {
        if weights.is_empty() {
            return MobilePrecision::FP16;
        }

        let mut total_bits = 0;
        let mut total_tensors = 0;

        for tensor in weights.values() {
            total_bits += tensor.scheme.bits as u32;
            total_tensors += 1;
        }

        let avg_bits = total_bits as f32 / total_tensors as f32;

        match avg_bits.round() as u8 {
            4 => MobilePrecision::INT4,
            8 => MobilePrecision::INT8,
            16 => MobilePrecision::FP16,
            _ => MobilePrecision::INT8, // Default fallback
        }
    }

    /// Estimate power reduction from quantization
    fn estimate_power_reduction(&self, weights: &HashMap<String, QuantizedTensor>) -> Result<f32> {
        let mut total_power_reduction = 0.0;

        for tensor in weights.values() {
            let tensor_size = tensor.data.len() as f32;

            // Power reduction per operation based on bit width
            let reduction_per_op = match tensor.scheme.bits {
                4 => 0.08,  // 8mW reduction per 1000 operations
                8 => 0.05,  // 5mW reduction per 1000 operations
                16 => 0.02, // 2mW reduction per 1000 operations
                _ => 0.04,  // Default
            };

            total_power_reduction += tensor_size * reduction_per_op / 1000.0;
        }

        Ok(total_power_reduction)
    }
}

/// Hardware-specific quantization configuration
#[derive(Debug, Clone)]
struct HardwareQuantizationConfig {
    use_npu_kernels: bool,
    use_gpu_kernels: bool,
    use_neon_instructions: bool,
    arm_specific_kernels: bool,
    gpu_memory_optimization: bool,
    preferred_precision: MobilePrecision,
}

impl Default for HardwareQuantizationConfig {
    fn default() -> Self {
        Self {
            use_npu_kernels: false,
            use_gpu_kernels: false,
            use_neon_instructions: false,
            arm_specific_kernels: false,
            gpu_memory_optimization: false,
            preferred_precision: MobilePrecision::INT8,
        }
    }
}

impl Default for QuantizationBenchmarks {
    fn default() -> Self {
        Self {
            original_inference_ms: 0.0,
            quantized_inference_ms: 0.0,
            speedup_factor: 1.0,
            memory_reduction_mb: 0.0,
            power_reduction_mw: 0.0,
        }
    }
}

impl Default for QuantizationConfig {
    fn default() -> Self {
        Self {
            target_precision: MobilePrecision::INT8,
            enable_mixed_precision: true,
            dynamic_strategy: DynamicQuantizationStrategy::Adaptive,
            hardware_aware: true,
            granularity: QuantizationGranularity::PerChannel,
            quality_threshold: 0.9,
            memory_constraint_mb: 512,
            enable_gradient_quantization: false,
            kl_threshold: 0.01,
            enable_ptq: true,
            enable_qat: false,
        }
    }
}

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

    #[test]
    fn test_model_format_detection() {
        let engine = create_test_engine();

        // Test SafeTensors format
        let safetensors_data = b"STFR\x00\x00\x00\x00test data";
        let format = engine.detect_model_format(safetensors_data).expect("Operation failed");
        assert_eq!(format, ModelFormat::SafeTensors);

        // Test PyTorch pickle format
        let pytorch_data = b"\x80\x02test data";
        let format = engine.detect_model_format(pytorch_data).expect("Operation failed");
        assert_eq!(format, ModelFormat::PyTorchPickle);

        // Test TensorFlow format
        let tf_data = b"TFtest data";
        let format = engine.detect_model_format(tf_data).expect("Operation failed");
        assert_eq!(format, ModelFormat::TensorFlow);

        // Test ONNX format
        let onnx_data = b"\x08\x01test data";
        let format = engine.detect_model_format(onnx_data).expect("Operation failed");
        assert_eq!(format, ModelFormat::ONNX);

        // Test custom format
        let custom_data = b"custom test data";
        let format = engine.detect_model_format(custom_data).expect("Operation failed");
        assert_eq!(format, ModelFormat::Custom);
    }

    #[test]
    fn test_quantization_parameters_calculation() {
        let engine = create_test_engine();
        let weights = create_test_quantized_weights();

        let params = engine.calculate_quantization_parameters(&weights).expect("Operation failed");

        assert!(params.global_scale > 0.0);
        assert!(!params.layer_scales.is_empty());
        assert!(!params.layer_zero_points.is_empty());
        assert!(params.dequant_overhead_ms >= 0.0);
    }

    #[test]
    fn test_quality_score_estimation() {
        let engine = create_test_engine();
        let weights = create_test_quantized_weights();

        let quality = engine.estimate_quality_score(&weights).expect("Operation failed");

        assert!((0.0..=1.0).contains(&quality));
    }

    #[test]
    fn test_layer_quality_factors() {
        let engine = create_test_engine();

        // Test different layer types
        assert_eq!(engine.get_layer_quality_factor("model.output.weight"), 0.95);
        assert_eq!(
            engine.get_layer_quality_factor("model.attention.weight"),
            0.92
        );
        assert_eq!(
            engine.get_layer_quality_factor("model.layer_norm.weight"),
            0.98
        );
        assert_eq!(
            engine.get_layer_quality_factor("model.embedding.weight"),
            0.90
        );
        assert_eq!(engine.get_layer_quality_factor("model.hidden.weight"), 1.0);
    }

    #[test]
    fn test_inference_time_estimation() {
        let engine = create_test_engine();

        let time = engine
            .estimate_inference_time(1000, MobilePrecision::INT8)
            .expect("Operation failed");
        assert!(time > 0.0);

        let time_fp16 = engine
            .estimate_inference_time(1000, MobilePrecision::FP16)
            .expect("Operation failed");
        let time_int4 = engine
            .estimate_inference_time(1000, MobilePrecision::INT4)
            .expect("Operation failed");

        // INT4 should be faster than FP16
        assert!(time_int4 < time_fp16);
    }

    #[test]
    fn test_power_reduction_estimation() {
        let engine = create_test_engine();
        let weights = create_test_quantized_weights();

        let power_reduction = engine.estimate_power_reduction(&weights).expect("Operation failed");
        assert!(power_reduction >= 0.0);
    }

    #[test]
    fn test_quantized_size_calculation() {
        let engine = create_test_engine();
        let weights = create_test_quantized_weights();

        let size = engine.calculate_quantized_size(&weights);
        assert!(size > 0);
    }

    #[test]
    fn test_weight_validation() {
        let engine = create_test_engine();

        // Test empty weights
        let empty_weights = HashMap::new();
        assert!(engine.validate_parsed_weights(&empty_weights).is_err());

        // Test valid weights
        let valid_weights = create_test_weights();
        assert!(engine.validate_parsed_weights(&valid_weights).is_ok());
    }

    #[test]
    fn test_calibration_data_validation() {
        let mut engine = create_test_engine();

        // Test empty calibration data
        let empty_dataset = CalibrationDataset {
            samples: vec![],
            weights: None,
            statistics: DatasetStatistics::default(),
        };
        assert!(engine.set_calibration_data(empty_dataset).is_err());

        // Test valid calibration data
        let valid_dataset = CalibrationDataset {
            samples: vec![Tensor::from_vec(vec![1.0, 2.0, 3.0], &[3]).expect("Operation failed")],
            weights: None,
            statistics: DatasetStatistics::default(),
        };
        assert!(engine.set_calibration_data(valid_dataset).is_ok());
    }

    // Helper functions for tests
    fn create_test_engine() -> MobileQuantizationEngine {
        let config = QuantizationConfig::default();
        // Use the actual device detector for consistent structure
        let device_info = crate::device_info::MobileDeviceDetector::detect().unwrap_or_else(|_| {
            // Fallback test device info if detection fails
            use crate::device_info::*;
            MobileDeviceInfo {
                platform: crate::MobilePlatform::Generic,
                basic_info: BasicDeviceInfo {
                    platform: crate::MobilePlatform::Generic,
                    manufacturer: "Test".to_string(),
                    model: "Test Device".to_string(),
                    os_version: "1.0".to_string(),
                    hardware_id: "test".to_string(),
                    device_generation: Some(2023),
                },
                cpu_info: CpuInfo {
                    architecture: "aarch64".to_string(),
                    total_cores: 8,
                    core_count: 8,
                    performance_cores: 4,
                    efficiency_cores: 4,
                    max_frequency_mhz: Some(3000),
                    l1_cache_kb: Some(64),
                    l2_cache_kb: Some(512),
                    l3_cache_kb: Some(4096),
                    features: vec!["neon".to_string(), "fp16".to_string()],
                    simd_support: SimdSupport::Advanced,
                },
                memory_info: MemoryInfo {
                    total_mb: 8192,
                    available_mb: 6144,
                    total_memory: 8192,
                    available_memory: 6144,
                    bandwidth_mbps: Some(51200),
                    memory_type: "LPDDR5".to_string(),
                    frequency_mhz: Some(6400),
                    is_low_memory_device: false,
                },
                gpu_info: Some(GpuInfo {
                    vendor: "ARM".to_string(),
                    model: "Mali-G78".to_string(),
                    driver_version: "1.0".to_string(),
                    memory_mb: Some(2048),
                    compute_units: Some(14),
                    supported_apis: vec![GpuApi::OpenGLES3, GpuApi::Vulkan11],
                    performance_tier: GpuPerformanceTier::High,
                }),
                npu_info: None,
                thermal_info: ThermalInfo {
                    current_state: ThermalState::Nominal,
                    state: ThermalState::Nominal,
                    throttling_supported: true,
                    temperature_sensors: vec![],
                    thermal_zones: vec![],
                },
                power_info: PowerInfo {
                    battery_capacity_mah: Some(4000),
                    battery_level_percent: Some(80),
                    battery_level: Some(80),
                    battery_health_percent: Some(100),
                    charging_status: ChargingStatus::Discharging,
                    is_charging: false,
                    power_save_mode: false,
                    low_power_mode_available: true,
                },
                available_backends: vec![crate::MobileBackend::CPU, crate::MobileBackend::GPU],
                performance_scores: PerformanceScores {
                    cpu_single_core: Some(1200),
                    cpu_multi_core: Some(4800),
                    gpu_score: Some(2500),
                    memory_score: Some(1800),
                    overall_tier: PerformanceTier::Mid,
                    tier: PerformanceTier::Mid,
                },
            }
        });

        MobileQuantizationEngine::new(config, device_info, None).expect("Operation failed")
    }

    fn create_test_quantized_weights() -> HashMap<String, QuantizedTensor> {
        let mut weights = HashMap::new();

        weights.insert(
            "layer1.weight".to_string(),
            QuantizedTensor {
                data: vec![1, 2, 3, 4, 5],
                scales: vec![0.1],
                zero_points: vec![0],
                shape: vec![5],
                scheme: QuantizationScheme {
                    bits: 8,
                    symmetric: false,
                    signed: true,
                    method: QuantizationMethod::Linear,
                },
            },
        );

        weights.insert(
            "layer2.weight".to_string(),
            QuantizedTensor {
                data: vec![6, 7, 8, 9, 10],
                scales: vec![0.2],
                zero_points: vec![1],
                shape: vec![5],
                scheme: QuantizationScheme {
                    bits: 4,
                    symmetric: false,
                    signed: false,
                    method: QuantizationMethod::Linear,
                },
            },
        );

        weights
    }

    fn create_test_weights() -> HashMap<String, Tensor> {
        let mut weights = HashMap::new();

        weights.insert(
            "layer1.weight".to_string(),
            Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], &[5]).expect("Operation failed"),
        );
        weights.insert(
            "layer2.weight".to_string(),
            Tensor::from_vec(vec![6.0, 7.0, 8.0, 9.0, 10.0], &[5]).expect("Operation failed"),
        );

        weights
    }
}