torsh-backend 0.1.2

Backend abstraction layer for ToRSh
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
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
//! Performance analysis, benchmarking, and auto-tuning for quantization operations.
//!
//! This module provides comprehensive performance analysis tools for quantization operations,
//! including benchmarking suites, profiling utilities, and auto-tuning systems to optimize
//! quantization parameters for different hardware configurations.
//!
//! # Key Components
//!
//! ## Performance Benchmarking
//! - [`QuantizationBenchmarkSuite`] - Comprehensive benchmarking for quantization operations
//! - [`BenchmarkMetrics`] - Performance metrics collection and analysis
//! - [`BenchmarkConfiguration`] - Configurable benchmark parameters
//!
//! ## Profiling and Analysis
//! - [`QuantizationProfiler`] - Detailed profiling of quantization operations
//! - [`ProfileReport`] - Comprehensive profiling results with optimization suggestions
//! - [`MemoryProfiler`] - Memory usage analysis for quantized operations
//!
//! ## Auto-tuning Systems
//! - [`AutoTuner`] - Automatic parameter optimization for quantization
//! - [`TuningStrategy`] - Different strategies for parameter optimization
//! - [`TuningResult`] - Results of auto-tuning with optimal parameters
//!
//! # Examples
//!
//! ## Basic Benchmarking
//! ```rust
//! use torsh_backend::quantization::{
//!     QuantizationBenchmarkSuite, BenchmarkConfiguration, QuantizationParams
//! };
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let benchmark_suite = QuantizationBenchmarkSuite::new();
//! let config = BenchmarkConfiguration::default();
//! let params = QuantizationParams::int8_symmetric();
//!
//! let metrics = benchmark_suite.benchmark_quantization(&params, &config)?;
//! println!("Quantization throughput: {:.2} GB/s", metrics.throughput_gbps);
//! println!("Average latency: {:.2} ms", metrics.average_latency_ms);
//! # Ok(())
//! # }
//! ```
//!
//! ## Performance Profiling
//! ```rust
//! use torsh_backend::quantization::{QuantizationProfiler, QuantizationParams};
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let profiler = QuantizationProfiler::new();
//! let params = QuantizationParams::int8_symmetric();
//!
//! let report = profiler.profile_operation(&params, &[1.0; 1000])?;
//! println!("Memory usage: {} bytes", report.peak_memory_usage);
//! println!("Compute efficiency: {:.1}%", report.compute_efficiency_percent);
//! # Ok(())
//! # }
//! ```
//!
//! ## Auto-tuning
//! ```rust
//! use torsh_backend::quantization::{AutoTuner, TuningStrategy};
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let auto_tuner = AutoTuner::new(TuningStrategy::LatencyOptimized);
//! let sample_data = vec![1.0; 10000];
//!
//! let result = auto_tuner.tune_parameters(&sample_data)?;
//! println!("Optimal quantization: {:?}", result.optimal_params);
//! println!("Expected speedup: {:.2}x", result.performance_improvement_factor);
//! # Ok(())
//! # }
//! ```

use crate::BackendResult;
use super::core::{QuantizationParams, QuantizedDType, QuantizationScheme};
use super::hardware::QuantizationHardwareFeatures;
use std::time::{Duration, Instant};
use std::collections::HashMap;

/// Comprehensive benchmarking suite for quantization operations.
///
/// This suite provides standardized benchmarks for different quantization schemes,
/// hardware configurations, and data patterns to evaluate quantization performance.
#[derive(Debug, Clone)]
pub struct QuantizationBenchmarkSuite {
    hardware_features: QuantizationHardwareFeatures,
    benchmark_cache: HashMap<String, BenchmarkMetrics>,
}

/// Performance metrics collected during benchmarking.
///
/// Provides comprehensive metrics for evaluating quantization performance
/// across different dimensions including throughput, latency, and resource usage.
#[derive(Debug, Clone, PartialEq)]
pub struct BenchmarkMetrics {
    /// Throughput in gigabytes per second
    pub throughput_gbps: f32,
    /// Average operation latency in milliseconds
    pub average_latency_ms: f32,
    /// Peak memory usage during operation in bytes
    pub peak_memory_usage: usize,
    /// Memory bandwidth utilization percentage
    pub memory_bandwidth_utilization: f32,
    /// Compute utilization percentage
    pub compute_utilization: f32,
    /// Number of operations per second
    pub operations_per_second: f64,
    /// Energy efficiency in operations per joule (if available)
    pub energy_efficiency: Option<f64>,
    /// Cache hit rate percentage
    pub cache_hit_rate: f32,
}

/// Configuration parameters for benchmarking operations.
///
/// Allows customization of benchmark execution including data sizes,
/// iteration counts, and measurement parameters.
#[derive(Debug, Clone)]
pub struct BenchmarkConfiguration {
    /// Number of benchmark iterations
    pub iterations: usize,
    /// Warm-up iterations before measurement
    pub warmup_iterations: usize,
    /// Data sizes to benchmark (in elements)
    pub data_sizes: Vec<usize>,
    /// Enable detailed memory profiling
    pub enable_memory_profiling: bool,
    /// Enable power measurement (if supported)
    pub enable_power_measurement: bool,
    /// Target confidence interval for measurements
    pub confidence_interval: f32,
}

/// Detailed profiler for quantization operations.
///
/// Provides in-depth analysis of quantization performance including
/// memory usage patterns, compute efficiency, and optimization opportunities.
#[derive(Debug)]
pub struct QuantizationProfiler {
    hardware_features: QuantizationHardwareFeatures,
    memory_tracker: MemoryProfiler,
}

