cuvs.neighbors.refine#
2 min read time
Attributes#
Functions#
|
check_cuvs(cuvsError_t status: cuvsError_t) |
auto_sync_resources(f) |
|
|
refine(dataset, queries, candidates, k=None, metric='sqeuclidean', indices=None, distances=None, resources=None) |
Module Contents#
- cuvs.neighbors.refine.check_cuvs(status: cuvsError_t)#
check_cuvs(cuvsError_t status: cuvsError_t)
Converts a status code into an exception
- cuvs.neighbors.refine.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
- cuvs.neighbors.refine.refine(*args, resources=None, **kwargs)#
refine(dataset, queries, candidates, k=None, metric=’sqeuclidean’, indices=None, distances=None, resources=None)
Refine nearest neighbor search.
Refinement is an operation that follows an approximate NN search. The approximate search has already selected n_candidates neighbor candidates for each query. We narrow it down to k neighbors. For each query, we calculate the exact distance between the query and its n_candidates neighbor candidate, and select the k nearest ones.
Input arrays can be either CUDA array interface compliant matrices or array interface compliant matrices in host memory. All array must be in the same memory space.
Parameters#
- datasetarray interface compliant matrix, shape (n_samples, dim)
Supported dtype [float32, int8, uint8, float16]
- queriesarray interface compliant matrix, shape (n_queries, dim)
Supported dtype [float32, int8, uint8, float16]
- candidatesarray interface compliant matrix, shape (n_queries, k0)
Supported dtype int64
- kint
Number of neighbors to search (k <= k0). Optional if indices or distances arrays are given (in which case their second dimension is k).
- metricstr
Name of distance metric to use, default =”sqeuclidean”
- indicesOptional array interface compliant matrix shape (n_queries, k).
If supplied, neighbor indices will be written here in-place. (default None). Supported dtype int64.
- distancesOptional array interface compliant matrix shape (n_queries, k).
If supplied, neighbor indices will be written here in-place. (default None) Supported dtype float.
- 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.
Examples#
>>> import cupy as cp >>> from cuvs.common import Resources >>> from cuvs.neighbors import ivf_pq, refine >>> n_samples = 50000 >>> n_features = 50 >>> n_queries = 1000 >>> dataset = cp.random.random_sample((n_samples, n_features), ... dtype=cp.float32) >>> resources = Resources() >>> index_params = ivf_pq.IndexParams(n_lists=1024, ... metric="sqeuclidean", ... pq_dim=10) >>> index = ivf_pq.build(index_params, dataset, resources=resources) >>> # Search using the built index >>> queries = cp.random.random_sample((n_queries, n_features), ... dtype=cp.float32) >>> k = 40 >>> _, candidates = ivf_pq.search(ivf_pq.SearchParams(), index, ... queries, k, resources=resources) >>> k = 10 >>> distances, neighbors = refine(dataset, queries, candidates, k)