haagenti 0.1.0

Next-generation compression library with LZ4, Zstd, Brotli, and Deflate support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
//! Create a compressed model for inference testing
//!
//! This compresses all weights using Spectral + INT4, then decompresses
//! and saves to a new safetensors file for comparison testing.
//!
//! Usage:
//! ```bash
//! RETENTION=0.30 cargo run --example create_compressed_model --features='lz4,zstd' --release
//! ```
//!
//! Environment variables:
//! - RETENTION: Base retention ratio (0.0-1.0), default 0.30
//! - TARGET_QUALITY: Target energy retention for adaptive mode (0.0-1.0), default 0.90
//! - NO_INT4: Disable INT4 quantization
//! - HYBRID: Use position-based hybrid compression mode
//! - LAYERTYPE: Use layer-type-based compression mode
//! - ADAPTIVE: Use per-tensor adaptive retention based on spectral analysis
//! - MIXED_PRECISION: Use FP16 essentials + INT4 details encoding
//! - FP16_RATIO: Fraction of retained coefficients to store as FP16 (0.0-1.0), default 0.20
//! - IMPORTANCE: Use importance-guided compression (heuristic or file-based)
//! - IMPORTANCE_FILE: Path to JSON importance map file (optional, uses heuristics if not set)
//! - MODEL: Specify model name (default: Qwen/Qwen2.5-0.5B-Instruct)
//! - QUIET: Disable progress bar (for CI/scripting)

use std::collections::HashMap;
use std::fmt;
use std::fs::{self, File};
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use std::time::Instant;

use haagenti::adaptive::AdaptiveSpectralEncoder;
use haagenti::compressive::{CompressiveSpectralDecoder, CompressiveSpectralEncoder};
use haagenti::importance::{ImportanceGuidedDecoder, ImportanceGuidedEncoder, ImportanceMap};
use haagenti::mixed_precision::{MixedPrecisionDecoder, MixedPrecisionEncoder};
use indicatif::{ProgressBar, ProgressStyle};

/// Error types for model compression
#[derive(Debug)]
enum CompressionError {
    ModelNotFound(String),
    FileReadError(String, std::io::Error),
    FileWriteError(String, std::io::Error),
    HeaderParseError(String),
    EncodingError(String),
    InvalidDtype(String),
}

impl fmt::Display for CompressionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CompressionError::ModelNotFound(model) => {
                write!(
                    f,
                    "Model '{}' not found in HuggingFace cache.\n\
                       Download with: huggingface-cli download {}",
                    model, model
                )
            }
            CompressionError::FileReadError(path, err) => {
                write!(f, "Failed to read '{}': {}", path, err)
            }
            CompressionError::FileWriteError(path, err) => {
                write!(f, "Failed to write '{}': {}", path, err)
            }
            CompressionError::HeaderParseError(msg) => {
                write!(f, "Failed to parse safetensors header: {}", msg)
            }
            CompressionError::EncodingError(msg) => {
                write!(f, "Encoding error: {}", msg)
            }
            CompressionError::InvalidDtype(dtype) => {
                write!(
                    f,
                    "Unsupported dtype '{}'. Supported: F32, F16, BF16",
                    dtype
                )
            }
        }
    }
}

impl std::error::Error for CompressionError {}

const Q4_BLOCK_SIZE: usize = 32;
const MAX_ELEMENTS: usize = 10_000_000;

/// Compression mode
#[derive(Debug, Clone, Copy, PartialEq)]
enum CompressionMode {
    Uniform,        // Same retention for all layers
    Hybrid,         // Position-based (first/last layers protected)
    LayerType,      // Type-based (FFN aggressive, attention careful, norms full)
    Adaptive,       // Per-tensor adaptive retention based on spectral analysis
    MixedPrecision, // FP16 essentials + INT4 details
    Importance,     // Learned/heuristic importance-guided compression
}