/// Comprehensive profiling report with optimization suggestions.
///
/// Contains detailed analysis results and recommendations for improving
/// quantization performance based on observed patterns.
#[derive(Debug, Clone)]
pub struct ProfileReport {
    /// Peak memory usage during operation
    pub peak_memory_usage: usize,
    /// Average memory usage during operation
    pub average_memory_usage: usize,
    /// Memory allocation pattern analysis
    pub memory_allocation_pattern: MemoryAllocationPattern,
    /// Compute efficiency percentage
    pub compute_efficiency_percent: f32,
    /// Identified performance bottlenecks
    pub bottlenecks: Vec<PerformanceBottleneck>,
    /// Optimization suggestions
    pub optimization_suggestions: Vec<OptimizationSuggestion>,
    /// Hardware utilization breakdown
    pub hardware_utilization: HardwareUtilization,
}

/// Memory profiler for quantized operations.
///
/// Tracks memory allocation patterns, usage efficiency, and identifies
/// opportunities for memory optimization in quantization workflows.
#[derive(Debug)]
pub struct MemoryProfiler {
    peak_usage: usize,
    current_usage: usize,
    allocation_count: usize,
    deallocation_count: usize,
    allocation_history: Vec<MemoryAllocation>,
}

/// Automatic parameter tuning system for quantization.
///
/// Uses various optimization strategies to automatically determine optimal
/// quantization parameters for given hardware and data characteristics.
#[derive(Debug)]
pub struct AutoTuner {
    strategy: TuningStrategy,
    hardware_features: QuantizationHardwareFeatures,
    tuning_cache: HashMap<String, TuningResult>,
}

/// Strategies for auto-tuning quantization parameters.
///
/// Different optimization objectives for parameter tuning, allowing
/// users to prioritize different aspects of quantization performance.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TuningStrategy {
    /// Optimize for minimum latency
    LatencyOptimized,
    /// Optimize for maximum throughput
    ThroughputOptimized,
    /// Optimize for minimum memory usage
    MemoryOptimized,
    /// Balance between accuracy and performance
    AccuracyBalanced,
    /// Optimize for energy efficiency
    EnergyOptimized,
    /// Custom optimization with user-defined weights
    Custom {
        latency_weight: f32,
        throughput_weight: f32,
        memory_weight: f32,
        accuracy_weight: f32,
    },
}

/// Results from auto-tuning with optimal parameters.
///
/// Contains the best parameters found during tuning along with
/// performance estimates and configuration details.
#[derive(Debug, Clone)]
pub struct TuningResult {
    /// Optimal quantization parameters
    pub optimal_params: QuantizationParams,
    /// Expected performance improvement factor
    pub performance_improvement_factor: f32,
    /// Estimated accuracy impact (relative to baseline)
    pub accuracy_impact: f32,
    /// Estimated memory savings percentage
    pub memory_savings_percent: f32,
    /// Tuning confidence score (0.0 to 1.0)
    pub confidence_score: f32,
    /// Alternative parameter configurations
    pub alternatives: Vec<(QuantizationParams, f32)>,
}

/// Memory allocation pattern analysis.
#[derive(Debug, Clone)]
pub struct MemoryAllocationPattern {
    pub fragmentation_level: f32,
    pub allocation_frequency: f32,
    pub peak_to_average_ratio: f32,
    pub temporal_locality: f32,
}

/// Identified performance bottleneck.
#[derive(Debug, Clone)]
pub struct PerformanceBottleneck {
    pub bottleneck_type: BottleneckType,
    pub severity: BottleneckSeverity,
    pub description: String,
    pub impact_percentage: f32,
}

/// Types of performance bottlenecks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BottleneckType {
    MemoryBandwidth,
    ComputeUtilization,
    CacheEfficiency,
    Synchronization,
    DataMovement,
    AlgorithmChoice,
}

/// Severity levels for performance bottlenecks.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum BottleneckSeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Optimization suggestion with implementation details.
#[derive(Debug, Clone)]
pub struct OptimizationSuggestion {
    pub suggestion_type: OptimizationType,
    pub description: String,
    pub expected_improvement: f32,
    pub implementation_complexity: ComplexityLevel,
    pub prerequisites: Vec<String>,
}

/// Types of optimization suggestions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OptimizationType {
    ParameterTuning,
    AlgorithmChange,
    MemoryOptimization,
    HardwareUtilization,
    BatchSizeOptimization,
    CacheOptimization,
}

/// Implementation complexity levels.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ComplexityLevel {
    Trivial,
    Simple,
    Moderate,
    Complex,
    Expert,
}

/// Hardware utilization breakdown.
#[derive(Debug, Clone)]
pub struct HardwareUtilization {
    pub cpu_utilization: f32,
    pub memory_utilization: f32,
    pub cache_utilization: f32,
    pub vector_unit_utilization: f32,
    pub gpu_utilization: Option<f32>,
}

/// Memory allocation tracking entry.
#[derive(Debug, Clone)]
pub struct MemoryAllocation {
    pub size: usize,
    pub timestamp: Instant,
    pub allocation_type: AllocationType,
}

/// Types of memory allocations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AllocationType {
    InputBuffer,
    OutputBuffer,
    IntermediateBuffer,
    LookupTable,
    Workspace,
}

impl QuantizationBenchmarkSuite {
    /// Create a new benchmark suite with hardware detection.
    pub fn new() -> Self {
        Self {
            hardware_features: QuantizationHardwareFeatures::detect_current(),
            benchmark_cache: HashMap::new(),
        }
    }

