GPU_CAGRA

GPU_CAGRA索引是一种针对 GPU 进行优化的基于图的索引。与使用昂贵的训练级 GPU 相比,使用推理级 GPU 运行 Milvus GPU 版本可能更具成本效益。

构建索引

要在 Milvus 中针对向量字段构建GPU_CAGRA 索引,请使用add_index() 方法,并指定index_type 、metric_type 以及索引的其他参数。

from pymilvus import MilvusClient

# Prepare index building params
index_params = MilvusClient.prepare_index_params()

index_params.add_index(
    field_name="your_vector_field_name", # Name of the vector field to be indexed
    index_type="GPU_CAGRA", # Type of the index to create
    index_name="vector_index", # Name of the index to create
    metric_type="L2", # Metric type used to measure similarity
    params={
        "intermediate_graph_degree": 64, # Affects recall and build time by determining the graph’s degree before pruning
        "graph_degree": 32, # Affets search performance and recall by setting the graph’s degree after pruning
        "build_algo": "IVF_PQ", # Selects the graph generation algorithm before pruning
        "cache_dataset_on_device": "true", # Decides whether to cache the original dataset in GPU memory
        "adapt_for_cpu": "false", # Decides whether to use GPU for index-building and CPU for search
    } # Index building params
)

在此配置中:

  • index_type: 要构建的索引类型。在此示例中,将值设置为GPU_CAGRA 。

  • metric_type: 用于计算向量之间距离的方法。有关详细信息,请参阅《度量类型》。

  • params: 构建索引的其他配置选项。要了解GPU_CAGRA 索引可用的更多构建参数,请参阅《索引构建参数》。

配置好索引参数后,您可以直接使用create_index() 方法创建索引,或在create_collection 方法中传入索引参数。详情请参阅“创建Collection”。

在索引上进行搜索

索引构建完成且实体插入后,即可对索引执行相似度搜索。

search_params = {
    "params": {
        "itopk_size": 16, # Determines the size of intermediate results kept during the search
        "search_width": 8, # Specifies the number of entry points into the CAGRA graph during the search
    }
}

res = MilvusClient.search(
    collection_name="your_collection_name", # Collection name
    anns_field="vector_field", # Vector field name
    data=[[0.1, 0.2, 0.3, 0.4, 0.5]],  # Query vector
    limit=3,  # TopK results to return
    search_params=search_params
)

在此配置中:

  • params:针对索引搜索的附加配置选项。要了解GPU_CAGRA 索引可用的更多搜索参数,请参阅《索引特定搜索参数》。

在加载时启用 CPU 搜索Compatible with Milvus 2.6.4+

若要在加载时动态启用 CPU 搜索,请在milvus.yaml 中编辑以下配置:

# milvus.yaml
knowhere:
  GPU_CAGRA:
    load: 
      adapt_for_cpu: true

行为

  • 当load.adapt_for_cpu 设置为true 时,Milvus会在加载过程中将GPU_CAGRA索引转换为可在CPU上执行的格式(类似HNSW)。

  • 后续的搜索操作将在 CPU 上执行,即使该索引最初是为 GPU 构建的。

  • 如果省略或设置为 false,索引将保留在 GPU 上,搜索操作也在 GPU 上运行。

在混合或成本敏感的环境中,GPU 资源被保留用于索引构建,但搜索在 CPU 上运行,请使用加载时的 CPU 适配功能。

索引参数

本节概述了用于构建索引和在索引上执行搜索的参数。

索引构建参数

下表列出了在params 中构建索引时可配置的参数。

参数

描述

默认值

intermediate_graph_degree

通过确定修剪前图的度数来影响召回率和构建时间。推荐值为32 或64 。

128

graph_degree

通过设置修剪后图的度数来影响搜索性能和召回率。这两个度数之间的差异越大,构建时间就越长。其值必须小于intermediate_graph_degree 的值。

64

build_algo

选择修剪前的图生成算法。可用值:

  • IVF_PQ:提供更高质量,但构建时间较长。

  • NN_DESCENT: 构建速度更快,但召回率可能较低。

IVF_PQ

cache_dataset_on_device

决定是否将原始数据集缓存到 GPU 内存中。可用值:

  • "true": 将原始数据集缓存到GPU内存中,通过优化搜索结果来提高召回率。

  • "false": 不缓存原始数据集,以节省 GPU 内存。

"false"

adapt_for_cpu

决定是否使用 GPU 进行索引构建,并使用 CPU 进行搜索。

将此参数设置为"true" 时,搜索请求中必须包含ef 参数。

"false"

索引特定搜索参数

下表列出了在索引上进行搜索时,可在search_params.params 中配置的参数。

参数

描述

默认值

itopk_size

确定搜索过程中保留的中间结果集大小。 较大的值可能会提高召回率,但会牺牲搜索性能。该值应至少等于最终的 top-k(限制)值,并且通常是 2 的幂(例如 16、32、64、128)。

空

search_width

指定搜索过程中进入 CAGRA 图的入口点数量。增加此值可以提高召回率,但可能会影响搜索性能(例如 1、2、4、8、16、32)。

空

num_random_samplings

控制CAGRA在选择图搜索初始入口点时进行随机采样的程度。较大的数值使CAGRA有更多机会从更优的点开始,从而提高召回率,但会增加搜索延迟。该值必须不小于1 。此功能在Milvus 2.6.20及以上版本中可用。

1

min_iterations /max_iterations

控制搜索迭代过程。默认设置为0 ,CAGRA会根据itopk_size 和search_width 自动确定迭代次数。手动调整这些值有助于平衡性能与准确性。

0

team_size

指定用于在 GPU 上计算度量距离的 CUDA 线程数。常见值为 2 的幂(最大 32,例如 2、4、8、16、32)。该参数对搜索性能影响较小。 默认值为0 ,其中 Milvus 会根据向量维度自动选择team_size 。

0

ef

指定查询时间与精度的权衡。ef 值越高,搜索精度越高,但速度越慢。

若在构建索引时将adapt_for_cpu 设置为true ,则此参数为必填项。

[top_k, int_max]

翻译自DeepL

想要更快、更简单、更好用的 Milvus SaaS服务 ?

Zilliz Cloud是基于Milvus的全托管向量数据库,拥有更高性能,更易扩展,以及卓越性价比

免费试用 Zilliz Cloud
反馈

此页对您是否有帮助?