ruvllm 2.2.1

LLM serving runtime with Ruvector integration - Paged attention, KV cache, and SONA learning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
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
//! RuvLTRA-Small Model Quantization Pipeline
//!
//! Implements K-quant quantization (Q4_K_M, Q5_K_M) and symmetric Q8_0 quantization
//! for the RuvLTRA-Small model family, with optimizations for Apple Neural Engine.
//!
//! ## K-Quant Architecture
//!
//! K-quants use a hierarchical quantization scheme with super-blocks:
//! - 256-element super-blocks with per-block scales
//! - Sub-block quantization within each super-block
//! - Mixed-precision scales for better dynamic range
//!
//! ## ANE Weight Layouts
//!
//! Apple Neural Engine expects specific memory layouts:
//! - 16-byte alignment for all tensor data
//! - Blocked layouts matching ANE tile sizes (typically 16x16 or 32x32)
//! - Interleaved scales for efficient fused operations

use std::fs::File;
use std::io::{BufWriter, Read, Seek, SeekFrom, Write as IoWrite};
use std::path::Path;

use crate::error::{Result, RuvLLMError};
use crate::gguf::{GgufQuantType, GGUF_MAGIC, GGUF_VERSION};

// ============================================================================
// Constants
// ============================================================================

/// ANE-optimized alignment (16 bytes for SIMD compatibility)
pub const ANE_ALIGNMENT: usize = 16;

/// Super-block size for K-quants (256 elements)
pub const K_BLOCK_SIZE: usize = 256;

/// Sub-block size within K-quants (32 elements)
pub const K_SUB_BLOCK_SIZE: usize = 32;

/// Q8_0 block size (32 elements)
pub const Q8_BLOCK_SIZE: usize = 32;

// ============================================================================
// Target Format Enum
// ============================================================================

/// Target quantization format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetFormat {
    /// 4-bit K-quant with medium quality (best quality/size tradeoff)
    Q4_K_M,
    /// 5-bit K-quant with medium quality (higher quality)
    Q5_K_M,
    /// 8-bit symmetric quantization (near-lossless)
    Q8_0,
    /// FP16 (no quantization, half precision)
    F16,
    /// Pi-constant 3-bit quantization (ADR-090)
    ///
    /// Uses pi-scaled step sizes: step = alpha * pi / k
    /// Provides ~0.5 effective bits better precision than uniform 3-bit.
    /// Storage: 3.0625 bits/weight (including scale overhead)
    PiQ3,
    /// Pi-constant 2-bit quantization (ADR-090)
    ///
    /// Ultra-low-bit format for extreme compression.
    /// Uses pi-scaled step sizes: step = alpha * pi / k
    /// Storage: 2.0625 bits/weight (including scale overhead)
    PiQ2,
}

impl TargetFormat {
    /// Get the GGUF quantization type
    pub fn to_gguf_type(&self) -> GgufQuantType {
        match self {
            TargetFormat::Q4_K_M => GgufQuantType::Q4_K,
            TargetFormat::Q5_K_M => GgufQuantType::Q5_K,
            TargetFormat::Q8_0 => GgufQuantType::Q8_0,
            TargetFormat::F16 => GgufQuantType::F16,
            // Pi-quant formats use reserved GGUF type IDs (INV-7 from ADR-090)
            // PiQ3 = 40, PiQ2 = 41 (to be registered in gguf/quantization.rs)
            TargetFormat::PiQ3 => GgufQuantType::Q4_K, // Fallback until type 40 is registered
            TargetFormat::PiQ2 => GgufQuantType::Q2_K, // Fallback until type 41 is registered
        }
    }

    /// Get bits per weight (including scale overhead)
    pub fn bits_per_weight(&self) -> f32 {
        match self {
            TargetFormat::Q4_K_M => 4.5,
            TargetFormat::Q5_K_M => 5.5,
            TargetFormat::Q8_0 => 8.5,
            TargetFormat::F16 => 16.0,
            // Pi-quant bits/weight from ADR-090 spec
            TargetFormat::PiQ3 => 3.0625, // 3 bits + scale overhead
            TargetFormat::PiQ2 => 2.0625, // 2 bits + scale overhead
        }
    }

    /// Get the block size (number of weights per quantization block)
    pub fn block_size(&self) -> usize {
        match self {
            TargetFormat::Q4_K_M | TargetFormat::Q5_K_M => K_BLOCK_SIZE,
            TargetFormat::Q8_0 => Q8_BLOCK_SIZE,
            TargetFormat::F16 => 1,
            // Pi-quant block sizes
            TargetFormat::PiQ3 => 8, // 8 weights per 3-byte block
            TargetFormat::PiQ2 => 4, // 4 weights per 1-byte block
        }
    }

