turboquant-rs 0.4.1

TurboQuant KV-Cache Quantization — 3-bit compression with zero accuracy loss (Zandieh et al., ICLR 2026)
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
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
//! High-level quantized KV-cache attention API.
//!
//! Provides [`QuantizedKVCache`] for storing quantized key-value pairs
//! and computing attention scores with QJL bias correction.
//!
//! This is the main entry point for users who want to quantize and query
//! KV-caches in a transformer model.

use half::f16;

use crate::codebook::get_codebook;
use crate::error::{check_values_match, require, Result, TurboQuantError};
use crate::packed::{PackedBlock, TurboQuantConfig};
use crate::qjl::{
    estimate_inner_product_with_codebook, precompute_query_projections, quantize_with_qjl,
    EstimationContext, QjlBlock,
};
use crate::quantize::{dequantize_into_with_codebook, DequantScratch};
use crate::rotation::generate_sign_pattern;

// ---------------------------------------------------------------------------
// Named constants (no magic numbers)
// ---------------------------------------------------------------------------

/// Number of bytes per FP16 value.
const BYTES_PER_F16: usize = 2;

/// Number of KV components (key + value).
const KV_PAIR_COUNT: usize = 2;

/// Size of the f16 residual_norm field in a QjlBlock.
const RESIDUAL_NORM_BYTES: usize = 2;

// ---------------------------------------------------------------------------
// QuantizedKVCache
// ---------------------------------------------------------------------------

/// A quantized key-value cache for transformer attention layers.
///
/// Stores keys and values quantized via TURBOQUANTprod (PolarQuant + QJL).
/// Keys are queried via approximate inner products; values are dequantized
/// for weighted summation.
// qual:allow(srp) — cohesive KV-cache struct: management, attention, and dequantization
// all operate on the same (keys, values, config) state.
pub struct QuantizedKVCache {
    /// Quantization configuration (bits, dim, rotation seed).
    config: TurboQuantConfig,
    /// Seed for the QJL Rademacher matrix.
    qjl_seed: u64,
    /// Quantized key blocks per layer: keys[layer][seq_pos].
    keys: Vec<Vec<QjlBlock>>,
    /// Quantized value blocks per layer: values[layer][seq_pos].
    values: Vec<Vec<QjlBlock>>,
}

// ---------------------------------------------------------------------------
// Pure Operation helpers (no project calls)
// ---------------------------------------------------------------------------

/// Checks whether a layer index is within bounds.
///
/// Pure Operation: comparison only.
fn validate_layer(index: usize, num_layers: usize) -> bool {
    index < num_layers
}

/// Fused multiply-add: result[i] += values[i] * weight.
///
/// Pure Operation: arithmetic only.
fn accumulate_weighted(result: &mut [f32], values: &[f32], weight: f32) {
    for (r, &v) in result.iter_mut().zip(values.iter()) {
        *r += v * weight;
    }
}

/// Computes the memory footprint of a single QjlBlock in bytes.
///
/// Pure Operation: field access and arithmetic only.
// iosp:allow — trivial size aggregation: arithmetic between accessor calls
fn qjl_block_size_bytes(block: &QjlBlock) -> usize {
    block.polar_block.size_bytes() + block.qjl_signs.len() + RESIDUAL_NORM_BYTES
}

/// Returns the length of a layer's key vector.
///
/// Pure Operation: field access only.
fn layer_len(layer_keys: &[QjlBlock]) -> usize {
    layer_keys.len()
}

// ---------------------------------------------------------------------------
// Validation helpers (Pure Integration: only calls require + pure ops)
// ---------------------------------------------------------------------------

/// Validates that a layer index is within bounds.
fn check_layer(index: usize, num_layers: usize) -> Result<()> {
    require(
        validate_layer(index, num_layers),
        TurboQuantError::LayerOutOfRange { index, num_layers },
    )
}

/// Validates that all keys and values in a batch have the expected dimension.
fn validate_batch_dims(keys: &[&[f32]], values: &[&[f32]], dim: usize) -> Result<()> {
    for (i, key) in keys.iter().enumerate() {
        check_values_match(key.len(), dim).map_err(|_| TurboQuantError::DimensionMismatch {
            expected: dim,
            actual: key.len(),
        })?;
        check_values_match(values[i].len(), dim).map_err(|_| {
            TurboQuantError::DimensionMismatch {
                expected: dim,
                actual: values[i].len(),
            }
        })?;
    }
    Ok(())
}

/// Checks whether a range `[start..end)` is valid for a given entry count.
fn validate_range(start: usize, end: usize, entry_count: usize) -> bool {
    start <= end && end <= entry_count
}

/// Validates that `[start..end)` is within `[0..entry_count)`.
///
/// Pure Integration: only calls `require` and `validate_range`.
fn check_range(start: usize, end: usize, entry_count: usize) -> Result<()> {
    require(
        validate_range(start, end, entry_count),
        TurboQuantError::RangeOutOfBounds {
            start,
            end,
            entry_count,
        },
    )
}

// ---------------------------------------------------------------------------
// GPU import parameter struct
// ---------------------------------------------------------------------------

/// Parameters for [`QuantizedKVCache::import_packed_range`].
///
/// Groups the many arguments needed to reconstruct QjlBlocks from
/// GPU-quantized flat buffers into a single struct.
pub struct PackedImport<'a> {
    /// Target layer index.
    pub layer: usize,
    /// Polar block bit width (2 for TQ3, 3 for TQ4).
    pub polar_bits: u8,
    /// Concatenated polar block packed indices.
    pub packed_bytes: &'a [u8],
    /// Polar block scale factors as raw u16 (f16 bits).
    pub scales: &'a [u16],
    /// Concatenated QJL sign bytes.
    pub qjl_signs_flat: &'a [u8],
    /// Residual L2 norms as raw u16 (f16 bits).
    pub residual_norms: &'a [u16],
    /// Number of packed bytes per polar block.
    pub bytes_per_block: usize,
    /// Number of QJL sign bytes per block (= ceil(dim/8)).
    pub signs_per_block: usize,
    /// `true` for keys, `false` for values.
    pub is_keys: bool,
}

// ---------------------------------------------------------------------------
// Pure Operation helpers for GPU export/import
// ---------------------------------------------------------------------------

/// Collects packed polar block bytes and f16 scales from a slice of QjlBlocks.
///
/// Pure Operation: accessor calls and collection only.
fn collect_packed_data(blocks: &[QjlBlock]) -> (Vec<u8>, Vec<u16>) {
    let packed_bytes: Vec<u8> = blocks
        .iter()
        .flat_map(|b| &b.polar_block.packed_indices)
        .copied()
        .collect();
    let scales: Vec<u16> = blocks
        .iter()
        .map(|b| b.polar_block.scale.to_bits())
        .collect();
    (packed_bytes, scales)
}

/// Reconstructs a single QjlBlock from flat packed buffers at a given index.
///
/// Pure Operation: slicing and construction only.
fn reconstruct_block(import: &PackedImport<'_>, index: usize) -> QjlBlock {
    let pb_start = index * import.bytes_per_block;
    let pb_end = pb_start + import.bytes_per_block;
    let polar_block = PackedBlock::from_raw(
        import.polar_bits,
        f16::from_bits(import.scales[index]),
        import.packed_bytes[pb_start..pb_end].to_vec(),
    );

    let qs_start = index * import.signs_per_block;
    let qs_end = qs_start + import.signs_per_block;
    let qjl_signs = import.qjl_signs_flat[qs_start..qs_end].to_vec();

    let residual_norm = f16::from_bits(import.residual_norms[index]);

    QjlBlock::from_parts(polar_block, qjl_signs, residual_norm)
}

