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
)

在此設定中:

  • 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 上執行。

請在混合或成本敏感的環境中使用載入時的 CPU 適應功能,此類環境中 GPU 資源會預留給索引建置,但搜尋則在 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 記憶體,以節省 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

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?