IVF-Flat#

IVF-Flat 方法是一种 ANN 算法。它使用倒排文件索引 (IVF) 和未经修改(即,扁平)的向量。该算法提供了简单的调整参数来减少总体搜索空间,并权衡准确性和速度。

#include <cuvs/neighbors/ivf_flat.hpp>

namespace cuvs::neighbors::ivf_flat

索引构建参数#

static uint32_t kIndexGroupSize = 32#

交错组的大小(参见 index::data 的描述)。

struct index_params : public cuvs::neighbors::index_params#
#include <ivf_flat.hpp>

公共成员

uint32_t n_lists = 1024#

倒排列表(聚类)的数量

uint32_t kmeans_n_iters = 20#

搜索 kmeans 中心(索引构建)的迭代次数。

double kmeans_trainset_fraction = 0.5#

在迭代 kmeans 构建期间使用的数据比例。

bool adaptive_centers = false#

默认情况下 (adaptive_centers = false),聚类中心在 ivf_flat::build 中进行训练,并且在 ivf_flat::extend 中从未修改。因此,在几次使用新数据调用 (ivf_flat::extend) 后,您可能需要从头开始重新训练索引,这些新数据的分布可能不再代表原始训练集。

另一种行为 (adaptive_centers = true) 是在添加新数据时更新聚类中心。在这种情况下,index.centers() 始终精确地对应于相应聚类中数据的中心。这种行为的缺点是,中心取决于添加新数据的顺序(通过添加数据的分类);也就是说,index.centers() 会随着新添加数据分布的变化而“漂移”。

bool conservative_memory_allocation = false#

默认情况下,算法会为单个聚类 (list_data) 分配比必要更多的空间。这有助于分摊内存分配的成本,并减少在重复调用 extend(扩展数据库)期间的数据拷贝次数。

另一种是保守分配行为;启用时,算法始终分配存储给定记录数量所需的最小内存量。如果您希望尽可能少地使用 GPU 内存来存储数据库,请将此标志设置为 true

bool add_data_on_build = true#

是否将数据集内容添加到索引,即:

  • true 表示在调用 build 后,索引已填充数据集向量并可供搜索。

  • false 表示 build 仅训练底层模型(例如量化器或聚类),但索引为空;之后您需要调用 extend 来填充索引。

索引搜索参数#

template<typename ValueT, typename IdxT, typename SizeT = uint32_t>
using list_data = ivf::list<list_spec, SizeT, ValueT, IdxT>#
struct search_params : public cuvs::neighbors::search_params#
#include <ivf_flat.hpp>

公共成员

uint32_t n_probes = 20#

要搜索的聚类数量。

template<typename SizeT, typename ValueT, typename IdxT>
struct list_spec#
#include <ivf_flat.hpp>

公共函数

inline list_extents make_list_extents(SizeT n_rows) const#

确定足以容纳给定数据量的数组范围。

索引#

template<typename T, typename IdxT>
struct index : public cuvs::neighbors::index#
#include <ivf_flat.hpp>

IVF-flat 索引。

模板参数:
  • T – 数据元素类型

  • IdxT – 源数据集中索引的类型

公共函数

index(raft::resources const &res)#

构造一个空索引。

构造一个空索引。此索引需要使用 build 进行训练,或使用 deserialize 从保存的副本加载。

index(
raft::resources const &res,
const index_params &params,
uint32_t dim
)#

构造一个空索引。需要进行训练然后填充。

index(
raft::resources const &res,
cuvs::distance::DistanceType metric,
uint32_t n_lists,
bool adaptive_centers,
bool conservative_memory_allocation,
uint32_t dim
)#

构造一个空索引。需要进行训练然后填充。

uint32_t veclen() const noexcept#

向量化加载/存储元素的数量,决定了交错数据块的大小。

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

用于聚类的距离指标。

bool adaptive_centers() const noexcept#