// ---------------------------------------------------------------------------
// Integration: QuantizedKVCache methods
// ---------------------------------------------------------------------------

impl QuantizedKVCache {
    /// Creates a new empty KV cache with the given number of layers.
    ///
    /// Integration: allocates layer vectors.
    pub fn new(config: TurboQuantConfig, num_layers: usize, qjl_seed: u64) -> Self {
        let keys = (0..num_layers).map(|_| Vec::new()).collect();
        let values = (0..num_layers).map(|_| Vec::new()).collect();
        Self {
            config,
            qjl_seed,
            keys,
            values,
        }
    }

    /// Appends a quantized key-value pair to the specified layer.
    ///
    /// Integration: validates layer and dimensions, then delegates to
    /// `quantize_with_qjl` for both key and value.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] if the layer index is invalid.
    /// Returns [`TurboQuantError::DimensionMismatch`] if key or value length
    /// does not match `config.dim`.
    pub fn push(&mut self, layer: usize, key: &[f32], value: &[f32]) -> Result<()> {
        check_layer(layer, self.num_layers())?;
        check_values_match(key.len(), self.config.dim)?;
        check_values_match(value.len(), self.config.dim)?;

        let key_block = quantize_with_qjl(&self.config, key, self.qjl_seed)?;
        let value_block = quantize_with_qjl(&self.config, value, self.qjl_seed)?;

        self.keys[layer].push(key_block);
        self.values[layer].push(value_block);

        Ok(())
    }

    /// Appends multiple quantized key-value pairs to the specified layer in a
    /// single batch.
    ///
    /// More efficient than calling [`push`](Self::push) repeatedly because the
    /// codebook, sign pattern, polar config, and scratch buffers are computed
    /// once and reused across all pairs.  Best used during prefill where
    /// `new_seq_len > 1`.
    ///
    /// # Arguments
    ///
    /// * `layer` - Target layer index.
    /// * `keys`  - Slice of key vectors (each of length `config.dim`).
    /// * `values` - Slice of value vectors (each of length `config.dim`).
    ///
    /// `keys` and `values` must have the same length.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] if the layer index is invalid.
    /// Returns [`TurboQuantError::DimensionMismatch`] if any key or value length
    /// does not match `config.dim`.
    // qual:api — public API for batch insertion during prefill (used by mistral.rs)
    pub fn push_batch(&mut self, layer: usize, keys: &[&[f32]], values: &[&[f32]]) -> Result<()> {
        check_layer(layer, self.num_layers())?;
        check_values_match(keys.len(), values.len())?;
        validate_batch_dims(keys, values, self.config.dim)?;
        self.quantize_and_store(layer, keys, values)
    }

    fn quantize_and_store(
        &mut self,
        layer: usize,
        keys: &[&[f32]],
        values: &[&[f32]],
    ) -> Result<()> {
        use crate::qjl::{quantize_with_qjl_resources, QjlBatchResources};

        let mut res = QjlBatchResources::new(&self.config)?;
        self.keys[layer].reserve(keys.len());
        self.values[layer].reserve(values.len());

        for (key, value) in keys.iter().zip(values.iter()) {
            let key_block = quantize_with_qjl_resources(key, self.qjl_seed, &mut res)?;
            let value_block = quantize_with_qjl_resources(value, self.qjl_seed, &mut res)?;
            self.keys[layer].push(key_block);
            self.values[layer].push(value_block);
        }

        Ok(())
    }

    /// Computes approximate attention scores for a query against all stored keys
    /// in the specified layer.
    ///
    /// Returns a vector of estimated inner products, one per stored key.
    ///
    /// Pre-computes R*query ONCE via `precompute_query_projections`, then
    /// reuses it across all keys via `estimate_inner_product`. Uses
    /// SIMD-friendly sign unpacking.
    ///
    /// Integration: validates layer, pre-computes query projections, then
    /// delegates to `estimate_inner_product` for each key.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] if the layer index is invalid.
    // qual:api — public API for cache consumers (mistral.rs integration)
    pub fn attention_scores(&self, layer: usize, query: &[f32]) -> Result<Vec<f32>> {
        check_layer(layer, self.num_layers())?;

        // Pre-compute R*query ONCE for all keys in the layer.
        let r_query = precompute_query_projections(query, self.config.dim, self.qjl_seed);

        // Fetch codebook, sign pattern, and polar config ONCE before the loop.
        let fetch_and_score = |keys: &[QjlBlock]| -> Result<Vec<f32>> {
            if keys.is_empty() {
                return Ok(Vec::new());
            }
            let polar_bits = keys[0].polar_block.bits;
            let polar_config = TurboQuantConfig::new(polar_bits, self.config.dim)?
                .with_seed(self.config.rotation_seed);
            let codebook = get_codebook(polar_bits, self.config.dim)?;
            let sign_pattern = generate_sign_pattern(self.config.dim, self.config.rotation_seed);

            let mut ctx = EstimationContext {
                polar_config: &polar_config,
                codebook: &codebook,
                sign_pattern: &sign_pattern,
                dim: self.config.dim,
                scratch: DequantScratch::new(self.config.dim),
            };

            let mut scores = Vec::with_capacity(keys.len());
            for key_block in keys {
                let score =
                    estimate_inner_product_with_codebook(query, &r_query, key_block, &mut ctx)?;
                scores.push(score);
            }
            Ok(scores)
        };
        fetch_and_score(&self.keys[layer])
    }

    /// Computes a weighted sum of dequantized values in the specified layer.
    ///
    /// Each value is fully dequantized (with inverse rotation) before
    /// accumulation, because summed values require the original domain.
    /// The polar block uses `(bits-1)` bits, so we create the appropriate
    /// config from each block's `polar_block.bits`.
    ///
    /// Integration: validates layer and weights length, then delegates to
    /// `dequantize_vec` and `accumulate_weighted`.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] if the layer index is invalid.
    /// Returns [`TurboQuantError::DimensionMismatch`] if `weights.len()` does
    /// not match the number of entries in the layer.
    // qual:api — public API for cache consumers (mistral.rs integration)
    pub fn weighted_values(&self, layer: usize, weights: &[f32]) -> Result<Vec<f32>> {
        check_layer(layer, self.num_layers())?;
        check_values_match(weights.len(), layer_len(&self.keys[layer]))?;

        let dim = self.config.dim;
        let mut result = vec![0.0_f32; dim];

        let dequantize_and_accumulate = |result: &mut Vec<f32>| -> Result<()> {
            let values = &self.values[layer];
            if values.is_empty() {
                return Ok(());
            }
            // Fetch codebook, sign pattern, and polar config ONCE before the loop.
            let polar_bits = values[0].polar_block.bits;
            let polar_config =
                TurboQuantConfig::new(polar_bits, dim)?.with_seed(self.config.rotation_seed);
            let codebook = get_codebook(polar_bits, dim)?;
            let sign_pattern = generate_sign_pattern(dim, self.config.rotation_seed);

            // Pre-allocate scratch buffers reused across all iterations.
            let mut scratch = DequantScratch::new(dim);

            for (block, &w) in values.iter().zip(weights.iter()) {
                dequantize_into_with_codebook(
                    &polar_config,
                    &block.polar_block,
                    &codebook,
                    &sign_pattern,
                    &mut scratch,
                )?;
                accumulate_weighted(result, &scratch.values, w);
            }
            Ok(())
        };
        dequantize_and_accumulate(&mut result)?;

        Ok(result)
    }