/// Determine retention ratio based on tensor name and compression mode
///
/// Layer-type mode uses insights from haagenti-importance QualityPredictor:
/// - FFN/MLP: Can tolerate 50% quality (largest portion of model)
/// - Attention Q/K: Medium sensitivity (70%)
/// - Attention V/O: Higher sensitivity (80%)
/// - LayerNorm/Bias: Critical (100%)
/// - Embeddings: Critical (100%)
fn get_layer_retention(name: &str, base_retention: f32, mode: CompressionMode) -> f32 {
    match mode {
        CompressionMode::Uniform => base_retention,

        CompressionMode::Hybrid => {
            // Original position-based hybrid mode
            if name.contains("layernorm") || name.contains("norm.weight") || name.contains("bias") {
                return 1.0;
            }
            if name.contains("layers.") {
                let parts: Vec<&str> = name.split('.').collect();
                if let Some(idx_str) = parts.get(2) {
                    if let Ok(layer_idx) = idx_str.parse::<usize>() {
                        if layer_idx == 0 || layer_idx >= 23 {
                            return 1.0;
                        }
                        if layer_idx <= 2 || layer_idx >= 21 {
                            return 0.80;
                        }
                        return base_retention;
                    }
                }
            }
            base_retention
        }

        CompressionMode::LayerType => {
            // === Critical layers: 100% retention ===
            // LayerNorm - critical for training stability
            if name.contains("layernorm")
                || name.contains("layer_norm")
                || name.contains("ln_")
                || name.contains("_ln")
                || name.contains("norm.weight")
                || name.contains("input_layernorm")
                || name.contains("post_attention_layernorm")
                || name.contains("final_layernorm")
            {
                return 1.0;
            }

            // Bias vectors - small but critical for output distribution
            if name.contains(".bias") || name.ends_with("_bias") {
                return 1.0;
            }

            // Embeddings - need full vocabulary precision
            if name.contains("embed_tokens")
                || name.contains("wte")
                || name.contains("word_embed")
                || name.contains("token_embed")
            {
                return 1.0;
            }

            // Output head (lm_head) - critical for token prediction
            if name.contains("lm_head") || name.contains("output.weight") {
                return 0.90; // Slightly less critical than embeddings
            }

            // === FFN/MLP: Aggressive compression (50-60% of base) ===
            // These are ~67% of model parameters and tolerate compression well
            if name.contains("mlp.")
                || name.contains("feed_forward")
                || name.contains("ffn")
                || name.contains(".fc1")
                || name.contains(".fc2")
                || name.contains("up_proj")
                || name.contains("down_proj")
                || name.contains("gate_proj")
                || name.contains("w1.")
                || name.contains("w2.")
                || name.contains("w3.")
            {
                // FFN can tolerate 50% quality per haagenti-importance
                return (base_retention * 0.70).max(0.40);
            }

            // === Attention Q/K: Medium compression ===
            if name.contains("q_proj")
                || name.contains("k_proj")
                || name.contains(".wq.")
                || name.contains(".wk.")
                || name.contains("query")
                || name.contains("key")
            {
                return base_retention; // Use base retention
            }

            // === Attention V/O: Careful compression ===
            if name.contains("v_proj")
                || name.contains("o_proj")
                || name.contains(".wv.")
                || name.contains(".wo.")
                || name.contains("value")
                || name.contains("dense")
            {
                return (base_retention * 1.1).min(1.0); // 10% more retention
            }

            // === Attention combined (qkv_proj) ===
            if name.contains("qkv") || name.contains("c_attn") {
                return base_retention;
            }

            // Default: base retention
            base_retention
        }

        CompressionMode::Adaptive => {
            // Adaptive mode is handled separately with AdaptiveSpectralEncoder
            // This branch should not be reached in normal operation
            base_retention
        }

        CompressionMode::MixedPrecision => {
            // Mixed precision mode uses base retention
            // Layer-type adjustments could be added here if needed
            base_retention
        }

        CompressionMode::Importance => {
            // Importance mode is handled separately with ImportanceGuidedEncoder
            // This branch should not be reached in normal operation
            base_retention
        }
    }
}

#[derive(Debug, Clone)]
struct TensorInfo {
    dtype: String,
    shape: Vec<usize>,
    data_offsets: (usize, usize),
}

fn parse_safetensors_header(data: &[u8]) -> Result<(usize, HashMap<String, TensorInfo>), String> {
    if data.len() < 8 {
        return Err("File too small".to_string());
    }

    let header_len = u64::from_le_bytes([
        data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
    ]) as usize;

    if data.len() < 8 + header_len {
        return Err("Invalid header length".to_string());
    }

    let header_json = std::str::from_utf8(&data[8..8 + header_len])
        .map_err(|e| format!("Invalid UTF-8: {}", e))?;

    let header: serde_json::Value =
        serde_json::from_str(header_json).map_err(|e| format!("Invalid JSON: {}", e))?;

    let mut tensors = HashMap::new();

    if let serde_json::Value::Object(obj) = header {
        for (name, value) in obj {
            if name == "__metadata__" {
                continue;
            }

            if let serde_json::Value::Object(tensor_obj) = value {
                let dtype = tensor_obj
                    .get("dtype")
                    .and_then(|v| v.as_str())
                    .unwrap_or("F32")
                    .to_string();

                let shape: Vec<usize> = tensor_obj
                    .get("shape")
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_u64().map(|n| n as usize))
                            .collect()
                    })
                    .unwrap_or_default();

                let offsets = tensor_obj.get("data_offsets").and_then(|v| v.as_array());
                if let Some(offs) = offsets {
                    let start = offs[0].as_u64().unwrap_or(0) as usize;
                    let end = offs[1].as_u64().unwrap_or(0) as usize;
                    tensors.insert(
                        name,
                        TensorInfo {
                            dtype,
                            shape,
                            data_offsets: (start, end),
                        },
                    );
                }
            }
        }
    }

    Ok((8 + header_len, tensors))
}

fn bytes_to_f32(data: &[u8], dtype: &str) -> Result<Vec<f32>, CompressionError> {
    match dtype {
        "F32" => Ok(data
            .chunks_exact(4)
            .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
            .collect()),
        "F16" => Ok(data
            .chunks_exact(2)
            .map(|c| {
                let bits = u16::from_le_bytes([c[0], c[1]]);
                half::f16::from_bits(bits).to_f32()
            })
            .collect()),
        "BF16" => Ok(data
            .chunks_exact(2)
            .map(|c| {
                let bits = u16::from_le_bytes([c[0], c[1]]);
                f32::from_bits((bits as u32) << 16)
            })
            .collect()),
        _ => Err(CompressionError::InvalidDtype(dtype.to_string())),
    }
}

fn f32_to_f16_bytes(values: &[f32]) -> Vec<u8> {
    let mut bytes = Vec::with_capacity(values.len() * 2);
    for &v in values {
        bytes.extend_from_slice(&half::f16::from_f32(v).to_le_bytes());
    }
    bytes
}