    /// Create a benchmark suite with specific hardware features.
    pub fn with_hardware_features(features: QuantizationHardwareFeatures) -> Self {
        Self {
            hardware_features: features,
            benchmark_cache: HashMap::new(),
        }
    }

    /// Benchmark quantization operation with given parameters.
    pub fn benchmark_quantization(
        &mut self,
        params: &QuantizationParams,
        config: &BenchmarkConfiguration,
    ) -> BackendResult<BenchmarkMetrics> {
        let cache_key = format!("{:?}_{:?}", params, config.data_sizes);

        if let Some(cached_metrics) = self.benchmark_cache.get(&cache_key) {
            return Ok(cached_metrics.clone());
        }

        let mut total_metrics = BenchmarkMetrics::default();
        let mut valid_runs = 0;

        for &data_size in &config.data_sizes {
            let data = self.generate_benchmark_data(data_size)?;

            // Warm-up runs
            for _ in 0..config.warmup_iterations {
                let _ = self.run_quantization_benchmark(&data, params)?;
            }

            // Measurement runs
            let mut iteration_metrics = Vec::new();
            for _ in 0..config.iterations {
                let metrics = self.run_quantization_benchmark(&data, params)?;
                iteration_metrics.push(metrics);
            }

            if !iteration_metrics.is_empty() {
                let averaged_metrics = self.average_metrics(&iteration_metrics);
                total_metrics = self.combine_metrics(&total_metrics, &averaged_metrics);
                valid_runs += 1;
            }
        }

        if valid_runs > 0 {
            total_metrics = self.normalize_metrics(&total_metrics, valid_runs);
            self.benchmark_cache.insert(cache_key, total_metrics.clone());
            Ok(total_metrics)
        } else {
            Err(crate::BackendError::QuantizationError(
                "No valid benchmark runs completed".to_string()
            ))
        }
    }

    /// Benchmark dequantization operation.
    pub fn benchmark_dequantization(
        &mut self,
        params: &QuantizationParams,
        config: &BenchmarkConfiguration,
    ) -> BackendResult<BenchmarkMetrics> {
        // Similar implementation to benchmark_quantization but for dequantization
        let mut total_metrics = BenchmarkMetrics::default();
        let mut valid_runs = 0;

        for &data_size in &config.data_sizes {
            let quantized_data = self.generate_quantized_benchmark_data(data_size, params)?;

            // Warm-up runs
            for _ in 0..config.warmup_iterations {
                let _ = self.run_dequantization_benchmark(&quantized_data, params)?;
            }

            // Measurement runs
            let mut iteration_metrics = Vec::new();
            for _ in 0..config.iterations {
                let metrics = self.run_dequantization_benchmark(&quantized_data, params)?;
                iteration_metrics.push(metrics);
            }

            if !iteration_metrics.is_empty() {
                let averaged_metrics = self.average_metrics(&iteration_metrics);
                total_metrics = self.combine_metrics(&total_metrics, &averaged_metrics);
                valid_runs += 1;
            }
        }

        if valid_runs > 0 {
            total_metrics = self.normalize_metrics(&total_metrics, valid_runs);
            Ok(total_metrics)
        } else {
            Err(crate::BackendError::QuantizationError(
                "No valid dequantization benchmark runs completed".to_string()
            ))
        }
    }

    /// Run comprehensive benchmark suite covering all supported operations.
    pub fn run_comprehensive_benchmark(
        &mut self,
        config: &BenchmarkConfiguration,
    ) -> BackendResult<HashMap<String, BenchmarkMetrics>> {
        let mut results = HashMap::new();

        // Test different quantization schemes
        let schemes = vec![
            QuantizationParams::int8_symmetric(),
            QuantizationParams::int8_asymmetric(),
            QuantizationParams::uint8_asymmetric(),
            QuantizationParams::int4_blockwise(),
        ];

        for (i, params) in schemes.iter().enumerate() {
            let scheme_name = format!("scheme_{}", i);

            // Benchmark quantization
            let quant_metrics = self.benchmark_quantization(params, config)?;
            results.insert(format!("{}_quantization", scheme_name), quant_metrics);

            // Benchmark dequantization
            let dequant_metrics = self.benchmark_dequantization(params, config)?;
            results.insert(format!("{}_dequantization", scheme_name), dequant_metrics);
        }

        Ok(results)
    }

    fn generate_benchmark_data(&self, size: usize) -> BackendResult<Vec<f32>> {
        // Generate representative benchmark data
        let mut data = Vec::with_capacity(size);
        for i in 0..size {
            // Use a mix of patterns to simulate real-world data
            let value = ((i as f32 * 0.1).sin() + (i as f32 * 0.01).cos()) * 10.0;
            data.push(value);
        }
        Ok(data)
    }

    fn generate_quantized_benchmark_data(
        &self,
        size: usize,
        params: &QuantizationParams,
    ) -> BackendResult<Vec<u8>> {
        let float_data = self.generate_benchmark_data(size)?;
        // Convert to quantized format based on params
        let mut quantized = Vec::with_capacity(size);
        for value in float_data {
            // Simplified quantization for benchmarking
            let quantized_value = ((value * params.scale[0]) + params.zero_point[0] as f32) as u8;
            quantized.push(quantized_value);
        }
        Ok(quantized)
    }

