CAGRA#
CAGRA 是一种基于图的最近邻算法,专为 GPU 加速而构建。CAGRA 在小批量和大批量搜索中都展示了最先进的索引构建和查询性能。
#include <cuvs/neighbors/cagra.hpp>
namespace cuvs::neighbors::cagra
索引构建参数#
-
struct index_params : public cuvs::neighbors::index_params#
- #include <cagra.hpp>
索引搜索参数#
-
enum class search_algo#
值
-
enumerator SINGLE_CTA#
适用于大批量。
-
enumerator MULTI_CTA#
适用于小批量。
-
enumerator MULTI_KERNEL#
-
enumerator AUTO#
-
enumerator SINGLE_CTA#
-
struct search_params : public cuvs::neighbors::search_params#
- #include <cagra.hpp>
索引扩展参数#
-
struct extend_params#
- #include <cagra.hpp>
索引扩展内存缓冲区#
警告
doxygengroup: Cannot find group “cagra_cpp_extend_memory_buffers” in doxygen xml output for project “cuvs” from directory: ../../cpp/doxygen/_xml/
索引#
-
template<typename T, typename IdxT>
struct index : public cuvs::neighbors::index# - #include <cagra.hpp>
CAGRA 索引。
该索引在设备内存中存储数据集和 kNN 图。
- 模板参数:
T – 数据元素类型
IdxT – 用于存储搜索图中邻居索引的数据类型。它必须足够大,以表示最高达 dataset.extent(0) 的值。
索引构建#
- cuvs::neighbors::cagra::index<float, uint32_t> build(
- raft::resources const &res,
- const cuvs::neighbors::cagra::index_params ¶ms,
- raft::device_matrix_view<const float, int64_t, raft::row_major> dataset
从数据集构建索引以进行高效搜索。
构建过程包括两个步骤:构建中间 knn 图,然后对其进行优化以创建最终图。index_params 结构体控制这些图的节点度。
支持以下距离指标
L2
内积(目前仅在将 IVF-PQ 用作构建算法时支持)
用法示例
using namespace cuvs::neighbors; // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
- 参数:
res – [in] raft 资源
params – [in] 构建索引的参数
dataset – [in] 指向行主序矩阵 [n_rows, dim] 的设备矩阵视图
- 返回值:
构建的 cagra 索引
- cuvs::neighbors::cagra::index<float, uint32_t> build(
- raft::resources const &res,
- const cuvs::neighbors::cagra::index_params ¶ms,
- raft::host_matrix_view<const float, int64_t, raft::row_major> dataset
从数据集构建索引以进行高效搜索。
构建过程包括两个步骤:构建中间 knn 图,然后对其进行优化以创建最终图。index_params 结构体控制这些图的节点度。
支持以下距离指标
L2
内积(目前仅在将 IVF-PQ 用作构建算法时支持)
用法示例
using namespace cuvs::neighbors; // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
- 参数:
res – [in] raft 资源
params – [in] 构建索引的参数
dataset – [in] 指向行主序矩阵 [n_rows, dim] 的主机矩阵视图
- 返回值:
构建的 cagra 索引
- cuvs::neighbors::cagra::index<half, uint32_t> build(
- raft::resources const &res,
- const cuvs::neighbors::cagra::index_params ¶ms,
- raft::device_matrix_view<const half, int64_t, raft::row_major> dataset
从数据集构建索引以进行高效搜索。
构建过程包括两个步骤:构建中间 knn 图,然后对其进行优化以创建最终图。index_params 结构体控制这些图的节点度。
支持以下距离指标
L2
内积(目前仅在将 IVF-PQ 用作构建算法时支持)
用法示例
using namespace cuvs::neighbors; // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
- 参数:
res – [in] raft 资源
params – [in] 构建索引的参数
dataset – [in] 指向行主序矩阵 [n_rows, dim] 的设备矩阵视图
- 返回值:
构建的 cagra 索引
- cuvs::neighbors::cagra::index<half, uint32_t> build(
- raft::resources const &res,
- const cuvs::neighbors::cagra::index_params ¶ms,
- raft::host_matrix_view<const half, int64_t, raft::row_major> dataset
从数据集构建索引以进行高效搜索。
构建过程包括两个步骤:构建中间 knn 图,然后对其进行优化以创建最终图。index_params 结构体控制这些图的节点度。
支持以下距离指标
L2
用法示例
using namespace cuvs::neighbors; // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
- 参数:
res – [in] raft 资源
params – [in] 构建索引的参数
dataset – [in] 指向行主序矩阵 [n_rows, dim] 的主机矩阵视图
- 返回值:
构建的 cagra 索引
- cuvs::neighbors::cagra::index<int8_t, uint32_t> build(
- raft::resources const &res,
- const cuvs::neighbors::cagra::index_params ¶ms,
- raft::device_matrix_view<const int8_t, int64_t, raft::row_major> dataset
从数据集构建索引以进行高效搜索。
构建过程包括两个步骤:构建中间 knn 图,然后对其进行优化以创建最终图。index_params 结构体控制这些图的节点度。
支持以下距离指标
L2
用法示例
using namespace cuvs::neighbors; // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
- 参数:
res – [in] raft 资源
params – [in] 构建索引的参数
dataset – [in] 指向行主序矩阵 [n_rows, dim] 的设备矩阵视图
- 返回值:
构建的 cagra 索引
- cuvs::neighbors::cagra::index<int8_t, uint32_t> build(
- raft::resources const &res,
- const cuvs::neighbors::cagra::index_params ¶ms,
- raft::host_matrix_view<const int8_t, int64_t, raft::row_major> dataset
从数据集构建索引以进行高效搜索。
构建过程包括两个步骤:构建中间 knn 图,然后对其进行优化以创建最终图。index_params 结构体控制这些图的节点度。
支持以下距离指标
L2
内积(目前仅在将 IVF-PQ 用作构建算法时支持)
用法示例
using namespace cuvs::neighbors; // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
- 参数:
res – [in] raft 资源
params – [in] 构建索引的参数
dataset – [in] 指向行主序矩阵 [n_rows, dim] 的主机矩阵视图
- 返回值:
构建的 cagra 索引
- cuvs::neighbors::cagra::index<uint8_t, uint32_t> build(
- raft::resources const &res,
- const cuvs::neighbors::cagra::index_params ¶ms,
- raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> dataset
从数据集构建索引以进行高效搜索。
构建过程包括两个步骤:构建中间 knn 图,然后对其进行优化以创建最终图。index_params 结构体控制这些图的节点度。
支持以下距离指标
L2
内积(目前仅在将 IVF-PQ 用作构建算法时支持)
用法示例
using namespace cuvs::neighbors; // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
- 参数:
res – [in] raft 资源
params – [in] 构建索引的参数
dataset – [in] 指向行主序矩阵 [n_rows, dim] 的设备矩阵视图
- 返回值:
构建的 cagra 索引
- cuvs::neighbors::cagra::index<uint8_t, uint32_t> build(
- raft::resources const &res,
- const cuvs::neighbors::cagra::index_params ¶ms,
- raft::host_matrix_view<const uint8_t, int64_t, raft::row_major> dataset
从数据集构建索引以进行高效搜索。
构建过程包括两个步骤:构建中间 knn 图,然后对其进行优化以创建最终图。index_params 结构体控制这些图的节点度。
支持以下距离指标
L2
内积(目前仅在将 IVF-PQ 用作构建算法时支持)
用法示例
using namespace cuvs::neighbors; // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
- 参数:
res – [in] raft 资源
params – [in] 构建索引的参数
dataset – [in] 指向行主序矩阵 [n_rows, dim] 的主机矩阵视图
- 返回值:
构建的 cagra 索引
索引搜索#
- void search(
- raft::resources const &res,
- cuvs::neighbors::cagra::search_params const ¶ms,
- const cuvs::neighbors::cagra::index<float, uint32_t> &index,
- raft::device_matrix_view<const float, int64_t, raft::row_major> queries,
- raft::device_matrix_view<uint32_t, int64_t, raft::row_major> neighbors,
- raft::device_matrix_view<float, int64_t, raft::row_major> distances,
- const cuvs::neighbors::filtering::base_filter &sample_filter = cuvs::neighbors::filtering::none_sample_filter{}
- void search(
- raft::resources const &res,
- cuvs::neighbors::cagra::search_params const ¶ms,
- const cuvs::neighbors::cagra::index<half, uint32_t> &index,
- raft::device_matrix_view<const half, int64_t, raft::row_major> queries,
- raft::device_matrix_view<uint32_t, int64_t, raft::row_major> neighbors,
- raft::device_matrix_view<float, int64_t, raft::row_major> distances,
- const cuvs::neighbors::filtering::base_filter &sample_filter = cuvs::neighbors::filtering::none_sample_filter{}
使用构建的索引搜索 ANN。
有关用法示例,请参阅 cagra::build 文档。
- 参数:
res – [in] raft 资源
params – [in] 配置搜索
index – [in] cagra 索引
queries – [in] 指向行主序矩阵 [n_queries, index->dim()] 的设备矩阵视图
neighbors – [out] 指向源数据集中邻居索引的设备矩阵视图 [n_queries, k]
distances – [out] 指向选定邻居距离的设备矩阵视图 [n_queries, k]
sample_filter – [in] 一个可选的设备过滤器函数对象,用于允许特定查询的样本通过。(none_sample_filter 表示不进行过滤)
- void search(
- raft::resources const &res,
- cuvs::neighbors::cagra::search_params const ¶ms,
- const cuvs::neighbors::cagra::index<int8_t, uint32_t> &index,
- raft::device_matrix_view<const int8_t, int64_t, raft::row_major> queries,
- raft::device_matrix_view<uint32_t, int64_t, raft::row_major> neighbors,
- raft::device_matrix_view<float, int64_t, raft::row_major> distances,
- const cuvs::neighbors::filtering::base_filter &sample_filter = cuvs::neighbors::filtering::none_sample_filter{}
使用构建的索引搜索 ANN。
有关用法示例,请参阅 cagra::build 文档。
- 参数:
res – [in] raft 资源
params – [in] 配置搜索
index – [in] cagra 索引
queries – [in] 指向行主序矩阵 [n_queries, index->dim()] 的设备矩阵视图
neighbors – [out] 指向源数据集中邻居索引的设备矩阵视图 [n_queries, k]
distances – [out] 指向选定邻居距离的设备矩阵视图 [n_queries, k]
sample_filter – [in] 一个可选的设备过滤器函数对象,用于允许特定查询的样本通过。(none_sample_filter 表示不进行过滤)
- void search(
- raft::resources const &res,
- cuvs::neighbors::cagra::search_params const ¶ms,
- const cuvs::neighbors::cagra::index<uint8_t, uint32_t, int64_t> &index,
- raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> queries,
- raft::device_matrix_view<uint32_t, int64_t, raft::row_major> neighbors,
- raft::device_matrix_view<float, int64_t, raft::row_major> distances,
- const cuvs::neighbors::filtering::base_filter &sample_filter = cuvs::neighbors::filtering::none_sample_filter{}
使用构建的索引搜索 ANN。
有关用法示例,请参阅 cagra::build 文档。
- 参数:
res – [in] raft 资源
params – [in] 配置搜索
index – [in] cagra 索引
queries – [in] 指向行主序矩阵 [n_queries, index->dim()] 的设备矩阵视图
neighbors – [out] 指向源数据集中邻居索引的设备矩阵视图 [n_queries, k]
distances – [out] 指向选定邻居距离的设备矩阵视图 [n_queries, k]
sample_filter – [in] 一个可选的设备过滤器函数对象,用于允许特定查询的样本通过。(none_sample_filter 表示不进行过滤)
- void search(
- raft::resources const &res,
- cuvs::neighbors::cagra::search_params const ¶ms,
- const cuvs::neighbors::cagra::index<float, uint32_t> &index,
- raft::device_matrix_view<const float, int64_t, raft::row_major> queries,
- raft::device_matrix_view<int64_t, int64_t, raft::row_major> neighbors,
- raft::device_matrix_view<float, int64_t, raft::row_major> distances,
- const cuvs::neighbors::filtering::base_filter &sample_filter = cuvs::neighbors::filtering::none_sample_filter{}
使用构建的索引搜索 ANN。
有关用法示例,请参阅 cagra::build 文档。
- 参数:
res – [in] raft 资源
params – [in] 配置搜索
index – [in] cagra 索引
queries – [in] 指向行主序矩阵 [n_queries, index->dim()] 的设备矩阵视图
neighbors – [out] 指向源数据集中邻居索引的设备矩阵视图 [n_queries, k]
distances – [out] 指向选定邻居距离的设备矩阵视图 [n_queries, k]
sample_filter – [in] 一个可选的设备过滤器函数对象,用于允许特定查询的样本通过。(none_sample_filter 表示不进行过滤)
- void search(
- raft::resources const &res,
- cuvs::neighbors::cagra::search_params const ¶ms,
- const cuvs::neighbors::cagra::index<half, uint32_t> &index,
- raft::device_matrix_view<const half, int64_t, raft::row_major> queries,
- raft::device_matrix_view<int64_t, int64_t, raft::row_major> neighbors,
- raft::device_matrix_view<float, int64_t, raft::row_major> distances,
- const cuvs::neighbors::filtering::base_filter &sample_filter = cuvs::neighbors::filtering::none_sample_filter{}
使用构建的索引搜索 ANN。
有关用法示例,请参阅 cagra::build 文档。
- 参数:
res – [in] raft 资源
params – [in] 配置搜索
index – [in] cagra 索引
queries – [in] 指向行主序矩阵 [n_queries, index->dim()] 的设备矩阵视图
neighbors – [out] 指向源数据集中邻居索引的设备矩阵视图 [n_queries, k]
distances – [out] 指向选定邻居距离的设备矩阵视图 [n_queries, k]
sample_filter – [in] 一个可选的设备过滤器函数对象,用于允许特定查询的样本通过。(none_sample_filter 表示不进行过滤)
- void search(
- raft::resources const &res,
- cuvs::neighbors::cagra::search_params const ¶ms,
- const cuvs::neighbors::cagra::index<int8_t, uint32_t> &index,
- raft::device_matrix_view<const int8_t, int64_t, raft::row_major> queries,
- raft::device_matrix_view<int64_t, int64_t, raft::row_major> neighbors,
- raft::device_matrix_view<float, int64_t, raft::row_major> distances,
- const cuvs::neighbors::filtering::base_filter &sample_filter = cuvs::neighbors::filtering::none_sample_filter{}
使用构建的索引搜索 ANN。
有关用法示例,请参阅 cagra::build 文档。
- 参数:
res – [in] raft 资源
params – [in] 配置搜索
index – [in] cagra 索引
queries – [in] 指向行主序矩阵 [n_queries, index->dim()] 的设备矩阵视图
neighbors – [out] 指向源数据集中邻居索引的设备矩阵视图 [n_queries, k]
distances – [out] 指向选定邻居距离的设备矩阵视图 [n_queries, k]
sample_filter – [in] 一个可选的设备过滤器函数对象,用于允许特定查询的样本通过。(none_sample_filter 表示不进行过滤)
- void search(
- raft::resources const &res,
- cuvs::neighbors::cagra::search_params const ¶ms,
- const cuvs::neighbors::cagra::index<uint8_t, uint32_t, int64_t> &index,
- raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> queries,
- raft::device_matrix_view<int64_t, int64_t, raft::row_major> neighbors,
- raft::device_matrix_view<float, int64_t, raft::row_major> distances,
- const cuvs::neighbors::filtering::base_filter &sample_filter = cuvs::neighbors::filtering::none_sample_filter{}
使用构建的索引搜索 ANN。
有关用法示例,请参阅 cagra::build 文档。
- 参数:
res – [in] raft 资源
params – [in] 配置搜索
index – [in] cagra 索引
queries – [in] 指向行主序矩阵 [n_queries, index->dim()] 的设备矩阵视图
neighbors – [out] 指向源数据集中邻居索引的设备矩阵视图 [n_queries, k]
distances – [out] 指向选定邻居距离的设备矩阵视图 [n_queries, k]
sample_filter – [in] 一个可选的设备过滤器函数对象,用于允许特定查询的样本通过。(none_sample_filter 表示不进行过滤)
索引扩展#
- void extend(
- raft::resources const &handle,
- const cagra::extend_params ¶ms,
- raft::device_matrix_view<const float, int64_t, raft::row_major> additional_dataset,
- cuvs::neighbors::cagra::index<float, uint32_t> &idx,
- std::optional<raft::device_matrix_view<float, int64_t, raft::layout_stride>> new_dataset_buffer_view = std::nullopt,
- std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt
向 CAGRA 索引添加新向量。
用法示例
using namespace raft::neighbors; auto additional_dataset = raft::make_device_matrix<float, int64_t>(handle,add_size,dim); // set_additional_dataset(additional_dataset.view()); cagra::extend_params params; cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
- 参数:
handle – [in] raft 资源
params – [in] 扩展参数
additional_dataset – [in] 设备内存中的额外数据集
idx – [inout] CAGRA 索引
new_dataset_buffer_view – [out] 包含额外部分的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是数据集的维度,步长必须与原始索引数据集相同。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理数据集的内存空间时,此选项很有用。
new_graph_buffer_view – [out] 包含额外部分的图的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是图的度。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理图的内存空间时,此选项很有用。
- void extend(
- raft::resources const &handle,
- const cagra::extend_params ¶ms,
- raft::host_matrix_view<const float, int64_t, raft::row_major> additional_dataset,
- cuvs::neighbors::cagra::index<float, uint32_t> &idx,
- std::optional<raft::device_matrix_view<float, int64_t, raft::layout_stride>> new_dataset_buffer_view = std::nullopt,
- std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt
向 CAGRA 索引添加新向量。
用法示例
using namespace raft::neighbors; auto additional_dataset = raft::make_host_matrix<float, int64_t>(handle,add_size,dim); // set_additional_dataset(additional_dataset.view()); cagra::extend_params params; cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
- 参数:
handle – [in] raft 资源
params – [in] 扩展参数
additional_dataset – [in] 主机内存中的额外数据集
idx – [inout] CAGRA 索引
new_dataset_buffer_view – [out] 包含额外部分的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是数据集的维度,步长必须与原始索引数据集相同。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理数据集的内存空间时,此选项很有用。
new_graph_buffer_view – [out] 包含额外部分的图的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是图的度。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理图的内存空间时,此选项很有用。
- void extend(
- raft::resources const &handle,
- const cagra::extend_params ¶ms,
- raft::device_matrix_view<const int8_t, int64_t, raft::row_major> additional_dataset,
- cuvs::neighbors::cagra::index<int8_t, uint32_t> &idx,
- std::optional<raft::device_matrix_view<int8_t, int64_t, raft::layout_stride>> new_dataset_buffer_view = std::nullopt,
- std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt
向 CAGRA 索引添加新向量。
用法示例
using namespace raft::neighbors; auto additional_dataset = raft::make_device_matrix<int8_t, int64_t>(handle,add_size,dim); // set_additional_dataset(additional_dataset.view()); cagra::extend_params params; cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
- 参数:
handle – [in] raft 资源
params – [in] 扩展参数
additional_dataset – [in] 设备内存中的额外数据集
idx – [inout] CAGRA 索引
new_dataset_buffer_view – [out] 包含额外部分的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是数据集的维度,步长必须与原始索引数据集相同。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理数据集的内存空间时,此选项很有用。
new_graph_buffer_view – [out] 包含额外部分的图的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是图的度。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理图的内存空间时,此选项很有用。
- void extend(
- raft::resources const &handle,
- const cagra::extend_params ¶ms,
- raft::host_matrix_view<const int8_t, int64_t, raft::row_major> additional_dataset,
- cuvs::neighbors::cagra::index<int8_t, uint32_t> &idx,
- std::optional<raft::device_matrix_view<int8_t, int64_t, raft::layout_stride>> new_dataset_buffer_view = std::nullopt,
- std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt
向 CAGRA 索引添加新向量。
用法示例
using namespace raft::neighbors; auto additional_dataset = raft::make_host_matrix<int8_t, int64_t>(handle,add_size,dim); // set_additional_dataset(additional_dataset.view()); cagra::extend_params params; cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
- 参数:
handle – [in] raft 资源
params – [in] 扩展参数
additional_dataset – [in] 主机内存中的额外数据集
idx – [inout] CAGRA 索引
new_dataset_buffer_view – [out] 包含额外部分的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是数据集的维度,步长必须与原始索引数据集相同。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理数据集的内存空间时,此选项很有用。
new_graph_buffer_view – [out] 包含额外部分的图的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是图的度。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理图的内存空间时,此选项很有用。
- void extend(
- raft::resources const &handle,
- const cagra::extend_params ¶ms,
- raft::device_matrix_view<const uint8_t, int64_t, raft::row_major> additional_dataset,
- cuvs::neighbors::cagra::index<uint8_t, uint32_t> &idx,
- std::optional<raft::device_matrix_view<uint8_t, int64_t, raft::layout_stride>> new_dataset_buffer_view = std::nullopt,
- std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt
向 CAGRA 索引添加新向量。
用法示例
using namespace raft::neighbors; auto additional_dataset = raft::make_host_matrix<uint8_t, int64_t>(handle,add_size,dim); // set_additional_dataset(additional_dataset.view()); cagra::extend_params params; cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
- 参数:
handle – [in] raft 资源
params – [in] 扩展参数
additional_dataset – [in] 主机内存中的额外数据集
idx – [inout] CAGRA 索引
new_dataset_buffer_view – [out] 包含额外部分的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是数据集的维度,步长必须与原始索引数据集相同。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理数据集的内存空间时,此选项很有用。
new_graph_buffer_view – [out] 包含额外部分的图的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是图的度。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理图的内存空间时,此选项很有用。
- void extend(
- raft::resources const &handle,
- const cagra::extend_params ¶ms,
- raft::host_matrix_view<const uint8_t, int64_t, raft::row_major> additional_dataset,
- cuvs::neighbors::cagra::index<uint8_t, uint32_t> &idx,
- std::optional<raft::device_matrix_view<uint8_t, int64_t, raft::layout_stride>> new_dataset_buffer_view = std::nullopt,
- std::optional<raft::device_matrix_view<uint32_t, int64_t>> new_graph_buffer_view = std::nullopt
向 CAGRA 索引添加新向量。
用法示例
using namespace raft::neighbors; auto additional_dataset = raft::make_host_matrix<uint8_t, int64_t>(handle,add_size,dim); // set_additional_dataset(additional_dataset.view()); cagra::extend_params params; cagra::extend(res, params, raft::make_const_mdspan(additional_dataset.view()), index);
- 参数:
handle – [in] raft 资源
params – [in] 扩展参数
additional_dataset – [in] 主机内存中的额外数据集
idx – [inout] CAGRA 索引
new_dataset_buffer_view – [out] 包含额外部分的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是数据集的维度,步长必须与原始索引数据集相同。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理数据集的内存空间时,此选项很有用。
new_graph_buffer_view – [out] 包含额外部分的图的内存缓冲区视图。数据将在此函数中从当前索引复制。行数必须是原始数据集和额外数据集的总和,列数必须是图的度。此视图将存储在输出索引中。调用者有责任确保数据集与索引一样长时间保持活动状态。当用户希望自行管理图的内存空间时,此选项很有用。
索引序列化#
-
using nn_descent_params = cuvs::neighbors::nn_descent::index_params#
-
using iterative_search_params = cuvs::neighbors::search_params#
-
using index_params_type = cagra::index_params#
-
using search_params_type = cagra::search_params#
-
using index_type = IdxT#
-
using value_type = T#
-
using dataset_index_type = int64_t#
-
cuvs::neighbors::ivf_pq::index_params build_params#
-
cuvs::neighbors::ivf_pq::search_params search_params#
-
float refinement_rate#
-
size_t intermediate_graph_degree = 128#
用于剪枝的输入图的度。
-
size_t graph_degree = 64#
输出图的度。
-
std::optional<cuvs::neighbors::vpq_params> compression = std::nullopt#
如果需要压缩,请指定压缩参数。如果设置,将覆盖 attach_dataset_on_build(并且压缩后的数据集始终会添加到索引中)。
-
std::variant<std::monostate, graph_build_params::ivf_pq_params, graph_build_params::nn_descent_params, graph_build_params::iterative_search_params> graph_build_params#
图构建参数。
设置 ivf_pq_params、nn_descent_params 或 iterative_search_params 来选择图构建算法并控制其参数。默认值 (std::monostate) 是使用启发式方法决定算法及其参数。
cagra::index_params params; // 1. Choose IVF-PQ algorithm params.graph_build_params = cagra::graph_build_params::ivf_pq_params(dataset.extent, params.metric); // 2. Choose NN Descent algorithm for kNN graph construction params.graph_build_params = cagra::graph_build_params::nn_descent_params(params.intermediate_graph_degree); // 3. Choose iterative graph building using CAGRA's search() and optimize() [Experimental] params.graph_build_params = cagra::graph_build_params::iterative_search_params();
-
bool guarantee_connectivity = false#
是否使用 MST 优化来保证图的连通性。
-
bool attach_dataset_on_build = true#
是否将数据集内容添加到索引,即
true
表示索引填充了数据集向量,并且在调用build
后,只要有足够的可用内存,就可以进行搜索。false
表示build
只构建图,用户需要使用 cuvs::neighbors::cagra::update_dataset 更新数据集。
无论
attach_dataset_on_build
的值如何,搜索图都是使用数据集中的所有向量创建的。将attach_dataset_on_build = false
设置为 false,在用户只需要构建搜索图但不打算使用 CAGRA 进行搜索(例如,使用其他图搜索算法进行搜索),或者在将数据集使用update_dataset
API 附加到索引之前需要应用特定的内存放置选项时,这可能很有用。auto dataset = raft::make_device_matrix<float, int64_t>(res, n_rows, n_cols); // use default index_parameters cagra::index_params index_params; // update index_params to only build the CAGRA graph index_params.attach_dataset_on_build = false; auto index = cagra::build(res, index_params, dataset.view()); // assert that the dataset is not attached to the index ASSERT(index.dataset().extent(0) == 0); // update dataset index.update_dataset(res, dataset.view()); // The index is now ready for search cagra::search(res, search_params, index, queries, neighbors, distances);
-
size_t max_queries = 0#
同时搜索的最大查询数(批量大小)。为 0 时自动选择。
-
size_t itopk_size = 64#
搜索过程中保留的中间搜索结果数量。
这是调整准确性和搜索速度之间权衡的主要旋钮。值越高,搜索准确性越好。
-
size_t max_iterations = 0#
搜索迭代的上限。为 0 时自动选择。
-
search_algo algo = search_algo::AUTO#
要使用的搜索实现。
-
size_t team_size = 0#
用于计算单个距离的线程数。4、8、16 或 32。
-
size_t search_width = 1#
在每次迭代中选择作为搜索起点的图节点数量。也称为搜索宽度?
-
size_t min_iterations = 0#
搜索迭代的下限。
-
size_t thread_block_size = 0#
线程块大小。0、64、128、256、512、1024。为 0 时自动选择。
-
size_t hashmap_min_bitlen = 0#
哈希映射位长度的下限。大于 8。
-
float hashmap_max_fill_rate = 0.5#
哈希映射填充率的上限。大于 0.1,小于 0.9。
-
uint32_t num_random_samplings = 1#
初始随机种子节点选择的迭代次数。1 次或更多。
-
uint64_t rand_xor_mask = 0x128394#
用于初始随机种子节点选择的位掩码。
-
bool persistent = false#
是否使用持久化版本的内核(目前仅支持 SINGLE_CTA)。
-
float persistent_lifetime = 2#
持久化内核:如果在指定时间内没有收到请求,内核将停止的时间(秒)。
-
float persistent_device_usage = 1.0#
设置持久化内核使用的最大网格大小比例。值 1.0 表示内核网格大小是所选设备的最大可能值。该值必须大于 0.0 且不大于 1.0。
可能需要在该持久化内核旁边运行其他内核。此参数可用于减小持久化内核的网格大小,以留出一些 SM 空闲。注意:在 GPU 上与持久化内核同时运行任何其他工作会使设置变得脆弱。
在另一个线程中运行另一个内核通常可行,但不保证进度。
任何 CUDA 分配都会阻塞上下文(使用内存池可能会隐藏此问题)。
复制到非锁定主机内存的内存操作可能会阻塞上下文。
即使我们知道没有其他内核同时运行,将 kDeviceUsage 设置为 1.0 有时也会意外地损害性能。请谨慎操作。如果您怀疑这是个问题,可以将此值降低到约 0.9,而不会对吞吐量产生显著影响。
-
float filtering_rate = -1.0#
使用过滤时,指示要过滤掉的节点比例的参数。该值必须大于等于 0.0 且小于 1.0。默认值为负数,在这种情况下,过滤率会自动计算。
-
uint32_t max_chunk_size = 0#
额外数据集被分成块并添加到图中。这是调整召回率和操作吞吐量之间权衡的旋钮。大块大小可以带来高吞吐量,但使用更多工作内存 (O(max_chunk_size*degree^2))。这还可能降低召回率,因为同一块中的节点之间不会添加边。
-
cagra::index_params output_index_params#
用于创建输出索引的参数。
-
MergeStrategy strategy = MergeStrategy::PHYSICAL#
合并策略。默认为
MergeStrategy::PHYSICAL
。
-
ivf_pq_params() = default#
- ivf_pq_params(
- raft::matrix_extent<int64_t> dataset_extents,
- cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Expanded
根据输入数据集的形状设置默认参数。使用示例
using namespace cuvs::neighbors; raft::resources res; // create index_params for a [N. D] dataset auto dataset = raft::make_device_matrix<float, int64_t>(res, N, D); auto pq_params = cagra::graph_build_params::ivf_pq_params(dataset.extents()); // modify/update index_params as needed pq_params.kmeans_trainset_fraction = 0.1;
-
merge_params() = default#
-
inline explicit merge_params(const cagra::index_params ¶ms)#
使用给定的索引参数构造合并参数。
- 参数:
params – 用于创建输出索引的参数。
-
inline cuvs::distance::DistanceType metric() const noexcept#
用于聚类的距离度量。
-
inline IdxT size() const noexcept#
索引的总长度(向量数量)。
-
inline uint32_t dim() const noexcept#
数据维度。
-
inline uint32_t graph_degree() const noexcept
图度
- inline raft::device_matrix_view<const T, int64_t, raft::layout_stride> dataset(
- inline const cuvs::neighbors::dataset<int64_t> &data(
数据集 [大小, 维度]
- inline raft::device_matrix_view<const IdxT, int64_t, raft::row_major> graph(
近邻图 [大小, 图度]
- index &=delete operator= (const index &)
- index &=default operator= (index &&)
-
~index() = default#
- inline index(
- raft::resources const &res,
- cuvs::distance::DistanceType metric = cuvs::distance::DistanceType::L2Expanded
构造一个空索引。
-
template<typename data_accessor, typename graph_accessor>
inline index( - raft::resources const &res,
- cuvs::distance::DistanceType metric,
- raft::mdspan<const T, raft::matrix_extent<int64_t>, raft::row_major, data_accessor> dataset,
- raft::mdspan<const IdxT, raft::matrix_extent<int64_t>, raft::row_major, graph_accessor> knn_graph
从数据集和 knn_graph 数组构造索引。
如果数据集和图已在 GPU 内存中,则索引只是一个围绕它们的精简包装器,存储对这些数组的非拥有引用。
构造函数也接受主机数组。在这种情况下,它们会被复制到设备上,并且设备数组将由索引拥有。
如果数据集行未按 16 字节对齐,则我们在设备内存中创建一个填充副本,以确保向量化加载的对齐。
使用示例
CAGRA 索引通常由 cagra::build 创建。
在上面的示例中,我们传递了一个主机数据集来构建。返回的索引将拥有数据集和 knn_graph 的设备副本。相反,如果我们将数据集作为 device_mdspan 传递给 build,那么它将只存储对它的引用。using namespace raft::neighbors::experimental; auto dataset = raft::make_host_matrix<float, int64_t>(n_rows, n_cols); load_dataset(dataset.view()); // use default index parameters cagra::index_params index_params; // create and fill the index from a [N, D] dataset auto index = cagra::build(res, index_params, dataset); // use default search parameters cagra::search_params search_params; // search K nearest neighbours auto neighbors = raft::make_device_matrix<uint32_t, int64_t>(res, n_queries, k); auto distances = raft::make_device_matrix<float, int64_t>(res, n_queries, k); cagra::search(res, search_params, index, queries, neighbors.view(), distances.view());
使用现有 knn-graph 构造索引
using namespace raft::neighbors::experimental; auto dataset = raft::make_device_matrix<float, int64_t>(res, n_rows, n_cols); auto knn_graph = raft::make_device_matrix<uint32_n, int64_t>(res, n_rows, graph_degree); // custom loading and graph creation // load_dataset(dataset.view()); // create_knn_graph(knn_graph.view()); // Wrap the existing device arrays into an index structure cagra::index<T, IdxT> index(res, metric, raft::make_const_mdspan(dataset.view()), raft::make_const_mdspan(knn_graph.view())); // Both knn_graph and dataset objects have to be in scope while the index is used because // the index only stores a reference to these. cagra::search(res, search_params, index, queries, neighbors, distances);
- inline void update_dataset(
- raft::resources const &res,
- raft::device_matrix_view<const T, int64_t, raft::row_major> dataset
用新数据集替换数据集。
如果新数据集行按 16 字节对齐,则仅存储对数据集的引用。调用者负责确保数据集与索引一样长寿。期望 update_dataset 和索引构建使用相同的向量集。
- inline void update_dataset(
- raft::resources const &res,
- raft::device_matrix_view<const T, int64_t, raft::layout_stride> dataset
显式将数据集引用设置为带填充的设备矩阵视图。
- inline void update_dataset(
- raft::resources const &res,
- raft::host_matrix_view<const T, int64_t, raft::row_major> dataset
用新数据集替换数据集。
我们在设备上创建数据集的副本。索引管理此副本的生命周期。期望 update_dataset 和索引构建使用相同的向量集。
-
template<typename DatasetT>
inline std::enable_if_t<std::is_base_of_v<cuvs::neighbors::dataset<dataset_index_type>, DatasetT>> update_dataset( - raft::resources const &res,
- DatasetT &&dataset
用新数据集替换数据集。期望 update_dataset 和索引构建使用相同的向量集。
-
template<typename DatasetT>
inline std::enable_if_t<std::is_base_of_v<neighbors::dataset<dataset_index_type>, DatasetT>> update_dataset( - raft::resources const &res,
- std::unique_ptr<DatasetT> &&dataset
- inline void update_graph(
- raft::resources const &res,
- raft::device_matrix_view<const IdxT, int64_t, raft::row_major> knn_graph
用新图替换图。
由于新图是设备数组,我们存储对其的引用,并且调用者负责确保 knn_graph 与索引一样长寿。
- inline void update_graph(
- raft::resources const &res,
- raft::host_matrix_view<const IdxT, int64_t, raft::row_major> knn_graph
用新图替换图。
我们在设备上创建图的副本。索引管理此副本的生命周期。
- void serialize(
- raft::resources const &handle,
- const std::string &filename,
- const cuvs::neighbors::cagra::index<float, uint32_t> &index,
- bool include_dataset = true
将索引保存到文件。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); // create an index with `auto index = cuvs::neighbors::cagra::build(...);` cuvs::neighbors::cagra::serialize(handle, filename, index);
- 参数:
handle – [in] raft 句柄
filename – [in] 保存索引的文件名
index – [in] CAGRA 索引
include_dataset – [in] 是否将数据集写入文件。
- void deserialize(
- raft::resources const &handle,
- const std::string &filename,
- cuvs::neighbors::cagra::index<float, uint32_t> *index
从文件加载索引。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); cuvs::neighbors::cagra::index<float, uint32_t> index; cuvs::neighbors::cagra::deserialize(handle, filename, &index);
- 参数:
handle – [in] raft 句柄
filename – [in] 存储索引的文件名
index – [out] CAGRA 索引
- void serialize(
- raft::resources const &handle,
- std::ostream &os,
- const cuvs::neighbors::cagra::index<float, uint32_t> &index,
- bool include_dataset = true
将索引写入输出流
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create an output stream std::ostream os(std::cout.rdbuf()); // create an index with `auto index = cuvs::neighbors::cagra::build(...);` cuvs::neighbors::cagra::serialize(handle, os, index);
- 参数:
handle – [in] raft 句柄
os – [in] 输出流
index – [in] CAGRA 索引
include_dataset – [in] 是否将数据集写入文件。
- void deserialize(
- raft::resources const &handle,
- std::istream &is,
- cuvs::neighbors::cagra::index<float, uint32_t> *index
从输入流加载索引
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create an input stream std::istream is(std::cin.rdbuf()); cuvs::neighbors::cagra::index<float, uint32_t> index; cuvs::neighbors::cagra::deserialize(handle, is, &index);
- 参数:
handle – [in] raft 句柄
is – [in] 输入流
index – [out] CAGRA 索引
- void serialize(
- raft::resources const &handle,
- const std::string &filename,
- const cuvs::neighbors::cagra::index<half, uint32_t> &index,
- bool include_dataset = true
将索引保存到文件。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); // create an index with `auto index = cuvs::neighbors::cagra::build(...);` cuvs::neighbors::cagra::serialize(handle, filename, index);
- 参数:
handle – [in] raft 句柄
filename – [in] 保存索引的文件名
index – [in] CAGRA 索引
include_dataset – [in] 是否将数据集写入文件。
- void deserialize(
- raft::resources const &handle,
- const std::string &filename,
- cuvs::neighbors::cagra::index<half, uint32_t> *index
从文件加载索引。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); cuvs::neighbors::cagra::index<half, uint32_t> index; cuvs::neighbors::cagra::deserialize(handle, filename, &index);
- 参数:
handle – [in] raft 句柄
filename – [in] 存储索引的文件名
index – [out] CAGRA 索引
- void serialize(
- raft::resources const &handle,
- std::ostream &os,
- const cuvs::neighbors::cagra::index<half, uint32_t> &index,
- bool include_dataset = true
将索引写入输出流
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create an output stream std::ostream os(std::cout.rdbuf()); // create an index with `auto index = cuvs::neighbors::cagra::build(...);` cuvs::neighbors::cagra::serialize(handle, os, index);
- 参数:
handle – [in] raft 句柄
os – [in] 输出流
index – [in] CAGRA 索引
include_dataset – [in] 是否将数据集写入文件。
- void deserialize(
- raft::resources const &handle,
- std::istream &is,
- cuvs::neighbors::cagra::index<half, uint32_t> *index
从输入流加载索引
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create an input stream std::istream is(std::cin.rdbuf()); cuvs::neighbors::cagra::index<half, uint32_t> index; cuvs::neighbors::cagra::deserialize(handle, is, &index);
- 参数:
handle – [in] raft 句柄
is – [in] 输入流
index – [out] CAGRA 索引
- void serialize(
- raft::resources const &handle,
- const std::string &filename,
- const cuvs::neighbors::cagra::index<int8_t, uint32_t> &index,
- bool include_dataset = true
将索引保存到文件。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); // create an index with `auto index = cuvs::neighbors::cagra::build(...);` cuvs::neighbors::cagra::serialize(handle, filename, index);
- 参数:
handle – [in] raft 句柄
filename – [in] 保存索引的文件名
index – [in] CAGRA 索引
include_dataset – [in] 是否将数据集写入文件。
- void deserialize(
- raft::resources const &handle,
- const std::string &filename,
- cuvs::neighbors::cagra::index<int8_t, uint32_t> *index
从文件加载索引。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); cuvs::neighbors::cagra::index<int8_t, uint32_t> index; cuvs::neighbors::cagra::deserialize(handle, filename, &index);
- 参数:
handle – [in] raft 句柄
filename – [in] 存储索引的文件名
index – [out] CAGRA 索引
- void serialize(
- raft::resources const &handle,
- std::ostream &os,
- const cuvs::neighbors::cagra::index<int8_t, uint32_t> &index,
- bool include_dataset = true
将索引写入输出流
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create an output stream std::ostream os(std::cout.rdbuf()); // create an index with `auto index = cuvs::neighbors::cagra::build(...);` cuvs::neighbors::cagra::serialize(handle, os, index);
- 参数:
handle – [in] raft 句柄
os – [in] 输出流
index – [in] CAGRA 索引
include_dataset – [in] 是否将数据集写入文件。
- void deserialize(
- raft::resources const &handle,
- std::istream &is,
- cuvs::neighbors::cagra::index<int8_t, uint32_t> *index
从输入流加载索引
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create an input stream std::istream is(std::cin.rdbuf()); cuvs::neighbors::cagra::index<int8_t, uint32_t> index; cuvs::neighbors::cagra::deserialize(handle, is, &index);
- 参数:
handle – [in] raft 句柄
is – [in] 输入流
index – [out] CAGRA 索引
- void serialize(
- raft::resources const &handle,
- const std::string &filename,
- const cuvs::neighbors::cagra::index<uint8_t, uint32_t, int64_t> &index,
- bool include_dataset = true
将索引保存到文件。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); // create an index with `auto index = cuvs::neighbors::cagra::build(...);` cuvs::neighbors::cagra::serialize(handle, filename, index);
- 参数:
handle – [in] raft 句柄
filename – [in] 保存索引的文件名
index – [in] CAGRA 索引
include_dataset – [in] 是否将数据集写入文件。
- void deserialize(
- raft::resources const &handle,
- const std::string &filename,
- cuvs::neighbors::cagra::index<uint8_t, uint32_t> *index
从文件加载索引。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); cuvs::neighbors::cagra::index<uint8_t, uint32_t> index; cuvs::neighbors::cagra::deserialize(handle, filename, &index);
- 参数:
handle – [in] raft 句柄
filename – [in] 存储索引的文件名
index – [out] CAGRA 索引
- void serialize(
- raft::resources const &handle,
- std::ostream &os,
- const cuvs::neighbors::cagra::index<uint8_t, uint32_t, int64_t> &index,
- bool include_dataset = true
将索引写入输出流
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create an output stream std::ostream os(std::cout.rdbuf()); // create an index with `auto index = cuvs::neighbors::cagra::build(...);` cuvs::neighbors::cagra::serialize(handle, os, index);
- 参数:
handle – [in] raft 句柄
os – [in] 输出流
index – [in] CAGRA 索引
include_dataset – [in] 是否将数据集写入文件。
- void deserialize(
- raft::resources const &handle,
- std::istream &is,
- cuvs::neighbors::cagra::index<uint8_t, uint32_t> *index
从输入流加载索引
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra.hpp> raft::resources handle; // create an input stream std::istream is(std::cin.rdbuf()); cuvs::neighbors::cagra::index<uint8_t, uint32_t> index; cuvs::neighbors::cagra::deserialize(handle, is, &index);
- 参数:
handle – [in] raft 句柄
is – [in] 输入流
index – [out] CAGRA 索引
- void serialize_to_hnswlib(
- raft::resources const &handle,
- std::ostream &os,
- const cuvs::neighbors::cagra::index<float, uint32_t> &index,
- std::optional<raft::host_matrix_view<const float, int64_t, raft::row_major>> dataset = std::nullopt
将构建的 CAGRA 索引作为基础层 HNSW 索引写入输出流。注意:保存的索引只能由 cuVS 中的 hnswlib 包装器读取,因为序列化格式与原始 hnswlib 不兼容。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra_serialize.hpp> raft::resources handle; // create an output stream std::ostream os(std::cout.rdbuf()); // create an index with `auto index = raft::cagra::build(...);` cuvs::neighbors::cagra::serialize_to_hnswlib(handle, os, index);
- 参数:
handle – [in] raft 句柄
os – [in] 输出流
index – [in] CAGRA 索引
dataset – [in] [可选] 存储数据集的主机数组,如果索引不包含数据集则必需。
- void serialize_to_hnswlib(
- raft::resources const &handle,
- const std::string &filename,
- const cuvs::neighbors::cagra::index<float, uint32_t> &index,
- std::optional<raft::host_matrix_view<const float, int64_t, raft::row_major>> dataset = std::nullopt
以 hnswlib 仅包含基础层的序列化格式保存构建的 CAGRA 索引。注意:保存的索引只能由 cuVS 中的 hnswlib 包装器读取,因为序列化格式与原始 hnswlib 不兼容。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra_serialize.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); // create an index with `auto index = raft::cagra::build(...);` cuvs::neighbors::cagra::serialize_to_hnswlib(handle, filename, index);
- 参数:
handle – [in] raft 句柄
filename – [in] 保存索引的文件名
index – [in] CAGRA 索引
dataset – [in] [可选] 存储数据集的主机数组,如果索引不包含数据集则必需。
- void serialize_to_hnswlib(
- raft::resources const &handle,
- std::ostream &os,
- const cuvs::neighbors::cagra::index<int8_t, uint32_t> &index,
- std::optional<raft::host_matrix_view<const int8_t, int64_t, raft::row_major>> dataset = std::nullopt
将构建的 CAGRA 索引作为基础层 HNSW 索引写入输出流。注意:保存的索引只能由 cuVS 中的 hnswlib 包装器读取,因为序列化格式与原始 hnswlib 不兼容。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra_serialize.hpp> raft::resources handle; // create an output stream std::ostream os(std::cout.rdbuf()); // create an index with `auto index = raft::cagra::build(...);` cuvs::neighbors::cagra::serialize_to_hnswlib(handle, os, index);
- 参数:
handle – [in] raft 句柄
os – [in] 输出流
index – [in] CAGRA 索引
dataset – [in] [可选] 存储数据集的主机数组,如果索引不包含数据集则必需。
- void serialize_to_hnswlib(
- raft::resources const &handle,
- const std::string &filename,
- const cuvs::neighbors::cagra::index<int8_t, uint32_t> &index,
- std::optional<raft::host_matrix_view<const int8_t, int64_t, raft::row_major>> dataset = std::nullopt
以 hnswlib 仅包含基础层的序列化格式保存构建的 CAGRA 索引。注意:保存的索引只能由 cuVS 中的 hnswlib 包装器读取,因为序列化格式与原始 hnswlib 不兼容。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra_serialize.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); // create an index with `auto index = raft::cagra::build(...);` cuvs::neighbors::cagra::serialize_to_hnswlib(handle, filename, index);
- 参数:
handle – [in] raft 句柄
filename – [in] 保存索引的文件名
index – [in] CAGRA 索引
dataset – [in] [可选] 存储数据集的主机数组,如果索引不包含数据集则必需。
- void serialize_to_hnswlib(
- raft::resources const &handle,
- std::ostream &os,
- const cuvs::neighbors::cagra::index<uint8_t, uint32_t, int64_t> &index,
- std::optional<raft::host_matrix_view<const uint8_t, int64_t, raft::row_major>> dataset = std::nullopt
将构建的 CAGRA 索引作为基础层 HNSW 索引写入输出流。注意:保存的索引只能由 cuVS 中的 hnswlib 包装器读取,因为序列化格式与原始 hnswlib 不兼容。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra_serialize.hpp> raft::resources handle; // create an output stream std::ostream os(std::cout.rdbuf()); // create an index with `auto index = raft::cagra::build(...);` cuvs::neighbors::cagra::serialize_to_hnswlib(handle, os, index);
- 参数:
handle – [in] raft 句柄
os – [in] 输出流
index – [in] CAGRA 索引
dataset – [in] [可选] 存储数据集的主机数组,如果索引不包含数据集则必需。
- void serialize_to_hnswlib(
- raft::resources const &handle,
- const std::string &filename,
- const cuvs::neighbors::cagra::index<uint8_t, uint32_t, int64_t> &index,
- std::optional<raft::host_matrix_view<const uint8_t, int64_t, raft::row_major>> dataset = std::nullopt
以 hnswlib 仅包含基础层的序列化格式保存构建的 CAGRA 索引。注意:保存的索引只能由 cuVS 中的 hnswlib 包装器读取,因为序列化格式与原始 hnswlib 不兼容。
实验性功能,API 和序列化格式都可能会更改。
#include <raft/core/resources.hpp> #include <cuvs/neighbors/cagra_serialize.hpp> raft::resources handle; // create a string with a filepath std::string filename("/path/to/index"); // create an index with `auto index = raft::cagra::build(...);` cuvs::neighbors::cagra::serialize_to_hnswlib(handle, filename, index);
- 参数:
handle – [in] raft 句柄
filename – [in] 保存索引的文件名
index – [in] CAGRA 索引
dataset – [in] [可选] 存储数据集的主机数组,如果索引不包含数据集则必需。
-
struct ivf_pq_params#
- #include <cagra.hpp>
利用 IVF-PQ 构建 knn 图的专用参数。