扩展索引时 centers() 是否会改变 (ivf_flat::extend)。

raft::device_vector_view<uint32_t, uint32_t> list_sizes() noexcept#

倒排列表数据 [size, dim]。

数据由按其标签分组(到聚类/列表)的数据集行组成。在每个列表(聚类)中,数据被分组为 kIndexGroupSize 个交错向量的块。注意,总索引长度略大于源数据集长度,因为每个聚类都用 kIndexGroupSize 个元素进行填充。

交错模式:在 kIndexGroupSize 行的组内,数据以等于 veclen * sizeof(T) 的块大小交错。也就是说,一行中 veclen 个连续组件的块后面跟着下一行相同大小的块,依此类推。

示例:veclen = 2, dim = 6, kIndexGroupSize = 32, list_size = 31

x[ 0, 0], x[ 0, 1], x[ 1, 0], x[ 1, 1], ... x[14, 0], x[14, 1], x[15, 0], x[15, 1],
x[16, 0], x[16, 1], x[17, 0], x[17, 1], ... x[30, 0], x[30, 1],    -    ,    -    ,
x[ 0, 2], x[ 0, 3], x[ 1, 2], x[ 1, 3], ... x[14, 2], x[14, 3], x[15, 2], x[15, 3],
x[16, 2], x[16, 3], x[17, 2], x[17, 3], ... x[30, 2], x[30, 3],    -    ,    -    ,
x[ 0, 4], x[ 0, 5], x[ 1, 4], x[ 1, 5], ... x[14, 4], x[14, 5], x[15, 4], x[15, 5],
x[16, 4], x[16, 5], x[17, 4], x[17, 5], ... x[30, 4], x[30, 5],    -    ,    -    ,
列表(聚类)的大小 [n_lists] 注:如果共享列表已被另一个索引扩展,这可能与实际列表大小不同。

raft::device_matrix_view<float, uint32_t, raft::row_major> centers(
) noexcept#

对应于列表的 k-means 聚类中心 [n_lists, dim]

std::optional<raft::device_vector_view<float, uint32_t>> center_norms(
) noexcept#

(可选)根据所选距离指标预计算的 centers 的范数 [n_lists]。

注:如果索引为空或距离指标不需要计算中心范数,此项可能为空。

raft::host_vector_view<IdxT, uint32_t> accum_sorted_sizes(
) noexcept#

累积的列表大小,按降序排序 [n_lists + 1]。最后一个值包含索引的总长度。索引零处的值始终为零。

也就是说,此 span 的内容就像 list_sizes 经过排序然后累积一样。

此 span 用于在搜索期间估计工作空间的最大大小。

IdxT size() const noexcept#

索引的总长度。

uint32_t dim() const noexcept#

数据的维度。

uint32_t n_lists() const noexcept#

聚类/倒排列表的数量。

raft::device_vector_view<IdxT*, uint32_t> inds_ptrs() noexcept#

指向倒排列表(聚类)索引的指针 [n_lists]。

bool conservative_memory_allocation() const noexcept#

扩展列表(聚类)数据时是否使用保守内存分配(参见 index_params.conservative_memory_allocation)。

std::vector<std::shared_ptr<list_data<T, IdxT>>> &lists(
) noexcept#

列表的数据和索引。

索引构建#

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

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

使用示例

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

参数:
  • handle – [输入]

  • index_params – 配置索引构建

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

返回值:

构建的 ivf-flat 索引

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

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// create and fill the index from a [N, D] dataset
ivf_flat::index<decltype(dataset::value_type), decltype(dataset::index_type)> index;
ivf_flat::build(handle, dataset, index_params, index);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

  • dataset – [输入] 指向行主序矩阵 [n_rows, dim] 的 raft::device_matrix_view

  • idx – [输出] 对 ivf_flat::index 的引用

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

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

使用示例

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

参数:
  • handle – [输入]

  • index_params – 配置索引构建

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

返回值:

