Brute Force

View as Markdown

Source header: cuvs/neighbors/brute_force.hpp

Bruteforce index

neighbors::brute_force::index

Brute Force index.

The index stores the dataset and norms for the dataset in device memory.

1template <typename T, typename DistT = T>
2struct index : cuvs::neighbors::index { ... };

neighbors::brute_force::index::index

Construct an empty index.

1index(raft::resources const& handle);

Constructs an empty index. This index will either need to be trained with build or loaded from a saved copy with deserialize

Parameters

NameDirectionTypeDescription
handleraft::resources const&

Returns

void

Additional overload: neighbors::brute_force::index::index

Construct a brute force index from dataset

1index(raft::resources const& res,
2raft::host_matrix_view<const T, int64_t, raft::row_major> dataset_view,
3std::optional<raft::device_vector<DistT, int64_t>>&& norms,
4cuvs::distance::DistanceType metric,
5DistT metric_arg = 0.0);

Constructs a brute force index from a dataset. This lets us precompute norms for the dataset, providing a speed benefit over doing this at query time. This index will copy the host dataset onto the device, and take ownership of any precaculated norms.

Parameters

NameDirectionTypeDescription
resraft::resources const&
dataset_viewraft::host_matrix_view<const T, int64_t, raft::row_major>
normsstd::optional<raft::device_vector<DistT, int64_t>>&&
metriccuvs::distance::DistanceType
metric_argDistTDefault: 0.0.

Returns

void

Additional overload: neighbors::brute_force::index::index

Construct a brute force index from dataset

1index(raft::resources const& res,
2raft::device_matrix_view<const T, int64_t, raft::row_major> dataset_view,
3std::optional<raft::device_vector<DistT, int64_t>>&& norms,
4cuvs::distance::DistanceType metric,
5DistT metric_arg = 0.0);

Constructs a brute force index from a dataset. This lets us precompute norms for the dataset, providing a speed benefit over doing this at query time. This index will store a non-owning reference to the dataset, but will move any norms supplied.

Parameters

NameDirectionTypeDescription
resraft::resources const&
dataset_viewraft::device_matrix_view<const T, int64_t, raft::row_major>
normsstd::optional<raft::device_vector<DistT, int64_t>>&&
metriccuvs::distance::DistanceType
metric_argDistTDefault: 0.0.

Returns

void

Additional overload: neighbors::brute_force::index::index

Construct a brute force index from dataset

1index(raft::resources const& res,
2raft::device_matrix_view<const T, int64_t, raft::row_major> dataset_view,
3std::optional<raft::device_vector_view<const DistT, int64_t>> norms_view,
4cuvs::distance::DistanceType metric,
5DistT metric_arg = 0.0);

This class stores a non-owning reference to the dataset and norms. Having precomputed norms gives us a performance advantage at query time.

Parameters

NameDirectionTypeDescription
resraft::resources const&
dataset_viewraft::device_matrix_view<const T, int64_t, raft::row_major>
norms_viewstd::optional<raft::device_vector_view<const DistT, int64_t>>
metriccuvs::distance::DistanceType
metric_argDistTDefault: 0.0.

Returns

void

Additional overload: neighbors::brute_force::index::index

Construct a brute force index from dataset

1index(raft::resources const& res,
2raft::device_matrix_view<const T, int64_t, raft::col_major> dataset_view,
3std::optional<raft::device_vector<DistT, int64_t>>&& norms,
4cuvs::distance::DistanceType metric,
5DistT metric_arg = 0.0);

Constructs a brute force index from a dataset. This lets us precompute norms for the dataset, providing a speed benefit over doing this at query time. This index will store a non-owning reference to the dataset, but will move any norms supplied.

Parameters

NameDirectionTypeDescription
resraft::resources const&
dataset_viewraft::device_matrix_view<const T, int64_t, raft::col_major>
normsstd::optional<raft::device_vector<DistT, int64_t>>&&
metriccuvs::distance::DistanceType
metric_argDistTDefault: 0.0.

Returns

void

Additional overload: neighbors::brute_force::index::index