fn quantize_int4(weights: &[f32]) -> Vec<u8> {
    let num_blocks = weights.len().div_ceil(Q4_BLOCK_SIZE);
    let mut output = Vec::with_capacity(num_blocks * 2 + weights.len().div_ceil(2));

    let mut scales = Vec::with_capacity(num_blocks);
    for block in weights.chunks(Q4_BLOCK_SIZE) {
        let max_abs = block.iter().map(|v| v.abs()).fold(0.0f32, f32::max);
        let scale = if max_abs > 1e-10 { max_abs / 7.0 } else { 1.0 };
        scales.push(scale);
        output.extend_from_slice(&half::f16::from_f32(scale).to_le_bytes());
    }

    let mut nibble_buffer = Vec::new();
    for (block_idx, block) in weights.chunks(Q4_BLOCK_SIZE).enumerate() {
        let scale = scales[block_idx];
        for &val in block {
            let q = ((val / scale).round() as i8).clamp(-8, 7);
            nibble_buffer.push((q + 8) as u8);
        }
    }

    for pair in nibble_buffer.chunks(2) {
        let byte = if pair.len() == 2 {
            (pair[0] & 0x0F) | ((pair[1] & 0x0F) << 4)
        } else {
            pair[0] & 0x0F
        };
        output.push(byte);
    }

    output
}

fn dequantize_int4(data: &[u8], num_elements: usize) -> Vec<f32> {
    let num_blocks = num_elements.div_ceil(Q4_BLOCK_SIZE);
    let scales_bytes = num_blocks * 2;

    if data.len() < scales_bytes {
        return vec![];
    }

    let scales: Vec<f32> = data[..scales_bytes]
        .chunks_exact(2)
        .map(|c| half::f16::from_le_bytes([c[0], c[1]]).to_f32())
        .collect();

    let packed_data = &data[scales_bytes..];
    let mut output = Vec::with_capacity(num_elements);

    let mut nibble_idx = 0;
    #[allow(clippy::needless_range_loop)]
    for block_idx in 0..num_blocks {
        let scale = scales[block_idx];
        let block_size = Q4_BLOCK_SIZE.min(num_elements - block_idx * Q4_BLOCK_SIZE);

        for _ in 0..block_size {
            let byte_idx = nibble_idx / 2;
            let is_high = nibble_idx % 2 == 1;

            if byte_idx >= packed_data.len() {
                break;
            }

            let nibble = if is_high {
                (packed_data[byte_idx] >> 4) & 0x0F
            } else {
                packed_data[byte_idx] & 0x0F
            };

            let q = (nibble as i8) - 8;
            output.push(q as f32 * scale);
            nibble_idx += 1;
        }
    }

    output
}

fn compress_decompress(
    values: &[f32],
    width: usize,
    height: usize,
    encoder: &CompressiveSpectralEncoder,
    use_int4: bool,
) -> Result<Vec<f32>, String> {
    // For 1D tensors (bias vectors), preserve the mean explicitly
    // because DCT can lose DC component precision
    let is_1d = height == 1;
    let mean = if is_1d {
        values.iter().sum::<f32>() / values.len() as f32
    } else {
        0.0
    };

    // Center the data for 1D tensors
    let centered: Vec<f32> = if is_1d {
        values.iter().map(|v| v - mean).collect()
    } else {
        values.to_vec()
    };

    let mut fragments = encoder
        .encode_2d(&centered, width, height)
        .map_err(|e| format!("Encode failed: {:?}", e))?;

    if use_int4 {
        // Extract coefficients
        let frag0 = &fragments[0].data;
        let total_coeffs = u32::from_le_bytes([frag0[0], frag0[1], frag0[2], frag0[3]]) as usize;
        let essential_count = u32::from_le_bytes([frag0[4], frag0[5], frag0[6], frag0[7]]) as usize;

        let mut all_coeffs: Vec<f32> = Vec::with_capacity(total_coeffs);
        let essential_start = 20;

        for i in 0..essential_count {
            let offset = essential_start + i * 4;
            if offset + 4 <= frag0.len() {
                all_coeffs.push(f32::from_le_bytes([
                    frag0[offset],
                    frag0[offset + 1],
                    frag0[offset + 2],
                    frag0[offset + 3],
                ]));
            }
        }

        for frag in &fragments[1..] {
            let data = &frag.data;
            if data.len() < 8 {
                continue;
            }
            let coeff_count = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as usize;
            for i in 0..coeff_count {
                let offset = 8 + i * 4;
                if offset + 4 <= data.len() {
                    all_coeffs.push(f32::from_le_bytes([
                        data[offset],
                        data[offset + 1],
                        data[offset + 2],
                        data[offset + 3],
                    ]));
                }
            }
        }

        // INT4 round-trip
        let quantized = quantize_int4(&all_coeffs);
        let dequantized = dequantize_int4(&quantized, all_coeffs.len());

        // Put back
        let mut coeff_idx = 0;
        {
            let frag0 = &mut fragments[0].data;
            for i in 0..essential_count {
                if coeff_idx < dequantized.len() {
                    let offset = essential_start + i * 4;
                    let bytes = dequantized[coeff_idx].to_le_bytes();
                    if offset + 4 <= frag0.len() {
                        frag0[offset..offset + 4].copy_from_slice(&bytes);
                    }
                    coeff_idx += 1;
                }
            }
        }

        for frag in &mut fragments[1..] {
            let data = &mut frag.data;
            if data.len() < 8 {
                continue;
            }
            let coeff_count = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as usize;
            for i in 0..coeff_count {
                if coeff_idx < dequantized.len() {
                    let offset = 8 + i * 4;
                    let bytes = dequantized[coeff_idx].to_le_bytes();
                    if offset + 4 <= data.len() {
                        data[offset..offset + 4].copy_from_slice(&bytes);
                    }
                    coeff_idx += 1;
                }
            }
        }
    }

    // Decode
    let mut decoder = CompressiveSpectralDecoder::new();
    decoder
        .add_essentials(&fragments[0])
        .map_err(|e| format!("{:?}", e))?;
    for frag in &fragments[1..] {
        decoder.add_detail(frag).map_err(|e| format!("{:?}", e))?;
    }
    let mut reconstructed = decoder.reconstruct().map_err(|e| format!("{:?}", e))?;

    // Add mean back for 1D tensors
    if is_1d {
        for v in &mut reconstructed {
            *v += mean;
        }
    }

    Ok(reconstructed)
}

