FAISS(Facebook AI Similarity Search)는 Meta AI가 개발한 고성능 벡터 유사도 검색 라이브러리다. 수십억 개의 고차원 벡터를 빠르게 색인하고 근사 최근접 이웃(ANN) 검색을 GPU 가속으로 수행한다.
인덱스 유형 비교
| 인덱스 | 방식 | 메모리 | 속도 | 정확도 |
|---|
| IndexFlatL2 | 완전 탐색 | 높음 | 느림 | 100% |
| IndexIVFFlat | 역파일 인덱스 | 중간 | 빠름 | 높음 |
| IndexIVFPQ | 적곱 양자화 | 낮음 | 매우 빠름 | 중간 |
| IndexHNSWFlat | 계층적 NSW | 중간 | 매우 빠름 | 높음 |
기본 사용법
python
import faiss
import numpy as np
d = 128
n = 1000
vectors = np.random.random((n, d)).astype('float32')
faiss.normalize_L2(vectors)
index = faiss.IndexFlatL2(d)
index.add(vectors)
print(f"총 벡터 수: {index.ntotal}")
query = np.random.random((1, d)).astype('float32')
faiss.normalize_L2(query)
k = 5
distances, indices = index.search(query, k)
print(f"상위 {k}개 결과 인덱스: {indices[0]}")
IVF 인덱스 (대규모 데이터셋)
python
nlist = 100
quantizer = faiss.IndexFlatL2(d)
index_ivf = faiss.IndexIVFFlat(quantizer, d, nlist)
index_ivf.train(vectors)
index_ivf.add(vectors)
index_ivf.nprobe = 10
distances, indices = index_ivf.search(query, k)
GPU 가속
python
res = faiss.StandardGpuResources()
gpu_index = faiss.index_cpu_to_gpu(res, 0, index)
# 멀티 GPU
gpu_index = faiss.index_cpu_to_all_gpus(index)
관련 문서