1use crate::{
8 device_info::{MobileDeviceInfo, PerformanceTier},
9 optimization::memory_pool::MobileMemoryPool,
10 Result,
11};
12use half::f16;
13use serde::{Deserialize, Serialize};
14use std::collections::HashMap;
15use std::sync::{Arc, Mutex};
16use trustformers_core::errors::{invalid_config, invalid_input};
17use trustformers_core::Tensor;
18
19pub struct MobileQuantizationEngine {
21 config: QuantizationConfig,
22 device_info: MobileDeviceInfo,
23 calibration_data: Option<CalibrationDataset>,
24 quantization_cache: Arc<Mutex<HashMap<String, QuantizedModel>>>,
25 memory_pool: Option<Arc<MobileMemoryPool>>,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct QuantizationConfig {
31 pub target_precision: MobilePrecision,
33 pub enable_mixed_precision: bool,
35 pub dynamic_strategy: DynamicQuantizationStrategy,
37 pub hardware_aware: bool,
39 pub granularity: QuantizationGranularity,
41 pub quality_threshold: f32,
43 pub memory_constraint_mb: usize,
45 pub enable_gradient_quantization: bool,
47 pub kl_threshold: f32,
49 pub enable_ptq: bool,
51 pub enable_qat: bool,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
57pub enum MobilePrecision {
58 INT4,
60 INT8,
62 FP16,
64 Mixed4_8,
66 Mixed8_16,
68 DYNAMIC,
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
74pub enum DynamicQuantizationStrategy {
75 BatteryAware,
77 ThermalAware,
79 MemoryAware,
81 PerformanceAware,
83 Adaptive,
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
89pub enum QuantizationGranularity {
90 PerTensor,
92 PerChannel,
94 PerGroup { group_size: usize },
96 PerLayer,
98}
99
100#[derive(Debug, Clone)]
102pub struct CalibrationDataset {
103 pub samples: Vec<Tensor>,
105 pub weights: Option<Vec<f32>>,
107 pub statistics: DatasetStatistics,
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize, Default)]
113pub struct DatasetStatistics {
114 pub activation_ranges: HashMap<String, (f32, f32)>,
116 pub layer_means: HashMap<String, f32>,
118 pub layer_variances: HashMap<String, f32>,
120 pub kl_scores: HashMap<String, f32>,
122}
123
124#[derive(Debug, Clone)]
126pub struct QuantizedModel {
127 pub weights: HashMap<String, QuantizedTensor>,
129 pub parameters: QuantizationParameters,
131 pub metadata: ModelMetadata,
133 pub benchmarks: QuantizationBenchmarks,
135}
136
137#[derive(Debug, Clone)]
139pub struct QuantizedTensor {
140 pub data: Vec<i8>,
142 pub scales: Vec<f32>,
144 pub zero_points: Vec<i32>,
146 pub shape: Vec<usize>,
148 pub scheme: QuantizationScheme,
150}
151
152#[derive(Debug, Clone, Serialize, Deserialize)]
154pub struct QuantizationScheme {
155 pub bits: u8,
157 pub symmetric: bool,
159 pub signed: bool,
161 pub method: QuantizationMethod,
163}
164
165#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
167pub enum QuantizationMethod {
168 Linear,
170 Logarithmic,
172 PowerOfTwo,
174 KMeans,
176 Learned,
178}
179
180#[derive(Debug, Clone, Copy, PartialEq)]
182pub enum ModelFormat {
183 SafeTensors,
185 PyTorchPickle,
187 TensorFlow,
189 ONNX,
191 Custom,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
197pub struct QuantizationParameters {
198 pub global_scale: f32,
200 pub layer_scales: HashMap<String, f32>,
202 pub layer_zero_points: HashMap<String, i32>,
204 pub dequant_overhead_ms: f32,
206}
207
208#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct ModelMetadata {
211 pub original_size_bytes: usize,
213 pub quantized_size_bytes: usize,
215 pub compression_ratio: f32,
217 pub quality_score: f32,
219 pub timestamp: std::time::SystemTime,
221}
222
223#[derive(Debug, Clone, Serialize, Deserialize)]
225pub struct QuantizationBenchmarks {
226 pub original_inference_ms: f32,
228 pub quantized_inference_ms: f32,
230 pub speedup_factor: f32,
232 pub memory_reduction_mb: f32,
234 pub power_reduction_mw: f32,
236}
237
238impl MobileQuantizationEngine {
239 pub fn new(
241 config: QuantizationConfig,
242 device_info: MobileDeviceInfo,
243 memory_pool: Option<Arc<MobileMemoryPool>>,
244 ) -> Result<Self> {
245 Ok(Self {
246 config,
247 device_info,
248 calibration_data: None,
249 quantization_cache: Arc::new(Mutex::new(HashMap::new())),
250 memory_pool,
251 })
252 }
253
254 pub fn set_calibration_data(&mut self, dataset: CalibrationDataset) -> Result<()> {
256 if dataset.samples.is_empty() {
258 return Err(invalid_config(
259 "set_calibration_data",
260 "Calibration dataset cannot be empty",
261 ));
262 }
263
264 self.calibration_data = Some(dataset);
265 Ok(())
266 }
267
268 pub fn quantize_model(&self, model_id: &str, model_data: &[u8]) -> Result<QuantizedModel> {
270 {
272 let cache = self.quantization_cache.lock().unwrap_or_else(|p| p.into_inner());
273 if let Some(cached_model) = cache.get(model_id) {
274 return Ok(cached_model.clone());
275 }
276 }
277
278 let strategy = self.determine_quantization_strategy()?;
280
281 let hardware_config = self.get_hardware_quantization_config()?;
283
284 let quantized_model = self.perform_quantization(model_data, &strategy, &hardware_config)?;
286
287 let benchmarks = self.benchmark_quantized_model(&quantized_model)?;
289
290 let final_model = QuantizedModel {
291 weights: quantized_model.weights,
292 parameters: quantized_model.parameters,
293 metadata: quantized_model.metadata,
294 benchmarks,
295 };
296
297 {
299 let mut cache = self.quantization_cache.lock().unwrap_or_else(|p| p.into_inner());
300 cache.insert(model_id.to_string(), final_model.clone());
301 }
302
303 Ok(final_model)
304 }
305
306 fn determine_quantization_strategy(&self) -> Result<MobilePrecision> {
308 match (
309 &self.device_info.performance_scores.overall_tier,
310 &self.config.target_precision,
311 ) {
312 (PerformanceTier::High, MobilePrecision::DYNAMIC) => {
313 Ok(MobilePrecision::Mixed8_16)
315 },
316 (PerformanceTier::Mid, MobilePrecision::DYNAMIC) => {
317 Ok(MobilePrecision::INT8)
319 },
320 (PerformanceTier::Budget, MobilePrecision::DYNAMIC) => {
321 Ok(MobilePrecision::Mixed4_8)
323 },
324 (_, precision) => Ok(*precision),
325 }
326 }
327
328 fn get_hardware_quantization_config(&self) -> Result<HardwareQuantizationConfig> {
330 let mut config = HardwareQuantizationConfig::default();
331
332 if self.device_info.npu_info.is_some() {
334 config.use_npu_kernels = true;
335 config.preferred_precision = MobilePrecision::INT8;
336 }
337
338 if self.device_info.gpu_info.is_some() {
339 config.use_gpu_kernels = true;
340 config.gpu_memory_optimization = true;
341 }
342
343 if self.device_info.cpu_info.architecture.contains("arm")
345 || self.device_info.cpu_info.architecture.contains("aarch64")
346 {
347 config.use_neon_instructions = true;
348 config.arm_specific_kernels = true;
349 }
350
351 Ok(config)
352 }
353
354 fn perform_quantization(
356 &self,
357 model_data: &[u8],
358 strategy: &MobilePrecision,
359 hardware_config: &HardwareQuantizationConfig,
360 ) -> Result<QuantizedModel> {
361 let weights = self.parse_model_weights(model_data)?;
363
364 let quantized_weights = match strategy {
366 MobilePrecision::INT4 => self.quantize_to_int4(&weights)?,
367 MobilePrecision::INT8 => self.quantize_to_int8(&weights)?,
368 MobilePrecision::FP16 => self.quantize_to_fp16(&weights)?,
369 MobilePrecision::Mixed4_8 => self.quantize_mixed_4_8(&weights)?,
370 MobilePrecision::Mixed8_16 => self.quantize_mixed_8_16(&weights)?,
371 MobilePrecision::DYNAMIC => self.quantize_dynamic(&weights)?,
372 };
373
374 let parameters = self.calculate_quantization_parameters(&quantized_weights)?;
376
377 let metadata = ModelMetadata {
379 original_size_bytes: model_data.len(),
380 quantized_size_bytes: self.calculate_quantized_size(&quantized_weights),
381 compression_ratio: model_data.len() as f32
382 / self.calculate_quantized_size(&quantized_weights) as f32,
383 quality_score: self.estimate_quality_score(&quantized_weights)?,
384 timestamp: std::time::SystemTime::now(),
385 };
386
387 Ok(QuantizedModel {
388 weights: quantized_weights,
389 parameters,
390 metadata,
391 benchmarks: QuantizationBenchmarks::default(), })
393 }
394
395 fn quantize_to_int4(
397 &self,
398 weights: &HashMap<String, Tensor>,
399 ) -> Result<HashMap<String, QuantizedTensor>> {
400 let mut quantized = HashMap::new();
401
402 for (layer_name, tensor) in weights {
403 let tensor_data = tensor.data()?.to_vec();
404
405 let min_val = tensor_data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
407 let max_val = tensor_data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
408
409 let scale = (max_val - min_val) / 15.0; let zero_point = (-min_val / scale).round() as i32;
411
412 let quantized_data: Vec<i8> = tensor_data
414 .iter()
415 .map(|&x| {
416 let quantized = ((x / scale) + zero_point as f32).round();
417 quantized.max(0.0).min(15.0) as i8
418 })
419 .collect();
420
421 let quantized_tensor = QuantizedTensor {
422 data: quantized_data,
423 scales: vec![scale],
424 zero_points: vec![zero_point],
425 shape: tensor.shape().to_vec(),
426 scheme: QuantizationScheme {
427 bits: 4,
428 symmetric: false,
429 signed: false,
430 method: QuantizationMethod::Linear,
431 },
432 };
433
434 quantized.insert(layer_name.clone(), quantized_tensor);
435 }
436
437 Ok(quantized)
438 }
439
440 fn quantize_to_int8(
442 &self,
443 weights: &HashMap<String, Tensor>,
444 ) -> Result<HashMap<String, QuantizedTensor>> {
445 let mut quantized = HashMap::new();
446
447 for (layer_name, tensor) in weights {
448 let tensor_data = tensor.data()?.to_vec();
449
450 let min_val = tensor_data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
452 let max_val = tensor_data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
453
454 let scale = (max_val - min_val) / 255.0; let zero_point = (-min_val / scale).round() as i32;
456
457 let quantized_data: Vec<i8> = tensor_data
459 .iter()
460 .map(|&x| {
461 let quantized = ((x / scale) + zero_point as f32).round();
462 (quantized.max(0.0).min(255.0) as i32 - 128) as i8 })
464 .collect();
465
466 let quantized_tensor = QuantizedTensor {
467 data: quantized_data,
468 scales: vec![scale],
469 zero_points: vec![zero_point],
470 shape: tensor.shape().to_vec(),
471 scheme: QuantizationScheme {
472 bits: 8,
473 symmetric: false,
474 signed: true,
475 method: QuantizationMethod::Linear,
476 },
477 };
478
479 quantized.insert(layer_name.clone(), quantized_tensor);
480 }
481
482 Ok(quantized)
483 }
484
485 fn quantize_to_fp16(
487 &self,
488 weights: &HashMap<String, Tensor>,
489 ) -> Result<HashMap<String, QuantizedTensor>> {
490 let mut quantized = HashMap::new();
492
493 for (layer_name, tensor) in weights {
494 let tensor_data = tensor.data()?.to_vec();
495
496 let quantized_data: Vec<i8> = tensor_data
498 .iter()
499 .flat_map(|&x| {
500 let fp16_bits = f16::from_f32(x).to_bits();
501 [(fp16_bits & 0xFF) as i8, ((fp16_bits >> 8) & 0xFF) as i8]
502 })
503 .collect();
504
505 let quantized_tensor = QuantizedTensor {
506 data: quantized_data,
507 scales: vec![1.0], zero_points: vec![0],
509 shape: tensor.shape().to_vec(),
510 scheme: QuantizationScheme {
511 bits: 16,
512 symmetric: true,
513 signed: true,
514 method: QuantizationMethod::Linear,
515 },
516 };
517
518 quantized.insert(layer_name.clone(), quantized_tensor);
519 }
520
521 Ok(quantized)
522 }
523
524 fn quantize_mixed_4_8(
526 &self,
527 weights: &HashMap<String, Tensor>,
528 ) -> Result<HashMap<String, QuantizedTensor>> {
529 let mut quantized = HashMap::new();
530
531 for (layer_name, tensor) in weights {
532 let use_4bit = self.should_use_4bit_for_layer(layer_name, tensor)?;
534
535 if use_4bit {
536 let quantized_4bit = self.quantize_to_int4(
537 &[(layer_name.clone(), tensor.clone())].iter().cloned().collect(),
538 )?;
539 quantized.extend(quantized_4bit);
540 } else {
541 let quantized_8bit = self.quantize_to_int8(
542 &[(layer_name.clone(), tensor.clone())].iter().cloned().collect(),
543 )?;
544 quantized.extend(quantized_8bit);
545 }
546 }
547
548 Ok(quantized)
549 }
550
551 fn quantize_mixed_8_16(
553 &self,
554 weights: &HashMap<String, Tensor>,
555 ) -> Result<HashMap<String, QuantizedTensor>> {
556 let mut quantized = HashMap::new();
557
558 for (layer_name, tensor) in weights {
559 let use_8bit = self.should_use_8bit_for_layer(layer_name, tensor)?;
561
562 if use_8bit {
563 let quantized_8bit = self.quantize_to_int8(
564 &[(layer_name.clone(), tensor.clone())].iter().cloned().collect(),
565 )?;
566 quantized.extend(quantized_8bit);
567 } else {
568 let quantized_16bit = self.quantize_to_fp16(
569 &[(layer_name.clone(), tensor.clone())].iter().cloned().collect(),
570 )?;
571 quantized.extend(quantized_16bit);
572 }
573 }
574
575 Ok(quantized)
576 }
577
578 fn quantize_dynamic(
580 &self,
581 weights: &HashMap<String, Tensor>,
582 ) -> Result<HashMap<String, QuantizedTensor>> {
583 self.quantize_to_int8(weights)
585 }
586
587 fn should_use_4bit_for_layer(&self, layer_name: &str, tensor: &Tensor) -> Result<bool> {
589 let is_embedding = layer_name.contains("embed") || layer_name.contains("token");
591 let is_output = layer_name.contains("output") || layer_name.contains("head");
592 let is_large = tensor.shape().iter().product::<usize>() > 1000000;
593
594 Ok(is_embedding || (is_large && !is_output))
595 }
596
597 fn should_use_8bit_for_layer(&self, layer_name: &str, tensor: &Tensor) -> Result<bool> {
599 let is_attention = layer_name.contains("attn") || layer_name.contains("attention");
601 let is_output = layer_name.contains("output") || layer_name.contains("head");
602 let is_norm = layer_name.contains("norm") || layer_name.contains("ln");
603
604 Ok(!(is_attention || is_output || is_norm))
605 }
606
607 fn parse_model_weights(&self, model_data: &[u8]) -> Result<HashMap<String, Tensor>> {
609 let mut weights = HashMap::new();
611
612 let format = self.detect_model_format(model_data)?;
614
615 match format {
616 ModelFormat::SafeTensors => {
617 weights = self.parse_safetensors(model_data)?;
618 },
619 ModelFormat::PyTorchPickle => {
620 weights = self.parse_pytorch_pickle(model_data)?;
621 },
622 ModelFormat::TensorFlow => {
623 weights = self.parse_tensorflow(model_data)?;
624 },
625 ModelFormat::ONNX => {
626 weights = self.parse_onnx(model_data)?;
627 },
628 ModelFormat::Custom => {
629 weights = self.parse_custom_format(model_data)?;
630 },
631 }
632
633 self.validate_parsed_weights(&weights)?;
635
636 Ok(weights)
637 }
638
639 fn calculate_quantization_parameters(
641 &self,
642 weights: &HashMap<String, QuantizedTensor>,
643 ) -> Result<QuantizationParameters> {
644 let mut layer_scales = HashMap::new();
645 let mut layer_zero_points = HashMap::new();
646 let mut total_dequant_overhead = 0.0;
647
648 for (layer_name, quantized_tensor) in weights {
650 let scale = quantized_tensor.scales.first().copied().unwrap_or(1.0);
652 let zero_point = quantized_tensor.zero_points.first().copied().unwrap_or(0);
653
654 layer_scales.insert(layer_name.clone(), scale);
655 layer_zero_points.insert(layer_name.clone(), zero_point);
656
657 let tensor_size = quantized_tensor.data.len();
659 let overhead_factor = match quantized_tensor.scheme.bits {
660 4 => 0.05, 8 => 0.03, 16 => 0.01, _ => 0.04, };
665
666 total_dequant_overhead += (tensor_size as f32 * overhead_factor) / 1000.0;
667 }
669
670 let total_elements: f32 = weights.values().map(|t| t.data.len() as f32).sum();
672 let global_scale = if total_elements > 0.0 {
673 layer_scales.values().sum::<f32>() / layer_scales.len() as f32
674 } else {
675 1.0
676 };
677
678 Ok(QuantizationParameters {
679 global_scale,
680 layer_scales,
681 layer_zero_points,
682 dequant_overhead_ms: total_dequant_overhead,
683 })
684 }
685
686 fn calculate_quantized_size(&self, weights: &HashMap<String, QuantizedTensor>) -> usize {
688 let mut total_size = 0;
689
690 for (layer_name, quantized_tensor) in weights {
691 let data_size = quantized_tensor.data.len();
693
694 let metadata_size = quantized_tensor.scales.len() * 4 + quantized_tensor.zero_points.len() * 4 + quantized_tensor.shape.len() * 8 + layer_name.len() + 32; total_size += data_size + metadata_size;
701 }
702
703 total_size
704 }
705
706 fn estimate_quality_score(&self, weights: &HashMap<String, QuantizedTensor>) -> Result<f32> {
708 if weights.is_empty() {
709 return Ok(1.0);
710 }
711
712 let mut total_quality = 0.0;
713 let mut total_weight = 0.0;
714
715 for (layer_name, quantized_tensor) in weights {
716 let layer_weight = quantized_tensor.data.len() as f32;
717
718 let base_quality = match quantized_tensor.scheme.bits {
720 4 => 0.85, 8 => 0.93, 16 => 0.98, _ => 0.90, };
725
726 let layer_quality_factor = self.get_layer_quality_factor(layer_name);
728 let adjusted_quality = base_quality * layer_quality_factor;
729
730 total_quality += adjusted_quality * layer_weight;
732 total_weight += layer_weight;
733 }
734
735 let overall_quality = if total_weight > 0.0 { total_quality / total_weight } else { 1.0 };
736
737 let calibration_factor = if let Some(ref cal_data) = self.calibration_data {
739 self.estimate_calibration_quality_impact(cal_data)?
740 } else {
741 0.95 };
743
744 Ok((overall_quality * calibration_factor).min(1.0))
745 }
746
747 fn benchmark_quantized_model(&self, model: &QuantizedModel) -> Result<QuantizationBenchmarks> {
749 let mut benchmarks = QuantizationBenchmarks::default();
750
751 let original_params = model.metadata.original_size_bytes / 4; benchmarks.original_inference_ms =
754 self.estimate_inference_time(original_params, MobilePrecision::FP16)?;
755
756 let quantized_params = model.metadata.quantized_size_bytes;
758 let avg_precision = self.estimate_average_precision(&model.weights);
759 benchmarks.quantized_inference_ms =
760 self.estimate_inference_time(quantized_params, avg_precision)?;
761
762 benchmarks.speedup_factor = if benchmarks.quantized_inference_ms > 0.0 {
764 benchmarks.original_inference_ms / benchmarks.quantized_inference_ms
765 } else {
766 1.0
767 };
768
769 benchmarks.memory_reduction_mb = (model.metadata.original_size_bytes
771 - model.metadata.quantized_size_bytes) as f32
772 / (1024.0 * 1024.0);
773
774 benchmarks.power_reduction_mw = self.estimate_power_reduction(&model.weights)?;
776
777 Ok(benchmarks)
778 }
779
780 fn detect_model_format(&self, data: &[u8]) -> Result<ModelFormat> {
784 if data.len() < 8 {
785 return Err(invalid_input("Model data too small to detect format"));
786 }
787
788 if data.starts_with(b"STFR") || data.starts_with(&[0x53, 0x54, 0x46, 0x52]) {
790 return Ok(ModelFormat::SafeTensors);
791 }
792
793 if data.starts_with(&[0x80, 0x02]) || data.starts_with(&[0x80, 0x03]) {
795 return Ok(ModelFormat::PyTorchPickle);
796 }
797
798 if data.starts_with(b"TF") {
800 return Ok(ModelFormat::TensorFlow);
801 }
802
803 if data.starts_with(&[0x08, 0x01]) {
805 return Ok(ModelFormat::ONNX);
806 }
807
808 Ok(ModelFormat::Custom)
810 }
811
812 fn parse_safetensors(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
814 Ok(HashMap::new())
816 }
817
818 fn parse_pytorch_pickle(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
820 Ok(HashMap::new())
822 }
823
824 fn parse_tensorflow(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
826 Ok(HashMap::new())
828 }
829
830 fn parse_onnx(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
832 Ok(HashMap::new())
834 }
835
836 fn parse_custom_format(&self, _data: &[u8]) -> Result<HashMap<String, Tensor>> {
838 Ok(HashMap::new())
840 }
841
842 fn validate_parsed_weights(&self, weights: &HashMap<String, Tensor>) -> Result<()> {
844 if weights.is_empty() {
845 return Err(invalid_input("No weights found in model"));
846 }
847
848 for (layer_name, tensor) in weights {
849 if tensor.shape().is_empty() {
851 return Err(invalid_input(format!(
852 "Invalid tensor shape for layer: {}",
853 layer_name
854 )));
855 }
856
857 let total_elements: usize = tensor.shape().iter().product();
859 if total_elements == 0 {
860 return Err(invalid_input(format!(
861 "Empty tensor for layer: {}",
862 layer_name
863 )));
864 }
865
866 if total_elements > 100_000_000 {
868 tracing::warn!(
869 "Large tensor detected in layer {}: {} elements",
870 layer_name,
871 total_elements
872 );
873 }
874 }
875
876 Ok(())
877 }
878
879 fn get_layer_quality_factor(&self, layer_name: &str) -> f32 {
881 if layer_name.contains("output") || layer_name.contains("head") {
883 0.95 } else if layer_name.contains("attention") || layer_name.contains("attn") {
885 0.92 } else if layer_name.contains("norm") || layer_name.contains("ln") {
887 0.98 } else if layer_name.contains("embed") || layer_name.contains("token") {
889 0.90 } else {
891 1.0 }
893 }
894
895 fn estimate_calibration_quality_impact(&self, cal_data: &CalibrationDataset) -> Result<f32> {
897 let sample_factor = (cal_data.samples.len() as f32 / 100.0).min(1.0);
899
900 let stats_quality =
902 if !cal_data.statistics.activation_ranges.is_empty() { 1.0 } else { 0.9 };
903
904 Ok(0.95 + 0.05 * sample_factor * stats_quality)
905 }
906
907 fn estimate_inference_time(&self, params: usize, precision: MobilePrecision) -> Result<f32> {
909 let base_time_per_param = match self.device_info.performance_scores.overall_tier {
911 PerformanceTier::VeryLow => 0.01, PerformanceTier::Low => 0.008, PerformanceTier::Budget => 0.005, PerformanceTier::Medium => 0.003, PerformanceTier::Mid => 0.002, PerformanceTier::High => 0.001, PerformanceTier::VeryHigh => 0.0007, PerformanceTier::Flagship => 0.0005, };
920
921 let precision_factor = match precision {
923 MobilePrecision::INT4 => 0.5,
924 MobilePrecision::INT8 => 0.7,
925 MobilePrecision::FP16 => 1.0,
926 MobilePrecision::Mixed4_8 => 0.6,
927 MobilePrecision::Mixed8_16 => 0.85,
928 MobilePrecision::DYNAMIC => 0.8,
929 };
930
931 let hw_factor = if self.device_info.npu_info.is_some() {
933 0.6
934 } else if self.device_info.gpu_info.is_some() {
935 0.8
936 } else {
937 1.0
938 };
939
940 let total_time = params as f32 * base_time_per_param * precision_factor * hw_factor;
941 Ok(total_time)
942 }
943
944 fn estimate_average_precision(
946 &self,
947 weights: &HashMap<String, QuantizedTensor>,
948 ) -> MobilePrecision {
949 if weights.is_empty() {
950 return MobilePrecision::FP16;
951 }
952
953 let mut total_bits = 0;
954 let mut total_tensors = 0;
955
956 for tensor in weights.values() {
957 total_bits += tensor.scheme.bits as u32;
958 total_tensors += 1;
959 }
960
961 let avg_bits = total_bits as f32 / total_tensors as f32;
962
963 match avg_bits.round() as u8 {
964 4 => MobilePrecision::INT4,
965 8 => MobilePrecision::INT8,
966 16 => MobilePrecision::FP16,
967 _ => MobilePrecision::INT8, }
969 }
970
971 fn estimate_power_reduction(&self, weights: &HashMap<String, QuantizedTensor>) -> Result<f32> {
973 let mut total_power_reduction = 0.0;
974
975 for tensor in weights.values() {
976 let tensor_size = tensor.data.len() as f32;
977
978 let reduction_per_op = match tensor.scheme.bits {
980 4 => 0.08, 8 => 0.05, 16 => 0.02, _ => 0.04, };
985
986 total_power_reduction += tensor_size * reduction_per_op / 1000.0;
987 }
988
989 Ok(total_power_reduction)
990 }
991}
992
993#[derive(Debug, Clone)]
995struct HardwareQuantizationConfig {
996 use_npu_kernels: bool,
997 use_gpu_kernels: bool,
998 use_neon_instructions: bool,
999 arm_specific_kernels: bool,
1000 gpu_memory_optimization: bool,
1001 preferred_precision: MobilePrecision,
1002}
1003
1004impl Default for HardwareQuantizationConfig {
1005 fn default() -> Self {
1006 Self {
1007 use_npu_kernels: false,
1008 use_gpu_kernels: false,
1009 use_neon_instructions: false,
1010 arm_specific_kernels: false,
1011 gpu_memory_optimization: false,
1012 preferred_precision: MobilePrecision::INT8,
1013 }
1014 }
1015}
1016
1017impl Default for QuantizationBenchmarks {
1018 fn default() -> Self {
1019 Self {
1020 original_inference_ms: 0.0,
1021 quantized_inference_ms: 0.0,
1022 speedup_factor: 1.0,
1023 memory_reduction_mb: 0.0,
1024 power_reduction_mw: 0.0,
1025 }
1026 }
1027}
1028
1029impl Default for QuantizationConfig {
1030 fn default() -> Self {
1031 Self {
1032 target_precision: MobilePrecision::INT8,
1033 enable_mixed_precision: true,
1034 dynamic_strategy: DynamicQuantizationStrategy::Adaptive,
1035 hardware_aware: true,
1036 granularity: QuantizationGranularity::PerChannel,
1037 quality_threshold: 0.9,
1038 memory_constraint_mb: 512,
1039 enable_gradient_quantization: false,
1040 kl_threshold: 0.01,
1041 enable_ptq: true,
1042 enable_qat: false,
1043 }
1044 }
1045}
1046
1047#[cfg(test)]
1048mod tests {
1049 use super::*;
1050 use trustformers_core::Tensor;
1051
1052 #[test]
1053 fn test_model_format_detection() {
1054 let engine = create_test_engine();
1055
1056 let safetensors_data = b"STFR\x00\x00\x00\x00test data";
1058 let format = engine.detect_model_format(safetensors_data).expect("Operation failed");
1059 assert_eq!(format, ModelFormat::SafeTensors);
1060
1061 let pytorch_data = b"\x80\x02test data";
1063 let format = engine.detect_model_format(pytorch_data).expect("Operation failed");
1064 assert_eq!(format, ModelFormat::PyTorchPickle);
1065
1066 let tf_data = b"TFtest data";
1068 let format = engine.detect_model_format(tf_data).expect("Operation failed");
1069 assert_eq!(format, ModelFormat::TensorFlow);
1070
1071 let onnx_data = b"\x08\x01test data";
1073 let format = engine.detect_model_format(onnx_data).expect("Operation failed");
1074 assert_eq!(format, ModelFormat::ONNX);
1075
1076 let custom_data = b"custom test data";
1078 let format = engine.detect_model_format(custom_data).expect("Operation failed");
1079 assert_eq!(format, ModelFormat::Custom);
1080 }
1081
1082 #[test]
1083 fn test_quantization_parameters_calculation() {
1084 let engine = create_test_engine();
1085 let weights = create_test_quantized_weights();
1086
1087 let params = engine.calculate_quantization_parameters(&weights).expect("Operation failed");
1088
1089 assert!(params.global_scale > 0.0);
1090 assert!(!params.layer_scales.is_empty());
1091 assert!(!params.layer_zero_points.is_empty());
1092 assert!(params.dequant_overhead_ms >= 0.0);
1093 }
1094
1095 #[test]
1096 fn test_quality_score_estimation() {
1097 let engine = create_test_engine();
1098 let weights = create_test_quantized_weights();
1099
1100 let quality = engine.estimate_quality_score(&weights).expect("Operation failed");
1101
1102 assert!((0.0..=1.0).contains(&quality));
1103 }
1104
1105 #[test]
1106 fn test_layer_quality_factors() {
1107 let engine = create_test_engine();
1108
1109 assert_eq!(engine.get_layer_quality_factor("model.output.weight"), 0.95);
1111 assert_eq!(
1112 engine.get_layer_quality_factor("model.attention.weight"),
1113 0.92
1114 );
1115 assert_eq!(
1116 engine.get_layer_quality_factor("model.layer_norm.weight"),
1117 0.98
1118 );
1119 assert_eq!(
1120 engine.get_layer_quality_factor("model.embedding.weight"),
1121 0.90
1122 );
1123 assert_eq!(engine.get_layer_quality_factor("model.hidden.weight"), 1.0);
1124 }
1125
1126 #[test]
1127 fn test_inference_time_estimation() {
1128 let engine = create_test_engine();
1129
1130 let time = engine
1131 .estimate_inference_time(1000, MobilePrecision::INT8)
1132 .expect("Operation failed");
1133 assert!(time > 0.0);
1134
1135 let time_fp16 = engine
1136 .estimate_inference_time(1000, MobilePrecision::FP16)
1137 .expect("Operation failed");
1138 let time_int4 = engine
1139 .estimate_inference_time(1000, MobilePrecision::INT4)
1140 .expect("Operation failed");
1141
1142 assert!(time_int4 < time_fp16);
1144 }
1145
1146 #[test]
1147 fn test_power_reduction_estimation() {
1148 let engine = create_test_engine();
1149 let weights = create_test_quantized_weights();
1150
1151 let power_reduction = engine.estimate_power_reduction(&weights).expect("Operation failed");
1152 assert!(power_reduction >= 0.0);
1153 }
1154
1155 #[test]
1156 fn test_quantized_size_calculation() {
1157 let engine = create_test_engine();
1158 let weights = create_test_quantized_weights();
1159
1160 let size = engine.calculate_quantized_size(&weights);
1161 assert!(size > 0);
1162 }
1163
1164 #[test]
1165 fn test_weight_validation() {
1166 let engine = create_test_engine();
1167
1168 let empty_weights = HashMap::new();
1170 assert!(engine.validate_parsed_weights(&empty_weights).is_err());
1171
1172 let valid_weights = create_test_weights();
1174 assert!(engine.validate_parsed_weights(&valid_weights).is_ok());
1175 }
1176
1177 #[test]
1178 fn test_calibration_data_validation() {
1179 let mut engine = create_test_engine();
1180
1181 let empty_dataset = CalibrationDataset {
1183 samples: vec![],
1184 weights: None,
1185 statistics: DatasetStatistics::default(),
1186 };
1187 assert!(engine.set_calibration_data(empty_dataset).is_err());
1188
1189 let valid_dataset = CalibrationDataset {
1191 samples: vec![Tensor::from_vec(vec![1.0, 2.0, 3.0], &[3]).expect("Operation failed")],
1192 weights: None,
1193 statistics: DatasetStatistics::default(),
1194 };
1195 assert!(engine.set_calibration_data(valid_dataset).is_ok());
1196 }
1197
1198 fn create_test_engine() -> MobileQuantizationEngine {
1200 let config = QuantizationConfig::default();
1201 let device_info = crate::device_info::MobileDeviceDetector::detect().unwrap_or_else(|_| {
1203 use crate::device_info::*;
1205 MobileDeviceInfo {
1206 platform: crate::MobilePlatform::Generic,
1207 basic_info: BasicDeviceInfo {
1208 platform: crate::MobilePlatform::Generic,
1209 manufacturer: "Test".to_string(),
1210 model: "Test Device".to_string(),
1211 os_version: "1.0".to_string(),
1212 hardware_id: "test".to_string(),
1213 device_generation: Some(2023),
1214 },
1215 cpu_info: CpuInfo {
1216 architecture: "aarch64".to_string(),
1217 total_cores: 8,
1218 core_count: 8,
1219 performance_cores: 4,
1220 efficiency_cores: 4,
1221 max_frequency_mhz: Some(3000),
1222 l1_cache_kb: Some(64),
1223 l2_cache_kb: Some(512),
1224 l3_cache_kb: Some(4096),
1225 features: vec!["neon".to_string(), "fp16".to_string()],
1226 simd_support: SimdSupport::Advanced,
1227 },
1228 memory_info: MemoryInfo {
1229 total_mb: 8192,
1230 available_mb: 6144,
1231 total_memory: 8192,
1232 available_memory: 6144,
1233 bandwidth_mbps: Some(51200),
1234 memory_type: "LPDDR5".to_string(),
1235 frequency_mhz: Some(6400),
1236 is_low_memory_device: false,
1237 },
1238 gpu_info: Some(GpuInfo {
1239 vendor: "ARM".to_string(),
1240 model: "Mali-G78".to_string(),
1241 driver_version: "1.0".to_string(),
1242 memory_mb: Some(2048),
1243 compute_units: Some(14),
1244 supported_apis: vec![GpuApi::OpenGLES3, GpuApi::Vulkan11],
1245 performance_tier: GpuPerformanceTier::High,
1246 }),
1247 npu_info: None,
1248 thermal_info: ThermalInfo {
1249 current_state: ThermalState::Nominal,
1250 state: ThermalState::Nominal,
1251 throttling_supported: true,
1252 temperature_sensors: vec![],
1253 thermal_zones: vec![],
1254 },
1255 power_info: PowerInfo {
1256 battery_capacity_mah: Some(4000),
1257 battery_level_percent: Some(80),
1258 battery_level: Some(80),
1259 battery_health_percent: Some(100),
1260 charging_status: ChargingStatus::Discharging,
1261 is_charging: false,
1262 power_save_mode: Some(false),
1263 low_power_mode_available: true,
1264 },
1265 available_backends: vec![crate::MobileBackend::CPU, crate::MobileBackend::GPU],
1266 performance_scores: PerformanceScores {
1267 cpu_single_core: Some(1200),
1268 cpu_multi_core: Some(4800),
1269 gpu_score: Some(2500),
1270 memory_score: Some(1800),
1271 overall_tier: PerformanceTier::Mid,
1272 tier: PerformanceTier::Mid,
1273 },
1274 }
1275 });
1276
1277 MobileQuantizationEngine::new(config, device_info, None).expect("Operation failed")
1278 }
1279
1280 fn create_test_quantized_weights() -> HashMap<String, QuantizedTensor> {
1281 let mut weights = HashMap::new();
1282
1283 weights.insert(
1284 "layer1.weight".to_string(),
1285 QuantizedTensor {
1286 data: vec![1, 2, 3, 4, 5],
1287 scales: vec![0.1],
1288 zero_points: vec![0],
1289 shape: vec![5],
1290 scheme: QuantizationScheme {
1291 bits: 8,
1292 symmetric: false,
1293 signed: true,
1294 method: QuantizationMethod::Linear,
1295 },
1296 },
1297 );
1298
1299 weights.insert(
1300 "layer2.weight".to_string(),
1301 QuantizedTensor {
1302 data: vec![6, 7, 8, 9, 10],
1303 scales: vec![0.2],
1304 zero_points: vec![1],
1305 shape: vec![5],
1306 scheme: QuantizationScheme {
1307 bits: 4,
1308 symmetric: false,
1309 signed: false,
1310 method: QuantizationMethod::Linear,
1311 },
1312 },
1313 );
1314
1315 weights
1316 }
1317
1318 fn create_test_weights() -> HashMap<String, Tensor> {
1319 let mut weights = HashMap::new();
1320
1321 weights.insert(
1322 "layer1.weight".to_string(),
1323 Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], &[5]).expect("Operation failed"),
1324 );
1325 weights.insert(
1326 "layer2.weight".to_string(),
1327 Tensor::from_vec(vec![6.0, 7.0, 8.0, 9.0, 10.0], &[5]).expect("Operation failed"),
1328 );
1329
1330 weights
1331 }
1332}