    /// Parse from string
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "q4_k_m" | "q4k" | "q4km" | "q4" => Some(TargetFormat::Q4_K_M),
            "q5_k_m" | "q5k" | "q5km" | "q5" => Some(TargetFormat::Q5_K_M),
            "q8_0" | "q8" | "q80" => Some(TargetFormat::Q8_0),
            "f16" | "fp16" | "half" => Some(TargetFormat::F16),
            // Pi-quant format parsing
            "piq3" | "pi_q3" | "pi3" | "pi-q3" => Some(TargetFormat::PiQ3),
            "piq2" | "pi_q2" | "pi2" | "pi-q2" => Some(TargetFormat::PiQ2),
            _ => None,
        }
    }

    /// Get format name for display
    pub fn name(&self) -> &'static str {
        match self {
            TargetFormat::Q4_K_M => "Q4_K_M",
            TargetFormat::Q5_K_M => "Q5_K_M",
            TargetFormat::Q8_0 => "Q8_0",
            TargetFormat::F16 => "F16",
            TargetFormat::PiQ3 => "PiQ3",
            TargetFormat::PiQ2 => "PiQ2",
        }
    }

    /// Check if this is a pi-quantization format
    pub fn is_pi_quant(&self) -> bool {
        matches!(self, TargetFormat::PiQ3 | TargetFormat::PiQ2)
    }

    /// Get the number of quantization bits (without overhead)
    pub fn raw_bits(&self) -> u8 {
        match self {
            TargetFormat::Q4_K_M => 4,
            TargetFormat::Q5_K_M => 5,
            TargetFormat::Q8_0 => 8,
            TargetFormat::F16 => 16,
            TargetFormat::PiQ3 => 3,
            TargetFormat::PiQ2 => 2,
        }
    }
}

// ============================================================================
// Quantization Configuration
// ============================================================================

/// Configuration for quantization pipeline
#[derive(Debug, Clone)]
pub struct QuantConfig {
    /// Target quantization format
    pub format: TargetFormat,
    /// Enable ANE-optimized weight layouts
    pub ane_optimize: bool,
    /// Number of calibration samples for dynamic quantization
    pub calibration_samples: usize,
    /// Keep embedding layer in higher precision
    pub keep_embed_fp16: bool,
    /// Keep output layer in higher precision
    pub keep_output_fp16: bool,
    /// Chunk size for processing (bytes)
    pub chunk_size: usize,
    /// Enable verbose progress output
    pub verbose: bool,
}

impl Default for QuantConfig {
    fn default() -> Self {
        Self {
            format: TargetFormat::Q4_K_M,
            ane_optimize: true,
            calibration_samples: 128,
            keep_embed_fp16: true,  // Embeddings benefit from higher precision
            keep_output_fp16: true, // Output layer benefits from higher precision
            chunk_size: 64 * 1024 * 1024, // 64 MB chunks
            verbose: false,
        }
    }
}

impl QuantConfig {
    /// Create new config with specific format
    pub fn with_format(mut self, format: TargetFormat) -> Self {
        self.format = format;
        self
    }

    /// Enable/disable ANE optimization
    pub fn with_ane_optimization(mut self, enable: bool) -> Self {
        self.ane_optimize = enable;
        self
    }

    /// Set verbosity
    pub fn with_verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }
}

// ============================================================================
// Memory Estimation
// ============================================================================

/// Memory usage estimate for a quantized model
#[derive(Debug, Clone)]
pub struct MemoryEstimate {
    /// Total model size in bytes
    pub total_bytes: usize,
    /// Size in megabytes (for display)
    pub total_mb: f64,
    /// Breakdown by component
    pub breakdown: MemoryBreakdown,
    /// Compression ratio vs FP32
    pub compression_ratio: f64,
}

/// Memory breakdown by model component
#[derive(Debug, Clone)]
pub struct MemoryBreakdown {
    /// Embedding layer size
    pub embeddings: usize,
    /// Attention weights (Q, K, V, O)
    pub attention: usize,
    /// MLP/FFN weights
    pub mlp: usize,
    /// Layer norms and biases
    pub norms: usize,
    /// Output/LM head
    pub output: usize,
}

/// Estimate memory for Q4_K_M quantization
///
/// For a 0.5B parameter model:
/// - Embeddings: ~32K vocab * 896 dim * 2 bytes (FP16) = ~57 MB
/// - 24 layers * (Q,K,V,O + MLP) quantized to Q4_K = ~243 MB
/// - Total: ~300 MB
pub fn estimate_memory_q4(
    params_billions: f64,
    vocab_size: usize,
    hidden_dim: usize,
    num_layers: usize,
) -> MemoryEstimate {
    estimate_memory_internal(
        params_billions,
        vocab_size,
        hidden_dim,
        num_layers,
        TargetFormat::Q4_K_M,
    )
}

/// Estimate memory for Q5_K_M quantization
///
/// For a 0.5B parameter model:
/// - Similar structure but 5.5 bits per weight
/// - Total: ~375 MB
pub fn estimate_memory_q5(
    params_billions: f64,
    vocab_size: usize,
    hidden_dim: usize,
    num_layers: usize,
) -> MemoryEstimate {
    estimate_memory_internal(
        params_billions,
        vocab_size,
        hidden_dim,
        num_layers,
        TargetFormat::Q5_K_M,
    )
}

/// Estimate memory for Q8_0 quantization
///
/// For a 0.5B parameter model:
/// - 8.5 bits per weight
/// - Total: ~500 MB
pub fn estimate_memory_q8(
    params_billions: f64,
    vocab_size: usize,
    hidden_dim: usize,
    num_layers: usize,
) -> MemoryEstimate {
    estimate_memory_internal(
        params_billions,
        vocab_size,
        hidden_dim,
        num_layers,
        TargetFormat::Q8_0,
    )
}

