暴力搜索#

暴力搜索方法运行KNN算法。它执行穷举搜索,与ANN方法相反,产生精确结果。

#include <cuvs/neighbors/brute_force.hpp>

命名空间 cuvs::neighbors::brute_force

索引#

template<typename T, typename DistT = T>
struct index : public cuvs::neighbors::index#
#include <brute_force.hpp>

暴力搜索索引。

该索引在设备内存中存储数据集及其范数。

模板参数:

T – 数据元素类型

公有函数

index(raft::resources const &handle)#

构造一个空索引。

构造一个空索引。该索引将需要通过build进行训练,或通过deserialize从保存的副本加载。

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

从数据集中构造一个暴力搜索索引

从数据集中构造一个暴力搜索索引。这使我们能够预先计算数据集的范数,从而在查询时提供速度优势。此索引将把主机数据集复制到设备上,并拥有任何预计算范数的所有权。

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

从数据集中构造一个暴力搜索索引

从数据集中构造一个暴力搜索索引。这使我们能够预先计算数据集的范数,从而在查询时提供速度优势。此索引将存储对数据集的非拥有引用,但会移动提供的任何范数。

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

从数据集中构造一个暴力搜索索引

此类存储对数据集和范数的非拥有引用。预计算范数可在查询时提供性能优势。

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

从数据集中构造一个暴力搜索索引

从数据集中构造一个暴力搜索索引。这使我们能够预先计算数据集的范数,从而在查询时提供速度优势。此索引将存储对数据集的非拥有引用,但会移动提供的任何范数。

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

从数据集中构造一个暴力搜索索引

此类存储对数据集和范数的非拥有引用,其中数据集以列主序格式在设备上提供。

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

用新数据集替换数据集。

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

用新数据集替换数据集。

我们在设备上创建数据集的副本。索引管理此副本的生命周期。

inline cuvs::distance::DistanceType metric() const noexcept#

用于检索的距离度量

inline DistT metric_arg() const noexcept#

度量参数

inline size_t size() const noexcept#

索引总长度(向量数量)。

inline size_t dim() const noexcept#

数据的维度。

inline raft::device_matrix_view<const T, int64_t, raft::row_major> dataset(
) const noexcept#

数据集 [size, dim]

inline raft::device_vector_view<const DistT, int64_t, raft::row_major> norms(
) const#

数据集范数

inline bool has_norms() const noexcept#

此索引是否包含数据集范数

索引构建#

cuvs::neighbors::brute_force::index<float, float> build(
raft::resources const &handle,
const cuvs::neighbors::brute_force::index_params &index_params,
raft::device_matrix_view<const float, int64_t, raft::row_major> dataset
)#

从数据集中构建索引以进行高效搜索。

使用示例

using namespace cuvs::neighbors;
// create and fill the index from a [N, D] dataset
brute_force::index_params index_params;
auto index = brute_force::build(handle, index_params, dataset);

参数:
  • handle[in]

  • index_params – 索引参数,例如要使用的距离度量

  • dataset[in] 指向行主序矩阵 [n_rows, dim] 的设备指针

返回:

构建的暴力搜索索引

cuvs::neighbors::brute_force::index<float, float> build(
raft::resources const &handle,
const cuvs::neighbors::brute_force::index_params &index_params,
raft::host_matrix_view<const float, int64_t, raft::row_major> dataset
)#

从数据集中构建索引以进行高效搜索。

参数:
  • handle[in]

  • index_params – 索引参数,例如要使用的距离度量

  • dataset[in] 指向行主序矩阵 [n_rows, dim] 的主机指针

返回:

构建的暴力搜索索引

cuvs::neighbors::brute_force::index<float, float> build(
raft::resources const &handle,
raft::device_matrix_view<const float, int64_t, raft::row_major> dataset,
cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Unexpanded,
float metric_arg = 0
)#
cuvs::neighbors::brute_force::index<half, float> build(
raft::resources const &handle,
const cuvs::neighbors::brute_force::index_params &index_params,
raft::device_matrix_view<const half, int64_t, raft::row_major> dataset
)#

