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
// Copyright 2023 Lance Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Product Quantization
//!

use std::any::Any;
use std::sync::Arc;

use arrow_array::{cast::AsArray, Array, FixedSizeListArray, UInt8Array};
use arrow_array::{ArrayRef, Float32Array};
use async_trait::async_trait;
use lance_arrow::floats::FloatArray;
use lance_arrow::*;
use lance_core::{Error, Result};
use lance_linalg::distance::{
    cosine_distance_batch, dot_distance_batch, l2_distance_batch, norm_l2, Cosine, Dot, L2,
};
use lance_linalg::kernels::{argmin, argmin_value_float};
use lance_linalg::{distance::MetricType, MatrixView};
use snafu::{location, Location};
pub mod builder;
pub mod transform;
pub(crate) mod utils;

pub use self::utils::num_centroids;
use super::pb;
pub use builder::PQBuildParams;
use lance_linalg::simd::{f32::f32x8, SIMD};

/// Product Quantization

#[async_trait::async_trait]
pub trait ProductQuantizer: Send + Sync + std::fmt::Debug {
    fn as_any(&self) -> &dyn Any;

    /// Transform a vector column to PQ code column.
    async fn transform(&self, data: &dyn Array) -> Result<ArrayRef>;

    /// Build the distance lookup in `f32`.
    fn build_distance_table(&self, query: &dyn Array, code: &UInt8Array) -> Result<ArrayRef>;

    /// Get the centroids for one sub-vector.
    fn num_bits(&self) -> u32;

    /// Number of sub-vectors
    fn num_sub_vectors(&self) -> usize;

    fn dimension(&self) -> usize;

    // TODO: move to pub(crate) once the refactor of lance::index to lance-index is done.
    fn codebook_as_fsl(&self) -> FixedSizeListArray;
}

/// Product Quantization, optimized for [Apache Arrow] buffer memory layout.
///
//
// TODO: move this to be pub(crate) once we have a better way to test it.
#[derive(Debug)]
pub struct ProductQuantizerImpl<T: ArrowFloatType + Cosine + Dot + L2> {
    /// Number of bits for the centroids.
    ///
    /// Only support 8, as one of `u8` byte now.
    pub num_bits: u32,

    /// Number of sub-vectors.
    pub num_sub_vectors: usize,

    /// Vector dimension.
    pub dimension: usize,

    /// Distance type.
    pub metric_type: MetricType,

    /// PQ codebook
    ///
    /// ```((2 ^ nbits) * num_subvector * sub_vector_length)``` of `f32`
    ///
    /// Use a layout that is cache / SIMD friendly to compute centroid.
    /// But not sure how to make distance lookup via PQ code lookup
    /// be cache friendly tho.
    ///
    /// Layout:
    ///
    ///  - *row*: all centroids for the same sub-vector.
    ///  - *column*: the centroid value of the n-th sub-vector.
    ///
    /// ```text
    /// // Centroids for a sub-vector.
    /// Codebook[sub_vector_id][pq_code]
    /// ```
    pub codebook: Arc<T::ArrayType>,
}

fn get_sub_vector_centroids<T: FloatToArrayType>(
    codebook: &[T],
    dimension: usize,
    num_bits: impl Into<u32>,
    num_sub_vectors: usize,
    sub_vector_idx: usize,
) -> &[T] {
    assert!(sub_vector_idx < num_sub_vectors);

    let num_centroids = num_centroids(num_bits);
    let sub_vector_width = dimension / num_sub_vectors;
    &codebook[sub_vector_idx * num_centroids * sub_vector_width
        ..(sub_vector_idx + 1) * num_centroids * sub_vector_width]
}

impl<T: ArrowFloatType + Cosine + Dot + L2> ProductQuantizerImpl<T> {
    /// Create a [`ProductQuantizer`] with pre-trained codebook.
    pub fn new(
        m: usize,
        nbits: u32,
        dimension: usize,
        codebook: Arc<T::ArrayType>,
        metric_type: MetricType,
    ) -> Self {
        assert_eq!(nbits, 8, "nbits can only be 8");
        Self {
            num_bits: nbits,
            num_sub_vectors: m,
            dimension,
            codebook,
            metric_type,
        }
    }