Construct a brute force index from dataset

1index(raft::resources const& res,
2raft::device_matrix_view<const T, int64_t, raft::col_major> dataset_view,
3std::optional<raft::device_vector_view<const DistT, int64_t>> norms_view,
4cuvs::distance::DistanceType metric,
5DistT metric_arg = 0.0);

This class stores a non-owning reference to the dataset and norms, with the dataset being supplied on device in a col_major format

Parameters

NameDirectionTypeDescription
resraft::resources const&
dataset_viewraft::device_matrix_view<const T, int64_t, raft::col_major>
norms_viewstd::optional<raft::device_vector_view<const DistT, int64_t>>
metriccuvs::distance::DistanceType
metric_argDistTDefault: 0.0.

Returns

void

neighbors::brute_force::index::update_dataset

Replace the dataset with a new dataset.

1void update_dataset(raft::resources const& res,
2raft::device_matrix_view<const T, int64_t, raft::row_major> dataset);

Parameters

NameDirectionTypeDescription
resraft::resources const&
datasetraft::device_matrix_view<const T, int64_t, raft::row_major>

Returns

void

Additional overload: neighbors::brute_force::index::update_dataset

Replace the dataset with a new dataset.

1void update_dataset(raft::resources const& res,
2raft::host_matrix_view<const T, int64_t, raft::row_major> dataset);

We create a copy of the dataset on the device. The index manages the lifetime of this copy.

Parameters

NameDirectionTypeDescription
resraft::resources const&
datasetraft::host_matrix_view<const T, int64_t, raft::row_major>

Returns

void

neighbors::brute_force::index::metric

Distance metric used for retrieval

1cuvs::distance::DistanceType metric() const noexcept;

Returns

cuvs::distance::DistanceType

neighbors::brute_force::index::metric_arg

Metric argument

1DistT metric_arg() const noexcept;

Returns

DistT

neighbors::brute_force::index::size

Total length of the index (number of vectors).

1size_t size() const noexcept;

Returns

size_t

neighbors::brute_force::index::dim

Dimensionality of the data.

1size_t dim() const noexcept;

Returns

size_t

neighbors::brute_force::index::dataset

Dataset [size, dim]

1raft::device_matrix_view<const T, int64_t, raft::row_major> dataset() const noexcept;

Returns

raft::device_matrix_view<const T, int64_t, raft::row_major>

neighbors::brute_force::index::norms

Dataset norms

1raft::device_vector_view<const DistT, int64_t, raft::row_major> norms() const;

Returns

raft::device_vector_view<const DistT, int64_t, raft::row_major>

neighbors::brute_force::index::has_norms

Whether ot not this index has dataset norms

1inline bool has_norms() const noexcept;

Returns

inline bool

Bruteforce index build

neighbors::brute_force::build

Build the index from the dataset for efficient search.

1auto build(raft::resources const& handle,
2const cuvs::neighbors::brute_force::index_params& index_params,
3raft::device_matrix_view<const float, int64_t, raft::row_major> dataset)
4-> cuvs::neighbors::brute_force::index<float, float>;

Usage example:

Parameters

NameDirectionTypeDescription
handleinraft::resources const&
index_paramsinconst cuvs::neighbors::brute_force::index_params&parameters such as the distance metric to use
datasetinraft::device_matrix_view<const float, int64_t, raft::row_major>a device pointer to a row-major matrix [n_rows, dim]

Returns

cuvs::neighbors::brute_force::index<float, float>

Additional overload: neighbors::brute_force::build

Build the index from the dataset for efficient search.

1auto build(raft::resources const& handle,
2const cuvs::neighbors::brute_force::index_params& index_params,
3raft::host_matrix_view<const float, int64_t, raft::row_major> dataset)
4-> cuvs::neighbors::brute_force::index<float, float>;

Parameters

NameDirectionTypeDescription
handleinraft::resources const&
index_paramsinconst cuvs::neighbors::brute_force::index_params&parameters such as the distance metric to use
datasetinraft::host_matrix_view<const float, int64_t, raft::row_major>a host pointer to a row-major matrix [n_rows, dim]