从数据集中构建索引以进行高效搜索。

使用示例

using namespace cuvs::neighbors;
// create and fill the index from a [N, D] dataset
brute_force::index_params index_params;
auto index = brute_force::build(handle, index_params, dataset);

参数:
  • handle[in]

  • index_params – 索引参数,例如要使用的距离度量

  • dataset[in] 指向行主序矩阵 [n_rows, dim] 的设备指针

返回:

构建的暴力搜索索引

cuvs::neighbors::brute_force::index<half, float> build(
raft::resources const &handle,
const cuvs::neighbors::brute_force::index_params &index_params,
raft::host_matrix_view<const half, int64_t, raft::row_major> dataset
)#

从数据集中构建索引以进行高效搜索。

参数:
  • handle[in]

  • index_params – 索引参数,例如要使用的距离度量

  • dataset[in] 指向行主序矩阵 [n_rows, dim] 的主机指针

返回:

构建的暴力搜索索引

cuvs::neighbors::brute_force::index<half, float> build(
raft::resources const &handle,
raft::device_matrix_view<const half, int64_t, raft::row_major> dataset,
cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Unexpanded,
float metric_arg = 0
)#
cuvs::neighbors::brute_force::index<float, float> build(
raft::resources const &handle,
const cuvs::neighbors::brute_force::index_params &index_params,
raft::device_matrix_view<const float, int64_t, raft::col_major> dataset
)#

从数据集中构建索引以进行高效搜索。

使用示例

brute_force::index_params index_params;
auto index = brute_force::build(handle, index_params, dataset);

参数:
  • handle[in]

  • index_params – 索引参数,例如要使用的距离度量

  • dataset[in] 指向行主序矩阵 [n_rows, dim] 的设备指针

返回:

构建的暴力搜索索引

cuvs::neighbors::brute_force::index<float, float> build(
raft::resources const &handle,
raft::device_matrix_view<const float, int64_t, raft::col_major> dataset,
cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Unexpanded,
float metric_arg = 0
)#
cuvs::neighbors::brute_force::index<half, float> build(
raft::resources const &handle,
const cuvs::neighbors::brute_force::index_params &index_params,
raft::device_matrix_view<const half, int64_t, raft::col_major> dataset
)#

从数据集中构建索引以进行高效搜索。

使用示例

brute_force::index_params index_params;
auto index = brute_force::build(handle, index_params, dataset);

参数:
  • handle[in]

  • index_params – 索引参数,例如要使用的距离度量

  • dataset[in] 指向行主序矩阵 [n_rows, dim] 的设备指针

返回:

构建的暴力搜索索引

cuvs::neighbors::brute_force::index<half, float> build(
raft::resources const &handle,
raft::device_matrix_view<const half, int64_t, raft::col_major> dataset,
cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Unexpanded,
float metric_arg = 0
)#

索引序列化#

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

将索引保存到文件。序列化格式可能会发生变化,因此不保证加载使用 cuvs 之前版本保存的索引能够正常工作。

#include <raft/core/resources.hpp>
#include <cuvs/neighbors/brute_force.hpp>

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
// create an index with `auto index = brute_force::build(...);`
cuvs::neighbors::brute_force::serialize(handle, filename, index);
模板参数:

T – 数据元素类型

参数:
  • handle[in] raft 句柄

  • filename[in] 用于保存索引的文件名

  • index[in] 暴力搜索索引

  • include_dataset[in] 是否在序列化输出中包含数据集

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

将索引保存到文件。序列化格式可能会发生变化,因此不保证加载使用 cuvs 之前版本保存的索引能够正常工作。