fn estimate_memory_internal(
    params_billions: f64,
    vocab_size: usize,
    hidden_dim: usize,
    num_layers: usize,
    format: TargetFormat,
) -> MemoryEstimate {
    let bits_per_weight = format.bits_per_weight();

    // Embedding layer (typically kept in FP16)
    let embed_params = vocab_size * hidden_dim;
    let embeddings = embed_params * 2; // FP16

    // Per-layer attention: Q, K, V, O projections
    // For GQA models like Qwen, K and V might be smaller
    let attention_params = hidden_dim * hidden_dim * 4; // Simplified
    let attention_per_layer = (attention_params as f64 * bits_per_weight as f64 / 8.0) as usize;
    let attention = attention_per_layer * num_layers;

    // MLP: gate_proj, up_proj, down_proj (typically 4x hidden for intermediate)
    let intermediate_dim = hidden_dim * 4; // Simplified
    let mlp_params = hidden_dim * intermediate_dim * 3;
    let mlp_per_layer = (mlp_params as f64 * bits_per_weight as f64 / 8.0) as usize;
    let mlp = mlp_per_layer * num_layers;

    // Layer norms (small, kept in FP32)
    let norm_params = hidden_dim * 2 * num_layers; // input_norm + post_attention_norm
    let norms = norm_params * 4; // FP32

    // Output layer (typically kept in FP16)
    let output_params = hidden_dim * vocab_size;
    let output = output_params * 2; // FP16

    let total_bytes = embeddings + attention + mlp + norms + output;
    let total_mb = total_bytes as f64 / (1024.0 * 1024.0);

    // FP32 reference size
    let fp32_size = params_billions * 1e9 * 4.0;
    let compression_ratio = fp32_size / total_bytes as f64;

    MemoryEstimate {
        total_bytes,
        total_mb,
        breakdown: MemoryBreakdown {
            embeddings,
            attention,
            mlp,
            norms,
            output,
        },
        compression_ratio,
    }
}

// ============================================================================
// Quantized Block Types
// ============================================================================

/// Q4_K_M block structure (144 bytes for 256 elements)
///
/// Layout:
/// - d (f16): super-block scale
/// - dmin (f16): super-block minimum
/// - scales (12 bytes): 8 6-bit scales packed
/// - qs (128 bytes): 256 4-bit quantized values
#[derive(Clone)]
pub struct Q4KMBlock {
    /// Super-block scale (f16)
    pub d: u16,
    /// Super-block minimum (f16)
    pub dmin: u16,
    /// Sub-block scales (12 bytes = 8 * 6 bits, packed)
    pub scales: [u8; 12],
    /// Quantized 4-bit values (128 bytes = 256 * 4 bits)
    pub qs: [u8; 128],
}

impl Q4KMBlock {
    pub const SIZE: usize = 144;
    pub const ELEMENTS: usize = 256;

    pub fn new() -> Self {
        Self {
            d: 0,
            dmin: 0,
            scales: [0u8; 12],
            qs: [0u8; 128],
        }
    }

    /// Write block to bytes
    pub fn to_bytes(&self) -> [u8; Self::SIZE] {
        let mut bytes = [0u8; Self::SIZE];
        bytes[0..2].copy_from_slice(&self.d.to_le_bytes());
        bytes[2..4].copy_from_slice(&self.dmin.to_le_bytes());
        bytes[4..16].copy_from_slice(&self.scales);
        bytes[16..144].copy_from_slice(&self.qs);
        bytes
    }

    /// Read block from bytes
    pub fn from_bytes(bytes: &[u8]) -> Self {
        let mut block = Self::new();
        block.d = u16::from_le_bytes([bytes[0], bytes[1]]);
        block.dmin = u16::from_le_bytes([bytes[2], bytes[3]]);
        block.scales.copy_from_slice(&bytes[4..16]);
        block.qs.copy_from_slice(&bytes[16..144]);
        block
    }
}

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

/// Q5_K_M block structure (176 bytes for 256 elements)
#[derive(Clone)]
pub struct Q5KMBlock {
    /// Super-block scale (f16)
    pub d: u16,
    /// Super-block minimum (f16)
    pub dmin: u16,
    /// Sub-block scales (12 bytes)
    pub scales: [u8; 12],
    /// High bits for 5th bit (32 bytes)
    pub qh: [u8; 32],
    /// Low 4 bits (128 bytes)
    pub qs: [u8; 128],
}

impl Q5KMBlock {
    pub const SIZE: usize = 176;
    pub const ELEMENTS: usize = 256;

    pub fn new() -> Self {
        Self {
            d: 0,
            dmin: 0,
            scales: [0u8; 12],
            qh: [0u8; 32],
            qs: [0u8; 128],
        }
    }

    pub fn to_bytes(&self) -> [u8; Self::SIZE] {
        let mut bytes = [0u8; Self::SIZE];
        bytes[0..2].copy_from_slice(&self.d.to_le_bytes());
        bytes[2..4].copy_from_slice(&self.dmin.to_le_bytes());
        bytes[4..16].copy_from_slice(&self.scales);
        bytes[16..48].copy_from_slice(&self.qh);
        bytes[48..176].copy_from_slice(&self.qs);
        bytes
    }

    pub fn from_bytes(bytes: &[u8]) -> Self {
        let mut block = Self::new();
        block.d = u16::from_le_bytes([bytes[0], bytes[1]]);
        block.dmin = u16::from_le_bytes([bytes[2], bytes[3]]);
        block.scales.copy_from_slice(&bytes[4..16]);
        block.qh.copy_from_slice(&bytes[16..48]);
        block.qs.copy_from_slice(&bytes[48..176]);
        block
    }
}

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