/// Compress and decompress using adaptive per-tensor retention.
///
/// Returns (reconstructed_values, retention_used).
fn compress_decompress_adaptive(
    values: &[f32],
    width: usize,
    height: usize,
    target_quality: f32,
    use_int4: bool,
) -> Result<(Vec<f32>, f32), String> {
    // For 1D tensors (bias vectors), preserve the mean explicitly
    let is_1d = height == 1;
    let mean = if is_1d {
        values.iter().sum::<f32>() / values.len() as f32
    } else {
        0.0
    };

    // Center the data for 1D tensors
    let centered: Vec<f32> = if is_1d {
        values.iter().map(|v| v - mean).collect()
    } else {
        values.to_vec()
    };

    // Use adaptive encoder
    let encoder = AdaptiveSpectralEncoder::new(target_quality, 8);
    let (meta, mut fragments) = encoder
        .encode_2d(&centered, width, height)
        .map_err(|e| format!("Adaptive encode failed: {:?}", e))?;

    let retention_used = meta.retention_ratio;

    if use_int4 {
        // Extract and quantize coefficients (same as non-adaptive)
        let frag0 = &fragments[0].data;
        let total_coeffs = u32::from_le_bytes([frag0[0], frag0[1], frag0[2], frag0[3]]) as usize;
        let essential_count = u32::from_le_bytes([frag0[4], frag0[5], frag0[6], frag0[7]]) as usize;

        let mut all_coeffs: Vec<f32> = Vec::with_capacity(total_coeffs);
        let essential_start = 20;

        for i in 0..essential_count {
            let offset = essential_start + i * 4;
            if offset + 4 <= frag0.len() {
                all_coeffs.push(f32::from_le_bytes([
                    frag0[offset],
                    frag0[offset + 1],
                    frag0[offset + 2],
                    frag0[offset + 3],
                ]));
            }
        }

        for frag in &fragments[1..] {
            let data = &frag.data;
            if data.len() < 8 {
                continue;
            }
            let coeff_count = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as usize;
            for i in 0..coeff_count {
                let offset = 8 + i * 4;
                if offset + 4 <= data.len() {
                    all_coeffs.push(f32::from_le_bytes([
                        data[offset],
                        data[offset + 1],
                        data[offset + 2],
                        data[offset + 3],
                    ]));
                }
            }
        }

        // INT4 round-trip
        let quantized = quantize_int4(&all_coeffs);
        let dequantized = dequantize_int4(&quantized, all_coeffs.len());

        // Put back
        let mut coeff_idx = 0;
        {
            let frag0 = &mut fragments[0].data;
            for i in 0..essential_count {
                if coeff_idx < dequantized.len() {
                    let offset = essential_start + i * 4;
                    let bytes = dequantized[coeff_idx].to_le_bytes();
                    if offset + 4 <= frag0.len() {
                        frag0[offset..offset + 4].copy_from_slice(&bytes);
                    }
                    coeff_idx += 1;
                }
            }
        }

        for frag in &mut fragments[1..] {
            let data = &mut frag.data;
            if data.len() < 8 {
                continue;
            }
            let coeff_count = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as usize;
            for i in 0..coeff_count {
                if coeff_idx < dequantized.len() {
                    let offset = 8 + i * 4;
                    let bytes = dequantized[coeff_idx].to_le_bytes();
                    if offset + 4 <= data.len() {
                        data[offset..offset + 4].copy_from_slice(&bytes);
                    }
                    coeff_idx += 1;
                }
            }
        }
    }

    // Decode
    let mut decoder = CompressiveSpectralDecoder::new();
    decoder
        .add_essentials(&fragments[0])
        .map_err(|e| format!("{:?}", e))?;
    for frag in &fragments[1..] {
        decoder.add_detail(frag).map_err(|e| format!("{:?}", e))?;
    }
    let mut reconstructed = decoder.reconstruct().map_err(|e| format!("{:?}", e))?;

    // Add mean back for 1D tensors
    if is_1d {
        for v in &mut reconstructed {
            *v += mean;
        }
    }

    Ok((reconstructed, retention_used))
}

/// Compress and decompress using mixed precision (FP16 essentials + INT4 details).
///
/// Uses a simpler DCT-based approach suitable for moderate tensor sizes.
fn compress_decompress_mixed_precision(
    values: &[f32],
    width: usize,
    height: usize,
    retention: f32,
    fp16_ratio: f32,
) -> Result<Vec<f32>, String> {
    // For 1D tensors (bias vectors), preserve the mean explicitly
    let is_1d = height == 1;
    let mean = if is_1d {
        values.iter().sum::<f32>() / values.len() as f32
    } else {
        0.0
    };

    // Center the data for 1D tensors
    let centered: Vec<f32> = if is_1d {
        values.iter().map(|v| v - mean).collect()
    } else {
        values.to_vec()
    };

    // Use mixed precision encoder
    let encoder = MixedPrecisionEncoder::new(retention, fp16_ratio);
    let decoder = MixedPrecisionDecoder::new();

    let compressed = encoder
        .encode(&centered, width, height)
        .map_err(|e| format!("MixedPrecision encode failed: {:?}", e))?;

    let mut reconstructed = decoder
        .decode(&compressed)
        .map_err(|e| format!("MixedPrecision decode failed: {:?}", e))?;

    // Add mean back for 1D tensors
    if is_1d {
        for v in &mut reconstructed {
            *v += mean;
        }
    }

    Ok(reconstructed)
}

