cuvs.preprocessing.quantize.pq.pq#
6 min read time
Attributes#
Classes#
DeviceTensorView(array_like=None) |
|
Defines and stores Product Quantizer upon training |
|
QuantizerParams(pq_bits=8, *, pq_dim=0, use_subspaces=True, use_vq=False, vq_n_centers=0, kmeans_n_iters=25, pq_kmeans_type='kmeans_balanced', max_train_points_per_pq_code=256, max_train_points_per_vq_cluster=1024) |
Functions#
|
check_cuvs(cuvsError_t status: cuvsError_t) |
auto_sync_resources(f) |
|
|
build(QuantizerParams params, dataset, resources=None) |
|
inverse_transform(Quantizer quantizer, codes, output=None, vq_labels=None, resources=None) |
|
transform(Quantizer quantizer, dataset, codes_output=None, vq_labels=None, resources=None) |
Module Contents#
- class cuvs.preprocessing.quantize.pq.pq.DeviceTensorView#
DeviceTensorView(array_like=None)
A __cuda_array_interface__ compatible tensor on device memory
This aims to be a lightweight wrapper around device_matrix_view and device_vector_view objects in the C++ layer of cuvs - and lets us access memory internals of cuvs without copying into temporary storage.
Since this support the __cuda_array_interface__ protocol, this object gives us easy interopability with other libraries such as cupy, pytorch, and cudf - without adding any dependencies on these libraries inside cuvs.
Since cuvs also understands CAI, DeviceTensorView objects can also be passed directly to cuvs python functions.
- static __reduce__(*args, **kwargs)#
DeviceTensorView.__reduce_cython__(self)
- static __setstate__(*args, **kwargs)#
DeviceTensorView.__setstate_cython__(self, __pyx_state)
- static copy_to_host(*args, resources=None, **kwargs)#
DeviceTensorView.copy_to_host(self, resources=None)
copies a device matrix to a numpy array
Parameters#
- resourcesOptional cuVS Resource handle for reusing CUDA resources.
If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.
Returns#
- tensor: np.array
This tensor copied into host memory
- static slice_rows(*args, resources=None, **kwargs)#
DeviceTensorView.slice_rows(self, start, end, resources=None)
Slices rows from this tensor
Slices a subset, and returns in a new DeviceTensorView without copying data.
Parameters#
- start: int
the index of the first row to slice
- end: int
the index of the last row to slice
- resourcesOptional cuVS Resource handle for reusing CUDA resources.
If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.
Returns#
- tensor: DeviceTensorView
A non-owning view of the rows sliced from this tensor
- get_handle()#
DeviceTensorView.get_handle(self)
- cuvs.preprocessing.quantize.pq.pq.check_cuvs(status: cuvsError_t)#
check_cuvs(cuvsError_t status: cuvsError_t)
Converts a status code into an exception
- cuvs.preprocessing.quantize.pq.pq.auto_sync_resources(f)#
auto_sync_resources(f)
Decorator to automatically call sync on a cuVS Resources object when it isn’t passed to a function.
When a resources=None is passed to the wrapped function, this decorator will automatically create a default resources for the function, and call sync on that resources when the function exits.
This will also insert the appropriate docstring for the resources parameter
- class cuvs.preprocessing.quantize.pq.pq.Quantizer#
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.
- static __reduce__(*args, **kwargs)#
Quantizer.__reduce_cython__(self)
- static __setstate__(*args, **kwargs)#
Quantizer.__setstate_cython__(self, __pyx_state)
- __repr__()#
Return repr(self).
- class cuvs.preprocessing.quantize.pq.pq.QuantizerParams#
QuantizerParams(pq_bits=8, *, pq_dim=0, use_subspaces=True, use_vq=False, vq_n_centers=0, kmeans_n_iters=25, pq_kmeans_type=’kmeans_balanced’, max_train_points_per_pq_code=256, max_train_points_per_vq_cluster=1024)
Parameters for product quantization
Parameters#
- pq_bits: int
specifies the bit length of the vector element after compression by PQ possible values: within [4, 16]
- pq_dim: int
specifies the dimensionality of the vector after compression by PQ
- use_subspaces: bool
specifies whether 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_vq: bool
specifies whether to use Vector Quantization (KMeans) before product quantization (PQ).
- vq_n_centers: int
specifies the number of centers for the vector quantizer. When zero, an optimal value is selected using a heuristic. When one, only product quantization is used.
- kmeans_n_iters: int
specifies the number of iterations searching for kmeans centers
- pq_kmeans_type: str
specifies the type of kmeans algorithm to use for PQ training possible values: “kmeans”, “kmeans_balanced”
- max_train_points_per_pq_code: int
specifies the 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.
- max_train_points_per_vq_cluster: int
specifies the max number of data points to use per VQ cluster.
- static __reduce__(*args, **kwargs)#
QuantizerParams.__reduce_cython__(self)
- static __setstate__(*args, **kwargs)#
QuantizerParams.__setstate_cython__(self, __pyx_state)
- cuvs.preprocessing.quantize.pq.pq.build(*args, resources=None, **kwargs)#
build(QuantizerParams params, dataset, resources=None)
Builds a Product Quantizer to be used later for quantizing the dataset.
Parameters#
params : QuantizerParams object dataset : row major dataset on host or device memory. FP32 resources : Optional cuVS Resource handle for reusing CUDA resources.
If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.
Returns#
quantizer: cuvs.preprocessing.quantize.pq.Quantizer
Examples#
>>> import cupy as cp >>> from cuvs.preprocessing.quantize import pq >>> n_samples = 5000 >>> n_features = 64 >>> dataset = cp.random.random_sample((n_samples, n_features), ... dtype=cp.float32) >>> params = pq.QuantizerParams(pq_bits=8, pq_dim=16) >>> quantizer = pq.build(params, dataset) >>> transformed, _ = pq.transform(quantizer, dataset)
- cuvs.preprocessing.quantize.pq.pq.inverse_transform(*args, resources=None, **kwargs)#
inverse_transform(Quantizer quantizer, codes, output=None, vq_labels=None, resources=None)
Applies Product Quantization inverse transform to given codes
Parameters#
quantizer : trained Quantizer object codes : row major device codes to inverse transform. uint8 output : optional preallocated output memory, on device memory vq_labels : optional VQ labels when VQ is used, on device memory resources : Optional cuVS Resource handle for reusing CUDA resources.
If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.
Returns#
output : Original dataset reconstructed from quantized codes
Examples#
>>> import cupy as cp >>> from cuvs.preprocessing.quantize import pq >>> n_samples = 5000 >>> n_features = 64 >>> dataset = cp.random.random_sample((n_samples, n_features), ... dtype=cp.float32) >>> params = pq.QuantizerParams(pq_bits=8, pq_dim=16, use_vq=True) >>> quantizer = pq.build(params, dataset) >>> transformed, vq_labels = pq.transform(quantizer, dataset) >>> reconstructed = pq.inverse_transform(quantizer, transformed, vq_labels=vq_labels)
- cuvs.preprocessing.quantize.pq.pq.transform(*args, resources=None, **kwargs)#
transform(Quantizer quantizer, dataset, codes_output=None, vq_labels=None, resources=None)
Applies Product Quantization transform to given dataset
Parameters#
quantizer : trained Quantizer object dataset : row major dataset on host or device memory. FP32 codes_output : optional preallocated output memory, on device memory vq_labels : optional preallocated output memory for VQ labels, on device memory resources : Optional cuVS Resource handle for reusing CUDA resources.
If Resources aren’t supplied, CUDA resources will be allocated inside this function and synchronized before the function exits. If resources are supplied, you will need to explicitly synchronize yourself by calling resources.sync() before accessing the output.
Returns#
codes_output : transformed dataset quantized into a uint8 vq_labels : VQ labels when VQ is used, None otherwise
Examples#
>>> import cupy as cp >>> from cuvs.preprocessing.quantize import pq >>> n_samples = 5000 >>> n_features = 64 >>> dataset = cp.random.random_sample((n_samples, n_features), ... dtype=cp.float32) >>> params = pq.QuantizerParams(pq_bits=8, pq_dim=16) >>> quantizer = pq.build(params, dataset) >>> transformed, _ = pq.transform(quantizer, dataset)