# Faiss

**Faiss**（Facebook AI Similarity Search）是一个开源 C++ 库，配有完整的 Python 封装，用于高效地对稠密向量进行相似度搜索与聚类，包括大到无法装入内存的向量集。<sup>[1](https://github.com/facebookresearch/faiss/)</sup> 它主要由 Meta 的基础人工智能研究（FAIR）团队开发，采用 MIT 许可证。<sup>[1](https://github.com/facebookresearch/faiss/)</sup> 按照 Faiss 论文的描述，它是一套索引方法及相关原语的工具包，用于搜索、聚类、压缩和变换向量，也是一个近似最近邻搜索（ANNS）库——这是向量数据库的核心功能。<sup>[2](https://arxiv.org/html/2401.08281v4)</sup> Meta 将其描述为让开发者能够快速搜索多媒体文档的相似嵌入。<sup>[3](https://ai.meta.com/tools/faiss/)</sup>

埃尔韦·热古（Hervé Jégou）发起了该项目并编写了第一个实现；马蒂亚斯·杜兹（Matthijs Douze）实现了大部分 CPU 版 Faiss；杰夫·约翰逊（Jeff Johnson）实现了全部 GPU 版 Faiss。<sup>[1](https://github.com/facebookresearch/faiss/)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | MIT 许可的 C++/Python 库，用于稠密向量的相似度搜索与聚类，由 Meta FAIR 开发<sup>[1](https://github.com/facebookresearch/faiss/)</sup> |
| 核心操作 | 在维度为 \(d\) 的向量 \(x_i\) 上构建内存数据结构，通过欧几里得距离（argmin）或最大内积（argmax）寻找最近邻<sup>[4](https://faiss.ai/)</sup> |
| 索引族 | 约十几种类型：Flat（精确）、IVF、HNSW、PQ/OPQ、IVF-PQ、IVFPQFastScan、RaBitQ<sup>[2](https://arxiv.org/html/2401.08281v4)</sup><sup> • </sup><sup>[5](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index)</sup> |
| GPU 速度 | 单块 GPU 上比对应的 CPU 实现快 5–10 倍；通过复制在 8 块 GPU 上获得 6–7 倍加速<sup>[6](https://github.com/facebookresearch/faiss/wiki/Faiss-on-the-GPU)</sup> |
| 十亿级规模 | 在 SIFT1B 上，单块 GPU、每向量 8 字节，每次查询 17.7 微秒达到 R@10 = 0.376，比对比实现快 8.5 倍且更准确<sup>[7](https://arxiv.org/html/1702.08734v1)</sup> |
| 采用情况 | 自 2017 年开源发布以来，GitHub 星标 3.7 万、软件包下载量 600 万次、GPU 论文被引用超过 5200 次；Zilliz 和 Pinecone 依赖 Faiss 或重新实现其算法<sup>[2](https://arxiv.org/html/2401.08281v4)</sup> |
| 近期版本 | 从 1.8.0（2024 年 2 月）到 1.15.0：RAFT/cuVS GPU 集成、AVX-512 模式，以及在 Apple Silicon 上默认启用的 Metal 后端<sup>[8](https://github.com/facebookresearch/faiss/blob/main/CHANGELOG.md)</sup><sup> • </sup><sup>[9](https://github.com/facebookresearch/faiss/releases)</sup> |

## 背景：向量相似度搜索

给定维度为 \(d\) 的一组向量 \(x_i\)，Faiss 在内存中构建数据结构，并高效地计算最近邻，即[欧几里得距离](https://www.edgepedia.org/zh/euclidean-distance)的 argmin；它还支持最大内积搜索 \( \arg\max_i \langle x, x_i \rangle \)。<sup>[4](https://faiss.ai/)</sup> 在高维嵌入上进行精确搜索代价高昂，因此 Faiss 的设计宗旨是以精度换速度，例如用快 10 倍或内存用量仅为原来十分之一的方法，有 10% 的概率给出错误结果。<sup>[4](https://faiss.ai/)</sup> 除了 k 近邻和批量搜索，它还支持范围搜索、磁盘索引和二进制向量索引。<sup>[4](https://faiss.ai/)</sup>

Faiss 内部的主要近似策略是倒排文件（IVF）设计：k-means 找到 M 个质心，将空间划分为 M 个部分，存储为倒排文件索引，查询时只检查离它最近的列表。<sup>[10](https://pure.itu.dk/portal/da/publications/20239d14-6dff-4b27-bd57-f85484104308)</sup>

## 索引类型与选择

**Flat。** IndexFlatL2 和 IndexFlatIP 是仅有的保证精确结果的索引，也是其他索引的基线；它们每向量使用 \( 4 \cdot d \) 字节，采用暴力搜索。<sup>[5](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index)</sup><sup> • </sup><sup>[11](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes)</sup> 如果只计划进行少量搜索（1000 到 10000 次），直接计算是最高效的选择，因为索引构建时间永远无法摊销。<sup>[5](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index)</sup>

**HNSW。** 分层可导航小世界方法在索引向量上构建图，并在搜索时探索该图，快速收敛到最近邻。<sup>[11](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes)</sup> Wiki 建议在内存充足或数据集较小时使用 HNSW：每向量 M 条链接，M 在 4 到 64 之间（越高越准确但占用更多内存），每向量内存为 \( d \cdot 4 + M \cdot 2 \cdot 4 \) 字节，无需训练，仅支持顺序添加，不支持删除。<sup>[5](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index)</sup> Faiss 库论文称 HNSW 是中型数据集最流行的索引方法。<sup>[2](https://arxiv.org/html/2401.08281v4)</sup>

**IVF。** 对于 100 万向量以下的数据集，Wiki 建议使用 IVF，质心数 \( K = 4 \cdot \sqrt{N} \) 到 \( 16 \cdot \sqrt{N} \)，需要 \( 30 \cdot K \) 到 \( 256 \cdot K \) 个训练向量；对于 100 万到 1000 万向量，建议使用 IVF65536_HNSW32；1000 万到 1 亿使用 IVF262144_HNSW32；1 亿到 10 亿使用 IVF1048576_HNSW32。<sup>[5](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index)</sup> IndexIVFFlat 每向量额外存储 8 字节用于向量 ID。<sup>[11](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes)</sup>

**压缩：PQ、OPQ、IVF-PQ。** IndexIVFPQ（IVFADC）每向量使用 \( \lceil M \cdot \mathrm{nbits}/8 \rceil + 8 \) 字节，是非穷举的。<sup>[11](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes)</sup> 当存储完整向量过于昂贵时，先做 OPQ 再做 M 个 4 位编码的 PQ，可将存储降至每向量 \( M/2 \) 字节；需要最大压缩时推荐 RaBitQ。<sup>[5](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index)</sup> Faiss 库论文明确划出了规模界线：基于图的索引适合内存不受限、通常低于 100 万向量的情况；超过 1000 万向量后构建时间成为限制；对于需要压缩的更大索引，IVF 索引是唯一选择。<sup>[2](https://arxiv.org/html/2401.08281v4)</sup> 在十亿向量规模下，IVFPQFastScan 在大多数工作点上与 HNSW 性能相当，但调优更难，因为它需要设置两个参数（nprobe 和 k_factor）而非一个（efSearch）；PQ 编码始终能达到最高的精度上限，而 4 位快速扫描 PQ 变体（PQx4fsr）在几乎所有工作点上都优于标量量化。<sup>[12](https://github.com/facebookresearch/faiss/wiki/Indexing-1G-vectors)</sup>

## 实现与硬件

Faiss 的速度来自三个方向。在 CPU 上，1.8.0 版本引入了通过 FAISS_OPT_LEVEL 环境变量选择的 avx512 优化模式。<sup>[8](https://github.com/facebookresearch/faiss/blob/main/CHANGELOG.md)</sup> 在 GPU 上，Faiss 在单块 GPU 上比对应的 CPU 实现快 5 到 10 倍，通过在多个 GPU 上复制索引可获得接近线性的加速（8 块 GPU 时 6 到 7 倍）。<sup>[6](https://github.com/facebookresearch/faiss/wiki/Faiss-on-the-GPU)</sup> GPU 性能在批量查询时最佳，典型批量大小为 2 的幂，约 8192，k 值超过约 512 时性能下降；IndexFlat、IndexIVFFlat、IndexIVFScalarQuantizer 和 IndexIVFPQ 在 GPU 上有实现。<sup>[6](https://github.com/facebookresearch/faiss/wiki/Faiss-on-the-GPU)</sup>

**cuVS 与 CAGRA。** 集成了 NVIDIA 的 cuVS，因此 GpuIndexFlat、GpuIndexIVFFlat 和 GpuIndexIVFPQ 可以使用 cuVS 实现，基于图的 CAGRA 索引（基于图的 CUDA ANN，大致基于可导航小世界图算法）可通过 GpuIndexCagra 使用，在高召回率水平下实现更快搜索。<sup>[13](https://github.com/facebookresearch/faiss/wiki/GPU-Faiss-with-cuVS)</sup> cuVS 允许每个 PQ 码 4 到 8 位，子量化器数量任意，最多可达基础维度，而经典 Faiss GPU 索引仅支持 8 位编码，子量化器数量仅支持若干固定值，最多为 96；cuVS CAGRA 索引可在 GPU 上构建并序列化为 CPU HNSW 索引，但 cuVS 索引不支持多 GPU。<sup>[13](https://github.com/facebookresearch/faiss/wiki/GPU-Faiss-with-cuVS)</sup>

## 数据一览

**SIFT1M（100 万向量）。** HNSW 在每次查询 0.020 毫秒（efSearch 32）时达到 recall@1 0.9492，而 IVF Flat 在每次查询 0.140 毫秒（nprobe 64）时为 0.9470，不过 HNSW 的内存开销更高；带标量量化器的 HNSW 在每次查询 0.011 毫秒时达到 0.9242。<sup>[14](https://github.com/facebookresearch/faiss/wiki/Indexing-1M-vectors)</sup> CPU 上的精确 Flat 搜索对查询集耗时 9.100 秒，recall@1 为 1.0000，索引大小 512 MB；而 Titan X GPU 上的 IVF16384,Flat 耗时 0.059 秒，recall@1 为 0.8145，除原始向量外索引开销仅 8 MB。<sup>[14](https://github.com/facebookresearch/faiss/wiki/Indexing-1M-vectors)</sup>

**GPU 论文。** 在 SIFT1M 上，Titan X 上每次查询 0.02 毫秒的预算下，GPU 实现达到 R@1 = 0.80 和 R@100 = 0.95，而此前的深度学习实现为 R@1 = 0.51 和 R@100 = 0.86。<sup>[7](https://arxiv.org/html/1702.08734v1)</sup> 在 SIFT1B 上，单块 GPU、每向量 m = 8 字节，每次查询 17.7 微秒达到 R@10 = 0.376，对比实现报告为每次查询 150 微秒 R@10 = 0.35，快 8.5 倍且更准确。<sup>[7](https://arxiv.org/html/1702.08734v1)</sup>

**十亿级工作点。** 在 10 亿向量索引上，nprobe = 128、quantizer_efSearch = 32 时，每次查询 0.05387 毫秒达到 recall@1 0.6786（1 万次查询共计算 142,670,158 个距离）；nprobe = 64 时，每次查询 0.03028 毫秒达到 0.6421。<sup>[12](https://github.com/facebookresearch/faiss/wiki/Indexing-1G-vectors)</sup> 压缩设定了硬上限：在 BigANN 1B 上，使用工厂字符串 OPQ16_128,IVF1048576_HNSW32,PQ16（16 字节 PQ，使索引能装入单块 GPU），可达到的最大 recall@1 约为 0.352。<sup>[15](https://github.com/facebookresearch/faiss/wiki/Hybrid-CPU-GPU-search-and-multiple-GPUs)</sup>

## 与其他 ANN 库的比较

与专注于单一索引方法的库不同，Faiss 是链式组件的工具箱（预处理、压缩、非穷举搜索）；其核心是无依赖的 C++，配有全面的 Python 封装，可用于简单脚本或作为数据库管理系统的构建模块。<sup>[2](https://arxiv.org/html/2401.08281v4)</sup> 标准的独立比较是 ANN-Benchmarks，这是一个由[埃里克·伯恩哈德松](https://www.edgepedia.org/zh/erik-bernhardsson)（Erik Bernhardsson）发起的开源项目，他也编写了 Annoy 库，该基准报告了 hnswlib、Faiss（HNSW、IVF、IVF-PQ）、Annoy、ScaNN、Vespa、pgvector、[Milvus](https://www.edgepedia.org/zh/milvus-vector-database)、Qdrant、Weaviate 等数十个库在各数据集和距离度量下的召回率对 k 的关系。<sup>[16](https://ann-benchmarks.com/index.html)</sup>

一个有来源的正面对比来自 Faiss 自己的 wiki：在 SIFT1M 上，nmslib 的 HNSW 比 Faiss 快，但使用明显更多内存（512 + 796 MB 对 IVF16384,Flat 的 512 + 8 MB）；对于 Faiss，构建时间是次线性的，内存使用是线性的。<sup>[14](https://github.com/facebookresearch/faiss/wiki/Indexing-1M-vectors)</sup> 在生产方面，2023 年 SPAA 教程将 Milvus、Pinecone、Qdrant、Vespa、Weaviate 和 Zilliz 等向量搜索即服务公司列为封装 ANN 搜索技术的企业，<sup>[17](https://www.cs.ucr.edu/~ygu/SPAA/2023/ANNS-tutorial.pdf)</sup> Faiss 库论文指出 Zilliz 和 Pinecone 要么依赖 Faiss 作为核心引擎，要么重新实现了 Faiss 算法。<sup>[2](https://arxiv.org/html/2401.08281v4)</sup>

## 实际使用与陷阱

**调优。** 主要运行时参数是 IVF 索引的 nprobe、HNSW 的 efSearch、PQ 多义编码的 ht、IndexIVFPQR 的 k_factor 和 IndexRefine 的 kf_factor；超参数调优需要至少 1000 个查询点，最好 10000 个。<sup>[18](https://github.com/facebookresearch/faiss/wiki/Index-IO,-cloning-and-hyper-parameter-tuning)</sup> Faiss 的 AutoTuneCriterion 类保存真实结果并返回 0 到 1 之间的性能度量（1-recall@R 和 R-recall@R 准则）。<sup>[18](https://github.com/facebookresearch/faiss/wiki/Index-IO,-cloning-and-hyper-parameter-tuning)</sup>

**内存。** Faiss 索引通常存储在内存中。<sup>[5](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index)</sup> 当索引即使压缩后也无法装入内存时，选项包括：在多台机器上分片、存储在磁盘上（可能是分布式文件系统），或分布式键值存储，所有这些都比驻留内存的索引有运行时损失。<sup>[19](https://github.com/facebookresearch/faiss/wiki/Indexes-that-do-not-fit-in-RAM)</sup> 推荐的磁盘工作流程是训练一个不添加数据的 IVF 索引，存储训练好的空索引，然后在多台机器上独立添加数据库部分；Faiss 附带一个简单的 RPC 库，其中每个从节点持有一个分片，主节点查询所有从节点并合并结果。<sup>[19](https://github.com/facebookresearch/faiss/wiki/Indexes-that-do-not-fit-in-RAM)</sup> 在 GPU 上，StandardGpuResources 默认预留 512 MiB（4 GiB 或更少的 GPU）、1024 MiB（8 GiB 或更少）或最多 1536 MiB 的 GPU 内存用于临时计算空间，而每码 56 字节或更多的 GpuIndexIVFPQ 由于共享内存限制需要 float16 模式。<sup>[6](https://github.com/facebookresearch/faiss/wiki/Faiss-on-the-GPU)</sup> 使用 4 块 GPU 时，副本在大多数工作点上效果最好，但当 GPU 内存是限制因素时，使用公共 IVF 量化器的分片（IndexShardsIVF）是唯一选择。<sup>[15](https://github.com/facebookresearch/faiss/wiki/Hybrid-CPU-GPU-search-and-multiple-GPUs)</sup> 一个更小的陷阱：如果 PQ 预计算表大小超过 2 GB，Faiss 默认不计算它们。<sup>[12](https://github.com/facebookresearch/faiss/wiki/Indexing-1G-vectors)</sup>

## 近期发展

Faiss 1.8.0 于 2024 年 2 月 27 日发布，增加了 faiss-gpu-raft conda 包，集成了 NVIDIA RAFT 的 IVF-Flat 和 IVF-PQ 实现，支持 Python 3.11 和 3.12 以及 CUDA 12，引入了 avx512 优化模式，修复了一个 HNSW 错误以提高召回率，并为 IndexRefine 添加了搜索参数。<sup>[8](https://github.com/facebookresearch/faiss/blob/main/CHANGELOG.md)</sup> 此后的版本已推进到 1.15.0，增加了 Metal GPU 后端和新的 MetalIndexIVFFlat（IVF 扫描/合并内核及扩展的 top-k 支持），在 Apple Silicon 上默认启用 Metal，以及 HNSW 搜索性能改进和 SVS 相关变更。<sup>[9](https://github.com/facebookresearch/faiss/releases)</sup> 在打包方面，faiss-cpu conda 包可用于 Linux（x86-64 和 aarch64）、OSX（arm64）和 Windows（x86-64）；faiss-gpu 覆盖 CUDA 11.4 和 12.1，带有 NVIDIA cuVS 26.06 的 faiss-gpu-cuvs 包可用于 Linux x86-64 上的 CUDA 13.2。<sup>[20](https://github.com/facebookresearch/faiss/blob/HEAD/INSTALL.md)</sup>

## References

1. [facebookresearch/faiss GitHub repository](https://github.com/facebookresearch/faiss/)
2. [The Faiss Library (Douze et al., arXiv 2401.08281)](https://arxiv.org/html/2401.08281v4)
3. [Faiss — Meta AI](https://ai.meta.com/tools/faiss/)
4. [Faiss documentation](https://faiss.ai/)
5. [Guidelines to choose an index (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index)
6. [Faiss on the GPU (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/Faiss-on-the-GPU)
7. [Billion-scale similarity search with GPUs (Johnson, Douze, Jégou, arXiv 1702.08734)](https://arxiv.org/html/1702.08734v1)
8. [Faiss CHANGELOG.md](https://github.com/facebookresearch/faiss/blob/main/CHANGELOG.md)
9. [Releases · facebookresearch/faiss](https://github.com/facebookresearch/faiss/releases)
10. [Recent Approaches and Trends in Approximate Nearest Neighbor Search](https://pure.itu.dk/portal/da/publications/20239d14-6dff-4b27-bd57-f85484104308)
11. [Faiss indexes (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes)
12. [Indexing 1G vectors (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/Indexing-1G-vectors)
13. [GPU Faiss with cuVS (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/GPU-Faiss-with-cuVS)
14. [Indexing 1M vectors (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/Indexing-1M-vectors)
15. [Hybrid CPU GPU search and multiple GPUs (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/Hybrid-CPU-GPU-search-and-multiple-GPUs)
16. [ANN-Benchmarks](https://ann-benchmarks.com/index.html)
17. [Tutorial on Approximate Nearest Neighbor Search (SPAA 2023)](https://www.cs.ucr.edu/~ygu/SPAA/2023/ANNS-tutorial.pdf)
18. [Index IO, cloning and hyper parameter tuning (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/Index-IO,-cloning-and-hyper-parameter-tuning)
19. [Indexes that do not fit in RAM (Faiss wiki)](https://github.com/facebookresearch/faiss/wiki/Indexes-that-do-not-fit-in-RAM)
20. [Faiss INSTALL.md](https://github.com/facebookresearch/faiss/blob/HEAD/INSTALL.md)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database engines and systems › Vector databases and similarity-search stores

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/faiss

English original: https://www.edgepedia.org/faiss
