From f809cf0d512eb69b3c675be53d287b9cc79c0f3e Mon Sep 17 00:00:00 2001 From: Gergely Szilvasy Date: Fri, 12 May 2023 09:07:50 -0700 Subject: [PATCH] fix osx openmp (#2857) Summary: Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2857 Reviewed By: alexanderguzhva Differential Revision: D45791625 Pulled By: algoriddle fbshipit-source-id: 52ec30f0ec66812f60a08843947aabe8be876afa --- conda/faiss-gpu/meta.yaml | 26 ++++++++++++++++---------- conda/faiss/build-lib-osx.sh | 27 +++++++++++++++++++++++++++ conda/faiss/build-pkg-osx.sh | 26 ++++++++++++++++++++++++++ conda/faiss/meta.yaml | 19 +++++++++++-------- 4 files changed, 80 insertions(+), 18 deletions(-) create mode 100755 conda/faiss/build-lib-osx.sh create mode 100755 conda/faiss/build-pkg-osx.sh diff --git a/conda/faiss-gpu/meta.yaml b/conda/faiss-gpu/meta.yaml index 2192d1b311..39e4060a20 100644 --- a/conda/faiss-gpu/meta.yaml +++ b/conda/faiss-gpu/meta.yaml @@ -26,7 +26,9 @@ source: outputs: - name: libfaiss - script: build-lib.sh + script: build-lib.sh # [x86_64 and not win] + script: build-lib-arm64.sh # [not x86_64] + script: build-lib.bat # [win] build: string: "h{{ PKG_HASH }}_{{ number }}_cuda{{ cudatoolkit }}{{ suffix }}" run_exports: @@ -36,28 +38,32 @@ outputs: requirements: build: - {{ compiler('cxx') }} - - sysroot_linux-64 =2.17 # [linux64] + - sysroot_linux-64 =2.17 # [linux64] - llvm-openmp # [osx] - cmake =3.23.1 - make # [not win] - - mkl-devel =2023 + - mkl-devel =2023 # [x86_64] host: - - mkl =2023 + - mkl =2023 # [x86_64] + - openblas # [not x86_64] - cudatoolkit {{ cudatoolkit }} run: - - mkl =2023 + - mkl =2023 # [x86_64] + - openblas # [not x86_64] - {{ pin_compatible('cudatoolkit', max_pin='x.x') }} test: requires: - conda-build commands: - - test -f $PREFIX/lib/libfaiss.so # [linux] - - test -f $PREFIX/lib/libfaiss.dylib # [osx] + - 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] - name: faiss-gpu - script: build-pkg.sh + script: build-pkg.sh # [x86_64 and not win] + 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: @@ -86,9 +92,9 @@ outputs: - cp tests/common_faiss_tests.py faiss/gpu/test - python -X faulthandler -m unittest discover -v -s faiss/gpu/test/ -p "test_*" - python -X faulthandler -m unittest discover -v -s faiss/gpu/test/ -p "torch_*" - - sh test_cpu_dispatch.sh # [linux] + - sh test_cpu_dispatch.sh # [linux64] files: - - test_cpu_dispatch.sh # [linux] + - test_cpu_dispatch.sh # [linux64] source_files: - tests/ - faiss/gpu/test/ diff --git a/conda/faiss/build-lib-osx.sh b/conda/faiss/build-lib-osx.sh new file mode 100755 index 0000000000..a30de2d000 --- /dev/null +++ b/conda/faiss/build-lib-osx.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +set -e + + +# Build libfaiss.so/libfaiss_avx2.so. +cmake -B _build \ + -DBUILD_SHARED_LIBS=ON \ + -DBUILD_TESTING=OFF \ + -DFAISS_OPT_LEVEL=avx2 \ + -DFAISS_ENABLE_GPU=OFF \ + -DFAISS_ENABLE_PYTHON=OFF \ + -DBLA_VENDOR=Intel10_64lp \ + -DOpenMP_CXX_FLAGS=-fopenmp=libiomp5 \ + -DOpenMP_CXX_LIB_NAMES=libiomp5 \ + -DOpenMP_libiomp5_LIBRARY=$PREFIX/lib/libiomp5.dylib \ + -DCMAKE_INSTALL_LIBDIR=lib \ + -DCMAKE_BUILD_TYPE=Release . + +make -C _build -j$(nproc) faiss faiss_avx2 + +cmake --install _build --prefix $PREFIX +cmake --install _build --prefix _libfaiss_stage/ diff --git a/conda/faiss/build-pkg-osx.sh b/conda/faiss/build-pkg-osx.sh new file mode 100755 index 0000000000..15016face9 --- /dev/null +++ b/conda/faiss/build-pkg-osx.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +set -e + + +# Build swigfaiss.so/swigfaiss_avx2.so. +cmake -B _build_python_${PY_VER} \ + -Dfaiss_ROOT=_libfaiss_stage/ \ + -DFAISS_OPT_LEVEL=avx2 \ + -DFAISS_ENABLE_GPU=OFF \ + -DOpenMP_CXX_FLAGS=-fopenmp=libiomp5 \ + -DOpenMP_CXX_LIB_NAMES=libiomp5 \ + -DOpenMP_libiomp5_LIBRARY=$PREFIX/lib/libiomp5.dylib \ + -DCMAKE_BUILD_TYPE=Release \ + -DPython_EXECUTABLE=$PYTHON \ + faiss/python + +make -C _build_python_${PY_VER} -j$(nproc) swigfaiss swigfaiss_avx2 + +# Build actual python module. +cd _build_python_${PY_VER}/ +$PYTHON setup.py install --single-version-externally-managed --record=record.txt --prefix=$PREFIX diff --git a/conda/faiss/meta.yaml b/conda/faiss/meta.yaml index 3c0ac9781d..31b785eb9b 100644 --- a/conda/faiss/meta.yaml +++ b/conda/faiss/meta.yaml @@ -26,8 +26,9 @@ source: outputs: - name: libfaiss - script: build-lib.sh # [x86_64 and not win] - script: build-lib-arm64.sh # [not x86_64] + 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 }}_cpu{{ suffix }}" @@ -36,16 +37,17 @@ outputs: requirements: build: - {{ compiler('cxx') }} - - sysroot_linux-64 =2.17 # [linux64] + - sysroot_linux-64 =2.17 # [linux64] + - llvm-openmp # [osx] - cmake =3.23.1 - make # [not win] - - mkl-devel =2023 # [x86_64] + - mkl-devel =2023 # [x86_64] host: - - mkl =2023 # [x86_64] - - openblas # [not x86_64] + - mkl =2023 # [x86_64] + - openblas # [not x86_64] run: - mkl =2023 # [x86_64] - - openblas # [not x86_64] + - openblas # [not x86_64] test: requires: - conda-build @@ -56,7 +58,8 @@ outputs: - conda inspect objects -p $PREFIX $PKG_NAME # [osx] - name: faiss-cpu - script: build-pkg.sh # [x86_64 and not win] + 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: