PQ

View as Markdown

Source header: cuvs/preprocessing/quantize/pq.h

C API for Product Quantizer

cuvsProductQuantizerParams

Product quantizer parameters.

1struct cuvsProductQuantizerParams { ... };

Fields

NameTypeDescription
pq_bitsuint32_tThe bit length of the vector element after compression by PQ. Possible values: within [4, 16]. Hint: the smaller the ‘pq_bits’, the smaller the index size and the better the search performance, but the lower the recall.
pq_dimuint32_tThe dimensionality of the vector after compression by PQ. When zero, an optimal value is selected using a heuristic. TODO: at the moment dim must be a multiple pq_dim.
use_subspacesboolWhether to use subspaces for product quantization (PQ). When true, one PQ codebook is used for each subspace. Otherwise, a single PQ codebook is used.
use_vqboolWhether to use Vector Quantization (KMeans) before product quantization (PQ). When true, VQ is used before PQ. When false, only product quantization is used.
vq_n_centersuint32_tVector Quantization (VQ) codebook size - number of “coarse cluster centers”. When zero, an optimal value is selected using a heuristic. When one, only product quantization is used.
kmeans_n_itersuint32_tThe number of iterations searching for kmeans centers (both VQ & PQ phases).
pq_kmeans_typecuvsKMeansTypeThe type of kmeans algorithm to use for PQ training.
max_train_points_per_pq_codeuint32_tThe max number of data points to use per PQ code during PQ codebook training. Using more data points per PQ code may increase the quality of PQ codebook but may also increase the build time. We will use pq_n_centers * max_train_points_per_pq_code training points to train each PQ codebook.
max_train_points_per_vq_clusteruint32_tThe max number of data points to use per VQ cluster.

cuvsProductQuantizerParamsCreate

Allocate Product Quantizer params, and populate with default values

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerParamsCreate(cuvsProductQuantizerParams_t* params);

Parameters

NameDirectionTypeDescription
paramsincuvsProductQuantizerParams_t*cuvsProductQuantizerParams_t to allocate

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerParamsDestroy

De-allocate Product Quantizer params

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerParamsDestroy(cuvsProductQuantizerParams_t params);

Parameters

NameDirectionTypeDescription
paramsincuvsProductQuantizerParams_t

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizer

Defines and stores product quantizer upon training

The quantization is performed by a linear mapping of an interval in the float data type to the full range of the quantized int type.

1typedef struct { ... } cuvsProductQuantizer;

Fields

NameTypeDescription
addruintptr_t
dtypeDLDataType

cuvsProductQuantizerCreate

Allocate Product Quantizer

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerCreate(cuvsProductQuantizer_t* quantizer);

Parameters

NameDirectionTypeDescription
quantizerincuvsProductQuantizer_t*cuvsProductQuantizer_t to allocate

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerDestroy

De-allocate Product Quantizer

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerDestroy(cuvsProductQuantizer_t quantizer);

Parameters

NameDirectionTypeDescription
quantizerincuvsProductQuantizer_t

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerBuild

Builds a product quantizer to be used later for quantizing the dataset.

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerBuild(cuvsResources_t res,
2cuvsProductQuantizerParams_t params,
3DLManagedTensor* dataset,
4cuvsProductQuantizer_t quantizer);

Parameters

NameDirectionTypeDescription
resincuvsResources_traft resource
paramsincuvsProductQuantizerParams_tParameters for product quantizer training
datasetinDLManagedTensor*a row-major host or device matrix
quantizeroutcuvsProductQuantizer_ttrained product quantizer

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerTransform

Applies product quantization transform to the given dataset

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerTransform(cuvsResources_t res,
2cuvsProductQuantizer_t quantizer,
3DLManagedTensor* dataset,
4DLManagedTensor* codes_out,
5DLManagedTensor* vq_labels);

This applies product quantization to a dataset.

Parameters

NameDirectionTypeDescription
resincuvsResources_traft resource
quantizerincuvsProductQuantizer_tproduct quantizer
datasetinDLManagedTensor*a row-major host or device matrix to transform
codes_outoutDLManagedTensor*a row-major device matrix to store transformed data
vq_labelsoutDLManagedTensor*a device vector to store VQ labels. Optional, can be NULL.

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerInverseTransform

Applies product quantization inverse transform to the given quantized codes

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerInverseTransform(cuvsResources_t res,
2cuvsProductQuantizer_t quantizer,
3DLManagedTensor* pq_codes,
4DLManagedTensor* out,
5DLManagedTensor* vq_labels);

This applies product quantization inverse transform to the given quantized codes.

Parameters

NameDirectionTypeDescription
resincuvsResources_traft resource
quantizerincuvsProductQuantizer_tproduct quantizer
pq_codesinDLManagedTensor*a row-major device matrix of quantized codes
outoutDLManagedTensor*a row-major device matrix to store the original data
vq_labelsoutDLManagedTensor*a device vector containing the VQ labels when VQ is used. Optional, can be NULL.

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerGetPqBits

Get the bit length of the vector element after compression by PQ.

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerGetPqBits(cuvsProductQuantizer_t quantizer, uint32_t* pq_bits);

Parameters

NameDirectionTypeDescription
quantizerincuvsProductQuantizer_tproduct quantizer
pq_bitsoutuint32_t*bit length of the vector element after compression by PQ

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerGetPqDim

Get the dimensionality of the vector after compression by PQ.

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerGetPqDim(cuvsProductQuantizer_t quantizer, uint32_t* pq_dim);

Parameters

NameDirectionTypeDescription
quantizerincuvsProductQuantizer_tproduct quantizer
pq_dimoutuint32_t*dimensionality of the vector after compression by PQ

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerGetPqCodebook

Get the PQ codebook.

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerGetPqCodebook(cuvsProductQuantizer_t quantizer,
2DLManagedTensor* pq_codebook);

Parameters

NameDirectionTypeDescription
quantizerincuvsProductQuantizer_tproduct quantizer
pq_codebookoutDLManagedTensor*PQ codebook

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerGetVqCodebook

Get the VQ codebook.

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerGetVqCodebook(cuvsProductQuantizer_t quantizer,
2DLManagedTensor* vq_codebook);

Parameters

NameDirectionTypeDescription
quantizerincuvsProductQuantizer_tproduct quantizer
vq_codebookoutDLManagedTensor*VQ codebook

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerGetEncodedDim

Get the encoded dimension of the quantized dataset.

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerGetEncodedDim(cuvsProductQuantizer_t quantizer,
2uint32_t* encoded_dim);

Parameters

NameDirectionTypeDescription
quantizerincuvsProductQuantizer_tproduct quantizer
encoded_dimoutuint32_t*encoded dimension of the quantized dataset

Returns

CUVS_EXPORT cuvsError_t

cuvsProductQuantizerGetUseVq

Get whether VQ is used.

1CUVS_EXPORT cuvsError_t cuvsProductQuantizerGetUseVq(cuvsProductQuantizer_t quantizer, bool* use_vq);

Parameters

NameDirectionTypeDescription
quantizerincuvsProductQuantizer_tproduct quantizer
use_vqoutbool*whether VQ is used

Returns

CUVS_EXPORT cuvsError_t