构建的 ivf-flat 索引

void build(
raft::resources const &handle,
const cuvs::neighbors::ivf_flat::index_params &index_params,
raft::device_matrix_view<const int8_t, int64_t, raft::row_major> dataset,
cuvs::neighbors::ivf_flat::index<int8_t, int64_t> &idx
)#

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// create and fill the index from a [N, D] dataset
ivf_flat::index<decltype(dataset::value_type), decltype(dataset::index_type)> index;
ivf_flat::build(handle, dataset, index_params, index);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

  • dataset – [输入] 指向行主序矩阵 [n_rows, dim] 的 raft::device_matrix_view

  • idx – [输出] 对 ivf_flat::index 的引用

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

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

使用示例

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

参数:
  • handle – [输入]

  • index_params – 配置索引构建

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

返回值:

构建的 ivf-flat 索引

void build(
raft::resources const &handle,
const cuvs::neighbors::ivf_flat::index_params &index_params,
raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> dataset,
cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> &idx
)#

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// create and fill the index from a [N, D] dataset
ivf_flat::index<decltype(dataset::value_type), decltype(dataset::index_type)> index;
ivf_flat::build(handle, dataset, index_params, index);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

  • dataset – [输入] 指向行主序矩阵 [n_rows, dim] 的 raft::device_matrix_view

  • idx – [输出] 对 ivf_flat::index 的引用

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

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

注意,如果 index_params.add_data_on_build 设置为 true,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping. This is only applicable if index_params.add_data_on_build is set to true
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// create and fill the index from a [N, D] dataset
auto index = ivf_flat::build(handle, dataset, index_params);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

  • dataset – [输入] 指向行主序矩阵 [n_rows, dim] 的 raft::host_matrix_view

返回值:

构建的 ivf-flat 索引

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

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

注意,如果 index_params.add_data_on_build 设置为 true,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping. This is only applicable if index_params.add_data_on_build is set to true
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// create and fill the index from a [N, D] dataset
ivf_flat::index<decltype(dataset::value_type), decltype(dataset::index_type)> index;
ivf_flat::build(handle, dataset, index_params, index);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

  • dataset – [输入] 指向行主序矩阵 [n_rows, dim] 的 raft::host_matrix_view

  • idx – [输出] 对 ivf_flat::index 的引用

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

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

注意,如果 index_params.add_data_on_build 设置为 true,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping. This is only applicable if index_params.add_data_on_build is set to true
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// create and fill the index from a [N, D] dataset
auto index = ivf_flat::build(handle, dataset, index_params);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

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

返回值:

构建的 ivf-flat 索引

void build(
raft::resources const &handle,
const cuvs::neighbors::ivf_flat::index_params &index_params,
raft::host_matrix_view<const int8_t, int64_t, raft::row_major> dataset,
cuvs::neighbors::ivf_flat::index<int8_t, int64_t> &idx
)#

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

注意,如果 index_params.add_data_on_build 设置为 true,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping. This is only applicable if index_params.add_data_on_build is set to true
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// create and fill the index from a [N, D] dataset
ivf_flat::index<decltype(dataset::value_type), decltype(dataset::index_type)> index;
ivf_flat::build(handle, dataset, index_params, index);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

  • dataset – [输入] 指向行主序矩阵 [n_rows, dim] 的 raft::host_matrix_view

  • idx – [输出] 对 ivf_flat::index 的引用

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

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

注意,如果 index_params.add_data_on_build 设置为 true,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping. This is only applicable if index_params.add_data_on_build is set to true
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// create and fill the index from a [N, D] dataset
auto index = ivf_flat::build(handle, dataset, index_params);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

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

返回值:

构建的 ivf-flat 索引

void build(
raft::resources const &handle,
const cuvs::neighbors::ivf_flat::index_params &index_params,
raft::host_matrix_view<const uint8_t, int64_t, raft::row_major> dataset,
cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> &idx
)#

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