    pub fn num_centroids(num_bits: u32) -> usize {
        2_usize.pow(num_bits)
    }

    /// Calculate codebook length.
    pub fn codebook_length(num_bits: u32, num_sub_vectors: usize) -> usize {
        Self::num_centroids(num_bits) * num_sub_vectors
    }

    /// Get the centroids for one sub-vector.
    ///
    /// Returns a flatten `num_centroids * sub_vector_width` f32 array.
    pub fn centroids(&self, sub_vector_idx: usize) -> &[T::Native] {
        get_sub_vector_centroids(
            self.codebook.as_slice(),
            self.dimension,
            self.num_bits,
            self.num_sub_vectors,
            sub_vector_idx,
        )
    }

    /// Reconstruct a vector from its PQ code.
    ///
    /// It only supports U8 PQ code for now.
    #[allow(dead_code)]
    pub(crate) fn reconstruct(&self, code: &[u8]) -> Arc<T::ArrayType> {
        assert_eq!(code.len(), self.num_sub_vectors);
        let mut builder = Vec::with_capacity(self.dimension);
        let sub_vector_dim = self.dimension / self.num_sub_vectors;
        for (i, sub_code) in code.iter().enumerate() {
            let centroids = self.centroids(i);
            builder.extend_from_slice(
                &centroids[*sub_code as usize * sub_vector_dim
                    ..(*sub_code as usize + 1) * sub_vector_dim],
            );
        }
        Arc::new(T::ArrayType::from(builder))
    }

    /// Compute the quantization distortion (E).
    ///
    /// Quantization distortion is the difference between the centroids
    /// from the PQ code to the actual vector.
    ///
    /// This method is just for debugging purpose.
    #[allow(dead_code)]
    pub(crate) async fn distortion(
        &self,
        data: &MatrixView<T>,
        metric_type: MetricType,
    ) -> Result<f64> {
        let sub_vector_width = self.dimension / self.num_sub_vectors;
        let total_distortion = data
            .iter()
            .map(|vector| {
                vector
                    .chunks_exact(sub_vector_width)
                    .enumerate()
                    .map(|(sub_vector_idx, sub_vec)| {
                        let centroids = self.centroids(sub_vector_idx);
                        let distances = match metric_type {
                            lance_linalg::distance::DistanceType::L2 => {
                                l2_distance_batch(sub_vec, centroids, sub_vector_width)
                            }
                            lance_linalg::distance::DistanceType::Cosine => {
                                cosine_distance_batch(sub_vec, centroids, sub_vector_width)
                            }
                            lance_linalg::distance::DistanceType::Dot => {
                                dot_distance_batch(sub_vec, centroids, sub_vector_width)
                            }
                        };
                        argmin_value_float(distances).1
                    })
                    .sum::<f32>() as f64
            })
            .sum::<f64>();
        Ok(total_distortion / data.num_rows() as f64)
    }