/// Compress and decompress using importance-guided compression.
///
/// Uses heuristic layer-type importance or loaded importance map.
/// Returns (reconstructed_values, retention_used).
fn compress_decompress_importance(
    values: &[f32],
    width: usize,
    height: usize,
    tensor_name: &str,
    encoder: &ImportanceGuidedEncoder,
    use_int4: bool,
) -> Result<(Vec<f32>, f32), String> {
    // For 1D tensors (bias vectors), preserve the mean explicitly
    let is_1d = height == 1;
    let mean = if is_1d {
        values.iter().sum::<f32>() / values.len() as f32
    } else {
        0.0
    };

    // Center the data for 1D tensors
    let centered: Vec<f32> = if is_1d {
        values.iter().map(|v| v - mean).collect()
    } else {
        values.to_vec()
    };

    // Get tensor-specific retention from importance encoder
    let tensor_retention = encoder.effective_retention(tensor_name);

    // Encode using importance-guided encoder
    let compressed = encoder
        .encode(&centered, width, height, tensor_name)
        .map_err(|e| format!("Importance encode failed: {:?}", e))?;

    // Decode
    let decoder = ImportanceGuidedDecoder::new();
    let mut reconstructed = decoder
        .decode(&compressed)
        .map_err(|e| format!("Importance decode failed: {:?}", e))?;

    // If INT4 is enabled, apply additional quantization to the output
    // (importance-guided encoding uses its own quantization, but we can apply INT4 on top)
    if use_int4 {
        // Apply INT4 round-trip to reconstructed values
        let quantized = quantize_int4(&reconstructed);
        reconstructed = dequantize_int4(&quantized, reconstructed.len());
    }

    // Add mean back for 1D tensors
    if is_1d {
        for v in &mut reconstructed {
            *v += mean;
        }
    }

    Ok((reconstructed, tensor_retention))
}

fn find_model_in_cache(model_name: &str) -> Option<Vec<PathBuf>> {
    let home = std::env::var("HOME").unwrap_or_else(|_| "/home/crook".to_string());
    let cache_dir = PathBuf::from(home).join(".cache/huggingface/hub");
    let model_dir_name = format!("models--{}", model_name.replace('/', "--"));
    let model_dir = cache_dir.join(&model_dir_name);

    if !model_dir.exists() {
        return None;
    }

    let snapshots = model_dir.join("snapshots");
    if !snapshots.exists() {
        return None;
    }

    // Find the first snapshot directory
    let mut snapshot_dir: Option<PathBuf> = None;
    if let Ok(entries) = fs::read_dir(&snapshots) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                snapshot_dir = Some(path);
                break;
            }
        }
    }

    let snapshot = snapshot_dir?;

    // Find all safetensor files
    let mut files = Vec::new();
    if let Ok(entries) = fs::read_dir(&snapshot) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path
                .extension()
                .map(|e| e == "safetensors")
                .unwrap_or(false)
            {
                // Skip index files
                if !path
                    .file_name()
                    .map(|n| n.to_string_lossy().contains("index"))
                    .unwrap_or(false)
                {
                    files.push(path);
                }
            }
        }
    }

    // Sort files for consistent ordering (model-00001, model-00002, etc.)
    files.sort();

    if files.is_empty() {
        None
    } else {
        Some(files)
    }
}

fn main() {
    if let Err(e) = run() {
        eprintln!("\nError: {}", e);
        std::process::exit(1);
    }
}