注:目前支持以下距离指标:

  • L2Expanded

  • L2Unexpanded

  • 内积 (InnerProduct)

  • CosineExpanded

注意,如果 index_params.add_data_on_build 设置为 true,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
// use default index parameters
ivf_flat::index_params index_params;
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping. This is only applicable if index_params.add_data_on_build is set to true
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// create and fill the index from a [N, D] dataset
ivf_flat::index<decltype(dataset::value_type), decltype(dataset::index_type)> index;
ivf_flat::build(handle, dataset, index_params, index);

参数:
  • handle – [输入]

  • index_params – 配置索引构建

  • dataset – [输入] 指向行主序矩阵 [n_rows, dim] 的 raft::host_matrix_view

  • idx – [输出] 对 ivf_flat::index 的引用

索引扩展#

cuvs::neighbors::ivf_flat::index<float, int64_t> extend(
raft::resources const &handle,
raft::device_matrix_view<const float, int64_t, raft::row_major> new_vectors,
std::optional<raft::device_vector_view<const int64_t, int64_t>> new_indices,
const cuvs::neighbors::ivf_flat::index<float, int64_t> &idx
)#

构建一个包含原始数据和新额外向量的新索引。

实现说明:新数据根据现有的 kmeans 聚类进行聚类,然后调整聚类中心以匹配新标记的数据。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, index_params, dataset);
// fill the index with the data
std::optional<raft::device_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
auto index = ivf_flat::extend(handle, new_vectors, no_op, index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::device_matrix_view

  • new_indices – [输入] 可选的 raft::device_vector_view,指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt 以表示连续范围 [0...n_rows)

  • idx – [输入] 原始索引

返回值:

构建的扩展 ivf-flat 索引

void extend(
raft::resources const &handle,
raft::device_matrix_view<const float, int64_t, raft::row_major> new_vectors,
std::optional<raft::device_vector_view<const int64_t, int64_t>> new_indices,
cuvs::neighbors::ivf_flat::index<float, int64_t> *idx
)#

使用新数据就地扩展索引。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, index_params, dataset);
// fill the index with the data
std::optional<raft::device_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
ivf_flat::extend(handle, dataset, no_opt, &index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::device_matrix_view

  • new_indices – [输入] 可选的 raft::device_vector_view,指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt 以表示连续范围 [0...n_rows)

  • idx – [输入/输出] 指向索引的指针,将就地覆盖

cuvs::neighbors::ivf_flat::index<int8_t, int64_t> extend(
raft::resources const &handle,
raft::device_matrix_view<const int8_t, int64_t, raft::row_major> new_vectors,
std::optional<raft::device_vector_view<const int64_t, int64_t>> new_indices,
const cuvs::neighbors::ivf_flat::index<int8_t, int64_t> &idx
)#

构建一个包含原始数据和新额外向量的新索引。

实现说明:新数据根据现有的 kmeans 聚类进行聚类,然后调整聚类中心以匹配新标记的数据。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, dataset, index_params, dataset);
// fill the index with the data
std::optional<raft::device_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
auto index = ivf_flat::extend(handle, new_vectors, no_op, index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::device_matrix_view

  • new_indices – [输入] 可选的 raft::device_vector_view,指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt 以表示连续范围 [0...n_rows)

  • idx – [输入] 原始索引

返回值:

构建的扩展 ivf-flat 索引

void extend(
raft::resources const &handle,
raft::device_matrix_view<const int8_t, int64_t, raft::row_major> new_vectors,
std::optional<raft::device_vector_view<const int64_t, int64_t>> new_indices,
cuvs::neighbors::ivf_flat::index<int8_t, int64_t> *idx
)#

使用新数据就地扩展索引。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, index_params, dataset);
// fill the index with the data
std::optional<raft::device_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
ivf_flat::extend(handle, dataset, no_opt, &index_empty);

