cuvs.neighbors.nn_descent#
3 min read time
Submodules#
Classes#
NN-Descent index object. This object stores the trained NN-Descent index, |
|
IndexParams(metric=None, *, metric_arg=None, graph_degree=None, intermediate_graph_degree=None, max_iterations=None, termination_threshold=None, return_distances=None, dist_comp_dtype='auto') |
Functions#
|
build(IndexParams index_params, dataset, graph=None, resources=None) |
Package Contents#
- class cuvs.neighbors.nn_descent.Index#
NN-Descent index object. This object stores the trained NN-Descent index, which can be used to get the NN-Descent graph and distances after building
- static __reduce__(*args, **kwargs)#
Index.__reduce_cython__(self)
- static __setstate__(*args, **kwargs)#
Index.__setstate_cython__(self, __pyx_state)
- static _get_distances(*args, resources=None, **kwargs)#
Index._get_distances(self, resources=None)
- static _get_graph(*args, resources=None, **kwargs)#
Index._get_graph(self, resources=None)
- __repr__()#
Return repr(self).
- class cuvs.neighbors.nn_descent.IndexParams#
IndexParams(metric=None, *, metric_arg=None, graph_degree=None, intermediate_graph_degree=None, max_iterations=None, termination_threshold=None, return_distances=None, dist_comp_dtype=’auto’)
Parameters to build NN-Descent Index
Parameters#
- metricstr, default = “sqeuclidean”
String denoting the metric type. Supported metrics are l2, euclidean, sqeuclidean, inner_product, cosine, and bitwise_hamming (bitwise_hamming is for int8 and uint8 data types only)
- graph_degreeint
For an input dataset of dimensions (N, D), determines the final dimensions of the all-neighbors knn graph which turns out to be of dimensions (N, graph_degree)
- intermediate_graph_degreeint
Internally, nn-descent builds an all-neighbors knn graph of dimensions (N, intermediate_graph_degree) before selecting the final graph_degree neighbors. It’s recommended that intermediate_graph_degree >= 1.5 * graph_degree
- max_iterationsint
The number of iterations that nn-descent will refine the graph for. More iterations produce a better quality graph at cost of performance
- termination_thresholdfloat
The delta at which nn-descent will terminate its iterations
- return_distancesbool
Whether to return distances array
- dist_comp_dtypestr, default = “auto”
Dtype to use for distance computation. Supported dtypes are auto, fp32, and fp16 auto automatically determines the best dtype for distance computation based on the dataset dimensions. fp32 uses fp32 distance computation for better precision at the cost of performance and memory usage. This option is only valid when data type is fp32. fp16 uses fp16 distance computation for better performance and memory usage at the cost of precision.
- static __reduce__(*args, **kwargs)#
IndexParams.__reduce_cython__(self)
- static __setstate__(*args, **kwargs)#
IndexParams.__setstate_cython__(self, __pyx_state)
- get_handle()#
IndexParams.get_handle(self)
Get a pointer to the underlying C object.
- cuvs.neighbors.nn_descent.build(*args, resources=None, **kwargs)#
build(IndexParams index_params, dataset, graph=None, resources=None)
Build KNN graph from the dataset
Parameters#
index_params :
cuvs.neighbors.nn_descent.IndexParamsdataset : Array interface compliant matrix, on either host or device memorySupported dtype [float32, int8, uint8]
graph : Optional host matrix for storing output graph 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#
index: py:class:cuvs.neighbors.nn_descent.Index
Examples#
>>> import cupy as cp >>> from cuvs.neighbors import nn_descent >>> n_samples = 50000 >>> n_features = 50 >>> n_queries = 1000 >>> k = 10 >>> dataset = cp.random.random_sample((n_samples, n_features), ... dtype=cp.float32) >>> build_params = nn_descent.IndexParams(metric="sqeuclidean") >>> index = nn_descent.build(build_params, dataset) >>> graph = index.graph