qdrant-edge 0.7.2

A lightweight, in-process vector search engine designed for embedded devices, autonomous systems, and mobile agents.
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
use std::alloc::Layout;
use std::borrow::Cow;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};

use crate::common::counter::hardware_counter::HardwareCounterCell;
use crate::common::fs::atomic_save_json;
use crate::common::mmap::MmapFlusher;
#[expect(deprecated, reason = "legacy code")]
use crate::common::mmap::{transmute_from_u8_to_slice, transmute_to_u8_slice};
use crate::common::typelevel::True;
use crate::common::types::PointOffsetType;
use fs_err as fs;
use serde::{Deserialize, Serialize};
use strum::EnumIter;

use crate::quantization::encoded_vectors::validate_vector_parameters;
use crate::quantization::vector_stats::{VectorElementStats, VectorStats};
use crate::quantization::{
    DistanceType, EncodedStorage, EncodedStorageBuilder, EncodedVectors, EncodingError,
    VectorParameters,
};

pub struct EncodedVectorsBin<TBitsStoreType: BitsStoreType, TStorage: EncodedStorage> {
    encoded_vectors: TStorage,
    metadata: Metadata,
    metadata_path: Option<PathBuf>,
    bits_store_type: PhantomData<TBitsStoreType>,
}

#[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize, Deserialize, Default, EnumIter)]
pub enum Encoding {
    #[default]
    OneBit,
    TwoBits,
    OneAndHalfBits,
}

impl Encoding {
    pub fn is_one(&self) -> bool {
        matches!(self, Encoding::OneBit)
    }
}

#[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize, Deserialize, Default, EnumIter)]
pub enum QueryEncoding {
    #[default]
    SameAsStorage,
    Scalar4bits,
    Scalar8bits,
}

impl QueryEncoding {
    pub fn is_same_as_storage(&self) -> bool {
        matches!(self, QueryEncoding::SameAsStorage)
    }
}

pub enum EncodedQueryBQ<TBitsStoreType: BitsStoreType> {
    Binary(EncodedBinVector<TBitsStoreType>),
    Scalar4bits(EncodedScalarVector<TBitsStoreType>),
    Scalar8bits(EncodedScalarVector<TBitsStoreType>),
}

pub struct EncodedBinVector<TBitsStoreType: BitsStoreType> {
    encoded_vector: Vec<TBitsStoreType>,
}

/// Transposed Scalar Encoded Vector
///
/// This data structure represents a scalar-encoded vector optimized for efficient scoring
/// against Binary Quantized (BQ) vectors through bit-level transposition.
///
/// STANDARD ENCODING:
/// A regular scalar vector [float_1, float_2, ..., float_n] is quantized to
/// [scalar_1, scalar_2, ..., scalar_n], where each scalar_i is a u8 value.
///
/// PERFORMANCE ISSUE:
/// Standard encoding is inefficient for scoring because it requires extracting
/// individual bits from each BQ vector in the dataset to perform XOR operations
/// with the scalar vector.
///
/// TRANSPOSITION OPTIMIZATION:
/// To improve scoring efficiency, we reorganize the data using bit-level transposition:
///
/// 1. Take the encoded scalar vector [scalar_1, scalar_2, ..., scalar_n]
/// 2. Divide into batches of size sizeof::<TBitsStoreType>() = N:
///    [[scalar_1, scalar_2, ..., scalar_N], [scalar_N+1, ...], ...]
/// 3. Transpose bit positions within each batch:
///    - Store all first bits: [scalar_1[0], scalar_2[0], ..., scalar_N[0]]
///    - Store all second bits: [scalar_1[1], scalar_2[1], ..., scalar_N[1]]
///    - Continue for all bit positions...
///
/// SCORING ADVANTAGE:
/// This layout enables efficient batch operations:
/// - Extract a single TBitsStoreType value from the BQ vector
/// - Perform N parallel operations with corresponding scalar bits
/// - Use shift operations to compute the final score:
///   (scalar_1[0] ^ bq_vector[0] + ) << 0 +
///   (scalar_1[0] ^ bq_vector[0] + ) << 1 +
///   (scalar_1[0] ^ bq_vector[0] + ) << 2 ...
///
/// This eliminates the need to extract individual bits from BQ vectors during scoring.
/// This idea was taken from http://arxiv.org/pdf/2405.12497, see Figure 2.
pub struct EncodedScalarVector<TBitsStoreType: BitsStoreType> {
    pub encoded_vector: Vec<TBitsStoreType>,
}

#[derive(Serialize, Deserialize)]
struct Metadata {
    vector_parameters: VectorParameters,
    #[serde(default)]
    #[serde(skip_serializing_if = "Encoding::is_one")]
    encoding: Encoding,
    #[serde(default)]
    #[serde(skip_serializing_if = "QueryEncoding::is_same_as_storage")]
    query_encoding: QueryEncoding,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    vector_stats: Option<VectorStats>,
}

