如何测试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} 秒")
相关推荐
SHIPKING39314 小时前
【嵌入模型与向量数据库】
embedding·faiss·向量数据库·阿里百炼
扉间7986 天前
Faiss 索引深度解析:从基础到实战
faiss
tangjunjun-owen8 天前
第三章:langchain加载word文档构建RAG检索教程(基于FAISS库为例)
langchain·llm·word·faiss·rag
BB_CC_DD23 天前
五. 以聚类和搜图方式清洗图像数据集,采用Pickle和Faiss(百万数据集,ms级响应)快速搜图(附完整代码)
深度学习·聚类·faiss
邴越1 个月前
不同向量数据库(FAISS / Pinecone / Weaviate)在 RAG 中的优缺点
数据库·faiss
maxmaxma2 个月前
检索增强生成RAG with LangChain、OpenAI and FAISS
langchain·faiss·rag
maxmaxma2 个月前
LLM之向量数据库Chroma milvus FAISS
数据库·milvus·faiss
tortorish2 个月前
faiss-gpu安装方法
faiss
MMMMMMMay Love Code2 个月前
大模型工程师学习日记(十一):FAISS 高效相似度搜索和密集向量聚类的库
学习·聚类·faiss
橙子小哥的代码世界4 个月前
打造RAG系统:四大向量数据库Milvus、Faiss、Elasticsearch、Chroma 全面对比与选型指南
数据库·人工智能·深度学习·神经网络·elasticsearch·milvus·faiss