如何测试faiss是否为gpu版本

这个脚本将测量使用GPU进行搜索的时间。如果搜索时间显著减少,那么GPU版本的Faiss应该正常工作。

本人实测,gpu版本的输出应该如下:

复制代码
Faiss支持GPU
搜索时间: 0.0423 秒

cpu的输出为:

复制代码
Faiss不支持GPU
搜索时间: 66.4641 秒
python 复制代码
import faiss
import numpy as np
import time

d = 64                           # 向量维度
nb = 100000                      # 数据集大小
nq = 10000                       # 查询集大小
np.random.seed(0)                # 设置随机种子

# 检查GPU支持
if faiss.get_num_gpus() > 0:
    print("Faiss支持GPU")
else:
    print("Faiss不支持GPU")

# 创建数据集和查询集
xb = np.random.random((nb, d)).astype('float32')
xq = np.random.random((nq, d)).astype('float32')

# 使用GPU进行搜索
index = faiss.IndexFlatL2(d)     # 使用L2距离
if faiss.get_num_gpus() > 0:
    res = faiss.StandardGpuResources()  # 使用默认的GPU资源
    index = faiss.index_cpu_to_gpu(res, 0, index)

# 计时搜索
start_time = time.time()
index.add(xb)
D, I = index.search(xq, 10)      # 搜索最近的10个邻居
end_time = time.time()

# 打印搜索时间
print(f"搜索时间: {end_time - start_time:.4f} 秒")
相关推荐
海天一色y1 天前
深入理解 RAG 技术:从语义张量到向量数据库,Milvus 与 FAISS 全面对比
数据库·milvus·faiss
沪漂阿龙2 天前
Vector Store:FAISS、Chroma、Milvus、Qdrant、ES 怎么选?
人工智能·elasticsearch·架构·milvus·faiss
程序员佳佳4 天前
四个月长期实测:自建 Milvus、FAISS、原生向量 API 和向量引擎中转方案,到底怎么选?
人工智能·windows·python·gpt·milvus·faiss
li星野5 天前
从 PDF 到 FAISS 向量索引:构建本地 RAG 数据预处理流水线
pdf·faiss
veminhe8 天前
关于下载pip install faiss-cpu失败的问题
python·pip·faiss
程序员佳佳8 天前
我在 Windows 和低配 Linux 上做 RAG:Milvus、FAISS、向量 API 中转的中立实测
linux·人工智能·windows·gpt·aigc·milvus·faiss
li星野11 天前
FAISS 详解:原理、使用与面试指南——向量检索的基石
面试·职场和发展·faiss
尽兴-11 天前
2.3 向量数据库:FAISS、Chroma、Milvus、Pinecone、Qdrant
pinecone·milvus·faiss·向量数据库·chroma·qdrant
codefan※15 天前
RAG 加速指南:Faiss / Milvus / Qdrant 向量库选型与调优
知识图谱·milvus·faiss·向量数据库·rag·qdrant