    fn run_quantization_benchmark(
        &self,
        data: &[f32],
        params: &QuantizationParams,
    ) -> BackendResult<BenchmarkMetrics> {
        let start_time = Instant::now();
        let start_memory = self.get_current_memory_usage();

        // Simulate quantization operation
        let mut quantized = Vec::with_capacity(data.len());
        for &value in data {
            let quantized_value = ((value * params.scale[0]) + params.zero_point[0] as f32) as u8;
            quantized.push(quantized_value);
        }

        let end_time = Instant::now();
        let end_memory = self.get_current_memory_usage();

        let duration = end_time.duration_since(start_time);
        let data_size_bytes = data.len() * std::mem::size_of::<f32>();

        Ok(BenchmarkMetrics {
            throughput_gbps: (data_size_bytes as f32) / (duration.as_secs_f32() * 1e9),
            average_latency_ms: duration.as_secs_f32() * 1000.0,
            peak_memory_usage: end_memory.saturating_sub(start_memory),
            memory_bandwidth_utilization: 85.0, // Placeholder
            compute_utilization: 75.0, // Placeholder
            operations_per_second: data.len() as f64 / duration.as_secs_f64(),
            energy_efficiency: None,
            cache_hit_rate: 90.0, // Placeholder
        })
    }

    fn run_dequantization_benchmark(
        &self,
        data: &[u8],
        params: &QuantizationParams,
    ) -> BackendResult<BenchmarkMetrics> {
        let start_time = Instant::now();
        let start_memory = self.get_current_memory_usage();

        // Simulate dequantization operation
        let mut dequantized = Vec::with_capacity(data.len());
        for &value in data {
            let dequantized_value = (value as f32 - params.zero_point[0] as f32) / params.scale[0];
            dequantized.push(dequantized_value);
        }

        let end_time = Instant::now();
        let end_memory = self.get_current_memory_usage();

        let duration = end_time.duration_since(start_time);
        let data_size_bytes = data.len() * std::mem::size_of::<u8>();

        Ok(BenchmarkMetrics {
            throughput_gbps: (data_size_bytes as f32) / (duration.as_secs_f32() * 1e9),
            average_latency_ms: duration.as_secs_f32() * 1000.0,
            peak_memory_usage: end_memory.saturating_sub(start_memory),
            memory_bandwidth_utilization: 80.0, // Placeholder
            compute_utilization: 70.0, // Placeholder
            operations_per_second: data.len() as f64 / duration.as_secs_f64(),
            energy_efficiency: None,
            cache_hit_rate: 88.0, // Placeholder
        })
    }

    fn get_current_memory_usage(&self) -> usize {
        // Platform-specific memory usage detection would go here
        // For now, return a placeholder value
        1024 * 1024 // 1MB placeholder
    }

    fn average_metrics(&self, metrics: &[BenchmarkMetrics]) -> BenchmarkMetrics {
        if metrics.is_empty() {
            return BenchmarkMetrics::default();
        }

        let count = metrics.len() as f32;
        BenchmarkMetrics {
            throughput_gbps: metrics.iter().map(|m| m.throughput_gbps).sum::<f32>() / count,
            average_latency_ms: metrics.iter().map(|m| m.average_latency_ms).sum::<f32>() / count,
            peak_memory_usage: metrics.iter().map(|m| m.peak_memory_usage).max().unwrap_or(0),
            memory_bandwidth_utilization: metrics.iter().map(|m| m.memory_bandwidth_utilization).sum::<f32>() / count,
            compute_utilization: metrics.iter().map(|m| m.compute_utilization).sum::<f32>() / count,
            operations_per_second: metrics.iter().map(|m| m.operations_per_second).sum::<f64>() / count as f64,
            energy_efficiency: None,
            cache_hit_rate: metrics.iter().map(|m| m.cache_hit_rate).sum::<f32>() / count,
        }
    }

    fn combine_metrics(&self, a: &BenchmarkMetrics, b: &BenchmarkMetrics) -> BenchmarkMetrics {
        BenchmarkMetrics {
            throughput_gbps: (a.throughput_gbps + b.throughput_gbps) / 2.0,
            average_latency_ms: (a.average_latency_ms + b.average_latency_ms) / 2.0,
            peak_memory_usage: a.peak_memory_usage.max(b.peak_memory_usage),
            memory_bandwidth_utilization: (a.memory_bandwidth_utilization + b.memory_bandwidth_utilization) / 2.0,
            compute_utilization: (a.compute_utilization + b.compute_utilization) / 2.0,
            operations_per_second: (a.operations_per_second + b.operations_per_second) / 2.0,
            energy_efficiency: None,
            cache_hit_rate: (a.cache_hit_rate + b.cache_hit_rate) / 2.0,
        }
    }

    fn normalize_metrics(&self, metrics: &BenchmarkMetrics, count: usize) -> BenchmarkMetrics {
        if count <= 1 {
            return metrics.clone();
        }

        let count_f32 = count as f32;
        BenchmarkMetrics {
            throughput_gbps: metrics.throughput_gbps / count_f32,
            average_latency_ms: metrics.average_latency_ms / count_f32,
            peak_memory_usage: metrics.peak_memory_usage,
            memory_bandwidth_utilization: metrics.memory_bandwidth_utilization / count_f32,
            compute_utilization: metrics.compute_utilization / count_f32,
            operations_per_second: metrics.operations_per_second / count as f64,
            energy_efficiency: None,
            cache_hit_rate: metrics.cache_hit_rate / count_f32,
        }
    }
}

impl QuantizationProfiler {
    /// Create a new profiler with hardware detection.
    pub fn new() -> Self {
        Self {
            hardware_features: QuantizationHardwareFeatures::detect_current(),
            memory_tracker: MemoryProfiler::new(),
        }
    }

