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 方法中傳入索引參數來建立索引。如需詳細資訊,請參閱建立集合。
在索引上搜尋
索引建立且實體插入後,您就可以在索引上執行相似性搜尋。
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
)
在此配置中
在載入時啟用 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)。即使索引原本是為 GPU 建立的,後續的搜尋作業也會在 CPU 上執行。
如果省略或為 false,索引會保留在 GPU 上,搜尋也會在 GPU 上執行。
在混合或對成本敏感的環境中使用負載時 CPU 適應,其中 GPU 資源保留用於索引建立,但搜尋在 CPU 上執行。
索引參數
本節概述用於建立索引和在索引上執行搜尋的參數。
索引建立參數
下表列出了建立索引時可在params 中設定的參數。
參數 |
說明 |
預設值 |
|---|---|---|
|
通過在剪枝之前確定圖表的度來影響召回和建立時間。建議值為 |
|
|
透過設定剪枝後的圖形度,影響搜尋效能和召回率。這兩個程度之間的差異越大,建立時間越長。其值必須小於 |
|
|
選擇剪枝前的圖生成演算法。可能的值:
|
|
|
決定是否在 GPU 記憶體中快取原始資料集。可能的值:
|
|
|
決定是否使用 GPU 建立索引,並使用 CPU 進行搜尋。 將此參數設定為 |
|
特定於索引的搜尋參數
下表列出在索引上搜尋時,可在search_params.params 中設定的參數。
參數 |
說明 |
預設值 |
|---|---|---|
|
決定搜尋期間保留的中間結果大小。較大的值可能會在犧牲搜尋效能的情況下提高召回率。它應該至少等於最終 top-k (限制) 值,而且通常是 2 的幂次 (例如:16、32、64、128)。 |
空 |
|
指定搜尋期間進入 CAGRA 圖形的入口點數量。增加此值可提高召回率,但可能會影響搜尋效能(例如:1、2、4、8、16、32)。 |
空 |
|
控制搜尋的迭代過程。預設值為 |
|
|
指定用於計算 GPU 公制距離的 CUDA 線程數。常見的值是 2 的幂次,最高為 32 (例如 2、4、8、16、32)。它對搜尋效能影響不大。預設值是 |
|
|
指定查詢時間/精確度的權衡。 如果在建立索引時將 |
|