IVF PQ
Source header: cuvs/neighbors/ivf_pq.hpp
IVF-PQ index build parameters
neighbors::ivf_pq::codebook_gen
A type for specifying how PQ codebooks are created.
neighbors::ivf_pq::list_layout
A type for specifying the memory layout of PQ codes in IVF lists.
neighbors::ivf_pq::index_params::from_dataset
Creates index_params based on shape of the input dataset.
Usage example:
Parameters
Returns
static index_params
IVF-PQ index search parameters
neighbors::ivf_pq::search_params
IVF-PQ index search parameters
Fields
Types
list_extents
PQ-encoded data stored in the interleaved format:
neighbors::ivf_pq::list_spec_flat
Flat (non-interleaved) storage specification for PQ-encoded data.
This stores each vector’s PQ codes contiguously: [n_rows, bytes_per_vector] where bytes_per_vector = ceildiv(pq_dim * pq_bits, 8)
Fields
neighbors::graph_build_params::ivf_pq_params
Specialized parameters utilizing IVF-PQ to build knn graph
Fields
IVF-PQ index
neighbors::ivf_pq::index
IVF-PQ index.
In the IVF-PQ index, a database vector y is approximated with two level quantization:
y = Q_1(y) + Q_2(y - Q_1(y))
The first level quantizer (Q_1), maps the vector y to the nearest cluster center. The number of clusters is n_lists.
The second quantizer encodes the residual, and it is defined as a product quantizer [1].
A product quantizer encodes a dim dimensional vector with a pq_dim dimensional vector. First we split the input vector into pq_dim subvectors (denoted by u), where each u vector contains pq_len distinct components of y
y_1, y_2, … y_{pq_len}, y_{pq_len+1}, … y_{2*pq_len}, … y_{dim-pq_len+1} … y_{dim} u_1 u_2 u_{pq_dim}
Then each subvector encoded with a separate quantizer q_i, end the results are concatenated
Q_2(y) = q_1(u_1),q_2(u_2),…,q_{pq_dim}(u_pq_dim})
Each quantizer q_i outputs a code with pq_bit bits. The second level quantizers are also defined by k-means clustering in the corresponding sub-space: the reproduction values are the centroids, and the set of reproduction values is the codebook.
When the data dimensionality dim is not multiple of pq_dim, the feature space is transformed using a random orthogonal matrix to have rot_dim = pq_dim * pq_len dimensions (rot_dim >= dim).
The second-level quantizers are trained either for each subspace or for each cluster: (a) codebook_gen::PER_SUBSPACE: creates pq_dim second-level quantizers - one for each slice of the data along features; (b) codebook_gen::PER_CLUSTER: creates n_lists second-level quantizers - one for each first-level cluster. In either case, the centroids are again found using k-means clustering interpreting the data as having pq_len dimensions.
[1] Product quantization for nearest neighbor search Herve Jegou, Matthijs Douze, Cordelia Schmid
neighbors::ivf_pq::index::index
Construct an empty index.
Constructs an empty index. This index will either need to be trained with build or loaded from a saved copy with deserialize
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::index::index
Construct an index with specified parameters.
This constructor creates an owning index with the given parameters.
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::index::index
Construct an index from index parameters.
Parameters
Returns
void
neighbors::ivf_pq::index::size
Total length of the index.
Returns
IdxT
neighbors::ivf_pq::index::dim
Dimensionality of the input data.
Returns
uint32_t
neighbors::ivf_pq::index::dim_ext
Dimensionality of the cluster centers:
input data dim extended with vector norms and padded to 8 elems.
Returns
uint32_t
neighbors::ivf_pq::index::rot_dim
Dimensionality of the data after transforming it for PQ processing
(rotated and augmented to be muplitple of pq_dim).
Returns
uint32_t
neighbors::ivf_pq::index::pq_bits
The bit length of an encoded vector element after compression by PQ.
Returns
uint32_t
neighbors::ivf_pq::index::pq_dim
The dimensionality of an encoded vector after compression by PQ.
Returns
uint32_t
neighbors::ivf_pq::index::pq_len
Dimensionality of a subspace, i.e. the number of vector components mapped to a subspace
Returns
uint32_t
neighbors::ivf_pq::index::pq_book_size
The number of vectors in a PQ codebook (1 << pq_bits).
Returns
uint32_t
neighbors::ivf_pq::index::metric
Distance metric used for clustering.
Returns
neighbors::ivf_pq::index::codebook_kind
How PQ codebooks are created.
Returns
neighbors::ivf_pq::index::codes_layout
Memory layout of PQ codes in IVF lists.
Returns
neighbors::ivf_pq::index::n_lists
Number of clusters/inverted lists (first level quantization).
Returns
uint32_t
neighbors::ivf_pq::index::conservative_memory_allocation
Whether to use conservative memory allocation when extending the list (cluster) data
(see index_params.conservative_memory_allocation).
Returns
bool
neighbors::ivf_pq::index::pq_centers
PQ cluster centers
- codebook_gen::PER_SUBSPACE: [pq_dim , pq_len, pq_book_size]
- codebook_gen::PER_CLUSTER: [n_lists, pq_len, pq_book_size]
Returns
raft::device_mdspan<const float, pq_centers_extents, raft::row_major>
neighbors::ivf_pq::index::lists
Lists’ data and indices (polymorphic, works for both FLAT and INTERLEAVED layouts).
Returns
std::vector<std::shared_ptr<list_data_base<IdxT>>>&
neighbors::ivf_pq::index::data_ptrs
Pointers to the inverted lists (clusters) data [n_lists].
Returns
raft::device_vector_view<uint8_t*, uint32_t, raft::row_major>
neighbors::ivf_pq::index::inds_ptrs
Pointers to the inverted lists (clusters) indices [n_lists].
Returns
raft::device_vector_view<IdxT*, uint32_t, raft::row_major>
neighbors::ivf_pq::index::rotation_matrix
The transform matrix (original space -> rotated padded space) [rot_dim, dim]
Returns
raft::device_matrix_view<const float, uint32_t, raft::row_major>
neighbors::ivf_pq::index::accum_sorted_sizes
Accumulated list sizes, sorted in descending order [n_lists + 1].
The last value contains the total length of the index. The value at index zero is always zero.
That is, the content of this span is as if the list_sizes was sorted and then accumulated.
This span is used during search to estimate the maximum size of the workspace.
Returns
raft::host_vector_view<IdxT, uint32_t, raft::row_major>
neighbors::ivf_pq::index::list_sizes
Sizes of the lists [n_lists].
Returns
raft::device_vector_view<uint32_t, uint32_t, raft::row_major>
neighbors::ivf_pq::index::centers
Cluster centers corresponding to the lists in the original space [n_lists, dim_ext]
Returns
raft::device_matrix_view<const float, uint32_t, raft::row_major>
neighbors::ivf_pq::index::centers_rot
Cluster centers corresponding to the lists in the rotated space [n_lists, rot_dim]
Returns
raft::device_matrix_view<const float, uint32_t, raft::row_major>
neighbors::ivf_pq::index::get_list_size_in_bytes
fetch size of a particular IVF list in bytes using the list extents.
Usage example:
Parameters
Returns
uint32_t
Additional overload: neighbors::ivf_pq::index::index
Construct index from implementation pointer.
This constructor is used internally by build/extend/deserialize functions.
Parameters
Returns
explicit
IVF-PQ index build
neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
NB: Currently, the following distance metrics are supported:
- L2Expanded
- L2Unexpanded
- InnerProduct
- CosineExpanded
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
NB: Currently, the following distance metrics are supported:
- L2Expanded
- L2Unexpanded
- InnerProduct
- CosineExpanded
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
NB: Currently, the following distance metrics are supported:
- L2Expanded
- L2Unexpanded
- InnerProduct
- CosineExpanded
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
NB: Currently, the following distance metrics are supported:
- L2Expanded
- L2Unexpanded
- InnerProduct
- CosineExpanded
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
Note, if index_params.add_data_on_build is set to true, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
NB: Currently, the following distance metrics are supported:
- L2Expanded
- L2Unexpanded
- InnerProduct
- CosineExpanded
Note, if index_params.add_data_on_build is set to true, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
Note, if index_params.add_data_on_build is set to true, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
NB: Currently, the following distance metrics are supported:
- L2Expanded
- L2Unexpanded
- InnerProduct
- CosineExpanded
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
NB: Currently, the following distance metrics are supported:
- L2Expanded
- L2Unexpanded
- InnerProduct
- CosineExpanded
Note, if index_params.add_data_on_build is set to true, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
Note, if index_params.add_data_on_build is set to true, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build the index from the dataset for efficient search.
NB: Currently, the following distance metrics are supported:
- L2Expanded
- L2Unexpanded
- InnerProduct
- CosineExpanded
Note, if index_params.add_data_on_build is set to true, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build a view-type IVF-PQ index from device memory centroids and codebook.
This function creates a non-owning index that stores a reference to the provided device data. All parameters must be provided with correct extents. The caller is responsible for ensuring the lifetime of the input data exceeds the lifetime of the returned index.
The index_params must be consistent with the provided matrices. Specifically:
- index_params.codebook_kind determines the expected shape of pq_centers
- index_params.metric will be stored in the index
- index_params.conservative_memory_allocation will be stored in the index The function will verify consistency between index_params, dim, and the matrix extents.
dim]
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build an IVF-PQ index from device memory centroids and codebook.
This function creates a non-owning index that references the provided device data directly. All parameters must be provided with correct extents. The caller is responsible for ensuring the lifetime of the input data exceeds the lifetime of the returned index.
The index_params must be consistent with the provided matrices. Specifically:
- index_params.codebook_kind determines the expected shape of pq_centers
- index_params.metric will be stored in the index
- index_params.conservative_memory_allocation will be stored in the index The function will verify consistency between index_params, dim, and the matrix extents.
dim]
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::build
Build an IVF-PQ index from host memory centroids and codebook (in-place).
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::build
Build an IVF-PQ index from host memory centroids and codebook (in-place).
Parameters
Returns
void
IVF-PQ index extend
neighbors::ivf_pq::extend
Extend the index with the new data.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Note, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Note, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Note, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Note, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Note, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Note, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Note, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
cuvs::neighbors::ivf_pq::index<int64_t>
Additional overload: neighbors::ivf_pq::extend
Extend the index with the new data.
Note, the user can set a stream pool in the input raft::resource with at least one stream to enable kernel and copy overlapping.
Usage example:
Parameters
Returns
void
IVF-PQ index transform
neighbors::ivf_pq::transform
Transform a dataset by applying pq-encoding to each vector
cluster ids (labels) for each vector in the input dataset index.pq_bits(), 8)]] that will get populated with the pq-encoded dataset
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::transform
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::transform
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::transform
Parameters
Returns
void
IVF-PQ index serialize
neighbors::ivf_pq::serialize
Write the index to an output stream
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::serialize
Save the index to file.
Parameters
Returns
void
neighbors::ivf_pq::deserialize
Load index from input stream
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::deserialize
Load index from file.
Parameters
Returns
void
IVF-PQ helper methods
neighbors::ivf_pq::helpers::codepacker::unpack
Unpack n_take consecutive records of a single list (cluster) in the compressed index
starting at given offset.
Bit compression is removed, which means output will have pq_dim dimensional vectors (one code per byte, instead of ceildiv(pq_dim * pq_bits, 8) bytes of pq codes).
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::unpack_contiguous
Unpack n_rows consecutive records of a single list (cluster) in the compressed index
starting at given offset. The output codes of a single vector are contiguous, not expanded to one code per byte, which means the output has ceildiv(pq_dim * pq_bits, 8) bytes per PQ encoded vector.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::pack
Write flat PQ codes into an existing list by the given offset.
NB: no memory allocation happens here; the list must fit the data (offset + n_vec).
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::pack_contiguous
Write flat PQ codes into an existing list by the given offset. The input codes of a single vector
are contiguous (not expanded to one code per byte).
NB: no memory allocation happens here; the list must fit the data (offset + n_rows records).
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::pack_list_data
Write flat PQ codes into an existing list by the given offset.
The list is identified by its label.
NB: no memory allocation happens here; the list must fit the data (offset + n_vec).
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::pack_contiguous_list_data
Write flat PQ codes into an existing list by the given offset. Use this when the input
vectors are PQ encoded and not expanded to one code per byte.
The list is identified by its label.
NB: no memory allocation happens here; the list into which the vectors are packed must fit offset + n_rows rows.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::unpack_list_data
Unpack n_take consecutive records of a single list (cluster) in the compressed index
starting at given offset, one code per byte (independently of pq_bits).
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::helpers::codepacker::unpack_list_data
Unpack a series of records of a single list (cluster) in the compressed index
by their in-list offsets, one code per byte (independently of pq_bits).
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::unpack_contiguous_list_data
Unpack n_rows consecutive PQ encoded vectors of a single list (cluster) in the
compressed index starting at given offset, not expanded to one code per byte. Each code in the output buffer occupies ceildiv(index.pq_dim() * index.pq_bits(), 8) bytes.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::reconstruct_list_data
Decode n_take consecutive records of a single list (cluster) in the compressed index
starting at given offset.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::helpers::codepacker::reconstruct_list_data
Decode a series of records of a single list (cluster) in the compressed index
by their in-list offsets.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::extend_list_with_codes
Extend one list of the index in-place, by the list label, skipping the classification and
encoding steps.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::extend_list_with_contiguous_codes
Extend one list of the index in-place, by the list label, skipping the classification and
encoding steps. Uses contiguous/packed codes format.
This is similar to extend_list_with_codes but takes codes in contiguous packed format [n_rows, ceildiv(pq_dim * pq_bits, 8)] instead of unpacked format [n_rows, pq_dim]. This works correctly with any pq_bits value.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::codepacker::extend_list
Extend one list of the index in-place, by the list label, skipping the classification
step.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::erase_list
Remove all data from a single list (cluster) in the index.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::reset_index
Public helper API to reset the data and indices ptrs, and the list sizes. Useful for
externally modifying the index without going through the build stage. The data and indices of the IVF lists will be lost.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::pad_centers_with_norms
Pad cluster centers with their L2 norms for efficient GEMM operations.
This function takes cluster centers and pads them with their L2 norms to create extended centers suitable for coarse search operations. The output has dimensions [n_centers, dim_ext] where dim_ext = round_up(dim + 1, 8).
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::helpers::pad_centers_with_norms
Pad cluster centers with their L2 norms for efficient GEMM operations.
This function takes cluster centers and pads them with their L2 norms to create extended centers suitable for coarse search operations. The output has dimensions [n_centers, dim_ext] where dim_ext = round_up(dim + 1, 8).
Parameters
Returns
void
neighbors::ivf_pq::helpers::rotate_padded_centers
Rotate padded centers with the rotation matrix.
Parameters
Returns
void
neighbors::ivf_pq::helpers::extract_centers
Public helper API for fetching a trained index’s IVF centroids
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::helpers::extract_centers
Parameters
Returns
void
neighbors::ivf_pq::helpers::recompute_internal_state
Helper exposing the re-computation of list sizes and related arrays if IVF lists have been
modified externally.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::make_rotation_matrix
Generate a rotation matrix into user-provided buffer (standalone version).
This standalone helper generates a rotation matrix without requiring an index object. Users can call this to prepare a rotation matrix before building from precomputed data.
Usage example:
Parameters
Returns
void
neighbors::ivf_pq::helpers::resize_list
Resize an IVF-PQ list with flat layout.
This helper resizes an IVF list that uses the flat (non-interleaved) PQ code layout. If the new size exceeds the current capacity, a new list is allocated and existing data is copied. The function handles the type casting internally.
Usage example:
Parameters
Returns
void
Additional overload: neighbors::ivf_pq::helpers::resize_list
Resize an IVF-PQ list with interleaved layout.
This helper resizes an IVF list that uses the interleaved PQ code layout (default). If the new size exceeds the current capacity, a new list is allocated and existing data is copied. The function handles the type casting internally.
Usage example:
Parameters
Returns
void