    /// Profile a quantization operation and generate detailed report.
    pub fn profile_operation(
        &mut self,
        params: &QuantizationParams,
        data: &[f32],
    ) -> BackendResult<ProfileReport> {
        self.memory_tracker.reset();

        let start_time = Instant::now();

        // Track memory allocation for input
        self.memory_tracker.track_allocation(
            data.len() * std::mem::size_of::<f32>(),
            AllocationType::InputBuffer,
        );

        // Simulate quantization with profiling
        let output_size = data.len();
        self.memory_tracker.track_allocation(
            output_size,
            AllocationType::OutputBuffer,
        );

        // Perform operation analysis
        let mut quantized = Vec::with_capacity(data.len());
        for &value in data {
            let quantized_value = ((value * params.scale[0]) + params.zero_point[0] as f32) as u8;
            quantized.push(quantized_value);
        }

        let operation_time = start_time.elapsed();

        // Analyze performance characteristics
        let bottlenecks = self.identify_bottlenecks(params, data.len(), operation_time);
        let suggestions = self.generate_optimization_suggestions(&bottlenecks, params);
        let hardware_utilization = self.analyze_hardware_utilization();

        Ok(ProfileReport {
            peak_memory_usage: self.memory_tracker.get_peak_usage(),
            average_memory_usage: self.memory_tracker.get_average_usage(),
            memory_allocation_pattern: self.memory_tracker.analyze_allocation_pattern(),
            compute_efficiency_percent: self.calculate_compute_efficiency(operation_time, data.len()),
            bottlenecks,
            optimization_suggestions: suggestions,
            hardware_utilization,
        })
    }

    /// Profile memory usage patterns for batch operations.
    pub fn profile_batch_operations(
        &mut self,
        params: &QuantizationParams,
        batch_sizes: &[usize],
    ) -> BackendResult<Vec<ProfileReport>> {
        let mut reports = Vec::new();

        for &batch_size in batch_sizes {
            let data = vec![1.0; batch_size]; // Simplified data for profiling
            let report = self.profile_operation(params, &data)?;
            reports.push(report);
        }

        Ok(reports)
    }

    fn identify_bottlenecks(
        &self,
        params: &QuantizationParams,
        data_size: usize,
        operation_time: Duration,
    ) -> Vec<PerformanceBottleneck> {
        let mut bottlenecks = Vec::new();

        // Memory bandwidth bottleneck analysis
        let theoretical_bandwidth_gbps = 100.0; // Placeholder
        let actual_bandwidth = (data_size as f32 * std::mem::size_of::<f32>() as f32) /
                              (operation_time.as_secs_f32() * 1e9);

        if actual_bandwidth < theoretical_bandwidth_gbps * 0.5 {
            bottlenecks.push(PerformanceBottleneck {
                bottleneck_type: BottleneckType::MemoryBandwidth,
                severity: BottleneckSeverity::High,
                description: "Memory bandwidth utilization is below 50% of theoretical maximum".to_string(),
                impact_percentage: 60.0,
            });
        }

        // Compute utilization analysis
        if !self.hardware_features.supports_int8_simd && params.dtype == QuantizedDType::Int8 {
            bottlenecks.push(PerformanceBottleneck {
                bottleneck_type: BottleneckType::ComputeUtilization,
                severity: BottleneckSeverity::Medium,
                description: "Hardware lacks SIMD support for INT8 operations".to_string(),
                impact_percentage: 40.0,
            });
        }

        bottlenecks
    }

    fn generate_optimization_suggestions(
        &self,
        bottlenecks: &[PerformanceBottleneck],
        params: &QuantizationParams,
    ) -> Vec<OptimizationSuggestion> {
        let mut suggestions = Vec::new();

        for bottleneck in bottlenecks {
            match bottleneck.bottleneck_type {
                BottleneckType::MemoryBandwidth => {
                    suggestions.push(OptimizationSuggestion {
                        suggestion_type: OptimizationType::BatchSizeOptimization,
                        description: "Increase batch size to improve memory bandwidth utilization".to_string(),
                        expected_improvement: 25.0,
                        implementation_complexity: ComplexityLevel::Simple,
                        prerequisites: vec!["Available memory for larger batches".to_string()],
                    });
                }
                BottleneckType::ComputeUtilization => {
                    if self.hardware_features.supports_vnni {
                        suggestions.push(OptimizationSuggestion {
                            suggestion_type: OptimizationType::HardwareUtilization,
                            description: "Enable VNNI acceleration for INT8 operations".to_string(),
                            expected_improvement: 40.0,
                            implementation_complexity: ComplexityLevel::Moderate,
                            prerequisites: vec!["VNNI-compatible data layout".to_string()],
                        });
                    }
                }
                _ => {}
            }
        }

        suggestions
    }

    fn analyze_hardware_utilization(&self) -> HardwareUtilization {
        // Simplified hardware utilization analysis
        HardwareUtilization {
            cpu_utilization: 75.0,
            memory_utilization: 60.0,
            cache_utilization: 80.0,
            vector_unit_utilization: if self.hardware_features.supports_int8_simd { 85.0 } else { 0.0 },
            gpu_utilization: None,
        }
    }

    fn calculate_compute_efficiency(&self, operation_time: Duration, data_size: usize) -> f32 {
        // Simplified efficiency calculation
        let theoretical_ops_per_second = 1e9; // 1 GOP/s theoretical
        let actual_ops_per_second = data_size as f64 / operation_time.as_secs_f64();

        ((actual_ops_per_second / theoretical_ops_per_second) * 100.0) as f32
    }
}