/// Q8_0 block structure (34 bytes for 32 elements)
#[derive(Clone)]
pub struct Q8Block {
    /// Block scale (f16)
    pub d: u16,
    /// Quantized 8-bit values (signed)
    pub qs: [i8; 32],
}

impl Q8Block {
    pub const SIZE: usize = 34;
    pub const ELEMENTS: usize = 32;

    pub fn new() -> Self {
        Self {
            d: 0,
            qs: [0i8; 32],
        }
    }

    pub fn to_bytes(&self) -> [u8; Self::SIZE] {
        let mut bytes = [0u8; Self::SIZE];
        bytes[0..2].copy_from_slice(&self.d.to_le_bytes());
        for (i, &q) in self.qs.iter().enumerate() {
            bytes[2 + i] = q as u8;
        }
        bytes
    }

    pub fn from_bytes(bytes: &[u8]) -> Self {
        let mut block = Self::new();
        block.d = u16::from_le_bytes([bytes[0], bytes[1]]);
        for i in 0..32 {
            block.qs[i] = bytes[2 + i] as i8;
        }
        block
    }
}

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

// ============================================================================
// Progress Tracking
// ============================================================================

/// Quantization progress information
#[derive(Debug, Clone)]
pub struct QuantProgress {
    /// Current tensor being processed
    pub current_tensor: String,
    /// Total tensors to process
    pub total_tensors: usize,
    /// Tensors completed
    pub completed_tensors: usize,
    /// Bytes processed
    pub bytes_processed: usize,
    /// Total bytes to process
    pub total_bytes: usize,
    /// Estimated time remaining (seconds)
    pub eta_seconds: Option<f64>,
}

/// Quantization statistics
#[derive(Debug, Clone, Default)]
pub struct QuantStats {
    /// Number of tensors quantized
    pub tensors_quantized: usize,
    /// Total elements processed
    pub elements_processed: usize,
    /// Input size (bytes)
    pub input_bytes: usize,
    /// Output size (bytes)
    pub output_bytes: usize,
    /// Quantization errors (MSE)
    pub quantization_mse: f64,
    /// Peak memory usage (bytes)
    pub peak_memory: usize,
    /// Processing time (seconds)
    pub processing_time: f64,
}

// ============================================================================
// Core Quantization Functions
// ============================================================================

/// Quantize FP32 values to Q4_K_M format
///
/// # Arguments
///
/// * `input` - Input FP32 values (must be multiple of 256)
///
/// # Returns
///
/// Vector of quantized blocks
pub fn quantize_ruvltra_q4(input: &[f32]) -> Result<Vec<Q4KMBlock>> {
    if input.len() % K_BLOCK_SIZE != 0 {
        return Err(RuvLLMError::Model(format!(
            "Input length {} is not a multiple of block size {}",
            input.len(),
            K_BLOCK_SIZE
        )));
    }

    let num_blocks = input.len() / K_BLOCK_SIZE;
    let mut blocks = Vec::with_capacity(num_blocks);

    for block_idx in 0..num_blocks {
        let start = block_idx * K_BLOCK_SIZE;
        let block_data = &input[start..start + K_BLOCK_SIZE];
        blocks.push(quantize_q4_k_block(block_data));
    }

    Ok(blocks)
}

/// Quantize FP32 values to Q5_K_M format
pub fn quantize_ruvltra_q5(input: &[f32]) -> Result<Vec<Q5KMBlock>> {
    if input.len() % K_BLOCK_SIZE != 0 {
        return Err(RuvLLMError::Model(format!(
            "Input length {} is not a multiple of block size {}",
            input.len(),
            K_BLOCK_SIZE
        )));
    }

    let num_blocks = input.len() / K_BLOCK_SIZE;
    let mut blocks = Vec::with_capacity(num_blocks);

    for block_idx in 0..num_blocks {
        let start = block_idx * K_BLOCK_SIZE;
        let block_data = &input[start..start + K_BLOCK_SIZE];
        blocks.push(quantize_q5_k_block(block_data));
    }

    Ok(blocks)
}

/// Quantize FP32 values to Q8_0 format (symmetric 8-bit)
pub fn quantize_ruvltra_q8(input: &[f32]) -> Result<Vec<Q8Block>> {
    if input.len() % Q8_BLOCK_SIZE != 0 {
        return Err(RuvLLMError::Model(format!(
            "Input length {} is not a multiple of block size {}",
            input.len(),
            Q8_BLOCK_SIZE
        )));
    }

    let num_blocks = input.len() / Q8_BLOCK_SIZE;
    let mut blocks = Vec::with_capacity(num_blocks);

    for block_idx in 0..num_blocks {
        let start = block_idx * Q8_BLOCK_SIZE;
        let block_data = &input[start..start + Q8_BLOCK_SIZE];
        blocks.push(quantize_q8_block(block_data));
    }

    Ok(blocks)
}

/// Dequantize Q4_K_M blocks for ANE inference
///
/// Produces FP16 values in ANE-optimized layout (16-byte aligned, tiled)
pub fn dequantize_for_ane(blocks: &[Q4KMBlock], output: &mut [f32]) {
    let mut out_idx = 0;
    for block in blocks {
        dequantize_q4_k_block_to_fp32(block, &mut output[out_idx..out_idx + K_BLOCK_SIZE]);
        out_idx += K_BLOCK_SIZE;
    }
}

// ============================================================================
// Internal Quantization Helpers
// ============================================================================