fn run() -> Result<(), CompressionError> {
    let base_retention: f32 = std::env::var("RETENTION")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(0.30);

    let use_int4 = std::env::var("NO_INT4").is_err();

    // Determine compression mode
    let mode = if std::env::var("IMPORTANCE").is_ok() {
        CompressionMode::Importance
    } else if std::env::var("MIXED_PRECISION").is_ok() {
        CompressionMode::MixedPrecision
    } else if std::env::var("ADAPTIVE").is_ok() {
        CompressionMode::Adaptive
    } else if std::env::var("LAYERTYPE").is_ok() {
        CompressionMode::LayerType
    } else if std::env::var("HYBRID").is_ok() {
        CompressionMode::Hybrid
    } else {
        CompressionMode::Uniform
    };

    // Importance map file (optional - defaults to heuristics)
    let importance_file = std::env::var("IMPORTANCE_FILE").ok();

    // FP16 ratio for mixed precision mode (fraction of retained coefficients stored as FP16)
    let fp16_ratio: f32 = std::env::var("FP16_RATIO")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(0.20); // Default 20% FP16 essentials

    // Target quality for adaptive mode
    let target_quality: f32 = std::env::var("TARGET_QUALITY")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(0.90);

    // Get model name from environment or use default
    let model_name =
        std::env::var("MODEL").unwrap_or_else(|_| "Qwen/Qwen2.5-0.5B-Instruct".to_string());

    // MAX_TENSORS limit for testing
    let max_tensors: Option<usize> = std::env::var("MAX_TENSORS")
        .ok()
        .and_then(|s| s.parse().ok());

    let input_paths = find_model_in_cache(&model_name)
        .ok_or_else(|| CompressionError::ModelNotFound(model_name.clone()))?;

    // Use first path for display purposes
    let display_path = input_paths
        .first()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|| "unknown".to_string());

    let mode_prefix = match mode {
        CompressionMode::Uniform => "",
        CompressionMode::Hybrid => "hybrid-",
        CompressionMode::LayerType => "layertype-",
        CompressionMode::Adaptive => "adaptive-",
        CompressionMode::MixedPrecision => "mixedprec-",
        CompressionMode::Importance => "importance-",
    };

    let output_path = PathBuf::from(format!(
        "/tmp/qwen-compressed-{}{:.0}pct{}.safetensors",
        mode_prefix,
        if mode == CompressionMode::Adaptive {
            target_quality * 100.0
        } else {
            base_retention * 100.0
        },
        if use_int4
            && mode != CompressionMode::MixedPrecision
            && mode != CompressionMode::Importance
        {
            "-int4"
        } else if use_int4 && mode == CompressionMode::Importance {
            "-int4pp"
        }
        // INT4 post-processing
        else {
            ""
        }
    ));

    println!("\n=== Creating Compressed Model ===\n");
    println!(
        "Input:  {} ({} shard{})",
        display_path,
        input_paths.len(),
        if input_paths.len() > 1 { "s" } else { "" }
    );
    println!("Output: {}", output_path.display());
    if let Some(max) = max_tensors {
        println!("Max tensors: {}", max);
    }
    println!("Base Retention: {:.0}%", base_retention * 100.0);
    println!("Mode: {:?}", mode);
    match mode {
        CompressionMode::Uniform => {
            println!("  - All layers: {:.0}%", base_retention * 100.0);
        }
        CompressionMode::Hybrid => {
            println!("  - Layers 0, 23: 100% (critical)");
            println!("  - Layers 1-2, 21-22: 80% (near-critical)");
            println!(
                "  - Layers 3-20: {:.0}% (aggressive)",
                base_retention * 100.0
            );
            println!("  - Layernorms/biases: 100%");
        }
        CompressionMode::LayerType => {
            println!("  - LayerNorm/Bias/Embed: 100% (critical)");
            println!("  - lm_head: 90%");
            println!("  - Attention Q/K: {:.0}% (base)", base_retention * 100.0);
            println!(
                "  - Attention V/O: {:.0}% (+10%)",
                (base_retention * 1.1).min(1.0) * 100.0
            );
            println!(
                "  - FFN/MLP: {:.0}% (aggressive)",
                (base_retention * 0.70).max(0.40) * 100.0
            );
        }
        CompressionMode::Adaptive => {
            println!("  - Target quality: {:.0}%", target_quality * 100.0);
            println!("  - Per-tensor retention based on spectral energy analysis");
            println!("  - Optimizes each tensor independently for target quality");
        }
        CompressionMode::MixedPrecision => {
            println!("  - Retention: {:.0}%", base_retention * 100.0);
            println!(
                "  - FP16 essentials: {:.0}% of retained coefficients",
                fp16_ratio * 100.0
            );
            println!(
                "  - INT4 details: {:.0}% of retained coefficients",
                (1.0 - fp16_ratio) * 100.0
            );
            println!("  - Progressive decode: load FP16 first, then INT4");
        }
        CompressionMode::Importance => {
            println!("  - Base retention: {:.0}%", base_retention * 100.0);
            if let Some(ref file) = importance_file {
                println!("  - Importance map: {}", file);
            } else {
                println!("  - Importance: Heuristic (layer-type based)");
            }
            println!("  - Higher retention for: embeddings, v_proj, o_proj, LayerNorm");
            println!("  - Lower retention for: MLP/FFN, q_proj, k_proj");
        }
    }
    if mode != CompressionMode::MixedPrecision && mode != CompressionMode::Importance {
        println!("INT4: {}", use_int4);
    } else if mode == CompressionMode::Importance {
        println!("INT4 post-processing: {}", use_int4);
    }
    println!();

    let start = Instant::now();

    // Check if we should show progress bar
    let quiet = std::env::var("QUIET").is_ok();

    // Process all tensors and collect results
    let mut processed_tensors: Vec<(String, Vec<usize>, Vec<u8>)> = Vec::new();
    let mut skipped = 0;
    let mut processed = 0;
    let mut retention_stats: std::collections::HashMap<String, usize> =
        std::collections::HashMap::new();
    let mut total_input_size: u64 = 0;

    // Create importance encoder if in importance mode
    let importance_encoder = if mode == CompressionMode::Importance {
        let importance_map = if let Some(ref path) = importance_file {
            ImportanceMap::load(path).unwrap_or_else(|e| {
                eprintln!(
                    "Warning: Failed to load importance map: {}. Using heuristics.",
                    e
                );
                ImportanceMap::heuristic_only()
            })
        } else {
            ImportanceMap::heuristic_only()
        };
        Some(ImportanceGuidedEncoder::new(base_retention, importance_map))
    } else {
        None
    };

    // Count total tensors across all shards for progress bar
    let mut total_tensors = 0usize;
    for input_path in &input_paths {
        let data = fs::read(input_path)
            .map_err(|e| CompressionError::FileReadError(input_path.display().to_string(), e))?;
        let (_, tensors) =
            parse_safetensors_header(&data).map_err(CompressionError::HeaderParseError)?;
        total_tensors += tensors.len();
        total_input_size += data.len() as u64;
    }

    let effective_total = max_tensors
        .map(|m| m.min(total_tensors))
        .unwrap_or(total_tensors);
    println!(
        "Found {} tensors across {} shard(s){}\n",
        total_tensors,
        input_paths.len(),
        if max_tensors.is_some() {
            format!(" (processing {})", effective_total)
        } else {
            String::new()
        }
    );

    // Create progress bar
    let progress = if quiet {
        ProgressBar::hidden()
    } else {
        let pb = ProgressBar::new(effective_total as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta}) {msg}")
                .expect("valid template")
                .progress_chars("=>-")
        );
        pb
    };

    // Track total processed across all shards
    let mut tensors_processed_total = 0usize;
    let max_tensor_limit = max_tensors.unwrap_or(usize::MAX);

    // Process each shard file
    for (shard_idx, input_path) in input_paths.iter().enumerate() {
        if tensors_processed_total >= max_tensor_limit {
            break;
        }

        if !quiet && input_paths.len() > 1 {
            progress.println(format!(
                "Processing shard {}/{}: {}",
                shard_idx + 1,
                input_paths.len(),
                input_path.file_name().unwrap_or_default().to_string_lossy()
            ));
        }

        let data = fs::read(input_path)
            .map_err(|e| CompressionError::FileReadError(input_path.display().to_string(), e))?;
        let (data_start, tensors) =
            parse_safetensors_header(&data).map_err(CompressionError::HeaderParseError)?;

        // Sort tensors by name for consistent ordering
        let mut tensor_items: Vec<_> = tensors.iter().collect();
        tensor_items.sort_by(|a, b| a.0.cmp(b.0));

        for (name, info) in tensor_items {
            if tensors_processed_total >= max_tensor_limit {
                break;
            }

            let num_elements: usize = info.shape.iter().product();

            // Get original data
            let (start_off, end_off) = info.data_offsets;
            let tensor_data = &data[data_start + start_off..data_start + end_off];

            // Update progress bar with current tensor name
            progress.set_message(name.clone());

            if !(256..=MAX_ELEMENTS).contains(&num_elements) {
                // Keep original (just convert to F16 for consistency)
                let values = bytes_to_f32(tensor_data, &info.dtype)?;
                let output_bytes = f32_to_f16_bytes(&values);
                processed_tensors.push((name.clone(), info.shape.clone(), output_bytes));
                if num_elements > MAX_ELEMENTS && !quiet {
                    progress.println(format!("  SKIP {} (too large)", name));
                }
                skipped += 1;
                tensors_processed_total += 1;
                progress.inc(1);
                continue;
            }

            let values = bytes_to_f32(tensor_data, &info.dtype)?;
            if values.is_empty() {
                skipped += 1;
                tensors_processed_total += 1;
                progress.inc(1);
                continue;
            }

            let (width, height) = if info.shape.len() == 2 {
                (info.shape[1], info.shape[0])
            } else if info.shape.len() == 1 {
                (info.shape[0], 1)
            } else {
                (num_elements, 1)
            };

            // Handle different compression modes
            if mode == CompressionMode::Adaptive {
                match compress_decompress_adaptive(&values, width, height, target_quality, use_int4)
                {
                    Ok((reconstructed, retention_used)) => {
                        let retention_key = format!("{:.0}%", retention_used * 100.0);
                        *retention_stats.entry(retention_key).or_insert(0) += 1;
                        let output_bytes = f32_to_f16_bytes(&reconstructed);
                        processed_tensors.push((name.clone(), info.shape.clone(), output_bytes));
                        processed += 1;
                    }
                    Err(e) => {
                        if !quiet {
                            progress.println(format!("  FAIL {}: {}", name, e));
                        }
                        // Keep original on compression failure (graceful degradation)
                        let fallback_values = bytes_to_f32(tensor_data, &info.dtype)?;
                        let output_bytes = f32_to_f16_bytes(&fallback_values);
                        processed_tensors.push((name.clone(), info.shape.clone(), output_bytes));
                        skipped += 1;
                    }
                }
            } else if mode == CompressionMode::MixedPrecision {
                // Mixed precision: FP16 essentials + INT4 details
                // Limit tensor size to avoid slow DCT on large tensors
                if num_elements > 100_000 {
                    // Too large for mixed precision DCT - fall back to compressive spectral
                    let tensor_retention = get_layer_retention(name, base_retention, mode);
                    let encoder = CompressiveSpectralEncoder::new(8, tensor_retention);
                    match compress_decompress(&values, width, height, &encoder, use_int4) {
                        Ok(reconstructed) => {
                            let output_bytes = f32_to_f16_bytes(&reconstructed);
                            processed_tensors.push((
                                name.clone(),
                                info.shape.clone(),
                                output_bytes,
                            ));
                            processed += 1;
                        }
                        Err(e) => {
                            if !quiet {
                                progress.println(format!("  FAIL {}: {}", name, e));
                            }
                            let fallback_values = bytes_to_f32(tensor_data, &info.dtype)?;
                            let output_bytes = f32_to_f16_bytes(&fallback_values);
                            processed_tensors.push((
                                name.clone(),
                                info.shape.clone(),
                                output_bytes,
                            ));
                            skipped += 1;
                        }
                    }
                } else {
                    // Use mixed precision for smaller tensors
                    match compress_decompress_mixed_precision(
                        &values,
                        width,
                        height,
                        base_retention,
                        fp16_ratio,
                    ) {
                        Ok(reconstructed) => {
                            let retention_key = format!("{:.0}%", base_retention * 100.0);
                            *retention_stats.entry(retention_key).or_insert(0) += 1;
                            let output_bytes = f32_to_f16_bytes(&reconstructed);
                            processed_tensors.push((
                                name.clone(),
                                info.shape.clone(),
                                output_bytes,
                            ));
                            processed += 1;
                        }
                        Err(e) => {
                            if !quiet {
                                progress.println(format!("  FAIL {}: {}", name, e));
                            }
                            // Keep original on compression failure (graceful degradation)
                            let fallback_values = bytes_to_f32(tensor_data, &info.dtype)?;
                            let output_bytes = f32_to_f16_bytes(&fallback_values);
                            processed_tensors.push((
                                name.clone(),
                                info.shape.clone(),
                                output_bytes,
                            ));
                            skipped += 1;
                        }
                    }
                }
            } else if mode == CompressionMode::Importance {
                // Importance-guided compression
                let encoder = importance_encoder
                    .as_ref()
                    .expect("importance_encoder should be initialized");
                match compress_decompress_importance(
                    &values, width, height, name, encoder, use_int4,
                ) {
                    Ok((reconstructed, retention_used)) => {
                        let retention_key = format!("{:.0}%", retention_used * 100.0);
                        *retention_stats.entry(retention_key).or_insert(0) += 1;
                        let output_bytes = f32_to_f16_bytes(&reconstructed);
                        processed_tensors.push((name.clone(), info.shape.clone(), output_bytes));
                        processed += 1;
                    }
                    Err(e) => {
                        if !quiet {
                            progress.println(format!("  FAIL {}: {}", name, e));
                        }
                        // Keep original on compression failure (graceful degradation)
                        let fallback_values = bytes_to_f32(tensor_data, &info.dtype)?;
                        let output_bytes = f32_to_f16_bytes(&fallback_values);
                        processed_tensors.push((name.clone(), info.shape.clone(), output_bytes));
                        skipped += 1;
                    }
                }
            } else {
                // Other modes: use compressive spectral encoder
                let tensor_retention = get_layer_retention(name, base_retention, mode);
                let retention_key = format!("{:.0}%", tensor_retention * 100.0);
                *retention_stats.entry(retention_key).or_insert(0) += 1;

                let encoder = CompressiveSpectralEncoder::new(8, tensor_retention);

                match compress_decompress(&values, width, height, &encoder, use_int4) {
                    Ok(reconstructed) => {
                        let output_bytes = f32_to_f16_bytes(&reconstructed);
                        processed_tensors.push((name.clone(), info.shape.clone(), output_bytes));
                        processed += 1;
                    }
                    Err(e) => {
                        if !quiet {
                            progress.println(format!("  FAIL {}: {}", name, e));
                        }
                        // Keep original on compression failure (graceful degradation)
                        let fallback_values = bytes_to_f32(tensor_data, &info.dtype)?;
                        let output_bytes = f32_to_f16_bytes(&fallback_values);
                        processed_tensors.push((name.clone(), info.shape.clone(), output_bytes));
                        skipped += 1;
                    }
                }
            }
            tensors_processed_total += 1;
            progress.inc(1);
        }
    }

    progress.finish_with_message("done");
    println!("\nProcessed {} tensors, skipped {}", processed, skipped);

    if mode != CompressionMode::Uniform {
        println!("\nRetention distribution:");
        let mut stats: Vec<_> = retention_stats.into_iter().collect();
        stats.sort_by(|a, b| b.0.cmp(&a.0));
        for (retention, count) in stats {
            println!("  {}: {} tensors", retention, count);
        }
    }

    // Write safetensors file
    println!("\nWriting output...");

    // Build header
    let mut header_map = serde_json::Map::new();
    let mut current_offset: u64 = 0;

    for (name, shape, bytes) in &processed_tensors {
        let mut tensor_info = serde_json::Map::new();
        tensor_info.insert(
            "dtype".to_string(),
            serde_json::Value::String("F16".to_string()),
        );
        tensor_info.insert(
            "shape".to_string(),
            serde_json::Value::Array(
                shape
                    .iter()
                    .map(|&s| serde_json::Value::Number(s.into()))
                    .collect(),
            ),
        );
        let end_offset = current_offset + bytes.len() as u64;
        tensor_info.insert(
            "data_offsets".to_string(),
            serde_json::Value::Array(vec![
                serde_json::Value::Number(current_offset.into()),
                serde_json::Value::Number(end_offset.into()),
            ]),
        );
        header_map.insert(name.clone(), serde_json::Value::Object(tensor_info));
        current_offset = end_offset;
    }

    let header_json =
        serde_json::to_string(&serde_json::Value::Object(header_map)).map_err(|e| {
            CompressionError::EncodingError(format!("JSON serialization failed: {}", e))
        })?;
    let header_bytes = header_json.as_bytes();

    // Pad header to 8-byte alignment
    let padding = (8 - (header_bytes.len() % 8)) % 8;
    let padded_header_len = header_bytes.len() + padding;

    let file = File::create(&output_path)
        .map_err(|e| CompressionError::FileWriteError(output_path.display().to_string(), e))?;
    let mut writer = BufWriter::new(file);

    // Write header length
    writer
        .write_all(&(padded_header_len as u64).to_le_bytes())
        .map_err(|e| CompressionError::FileWriteError(output_path.display().to_string(), e))?;
    // Write header
    writer
        .write_all(header_bytes)
        .map_err(|e| CompressionError::FileWriteError(output_path.display().to_string(), e))?;
    // Write padding
    writer
        .write_all(&vec![0x20u8; padding])
        .map_err(|e| CompressionError::FileWriteError(output_path.display().to_string(), e))?;

    // Write tensor data
    for (_, _, bytes) in &processed_tensors {
        writer
            .write_all(bytes)
            .map_err(|e| CompressionError::FileWriteError(output_path.display().to_string(), e))?;
    }

    writer
        .flush()
        .map_err(|e| CompressionError::FileWriteError(output_path.display().to_string(), e))?;

    let elapsed = start.elapsed();
    let output_size = fs::metadata(&output_path)
        .map_err(|e| CompressionError::FileReadError(output_path.display().to_string(), e))?
        .len();

    println!("\n=== Done ===\n");
    println!("Time: {:.1}s", elapsed.as_secs_f64());
    println!(
        "Input:  {:.2} MB",
        total_input_size as f64 / 1024.0 / 1024.0
    );
    println!("Output: {:.2} MB", output_size as f64 / 1024.0 / 1024.0);
    println!("\nCompressed model saved to: {}", output_path.display());
    println!("\nTest with Python:");
    println!(
        "  python3 test_inference.py --original {} --compressed {}",
        display_path,
        output_path.display()
    );

    Ok(())
}