impl AutoTuner {
    /// Create a new auto-tuner with specified strategy.
    pub fn new(strategy: TuningStrategy) -> Self {
        Self {
            strategy,
            hardware_features: QuantizationHardwareFeatures::detect_current(),
            tuning_cache: HashMap::new(),
        }
    }

    /// Tune quantization parameters for optimal performance.
    pub fn tune_parameters(&mut self, sample_data: &[f32]) -> BackendResult<TuningResult> {
        let data_hash = self.calculate_data_hash(sample_data);
        let cache_key = format!("{:?}_{}", self.strategy, data_hash);

        if let Some(cached_result) = self.tuning_cache.get(&cache_key) {
            return Ok(cached_result.clone());
        }

        let candidate_params = self.generate_candidate_parameters();
        let mut best_result = None;
        let mut best_score = f32::NEG_INFINITY;

        for params in candidate_params {
            let score = self.evaluate_parameters(&params, sample_data)?;

            if score > best_score {
                best_score = score;
                best_result = Some(params);
            }
        }

        let optimal_params = best_result.ok_or_else(|| {
            crate::BackendError::QuantizationError("No suitable parameters found".to_string())
        })?;

        let result = TuningResult {
            optimal_params: optimal_params.clone(),
            performance_improvement_factor: self.estimate_improvement_factor(&optimal_params, sample_data)?,
            accuracy_impact: self.estimate_accuracy_impact(&optimal_params, sample_data)?,
            memory_savings_percent: self.estimate_memory_savings(&optimal_params),
            confidence_score: 0.85, // Placeholder
            alternatives: Vec::new(),
        };

        self.tuning_cache.insert(cache_key, result.clone());
        Ok(result)
    }

    /// Tune parameters for specific hardware configuration.
    pub fn tune_for_hardware(
        &mut self,
        sample_data: &[f32],
        target_hardware: &QuantizationHardwareFeatures,
    ) -> BackendResult<TuningResult> {
        let original_features = self.hardware_features.clone();
        self.hardware_features = target_hardware.clone();

        let result = self.tune_parameters(sample_data);

        self.hardware_features = original_features;
        result
    }

    fn generate_candidate_parameters(&self) -> Vec<QuantizationParams> {
        let mut candidates = Vec::new();

        // Generate candidates based on hardware capabilities
        if self.hardware_features.supports_int8_simd {
            candidates.push(QuantizationParams::int8_symmetric());
            candidates.push(QuantizationParams::int8_asymmetric());
        }

        if self.hardware_features.supports_int4_operations {
            candidates.push(QuantizationParams::int4_blockwise());
        }

        candidates.push(QuantizationParams::uint8_asymmetric());

        // Add mixed precision candidates if supported
        if self.hardware_features.supports_mixed_precision {
            candidates.extend(self.generate_mixed_precision_candidates());
        }

        candidates
    }

    fn generate_mixed_precision_candidates(&self) -> Vec<QuantizationParams> {
        // Generate mixed precision quantization parameters
        vec![
            QuantizationParams {
                dtype: QuantizedDType::Mixed(vec![8, 4, 8]), // INT8 for weights, INT4 for activations
                scheme: QuantizationScheme::Symmetric,
                scale: vec![0.1, 0.05, 0.1],
                zero_point: vec![0, 0, 0],
                block_size: Some(64),
                channel_axis: None,
            }
        ]
    }

    fn evaluate_parameters(&self, params: &QuantizationParams, data: &[f32]) -> BackendResult<f32> {
        // Simulate quantization and evaluate based on strategy
        let simulated_performance = self.simulate_performance(params, data)?;
        let simulated_accuracy = self.simulate_accuracy_impact(params, data)?;
        let memory_usage = self.estimate_memory_usage(params, data.len());

        let score = match &self.strategy {
            TuningStrategy::LatencyOptimized => {
                -simulated_performance.average_latency_ms
            }
            TuningStrategy::ThroughputOptimized => {
                simulated_performance.throughput_gbps
            }
            TuningStrategy::MemoryOptimized => {
                -(memory_usage as f32) / 1e6 // Negative because we want to minimize memory
            }
            TuningStrategy::AccuracyBalanced => {
                simulated_performance.throughput_gbps * 0.6 + simulated_accuracy * 0.4
            }
            TuningStrategy::EnergyOptimized => {
                simulated_performance.energy_efficiency.unwrap_or(0.0) as f32
            }
            TuningStrategy::Custom {
                latency_weight,
                throughput_weight,
                memory_weight,
                accuracy_weight
            } => {
                -simulated_performance.average_latency_ms * latency_weight +
                simulated_performance.throughput_gbps * throughput_weight +
                -(memory_usage as f32) / 1e6 * memory_weight +
                simulated_accuracy * accuracy_weight
            }
        };

        Ok(score)
    }

    fn simulate_performance(&self, params: &QuantizationParams, data: &[f32]) -> BackendResult<BenchmarkMetrics> {
        // Simplified performance simulation
        let base_latency = 1.0; // 1ms base latency
        let latency_factor = match params.dtype {
            QuantizedDType::Int8 => if self.hardware_features.supports_int8_simd { 0.5 } else { 1.0 },
            QuantizedDType::UInt8 => 0.8,
            QuantizedDType::Int4 => if self.hardware_features.supports_int4_operations { 0.3 } else { 1.5 },
            _ => 1.0,
        };

        let latency = base_latency * latency_factor;
        let throughput = (data.len() as f32 * std::mem::size_of::<f32>() as f32) / (latency * 1e6);

        Ok(BenchmarkMetrics {
            throughput_gbps: throughput,
            average_latency_ms: latency,
            peak_memory_usage: data.len() * 2, // Simplified
            memory_bandwidth_utilization: 80.0,
            compute_utilization: 70.0,
            operations_per_second: data.len() as f64 / (latency as f64 / 1000.0),
            energy_efficiency: Some(1000.0), // Simplified
            cache_hit_rate: 85.0,
        })
    }