    /// Total bytes used by all stored QJL blocks across all layers.
    ///
    /// Integration: delegates to `qjl_block_size_bytes` for each block.
    // qual:api — public API for cache consumers
    pub fn memory_usage(&self) -> usize {
        let key_bytes: usize = self
            .keys
            .iter()
            .flat_map(|layer| layer.iter())
            .map(qjl_block_size_bytes)
            .sum();
        let value_bytes: usize = self
            .values
            .iter()
            .flat_map(|layer| layer.iter())
            .map(qjl_block_size_bytes)
            .sum();
        key_bytes + value_bytes
    }

    /// What the same data would cost in FP16 storage.
    ///
    /// Each entry stores a key and a value, each of dimension `dim`,
    /// at 2 bytes per FP16 element.
    ///
    /// Pure Operation: arithmetic only.
    // qual:api — public API for cache consumers
    pub fn fp16_equivalent_memory(&self) -> usize {
        let total_entries: usize = self.keys.iter().map(|layer| layer.len()).sum();
        total_entries * self.config.dim * BYTES_PER_F16 * KV_PAIR_COUNT
    }

    /// Number of stored entries in a layer.
    ///
    /// Pure Operation: field access only.
    // qual:api — public API for cache consumers
    pub fn entry_count(&self, layer: usize) -> usize {
        layer_len(&self.keys[layer])
    }

    /// Returns a reference to the key QjlBlock at `(layer, index)`.
    ///
    /// Returns `None` if the index is out of bounds.
    ///
    /// Pure Operation: field access only.
    // qual:api — used by mistral.rs TurboQuantKVCache for QJL data access
    pub fn key_block(&self, layer: usize, index: usize) -> Option<&QjlBlock> {
        self.keys.get(layer).and_then(|blocks| blocks.get(index))
    }

    /// Number of layers in the cache.
    ///
    /// Pure Operation: field access only.
    pub fn num_layers(&self) -> usize {
        self.keys.len()
    }

    /// Dequantizes all stored key vectors at the given layer.
    ///
    /// Returns a `Vec` of `Vec<f32>`, one inner vector per stored entry.
    /// Each inner vector has length `config.dim`.
    ///
    /// Integration: validates layer, fetches codebook and sign pattern once,
    /// then delegates to `dequantize_into_with_codebook` for each block.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] if the layer index is invalid.
    // qual:api — used by mistral.rs TurboQuantKVCache integration
    pub fn dequantize_all_keys(&self, layer: usize) -> Result<Vec<Vec<f32>>> {
        self.dequantize_all_blocks(layer, &self.keys)
    }

    /// Dequantizes all stored value vectors at the given layer.
    ///
    /// Returns a `Vec` of `Vec<f32>`, one inner vector per stored entry.
    /// Each inner vector has length `config.dim`.
    ///
    /// Integration: validates layer, fetches codebook and sign pattern once,
    /// then delegates to `dequantize_into_with_codebook` for each block.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] if the layer index is invalid.
    // qual:api — used by mistral.rs TurboQuantKVCache integration
    pub fn dequantize_all_values(&self, layer: usize) -> Result<Vec<Vec<f32>>> {
        self.dequantize_all_blocks(layer, &self.values)
    }

    /// Shared implementation for dequantizing all blocks in a layer.
    ///
    /// Integration: validates layer, fetches codebook and sign pattern once,
    /// then iterates over blocks with scratch-based dequantization.
    fn dequantize_all_blocks(
        &self,
        layer: usize,
        blocks_per_layer: &[Vec<QjlBlock>],
    ) -> Result<Vec<Vec<f32>>> {
        check_layer(layer, self.num_layers())?;
        self.dequantize_block_slice(&blocks_per_layer[layer])
    }

    /// Dequantizes a slice of QjlBlocks into per-entry f32 vectors.
    /// Returns an empty Vec for empty input (safe to call with empty slice).
    // iosp:allow — algorithmic function: codebook/config setup + dequantization loop
    fn dequantize_block_slice(&self, blocks: &[QjlBlock]) -> Result<Vec<Vec<f32>>> {
        if blocks.is_empty() {
            return Ok(Vec::new());
        }
        let dim = self.config.dim;
        let polar_bits = blocks[0].polar_block.bits;
        let polar_config =
            TurboQuantConfig::new(polar_bits, dim)?.with_seed(self.config.rotation_seed);
        let codebook = get_codebook(polar_bits, dim)?;
        let sign_pattern = generate_sign_pattern(dim, self.config.rotation_seed);

        let mut scratch = DequantScratch::new(dim);
        let mut result = Vec::with_capacity(blocks.len());

        for block in blocks {
            dequantize_into_with_codebook(
                &polar_config,
                &block.polar_block,
                &codebook,
                &sign_pattern,
                &mut scratch,
            )?;
            result.push(scratch.values.clone());
        }

        Ok(result)
    }

    /// Shared implementation for dequantizing blocks in a range `[start..end)`
    /// at the given layer.
    ///
    /// Integration: validates layer and range, fetches codebook and sign pattern
    /// once, then iterates only over the requested sub-slice of blocks.
    fn dequantize_blocks_range(
        &self,
        layer: usize,
        start: usize,
        end: usize,
        blocks_per_layer: &[Vec<QjlBlock>],
    ) -> Result<Vec<Vec<f32>>> {
        check_layer(layer, self.num_layers())?;
        let blocks = &blocks_per_layer[layer];
        check_range(start, end, blocks.len())?;
        self.dequantize_block_slice(&blocks[start..end])
    }

    /// Dequantizes key vectors in the range `[start..end)` at the given layer.
    ///
    /// Returns a `Vec` of `Vec<f32>`, one per entry in the range.
    /// Each inner vector has length `config.dim`.
    ///
    /// Integration: validates layer and range, then delegates to
    /// `dequantize_blocks_range`.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] if the layer index is invalid.
    /// Returns [`TurboQuantError::RangeOutOfBounds`] if `start > end` or
    /// `end > entry_count`.
    // qual:api — used by mistral.rs TurboQuantKVCache for delta dequantization
    pub fn dequantize_keys_range(
        &self,
        layer: usize,
        start: usize,
        end: usize,
    ) -> Result<Vec<Vec<f32>>> {
        self.dequantize_blocks_range(layer, start, end, &self.keys)
    }

    /// Dequantizes value vectors in the range `[start..end)` at the given layer.
    ///
    /// Returns a `Vec` of `Vec<f32>`, one per entry in the range.
    /// Each inner vector has length `config.dim`.
    ///
    /// Integration: validates layer and range, then delegates to
    /// `dequantize_blocks_range`.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] if the layer index is invalid.
    /// Returns [`TurboQuantError::RangeOutOfBounds`] if `start > end` or
    /// `end > entry_count`.
    // qual:api — used by mistral.rs TurboQuantKVCache for delta dequantization
    pub fn dequantize_values_range(
        &self,
        layer: usize,
        start: usize,
        end: usize,
    ) -> Result<Vec<Vec<f32>>> {
        self.dequantize_blocks_range(layer, start, end, &self.values)
    }

    // -----------------------------------------------------------------------
    // Block selection helpers (Pure Operation: field access only)
    // -----------------------------------------------------------------------

