FAISS:快速入门到实战 目录 引言 基础使用 索引 问答实战 GPU加速 总结 引言 Faiss(Facebook AI Similarity Search)是由Facebook AI Research开发的高性能相似性搜索库,专门用于密集向量的相似性搜索和聚类操作。该库能够高效处理十亿级别的大规模向量数据,是目前最为成熟的近似近邻搜索(Approximate Nearest Neighbor, ANN)库之一。Faiss的核心价值在于它能够帮助开发者和研究人员在海量高维数据中快速找到最相似的内容,这种能力在众多AI应用中至关重要。 Faiss库的设计哲学是在保证搜索质量的同时,显著提升搜索速度。它提供了多种索引类型和搜索算法,让用户能够根据具体需求在精度和速度之间做出合理的权衡。
Faiss(Facebook AI Similarity Search)是由Facebook AI Research开发的高性能相似性搜索库,专门用于密集向量的相似性搜索和聚类操作。该库能够高效处理十亿级别的大规模向量数据,是目前最为成熟的近似近邻搜索(Approximate Nearest Neighbor, ANN)库之一。Faiss的核心价值在于它能够帮助开发者和研究人员在海量高维数据中快速找到最相似的内容,这种能力在众多AI应用中至关重要。
Faiss库的设计哲学是在保证搜索质量的同时,显著提升搜索速度。它提供了多种索引类型和搜索算法,让用户能够根据具体需求在精度和速度之间做出合理的权衡。Faiss使用C++编写核心代码,并提供了完整的Python/numpy接口,使得用户能够方便地在Python环境中使用其强大功能。此外,Faiss还对GPU提供了深度支持,利用GPU的并行计算能力进一步加速相似性搜索任务。
相似性搜索是许多机器学习应用中的基础操作,它基于一个简单而重要的概念:在向量空间中,相似的数据点在位置上也彼此接近。Faiss支持多种相似性度量方式,包括L2距离(欧几里得距离)、点积和余弦相似度等。其中,L2距离是最常用的相似性度量,计算两个向量之间的直线距离;点积则反映了两个向量的方向相似性;而余弦相似度实质上是归一化向量上的点积,特别适用于衡量文本或图像等高维数据的语义相似性。
在现代人工智能应用中,Faiss的GPU加速功能显得尤为重要。传统CPU在处理大规模向量搜索时往往力不从心,而GPU凭借其大规模并行架构,能够同时处理数百甚至数千个向量计算任务,为相似性搜索带来数量级的性能提升。Faiss的GPU实现经过精心优化,不仅支持单GPU加速,还提供了多GPU并行处理能力,使得用户能够在合理硬件成本下处理十亿级向量的实时搜索任务。
Faiss Chapter中的除了GPU的部分外,其他大部分的技术细节或者原理都在Milvus章节有详细的介绍,比如向量维度、归一化、量化以及索引选择指南等细节信息。
# CPU版本 pip install faiss-cpu # GPU版本(需要CUDA环境) pip install faiss-gpu
首先解释下为什么要有训练(train)、添加(add)和搜索(search)这几个操作:
IndexFlatL2 这种暴力搜索索引,不需要学习数据分布,所以不用训练。所以,当索引创建好,完成必要的训练后,我们就可以使用 add 把数据添加进去,再用 search 进行相似性搜索。
import faiss import numpy as np # 基本使用流程示例 def basic_faiss_usage(): # 向量维度 dimension = 128 # 生成示例数据 np.random.seed(1234) # 数据库向量:1000个128维向量 database_vectors = np.random.random((1000, dimension)).astype('float32') # 查询向量:5个128维向量 query_vectors = np.random.random((5, dimension)).astype('float32') # 1. 创建索引 index = faiss.IndexFlatL2(dimension) # 2. 添加数据到索引 index.add(database_vectors) print(f"索引是否已训练:{index.is_trained}") # 3. 执行搜索 k = 3 # 返回每个查询的最相似3个结果 distances, indices = index.search(query_vectors, k) print("搜索结果索引:", indices) print("对应距离:", distances) basic_faiss_usage()
# L2距离(欧氏距离)索引 index_l2 = faiss.IndexFlatL2(dimension) # 内积相似度索引 index_ip = faiss.IndexFlatIP(dimension) # 带ID映射的索引(便于跟踪原始ID) index_id = faiss.IndexIDMap(index_l2)
# 添加向量数据 def add_vectors_example(): dimension = 64 index = faiss.IndexFlatL2(dimension) # 生成100个64维向量 vectors = np.random.random((100, dimension)).astype('float32') # 添加向量到索引 index.add(vectors) print(f"索引中的向量数量:{index.ntotal}") # 添加带自定义ID的向量 index_with_ids = faiss.IndexIDMap(faiss.IndexFlatL2(dimension)) vector_ids = np.arange(100, 200, dtype=np.int64) # 自定义ID index_with_ids.add_with_ids(vectors, vector_ids) add_vectors_example()
import faiss import numpy as np def search_operations(): dimension = 64 index = faiss.IndexFlatL2(dimension) # 生成100个64维向量 vectors = np.random.random((100, dimension)).astype('float32') # 添加向量到索引 index.add(vectors) print(f"索引中的向量数量:{index.ntotal}") k = 2 # 生成10个查询向量 queries = np.random.random((4, dimension)).astype('float32') distances, indices = index.search(queries, k) return distances, indices distances, indices = search_operations() print("距离:", distances) print("索引:", indices)
import faiss import numpy as np def index_management(): dimension = 64 index = faiss.IndexFlatL2(dimension) vectors = np.random.rand(1000, dimension).astype('float32') index.add(vectors) # 检查索引状态 print(f"索引是否已训练:{index.is_trained}") print(f"索引中的向量数量:{index.ntotal}") # 重置索引(清空所有数据) index.reset() print(f"重置后向量数量:{index.ntotal}") index_management()
FAISS提供了多种索引类型,每种索引在精度、速度和内存使用之间有不同的权衡。
使用场景:
特点:
def flat_index_demo(): dimension = 256 n_vectors = 5000 data = np.random.random((n_vectors, dimension)).astype('float32') # 创建L2距离索引 index_l2 = faiss.IndexFlatL2(dimension) index_l2.add(data) # 创建内积相似度索引 index_ip = faiss.IndexFlatIP(dimension) index_ip.add(data) return index_l2, index_ip index_l2, index_ip = flat_index_demo()
使用场景:
特点:
import faiss import numpy as np def ivf_index_demo(): dimension = 128 n_vectors = 50000 n_clusters = 1024 # 聚类中心数量 print(f"初始化参数 - 维度: {dimension}, 向量数量: {n_vectors}, 聚类中心数量: {n_clusters}") # 生成数据 data = np.random.random((n_vectors, dimension)).astype('float32') print("数据生成完成,数据形状:", data.shape) # 创建IVF索引 quantizer = faiss.IndexFlatL2(dimension) # 量化器 index = faiss.IndexIVFFlat(quantizer, dimension, n_clusters, faiss.METRIC_L2) print("IVF索引创建完成") print("-------------------------------------------------------------------------------------------") # 训练索引 assert not index.is_trained print("开始训练索引...") index.train(data) assert index.is_trained print("索引训练完成") print("-------------------------------------------------------------------------------------------") # 添加数据 index.add(data) print("数据添加到索引完成") # 设置搜索时检查的聚类数量(平衡速度与精度) index.nprobe = 16 print(f"设置搜索时检查的聚类数量为: {index.nprobe}") # 搜索 queries = np.random.random((10, dimension)).astype('float32') print("查询向量生成完成,形状:", queries.shape) distances, indices = index.search(queries, 5) print("-------------------------------------------------------------------------------------------") print("搜索完成") print("搜索结果 - 距离:", distances) print("搜索结果 - 索引:", indices) return index ivf_index = ivf_index_demo()
使用场景:
特点:
HNSW索引属于图索引,它通过构建节点之间的连接关系来组织向量数据。
构建过程只是将新向量添加到图中并建立相应的连接,因此不需要训练阶段。
而在FAISS中,需要训练的索引通常是基于量化(Quantization)或聚类(Clustering)的索引,
例如 IndexIVFFlat、IndexIVFPQ、IndexFlatL2 等。这些索引在添加数据前需要先通过训练数据学习量化或聚类的参数。
import faiss import numpy as np def hnsw_index_demo(): dimension = 128 n_vectors = 1000 data = np.random.random((n_vectors, dimension)).astype('float32') # 创建HNSW索引 index = faiss.IndexHNSWFlat(dimension, 32) # 32表示每个节点的连接数 print("创建HNSW索引") print("-------------------------------------------------------------------------------------------") # 设置构建参数 index.hnsw.efConstruction = 200 # 构建时考虑的邻居数量 index.hnsw.efSearch = 50 # 搜索时考虑的邻居数量 # 添加数据 index.add(data) print(f"已向索引中添加 {n_vectors} 条数据") print("-------------------------------------------------------------------------------------------") # 搜索 queries = np.random.random((5, dimension)).astype('float32') distances, indices = index.search(queries, 3) print("搜索完成,搜索结果示例:") print("距离:", distances) print("索引:", indices) return index hnsw_index = hnsw_index_demo()
使用场景:
特点:
def pq_index_demo(): dimension = 128 n_vectors = 100000 data = np.random.random((n_vectors, dimension)).astype('float32') # 创建PQ索引参数 m = 8 # 子量化器数量(必须能被dimension整除) n_bits = 8 # 每个子量化器的位数 # 创建PQ索引 index = faiss.IndexPQ(dimension, m, n_bits) # 训练并添加数据 index.train(data) index.add(data) print(f"PQ索引大小:{index.ntotal} 个向量") return index pq_index = pq_index_demo()
使用场景:
import faiss import numpy as np def ivfpq_index_demo(): dimension = 128 n_vectors = 1000000 n_clusters = 1024 data = np.random.random((n_vectors, dimension)).astype('float32') # 创建IVFPQ索引 quantizer = faiss.IndexFlatL2(dimension) m = 16 # 字节数(压缩后) index = faiss.IndexIVFPQ(quantizer, dimension, n_clusters, m, 8) # 训练并添加数据 index.train(data) index.add(data) index.nprobe = 32 print(f"聚类中心数量: {n_clusters}") print(f"压缩后字节数: {m}") return index ivfpq_index = ivfpq_index_demo()
IVF(Inverted File Index)索引的核心思想是"分而治之":
数学原理:
HNSW(Hierarchical Navigable Small World)基于小世界网络理论:
优势:
乘积量化通过向量分解和标量化化实现压缩:
压缩效果:
def benchmark_indices(): """不同索引的性能比较""" dimension = 128 n_vectors = 100000 n_queries = 1000 # 生成测试数据 data = np.random.random((n_vectors, dimension)).astype('float32') queries = np.random.random((n_queries, dimension)).astype('float32') indices = { 'FlatL2': faiss.IndexFlatL2(dimension), 'IVFFlat': faiss.IndexIVFFlat(faiss.IndexFlatL2(dimension), dimension, 1024), 'HNSW': faiss.IndexHNSWFlat(dimension, 32), 'IVFPQ': faiss.IndexIVFPQ(faiss.IndexFlatL2(dimension), dimension, 1024, 16, 8) } # 训练需要训练的索引 for name, index in indices.items(): if name != 'FlatL2' and name != 'HNSW': index.train(data) index.add(data) if name == 'IVFFlat' or name == 'IVFPQ': index.nprobe = 32 # 测试搜索性能 results = {} for name, index in indices.items(): import time start = time.time() distances, indices_result = index.search(queries, 10) end = time.time() results[name] = { 'time': end - start, 'throughput': n_queries / (end - start) } return results # 性能测试结果 performance_results = benchmark_indices() for name, result in performance_results.items(): print(f"{name}: {result['throughput']:.1f} queries/second")
在安装Faiss GPU版本之前,需要确保系统满足基本的硬件和软件要求。首先,必须具备NVIDIA显卡,并安装合适版本的CUDA工具包。Faiss通常支持CUDA 11.4及以上版本,具体版本要求可能因Faiss发行版而异。其次,需要安装对应版本的NVIDIA驱动,建议使用较新的驱动版本以保证兼容性。软件方面,需要Python环境(通常为3.6及以上版本)和基本的科学计算库如numpy。
验证系统是否符合安装要求的方法很简单。对于GPU环境,可以使用nvidia-smi命令查看显卡信息和CUDA版本。对于Python环境,可以使用python --version检查Python版本,并使用import numpy验证numpy是否可用。如果这些基础依赖项没有问题,就可以开始安装Faiss GPU版本了。
Faiss GPU版本有多种安装方式,根据用户的需求和技术背景可以选择最适合的方法。对于大多数用户,推荐使用Conda安装,这种方法简单快捷,能自动解决大部分依赖问题。对于有特殊需求的高级用户,可以考虑源码编译安装,以便进行特定优化或自定义功能。此外,还可以通过Docker方式安装,这种方式能提供完全隔离的环境,避免系统污染。
pip安装
上文提到的conda,适合于本地,但是本教程为了让学习者更好的操作,选择使用魔搭社区中GPU的noteBook上通过pip安装Faiss。
我使用的GPU环境为ubunitu22.04-cuda12.1.0-py311-torch2.3.1
pip install faiss-gpu-cu12
往往cpu和gpu版本的库不能共存,如果你下载了CPU版本的Faiss,那么使用GPU版本之前需要先卸载cpu版本的,才能下载gpu版本的
Conda安装是最简单的方法,只需执行以下命令即可:
# 安装基础GPU版本(包含CUDA支持) conda install -c pytorch -c nvidia faiss-gpu=1.12.0 # 或者安装NVIDIA cuVS加速版(需要CUDA 12.4+) conda install -c pytorch -c nvidia -c rapidsai -c conda-forge libnvjitlink faiss-gpu-cuvs=1.12.0
Conda安装方式会自动处理所有依赖关系,包括CUDA工具包和必要的库文件。安装完成后,可以通过Python接口验证是否安装成功。
对于需要定制化功能或性能优化的用户,源码编译是更好的选择。源码编译虽然过程复杂,但可以针对特定硬件架构进行优化,如启用AVX2或AVX512指令集加速。以下是基本的源码编译步骤:
# 1. 克隆Faiss源码 git clone https://github.com/facebookresearch/faiss.git cd faiss # 2. 配置编译环境(以CUDA 12.1为例) cmake -B build . \ -DCMAKE_BUILD_TYPE=Release \ -DFAISS_ENABLE_GPU=ON \ -DCUDAToolkit_ROOT=/usr/local/cuda-12.1 \ -DCMAKE_CUDA_ARCHITECTURES="75;86" \ -DFAISS_OPT_LEVEL=avx2 # 3. 编译Faiss make -C build -j faiss_gpu # 4. 安装Python绑定 make -C build -j swigfaiss (cd build/faiss/python && python setup.py install)
源码编译过程中,关键配置项包括-DFAISS_ENABLE_GPU=ON(启用GPU支持)、-DCUDAToolkit_ROOT(指定CUDA路径)和-DCMAKE_CUDA_ARCHITECTURES(指定GPU架构)。用户应根据自己的GPU架构调整这些参数,以获得最佳性能。
安装完成后,需要验证Faiss GPU是否正常工作。可以通过简单的Python脚本来测试基础功能和GPU加速效果:
import numpy as np import faiss # 测试GPU资源是否可用 res = faiss.StandardGpuResources() print("GPU资源初始化成功") # 创建测试数据 d = 128 # 向量维度 nb = 10000 # 数据库大小 nq = 100 # 查询数量 np.random.seed(1234) xb = np.random.random((nb, d)).astype('float32') xq = np.random.random((nq, d)).astype('float32') # 创建GPU索引 index = faiss.IndexFlatL2(d) # 创建CPU索引 gpu_index = faiss.index_cpu_to_gpu(res, 0, index) # 转换为GPU索引 # 添加数据并搜索 gpu_index.add(xb) k = 4 # 返回最近邻数量 D, I = gpu_index.search(xq, k) print("搜索完成,前5个查询结果:") print("索引:", I[:5]) print("距离:", D[:5])
如果脚本正常运行并输出合理结果,说明Faiss GPU安装成功。此外,还可以通过比较CPU和GPU版本的搜索速度来直观感受性能提升。
Faiss提供了丰富多样的索引类型,每种索引都有其特定的适用场景和性能特点。根据索引结构和算法的不同,Faiss GPU索引大致可以分为三类:Flat索引、量化索引和层次导航图索引。Flat索引提供精确搜索,保证结果准确但内存消耗较大;量化索引采用向量压缩技术,显著减少内存占用但会损失一定精度;层次导航图索引基于图结构构建搜索路径,在保证较高准确率的同时提供快速的搜索速度。
选择合适的索引类型需要考虑多个因素,包括数据规模、向量维度、精度要求、搜索速度需求和硬件资源等。对于小规模数据集(如百万级别以下),Flat索引通常是理想选择,因为它能提供精确结果且实现简单。对于中到大规規数据集(百万到十亿级别),量化索引如IVF-PQ和层次导航图索引如HNSW更能平衡资源使用和搜索性能。此外,还需要考虑索引的训练需求,有些索引类型需要额外的训练阶段来构建内部数据结构。
Flat索引是最简单直接的索引类型,它不加转换地存储原始向量,并通过暴力比对的方式计算查询向量与数据库中所有向量的距离。Faiss GPU支持多种Flat索引,包括GpuIndexFlatL2(基于L2距离)、GpuIndexFlatIP(基于点积)和GpuIndexFlatCos(基于余弦相似度)。这些索引虽然简单,但在许多场景下非常实用,特别是当数据规模不大或需要精确结果时。
Flat索引的主要优点是100%准确率,因为它不采用任何近似算法。同时,由于不需要训练过程,Flat索引可以立即添加数据并进行搜索。然而,它的明显缺点是搜索速度与数据量成线性关系,当数据量很大时,搜索性能会显著下降在CPU下使用单线程或多线程顺序计算
在GPU下并行计算所有向量对的距离,利用数千个CUDA核心同时计算。此外,Flat索引的内存占用也最高,因为它需要存储所有原始向量。
使用场景:小到中等数据集、需要100%精度、基准测试和精度验证
以下是Flat索引的使用示例:
import faiss import numpy as np import time def benchmark_flat_l2(): dimension = 768 n_vectors = 100000 n_queries = 1000 k = 10 np.random.seed(42) database = np.random.random((n_vectors, dimension)).astype('float32') queries = np.random.random((n_queries, dimension)).astype('float32') # CPU 版本 cpu_index = faiss.IndexFlatL2(dimension) start_time = time.time() cpu_index.add(database) cpu_add_time = time.time() - start_time start_time = time.time() cpu_distances, cpu_indices = cpu_index.search(queries, k) cpu_search_time = time.time() - start_time # GPU 版本 res = faiss.StandardGpuResources() gpu_index = faiss.index_cpu_to_gpu(res, 0, faiss.IndexFlatL2(dimension)) start_time = time.time() gpu_index.add(database) gpu_add_time = time.time() - start_time start_time = time.time() gpu_distances, gpu_indices = gpu_index.search(queries, k) gpu_search_time = time.time() - start_time # 结果对比 print(f"{'指标':<15} {'CPU':<12} {'GPU':<12} {'加速比':<10}") print("-" * 50) print(f"{'添加时间(s)':<15} {cpu_add_time:<12.4f} {gpu_add_time:<12.4f} {cpu_add_time/gpu_add_time:.2f}x") print(f"{'搜索时间(s)':<15} {cpu_search_time:<12.4f} {gpu_search_time:<12.4f} {cpu_search_time/gpu_search_time:.2f}x") # 修正 QPS 加速比的计算,应该用 GPU QPS 除以 CPU QPS print(f"{'QPS':<15} {n_queries/cpu_search_time:<12.1f} {n_queries/gpu_search_time:<12.1f} {(n_queries/gpu_search_time)/(n_queries/cpu_search_time):.2f}x") # 验证结果一致性 accuracy = np.mean(cpu_indices == gpu_indices) print(f"\n结果一致性: {accuracy:.4f}") benchmark_flat_l2()
IndexIVFFlat
IndexIVFFlat 是一种基于倒排文件系统(Inverted File System)的索引类型,它将向量空间划分为多个Voronoi 单元,并使用**乘积量化(Product Quantization)**对每个单元进行量化;具体来说,其搜索分为两个阶段:
CPU:顺序处理聚类,GPU:并行处理多个聚类、同时搜索多个查询
使用场景:大规模数据集、平衡精度和速度的场景、需要可调节精度、速度权衡的应用
示例代码如下:
def benchmark_ivf_flat(): dimension = 768 n_vectors = 500000 n_queries = 1000 k = 10 nlist = 100 # 聚类数量 np.random.seed(42) database = np.random.random((n_vectors, dimension)).astype('float32') queries = np.random.random((n_queries, dimension)).astype('float32') # 测试不同 nprobe 参数 nprobe_values = [1, 10, 50] for nprobe in nprobe_values: print(f"\n测试 nprobe = {nprobe}") # CPU 版本 quantizer_cpu = faiss.IndexFlatL2(dimension) cpu_index = faiss.IndexIVFFlat(quantizer_cpu, dimension, nlist) cpu_index.train(database) cpu_index.add(database) cpu_index.nprobe = nprobe start_time = time.time() cpu_distances, cpu_indices = cpu_index.search(queries, k) cpu_search_time = time.time() - start_time # GPU 版本 res = faiss.StandardGpuResources() quantizer_gpu = faiss.index_cpu_to_gpu(res, 0, faiss.IndexFlatL2(dimension)) gpu_index = faiss.GpuIndexIVFFlat(res, dimension, nlist, faiss.METRIC_L2) gpu_index.train(database) gpu_index.add(database) gpu_index.nprobe = nprobe start_time = time.time() gpu_distances, gpu_indices = gpu_index.search(queries, k) gpu_search_time = time.time() - start_time # 结果对比 print(f"{'指标':<15} {'CPU':<12} {'GPU':<12} {'加速比':<10}") print("-" * 50) print(f"{'搜索时间(s)':<15} {cpu_search_time:<12.4f} {gpu_search_time:<12.4f} {cpu_search_time/gpu_search_time:.2f}x") print(f"{'QPS':<15} {n_queries/cpu_search_time:<12.1f} {n_queries/gpu_search_time:<12.1f} {(n_queries/gpu_search_time)/(n_queries/cpu_search_time):.2f}x") benchmark_ivf_flat()
再次强调,Faiss Chapter中的除了GPU的部分外,其他大部分的技术细节或者原理都在Milvus章节有详细的介绍,例如此处的量化原理。
量化索引是Faiss中处理大规模数据的关键技术,它通过向量压缩技术显著减少内存占用,从而支持十亿级别向量的搜索。量化索引的核心思想是将高维向量空间划分为多个子空间,并对每个子空间进行聚类量化,用压缩编码表示原始向量。Faiss GPU支持多种量化索引,其中最常用的是IVF-PQ(Inverted File System with Product Quantization)索引。
IVF-PQ索引结合了两种压缩技术:倒排文件系统(IVF)和乘积量化(PQ)。IVF通过聚类将向量空间划分为多个 Voronoi 单元,搜索时只需查询少数几个相关单元,大幅减少计算量。PQ则将高维向量分解为多个子向量,并对每个子向量进行独立量化,进一步压缩向量表示。这两种技术结合使IVF-PQ能在保持较高搜索准确率的同时,大幅提升搜索速度和减少内存占用。
使用场景:超大规模数据集、内存受限、需要内存效率的移动端或者边缘计算。
以下是IVF-PQ索引的创建和使用示例:
def benchmark_ivf_pq(): dimension = 768 n_vectors = 1000000 # 1M 向量 n_queries = 1000 k = 10 nlist = 100 m = 8 # 子量化器数量 bits = 8 # 每个量化器的比特数 # 生成更大的数据集 np.random.seed(42) database = np.random.random((n_vectors, dimension)).astype('float32') queries = np.random.random((n_queries, dimension)).astype('float32') print(f"数据集: {n_vectors} 个 {dimension} 维向量") print(f"PQ 配置: {m} 个子量化器, {bits} 比特") # CPU 版本 quantizer_cpu = faiss.IndexFlatL2(dimension) cpu_index = faiss.IndexIVFPQ(quantizer_cpu, dimension, nlist, m, bits) start_time = time.time() cpu_index.train(database) cpu_train_time = time.time() - start_time start_time = time.time() cpu_index.add(database) cpu_add_time = time.time() - start_time cpu_index.nprobe = 10 start_time = time.time() cpu_distances, cpu_indices = cpu_index.search(queries, k) cpu_search_time = time.time() - start_time # GPU 版本 res = faiss.StandardGpuResources() # 注意: GPU IVFPQ 需要特定的配置 config = faiss.GpuIndexIVFPQConfig() config.device = 0 quantizer_gpu = faiss.GpuIndexFlatL2(res, dimension) gpu_index = faiss.GpuIndexIVFPQ(res, dimension, nlist, m, bits, faiss.METRIC_L2, config) start_time = time.time() gpu_index.train(database) gpu_train_time = time.time() - start_time start_time = time.time() gpu_index.add(database) gpu_add_time = time.time() - start_time gpu_index.nprobe = 10 start_time = time.time() gpu_distances, gpu_indices = gpu_index.search(queries, k) gpu_search_time = time.time() - start_time # 内存使用估算:修正原代码中获取向量大小的方法,避免 AttributeError # 对于 IndexIVFPQ,我们可以通过计算大致估算内存使用 # 这里估算每个向量的存储大小为 m * (bits/8) 字节 cpu_memory = (n_vectors * m * (bits / 8)) / 1024**2 print(f"\n内存使用估算: CPU ~{cpu_memory:.1f} MB") # 结果对比 print(f"{'指标':<15} {'CPU':<12} {'GPU':<12} {'加速比':<10}") print("-" * 50) print(f"{'训练时间(s)':<15} {cpu_train_time:<12.4f} {gpu_train_time:<12.4f} {cpu_train_time/gpu_train_time:.2f}x") print(f"{'添加时间(s)':<15} {cpu_add_time:<12.4f} {gpu_add_time:<12.4f} {cpu_add_time/gpu_add_time:.2f}x") print(f"{'搜索时间(s)':<15} {cpu_search_time:<12.4f} {gpu_search_time:<12.4f} {cpu_search_time/gpu_search_time:.2f}x") # 修正 QPS 的加速比计算逻辑,应该是 GPU_QPS / CPU_QPS cpu_qps = n_queries / cpu_search_time gpu_qps = n_queries / gpu_search_time print(f"{'QPS':<15} {cpu_qps:<12.1f} {gpu_qps:<12.1f} {gpu_qps/cpu_qps:.2f}x") benchmark_ivf_pq()
除了IVF-PQ,Faiss还支持其他量化方法,如标量量化(Scalar Quantization)和残差量化(Residual Quantization)。近年来,Faiss还引入了RaBitQ技术,这是一种基于优化的量化方法,能提供更好的准确率-压缩率平衡。RaBitQ在传统乘积量化的基础上,进一步优化了编码和距离计算方式,提升了检索的准确率和速度。
Faiss对GPU上的标量量化支持有限,很多标量量化类型在GPU上没有原生实现,可以通过index_cpu_to_gpu()方法将CPU标量量化索引转移到GPU上,但性能提升有限,对于生产环境,建议使用IVFFlat、IVFPQ等GPU支持更好的索引类型。
下面将使用IndexScalarQuantizer使用标量量化:
def benchmark_scalar_quantizer(): dimension = 256 n_vectors = 100000 n_queries = 1000 k = 10 np.random.seed(42) database = np.random.random((n_vectors, dimension)).astype('float32') queries = np.random.random((n_queries, dimension)).astype('float32') quantizer_types = [ (faiss.ScalarQuantizer.QT_8bit, "8-bit"), (faiss.ScalarQuantizer.QT_6bit, "6-bit"), (faiss.ScalarQuantizer.QT_4bit, "4-bit"), ] results = [] for qtype, qname in quantizer_types: print(f"\n测试 {qname} 量化:") try: # CPU 版本 nlist = 100 quantizer = faiss.IndexFlatL2(dimension) cpu_index = faiss.IndexIVFScalarQuantizer(quantizer, dimension, nlist, qtype, faiss.METRIC_L2) # 训练索引 cpu_index.train(database) start_time = time.time() cpu_index.add(database) cpu_add_time = time.time() - start_time cpu_index.nprobe = 10 start_time = time.time() cpu_distances, cpu_indices = cpu_index.search(queries, k) cpu_search_time = time.time() - start_time # 内存使用估算 if hasattr(cpu_index, 'invlists') and hasattr(cpu_index.invlists, 'vector_size'): memory_usage = (cpu_index.invlists.vector_size * cpu_index.invlists.size) / 1024**2 else: # 简单估算 if qtype == faiss.ScalarQuantizer.QT_8bit: memory_usage = n_vectors * dimension * 1 / 1024**2 elif qtype == faiss.ScalarQuantizer.QT_6bit: memory_usage = n_vectors * dimension * 0.75 / 1024**2 else: # 4bit memory_usage = n_vectors * dimension * 0.5 / 1024**2 # 与原始浮点数的内存对比 original_memory = n_vectors * dimension * 4 / 1024**2 # float32 compression_ratio = original_memory / memory_usage results.append({ 'name': qname, 'search_time': cpu_search_time, 'add_time': cpu_add_time, 'qps': n_queries / cpu_search_time, 'memory_usage': memory_usage, 'compression_ratio': compression_ratio }) print(f"搜索时间: {cpu_search_time:.4f}s, QPS: {n_queries/cpu_search_time:.1f}") print(f"内存使用: {memory_usage:.1f} MB, 压缩比: {compression_ratio:.1f}x") except Exception as e: print(f"创建 {qname} 量化索引失败: {e}") continue print(f"\n对比基准 - FlatL2:") cpu_index_flat = faiss.IndexFlatL2(dimension) start_time = time.time() cpu_index_flat.add(database) flat_add_time = time.time() - start_time start_time = time.time() flat_distances, flat_indices = cpu_index_flat.search(queries, k) flat_search_time = time.time() - start_time flat_memory = n_vectors * dimension * 4 / 1024**2 results.append({ 'name': 'FlatL2', 'search_time': flat_search_time, 'add_time': flat_add_time, 'qps': n_queries / flat_search_time, 'memory_usage': flat_memory, 'compression_ratio': 1.0 }) print(f"搜索时间: {flat_search_time:.4f}s, QPS: {n_queries/flat_search_time:.1f}") print(f"内存使用: {flat_memory:.1f} MB") # 输出对比结果 print(f"\n{'量化类型':<10} {'搜索时间(s)':<12} {'QPS':<10} {'内存(MB)':<12} {'压缩比':<10}") print("-" * 65) for result in results: print(f"{result['name']:<10} {result['search_time']:<12.4f} {result['qps']:<10.1f} {result['memory_usage']:<12.1f} {result['compression_ratio']:<10.1f}") benchmark_scalar_quantizer()
下面我们尝试转移到GPU上,看一下查询速度的提升
def benchmark_gpu_scalar_quantizer(): print("\n" + "=" * 60) print("GPU 标量量化尝试") print("=" * 60) dimension = 128 n_vectors = 50000 n_queries = 500 np.random.seed(42) database = np.random.random((n_vectors, dimension)).astype('float32') queries = np.random.random((n_queries, dimension)).astype('float32') try: # 在CPU上创建标量量化索引 nlist = 50 quantizer_cpu = faiss.IndexFlatL2(dimension) cpu_index = faiss.IndexIVFScalarQuantizer(quantizer_cpu, dimension, nlist, faiss.ScalarQuantizer.QT_8bit, faiss.METRIC_L2) cpu_index.train(database) cpu_index.add(database) cpu_index.nprobe = 10 # 测试CPU性能 start_time = time.time() cpu_distances, cpu_indices = cpu_index.search(queries, 10) cpu_time = time.time() - start_time # 尝试转移到GPU res = faiss.StandardGpuResources() gpu_index = faiss.index_cpu_to_gpu(res, 0, cpu_index) # 测试GPU性能 start_time = time.time() gpu_distances, gpu_indices = gpu_index.search(queries, 10) gpu_time = time.time() - start_time print(f"CPU 搜索时间: {cpu_time:.4f}s") print(f"GPU 搜索时间: {gpu_time:.4f}s") print(f"加速比: {cpu_time/gpu_time:.2f}x") except Exception as e: print(f"GPU标量量化测试失败: {e}") print("Faiss对GPU标量量化的直接支持有限,建议使用IVFFlat或IVFPQ") benchmark_gpu_scalar_quantizer()
层次可导航小世界(HNSW)索引是基于图结构的近似最近邻搜索算法,它通过构建一个多层次的图结构来加速搜索过程。HNSW索引结合了可导航小世界(NSW)图和跳表的思想,能够在高维空间中快速找到近似最近邻。Faiss GPU貌似不支持HNSW索引。
HNSW索引在Faiss中是通过IndexHNSW类实现的,属于CPU-only的索引类型,可以尝试使用faiss.index_cpu_to_gpu(res, 0, cpu_index)将HNSW索引转移到GPU上,但由于底层没有对应的GPU kernel,所以会失败。
HNSW索引的主要优点包括:高搜索速度、高准确率和无需训练。与量化索引不同,HNSW不需要单独的训练阶段,可以直接添加数据并构建索引。HNSW通过启发式方法构建图结构,确保图中节点具有良好的连接性,使得搜索过程可以快速收敛到最近邻区域。HNSW索引的性能主要通过efConstruction(控制图构建质量)和efSearch(控制搜索深度)参数调节。
使用场景:高召回率要求、动态数据集、支持增量添加、中等规模的高维数据。
先执行以下代码,检查当前Faiss版本是否支持GPU原生HNSW索引
def benchmark_hnsw_gpu_native(): """测试原生 GPU HNSW 支持""" dimension = 128 n_vectors = 50000 n_queries = 1000 k = 10 np.random.seed(42) database = np.random.random((n_vectors, dimension)).astype('float32') queries = np.random.random((n_queries, dimension)).astype('float32') # 参数设置 M = 16 # 每个节点的连接数 efConstruction = 200 efSearch = 100 try: # 尝试创建原生 GPU HNSW res = faiss.StandardGpuResources() config = faiss.GpuIndexHNSWFlatConfig() config.device = 0 config.efConstruction = efConstruction config.efSearch = efSearch gpu_index = faiss.GpuIndexHNSWFlat(res, dimension, M, config) print("使用原生 GpuIndexHNSWFlat...") gpu_index.add(database) start_time = time.time() gpu_distances, gpu_indices = gpu_index.search(queries, k) gpu_time = time.time() - start_time print(f"GPU HNSW 搜索时间: {gpu_time:.4f}s") except AttributeError: print("当前 Faiss 版本不支持 GpuIndexHNSWFlat") return None return gpu_time benchmark_hnsw_gpu_native()
| 索引类型 | CPU优势 | GPU优势 | 内存效率 | 精度 |
|---|---|---|---|---|
| IndexFlatL2 | 小数据集,精确搜索 | 5 - 50x加速,大数据集 | 低 | 100% |
| IndexIVFFlat | 可调节精度 | 10 - 100x加速,并行聚类 | 中 | 95 - 99% |
| IndexIVFPQ | 内存敏感场景 | 5 - 20x加速,批量量化 | 高 | 90 - 98% |
| IndexHNSW | 高召回率 | 1 - 3x加速,有限并行 | 中 | 98 - 99% |
| IndexScalarQuantizer | 极致内存优化 | 3 - 10x加速,并行解压 | 极高 | 85 - 95% |
通过运行上文的代码,你可以发现:
要充分利用Faiss GPU的强大功能,需要了解其完整的工作流程。本节将通过一个实际的例子,展示从数据准备到结果分析的全过程。我们假设场景是一个图像检索系统,需要从大量图片中快速找到视觉上相似的图片。这个例子虽然简化了实际应用的复杂性,但涵盖了Faiss GPU的核心使用模式。
首先,我们需要准备数据和环境。在实际应用中,图像通常通过深度学习模型(如CNN)提取为特征向量。为了简化,我们使用随机生成的模拟数据,但处理流程与真实场景一致。完整的示例代码如下:
import numpy as np import faiss import time print("Faiss版本:", faiss.__version__) # 初始化GPU资源 res = faiss.StandardGpuResources() res.setTempMemory(512 * 1024 * 1024) # 设置临时内存为512MB res.setPinnedMemory(256 * 1024 * 1024) # 设置固定内存为256MB,提升数据传输效率 # 参数配置 d = 512 # 向量维度(通常对应特征提取器的输出维度,如ResNet-50为2048) nb = 100000 # 数据库大小 nq = 1000 # 查询数量 k = 10 # 返回的最近邻数量 print(f"生成随机数据: 维度={d}, 数据库大小={nb}, 查询数量={nq}") # 生成随机数据模拟特征向量 np.random.seed(1234) xb = np.random.random((nb, d)).astype('float32') xq = np.random.random((nq, d)).astype('float32') # 数据归一化(重要步骤,特别是使用点积或余弦相似度时) faiss.normalize_L2(xb) faiss.normalize_L2(xq) print("数据准备完成,开始构建索引...") # 创建量化器 nlist = 1024 # 聚类中心数量 quantizer = faiss.IndexFlatIP(d) # 使用点积作为距离度量 # 创建IVF-PQ索引 m = 16 # 子量化器数量(必须是维度d的因数) bits = 8 # 每个子量化器的比特数 index_cpu = faiss.IndexIVFPQ(quantizer, d, nlist, m, bits) # 训练索引 print("开始训练索引...") start_time = time.time() index_cpu.train(xb) end_time = time.time() print(f"索引训练完成,耗时: {end_time - start_time:.2f}秒") # 将索引转移到GPU print("将索引转移到GPU...") gpu_index = faiss.index_cpu_to_gpu(res, 0, index_cpu) # 添加数据到索引 print("添加数据到索引...") start_time = time.time() gpu_index.add(xb) end_time = time.time() print(f"数据添加完成,耗时: {end_time - start_time:.2f}秒") print(f"索引中的向量总数: {gpu_index.ntotal}") # 设置搜索参数 gpu_index.nprobe = 32 # 搜索的聚类中心数量,平衡速度与精度 # 执行搜索 print("开始搜索...") start_time = time.time() D, I = gpu_index.search(xq, k) end_time = time.time() print(f"搜索完成,总耗时: {end_time - start_time:.2f}秒") print(f"搜索速度: {nq / (end_time - start_time):.2f} QPS (每秒查询数)") # 分析搜索结果 print("\n搜索结果分析:") print("第一个查询的前5个最近邻索引:", I[0][:5]) print("第一个查询的前5个最近邻距离:", D[0][:5]) # 验证搜索质量 - 检查第一个查询的第一个结果是否正确 # 由于我们使用的是随机数据,这里主要检查程序是否正常运行 query_vector = xq[0] nearest_index = I[0][0] nearest_distance = D[0][0] print(f"\n查询向量0 -> 最近邻索引{nearest_index}, 距离{nearest_distance}") # 保存索引(可选) # faiss.write_index(faiss.index_gpu_to_cpu(gpu_index), "gpu_trained.index")
在实际应用中使用Faiss GPU时,性能优化至关重要。通过合理的参数配置和系统优化,可以显著提升搜索速度和准确率。以下是一些关键的优化策略和对应的代码示例:
调整nprobe参数:nprobe控制搜索时访问的聚类中心数量,对搜索性能和精度有重大影响。增加nprobe会提高搜索质量但降低速度,需要根据实际需求平衡。
# 测试不同nprobe值对性能的影响 nprobe_values = [8, 16, 32, 64, 128] query_vectors = xq[:100] # 使用100个查询向量测试 for nprobe in nprobe_values: gpu_index.nprobe = nprobe start_time = time.time() D, I = gpu_index.search(query_vectors, k) end_time = time.time() qps = 100 / (end_time - start_time) print(f"nprobe={nprobe}: {qps:.2f} QPS")
多GPU并行处理:对于超大规模数据,可以使用多GPU加速搜索过程。Faiss提供了完善的多GPU支持,可以透明地分布索引和计算任务。
# 多GPU配置示例 gpu_list = [0, 1, 2, 3] # 使用的GPU设备列表 # 方法1: 索引复制(每个GPU保存完整索引副本) # 适用于搜索负载高的场景 gpu_indices = [] for gpu_id in gpu_list: res = faiss.StandardGpuResources() gpu_index = faiss.index_cpu_to_gpu(res, gpu_id, index_cpu) gpu_indices.append(gpu_index) # 创建多GPU索引 multi_gpu_index = faiss.IndexProxy() for index in gpu_indices: multi_gpu_index.addIndex(index) # 方法2: 索引分片(每个GPU保存部分索引) # 适用于索引过大或需要极高吞吐量的场景 cpu_index_shard = faiss.IndexShards(d) gpu_resources = [] for i, gpu_id in enumerate(gpu_list): res = faiss.StandardGpuResources() gpu_resources.append(res) # 将数据分片到不同GPU shard = faiss.index_cpu_to_gpu(res, gpu_id, index_cpu) cpu_index_shard.add_shard(shard) # 使用分片索引搜索 cpu_index_shard.add(xb) # 数据会自动分布到各个分片 D, I = cpu_index_shard.search(xq, k)
Faiss GPU的性能很大程度上取决于显存的使用效率。Faiss通过双层次内存池架构管理显存,核心组件位于faiss/gpu/StandardGpuResources.h。这个架构包含设备内存栈(StackDeviceMemory)和资源管理器(StandardGpuResources)两个关键层级。
设备内存栈采用"预分配-复用"策略,在初始化时根据GPU显存总量自动调整预分配大小(通常不超过1.5GiB),并通过allocMemory()和deallocMemory()实现内存块复用,同时确保16字节对齐的内存访问效率。资源管理器则负责管理全生命周期显存,包括临时内存池(用于搜索/训练过程中的中间变量)和固定内存池(用于CPU-GPU异步数据传输)。
优化显存使用的具体策略包括:
精准配置临时内存池:默认1.5GiB临时内存可能导致小显存GPU资源浪费或大显存GPU利用不足。应根据业务需求和GPU容量动态调整:
import faiss res = faiss.StandardGpuResources() # 为16GiB GPU配置4GiB临时内存 res.setTempMemory(4 * 1024 * 1024 * 1024) # 4GB # 最佳实践建议: # - 显存≤8GiB GPU:设置为总显存的30% # - 显存>16GiB GPU:设置为总显存的20% # - 纯检索场景:可降低至10%
启用固定内存传输:传统分页内存传输存在额外拷贝开销,固定内存可提升传输效率30%+,特别适合大规模向量批量导入和频繁的CPU-GPU数据交互。
# 分配2GB固定内存用于CPU-GPU数据传输 res.setPinnedMemory(2 * 1024 * 1024 * 1024)
监控与诊断:Faiss提供内存监控接口,帮助识别显存问题:
# 导出各设备内存使用详情 mem_info = res.getMemoryInfo() for device, mem in mem_info.items(): print(f"Device {device}:") for type_, (count, size) in mem.items(): print(f" {type_}: {count} allocations, {size/1e6:.2f} MB") # 启用内存分配日志检测内存泄漏 res.setLogMemoryAllocations(True)
Faiss支持多GPU并行处理,能够显著提升吞吐量并解决单卡显存不足的问题。多GPU并行主要有两种模式:数据并行和模型并行。数据并行指每个GPU存储完整的索引副本,查询请求被分发到不同GPU并行处理;模型并行指索引被分片存储在不同GPU上,单个查询需要聚合多个GPU的结果。
以下是多GPU并行的实现示例:
import faiss import concurrent.futures # 多GPU数据并行配置 def setup_multi_gpu_data_parallel(gpu_list): resources = [] indexes = [] for gpu_id in gpu_list: res = faiss.StandardGpuResources() # 优化每个GPU的临时内存配置 res.setTempMemory(1024 * 1024 * 1024) # 1GB resources.append(res) # 创建CPU索引 cpu_index = faiss.IndexIVFPQ( faiss.IndexFlatL2(d), d, nlist, m, bits ) cpu_index.train(xb) # 转换为GPU索引 gpu_index = faiss.index_cpu_to_gpu(res, gpu_id, cpu_index) gpu_index.add(xb) indexes.append(gpu_index) # 创建索引代理,透明地分发查询到多个GPU index_proxy = faiss.IndexProxy() for index in indexes: index_proxy.addIndex(index) return index_proxy, resources # 使用多GPU索引搜索 gpu_list = [0, 1, 2, 3] multi_gpu_index, resources = setup_multi_gpu_data_parallel(gpu_list) # 搜索会自动并行化到所有GPU D, I = multi_gpu_index.search(xq, k)
对于超大规模数据,可以采用分片索引策略,将数据分布到多个GPU:
# 分片索引示例 def setup_sharded_index(gpu_list, dim, nlist, m, bits): index = faiss.IndexShards(dim, True) # True表示使用连续ID for i, gpu_id in enumerate(gpu_list): res = faiss.StandardGpuResources() # 每个GPU构建自己的索引 quantizer = faiss.IndexFlatL2(dim) cpu_index = faiss.IndexIVFPQ(quantizer, dim, nlist, m, bits) gpu_index = faiss.index_cpu_to_gpu(res, gpu_id, cpu_index) index.add_shard(gpu_index) return index # 训练分片索引需要特殊处理 sharded_index = setup_sharded_index([0, 1], d, nlist, m, bits) # 分片索引的训练 - 需要统一训练数据 train_data = xb # 所有训练数据 sharded_index.train(train_data) # 添加数据时会自动分配到各个分片 sharded_index.add(xb)
索引选择与参数调优:根据数据特性和需求选择合适的索引类型和参数是性能优化的关键。以下是指数选择的指导原则:
批处理与流水线:通过批处理查询和重叠CPU-GPU数据传输与计算,可以显著提升吞吐量:
GPU通常比CPU在搜索方面有着显著的加速,但有两个问题需要注意:
将已填充好数据的索引从CPU复制到GPU可能会花费大量时间。只有当需要在GPU上执行较多数量的查询时,复制索引带来的时间开销才能被分摊,从而变得划算。因此,最佳做法是将索引一次性放在GPU上,或者直接在GPU上创建索引并填充数据。
如果CPU上有大量的查询向量,将这些向量复制到GPU也可能需要一些时间。不过,这种复制开销通常只有在索引较小时才会成为影响性能的问题。因为当索引较小时,它可以存放在CPU的最后一级缓存中(容量大约只有几MB),此时数据在CPU内处理会更快,将数据复制到GPU反而会增加额外的传输开销。
总结一下:
GPU索引实现与CPU相同的算法,但不一定会返回完全相同的结果。需要记住以下三件重要的事情:
浮点数约简顺序
浮点数运算默认不满足结合律。GPU代码中的运算顺序可能与CPU版本有很大差异,因此最终报告的距离结果或返回元素的顺序可能与CPU报告的有所不同。即使在最简单的CPU IndexFlatIP 与GPU GpuIndexFlatIP 情况下,由于各自矩阵乘法内核的浮点数约简差异,也可能报告不同的结果。
等价元素的Top-K选择顺序
在GPU上扫描数据并进行Top-K选择(k-select)时,得到结果的顺序不一定和在CPU上操作时相同。当存在等效值(例如索引中的向量或者搜索返回的距离值相同)的情况,这些等效元素之间的相对顺序无法保证,这就类似于排序算法中的不稳定性(见附录1.排序算法的不稳定性)。
例如,一个索引可能有1000个重复向量,每个向量都有不同的用户ID。如果其中一些向量在min-k(L2距离)或max-k(内积)范围内,GPU返回的ID可能与CPU返回的ID不同。
GpuIndexFlat),那么距离计算也会有所不同。为了比较CPU和GPU结果的等效性,可能应该使用召回率(recall @ N)框架来确定CPU和GPU结果之间的重叠程度。对于GPU和CPU返回的具有相同ID的结果,其距离值应该在某个合理的误差范围(epsilon)内,(比如,最后一位的1 - 500个单位)(见附录2.ULP)
Faiss作为一个活跃的开源项目,持续在相似性搜索领域创新。近期发布的Faiss v1.11.0版本引入了多项重要改进,其中最引人注目的是RaBitQ模块的实现。RaBitQ是在传统乘积量化(PQ)基础上的创新,进一步优化了编码和距离计算方式,提升了检索的准确率和速度。RaBitQ已集成到Swig绑定的Python接口,用户可以通过Python方便地访问和操作RaBitQ索引属性。
另一个重要改进是内存映射与零拷贝机制的优化。Faiss v1.11.0正式回归并改进了内存映射(mmap)和零拷贝的反序列化机制,使得用户能够快速加载大规模索引文件,降低启动时延和内存占用。零拷贝技术还优化了Python绑定,避免不必要的内存复制,带来整体性能的明显提升。
训练API也得到显著增强,新增了is_spherical和normalize_L2两个布尔参数,支持训练时是否将向量单独归一化到球面空间,提升了训练的灵活性。分布式训练API中也支持了normalize_l2参数,更便于大规模集群上的高效训练。此外,Faiss现在原生支持余弦距离计算,增强了对不同相似度度量的泛用性。
在GPU支持方面,Faiss持续优化多平台兼容性。新增了MinGW工具链编译支持,为Windows用户提供了除MSVC外的更多选择。GPU资源管理、kernel实现等细节也得到修复和优化,确保在不同架构下的稳定运行。openBLAS已升级到0.3.29版本,全面兼容ARM架构,强化了Faiss在多种硬件上的适用性。
Faiss GPU是一个功能强大、性能优异的相似性搜索库,能够在十亿级别向量数据集上实现毫秒级的搜索响应。通过本报告的详细介绍,相信读者已经对Faiss GPU的基本概念、安装配置、索引类型、使用方法和优化技巧有了全面了解。
对于初学者,建议从以下步骤开始Faiss GPU之旅:
对于生产环境部署,建议遵循以下最佳实践:
在实际应用中,选择合适的索引需要综合考虑数据规模、查询延迟要求、精度要求和硬件资源等因素:
参考:non-guarantee of stability for a sort
在排序算法里,稳定排序是指相等元素在排序前后的相对顺序保持不变。举个例子,当我们对扑克牌按点数排序时,如果有两张5点的牌,在排序完成后的结果里,它们还会保持和原始输入一样的先后顺序,这种排序方式就是稳定排序;而不稳定排序则可能会改变它们之间的先后顺序。
咱们可以把要排序的数据想象成一个个记录或者元组,排序时依据的那部分数据就叫做键。比如扑克牌,每张牌可以用(点数,花色)这样的记录来表示,排序时若以点数为键,稳定排序就会保证点数相同的牌维持原有的顺序。
在实际应用中,如果需要对同一批数据进行多次排序,还想保留某些顺序关系,排序的稳定性就很关键了。比如有一份学生记录,包含姓名和班级信息。我们先按姓名排序,再按班级排序,如果两次都用稳定排序,那么按班级排序后,学生姓名依然会保持字母顺序;但要是用不稳定排序,按班级排序后,学生姓名可能就不再按字母顺序排列了。
不过,当元素完全一样,比如对整数排序,或者数据的全部内容就是排序的键时,排序是否稳定就没什么影响。另外,如果所有键值都不相同,排序的稳定性也无关紧要。
不稳定的排序算法也能通过特殊方式实现稳定。比如在比较两个键值相等的对象时,借助它们在原始输入列表里的顺序来决定先后。但这样做可能需要额外的时间和空间来记录顺序。
在FAISS里,由于GPU和CPU的计算机制不一样,处理等值元素时和不稳定排序类似,不会保证这些元素在不同设备上的相对顺序一致。
参考:units in the last place
units in the last place(最后一位单位)来自维基百科,解释的是一个衡量浮点数计算误差的精密标准。
简单来说:
一个直观的例子:
假设我们使用一种非常简单的十进制浮点数系统,只能表示 1.00, 1.01, 1.02, ... 那么:
1.00,下一个可表示的数字是 1.01。1.00 附近,1个ULP就等于 0.01。1.005,但系统只能给出 1.00 或 1.01,那么无论选择哪一个,误差都是 0.005,也就是 0.5个ULP。在真实的二进制计算机中,原理完全相同,只是基于二进制。ULP的大小会随着浮点数本身的大小而变化(类似科学计数法),它是一个相对误差的度量。
现在,我们把它放回到上文提供的FAISS(GPU vs CPU)的上下文中。文中提到:
- float选择加入
如果使用float16v版本的GPU算法(例如,对于GpuIndexFlat),那么距离计算也会有所不同。
为了比较CPU和GPU的等效性,可能应该使用召回框架来确定CPU和GPU结果之间的重叠程度,对于GPU和CPU之间的具有相同ID的结果,距离应该在某个合理的epsilon范围内,(比如,最后的1-500? 个单位)
这里使用“ULP”这个概念,有以下几个重要意义:
提供了一个科学、严谨的误差衡量标准
如果只是简单地说“误差应该在0.0001以内”,这是不科学的。因为对于非常大或非常小的浮点数,0.0001这个绝对误差可能显得过于苛刻或者过于宽松。而ULP是一个与数值本身量级相关的相对误差,用它来衡量由不同计算顺序(GPU vs CPU)带来的浮点误差是非常合适的。
解释了为什么结果会有微小差异
上文提到了三个主要原因(计算顺序、k-选择顺序、float16),这些都会导致GPU和CPU在计算距离(如内积或L2距离)时,产生微小的浮点数差异。这些差异的本质,就是最终结果在浮点数数轴上“跳动”了几个相邻的位置。这个“跳动的步数”,就是ULP。
给出了一个合理的可接受范围
文中建议的 1-500 ULP 是一个非常关键的实践指导。
这个ULP的概念告诉你,当FAISS文档说GPU和CPU的结果可能不完全相同时,它们指的差异是一种符合浮点数计算规律的、微小的、可以用“ULP”这种专业单位来量化的差异。而不是一个随机的、巨大的Bug。
所以,在验证你的GPU和CPU索引是否“等效”时,你不应该期望它们的距离值完全一致 (a == b),而应该:
Faiss官方文档!
该部分将引导完成一个基于Faiss的简单问答系统,可以通过构建不同的索引和选择不同CPU \ GPU配置来看到性能差异。