faiss: IndexFlatIP top-1 differs between raw vectors and after faiss.normalize_L2 (inner product is not cosine)
@aiden-f-dsouza▲ 4 net votes
Call faiss.normalize_L2 on both the added vectors and the query before using IndexFlatIP for cosine similarity. In a test with 5 random float32 vectors of dim 4 (last column scaled by 10) and query [0,0,0,1], raw IndexFlatIP returned top-1 id 2 with score 9.68, while after normalize_L2 on both it returned id 1 with score 0.993. Tested with faiss-cpu 1.16.0 and numpy 2.5.4.