pub trait BitsStoreType:
    Default
    + Copy
    + Clone
    + core::ops::BitOrAssign
    + std::ops::Shl<usize, Output = Self>
    + std::ops::Shr<usize, Output = Self>
    + std::ops::BitAnd<Output = Self>
    + num_traits::identities::One
    + num_traits::cast::FromPrimitive
    + num_traits::cast::ToPrimitive
    + bytemuck::Pod
    + std::fmt::Debug
{
    /// Xor vectors and return the number of bits set to 1
    ///
    /// Assume that `v1` and `v2` are aligned to `BITS_STORE_TYPE_SIZE` with both with zeros
    /// So it does not affect the resulting number of bits set to 1
    fn xor_popcnt(v1: &[Self], v2: &[Self]) -> usize;

    /// Calculate score between BQ encoded vector and `EncodedScalarVector<Self>` query.
    ///
    /// It calculates sum of XOR popcount between each bit of the `vector` and the corresponding scalar value in the `query`.
    /// XOR between scalar and bit is a XOR for each bit of the scalar value.
    /// See `EncodedScalarVector` docs for more details about the transposition optimization to avoid extracting bits from BQ vectors.
    fn xor_popcnt_scalar(vector: &[Self], query: &[Self], query_bits_count: usize) -> usize;

    /// Estimates how many `StorageType` elements are needed to store `size` bits
    fn get_storage_size(size: usize) -> usize;
}

impl BitsStoreType for u8 {
    fn xor_popcnt(v1: &[Self], v2: &[Self]) -> usize {
        debug_assert!(v1.len() == v2.len());

        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        if is_x86_feature_detected!("sse4.2") {
            unsafe {
                if v1.len() > 16 {
                    return impl_xor_popcnt_sse_uint128(
                        v1.as_ptr(),
                        v2.as_ptr(),
                        (v1.len() as u32) / 16,
                    ) as usize;
                } else if v1.len() > 8 {
                    return impl_xor_popcnt_sse_uint64(
                        v1.as_ptr(),
                        v2.as_ptr(),
                        (v1.len() as u32) / 8,
                    ) as usize;
                } else if v1.len() > 4 {
                    return impl_xor_popcnt_sse_uint32(
                        v1.as_ptr(),
                        v2.as_ptr(),
                        (v1.len() as u32) / 4,
                    ) as usize;
                }
            }
        }

        #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
        if std::arch::is_aarch64_feature_detected!("neon") {
            unsafe {
                if v1.len() > 16 {
                    return impl_xor_popcnt_neon_uint128(
                        v1.as_ptr(),
                        v2.as_ptr(),
                        (v1.len() as u32) / 16,
                    ) as usize;
                } else if v1.len() > 8 {
                    return impl_xor_popcnt_neon_uint64(
                        v1.as_ptr(),
                        v2.as_ptr(),
                        (v1.len() as u32) / 8,
                    ) as usize;
                }
            }
        }

        let mut result = 0;
        for (&b1, &b2) in v1.iter().zip(v2.iter()) {
            result += (b1 ^ b2).count_ones() as usize;
        }
        result
    }