    /// Returns a reference to the key or value blocks at a given layer.
    fn select_blocks(&self, layer: usize, is_keys: bool) -> &[QjlBlock] {
        if is_keys {
            &self.keys[layer]
        } else {
            &self.values[layer]
        }
    }

    /// Returns a mutable reference to the key or value blocks at a given layer.
    fn select_blocks_mut(&mut self, layer: usize, is_keys: bool) -> &mut Vec<QjlBlock> {
        if is_keys {
            &mut self.keys[layer]
        } else {
            &mut self.values[layer]
        }
    }

    // -----------------------------------------------------------------------
    // GPU integration: export / import packed data
    // -----------------------------------------------------------------------

    /// Returns the quantization configuration.
    ///
    /// Pure Operation: field access only.
    // qual:api — used by GPU kernel integration
    pub fn config(&self) -> &TurboQuantConfig {
        &self.config
    }

    /// Returns the QJL seed used for Rademacher matrix generation.
    ///
    /// Pure Operation: field access only.
    // qual:api — used by GPU kernel integration
    pub fn qjl_seed(&self) -> u64 {
        self.qjl_seed
    }

    /// Exports packed polar block data for a range of entries at a given layer.
    ///
    /// Returns `(flat_packed_bytes, scales_as_u16)` where:
    /// - `flat_packed_bytes` contains all `polar_block.packed_indices` concatenated
    /// - `scales_as_u16` contains each `polar_block.scale` as raw `u16` bits
    ///
    /// This is the primary interface for bulk-transferring quantized data to GPU
    /// memory for GPU-side dequantization.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] or
    /// [`TurboQuantError::RangeOutOfBounds`] on invalid arguments.
    // qual:api — used by GPU kernel integration for bulk data export
    pub fn export_packed_range(
        &self,
        layer: usize,
        start: usize,
        end: usize,
        is_keys: bool,
    ) -> Result<(Vec<u8>, Vec<u16>)> {
        check_layer(layer, self.num_layers())?;
        let blocks = self.select_blocks(layer, is_keys);
        check_range(start, end, blocks.len())?;
        Ok(collect_packed_data(&blocks[start..end]))
    }

