FAISS (Facebook AI Similarity Search) is a powerful library for efficient similarity search and clustering of dense vectors. It’s optimized for high performance and scalability, making it an excellent choice for handling large datasets and computing nearest neighbors.
In this short blog, we’ll explore how to measure the average latency of searching 1000 records using FAISS on a Mac with an Apple M1 processor.
Setting Up the Environment
To get started, you’ll need to install FAISS and ensure you have appropriate development tools and libraries. You can install FAISS using pip:
pip install faiss-cpu |
Next, we set up our environment by importing necessary packages and defining constants.
import faiss |
Generating Random Vectors
FAISS works with dense vectors. Here, we generate random vectors that simulate our dataset:
# Create random vectors |
Initializing the FAISS Index
We initialize the FAISS index using IndexFlatL2
, which computes the Euclidean (L2) distance between vectors:
# Initialize FAISS index |
Measuring Latency
To measure the latency of search operations, we generate a query vector and perform multiple searches, recording the time taken for each one:
# Generate query vector |
Calculating Average Latency
Finally, we calculate the average latency in milliseconds and print the result:
# Calculate average latency in milliseconds |
Results
Running the above code on a Mac with an M1 processor, we achieve an impressive average latency of around 7 milliseconds for 1000 searches. This performance demonstrates the efficiency of FAISS and the capabilities of Apple M1 processors for machine learning tasks.