    fn l2_distance_table(&self, key: &dyn Array, code: &UInt8Array) -> Result<ArrayRef> {
        let key: &T::ArrayType = key.as_any().downcast_ref().ok_or(Error::Index {
            message: format!(
                "Build L2 distance table, type mismatch: {}",
                key.data_type()
            ),
            location: Default::default(),
        })?;

        // Build distance table for each sub-centroid to the query key.
        //
        // Distance table: `[T::Native: num_sub_vectors(row) * num_centroids(column)]`.
        let mut distance_table = vec![];

        let sub_vector_length = self.dimension / self.num_sub_vectors;
        key.as_slice()
            .chunks_exact(sub_vector_length)
            .enumerate()
            .for_each(|(i, sub_vec)| {
                let subvec_centroids = self.centroids(i);
                let distances = l2_distance_batch(sub_vec, subvec_centroids, sub_vector_length);
                distance_table.extend(distances);
            });

        #[cfg(target_feature = "avx512f")]
        {
            if self.num_sub_vectors % 16 == 0 {
                use std::arch::x86_64::*;
                return Ok(Arc::new(Float32Array::from_iter_values(
                    code.values()
                        .chunks_exact(self.num_sub_vectors)
                        .map(|c| unsafe {
                            let mut s = _mm512_setzero_ps();
                            c.chunks_exact(16)
                                .enumerate()
                                .for_each(|(idx, lane_chunk)| {
                                    let mut offsets: [i32; 16] = [0; 16];
                                    lane_chunk.iter().enumerate().for_each(|(j, &code)| {
                                        offsets[j] = ((idx * 8 + j) * 256 + code as usize) as i32
                                    });
                                    let simd_offsets = _mm512_loadu_epi32(offsets.as_ptr());
                                    let v = _mm512_i32gather_ps(
                                        simd_offsets,
                                        distance_table.as_ptr() as *const u8,
                                        4,
                                    );
                                    s = _mm512_add_ps(s, v);
                                });
                            _mm512_reduce_add_ps(s)
                        }),
                )));
            }
        }

        if cfg!(target_feature = "avx2") && self.num_sub_vectors % 8 == 0 {
            Ok(Arc::new(Float32Array::from_iter_values(
                code.values().chunks_exact(self.num_sub_vectors).map(|c| {
                    let mut s = f32x8::zeros();
                    c.chunks_exact(8).enumerate().for_each(|(idx, lane_chunk)| {
                        let mut offsets: [i32; 8] = [0; 8];
                        lane_chunk.iter().enumerate().for_each(|(j, &code)| {
                            offsets[j] = ((idx * 8 + j) * 256 + code as usize) as i32
                        });
                        let v = f32x8::gather(&distance_table, &offsets);
                        s += v;
                    });
                    s.reduce_sum()
                }),
            )))
        } else {
            Ok(Arc::new(Float32Array::from_iter_values(
                code.values().chunks_exact(self.num_sub_vectors).map(|c| {
                    c.iter()
                        .enumerate()
                        .map(|(sub_vec_idx, centroid)| {
                            distance_table[sub_vec_idx * 256 + *centroid as usize]
                        })
                        .sum()
                }),
            )))
        }
    }

    /// Pre-compute dot product to each sub-centroids.
    /// Parameters
    ///  - query: the query vector, with shape (dimension, )
    ///  - code: the PQ code in one partition.
    ///
    fn dot_distance_table(&self, key: &dyn Array, code: &UInt8Array) -> Result<ArrayRef> {
        let key: &T::ArrayType = key.as_any().downcast_ref().ok_or(Error::Index {
            message: format!(
                "Build Dot distance table, type mismatch: {}",
                key.data_type()
            ),
            location: Default::default(),
        })?;

        // Distance table: `[f32: num_sub_vectors(row) * num_centroids(column)]`.
        let capacity = self.num_sub_vectors * num_centroids(self.num_bits);
        let mut distance_table = Vec::with_capacity(capacity);

        let sub_vector_length = self.dimension / self.num_sub_vectors;
        key.as_slice()
            .chunks_exact(sub_vector_length)
            .enumerate()
            .for_each(|(sub_vec_id, sub_vec)| {
                let subvec_centroids = self.centroids(sub_vec_id);
                let distances = dot_distance_batch(sub_vec, subvec_centroids, sub_vector_length);
                distance_table.extend(distances);
            });

        // Compute distance from the pre-compute table.
        Ok(Arc::new(Float32Array::from_iter_values(
            code.values().chunks_exact(self.num_sub_vectors).map(|c| {
                c.iter()
                    .enumerate()
                    .map(|(sub_vec_idx, centroid)| {
                        distance_table[sub_vec_idx * 256 + *centroid as usize]
                    })
                    .sum::<f32>()
            }),
        )))
    }