#include <raft/core/resources.hpp>
#include <cuvs/neighbors/brute_force.hpp>

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
// create an index with `auto index = brute_force::build(...);`
cuvs::neighbors::brute_force::serialize(handle, filename, index);
模板参数:

T – 数据元素类型

参数:
  • handle[in] raft 句柄

  • filename[in] 用于保存索引的文件名

  • index[in] 暴力搜索索引

  • include_dataset[in] 是否在序列化输出中包含数据集

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

将索引写入输出流。序列化格式可能会发生变化,因此不保证加载使用 cuvs 之前版本保存的索引能够正常工作。

#include <raft/core/resources.hpp>
#include <cuvs/neighbors/brute_force.hpp>

raft::resources handle;

// create an output stream
std::ostream os(std::cout.rdbuf());
// create an index with `auto index = cuvs::neighbors::brute_force::build(...);`
cuvs::neighbors::brute_force::serialize(handle, os, index);
参数:
  • handle[in] raft 句柄

  • os[in] 输出流

  • index[in] 暴力搜索索引

  • include_dataset[in] 是否将数据集写入文件。

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

将索引写入输出流。序列化格式可能会发生变化,因此不保证加载使用 cuvs 之前版本保存的索引能够正常工作。

#include <raft/core/resources.hpp>
#include <cuvs/neighbors/brute_force.hpp>

raft::resources handle;

// create an output stream
std::ostream os(std::cout.rdbuf());
// create an index with `auto index = cuvs::neighbors::brute_force::build(...);`
cuvs::neighbors::brute_force::serialize(handle, os, index);
参数:
  • handle[in] raft 句柄

  • os[in] 输出流

  • index[in] 暴力搜索索引

  • include_dataset[in] 是否将数据集写入文件。

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

从文件加载索引。序列化格式可能会发生变化,因此不保证加载使用 cuvs 之前版本保存的索引能够正常工作。

#include <raft/core/resources.hpp>
#include <cuvs/neighbors/brute_force.hpp>

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
using T    = half; // data element type
brute_force::index<T, float> index(handle);
cuvs::neighbors::brute_force::deserialize(handle, filename, index);
参数:
  • handle[in] raft 句柄

  • filename[in] 存储索引的文件名

  • index[out] 暴力搜索索引

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

从文件加载索引。序列化格式可能会发生变化,因此不保证加载使用 cuvs 之前版本保存的索引能够正常工作。

#include <raft/core/resources.hpp>
#include <cuvs/neighbors/brute_force.hpp>

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
using T    = float; // data element type
brute_force::index<T, float> index(handle);
cuvs::neighbors::brute_force::deserialize(handle, filename, index);
参数:
  • handle[in] raft 句柄

  • filename[in] 存储索引的文件名

  • index[out] 暴力搜索索引

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

从输入流加载索引。序列化格式可能会发生变化,因此不保证加载使用 cuvs 之前版本保存的索引能够正常工作。

#include <raft/core/resources.hpp>
#include <cuvs/neighbors/brute_force.hpp>

raft::resources handle;

// create an input stream
std::istream is(std::cin.rdbuf());
using T    = half; // data element type
brute_force::index<T, float> index(handle);
cuvs::neighbors::brute_force::deserialize(handle, is, index);
参数:
  • handle[in] raft 句柄

  • is[in] 输入流

  • index[out] 暴力搜索索引

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

从输入流加载索引。序列化格式可能会发生变化,因此不保证加载使用 cuvs 之前版本保存的索引能够正常工作。

#include <raft/core/resources.hpp>
#include <cuvs/neighbors/brute_force.hpp>

raft::resources handle;

// create an input stream
std::istream is(std::cin.rdbuf());
using T    = float; // data element type
brute_force::index<T, float> index(handle);
cuvs::neighbors::brute_force::deserialize(handle, is, index);
参数:
  • handle[in] raft 句柄

  • is[in] 输入流

  • index[out] 暴力搜索索引