    /// Imports GPU-quantized data as QjlBlocks into the cache at a given layer.
    ///
    /// See [`PackedImport`] for field documentation.
    ///
    /// # Errors
    ///
    /// Returns [`TurboQuantError::LayerOutOfRange`] on invalid layer.
    // qual:api — used by GPU kernel integration for importing quantized data
    pub fn import_packed_range(&mut self, import: &PackedImport<'_>) -> Result<()> {
        check_layer(import.layer, self.num_layers())?;
        let count = import.scales.len();
        let target = self.select_blocks_mut(import.layer, import.is_keys);
        target.reserve(count);
        for i in 0..count {
            target.push(reconstruct_block(import, i));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::qjl::dot_product;
    use crate::quantize::l2_norm;
    use crate::test_utils::{pseudo_random_vec, LCG_MULTIPLIER};

    // -- Named test constants ------------------------------------------------

    /// Test dimension (power of two, suitable for WHT).
    const TEST_DIM: usize = 64;

    /// Bit width for 3-bit quantization.
    const BITS_3: u8 = 3;

    /// Seed for PolarQuant rotation.
    const TEST_ROTATION_SEED: u64 = 42;

    /// Seed for QJL Rademacher matrix.
    const TEST_QJL_SEED: u64 = 12345;

    /// Number of layers in multi-layer tests.
    const TEST_NUM_LAYERS: usize = 4;

    /// Seed for key vector generation.
    const TEST_KEY_SEED: u64 = 11111;

    /// Seed for value vector generation.
    const TEST_VALUE_SEED: u64 = 22222;

    /// Seed for query vector generation.
    const TEST_QUERY_SEED: u64 = 33333;

    /// Second key seed for multi-entry tests.
    const TEST_KEY_SEED_2: u64 = 44444;

    /// Second value seed for multi-entry tests.
    const TEST_VALUE_SEED_2: u64 = 55555;

    /// Third key seed for multi-entry tests.
    const TEST_KEY_SEED_3: u64 = 66666;

    /// Third value seed for multi-entry tests.
    const TEST_VALUE_SEED_3: u64 = 77777;

    /// Maximum relative error for single-vector attention score approximation.
    /// QJL with 3-bit quantization can have ~20% relative error on individual scores.
    const SCORE_RELATIVE_TOLERANCE: f32 = 0.5;

    /// Maximum relative error for weighted value approximation.
    /// Weighted sums accumulate quantization error from multiple values.
    const WEIGHTED_RELATIVE_TOLERANCE: f32 = 1.0;

    /// Number of entries pushed in memory-usage tests.
    const MEMORY_TEST_ENTRIES: usize = 10;

    /// Minimum expected compression ratio for TQ3 (3-bit) vs FP16.
    const MIN_COMPRESSION_RATIO: f32 = 2.0;

    /// Layer index for single-layer tests.
    const TEST_LAYER: usize = 0;

    /// Second layer index for multi-layer tests.
    const TEST_LAYER_2: usize = 1;

    /// An out-of-range layer index.
    const INVALID_LAYER: usize = 999;

    /// Tolerance for floating-point comparisons.
    const FLOAT_EPSILON: f32 = 1e-6;

    /// Weight used in simple weighted-values tests.
    const TEST_WEIGHT_A: f32 = 0.6;

    /// Weight used in simple weighted-values tests.
    const TEST_WEIGHT_B: f32 = 0.4;

    /// Number of entries for FP16-equivalent test.
    const FP16_TEST_ENTRIES: usize = 5;

    /// Seed offset base for multi-entry loops.
    const SEED_OFFSET_BASE: u64 = 100;

    /// Creates a test config with standard parameters.
    fn test_config() -> TurboQuantConfig {
        TurboQuantConfig::new(BITS_3, TEST_DIM)
            .unwrap()
            .with_seed(TEST_ROTATION_SEED)
    }

    // -- Cache push/read roundtrip -------------------------------------------

    #[test]
    fn push_increases_len() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);

        assert_eq!(cache.entry_count(TEST_LAYER), 0);

        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let value = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        cache.push(TEST_LAYER, &key, &value).unwrap();

        assert_eq!(cache.entry_count(TEST_LAYER), 1);

        let key2 = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED_2);
        let value2 = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED_2);
        cache.push(TEST_LAYER, &key2, &value2).unwrap();

        assert_eq!(cache.entry_count(TEST_LAYER), 2);
    }

    // -- Attention scores ----------------------------------------------------

    #[test]
    fn attention_scores_approximate_dot_product() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);

        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let value = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        cache.push(TEST_LAYER, &key, &value).unwrap();

        let query = pseudo_random_vec(TEST_DIM, TEST_QUERY_SEED);
        let scores = cache.attention_scores(TEST_LAYER, &query).unwrap();

        assert_eq!(scores.len(), 1);

        let true_ip = dot_product(&query, &key);
        let relative_error = (scores[0] - true_ip).abs() / true_ip.abs().max(1.0);
        assert!(
            relative_error < SCORE_RELATIVE_TOLERANCE,
            "score relative error {relative_error} exceeds tolerance {SCORE_RELATIVE_TOLERANCE} \
             (estimated={}, true={true_ip})",
            scores[0]
        );
    }

    // -- Weighted values -----------------------------------------------------

    #[test]
    fn weighted_values_approximate_naive_sum() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);

        let key1 = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val1 = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        cache.push(TEST_LAYER, &key1, &val1).unwrap();

        let key2 = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED_2);
        let val2 = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED_2);
        cache.push(TEST_LAYER, &key2, &val2).unwrap();

        let weights = vec![TEST_WEIGHT_A, TEST_WEIGHT_B];
        let result = cache.weighted_values(TEST_LAYER, &weights).unwrap();

        // Compute naive weighted sum from original values
        let naive: Vec<f32> = (0..TEST_DIM)
            .map(|i| val1[i] * TEST_WEIGHT_A + val2[i] * TEST_WEIGHT_B)
            .collect();

        let error_vec: Vec<f32> = result
            .iter()
            .zip(naive.iter())
            .map(|(&r, &n)| r - n)
            .collect();
        let error_norm = l2_norm(&error_vec);
        let naive_norm = l2_norm(&naive);
        let relative_error = error_norm / naive_norm;

        assert!(
            relative_error < WEIGHTED_RELATIVE_TOLERANCE,
            "weighted values relative error {relative_error} exceeds tolerance {WEIGHTED_RELATIVE_TOLERANCE}"
        );
    }

    // -- Memory usage --------------------------------------------------------

    #[test]
    fn memory_usage_shows_compression() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);

        for i in 0..MEMORY_TEST_ENTRIES {
            let key = pseudo_random_vec(
                TEST_DIM,
                TEST_KEY_SEED.wrapping_add(i as u64 * SEED_OFFSET_BASE),
            );
            let val = pseudo_random_vec(
                TEST_DIM,
                TEST_VALUE_SEED.wrapping_add(i as u64 * SEED_OFFSET_BASE),
            );
            cache.push(TEST_LAYER, &key, &val).unwrap();
        }

        let quantized_bytes = cache.memory_usage();
        let fp16_bytes = cache.fp16_equivalent_memory();

        assert!(quantized_bytes > 0);
        assert!(fp16_bytes > 0);

        let ratio = fp16_bytes as f32 / quantized_bytes as f32;
        assert!(
            ratio > MIN_COMPRESSION_RATIO,
            "compression ratio {ratio} is below minimum {MIN_COMPRESSION_RATIO}"
        );
    }

    // -- FP16 equivalent memory ----------------------------------------------

    #[test]
    fn fp16_equivalent_memory_correct_calculation() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);

        for i in 0..FP16_TEST_ENTRIES {
            let key = pseudo_random_vec(
                TEST_DIM,
                TEST_KEY_SEED_3.wrapping_add(i as u64 * SEED_OFFSET_BASE),
            );
            let val = pseudo_random_vec(
                TEST_DIM,
                TEST_VALUE_SEED_3.wrapping_add(i as u64 * SEED_OFFSET_BASE),
            );
            cache.push(TEST_LAYER, &key, &val).unwrap();
        }

        let expected = FP16_TEST_ENTRIES * TEST_DIM * BYTES_PER_F16 * KV_PAIR_COUNT;
        assert_eq!(cache.fp16_equivalent_memory(), expected);
    }

    // -- Multi-layer cache ---------------------------------------------------

    #[test]
    fn multi_layer_cache_independent() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);

        let key1 = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val1 = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        cache.push(TEST_LAYER, &key1, &val1).unwrap();

        let key2 = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED_2);
        let val2 = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED_2);
        cache.push(TEST_LAYER_2, &key2, &val2).unwrap();
        cache.push(TEST_LAYER_2, &key2, &val2).unwrap();

        assert_eq!(cache.entry_count(TEST_LAYER), 1);
        assert_eq!(cache.entry_count(TEST_LAYER_2), 2);
        assert_eq!(cache.num_layers(), TEST_NUM_LAYERS);
    }

    // -- Empty cache ---------------------------------------------------------

    #[test]
    fn empty_cache_returns_empty_scores() {
        let config = test_config();
        let cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let query = pseudo_random_vec(TEST_DIM, TEST_QUERY_SEED);
        let scores = cache.attention_scores(TEST_LAYER, &query).unwrap();
        assert!(scores.is_empty());
    }

    // -- Layer validation ----------------------------------------------------

    #[test]
    fn push_rejects_invalid_layer() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        let result = cache.push(INVALID_LAYER, &key, &val);
        assert!(result.is_err());
    }

    #[test]
    fn attention_scores_rejects_invalid_layer() {
        let config = test_config();
        let cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let query = pseudo_random_vec(TEST_DIM, TEST_QUERY_SEED);
        let result = cache.attention_scores(INVALID_LAYER, &query);
        assert!(result.is_err());
    }

    #[test]
    fn weighted_values_rejects_invalid_layer() {
        let config = test_config();
        let cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let weights = vec![1.0_f32];
        let result = cache.weighted_values(INVALID_LAYER, &weights);
        assert!(result.is_err());
    }

    // -- Dimension validation ------------------------------------------------

    #[test]
    fn push_rejects_wrong_key_dimension() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let wrong_key = vec![1.0_f32; TEST_DIM + 1];
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        let result = cache.push(TEST_LAYER, &wrong_key, &val);
        assert!(result.is_err());
    }

    #[test]
    fn push_rejects_wrong_value_dimension() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let wrong_val = vec![1.0_f32; TEST_DIM + 1];
        let result = cache.push(TEST_LAYER, &key, &wrong_val);
        assert!(result.is_err());
    }

    // -- Pure operation helpers ----------------------------------------------

    #[test]
    fn validate_layer_in_bounds() {
        assert!(validate_layer(0, TEST_NUM_LAYERS));
        assert!(validate_layer(TEST_NUM_LAYERS - 1, TEST_NUM_LAYERS));
    }

    #[test]
    fn validate_layer_out_of_bounds() {
        assert!(!validate_layer(TEST_NUM_LAYERS, TEST_NUM_LAYERS));
        assert!(!validate_layer(INVALID_LAYER, TEST_NUM_LAYERS));
    }

    #[test]
    fn accumulate_weighted_basic() {
        let mut result = vec![0.0_f32; TEST_DIM];
        let values: Vec<f32> = (0..TEST_DIM).map(|i| i as f32).collect();
        let weight = TEST_WEIGHT_A;

        accumulate_weighted(&mut result, &values, weight);

        for (i, &r) in result.iter().enumerate() {
            let expected = i as f32 * weight;
            assert!(
                (r - expected).abs() < FLOAT_EPSILON,
                "mismatch at index {i}: expected {expected}, got {r}"
            );
        }
    }

    // -----------------------------------------------------------------------
    // Weighted values full roundtrip quality test
    // -----------------------------------------------------------------------

    /// Dimension for weighted values roundtrip test.
    const WV_ROUNDTRIP_DIM: usize = 128;

    /// Number of KV pairs for weighted values roundtrip test.
    const WV_ROUNDTRIP_ENTRIES: usize = 50;

    /// Seed offset for weighted values roundtrip key generation.
    const WV_KEY_SEED_OFFSET: u64 = 90000;

    /// Seed offset for weighted values roundtrip value generation.
    const WV_VALUE_SEED_OFFSET: u64 = 91000;

    /// Uniform weight for each entry (1/50).
    const WV_UNIFORM_WEIGHT: f32 = 1.0 / WV_ROUNDTRIP_ENTRIES as f32;

    /// Maximum acceptable relative error for uniform-weighted value roundtrip.
    /// Loose bound (< 1.0) but catches gross reconstruction bugs.
    const WV_MAX_RELATIVE_ERROR: f32 = 1.0;

    /// Rotation seed for weighted values roundtrip test.
    const WV_ROTATION_SEED: u64 = 42;

    /// QJL seed for weighted values roundtrip test.
    const WV_QJL_SEED: u64 = 31415;

    #[test]
    fn weighted_values_uniform_roundtrip_quality() {
        let config = TurboQuantConfig::new(BITS_3, WV_ROUNDTRIP_DIM)
            .unwrap()
            .with_seed(WV_ROTATION_SEED);
        let mut cache = QuantizedKVCache::new(config, 1, WV_QJL_SEED);

        // Store original values for ground-truth comparison.
        let mut original_values: Vec<Vec<f32>> = Vec::with_capacity(WV_ROUNDTRIP_ENTRIES);

        for i in 0..WV_ROUNDTRIP_ENTRIES {
            let key_seed = (i as u64)
                .wrapping_mul(LCG_MULTIPLIER)
                .wrapping_add(WV_KEY_SEED_OFFSET);
            let val_seed = (i as u64)
                .wrapping_mul(LCG_MULTIPLIER)
                .wrapping_add(WV_VALUE_SEED_OFFSET);

            let key = pseudo_random_vec(WV_ROUNDTRIP_DIM, key_seed);
            let val = pseudo_random_vec(WV_ROUNDTRIP_DIM, val_seed);

            original_values.push(val.clone());
            cache.push(0, &key, &val).unwrap();
        }

        // Uniform weights: 1/N for each entry.
        let weights = vec![WV_UNIFORM_WEIGHT; WV_ROUNDTRIP_ENTRIES];
        let result = cache.weighted_values(0, &weights).unwrap();

        assert_eq!(result.len(), WV_ROUNDTRIP_DIM);

        // Compute naive average of original values.
        let mut naive_avg = vec![0.0_f32; WV_ROUNDTRIP_DIM];
        for val in &original_values {
            for (j, &v) in val.iter().enumerate() {
                naive_avg[j] += v * WV_UNIFORM_WEIGHT;
            }
        }

        // Compute relative error between quantized weighted sum and naive average.
        let error_vec: Vec<f32> = result
            .iter()
            .zip(naive_avg.iter())
            .map(|(&r, &n)| r - n)
            .collect();
        let error_norm = l2_norm(&error_vec);
        let naive_norm = l2_norm(&naive_avg);

        const NORM_FLOOR: f32 = 1e-10;
        let relative_error = error_norm / naive_norm.max(NORM_FLOOR);
        eprintln!(
            "Weighted values uniform roundtrip: relative_error={relative_error:.4}, \
             error_norm={error_norm:.6}, naive_norm={naive_norm:.6}"
        );
        assert!(
            relative_error < WV_MAX_RELATIVE_ERROR,
            "Weighted values uniform roundtrip relative error {relative_error:.4} \
             exceeds tolerance {WV_MAX_RELATIVE_ERROR}"
        );
    }

    // -----------------------------------------------------------------------
    // Compression ratio verification tests
    // -----------------------------------------------------------------------

    /// Dimension used in compression-ratio tests (power of two, realistic head size).
    const RATIO_TEST_DIM: usize = 128;

    /// Number of entries pushed in compression-ratio tests.
    const RATIO_TEST_ENTRIES: usize = 100;

    /// Bit width for TQ4 (4-bit total budget).
    const BITS_4: u8 = 4;

    /// Minimum expected compression ratio for TQ3 (3-bit) vs FP16.
    /// TQ3: 3 bits/dim data + small overhead vs FP16: 16 bits/dim.
    /// Per KV pair: quantized ~52 bytes * 2 = 104 vs FP16 = 512 bytes.
    /// Expected >= 4.0x.
    const TQ3_MIN_COMPRESSION_RATIO: f32 = 4.0;

    /// Minimum expected compression ratio for TQ4 (4-bit) vs FP16.
    /// TQ4: 4 bits/dim data + small overhead vs FP16: 16 bits/dim.
    /// Expected >= 3.0x.
    const TQ4_MIN_COMPRESSION_RATIO: f32 = 3.0;

    /// Seed offset for compression-ratio test entry generation.
    const RATIO_SEED_OFFSET: u64 = 7000;

    #[test]
    fn tq3_compression_ratio_meets_minimum() {
        let config = TurboQuantConfig::new(BITS_3, RATIO_TEST_DIM)
            .unwrap()
            .with_seed(TEST_ROTATION_SEED);
        let mut cache = QuantizedKVCache::new(config, 1, TEST_QJL_SEED);

        for i in 0..RATIO_TEST_ENTRIES {
            let key = pseudo_random_vec(
                RATIO_TEST_DIM,
                TEST_KEY_SEED.wrapping_add(i as u64 * RATIO_SEED_OFFSET),
            );
            let val = pseudo_random_vec(
                RATIO_TEST_DIM,
                TEST_VALUE_SEED.wrapping_add(i as u64 * RATIO_SEED_OFFSET),
            );
            cache.push(0, &key, &val).unwrap();
        }

        let quantized_bytes = cache.memory_usage();
        let fp16_bytes = cache.fp16_equivalent_memory();
        let ratio = fp16_bytes as f32 / quantized_bytes as f32;

        assert!(
            ratio >= TQ3_MIN_COMPRESSION_RATIO,
            "TQ3 compression ratio {ratio:.2}x is below minimum {TQ3_MIN_COMPRESSION_RATIO}x \
             (quantized={quantized_bytes} bytes, fp16={fp16_bytes} bytes)"
        );
    }

    // -----------------------------------------------------------------------
    // Range dequantization tests
    // -----------------------------------------------------------------------

    /// Number of entries for range dequantization tests.
    const RANGE_TEST_ENTRIES: usize = 5;

    /// Seed offset for range test entry generation.
    const RANGE_SEED_OFFSET: u64 = 500;

    /// Populates a cache with `RANGE_TEST_ENTRIES` entries at `TEST_LAYER`.
    fn make_range_test_cache() -> QuantizedKVCache {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        for i in 0..RANGE_TEST_ENTRIES {
            let key = pseudo_random_vec(
                TEST_DIM,
                TEST_KEY_SEED.wrapping_add(i as u64 * RANGE_SEED_OFFSET),
            );
            let val = pseudo_random_vec(
                TEST_DIM,
                TEST_VALUE_SEED.wrapping_add(i as u64 * RANGE_SEED_OFFSET),
            );
            cache.push(TEST_LAYER, &key, &val).unwrap();
        }
        cache
    }

    #[test]
    fn dequantize_keys_range_full_matches_all() {
        let cache = make_range_test_cache();
        let all = cache.dequantize_all_keys(TEST_LAYER).unwrap();
        let range_all = cache
            .dequantize_keys_range(TEST_LAYER, 0, RANGE_TEST_ENTRIES)
            .unwrap();
        assert_eq!(all.len(), range_all.len());
        for (a, r) in all.iter().zip(range_all.iter()) {
            assert_eq!(a, r);
        }
    }

    #[test]
    fn dequantize_values_range_full_matches_all() {
        let cache = make_range_test_cache();
        let all = cache.dequantize_all_values(TEST_LAYER).unwrap();
        let range_all = cache
            .dequantize_values_range(TEST_LAYER, 0, RANGE_TEST_ENTRIES)
            .unwrap();
        assert_eq!(all.len(), range_all.len());
        for (a, r) in all.iter().zip(range_all.iter()) {
            assert_eq!(a, r);
        }
    }

    #[test]
    fn dequantize_keys_range_subset_matches_slice() {
        let cache = make_range_test_cache();
        let all = cache.dequantize_all_keys(TEST_LAYER).unwrap();
        let start = 1;
        let end = 3;
        let range_subset = cache.dequantize_keys_range(TEST_LAYER, start, end).unwrap();
        assert_eq!(range_subset.len(), end - start);
        for (i, vec) in range_subset.iter().enumerate() {
            assert_eq!(vec, &all[start + i]);
        }
    }

    #[test]
    fn dequantize_values_range_subset_matches_slice() {
        let cache = make_range_test_cache();
        let all = cache.dequantize_all_values(TEST_LAYER).unwrap();
        let start = 2;
        let end = 4;
        let range_subset = cache
            .dequantize_values_range(TEST_LAYER, start, end)
            .unwrap();
        assert_eq!(range_subset.len(), end - start);
        for (i, vec) in range_subset.iter().enumerate() {
            assert_eq!(vec, &all[start + i]);
        }
    }

    #[test]
    fn dequantize_keys_range_empty_returns_empty() {
        let cache = make_range_test_cache();
        let empty = cache.dequantize_keys_range(TEST_LAYER, 2, 2).unwrap();
        assert!(empty.is_empty());
    }

    #[test]
    fn dequantize_values_range_empty_returns_empty() {
        let cache = make_range_test_cache();
        let empty = cache.dequantize_values_range(TEST_LAYER, 0, 0).unwrap();
        assert!(empty.is_empty());
    }

    #[test]
    fn dequantize_keys_range_single_entry() {
        let cache = make_range_test_cache();
        let all = cache.dequantize_all_keys(TEST_LAYER).unwrap();
        let single = cache.dequantize_keys_range(TEST_LAYER, 3, 4).unwrap();
        assert_eq!(single.len(), 1);
        assert_eq!(single[0], all[3]);
    }

    #[test]
    fn dequantize_keys_range_rejects_invalid_layer() {
        let cache = make_range_test_cache();
        let result = cache.dequantize_keys_range(INVALID_LAYER, 0, 1);
        assert!(result.is_err());
    }

    #[test]
    fn dequantize_values_range_rejects_invalid_layer() {
        let cache = make_range_test_cache();
        let result = cache.dequantize_values_range(INVALID_LAYER, 0, 1);
        assert!(result.is_err());
    }

    #[test]
    fn dequantize_keys_range_rejects_start_greater_than_end() {
        let cache = make_range_test_cache();
        let result = cache.dequantize_keys_range(TEST_LAYER, 3, 1);
        assert!(result.is_err());
    }

    #[test]
    fn dequantize_values_range_rejects_end_beyond_entry_count() {
        let cache = make_range_test_cache();
        let result = cache.dequantize_values_range(TEST_LAYER, 0, RANGE_TEST_ENTRIES + 1);
        assert!(result.is_err());
    }

    #[test]
    fn validate_range_pure_operation() {
        assert!(validate_range(0, 0, 0));
        assert!(validate_range(0, 5, 5));
        assert!(validate_range(2, 3, 5));
        assert!(!validate_range(3, 2, 5));
        assert!(!validate_range(0, 6, 5));
    }

    #[test]
    fn tq4_compression_ratio_meets_minimum() {
        let config = TurboQuantConfig::new(BITS_4, RATIO_TEST_DIM)
            .unwrap()
            .with_seed(TEST_ROTATION_SEED);
        let mut cache = QuantizedKVCache::new(config, 1, TEST_QJL_SEED);

        for i in 0..RATIO_TEST_ENTRIES {
            let key = pseudo_random_vec(
                RATIO_TEST_DIM,
                TEST_KEY_SEED.wrapping_add(i as u64 * RATIO_SEED_OFFSET),
            );
            let val = pseudo_random_vec(
                RATIO_TEST_DIM,
                TEST_VALUE_SEED.wrapping_add(i as u64 * RATIO_SEED_OFFSET),
            );
            cache.push(0, &key, &val).unwrap();
        }

        let quantized_bytes = cache.memory_usage();
        let fp16_bytes = cache.fp16_equivalent_memory();
        let ratio = fp16_bytes as f32 / quantized_bytes as f32;

        assert!(
            ratio >= TQ4_MIN_COMPRESSION_RATIO,
            "TQ4 compression ratio {ratio:.2}x is below minimum {TQ4_MIN_COMPRESSION_RATIO}x \
             (quantized={quantized_bytes} bytes, fp16={fp16_bytes} bytes)"
        );
    }

    // -----------------------------------------------------------------------
    // push_batch tests
    // -----------------------------------------------------------------------

    /// Number of vectors in the batch test.
    const BATCH_TEST_COUNT: usize = 8;

    /// Seed offset for batch test vectors.
    const BATCH_SEED_OFFSET: u64 = 3000;

    #[test]
    fn push_batch_produces_same_results_as_individual_pushes() {
        let config = test_config();

        // Build key/value vectors
        let mut key_vecs: Vec<Vec<f32>> = Vec::new();
        let mut val_vecs: Vec<Vec<f32>> = Vec::new();
        for i in 0..BATCH_TEST_COUNT {
            key_vecs.push(pseudo_random_vec(
                TEST_DIM,
                TEST_KEY_SEED.wrapping_add(i as u64 * BATCH_SEED_OFFSET),
            ));
            val_vecs.push(pseudo_random_vec(
                TEST_DIM,
                TEST_VALUE_SEED.wrapping_add(i as u64 * BATCH_SEED_OFFSET),
            ));
        }

        // Individual pushes
        let mut cache_individual = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        for i in 0..BATCH_TEST_COUNT {
            cache_individual
                .push(TEST_LAYER, &key_vecs[i], &val_vecs[i])
                .unwrap();
        }

        // Batch push
        let mut cache_batch = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let key_refs: Vec<&[f32]> = key_vecs.iter().map(|v| v.as_slice()).collect();
        let val_refs: Vec<&[f32]> = val_vecs.iter().map(|v| v.as_slice()).collect();
        cache_batch
            .push_batch(TEST_LAYER, &key_refs, &val_refs)
            .unwrap();

        // Verify same entry count
        assert_eq!(
            cache_individual.entry_count(TEST_LAYER),
            cache_batch.entry_count(TEST_LAYER),
        );

        // Verify dequantized keys are identical
        let keys_ind = cache_individual.dequantize_all_keys(TEST_LAYER).unwrap();
        let keys_bat = cache_batch.dequantize_all_keys(TEST_LAYER).unwrap();
        assert_eq!(keys_ind.len(), keys_bat.len());
        for (a, b) in keys_ind.iter().zip(keys_bat.iter()) {
            assert_eq!(a, b, "batch and individual key dequantizations differ");
        }

        // Verify dequantized values are identical
        let vals_ind = cache_individual.dequantize_all_values(TEST_LAYER).unwrap();
        let vals_bat = cache_batch.dequantize_all_values(TEST_LAYER).unwrap();
        assert_eq!(vals_ind.len(), vals_bat.len());
        for (a, b) in vals_ind.iter().zip(vals_bat.iter()) {
            assert_eq!(a, b, "batch and individual value dequantizations differ");
        }
    }

    #[test]
    fn push_batch_empty_is_noop() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let empty_keys: Vec<&[f32]> = Vec::new();
        let empty_vals: Vec<&[f32]> = Vec::new();
        cache
            .push_batch(TEST_LAYER, &empty_keys, &empty_vals)
            .unwrap();
        assert_eq!(cache.entry_count(TEST_LAYER), 0);
    }

    #[test]
    fn push_batch_rejects_invalid_layer() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        let keys: Vec<&[f32]> = vec![&key];
        let vals: Vec<&[f32]> = vec![&val];
        let result = cache.push_batch(INVALID_LAYER, &keys, &vals);
        assert!(result.is_err());
    }

    #[test]
    fn push_batch_rejects_wrong_dimension() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let wrong_key = vec![1.0_f32; TEST_DIM + 1];
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        let keys: Vec<&[f32]> = vec![wrong_key.as_slice()];
        let vals: Vec<&[f32]> = vec![val.as_slice()];
        let result = cache.push_batch(TEST_LAYER, &keys, &vals);
        assert!(result.is_err());
    }

    #[test]
    fn push_batch_rejects_mismatched_lengths() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val1 = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        let val2 = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED_2);
        let keys: Vec<&[f32]> = vec![key.as_slice()];
        let vals: Vec<&[f32]> = vec![val1.as_slice(), val2.as_slice()];
        let result = cache.push_batch(TEST_LAYER, &keys, &vals);
        assert!(result.is_err());
    }

    #[test]
    fn push_batch_single_element_matches_push() {
        let config = test_config();
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);

        let mut cache_push = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        cache_push.push(TEST_LAYER, &key, &val).unwrap();

        let mut cache_batch = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let keys: Vec<&[f32]> = vec![key.as_slice()];
        let vals: Vec<&[f32]> = vec![val.as_slice()];
        cache_batch.push_batch(TEST_LAYER, &keys, &vals).unwrap();

        assert_eq!(
            cache_push.entry_count(TEST_LAYER),
            cache_batch.entry_count(TEST_LAYER)
        );

        let k_push = cache_push.dequantize_all_keys(TEST_LAYER).unwrap();
        let k_batch = cache_batch.dequantize_all_keys(TEST_LAYER).unwrap();
        assert_eq!(k_push, k_batch);

        let v_push = cache_push.dequantize_all_values(TEST_LAYER).unwrap();
        let v_batch = cache_batch.dequantize_all_values(TEST_LAYER).unwrap();
        assert_eq!(v_push, v_batch);
    }

    #[test]
    fn push_batch_then_individual_push_consistent() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);

        // First: batch push of 3 entries
        let mut key_vecs: Vec<Vec<f32>> = Vec::new();
        let mut val_vecs: Vec<Vec<f32>> = Vec::new();
        for i in 0..3 {
            key_vecs.push(pseudo_random_vec(
                TEST_DIM,
                TEST_KEY_SEED.wrapping_add(i as u64 * BATCH_SEED_OFFSET),
            ));
            val_vecs.push(pseudo_random_vec(
                TEST_DIM,
                TEST_VALUE_SEED.wrapping_add(i as u64 * BATCH_SEED_OFFSET),
            ));
        }
        let key_refs: Vec<&[f32]> = key_vecs.iter().map(|v| v.as_slice()).collect();
        let val_refs: Vec<&[f32]> = val_vecs.iter().map(|v| v.as_slice()).collect();
        cache.push_batch(TEST_LAYER, &key_refs, &val_refs).unwrap();

        assert_eq!(cache.entry_count(TEST_LAYER), 3);

        // Then: individual push of 1 more entry
        let extra_key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED_3);
        let extra_val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED_3);
        cache.push(TEST_LAYER, &extra_key, &extra_val).unwrap();

        assert_eq!(cache.entry_count(TEST_LAYER), 4);

        // Dequantize should give 4 entries
        let all_keys = cache.dequantize_all_keys(TEST_LAYER).unwrap();
        assert_eq!(all_keys.len(), 4);
    }

    // -- GPU export/import helper tests --------------------------------------

    #[test]
    fn collect_packed_data_empty() {
        let (bytes, scales) = collect_packed_data(&[]);
        assert!(bytes.is_empty());
        assert!(scales.is_empty());
    }

    #[test]
    fn collect_packed_data_roundtrip() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        cache.push(TEST_LAYER, &key, &val).unwrap();

        // Export via collect_packed_data (tested function)
        let (packed, scales) = cache.export_packed_range(TEST_LAYER, 0, 1, true).unwrap();
        assert!(!packed.is_empty());
        assert_eq!(scales.len(), 1);

        // The exported data should allow reconstruction
        let key2 = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED_2);
        let val2 = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED_2);
        cache.push(TEST_LAYER, &key2, &val2).unwrap();
        let (packed2, scales2) = cache.export_packed_range(TEST_LAYER, 0, 2, true).unwrap();
        assert_eq!(scales2.len(), 2);
        // First block's packed data should match
        let bytes_per_block = packed.len();
        assert_eq!(&packed2[..bytes_per_block], &packed[..]);
    }

    #[test]
    fn reconstruct_block_preserves_data() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        cache.push(TEST_LAYER, &key, &val).unwrap();

        // Export, then reconstruct via import
        let (packed, scales) = cache.export_packed_range(TEST_LAYER, 0, 1, true).unwrap();
        let bytes_per_block = packed.len();
        const BITS_PER_BYTE: usize = 8;
        let signs_per_block = TEST_DIM.div_ceil(BITS_PER_BYTE);
        // Need QJL signs too — create dummy ones for the test
        let qjl_signs = vec![0u8; signs_per_block];
        let residual_norms = vec![0u16; 1];

        let import = PackedImport {
            layer: TEST_LAYER,
            polar_bits: BITS_3 - 1,
            packed_bytes: &packed,
            scales: &scales,
            qjl_signs_flat: &qjl_signs,
            residual_norms: &residual_norms,
            bytes_per_block,
            signs_per_block,
            is_keys: false,
        };
        let block = reconstruct_block(&import, 0);
        assert_eq!(block.polar_block.packed_indices, &packed[..]);
        assert_eq!(block.polar_block.scale.to_bits(), scales[0]);
    }

    #[test]
    fn select_blocks_returns_correct_side() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        cache.push(TEST_LAYER, &key, &val).unwrap();

        let keys = cache.select_blocks(TEST_LAYER, true);
        let vals = cache.select_blocks(TEST_LAYER, false);
        assert_eq!(keys.len(), 1);
        assert_eq!(vals.len(), 1);

        // Keys and values should have different packed data (different input vectors)
        assert_ne!(
            keys[0].polar_block.packed_indices,
            vals[0].polar_block.packed_indices
        );
    }

    #[test]
    fn select_blocks_mut_allows_push() {
        let config = test_config();
        let mut cache = QuantizedKVCache::new(config, TEST_NUM_LAYERS, TEST_QJL_SEED);
        assert_eq!(cache.select_blocks(TEST_LAYER, true).len(), 0);

        // Push via the normal API
        let key = pseudo_random_vec(TEST_DIM, TEST_KEY_SEED);
        let val = pseudo_random_vec(TEST_DIM, TEST_VALUE_SEED);
        cache.push(TEST_LAYER, &key, &val).unwrap();

        // select_blocks_mut should see the entry
        let keys_mut = cache.select_blocks_mut(TEST_LAYER, true);
        assert_eq!(keys_mut.len(), 1);
    }
}