    fn xor_popcnt_scalar(vector: &[Self], query: &[Self], query_bits_count: usize) -> usize {
        debug_assert!(query.len() >= vector.len() * query_bits_count);

        #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
        if std::arch::is_aarch64_feature_detected!("neon") {
            if query_bits_count == 8 {
                unsafe {
                    return impl_xor_popcnt_scalar8_neon_u8(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            } else if query_bits_count == 4 {
                unsafe {
                    return impl_xor_popcnt_scalar4_neon_u8(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            }
        }

        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        if is_x86_feature_detected!("sse4.2") {
            if query_bits_count == 8 {
                unsafe {
                    return impl_xor_popcnt_scalar8_sse_u8(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            } else if query_bits_count == 4 {
                unsafe {
                    return impl_xor_popcnt_scalar4_sse_u8(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            }
        }

        let mut result = 0;
        for (&b1, b2_chunk) in vector.iter().zip(query.chunks_exact(query_bits_count)) {
            for (i, &b2) in b2_chunk.iter().enumerate() {
                result += (b1 ^ b2).count_ones() << i;
            }
        }
        result as usize
    }

    fn get_storage_size(size: usize) -> usize {
        let bytes_count = if size > 128 {
            std::mem::size_of::<u128>()
        } else if size > 64 {
            std::mem::size_of::<u64>()
        } else if size > 32 {
            std::mem::size_of::<u32>()
        } else {
            std::mem::size_of::<u8>()
        };

        let bits_count = u8::BITS as usize * bytes_count;
        let mut result = size / bits_count;
        if !size.is_multiple_of(bits_count) {
            result += 1;
        }
        result * bytes_count
    }
}

impl BitsStoreType for u128 {
    fn xor_popcnt(v1: &[Self], v2: &[Self]) -> usize {
        debug_assert!(v1.len() == v2.len());

        #[cfg(target_arch = "x86_64")]
        if is_x86_feature_detected!("avx512vl")
            && is_x86_feature_detected!("avx512vpopcntdq")
            && is_x86_feature_detected!("avx2")
            && is_x86_feature_detected!("avx")
            && is_x86_feature_detected!("sse4.1")
            && is_x86_feature_detected!("sse2")
        {
            unsafe {
                return impl_xor_popcnt_avx512_uint128(
                    v1.as_ptr().cast::<u8>(),
                    v2.as_ptr().cast::<u8>(),
                    v1.len() as u32,
                ) as usize;
            }
        }

        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        if is_x86_feature_detected!("sse4.2") {
            unsafe {
                return impl_xor_popcnt_sse_uint128(
                    v1.as_ptr().cast::<u8>(),
                    v2.as_ptr().cast::<u8>(),
                    v1.len() as u32,
                ) as usize;
            }
        }

        #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
        if std::arch::is_aarch64_feature_detected!("neon") {
            unsafe {
                return impl_xor_popcnt_neon_uint128(
                    v1.as_ptr().cast::<u8>(),
                    v2.as_ptr().cast::<u8>(),
                    v1.len() as u32,
                ) as usize;
            }
        }

        let mut result = 0;
        for (&b1, &b2) in v1.iter().zip(v2.iter()) {
            result += (b1 ^ b2).count_ones() as usize;
        }
        result
    }

    fn xor_popcnt_scalar(vector: &[Self], query: &[Self], query_bits_count: usize) -> usize {
        debug_assert!(query.len() >= vector.len() * query_bits_count);

        #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
        if std::arch::is_aarch64_feature_detected!("neon") {
            if query_bits_count == 8 {
                unsafe {
                    return impl_xor_popcnt_scalar8_neon_uint128(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            } else if query_bits_count == 4 {
                unsafe {
                    return impl_xor_popcnt_scalar4_neon_uint128(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            }
        }

        #[cfg(target_arch = "x86_64")]
        if is_x86_feature_detected!("avx2") && is_x86_feature_detected!("sse4.2") {
            if query_bits_count == 8 {
                unsafe {
                    return impl_xor_popcnt_scalar8_avx_uint128(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            } else if query_bits_count == 4 {
                unsafe {
                    return impl_xor_popcnt_scalar4_avx_uint128(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            }
        }

        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        if is_x86_feature_detected!("sse4.2") {
            if query_bits_count == 8 {
                unsafe {
                    return impl_xor_popcnt_scalar8_sse_uint128(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            } else if query_bits_count == 4 {
                unsafe {
                    return impl_xor_popcnt_scalar4_sse_uint128(
                        query.as_ptr().cast::<u8>(),
                        vector.as_ptr().cast::<u8>(),
                        vector.len() as u32,
                    ) as usize;
                }
            }
        }

        let mut result = 0;
        for (&b1, b2_chunk) in vector.iter().zip(query.chunks_exact(query_bits_count)) {
            for (i, &b2) in b2_chunk.iter().enumerate() {
                result += (b1 ^ b2).count_ones() << i;
            }
        }
        result as usize
    }

    fn get_storage_size(size: usize) -> usize {
        let bits_count = u8::BITS as usize * std::mem::size_of::<Self>();
        let mut result = size / bits_count;
        if !size.is_multiple_of(bits_count) {
            result += 1;
        }
        result
    }
}

impl<TBitsStoreType: BitsStoreType, TStorage: EncodedStorage>
    EncodedVectorsBin<TBitsStoreType, TStorage>
{
    pub fn storage(&self) -> &TStorage {
        &self.encoded_vectors
    }

    pub fn encode<'a>(
        orig_data: impl Iterator<Item = impl AsRef<[f32]> + 'a> + Clone,
        mut storage_builder: impl EncodedStorageBuilder<Storage = TStorage>,
        vector_parameters: &VectorParameters,
        encoding: Encoding,
        query_encoding: QueryEncoding,
        meta_path: Option<&Path>,
        stopped: &AtomicBool,
    ) -> Result<Self, EncodingError> {
        debug_assert!(validate_vector_parameters(orig_data.clone(), vector_parameters).is_ok());

        let storage_encoding_needs_states = match encoding {
            Encoding::OneBit => false,
            // Requires stats for bit boundaries
            Encoding::TwoBits | Encoding::OneAndHalfBits => true,
        };

        let query_encoding_needs_stats = match query_encoding {
            QueryEncoding::SameAsStorage => storage_encoding_needs_states,
            QueryEncoding::Scalar4bits | QueryEncoding::Scalar8bits => true,
        };

        let vector_stats = if storage_encoding_needs_states || query_encoding_needs_stats {
            Some(VectorStats::build(orig_data.clone(), vector_parameters.dim))
        } else {
            None
        };

        for vector in orig_data {
            if stopped.load(Ordering::Relaxed) {
                return Err(EncodingError::Stopped);
            }

            let encoded_vector = Self::encode_vector(vector.as_ref(), &vector_stats, encoding);
            let encoded_vector_slice = encoded_vector.encoded_vector.as_slice();
            // TODO Safety: bytemuck::Pod type, but is it enough for slice?
            #[expect(deprecated, reason = "legacy code")]
            let bytes = unsafe { transmute_to_u8_slice(encoded_vector_slice) };
            storage_builder.push_vector_data(bytes).map_err(|e| {
                EncodingError::EncodingError(format!("Failed to push encoded vector: {e}",))
            })?;
        }

        let encoded_vectors = storage_builder
            .build()
            .map_err(|e| EncodingError::EncodingError(format!("Failed to build storage: {e}",)))?;

        let metadata = Metadata {
            vector_parameters: *vector_parameters,
            encoding,
            query_encoding,
            vector_stats,
        };
        if let Some(meta_path) = meta_path {
            meta_path
                .parent()
                .ok_or_else(|| {
                    std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "Path must have a parent directory",
                    )
                })
                .and_then(fs::create_dir_all)
                .map_err(|e| {
                    EncodingError::EncodingError(format!(
                        "Failed to create metadata directory: {e}",
                    ))
                })?;
            atomic_save_json(meta_path, &metadata).map_err(|e| {
                EncodingError::EncodingError(format!("Failed to save metadata: {e}",))
            })?;
        }

        Ok(Self {
            encoded_vectors,
            metadata,
            metadata_path: meta_path.map(PathBuf::from),
            bits_store_type: PhantomData,
        })
    }

    pub fn load(encoded_vectors: TStorage, meta_path: &Path) -> std::io::Result<Self> {
        let contents = fs::read_to_string(meta_path)?;
        let metadata: Metadata = serde_json::from_str(&contents)?;
        let result = Self {
            metadata,
            metadata_path: Some(meta_path.to_path_buf()),
            encoded_vectors,
            bits_store_type: PhantomData,
        };
        Ok(result)
    }

    fn encode_vector(
        vector: &[f32],
        vector_stats: &Option<VectorStats>,
        encoding: Encoding,
    ) -> EncodedBinVector<TBitsStoreType> {
        let encoded_vector_size =
            get_quantized_vector_size_from_params::<TBitsStoreType>(vector.len(), encoding)
                / std::mem::size_of::<TBitsStoreType>();
        let mut encoded_vector = vec![Default::default(); encoded_vector_size];

        match encoding {
            Encoding::OneBit => Self::encode_one_bit_vector(vector, &mut encoded_vector),
            Encoding::TwoBits => {
                Self::encode_two_bits_vector(vector, &mut encoded_vector, vector_stats)
            }
            Encoding::OneAndHalfBits => {
                Self::encode_one_and_half_bits_vector(vector, &mut encoded_vector, vector_stats)
            }
        }

        EncodedBinVector { encoded_vector }
    }

    fn encode_one_bit_vector(vector: &[f32], encoded_vector: &mut [TBitsStoreType]) {
        let bits_count = u8::BITS as usize * std::mem::size_of::<TBitsStoreType>();
        let one = TBitsStoreType::one();
        for (i, &v) in vector.iter().enumerate() {
            // flag is true if the value is positive
            // It's expected that the vector value is in range [-1; 1]
            if v > 0.0 {
                encoded_vector[i / bits_count] |= one << (i % bits_count);
            }
        }
    }

    fn encode_two_bits_vector(
        vector: &[f32],
        encoded_vector: &mut [TBitsStoreType],
        vector_stats: &Option<VectorStats>,
    ) {
        let bits_count = u8::BITS as usize * std::mem::size_of::<TBitsStoreType>();
        let one = TBitsStoreType::one();
        for i in 0..vector.len() {
            let value = vector[i];
            let stats = vector_stats.as_ref().map(|stats| &stats.elements_stats[i]);
            let (b1, b2) = Self::encode_two_bits_value(value, stats);
            if b1 {
                encoded_vector[i / bits_count] |= one << (i % bits_count);
            }
            if b2 {
                let j = vector.len() + i;
                encoded_vector[j / bits_count] |= one << (j % bits_count);
            }
        }
    }

    fn encode_one_and_half_bits_vector(
        vector: &[f32],
        encoded_vector: &mut [TBitsStoreType],
        vector_stats: &Option<VectorStats>,
    ) {
        // One and half bit encoding is a 2bit quantization but first bit,
        // which describes that value is less that sigma,
        // is united with the bit from the next value using OR operand.
        // Scoring for 1.5bit quantization is the same as for 2bit and 1bit quantization.
        //
        // Example 1:
        // `Value1` has `[1,0]` 2bits encoding, value `Value2` has `[1,1]` 2bits encoding.
        // The resulting 1.5bit encoding will be `[value1[0], value2[0], value1[1] | value2[1]] = [1, 1, 1]`.
        //
        // Example 2:
        // `Value1` has `[0,0]` 2bits encoding, value `Value2` has `[1,0]` 2bits encoding.
        // The resulting 1.5bit encoding will be `[value1[0], value2[0], value1[1] | value2[1]] = [0, 1, 0]`.
        let bits_count = u8::BITS as usize * std::mem::size_of::<TBitsStoreType>();
        let one = TBitsStoreType::one();
        for i in 0..vector.len() {
            let value = vector[i];
            let stats = vector_stats.as_ref().map(|stats| &stats.elements_stats[i]);
            let (b1, b2) = Self::encode_two_bits_value(value, stats);
            if b1 {
                encoded_vector[i / bits_count] |= one << (i % bits_count);
            }
            if b2 {
                let j = vector.len() + i / 2;
                encoded_vector[j / bits_count] |= one << (j % bits_count);
            }
        }
    }

    fn encode_two_bits_value(
        value: f32,
        element_stats: Option<&VectorElementStats>,
    ) -> (bool, bool) {
        // Two bit encoding is a regular BQ with "zero".
        // It uses 2 bits per value and encodes values in the following way:
        // 00 - if the value is in the range (-inf; -SIGMAS];
        // 10 - if the value is in the range (-SIGMAS; SIGMAS);
        // 11 - if the value is in the range [SIGMAS; +inf);
        // where sigma is the standard deviation of the value.
        //
        // Scoring for 2bit quantization is the same as for 1bit quantization.
        let Some(element_stats) = element_stats else {
            return if value > 0.0 {
                (true, true)
            } else {
                (false, false)
            };
        };
        let VectorElementStats {
            min: _,
            max: _,
            mean,
            stddev,
        } = element_stats;

        let sd = *stddev;

        if sd < f32::EPSILON {
            // If standard deviation is zero,
            // we cannot calculate z-score count so use regular BQ with zero-comparison.
            return (value > 0.0, false);
        }

        // Calculate z-score for the value
        let v_z = (value - mean) / sd;

        // Define sigmas count which describes a zero range for 2bit encoding.
        const SIGMAS: f32 = 2.0 / 3.0;

        if v_z <= -SIGMAS {
            (false, false) // (-inf; -SIGMAS]
        } else if v_z < SIGMAS {
            (true, false) // (-SIGMAS; SIGMAS)
        } else {
            (true, true) // [SIGMAS; +inf)
        }
    }

    fn encode_query_vector(
        query: &[f32],
        vector_stats: &Option<VectorStats>,
        encoding: Encoding,
        query_encoding: QueryEncoding,
    ) -> EncodedQueryBQ<TBitsStoreType> {
        match query_encoding {
            QueryEncoding::SameAsStorage => {
                EncodedQueryBQ::Binary(Self::encode_vector(query, vector_stats, encoding))
            }
            QueryEncoding::Scalar8bits => EncodedQueryBQ::Scalar8bits(
                Self::encode_scalar_query_vector(query, encoding, u8::BITS as usize),
            ),
            QueryEncoding::Scalar4bits => EncodedQueryBQ::Scalar4bits(
                Self::encode_scalar_query_vector(query, encoding, (u8::BITS / 2) as usize),
            ),
        }
    }

    fn encode_scalar_query_vector(
        query: &[f32],
        encoding: Encoding,
        bits_count: usize,
    ) -> EncodedScalarVector<TBitsStoreType> {
        match encoding {
            Encoding::OneBit => Self::_encode_scalar_query_vector(query, bits_count),
            Encoding::TwoBits => {
                // For two bits encoding we need to extend the query vector
                let mut extended_query = Vec::with_capacity(query.len() * 2);
                // Copy the original query vector twice: for first and second bits in 2bit BQ encoding
                extended_query.extend_from_slice(query);
                extended_query.extend_from_slice(query);
                Self::_encode_scalar_query_vector(&extended_query, bits_count)
            }
            Encoding::OneAndHalfBits => {
                // For one and half bits encoding we need to extend the query vector
                let mut extended_query = Vec::with_capacity(query.len() + query.len().div_ceil(2));
                extended_query.extend_from_slice(query);
                // For 1.5bit BQ use max of two consecutive values
                extended_query.extend(
                    query
                        .chunks(2)
                        .map(|v| if v.len() == 2 { v[0].max(v[1]) } else { v[0] }),
                );
                Self::_encode_scalar_query_vector(&extended_query, bits_count)
            }
        }
    }

    fn _encode_scalar_query_vector(
        query: &[f32],
        bits_count: usize,
    ) -> EncodedScalarVector<TBitsStoreType> {
        let encoded_query_size = TBitsStoreType::get_storage_size(query.len().max(1)) * bits_count;
        let mut encoded_query: Vec<TBitsStoreType> = vec![Default::default(); encoded_query_size];

        let max_abs_value = query.iter().map(|x| x.abs()).fold(0.0, f32::max);
        let (min, max) = (-max_abs_value, max_abs_value);

        let ranges = (1usize << bits_count) - 1;
        let delta = (max - min) / ranges as f32;

        let storage_bits_count = std::mem::size_of::<TBitsStoreType>() * u8::BITS as usize;
        for (chunk_index, chunk) in query.chunks(storage_bits_count).enumerate() {
            for (shift, value) in chunk.iter().enumerate() {
                let shifted_value = value - min;
                let delted_value = if delta > f32::EPSILON {
                    shifted_value / delta
                } else {
                    0.0
                };
                let rounded_value = delted_value.round() as usize;
                let quantized = rounded_value % (ranges + 1);
                let quantized = TBitsStoreType::from_usize(quantized).unwrap_or_default();
                for b in 0..bits_count {
                    let bit_value = ((quantized >> b) & TBitsStoreType::one()) << shift;
                    encoded_query[bits_count * chunk_index + b] |= bit_value;
                }
            }
        }

        EncodedScalarVector {
            encoded_vector: encoded_query,
        }
    }

    fn get_quantized_vector_size(&self) -> usize {
        get_quantized_vector_size_from_params::<TBitsStoreType>(
            self.metadata.vector_parameters.dim,
            self.metadata.encoding,
        )
    }

    fn calculate_metric(
        &self,
        vector: &[TBitsStoreType],
        query: &[TBitsStoreType],
        query_bits_count: usize,
    ) -> f32 {
        // Dot product in a range [-1; 1] is approximated by NXOR in a range [0; 1]
        // L1 distance in range [-1; 1] (alpha=2) is approximated by alpha*XOR in a range [0; 1]
        // L2 distance in range [-1; 1] (alpha=2) is approximated by alpha*sqrt(XOR) in a range [0; 1]
        // For example:

        // |  A   |  B   | Dot product | L1 | L2 |
        // | -0.5 | -0.5 |  0.25       | 0  | 0  |
        // | -0.5 |  0.5 | -0.25       | 1  | 1  |
        // |  0.5 | -0.5 | -0.25       | 1  | 1  |
        // |  0.5 |  0.5 |  0.25       | 0  | 0  |

        // | A | B | NXOR | XOR
        // | 0 | 0 | 1    | 0
        // | 0 | 1 | 0    | 1
        // | 1 | 0 | 0    | 1
        // | 1 | 1 | 1    | 0

        let xor_product = if query_bits_count == 1 {
            TBitsStoreType::xor_popcnt(vector, query) as f32
        } else {
            let xor_product = TBitsStoreType::xor_popcnt_scalar(vector, query, query_bits_count);
            (xor_product as f32) / (((1 << query_bits_count) - 1) as f32)
        };

        let dim = self.metadata.vector_parameters.dim as f32;
        let zeros_count = dim - xor_product;

        match (
            self.metadata.vector_parameters.distance_type,
            self.metadata.vector_parameters.invert,
        ) {
            // So if `invert` is true we return XOR, otherwise we return (dim - XOR)
            (DistanceType::Dot | DistanceType::Cosine, true) => xor_product - zeros_count,
            (DistanceType::Dot | DistanceType::Cosine, false) => zeros_count - xor_product,
            // This also results in exact ordering as L1 and L2 but reversed.
            (DistanceType::L1 | DistanceType::L2, true) => zeros_count - xor_product,
            (DistanceType::L1 | DistanceType::L2, false) => xor_product - zeros_count,
        }
    }

    pub fn get_quantized_vector(&self, i: PointOffsetType) -> Cow<'_, [u8]> {
        self.encoded_vectors.get_vector_data(i as _)
    }

    pub fn layout(&self) -> Layout {
        Layout::from_size_align(
            self.get_quantized_vector_size(),
            align_of::<TBitsStoreType>(),
        )
        .unwrap()
    }

    pub fn get_vector_parameters(&self) -> &VectorParameters {
        &self.metadata.vector_parameters
    }
}

pub fn get_quantized_vector_size_from_params<TBitsStoreType: BitsStoreType>(
    dim: usize,
    encoding: Encoding,
) -> usize {
    let extended_dim = match encoding {
        Encoding::OneBit => dim,
        Encoding::TwoBits => dim * 2,
        Encoding::OneAndHalfBits => (dim * 3).div_ceil(2), // ceil(dim * 1.5)
    };
    TBitsStoreType::get_storage_size(extended_dim.max(1)) * std::mem::size_of::<TBitsStoreType>()
}

impl<TBitsStoreType: BitsStoreType, TStorage: EncodedStorage> EncodedVectors
    for EncodedVectorsBin<TBitsStoreType, TStorage>
{
    type EncodedQuery = EncodedQueryBQ<TBitsStoreType>;

    fn is_in_ram_or_mmap() -> bool {
        TStorage::is_in_ram_or_mmap()
    }

    fn is_on_disk(&self) -> bool {
        self.encoded_vectors.is_on_disk()
    }

    fn encode_query(&self, query: &[f32]) -> EncodedQueryBQ<TBitsStoreType> {
        debug_assert!(query.len() == self.metadata.vector_parameters.dim);
        Self::encode_query_vector(
            query,
            &self.metadata.vector_stats,
            self.metadata.encoding,
            self.metadata.query_encoding,
        )
    }

    fn iter_batch(
        &self,
        offsets: &[PointOffsetType],
    ) -> impl Iterator<Item = (usize, Cow<'_, [u8]>)> {
        self.encoded_vectors.iter_batch(offsets)
    }

    fn score(
        &self,
        query: &Self::EncodedQuery,
        encoded_vector: &[u8],
        hw_counter: &HardwareCounterCell,
    ) -> f32 {
        self.score_bytes(True, query, encoded_vector, hw_counter)
    }

    fn score_point(
        &self,
        query: &EncodedQueryBQ<TBitsStoreType>,
        i: PointOffsetType,
        hw_counter: &HardwareCounterCell,
    ) -> f32 {
        let vector_data = self.encoded_vectors.get_vector_data(i);

        self.score_bytes(True, query, &vector_data, hw_counter)
    }

    fn score_internal(
        &self,
        i: PointOffsetType,
        j: PointOffsetType,
        hw_counter: &HardwareCounterCell,
    ) -> f32 {
        let vector_data_1 = self.encoded_vectors.get_vector_data(i);
        let vector_data_2 = self.encoded_vectors.get_vector_data(j);

        hw_counter
            .vector_io_read()
            .incr_delta(vector_data_1.len() + vector_data_2.len());

        // TODO Safety
        #[expect(deprecated, reason = "legacy code")]
        let vector_data_usize_1 = unsafe { transmute_from_u8_to_slice(&vector_data_1) };
        // TODO Safety
        #[expect(deprecated, reason = "legacy code")]
        let vector_data_usize_2 = unsafe { transmute_from_u8_to_slice(&vector_data_2) };

        hw_counter
            .cpu_counter()
            .incr_delta(vector_data_usize_2.len());

        self.calculate_metric(vector_data_usize_1, vector_data_usize_2, 1)
    }

    fn quantized_vector_size(&self) -> usize {
        self.get_quantized_vector_size()
    }

    fn encode_internal_vector(
        &self,
        id: PointOffsetType,
    ) -> Option<EncodedQueryBQ<TBitsStoreType>> {
        #[expect(deprecated, reason = "legacy code")]
        Some(EncodedQueryBQ::Binary(EncodedBinVector {
            // TODO Safety
            encoded_vector: unsafe {
                transmute_from_u8_to_slice(&self.encoded_vectors.get_vector_data(id)).to_vec()
            },
        }))
    }

    fn upsert_vector(
        &mut self,
        id: PointOffsetType,
        vector: &[f32],
        hw_counter: &HardwareCounterCell,
    ) -> std::io::Result<()> {
        let encoded_vector =
            Self::encode_vector(vector, &self.metadata.vector_stats, self.metadata.encoding);
        self.encoded_vectors.upsert_vector(
            id,
            bytemuck::cast_slice(encoded_vector.encoded_vector.as_slice()),
            hw_counter,
        )
    }

    fn vectors_count(&self) -> usize {
        self.encoded_vectors.vectors_count()
    }

    fn flusher(&self) -> MmapFlusher {
        self.encoded_vectors.flusher()
    }

    fn files(&self) -> Vec<PathBuf> {
        let mut files = self.encoded_vectors.files();
        if let Some(meta_path) = &self.metadata_path {
            files.push(meta_path.clone());
        }
        files
    }

    fn immutable_files(&self) -> Vec<PathBuf> {
        let mut files = self.encoded_vectors.immutable_files();
        if let Some(meta_path) = &self.metadata_path {
            files.push(meta_path.clone());
        }
        files
    }

    fn heap_size_bytes(&self) -> usize {
        let storage_heap = self.encoded_vectors.heap_size_bytes();
        let vector_stats_heap = self
            .metadata
            .vector_stats
            .as_ref()
            .map(|vs| vs.elements_stats.capacity() * std::mem::size_of::<VectorElementStats>())
            .unwrap_or(0);
        storage_heap + vector_stats_heap
    }

    type SupportsBytes = True;

    fn score_bytes(
        &self,
        _: Self::SupportsBytes,
        query: &Self::EncodedQuery,
        bytes: &[u8],
        hw_counter: &HardwareCounterCell,
    ) -> f32 {
        // TODO Safety
        #[expect(deprecated, reason = "legacy code")]
        let vector_data_usize = unsafe { transmute_from_u8_to_slice(bytes) };

        hw_counter.cpu_counter().incr_delta(bytes.len());

        match query {
            EncodedQueryBQ::Binary(encoded_vector) => {
                self.calculate_metric(vector_data_usize, &encoded_vector.encoded_vector, 1)
            }
            EncodedQueryBQ::Scalar8bits(encoded_vector) => self.calculate_metric(
                vector_data_usize,
                &encoded_vector.encoded_vector,
                u8::BITS as usize,
            ),
            EncodedQueryBQ::Scalar4bits(encoded_vector) => self.calculate_metric(
                vector_data_usize,
                &encoded_vector.encoded_vector,
                u8::BITS as usize / 2,
            ),
        }
    }
}

#[cfg(target_arch = "x86_64")]
unsafe extern "C" {
    fn impl_xor_popcnt_sse_uint128(query_ptr: *const u8, vector_ptr: *const u8, count: u32) -> u32;

    fn impl_xor_popcnt_sse_uint64(query_ptr: *const u8, vector_ptr: *const u8, count: u32) -> u32;

    fn impl_xor_popcnt_sse_uint32(query_ptr: *const u8, vector_ptr: *const u8, count: u32) -> u32;

    fn impl_xor_popcnt_scalar8_sse_uint128(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;

    fn impl_xor_popcnt_scalar4_sse_uint128(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;

    fn impl_xor_popcnt_scalar8_avx_uint128(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;

    fn impl_xor_popcnt_scalar4_avx_uint128(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;

    fn impl_xor_popcnt_scalar8_sse_u8(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;

    fn impl_xor_popcnt_scalar4_sse_u8(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;
}

#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
unsafe extern "C" {
    fn impl_xor_popcnt_neon_uint128(query_ptr: *const u8, vector_ptr: *const u8, count: u32)
    -> u32;

    fn impl_xor_popcnt_neon_uint64(query_ptr: *const u8, vector_ptr: *const u8, count: u32) -> u32;

    fn impl_xor_popcnt_scalar8_neon_uint128(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;

    fn impl_xor_popcnt_scalar4_neon_uint128(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;

    fn impl_xor_popcnt_scalar8_neon_u8(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;

    fn impl_xor_popcnt_scalar4_neon_u8(
        query_ptr: *const u8,
        vector_ptr: *const u8,
        count: u32,
    ) -> u32;
}

#[allow(missing_docs)]
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx512vl")]
#[target_feature(enable = "avx512vpopcntdq")]
#[target_feature(enable = "avx2")]
#[target_feature(enable = "avx")]
#[target_feature(enable = "sse4.1")]
#[target_feature(enable = "sse2")]
unsafe fn impl_xor_popcnt_avx512_uint128(
    query_ptr: *const u8,
    vector_ptr: *const u8,
    u128_count: u32,
) -> u32 {
    use std::arch::x86_64::*;

    let mut query_ptr = query_ptr.cast::<__m256i>();
    let mut vector_ptr = vector_ptr.cast::<__m256i>();
    let m256_count = u128_count / 2;
    let mut sum = _mm256_setzero_si256();
    for _ in 0..m256_count {
        let query_chunk = unsafe { _mm256_loadu_si256(query_ptr) };
        let vector_chunk = unsafe { _mm256_loadu_si256(vector_ptr) };
        sum = _mm256_add_epi64(
            _mm256_popcnt_epi64(_mm256_xor_si256(query_chunk, vector_chunk)),
            sum,
        );

        query_ptr = unsafe { query_ptr.add(1) };
        vector_ptr = unsafe { vector_ptr.add(1) };
    }

    if m256_count * 2 != u128_count {
        let vector_chunk = unsafe { _mm_loadu_si128(vector_ptr.cast::<__m128i>()) };
        let query_chunk = unsafe { _mm_loadu_si128(query_ptr.cast::<__m128i>()) };

        let popcnt = _mm_popcnt_epi64(_mm_xor_si128(query_chunk, vector_chunk));

        sum = _mm256_add_epi64(_mm256_set_m128i(popcnt, _mm_setzero_si128()), sum);
    }

    // Extract high and low 128-bit lanes
    let low = _mm256_castsi256_si128(sum); // Elements 0,1
    let high = _mm256_extracti128_si256(sum, 1); // Elements 2,3
    let sum128 = _mm_add_epi64(low, high);

    // Extract and add the two 64-bit elements
    let low64 = _mm_extract_epi64(sum128, 0);
    let high64 = _mm_extract_epi64(sum128, 1);

    (low64 + high64) as u32
}