    /// Pre-compute cosine distance to each sub-centroids.
    ///
    /// Parameters
    ///  - query: the query vector, with shape (dimension, )
    ///  - code: the PQ code in one partition.
    ///
    fn cosine_distances(&self, key: &dyn Array, code: &UInt8Array) -> Result<ArrayRef> {
        let query: &T::ArrayType = key.as_any().downcast_ref().ok_or(Error::Index {
            message: format!(
                "Build Dot distance table, type mismatch: {}",
                key.data_type()
            ),
            location: Default::default(),
        })?;

        // Build two tables for cosine distance.
        //
        // xy table: `[f32: num_sub_vectors(row) * num_centroids(column)]`.
        // y_norm table: `[f32: num_sub_vectors(row) * num_centroids(column)]`.
        let num_centroids = num_centroids(self.num_bits);
        let mut xy_table: Vec<f32> = Vec::with_capacity(self.num_sub_vectors * num_centroids);
        let mut y2_table: Vec<f32> = Vec::with_capacity(self.num_sub_vectors * num_centroids);

        let x_norm = norm_l2(query.as_slice());
        let sub_vector_length = self.dimension / self.num_sub_vectors;
        query
            .as_slice()
            .chunks_exact(sub_vector_length)
            .enumerate()
            .for_each(|(i, sub_vector)| {
                let sub_vector_centroids = self.centroids(i);
                xy_table.extend(dot_distance_batch(
                    sub_vector,
                    sub_vector_centroids,
                    sub_vector_length,
                ));
                y2_table.extend(
                    sub_vector_centroids
                        .chunks_exact(sub_vector_length)
                        .map(|cent| norm_l2(cent).powi(2)),
                );
            });

        // Compute distance from the pre-compute table.
        Ok(Arc::new(Float32Array::from_iter_values(
            code.values().chunks_exact(self.num_sub_vectors).map(|c| {
                let xy = c
                    .iter()
                    .enumerate()
                    .map(|(sub_vec_idx, centroid)| {
                        let idx = sub_vec_idx * num_centroids + *centroid as usize;
                        xy_table[idx]
                    })
                    .sum::<f32>();
                let y2 = c
                    .iter()
                    .enumerate()
                    .map(|(sub_vec_idx, centroid)| {
                        let idx = sub_vec_idx * num_centroids + *centroid as usize;
                        y2_table[idx]
                    })
                    .sum::<f32>();
                1.0 - xy / (x_norm * y2.sqrt())
            }),
        )))
    }
}

#[async_trait]
impl<T: ArrowFloatType + Cosine + Dot + L2 + 'static> ProductQuantizer for ProductQuantizerImpl<T> {
    fn as_any(&self) -> &dyn Any {
        self
    }

    async fn transform(&self, data: &dyn Array) -> Result<ArrayRef> {
        let fsl = data
            .as_fixed_size_list_opt()
            .ok_or(Error::Index {
                message: format!(
                    "Expect to be a float vector array, got: {:?}",
                    data.data_type()
                ),
                location: location!(),
            })?
            .clone();

        let num_sub_vectors = self.num_sub_vectors;
        let dim = self.dimension;
        let num_rows = fsl.len();
        let num_bits = self.num_bits;
        let codebook = self.codebook.clone();

        let metric_type = self.metric_type;
        let values = tokio::task::spawn_blocking(move || {
            let all_centroids = (0..num_sub_vectors)
                .map(|idx| {
                    get_sub_vector_centroids(
                        codebook.as_slice(),
                        dim,
                        num_bits,
                        num_sub_vectors,
                        idx,
                    )
                })
                .collect::<Vec<_>>();
            let flatten_data =
                fsl.values()
                    .as_any()
                    .downcast_ref::<T::ArrayType>()
                    .ok_or(Error::Index {
                        message: format!(
                            "Expect to be a float vector array, got: {:?}",
                            fsl.value_type()
                        ),
                        location: location!(),
                    })?;

            let flatten_values = flatten_data.as_slice();
            let capacity = num_sub_vectors * num_rows;
            let mut builder: Vec<u8> = vec![0; capacity];
            // Dimension of each sub-vector.
            let sub_dim = dim / num_sub_vectors;
            for i in 0..num_rows {
                let row_offset = i * dim;

                for sub_idx in 0..num_sub_vectors {
                    let offset = row_offset + sub_idx * sub_dim;
                    let sub_vector = &flatten_values[offset..offset + sub_dim];
                    let centroids = all_centroids[sub_idx];

                    let dist_iter = match metric_type {
                        lance_linalg::distance::DistanceType::L2 => {
                            l2_distance_batch(sub_vector, centroids, sub_dim)
                        }
                        lance_linalg::distance::DistanceType::Cosine => {
                            cosine_distance_batch(sub_vector, centroids, sub_dim)
                        }
                        lance_linalg::distance::DistanceType::Dot => {
                            dot_distance_batch(sub_vector, centroids, sub_dim)
                        }
                    };
                    let code = argmin(dist_iter).ok_or(Error::Index {
                        message: format!(
                            "Failed to assign PQ code: {}, sub-vector={:#?}",
                            "it is likely that distance is NaN or Inf", sub_vector
                        ),
                        location: location!(),
                    })? as u8;
                    builder[i * num_sub_vectors + sub_idx] = code as u8;
                }
            }
            Ok::<UInt8Array, Error>(UInt8Array::from(builder))
        })
        .await??;

        Ok(Arc::new(FixedSizeListArray::try_new_from_values(
            values,
            self.num_sub_vectors as i32,
        )?))
    }

    fn build_distance_table(&self, query: &dyn Array, code: &UInt8Array) -> Result<ArrayRef> {
        match self.metric_type {
            MetricType::Cosine => self.cosine_distances(query, code),
            MetricType::Dot => self.dot_distance_table(query, code),
            MetricType::L2 => self.l2_distance_table(query, code),
        }
    }

    fn num_bits(&self) -> u32 {
        self.num_bits
    }

    fn num_sub_vectors(&self) -> usize {
        self.num_sub_vectors
    }

    fn dimension(&self) -> usize {
        self.dimension
    }

    fn codebook_as_fsl(&self) -> FixedSizeListArray {
        FixedSizeListArray::try_new_from_values(
            self.codebook.as_ref().clone(),
            self.dimension as i32,
        )
        .unwrap()
    }
}