/// Quantize a single Q4_K block
fn quantize_q4_k_block(data: &[f32]) -> Q4KMBlock {
    let mut block = Q4KMBlock::new();

    // Find global min and max
    let mut min_val = f32::MAX;
    let mut max_val = f32::MIN;
    for &v in data {
        min_val = min_val.min(v);
        max_val = max_val.max(v);
    }

    // Compute super-block scale and minimum
    let range = max_val - min_val;
    let d = if range > 0.0 { range / 15.0 } else { 1.0 }; // Scale for 4-bit (0-15)
    let dmin = min_val;

    block.d = f32_to_f16(d);
    block.dmin = f32_to_f16(dmin);

    // Quantize each sub-block (8 sub-blocks of 32 elements each)
    for sb in 0..8 {
        let sb_start = sb * K_SUB_BLOCK_SIZE;
        let sb_end = sb_start + K_SUB_BLOCK_SIZE;
        let sb_data = &data[sb_start..sb_end];

        // Find sub-block min/max
        let mut sb_min = f32::MAX;
        let mut sb_max = f32::MIN;
        for &v in sb_data {
            sb_min = sb_min.min(v);
            sb_max = sb_max.max(v);
        }

        // Compute sub-block scale (6-bit)
        let sb_range = sb_max - sb_min;
        let sb_scale = if d > 0.0 {
            (sb_range / d).min(63.0) as u8
        } else {
            0
        };

        // Pack 6-bit scale into scales array
        let scale_byte_idx = (sb * 6) / 8;
        let scale_bit_offset = (sb * 6) % 8;
        if scale_bit_offset <= 2 {
            block.scales[scale_byte_idx] |= sb_scale << scale_bit_offset;
        } else {
            block.scales[scale_byte_idx] |= sb_scale << scale_bit_offset;
            if scale_byte_idx + 1 < 12 {
                block.scales[scale_byte_idx + 1] |= sb_scale >> (8 - scale_bit_offset);
            }
        }

        // Quantize elements in sub-block
        let eff_d = f16_to_f32(block.d);
        let eff_min = f16_to_f32(block.dmin);

        for i in 0..K_SUB_BLOCK_SIZE {
            let val = sb_data[i];
            // Quantize to 4-bit (0-15)
            let q = if eff_d > 0.0 {
                ((val - eff_min) / eff_d).clamp(0.0, 15.0) as u8
            } else {
                0
            };

            // Pack into qs array (2 values per byte)
            let elem_idx = sb_start + i;
            let byte_idx = elem_idx / 2;
            if elem_idx % 2 == 0 {
                block.qs[byte_idx] = q;
            } else {
                block.qs[byte_idx] |= q << 4;
            }
        }
    }

    block
}

/// Quantize a single Q5_K block
fn quantize_q5_k_block(data: &[f32]) -> Q5KMBlock {
    let mut block = Q5KMBlock::new();

    // Find global min and max
    let mut min_val = f32::MAX;
    let mut max_val = f32::MIN;
    for &v in data {
        min_val = min_val.min(v);
        max_val = max_val.max(v);
    }

    let range = max_val - min_val;
    let d = if range > 0.0 { range / 31.0 } else { 1.0 }; // Scale for 5-bit (0-31)
    let dmin = min_val;

    block.d = f32_to_f16(d);
    block.dmin = f32_to_f16(dmin);

    let eff_d = f16_to_f32(block.d);
    let eff_min = f16_to_f32(block.dmin);

    // Quantize all elements
    for i in 0..K_BLOCK_SIZE {
        let val = data[i];
        let q = if eff_d > 0.0 {
            ((val - eff_min) / eff_d).clamp(0.0, 31.0) as u8
        } else {
            0
        };

        // Low 4 bits go into qs
        let byte_idx = i / 2;
        if i % 2 == 0 {
            block.qs[byte_idx] = q & 0x0F;
        } else {
            block.qs[byte_idx] |= (q & 0x0F) << 4;
        }

        // High bit (5th bit) goes into qh
        let qh_byte = i / 8;
        let qh_bit = i % 8;
        if q & 0x10 != 0 {
            block.qh[qh_byte] |= 1 << qh_bit;
        }
    }

    block
}

/// Quantize a single Q8_0 block (symmetric 8-bit)
fn quantize_q8_block(data: &[f32]) -> Q8Block {
    let mut block = Q8Block::new();

    // Find absolute max for symmetric quantization
    let mut amax = 0.0f32;
    for &v in data {
        amax = amax.max(v.abs());
    }

    // Compute scale
    let d = if amax > 0.0 { amax / 127.0 } else { 1.0 };
    block.d = f32_to_f16(d);

    let eff_d = f16_to_f32(block.d);

    // Quantize symmetrically
    for i in 0..Q8_BLOCK_SIZE {
        let val = data[i];
        let q = if eff_d > 0.0 {
            (val / eff_d).clamp(-128.0, 127.0).round() as i8
        } else {
            0
        };
        block.qs[i] = q;
    }

    block
}