    fn simulate_accuracy_impact(&self, params: &QuantizationParams, _data: &[f32]) -> BackendResult<f32> {
        // Simplified accuracy impact simulation
        let accuracy_score = match params.dtype {
            QuantizedDType::Int8 => 0.95,
            QuantizedDType::UInt8 => 0.93,
            QuantizedDType::Int4 => 0.85,
            QuantizedDType::Binary => 0.70,
            _ => 0.90,
        };

        Ok(accuracy_score)
    }

    fn estimate_memory_usage(&self, params: &QuantizationParams, data_size: usize) -> usize {
        let element_size = match params.dtype {
            QuantizedDType::Int8 | QuantizedDType::UInt8 => 1,
            QuantizedDType::Int16 | QuantizedDType::UInt16 => 2,
            QuantizedDType::Int4 | QuantizedDType::UInt4 => 1, // Packed
            QuantizedDType::Binary => data_size / 8,
            _ => 1,
        };

        data_size * element_size
    }

    fn estimate_improvement_factor(&self, params: &QuantizationParams, data: &[f32]) -> BackendResult<f32> {
        let baseline_performance = self.simulate_performance(&QuantizationParams::int8_symmetric(), data)?;
        let optimized_performance = self.simulate_performance(params, data)?;

        Ok(baseline_performance.average_latency_ms / optimized_performance.average_latency_ms)
    }

    fn estimate_accuracy_impact(&self, params: &QuantizationParams, data: &[f32]) -> BackendResult<f32> {
        let baseline_accuracy = self.simulate_accuracy_impact(&QuantizationParams::int8_symmetric(), data)?;
        let optimized_accuracy = self.simulate_accuracy_impact(params, data)?;

        Ok((optimized_accuracy - baseline_accuracy) / baseline_accuracy)
    }

    fn estimate_memory_savings(&self, params: &QuantizationParams) -> f32 {
        let baseline_size = 4; // f32 baseline
        let quantized_size = match params.dtype {
            QuantizedDType::Int8 | QuantizedDType::UInt8 => 1,
            QuantizedDType::Int16 | QuantizedDType::UInt16 => 2,
            QuantizedDType::Int4 | QuantizedDType::UInt4 => 0.5,
            QuantizedDType::Binary => 0.125,
            _ => 1.0,
        };

        ((baseline_size - quantized_size) / baseline_size) * 100.0
    }

    fn calculate_data_hash(&self, data: &[f32]) -> u64 {
        // Simplified hash calculation for caching
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        data.len().hash(&mut hasher);

        // Sample a few values for hash to avoid hashing entire dataset
        let sample_indices = [0, data.len() / 4, data.len() / 2, 3 * data.len() / 4, data.len() - 1];
        for &idx in &sample_indices {
            if idx < data.len() {
                (data[idx] as u32).hash(&mut hasher);
            }
        }

        hasher.finish()
    }
}

impl MemoryProfiler {
    fn new() -> Self {
        Self {
            peak_usage: 0,
            current_usage: 0,
            allocation_count: 0,
            deallocation_count: 0,
            allocation_history: Vec::new(),
        }
    }

    fn reset(&mut self) {
        self.peak_usage = 0;
        self.current_usage = 0;
        self.allocation_count = 0;
        self.deallocation_count = 0;
        self.allocation_history.clear();
    }

    fn track_allocation(&mut self, size: usize, allocation_type: AllocationType) {
        self.current_usage += size;
        self.peak_usage = self.peak_usage.max(self.current_usage);
        self.allocation_count += 1;

        self.allocation_history.push(MemoryAllocation {
            size,
            timestamp: Instant::now(),
            allocation_type,
        });
    }

    fn get_peak_usage(&self) -> usize {
        self.peak_usage
    }

    fn get_average_usage(&self) -> usize {
        if self.allocation_history.is_empty() {
            0
        } else {
            self.allocation_history.iter().map(|a| a.size).sum::<usize>() / self.allocation_history.len()
        }
    }

    fn analyze_allocation_pattern(&self) -> MemoryAllocationPattern {
        let total_allocations = self.allocation_history.len();
        let avg_size = if total_allocations > 0 {
            self.allocation_history.iter().map(|a| a.size).sum::<usize>() / total_allocations
        } else {
            0
        };

        MemoryAllocationPattern {
            fragmentation_level: 0.1, // Simplified
            allocation_frequency: total_allocations as f32,
            peak_to_average_ratio: if avg_size > 0 { self.peak_usage as f32 / avg_size as f32 } else { 1.0 },
            temporal_locality: 0.8, // Simplified
        }
    }
}

impl Default for BenchmarkConfiguration {
    fn default() -> Self {
        Self {
            iterations: 10,
            warmup_iterations: 3,
            data_sizes: vec![1024, 4096, 16384, 65536],
            enable_memory_profiling: true,
            enable_power_measurement: false,
            confidence_interval: 0.95,
        }
    }
}