Returns

cuvs::neighbors::brute_force::index<float, float>

Additional overload: neighbors::brute_force::build

Build the index from the dataset for efficient search.

1auto build(raft::resources const& handle,
2const cuvs::neighbors::brute_force::index_params& index_params,
3raft::device_matrix_view<const half, int64_t, raft::row_major> dataset)
4-> cuvs::neighbors::brute_force::index<half, float>;

Usage example:

Parameters

NameDirectionTypeDescription
handleinraft::resources const&
index_paramsinconst cuvs::neighbors::brute_force::index_params&parameters such as the distance metric to use
datasetinraft::device_matrix_view<const half, int64_t, raft::row_major>a device pointer to a row-major matrix [n_rows, dim]

Returns

cuvs::neighbors::brute_force::index<half, float>

Additional overload: neighbors::brute_force::build

Build the index from the dataset for efficient search.

1auto build(raft::resources const& handle,
2const cuvs::neighbors::brute_force::index_params& index_params,
3raft::host_matrix_view<const half, int64_t, raft::row_major> dataset)
4-> cuvs::neighbors::brute_force::index<half, float>;

Parameters

NameDirectionTypeDescription
handleinraft::resources const&
index_paramsinconst cuvs::neighbors::brute_force::index_params&parameters such as the distance metric to use
datasetinraft::host_matrix_view<const half, int64_t, raft::row_major>a host pointer to a row-major matrix [n_rows, dim]

Returns

cuvs::neighbors::brute_force::index<half, float>

Additional overload: neighbors::brute_force::build

Build the index from the dataset for efficient search.

1auto build(raft::resources const& handle,
2const cuvs::neighbors::brute_force::index_params& index_params,
3raft::device_matrix_view<const float, int64_t, raft::col_major> dataset)
4-> cuvs::neighbors::brute_force::index<float, float>;

Usage example:

Parameters

NameDirectionTypeDescription
handleinraft::resources const&
index_paramsinconst cuvs::neighbors::brute_force::index_params&parameters such as the distance metric to use
datasetinraft::device_matrix_view<const float, int64_t, raft::col_major>a device pointer to a col-major matrix [n_rows, dim]

Returns

cuvs::neighbors::brute_force::index<float, float>

Additional overload: neighbors::brute_force::build

Build the index from the dataset for efficient search.

1auto build(raft::resources const& handle,
2const cuvs::neighbors::brute_force::index_params& index_params,
3raft::device_matrix_view<const half, int64_t, raft::col_major> dataset)
4-> cuvs::neighbors::brute_force::index<half, float>;

Usage example:

Parameters

NameDirectionTypeDescription
handleinraft::resources const&
index_paramsinconst cuvs::neighbors::brute_force::index_params&parameters such as the distance metric to use
datasetinraft::device_matrix_view<const half, int64_t, raft::col_major>a device pointer to a col-major matrix [n_rows, dim]

Returns

cuvs::neighbors::brute_force::index<half, float>

Sparse Brute Force index

neighbors::brute_force::sparse_index

Sparse Brute Force index.

1template <typename T, typename IdxT>
2struct sparse_index { ... };

neighbors::brute_force::sparse_index::sparse_index

Construct a sparse brute force sparse_index from dataset

1sparse_index(raft::resources const& res,
2raft::device_csr_matrix_view<const T, IdxT, IdxT, IdxT> dataset,
3cuvs::distance::DistanceType metric,
4T metric_arg);

Parameters

NameDirectionTypeDescription
resraft::resources const&
datasetraft::device_csr_matrix_view<const T, IdxT, IdxT, IdxT>
metriccuvs::distance::DistanceType
metric_argT

Returns

void

neighbors::brute_force::sparse_index::metric

Distance metric used for retrieval

1cuvs::distance::DistanceType metric() const noexcept;

Returns

cuvs::distance::DistanceType

neighbors::brute_force::sparse_index::metric_arg

Metric argument

1T metric_arg() const noexcept;

Returns

T

neighbors::brute_force::sparse_search_params

Sparse Brute Force index search

