GPU_CAGRA
GPU_CAGRA 索引是为 GPU 优化的基于图的索引。与使用昂贵的训练级 GPU 相比,使用推理级 GPU 运行 Milvus GPU 版本可以更具成本效益。
建立索引
要在 Milvus 中的向量字段上建立 GPU_CAGRA 索引,请使用 add_index() 方法,为索引指定 index_type、metric_type 以及附加参数。
- Python
- Java
- Node.js
- Go
- cURL
from pymilvus import MilvusClient
client = MilvusClient(uri="http://localhost:19530")
index_params = client.prepare_index_params()
index_params.add_index(
field_name="your_vector_field_name",
index_type="GPU_CAGRA",
index_name="vector_index",
metric_type="L2",
params={
"intermediate_graph_degree": 64,
"graph_degree": 32,
"build_algo": "IVF_PQ",
"cache_dataset_on_device": "True",
"adapt_for_cpu": "False"
},
)
client.create_index(
collection_name="your_collection_name",
index_params=index_params,
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
Map<String, Object> extraParams = new HashMap<>();
extraParams.put("intermediate_graph_degree", 64);
extraParams.put("graph_degree", 32);
extraParams.put("build_algo", "IVF_PQ");
extraParams.put("cache_dataset_on_device", "true");
extraParams.put("adapt_for_cpu", "false");
IndexParam indexParam = IndexParam.builder()
.fieldName("your_vector_field_name")
.indexName("vector_index")
.indexType(IndexParam.IndexType.GPU_CAGRA)
.metricType(IndexParam.MetricType.L2)
.extraParams(extraParams)
.build();
client.createIndex(CreateIndexReq.builder()
.collectionName("your_collection_name")
.indexParams(Collections.singletonList(indexParam))
.build());
await client.createIndex({
"collection_name": "your_collection_name",
"field_name": "your_vector_field_name",
"index_name": "vector_index",
"index_type": "GPU_CAGRA",
"metric_type": "L2",
"params": {
"intermediate_graph_degree": 64,
"graph_degree": 32,
"build_algo": "IVF_PQ",
"cache_dataset_on_device": "true",
"adapt_for_cpu": "false"
}
});
idx := index.NewGenericIndex("", map[string]string{
"index_type": "GPU_CAGRA",
"metric_type": "L2",
"intermediate_graph_degree": "64",
"graph_degree": "32",
"build_algo": "IVF_PQ",
"cache_dataset_on_device": "true",
"adapt_for_cpu": "false",
})
task, err := client.CreateIndex(
ctx,
milvusclient.NewCreateIndexOption(
"your_collection_name",
"your_vector_field_name",
idx,
).WithIndexName("vector_index"),
)
if err != nil {
panic(err)
}
if err := task.Await(ctx); err != nil {
panic(err)
}
curl --request POST \
--url "http://localhost:19530/v2/vectordb/indexes/create" \
--header "Authorization: Bearer root:Milvus" \
--header "Content-Type: application/json" \
--data '{
"collectionName": "your_collection_name",
"indexParams": [
{
"fieldName": "your_vector_field_name",
"indexName": "vector_index",
"indexType": "GPU_CAGRA",
"metricType": "L2",
"params": {
"intermediate_graph_degree": 64,
"graph_degree": 32,
"build_algo": "IVF_PQ",
"cache_dataset_on_device": "true",
"adapt_for_cpu": "false"
}
}
]
}'
在此配置中
-
index_type:要建立的索引类型。在本例中,将值设为GPU_CAGRA。 -
metric_type:用于计算向量间距离的方法。有关详情,请参阅 "度量类型"。
配置好索引参数后,可直接使用 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 搜索
要在加载时动态启用 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 构建的。
-
如果省略或为假,索引将保留在 GPU 上,搜索也在 GPU 上运行。
在混合或对成本敏感的环境中,GPU 资源被保留用于索引构建,但搜索在 CPU 上运行,此时可使用负载时 CPU 适应。
索引参数
本节概述了用于构建索引和在索引上执行搜索的参数。
索引构建参数
下表列出了 建立索引 时可在 params 中配置的参数。
参数 | 描述 | 默认值 |
|---|---|---|
| 通过在剪枝前确定图的度数来影响召回率和建立时间。推荐值为 |
|
| 通过设置剪枝后图形的度数来影响搜索性能和召回率。这两个度数之间的差值越大,构建时间就越长。其值必须小于 |
|
| 选择剪枝前的图生成算法。可能的值
|
|
| 决定是否在 GPU 内存中缓存原始数据集。可能的值
|
|
| 决定是否使用 GPU 建立索引和使用 CPU 进行搜索。 将该参数设置为 |
|
特定于索引的搜索参数
下表列出了 在索引上搜索 时可在 search_params.params 中配置的参数。
参数 | 描述 | 默认值 |
|---|---|---|
| 决定搜索过程中保留的中间结果的大小。较大的值可能会提高召回率,但会降低搜索性能。该值至少应等于最终的 top-k(limit)值,通常是 2 的幂次(如 16、32、64、128)。 | 空 |
| 指定搜索过程中进入 CAGRA 图的入口点数量。增加该值可以提高召回率,但可能会影响搜索性能(如 1、2、4、8、16、32)。 | 空 |
| 控制 CAGRA 在为图搜索选择初始入口点时执行的随机采样量。较大的值会让 CAGRA 有更多机会从更优的点开始,从而提高召回率,但会增加搜索延迟。该值必须至少为 |
|
| 控制搜索迭代过程。默认设置为 |
|
| 指定用于在 GPU 上计算度量距离的 CUDA 线程数。常用值是 2 的幂次,最高可达 32(例如 2、4、8、16、32)。它对搜索性能影响不大。默认值为 |
|
| 指定查询时间/准确性的权衡。 如果在建立索引时将 |
|