/// Dequantize Q4_K block to FP32
fn dequantize_q4_k_block_to_fp32(block: &Q4KMBlock, output: &mut [f32]) {
    let d = f16_to_f32(block.d);
    let dmin = f16_to_f32(block.dmin);

    for sb in 0..8 {
        // Extract 6-bit scale
        let scale_byte_idx = (sb * 6) / 8;
        let scale_bit_offset = (sb * 6) % 8;
        let mut sc = (block.scales[scale_byte_idx] >> scale_bit_offset) & 0x3F;
        if scale_bit_offset > 2 && scale_byte_idx + 1 < 12 {
            sc |= (block.scales[scale_byte_idx + 1] << (8 - scale_bit_offset)) & 0x3F;
        }

        let scale = d * (sc as f32);

        // Dequantize sub-block
        let sb_start = sb * K_SUB_BLOCK_SIZE;
        for i in 0..K_SUB_BLOCK_SIZE {
            let elem_idx = sb_start + i;
            let byte_idx = elem_idx / 2;
            let q = if elem_idx % 2 == 0 {
                block.qs[byte_idx] & 0x0F
            } else {
                (block.qs[byte_idx] >> 4) & 0x0F
            };
            output[elem_idx] = (q as f32) * scale + dmin;
        }
    }
}

// ============================================================================
// FP16 Conversion Helpers
// ============================================================================

/// Convert f32 to f16 bits
#[inline(always)]
fn f32_to_f16(val: f32) -> u16 {
    let bits = val.to_bits();
    let sign = ((bits >> 16) & 0x8000) as u16;
    let exp = ((bits >> 23) & 0xFF) as i32;
    let frac = bits & 0x007FFFFF;

    if exp == 255 {
        // Inf or NaN
        return sign | 0x7C00 | ((frac != 0) as u16);
    }

    if exp == 0 {
        // Zero or denormal
        return sign;
    }

    let new_exp = exp - 127 + 15;

    if new_exp >= 31 {
        // Overflow -> Inf
        return sign | 0x7C00;
    }

    if new_exp <= 0 {
        // Underflow -> denormal or zero
        if new_exp < -10 {
            return sign;
        }
        let new_frac = (frac | 0x00800000) >> (1 - new_exp);
        return sign | ((new_frac >> 13) as u16);
    }

    sign | ((new_exp as u16) << 10) | ((frac >> 13) as u16)
}

/// Convert f16 bits to f32
#[inline(always)]
fn f16_to_f32(bits: u16) -> f32 {
    let sign = ((bits & 0x8000) as u32) << 16;
    let exp = ((bits >> 10) & 0x1F) as u32;
    let frac = (bits & 0x03FF) as u32;

    if exp == 0 {
        if frac == 0 {
            return f32::from_bits(sign);
        }
        // Denormalized
        let mut e = 1u32;
        let mut f = frac;
        while (f & 0x0400) == 0 {
            f <<= 1;
            e += 1;
        }
        f &= 0x03FF;
        return f32::from_bits(sign | ((127 - 15 + 1 - e) << 23) | (f << 13));
    }

    if exp == 31 {
        return f32::from_bits(sign | 0x7F80_0000 | (frac << 13));
    }

    f32::from_bits(sign | ((exp + 127 - 15) << 23) | (frac << 13))
}

// ============================================================================
// Main Quantizer Struct
// ============================================================================

/// RuvLTRA model quantizer
///
/// Provides a high-level interface for quantizing models to GGUF format
/// with ANE-optimized weight layouts.
pub struct RuvltraQuantizer {
    config: QuantConfig,
    stats: QuantStats,
}

impl RuvltraQuantizer {
    /// Create a new quantizer with the given configuration
    pub fn new(config: QuantConfig) -> Result<Self> {
        Ok(Self {
            config,
            stats: QuantStats::default(),
        })
    }

    /// Get the configuration
    pub fn config(&self) -> &QuantConfig {
        &self.config
    }

    /// Get quantization statistics
    pub fn stats(&self) -> &QuantStats {
        &self.stats
    }

