mirror of
https://github.com/facebookresearch/faiss.git
synced 2026-10-11 22:50:00 +00:00
Summary: - The current Faiss conda recipe does not specify the OpenBLAS threading variant. - On Linux AArch64, the dependency solver selects the pthreads build. - This causes problems when Faiss calls BLAS from within an existing OpenMP parallel region due to nested threading - As a result, the benchmark below (with an OOB conda install of faiss) only yields 7% of the throughput reachable when an OMP build of OpenBLAS is selected (what this PR does) - This PR fixes the issue, and as a result accelerates affected workloads by ~ 14x ``` # SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <[email protected]> # SPDX-License-Identifier: MIT import ctypes import statistics import time import faiss import numpy as np rng = np.random.default_rng(1234) d = 128 nlist = 4096 nq = 40_000 quantizer = faiss.IndexFlatL2(d) quantizer.add(rng.random((nlist, d), dtype=np.float32)) index = faiss.IndexIVFPQFastScan(quantizer, d, nlist, 128, 4) # index is empty - this only triggeres the course quantizer index.is_trained = True index.nprobe = 64 xq = rng.random((nq, d), dtype=np.float32) openblas = ctypes.CDLL("libopenblas.so.0") openblas.openblas_get_parallel.restype = ctypes.c_int openblas.openblas_get_num_threads.restype = ctypes.c_int print(f"nq={nq:,}, d={d}, nlist={nlist}, nprobe={index.nprobe}") print(f"OpenMP threads={faiss.omp_get_max_threads()}") print( f"OpenBLAS parallel={openblas.openblas_get_parallel()}, " f"threads={openblas.openblas_get_num_threads()}" ) print(f"BLAS threshold={faiss.cvar.distance_compute_blas_threshold}") index.search(xq, 10) times = [] for _ in range(20): start = time.perf_counter() index.search(xq, 10) times.append(time.perf_counter() - start) elapsed = statistics.median(times) print(f"median={elapsed:.6f}s, throughput={nq / elapsed:,.0f} QPS") ``` Pull Request resolved: https://github.com/facebookresearch/faiss/pull/5669 Reviewed By: weixianghong Differential Revision: D124380509 Pulled By: mnorris11 fbshipit-source-id: 16e1521e7aaf7d65c6a4ce8e404a47bdb26c1198
138 lines
5.0 KiB
YAML
138 lines
5.0 KiB
YAML
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
# Copyright 2026 Arm Limited and/or its affiliates <[email protected]>
|
|
#
|
|
# This source code is licensed under the MIT license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
{% set version = environ.get('GIT_DESCRIBE_TAG').lstrip('v') %}
|
|
{% set suffix = "_nightly" if environ.get('PACKAGE_TYPE') == 'nightly' else "" %}
|
|
{% set number = GIT_DESCRIBE_NUMBER %}
|
|
{% set cuda_major = (cudatoolkit | default("12.0")).split('.')[0] | int %}
|
|
{% set cuda_minor = (cudatoolkit | default("12.0")).split('.')[1] | int %}
|
|
{% if cuda_major >= 13 %}
|
|
{% set cuda_constraints=">=13.2,<13.3" %}
|
|
{% set libcublas_constraints=">=13.3,<13.4" %} # libcublas 13.3 is actually for cuda 13.2
|
|
{% set cudart_constraints=">=13.2,<13.3" %}
|
|
{% else %}
|
|
{% set cuda_constraints=">=12.6,<12.7" %}
|
|
{% set libcublas_constraints=">=12.6,<12.7" %}
|
|
{% set cudart_constraints=">=12.6,<12.7" %}
|
|
{% endif %}
|
|
|
|
package:
|
|
name: faiss-pkg
|
|
version: {{ version }}
|
|
|
|
build:
|
|
number: {{ number }}
|
|
|
|
about:
|
|
home: https://github.com/facebookresearch/faiss
|
|
license: MIT
|
|
license_family: MIT
|
|
license_file: LICENSE
|
|
summary: A library for efficient similarity search and clustering of dense vectors.
|
|
|
|
source:
|
|
git_url: ../../
|
|
|
|
outputs:
|
|
- name: libfaiss
|
|
script: build-lib.sh # [x86_64 and not win and not osx]
|
|
script: build-lib-osx.sh # [x86_64 and osx]
|
|
script: build-lib-arm64.sh # [not x86_64]
|
|
script: build-lib.bat # [win]
|
|
build:
|
|
string: "h{{ PKG_HASH }}_{{ number }}_cuda{{ cudatoolkit }}{{ suffix }}"
|
|
run_exports:
|
|
- {{ pin_compatible('libfaiss', exact=True) }}
|
|
script_env:
|
|
- CUDA_ARCHS
|
|
- FAISS_FLATTEN_CONDA_INCLUDES
|
|
requirements:
|
|
build:
|
|
- {{ compiler('cxx') }} >=13.4,<14
|
|
- sysroot_linux-64 =2.17 # [linux64]
|
|
- llvm-openmp # [osx]
|
|
- cmake >=3.24.0
|
|
- make =4.2 # [not win and not (osx and arm64)]
|
|
- make =4.4 # [osx and arm64]
|
|
- mkl-devel >=2024.2.2,<2026 # [x86_64]
|
|
- cuda-toolkit {{ cudatoolkit }}
|
|
host:
|
|
- mkl >=2024.2.2,<2026 # [x86_64]
|
|
- openblas =0.3.34 openmp_* # [not x86_64]
|
|
- libsvs-runtime =0.4.0 # [x86_64 and linux]
|
|
run:
|
|
- mkl >=2024.2.2,<2026 # [x86_64]
|
|
- openblas =0.3.34 openmp_* # [not x86_64]
|
|
- cuda-cudart {{ cuda_constraints }}
|
|
- libcublas {{ libcublas_constraints }}
|
|
- libsvs-runtime =0.4.0 # [x86_64 and linux]
|
|
test:
|
|
requires:
|
|
- conda-build
|
|
commands:
|
|
- test -f $PREFIX/lib/libfaiss$SHLIB_EXT # [not win]
|
|
- test -f $PREFIX/lib/libfaiss_avx2$SHLIB_EXT # [x86_64 and not win]
|
|
- conda inspect linkages -p $PREFIX $PKG_NAME # [not win]
|
|
- conda inspect objects -p $PREFIX $PKG_NAME # [osx]
|
|
- python -c "import ctypes; lib = ctypes.CDLL('libopenblas.so.0'); assert lib.openblas_get_parallel() == 2" # [linux and not x86_64]
|
|
|
|
- name: faiss-gpu
|
|
script: build-pkg.sh # [x86_64 and not win and not osx]
|
|
script: build-pkg-osx.sh # [x86_64 and osx]
|
|
script: build-pkg-arm64.sh # [not x86_64]
|
|
script: build-pkg.bat # [win]
|
|
build:
|
|
string: "py{{ PY_VER }}_h{{ PKG_HASH }}_{{ number }}_cuda{{ cudatoolkit }}{{ suffix }}"
|
|
requirements:
|
|
build:
|
|
- {{ compiler('cxx') }} >=13.4,<14
|
|
- sysroot_linux-64 =2.17 # [linux64]
|
|
- swig =4.0
|
|
- cmake >=3.24.0
|
|
- make =4.2 # [not win and not (osx and arm64)]
|
|
- make =4.4 # [osx and arm64]
|
|
- _openmp_mutex =4.5=2_kmp_llvm # [x86_64 and not win]
|
|
- cuda-toolkit {{ cudatoolkit }}
|
|
- mkl-devel >=2024.2.2,<2026 # [x86_64]
|
|
host:
|
|
- mkl >=2024.2.2,<2026 # [x86_64]
|
|
- python {{ python }}
|
|
- numpy >=2.0,<3.0
|
|
- setuptools
|
|
- _openmp_mutex =4.5=2_kmp_llvm # [x86_64 and not win]
|
|
- {{ pin_subpackage('libfaiss', exact=True) }}
|
|
run:
|
|
- mkl >=2024.2.2,<2026 # [x86_64]
|
|
- python {{ python }}
|
|
- numpy >=2.0,<3.0
|
|
- packaging
|
|
- {{ pin_subpackage('libfaiss', exact=True) }}
|
|
test:
|
|
requires:
|
|
- numpy >=2.0,<3.0
|
|
- scipy
|
|
# pytorch-gpu on conda-forge requires CUDA >=12.9; skip for older CUDA 12.x.
|
|
# TODO: remove guard when we move to PyPI (pytorch via PyPI works on CUDA 13)
|
|
{% if cuda_major == 12 and cuda_minor >= 9 %}
|
|
- pytorch-gpu >=2.7
|
|
{% endif %}
|
|
commands:
|
|
- python -X faulthandler -m unittest discover -v -s tests/ -p "test_*"
|
|
{% if cuda_major == 12 and cuda_minor >= 9 %}
|
|
- python -X faulthandler -m unittest discover -v -s tests/ -p "torch_*"
|
|
{% endif %}
|
|
- cp tests/common_faiss_tests.py faiss/gpu/test
|
|
- python -X faulthandler -m unittest discover -v -s faiss/gpu/test/ -p "test_*"
|
|
{% if cuda_major == 12 and cuda_minor >= 9 %}
|
|
- python -X faulthandler -m unittest discover -v -s faiss/gpu/test/ -p "torch_*"
|
|
{% endif %}
|
|
- sh test_cpu_dispatch.sh # [linux64]
|
|
files:
|
|
- test_cpu_dispatch.sh # [linux64]
|
|
source_files:
|
|
- tests/
|
|
- faiss/gpu/test/
|