如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt 以表示连续范围 [0...n_rows)

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::device_matrix_view

  • new_indices – [输入] 可选的 raft::device_vector_view,指向一个索引向量 [n_rows]。

  • idx – [输入/输出] 指向索引的指针,将就地覆盖

cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> extend(
raft::resources const &handle,
raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> new_vectors,
std::optional<raft::device_vector_view<const int64_t, int64_t>> new_indices,
const cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> &idx
)#

构建一个包含原始数据和新额外向量的新索引。

实现说明:新数据根据现有的 kmeans 聚类进行聚类,然后调整聚类中心以匹配新标记的数据。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, dataset, index_params, dataset);
// fill the index with the data
std::optional<raft::device_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
auto index = ivf_flat::extend(handle, new_vectors, no_op, index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::device_matrix_view

  • new_indices – [输入] 可选的 raft::device_vector_view,指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt 以表示连续范围 [0...n_rows)

  • idx – [输入] 原始索引

返回值:

构建的扩展 ivf-flat 索引

void extend(
raft::resources const &handle,
raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> new_vectors,
std::optional<raft::device_vector_view<const int64_t, int64_t>> new_indices,
cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> *idx
)#

使用新数据就地扩展索引。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, index_params, dataset);
// fill the index with the data
std::optional<raft::device_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
ivf_flat::extend(handle, dataset, no_opt, &index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::device_matrix_view

  • new_indices – [输入] 可选的 raft::device_vector_view,指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt 以表示连续范围 [0...n_rows)

  • idx – [输入/输出] 指向索引的指针,将就地覆盖

cuvs::neighbors::ivf_flat::index<float, int64_t> extend(
raft::resources const &handle,
raft::host_matrix_view<const float, int64_t, raft::row_major> new_vectors,
std::optional<raft::host_vector_view<const int64_t, int64_t>> new_indices,
const cuvs::neighbors::ivf_flat::index<float, int64_t> &idx
)#

构建一个包含原始数据和新额外向量的新索引。

注意,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

实现说明:新数据根据现有的 kmeans 聚类进行聚类,然后调整聚类中心以匹配新标记的数据。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, index_params, dataset);
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// fill the index with the data
std::optional<raft::host_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
auto index = ivf_flat::extend(handle, new_vectors, no_op, index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::host_matrix_view

  • new_indices[in] 可选的 raft::host_vector_view 指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt,表示连续范围 [0...n_rows)

  • idx – [输入] 原始索引

返回值:

构建的扩展 ivf-flat 索引

void extend(
raft::resources const &handle,
raft::host_matrix_view<const float, int64_t, raft::row_major> new_vectors,
std::optional<raft::host_vector_view<const int64_t, int64_t>> new_indices,
cuvs::neighbors::ivf_flat::index<float, int64_t> *idx
)#

使用新数据就地扩展索引。

注意,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, index_params, dataset);
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// fill the index with the data
std::optional<raft::host_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
ivf_flat::extend(handle, dataset, no_opt, &index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::host_matrix_view

  • new_indices[in] 可选的 raft::host_vector_view 指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt,表示连续范围 [0...n_rows)

  • idx – [输入/输出] 指向索引的指针,将就地覆盖

cuvs::neighbors::ivf_flat::index<int8_t, int64_t> extend(
raft::resources const &handle,
raft::host_matrix_view<const int8_t, int64_t, raft::row_major> new_vectors,
std::optional<raft::host_vector_view<const int64_t, int64_t>> new_indices,
const cuvs::neighbors::ivf_flat::index<int8_t, int64_t> &idx
)#

构建一个包含原始数据和新额外向量的新索引。

注意,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

实现说明:新数据根据现有的 kmeans 聚类进行聚类,然后调整聚类中心以匹配新标记的数据。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, dataset, index_params, dataset);
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// fill the index with the data
std::optional<raft::host_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
auto index = ivf_flat::extend(handle, new_vectors, no_op, index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::host_matrix_view

  • new_indices[in] 可选的 raft::host_vector_view 指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt,表示连续范围 [0...n_rows)

  • idx – [输入] 原始索引

返回值:

构建的扩展 ivf-flat 索引

void extend(
raft::resources const &handle,
raft::host_matrix_view<const int8_t, int64_t, raft::row_major> new_vectors,
std::optional<raft::host_vector_view<const int64_t, int64_t>> new_indices,
cuvs::neighbors::ivf_flat::index<int8_t, int64_t> *idx
)#

使用新数据就地扩展索引。

注意,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, index_params, dataset);
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// fill the index with the data
std::optional<raft::host_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
ivf_flat::extend(handle, dataset, no_opt, &index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::host_matrix_view

  • new_indices[in] 可选的 raft::host_vector_view 指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt,表示连续范围 [0...n_rows)

  • idx – [输入/输出] 指向索引的指针,将就地覆盖

cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> extend(
raft::resources const &handle,
raft::host_matrix_view<const uint8_t, int64_t, raft::row_major> new_vectors,
std::optional<raft::host_vector_view<const int64_t, int64_t>> new_indices,
const cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> &idx
)#

构建一个包含原始数据和新额外向量的新索引。

注意,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

实现说明:新数据根据现有的 kmeans 聚类进行聚类,然后调整聚类中心以匹配新标记的数据。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, dataset, index_params, dataset);
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// fill the index with the data
std::optional<raft::host_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
auto index = ivf_flat::extend(handle, new_vectors, no_op, index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::host_matrix_view

  • new_indices[in] 可选的 raft::host_vector_view 指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt,表示连续范围 [0...n_rows)

  • idx – [输入] 原始索引

返回值:

构建的扩展 ivf-flat 索引

void extend(
raft::resources const &handle,
raft::host_matrix_view<const uint8_t, int64_t, raft::row_major> new_vectors,
std::optional<raft::host_vector_view<const int64_t, int64_t>> new_indices,
cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> *idx
)#

使用新数据就地扩展索引。

注意,用户可以在输入的 raft::resource 中设置至少包含一个流的流池,以实现内核和拷贝重叠。

使用示例

using namespace cuvs::neighbors;
ivf_flat::index_params index_params;
index_params.add_data_on_build = false;      // don't populate index on build
index_params.kmeans_trainset_fraction = 1.0; // use whole dataset for kmeans training
// train the index from a [N, D] dataset
auto index_empty = ivf_flat::build(handle, index_params, dataset);
// optional: create a stream pool with at least one stream to enable kernel and copy
// overlapping
raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));
// fill the index with the data
std::optional<raft::host_vector_view<const IdxT, IdxT>> no_op = std::nullopt;
ivf_flat::extend(handle, dataset, no_opt, &index_empty);