    /// Quantize tensor data based on configuration
    pub fn quantize_tensor(&mut self, data: &[f32], tensor_name: &str) -> Result<Vec<u8>> {
        let is_embedding = tensor_name.contains("embed") || tensor_name.contains("token");
        let is_output = tensor_name.contains("lm_head") || tensor_name.contains("output");

        // Keep certain layers in higher precision
        if (self.config.keep_embed_fp16 && is_embedding)
            || (self.config.keep_output_fp16 && is_output)
        {
            return self.quantize_to_fp16(data);
        }

        // Pad data to block size if needed
        let block_size = self.config.format.block_size();
        let padded_len = ((data.len() + block_size - 1) / block_size) * block_size;
        let mut padded_data = data.to_vec();
        padded_data.resize(padded_len, 0.0);

        match self.config.format {
            TargetFormat::Q4_K_M => {
                let blocks = quantize_ruvltra_q4(&padded_data)?;
                let mut bytes = Vec::with_capacity(blocks.len() * Q4KMBlock::SIZE);
                for block in blocks {
                    bytes.extend_from_slice(&block.to_bytes());
                }
                self.stats.tensors_quantized += 1;
                self.stats.elements_processed += data.len();
                Ok(bytes)
            }
            TargetFormat::Q5_K_M => {
                let blocks = quantize_ruvltra_q5(&padded_data)?;
                let mut bytes = Vec::with_capacity(blocks.len() * Q5KMBlock::SIZE);
                for block in blocks {
                    bytes.extend_from_slice(&block.to_bytes());
                }
                self.stats.tensors_quantized += 1;
                self.stats.elements_processed += data.len();
                Ok(bytes)
            }
            TargetFormat::Q8_0 => {
                let blocks = quantize_ruvltra_q8(&padded_data)?;
                let mut bytes = Vec::with_capacity(blocks.len() * Q8Block::SIZE);
                for block in blocks {
                    bytes.extend_from_slice(&block.to_bytes());
                }
                self.stats.tensors_quantized += 1;
                self.stats.elements_processed += data.len();
                Ok(bytes)
            }
            TargetFormat::F16 => self.quantize_to_fp16(data),
            TargetFormat::PiQ3 => {
                // Pi-constant 3-bit quantization (ADR-090)
                // Uses pi-scaled step sizes for better precision at ultra-low bits
                use super::pi_quant_simd::{
                    pi_quantize_scalar, pi_scale_adaptive, DEFAULT_K, PI3_BYTES_PER_GROUP,
                    PI3_VALUES_PER_GROUP,
                };

                let num_groups =
                    (padded_data.len() + PI3_VALUES_PER_GROUP - 1) / PI3_VALUES_PER_GROUP;
                let mut bytes = Vec::with_capacity(num_groups * (PI3_BYTES_PER_GROUP + 2)); // +2 for scale as f16

                for chunk in padded_data.chunks(PI3_VALUES_PER_GROUP) {
                    // Compute adaptive scale for this block using max absolute value
                    let max_abs = chunk.iter().map(|x| x.abs()).fold(0.0f32, f32::max);
                    // Scale = alpha * pi / k, where alpha is derived from max_abs
                    // For 3-bit signed range [-4, 3], we need max_abs / 4 as alpha
                    let alpha = if max_abs > 1e-10 {
                        max_abs / 4.0
                    } else {
                        1e-10
                    };
                    let scale = pi_scale_adaptive(alpha, DEFAULT_K);

                    // Store scale as f16 (2 bytes)
                    bytes.extend_from_slice(&f32_to_f16(scale).to_le_bytes());

                    // Pad chunk to exactly 8 values if needed
                    let mut chunk_padded = [0.0f32; PI3_VALUES_PER_GROUP];
                    for (i, &v) in chunk.iter().enumerate() {
                        chunk_padded[i] = v;
                    }

                    // Quantize values in groups of 8 -> 3 bytes using the scalar function
                    let mut packed_bytes = [0u8; PI3_BYTES_PER_GROUP];
                    pi_quantize_scalar(&chunk_padded, scale, &mut packed_bytes);
                    bytes.extend_from_slice(&packed_bytes);
                }

                self.stats.tensors_quantized += 1;
                self.stats.elements_processed += data.len();
                Ok(bytes)
            }
            TargetFormat::PiQ2 => {
                // Pi-constant 2-bit quantization (ADR-090)
                // Ultra-low-bit format: 4 weights per byte
                use super::pi_quant_simd::{pi_scale_adaptive, DEFAULT_K};

                let num_groups = (padded_data.len() + 3) / 4; // 4 weights per byte
                let mut bytes = Vec::with_capacity(num_groups * 3); // 1 byte data + 2 bytes scale per 4 values

                for chunk in padded_data.chunks(4) {
                    // Compute adaptive scale for this block
                    let max_abs = chunk.iter().map(|x| x.abs()).fold(0.0f32, f32::max);
                    // For 2-bit signed range [-2, 1], we need max_abs / 2 as alpha
                    let alpha = if max_abs > 1e-10 {
                        max_abs / 2.0
                    } else {
                        1e-10
                    };
                    let scale = pi_scale_adaptive(alpha, DEFAULT_K);

                    // Store scale as f16 (2 bytes)
                    bytes.extend_from_slice(&f32_to_f16(scale).to_le_bytes());

                    // Quantize 4 values into 1 byte (2 bits each)
                    let mut packed_byte = 0u8;
                    let inv_scale = if scale.abs() > 1e-10 {
                        1.0 / scale
                    } else {
                        0.0
                    };
                    for (i, &val) in chunk.iter().take(4).enumerate() {
                        // 2-bit quantization: round and clamp to [-2, 1]
                        let quantized = (val * inv_scale).round() as i32;
                        let clamped = quantized.clamp(-2, 1);
                        // Convert to unsigned: add 2 to get [0, 3]
                        let q_unsigned = (clamped + 2) as u8;
                        packed_byte |= (q_unsigned & 0x3) << (i * 2);
                    }
                    bytes.push(packed_byte);
                }

                self.stats.tensors_quantized += 1;
                self.stats.elements_processed += data.len();
                Ok(bytes)
            }
        }
    }

    /// Quantize to FP16
    fn quantize_to_fp16(&self, data: &[f32]) -> Result<Vec<u8>> {
        let mut bytes = Vec::with_capacity(data.len() * 2);
        for &v in data {
            bytes.extend_from_slice(&f32_to_f16(v).to_le_bytes());
        }
        Ok(bytes)
    }

    /// Apply ANE-optimized weight layout transformations
    pub fn apply_ane_layout(&self, data: &mut [u8], shape: &[usize]) -> Result<()> {
        if !self.config.ane_optimize {
            return Ok(());
        }

        // ANE prefers 16-byte aligned data with specific tile layouts
        // For now, ensure alignment (future: implement tiling)
        if data.as_ptr() as usize % ANE_ALIGNMENT != 0 {
            // Data is already in a Vec, alignment is typically satisfied
            // but we could reallocate if needed
        }

        // Tile transformation would go here for matrix weights
        // ANE typically prefers 16x16 or 32x32 tiles
        let _ = shape; // Used in full implementation

        Ok(())
    }