1struct sparse_search_params { ... };

Bruteforce index serialize functions

neighbors::brute_force::serialize

Save the index to file.

1void serialize(raft::resources const& handle,
2const std::string& filename,
3const cuvs::neighbors::brute_force::index<half, float>& index,
4bool include_dataset = true);

The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.

output

Template Parameters

NameTypeDescription
Tdata element type

Parameters

NameDirectionTypeDescription
handleinraft::resources const&the raft handle
filenameinconst std::string&the file name for saving the index
indexinconst cuvs::neighbors::brute_force::index<half, float>&brute force index
include_datasetinboolwhether to include the dataset in the serialized Default: true.

Returns

void

Additional overload: neighbors::brute_force::serialize

Save the index to file.

1void serialize(raft::resources const& handle,
2const std::string& filename,
3const cuvs::neighbors::brute_force::index<float, float>& index,
4bool include_dataset = true);

The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.

output

Template Parameters

NameTypeDescription
Tdata element type

Parameters

NameDirectionTypeDescription
handleinraft::resources const&the raft handle
filenameinconst std::string&the file name for saving the index
indexinconst cuvs::neighbors::brute_force::index<float, float>&brute force index
include_datasetinboolwhether to include the dataset in the serialized Default: true.

Returns

void

Additional overload: neighbors::brute_force::serialize

Write the index to an output stream

1void serialize(raft::resources const& handle,
2std::ostream& os,
3const cuvs::neighbors::brute_force::index<half, float>& index,
4bool include_dataset = true);

The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.

Parameters

NameDirectionTypeDescription
handleinraft::resources const&the raft handle
osinstd::ostream&output stream
indexinconst cuvs::neighbors::brute_force::index<half, float>&brute force index
include_datasetinboolWhether or not to write out the dataset to the file. Default: true.

Returns

void

Additional overload: neighbors::brute_force::serialize

Write the index to an output stream

1void serialize(raft::resources const& handle,
2std::ostream& os,
3const cuvs::neighbors::brute_force::index<float, float>& index,
4bool include_dataset = true);

The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.

Parameters

NameDirectionTypeDescription
handleinraft::resources const&the raft handle
osinstd::ostream&output stream
indexinconst cuvs::neighbors::brute_force::index<float, float>&brute force index
include_datasetinboolWhether or not to write out the dataset to the file. Default: true.

Returns

void

neighbors::brute_force::deserialize

Load index from file.

1void deserialize(raft::resources const& handle,
2const std::string& filename,
3cuvs::neighbors::brute_force::index<half, float>* index);

The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.

Parameters

NameDirectionTypeDescription
handleinraft::resources const&the raft handle
filenameinconst std::string&the name of the file that stores the index
indexoutcuvs::neighbors::brute_force::index<half, float>*brute force index

Returns

void

Additional overload: neighbors::brute_force::deserialize

Load index from file.

1void deserialize(raft::resources const& handle,
2const std::string& filename,
3cuvs::neighbors::brute_force::index<float, float>* index);

The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.

Parameters

NameDirectionTypeDescription
handleinraft::resources const&the raft handle
filenameinconst std::string&the name of the file that stores the index
indexoutcuvs::neighbors::brute_force::index<float, float>*brute force index

Returns

void

Additional overload: neighbors::brute_force::deserialize

Load index from input stream

1void deserialize(raft::resources const& handle,
2std::istream& is,
3cuvs::neighbors::brute_force::index<half, float>* index);

The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.

Parameters

NameDirectionTypeDescription
handleinraft::resources const&the raft handle
isinstd::istream&input stream
indexoutcuvs::neighbors::brute_force::index<half, float>*brute force index

Returns

void

Additional overload: neighbors::brute_force::deserialize

Load index from input stream

1void deserialize(raft::resources const& handle,
2std::istream& is,
3cuvs::neighbors::brute_force::index<float, float>* index);

The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.

Parameters

NameDirectionTypeDescription
handleinraft::resources const&the raft handle
isinstd::istream&input stream
indexoutcuvs::neighbors::brute_force::index<float, float>*brute force index

Returns

void