参数:
  • handle – [输入]

  • new_vectors – [输入] 指向行主序矩阵 [n_rows, index.dim()] 的 raft::host_matrix_view

  • new_indices[in] 可选的 raft::host_vector_view 指向一个索引向量 [n_rows]。如果原始索引为空 (orig_index.size() == 0),您可以在此处传递 std::nullopt,表示连续范围 [0...n_rows)

  • idx – [输入/输出] 指向索引的指针,将就地覆盖

索引序列化#

void serialize(
raft::resources const &handle,
const std::string &filename,
const cuvs::neighbors::ivf_flat::index<float, int64_t> &index
)#

将索引保存到文件。

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
// create an index with `auto index = ivf_flat::build(...);`
cuvs::neighbors::ivf_flat::serialize(handle, filename, index);
参数:
  • handle[in] raft 句柄

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

  • index[in] IVF-Flat 索引

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

从文件加载索引。

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
using T    = float; // data element type
using IdxT = int64_t; // type of the index
// create an empty index with `ivf_flat::index<T, IdxT> index(handle, index_params, dim);`
cuvs::neighbors::ivf_flat::deserialize(handle, filename, &index);
参数:
  • handle[in] raft 句柄

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

  • index[in] IVF-Flat 索引

void serialize(
raft::resources const &handle,
std::ostream &os,
const cuvs::neighbors::ivf_flat::index<float, int64_t> &index
)#