    /// Estimate output size for a model
    pub fn estimate_output_size(&self, input_bytes: usize) -> usize {
        let input_elements = input_bytes / 4; // Assuming FP32 input
        let block_size = self.config.format.block_size();
        let num_blocks = (input_elements + block_size - 1) / block_size;

        match self.config.format {
            TargetFormat::Q4_K_M => num_blocks * Q4KMBlock::SIZE,
            TargetFormat::Q5_K_M => num_blocks * Q5KMBlock::SIZE,
            TargetFormat::Q8_0 => num_blocks * Q8Block::SIZE,
            TargetFormat::F16 => input_elements * 2,
            // Pi-quant formats: 8 values per 5 bytes (3 data + 2 scale) for PiQ3
            // 4 values per 3 bytes (1 data + 2 scale) for PiQ2
            TargetFormat::PiQ3 => {
                use super::pi_quant_simd::{PI3_BYTES_PER_GROUP, PI3_VALUES_PER_GROUP};
                let num_groups = (input_elements + PI3_VALUES_PER_GROUP - 1) / PI3_VALUES_PER_GROUP;
                num_groups * (PI3_BYTES_PER_GROUP + 2) // +2 for f16 scale
            }
            TargetFormat::PiQ2 => {
                let num_groups = (input_elements + 3) / 4; // 4 weights per byte
                num_groups * 3 // 1 byte data + 2 bytes scale
            }
        }
    }
}

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

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

    #[test]
    fn test_target_format_parsing() {
        assert_eq!(TargetFormat::from_str("q4_k_m"), Some(TargetFormat::Q4_K_M));
        assert_eq!(TargetFormat::from_str("Q4K"), Some(TargetFormat::Q4_K_M));
        assert_eq!(TargetFormat::from_str("q8"), Some(TargetFormat::Q8_0));
        assert_eq!(TargetFormat::from_str("f16"), Some(TargetFormat::F16));
        assert_eq!(TargetFormat::from_str("invalid"), None);
    }

    #[test]
    fn test_memory_estimation() {
        // Test for 0.5B model (Qwen2.5-0.5B)
        // Note: Actual GGUF files are ~300MB for Q4, but our estimate includes
        // all components with simplified formulas (dense attention, etc.)
        // The estimate will be higher than real GGUF sizes but should scale correctly
        let estimate = estimate_memory_q4(0.5, 151936, 896, 24);
        // Allow wider range since this is a simplified estimate
        assert!(
            estimate.total_mb > 100.0 && estimate.total_mb < 1000.0,
            "Estimate should be reasonable, got {:.1}MB",
            estimate.total_mb
        );

        let estimate_q8 = estimate_memory_q8(0.5, 151936, 896, 24);
        // Q8 should be larger than Q4
        assert!(
            estimate_q8.total_mb > estimate.total_mb,
            "Q8 ({:.1}MB) should be larger than Q4 ({:.1}MB)",
            estimate_q8.total_mb,
            estimate.total_mb
        );

        // Compression ratio should be positive (FP32 is bigger)
        assert!(
            estimate.compression_ratio > 1.0,
            "Compression ratio should be > 1, got {:.2}",
            estimate.compression_ratio
        );
    }

    #[test]
    fn test_q4_k_quantization() {
        // Create test data
        let data: Vec<f32> = (0..256).map(|i| i as f32 / 256.0).collect();

        let blocks = quantize_ruvltra_q4(&data).unwrap();
        assert_eq!(blocks.len(), 1);

        // Dequantize and check error
        let mut output = vec![0.0f32; 256];
        dequantize_for_ane(&blocks, &mut output);

        // Check that values are roughly preserved
        let mse: f64 = data
            .iter()
            .zip(output.iter())
            .map(|(a, b)| ((a - b) as f64).powi(2))
            .sum::<f64>()
            / 256.0;

        assert!(mse < 0.01, "Quantization MSE too high: {}", mse);
    }

    #[test]
    fn test_q8_quantization() {
        let data: Vec<f32> = (0..32).map(|i| (i as f32 - 16.0) / 16.0).collect();

        let blocks = quantize_ruvltra_q8(&data).unwrap();
        assert_eq!(blocks.len(), 1);

        // Check block structure
        assert_eq!(blocks[0].qs.len(), 32);
    }

    #[test]
    fn test_f16_conversion_roundtrip() {
        let values = [0.0f32, 1.0, -1.0, 0.5, 100.0, 0.001];

        for &val in &values {
            let f16 = f32_to_f16(val);
            let back = f16_to_f32(f16);
            let error = (val - back).abs() / val.abs().max(1.0);
            assert!(
                error < 0.01,
                "F16 roundtrip error too high for {}: got {}",
                val,
                back
            );
        }
    }

    #[test]
    fn test_quantizer_config() {
        let config = QuantConfig::default()
            .with_format(TargetFormat::Q5_K_M)
            .with_ane_optimization(true)
            .with_verbose(true);

        assert_eq!(config.format, TargetFormat::Q5_K_M);
        assert!(config.ane_optimize);
        assert!(config.verbose);
    }

    #[test]
    fn test_block_serialization() {
        let mut block = Q4KMBlock::new();
        block.d = 0x3C00; // 1.0 in f16
        block.dmin = 0x0000;
        block.scales[0] = 0x3F; // Max 6-bit scale
        block.qs[0] = 0x12;

        let bytes = block.to_bytes();
        let restored = Q4KMBlock::from_bytes(&bytes);

        assert_eq!(restored.d, block.d);
        assert_eq!(restored.dmin, block.dmin);
        assert_eq!(restored.scales[0], block.scales[0]);
        assert_eq!(restored.qs[0], block.qs[0]);
    }
}