Skip to content

Commit

Permalink
Merge pull request #5 from matsui528/build
Browse files Browse the repository at this point in the history
Updated how-to-build with Ubuntu 22.04
  • Loading branch information
matsui528 authored May 22, 2023
2 parents 31e5822 + 65d1bab commit ecc1b47
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
40 changes: 38 additions & 2 deletions .github/workflows/build_from_source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,41 @@ jobs:
cd
python -c "import faiss, numpy; err = faiss.Kmeans(10, 20).train(numpy.random.rand(1000, 10).astype('float32')); print(err)"
echo "##### AVX2 is activated or not #####"
LD_DEBUG=libs python -c "import faiss" 2>&1 | grep libfaiss_avx2.so
echo "##### Check AVX2 is working or not #####"
LD_DEBUG=libs python -c "import faiss" 2>&1 | grep libfaiss_avx2.so
echo "import faiss
import numpy as np
import time
np.random.seed(234)
D = 128
N = 10000
X = np.random.random((N, D)).astype(np.float32)
M = 64
nbits = 4
pq = faiss.IndexPQ(D, M, nbits)
pq.train(X)
pq.add(X)
pq_fast = faiss.IndexPQFastScan(D, M, nbits)
pq_fast.train(X)
pq_fast.add(X)
t0 = time.time()
d1, ids1 = pq.search(x=X[:3], k=5)
t1 = time.time()
print(f'pq: {(t1 - t0) * 1000} msec')
t0 = time.time()
d2, ids2 = pq_fast.search(x=X[:3], k=5)
t1 = time.time()
print(f'pq_fast: {(t1 - t0) * 1000} msec')
assert np.allclose(ids1, ids2)
" > check.py
python check.py
16 changes: 10 additions & 6 deletions build.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This document is a step-by-step instruction for building faiss from the source. We assume:
- x86 architecture
- CPU
- Ubuntu 20.04
- Ubuntu 22.04
- miniconda for python environment
- Intel MKL (we can install it simply by `apt` for Ubuntu 20.04 or higher)
- AVX2
Expand Down Expand Up @@ -49,16 +49,19 @@ If you cannot install intel-mkl, you can use open-blas by `sudo apt install -y l


### cmake
Currently, cmake from apt is old (3.16). There are three options to install new cmake.
Currently, cmake from apt is old (3.16 for Ubuntu 20.04, and 3.22 for Ubuntu 22.04). For faiss 1.7+, we need cmake 3.23+. There are three options to install new cmake.
- Build from source
- Install by snap. This is the easiest.
```bash
sudo snap install cmake --classic
```
Note that WSL recently supported snap. See [this](https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/#set-the-systemd-flag-set-in-your-wsl-distro-settings).
- If you've installed conda, you can install cmake by conda.
```bash
conda install -c anaconda cmake
```
- [Use APT repository](https://askubuntu.com/a/1157132). Seems easy. Not tested by myself though.
### miniconda
We will use miniconda for python. See [this](https://conda.io/projects/conda/en/latest/user-guide/install/macos.html#install-macos-silent) for the instruction of the silent installation.
Expand Down Expand Up @@ -163,6 +166,7 @@ Then let's install the module on your python.
cd build/faiss/python
python setup.py install
```
This will update your python environment (You can uninstall it by `pip uninstall faiss`).

Finally, you need to specify the PYTHONPATH. Activate it, and write it on `~/.bashrc`.
```bash
Expand Down Expand Up @@ -202,9 +206,9 @@ import faiss
import numpy as np
import time
np.random.seed(123)
np.random.seed(234)
D = 128
N = 1000
N = 10000
X = np.random.random((N, D)).astype(np.float32)
M = 64
nbits = 4
Expand Down Expand Up @@ -233,8 +237,8 @@ assert np.allclose(ids1, ids2)
Then run `python check.py`.
If AVX2 is properly activated, pq_fast should be roughly 10x faster:
```bash
pq: 0.5166530609130859 msec
pq_fast: 0.06580352783203125 msec
pq: 1.8916130065917969 msec
pq_fast: 0.1723766326904297 msec
```


Expand Down

0 comments on commit ecc1b47

Please sign in to comment.