#[allow(clippy::fallible_impl_from)]
impl TryFrom<&dyn ProductQuantizer> for pb::Pq {
    type Error = Error;

    fn try_from(pq: &dyn ProductQuantizer) -> Result<Self> {
        let fsl = pq.codebook_as_fsl();
        let tensor = pb::Tensor::try_from(&fsl)?;
        Ok(Self {
            num_bits: pq.num_bits(),
            num_sub_vectors: pq.num_sub_vectors() as u32,
            dimension: pq.dimension() as u32,
            codebook: vec![],
            codebook_tensor: Some(tensor),
        })
    }
}

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

    use std::iter::repeat;

    use arrow_array::{
        types::{Float16Type, Float32Type},
        Float16Array, Float32Array,
    };
    use half::f16;
    use num_traits::Zero;

    #[test]
    fn test_f16_pq_to_protobuf() {
        let pq = ProductQuantizerImpl::<Float16Type> {
            num_bits: 8,
            num_sub_vectors: 4,
            dimension: 16,
            codebook: Arc::new(Float16Array::from_iter_values(
                repeat(f16::zero()).take(256 * 16),
            )),
            metric_type: MetricType::L2,
        };
        let proto: pb::Pq = pb::Pq::try_from(&pq as &dyn ProductQuantizer).unwrap();
        assert_eq!(proto.num_bits, 8);
        assert_eq!(proto.num_sub_vectors, 4);
        assert_eq!(proto.dimension, 16);
        assert!(proto.codebook.is_empty());
        assert!(proto.codebook_tensor.is_some());

        let tensor = proto.codebook_tensor.as_ref().unwrap();
        assert_eq!(tensor.data_type, pb::tensor::DataType::Float16 as i32);
        assert_eq!(tensor.shape, vec![256, 16]);
    }

    #[tokio::test]
    async fn test_empty_dist_iter() {
        let pq = ProductQuantizerImpl::<Float32Type> {
            num_bits: 8,
            num_sub_vectors: 4,
            dimension: 16,
            codebook: Arc::new(Float32Array::from_iter_values(
                (0..256 * 16).map(|v| v as f32),
            )),
            metric_type: MetricType::Cosine,
        };

        let data = Float32Array::from_iter_values(repeat(0.0).take(16));
        let data = FixedSizeListArray::try_new_from_values(data, 16).unwrap();
        let rst = pq.transform(&data).await;
        assert!(rst.is_err());
        assert!(rst
            .unwrap_err()
            .to_string()
            .contains("it is likely that distance is NaN"));
    }
}