将索引写入输出流

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

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

  • os[in] 输出流

  • index[in] IVF-Flat 索引

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

从输入流加载索引

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create an input stream
std::istream is(std::cin.rdbuf());
using T    = float; // data element type
using IdxT = int64_t; // type of the index
// create an empty index with `ivf_flat::index<T, IdxT> index(handle, index_params, dim);`
cuvs::neighbors::ivf_flat::deserialize(handle, is, &index);
参数:
  • handle[in] raft 句柄

  • is[in] 输入流

  • index[in] IVF-Flat 索引

void serialize(
raft::resources const &handle,
const std::string &filename,
const cuvs::neighbors::ivf_flat::index<int8_t, int64_t> &index
)#

将索引保存到文件。

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
// create an index with `auto index = ivf_flat::build(...);`
cuvs::neighbors::ivf_flat::serialize(handle, filename, index);
参数:
  • handle[in] raft 句柄

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

  • index[in] IVF-Flat 索引

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

从文件加载索引。

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
using T    = float; // data element type
using IdxT = int64_t; // type of the index
// create an empty index with `ivf_flat::index<T, IdxT> index(handle, index_params, dim);`
cuvs::neighbors::ivf_flat::deserialize(handle, filename, &index);
参数:
  • handle[in] raft 句柄

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

  • index[in] IVF-Flat 索引

void serialize(
raft::resources const &handle,
std::ostream &os,
const cuvs::neighbors::ivf_flat::index<int8_t, int64_t> &index
)#

将索引写入输出流

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

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

  • os[in] 输出流

  • index[in] IVF-Flat 索引

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

从输入流加载索引

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create an input stream
std::istream is(std::cin.rdbuf());
using T    = float; // data element type
using IdxT = int64_t; // type of the index
// create an empty index with `ivf_flat::index<T, IdxT> index(handle, index_params, dim);`
cuvs::neighbors::ivf_flat::deserialize(handle, is, &index);
参数:
  • handle[in] raft 句柄

  • is[in] 输入流

  • index[in] IVF-Flat 索引

void serialize(
raft::resources const &handle,
const std::string &filename,
const cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> &index
)#

将索引保存到文件。

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
// create an index with `auto index = ivf_flat::build(...);`
cuvs::neighbors::ivf_flat::serialize(handle, filename, index);
参数:
  • handle[in] raft 句柄

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

  • index[in] IVF-Flat 索引

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

从文件加载索引。

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create a string with a filepath
std::string filename("/path/to/index");
using T    = float; // data element type
using IdxT = int64_t; // type of the index
// create an empty index with ivf_flat::index<T, IdxT> index(handle, index_params, dim);`
cuvs::neighbors::ivf_flat::deserialize(handle, filename, &index);
参数:
  • handle[in] raft 句柄

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

  • index[in] IVF-Flat 索引

void serialize(
raft::resources const &handle,
std::ostream &os,
const cuvs::neighbors::ivf_flat::index<uint8_t, int64_t> &index
)#

将索引写入输出流

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

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

  • os[in] 输出流

  • index[in] IVF-Flat 索引

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

从输入流加载索引

实验性功能,API 和序列化格式可能会发生变化。

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

raft::resources handle;

// create an input stream
std::istream is(std::cin.rdbuf());
using T    = float; // data element type
using IdxT = int64_t; // type of the index
// create an empty index with `ivf_flat::index<T, IdxT> index(handle, index_params, dim);`
cuvs::neighbors::ivf_flat::deserialize(handle, is, &index);
参数:
  • handle[in] raft 句柄

  • is[in] 输入流

  • index[in] IVF-Flat 索引