impl Default for BenchmarkMetrics {
    fn default() -> Self {
        Self {
            throughput_gbps: 0.0,
            average_latency_ms: 0.0,
            peak_memory_usage: 0,
            memory_bandwidth_utilization: 0.0,
            compute_utilization: 0.0,
            operations_per_second: 0.0,
            energy_efficiency: None,
            cache_hit_rate: 0.0,
        }
    }
}

impl QuantizationParams {
    /// Create parameters optimized for INT8 symmetric quantization.
    pub fn int8_symmetric() -> Self {
        Self {
            dtype: QuantizedDType::Int8,
            scheme: QuantizationScheme::Symmetric,
            scale: vec![0.1],
            zero_point: vec![0],
            block_size: None,
            channel_axis: None,
        }
    }

    /// Create parameters optimized for INT8 asymmetric quantization.
    pub fn int8_asymmetric() -> Self {
        Self {
            dtype: QuantizedDType::Int8,
            scheme: QuantizationScheme::Asymmetric,
            scale: vec![0.1],
            zero_point: vec![128],
            block_size: None,
            channel_axis: None,
        }
    }

    /// Create parameters optimized for UINT8 asymmetric quantization.
    pub fn uint8_asymmetric() -> Self {
        Self {
            dtype: QuantizedDType::UInt8,
            scheme: QuantizationScheme::Asymmetric,
            scale: vec![0.1],
            zero_point: vec![128],
            block_size: None,
            channel_axis: None,
        }
    }

    /// Create parameters optimized for INT4 block-wise quantization.
    pub fn int4_blockwise() -> Self {
        Self {
            dtype: QuantizedDType::Int4,
            scheme: QuantizationScheme::BlockWise,
            scale: vec![0.05],
            zero_point: vec![8],
            block_size: Some(32),
            channel_axis: None,
        }
    }
}

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

    #[test]
    fn test_benchmark_suite_creation() {
        let suite = QuantizationBenchmarkSuite::new();
        assert!(suite.benchmark_cache.is_empty());
    }

    #[test]
    fn test_benchmark_configuration_default() {
        let config = BenchmarkConfiguration::default();
        assert_eq!(config.iterations, 10);
        assert_eq!(config.warmup_iterations, 3);
        assert!(!config.data_sizes.is_empty());
        assert!(config.enable_memory_profiling);
    }

    #[test]
    fn test_profiler_creation() {
        let profiler = QuantizationProfiler::new();
        assert_eq!(profiler.memory_tracker.allocation_count, 0);
    }

    #[test]
    fn test_auto_tuner_creation() {
        let tuner = AutoTuner::new(TuningStrategy::LatencyOptimized);
        assert_eq!(tuner.strategy, TuningStrategy::LatencyOptimized);
        assert!(tuner.tuning_cache.is_empty());
    }

    #[test]
    fn test_tuning_strategy_comparison() {
        assert_eq!(TuningStrategy::LatencyOptimized, TuningStrategy::LatencyOptimized);
        assert_ne!(TuningStrategy::LatencyOptimized, TuningStrategy::ThroughputOptimized);
    }

    #[test]
    fn test_benchmark_metrics_default() {
        let metrics = BenchmarkMetrics::default();
        assert_eq!(metrics.throughput_gbps, 0.0);
        assert_eq!(metrics.peak_memory_usage, 0);
        assert!(metrics.energy_efficiency.is_none());
    }

    #[test]
    fn test_memory_profiler_tracking() {
        let mut profiler = MemoryProfiler::new();

        profiler.track_allocation(1024, AllocationType::InputBuffer);
        assert_eq!(profiler.current_usage, 1024);
        assert_eq!(profiler.peak_usage, 1024);
        assert_eq!(profiler.allocation_count, 1);

        profiler.track_allocation(2048, AllocationType::OutputBuffer);
        assert_eq!(profiler.current_usage, 3072);
        assert_eq!(profiler.peak_usage, 3072);
        assert_eq!(profiler.allocation_count, 2);
    }

    #[test]
    fn test_quantization_params_presets() {
        let int8_sym = QuantizationParams::int8_symmetric();
        assert_eq!(int8_sym.dtype, QuantizedDType::Int8);
        assert_eq!(int8_sym.scheme, QuantizationScheme::Symmetric);
        assert_eq!(int8_sym.zero_point[0], 0);

        let uint8_asym = QuantizationParams::uint8_asymmetric();
        assert_eq!(uint8_asym.dtype, QuantizedDType::UInt8);
        assert_eq!(uint8_asym.scheme, QuantizationScheme::Asymmetric);
        assert_eq!(uint8_asym.zero_point[0], 128);

        let int4_block = QuantizationParams::int4_blockwise();
        assert_eq!(int4_block.dtype, QuantizedDType::Int4);
        assert_eq!(int4_block.scheme, QuantizationScheme::BlockWise);
        assert_eq!(int4_block.block_size, Some(32));
    }

    #[test]
    fn test_bottleneck_severity_ordering() {
        assert!(BottleneckSeverity::Low < BottleneckSeverity::Medium);
        assert!(BottleneckSeverity::Medium < BottleneckSeverity::High);
        assert!(BottleneckSeverity::High < BottleneckSeverity::Critical);
    }

    #[test]
    fn test_complexity_level_ordering() {
        assert!(ComplexityLevel::Trivial < ComplexityLevel::Simple);
        assert!(ComplexityLevel::Simple < ComplexityLevel::Moderate);
        assert!(ComplexityLevel::Moderate < ComplexityLevel::Complex);
        assert!(ComplexityLevel::Complex < ComplexityLevel::Expert);
    }
}