Skip to content

Commit 3542291

Browse files
zhichenxu-metafacebook-github-bot
authored andcommitted
Add Faiss library as an optional dependency (facebookincubator#13575)
Summary: Pull Request resolved: facebookincubator#13575 This PR introduces FAISS as an optional dependency, making the first step to support Faiss vector search in velox. Reviewed By: xiaoxmeng Differential Revision: D75707431 fbshipit-source-id: fc153cfc0acf3a7c12219b27a13c307257c1de5c
1 parent e554d33 commit 3542291

11 files changed

Lines changed: 151 additions & 4 deletions

File tree

.github/workflows/linux-build-base.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
GTest_SOURCE: BUNDLED
4040
cudf_SOURCE: BUNDLED
4141
CUDA_VERSION: '12.8'
42+
faiss_SOURCE: SYSTEM
4243
USE_CLANG: "${{ inputs.use-clang && 'true' || 'false' }}"
4344
steps:
4445
- uses: actions/checkout@v4
@@ -93,6 +94,7 @@ jobs:
9394
"-DVELOX_ENABLE_EXAMPLES=ON"
9495
"-DVELOX_ENABLE_ARROW=ON"
9596
"-DVELOX_ENABLE_GEO=ON"
97+
"-DVELOX_ENABLE_FAISS=ON"
9698
"-DVELOX_ENABLE_PARQUET=ON"
9799
"-DVELOX_ENABLE_HDFS=ON"
98100
"-DVELOX_ENABLE_S3=ON"

.github/workflows/macos.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ jobs:
7070
source scripts/setup-macos.sh
7171
install_build_prerequisites
7272
install_velox_deps_from_brew
73+
# Needed for faiss to find BLAS
74+
install_faiss_deps
7375
install_double_conversion
7476
7577
echo "NJOBS=`sysctl -n hw.ncpu`" >> $GITHUB_ENV
@@ -86,6 +88,7 @@ jobs:
8688
- name: Configure Build
8789
env:
8890
fmt_SOURCE: BUNDLED #brew fmt11 is not supported
91+
faiss_SOURCE: BUNDLED #brew faiss is not supported
8992
CMAKE_POLICY_VERSION_MINIMUM: '3.5'
9093
run: |
9194
ccache -sz -M 5Gi
@@ -97,7 +100,8 @@ jobs:
97100
-DVELOX_ENABLE_PARQUET=ON \
98101
-DVELOX_MONO_LIBRARY=ON \
99102
-DVELOX_BUILD_SHARED=ON \
100-
-DCMAKE_BUILD_TYPE=$BUILD_TYPE
103+
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
104+
-DVELOX_ENABLE_FAISS=ON
101105
102106
- name: Build
103107
run: |

CMake/resolve_dependency_modules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ by Velox. See details on bundling below.
3131
| re2 | 2021-04-01 | Yes |
3232
| fmt | 10.1.1 | Yes |
3333
| simdjson | 3.9.3 | Yes |
34+
| faiss | 1.11.0 | Yes |
3435
| folly | v2025.04.28.00 | Yes |
3536
| fizz | v2025.04.28.00 | No |
3637
| wangle | v2025.04.28.00 | No |
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
include_guard(GLOBAL)
15+
16+
set(VELOX_FAISS_BUILD_VERSION 1.11.0)
17+
set(VELOX_FAISS_BUILD_SHA256_CHECKSUM
18+
c5d517da6deb6a6d74290d7145331fc7474426025e2d826fa4a6d40670f4493c)
19+
set(VELOX_FAISS_SOURCE_URL
20+
"https://github.com/facebookresearch/faiss/archive/refs/tags/v${VELOX_FAISS_BUILD_VERSION}.tar.gz"
21+
)
22+
23+
velox_resolve_dependency_url(FAISS)
24+
25+
# We need these hints for macos to build.
26+
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
27+
message(STATUS "Detected Apple platform")
28+
execute_process(
29+
COMMAND brew --prefix libomp
30+
RESULT_VARIABLE BREW_LIBOMP_RESULT
31+
OUTPUT_VARIABLE BREW_LIBOMP_PREFIX
32+
OUTPUT_STRIP_TRAILING_WHITESPACE)
33+
if(BREW_LIBOMP_RESULT EQUAL 0 AND EXISTS "${BREW_LIBOMP_PREFIX}")
34+
list(APPEND CMAKE_PREFIX_PATH "${BREW_LIBOMP_PREFIX}")
35+
endif()
36+
37+
execute_process(
38+
COMMAND brew --prefix openblas
39+
RESULT_VARIABLE BREW_OPENBLAS_RESULT
40+
OUTPUT_VARIABLE BREW_OPENBLAS_PREFIX
41+
OUTPUT_STRIP_TRAILING_WHITESPACE)
42+
if(BREW_OPENBLAS_RESULT EQUAL 0 AND EXISTS "${BREW_OPENBLAS_PREFIX}")
43+
list(APPEND CMAKE_PREFIX_PATH "${BREW_OPENBLAS_PREFIX}")
44+
endif()
45+
endif()
46+
47+
FetchContent_Declare(
48+
faiss
49+
URL ${VELOX_FAISS_SOURCE_URL}
50+
URL_HASH ${VELOX_FAISS_BUILD_SHA256_CHECKSUM}
51+
SYSTEM # Once there are targets depending on FAISS we can add
52+
# `EXCLUDE_FROM_ALL` back in
53+
)
54+
55+
# Set build options
56+
block()
57+
set(BUILD_SHARED_LIBS OFF)
58+
set(CMAKE_BUILD_TYPE Release)
59+
set(FAISS_ENABLE_GPU OFF)
60+
set(FAISS_ENABLE_PYTHON OFF)
61+
set(FAISS_ENABLE_GPU_TESTS OFF)
62+
# Make FAISS available
63+
FetchContent_MakeAvailable(faiss)
64+
add_library(FAISS::faiss ALIAS faiss)
65+
endblock()

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ option(
160160
OFF)
161161
option(VELOX_SIMDJSON_SKIPUTF8VALIDATION
162162
"Skip simdjson utf8 validation in JSON parsing" OFF)
163+
option(VELOX_ENABLE_FAISS "Build faiss vector search support" OFF)
163164

164165
# Explicitly force compilers to generate colored output. Compilers usually do
165166
# this by default if they detect the output is a terminal, but this assumption
@@ -229,6 +230,11 @@ if(${VELOX_ENABLE_DUCKDB})
229230
velox_resolve_dependency(DuckDB)
230231
endif()
231232

233+
if(VELOX_ENABLE_FAISS)
234+
velox_set_source(faiss)
235+
velox_resolve_dependency(faiss)
236+
endif()
237+
232238
if(DEFINED ENV{INSTALL_PREFIX})
233239
# Allow installed package headers to be picked up before brew/system package
234240
# headers. We set this after DuckDB bundling since DuckDB uses its own

scripts/setup-centos9.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ function install_adapters {
142142
run_and_time install_hdfs
143143
}
144144

145+
function install_faiss_deps {
146+
dnf install -y openblas-devel
147+
dnf install -y libomp
148+
}
149+
145150
function install_velox_deps {
146151
run_and_time install_velox_deps_from_dnf
147152
run_and_time install_conda
@@ -164,6 +169,7 @@ function install_velox_deps {
164169
run_and_time install_xsimd
165170
run_and_time install_simdjson
166171
run_and_time install_geos
172+
run_and_time install_faiss
167173
}
168174

169175
(return 2> /dev/null) && return # If script was sourced, don't run commands.

scripts/setup-common.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ VELOX_BUILD_SHARED=${VELOX_BUILD_SHARED:-"OFF"} #Build folly and gflags shared f
2222
CMAKE_BUILD_TYPE="${BUILD_TYPE:-Release}"
2323
DEPENDENCY_DIR=${DEPENDENCY_DIR:-$(pwd)}
2424
BUILD_GEOS="${BUILD_GEOS:-true}"
25+
BUILD_FAISS="${BUILD_FAISS:-true}"
2526
BUILD_DUCKDB="${BUILD_DUCKDB:-true}"
2627
EXTRA_ARROW_OPTIONS=${EXTRA_ARROW_OPTIONS:-""}
2728

@@ -224,6 +225,26 @@ function install_geos {
224225
fi
225226
}
226227

228+
function install_faiss_deps {
229+
echo "Unsupported platform for faiss"
230+
}
231+
232+
function install_faiss {
233+
if [[ "$BUILD_FAISS" == "true" ]]; then
234+
# Install OpenBLAS and libomp if not already installed
235+
install_faiss_deps
236+
237+
wget_and_untar "https://github.com/facebookresearch/faiss/archive/refs/tags/v${FAISS_VERSION}.tar.gz" faiss
238+
cmake_install_dir faiss \
239+
-DFAISS_ENABLE_GPU=OFF \
240+
-DFAISS_ENABLE_PYTHON=OFF \
241+
-DFAISS_ENABLE_REMOTE=OFF \
242+
-DFAISS_ENABLE_GPU_TESTS=OFF \
243+
-DFAISS_ENABLE_BENCHMARKS=OFF
244+
fi
245+
}
246+
247+
227248
# Adapters that can be installed.
228249

229250
function install_aws_deps {

scripts/setup-helper-functions.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ function wget_and_untar {
196196
}
197197

198198
function cmake_install_dir {
199-
pushd "${DEPENDENCY_DIR}/$1"
199+
pushd "${DEPENDENCY_DIR}/$1" || return 1
200200
# remove the directory argument
201201
shift
202+
# shellcheck disable=SC2068
202203
cmake_install $@
203-
popd
204+
popd || return 1
204205
}
205206

206207
function cmake_install {

scripts/setup-macos.sh

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export CMAKE_POLICY_VERSION_MINIMUM="3.5"
4040

4141
DEPENDENCY_DIR=${DEPENDENCY_DIR:-$(pwd)}
4242
MACOS_VELOX_DEPS="bison flex gflags glog googletest icu4c libevent libsodium lz4 openssl protobuf@21 simdjson snappy xz xxhash zstd"
43+
4344
MACOS_BUILD_DEPS="ninja cmake"
4445

4546
SUDO="${SUDO:-""}"
@@ -133,6 +134,38 @@ function install_duckdb_clang {
133134
fi
134135
}
135136

137+
function install_faiss_deps {
138+
brew install openblas
139+
brew install libomp
140+
}
141+
142+
function install_faiss {
143+
if [[ "$BUILD_FAISS" == "true" ]]; then
144+
# Install OpenBLAS and libomp if not already installed
145+
install_faiss_deps
146+
147+
wget_and_untar "https://github.com/facebookresearch/faiss/archive/refs/tags/v${FAISS_VERSION}.tar.gz" faiss
148+
149+
local cmake_args
150+
cmake_args=(
151+
-DFAISS_ENABLE_GPU=OFF
152+
-DFAISS_ENABLE_PYTHON=OFF
153+
-DFAISS_ENABLE_REMOTE=OFF
154+
-DFAISS_ENABLE_GPU_TESTS=OFF
155+
-DFAISS_ENABLE_BENCHMARKS=OFF
156+
-DFAISS_ENABLE_GPU=OFF
157+
-FAISS_ENABLE_MKL=OFF
158+
)
159+
160+
local libomp_prefix
161+
libomp_prefix=$(brew --prefix libomp)
162+
cmake_args+=(
163+
"-DCMAKE_PREFIX_PATH=${libomp_prefix}"
164+
)
165+
cmake_install_dir faiss "${cmake_args[@]}"
166+
fi
167+
}
168+
136169
function install_velox_deps {
137170
run_and_time install_velox_deps_from_brew
138171
run_and_time install_ranges_v3
@@ -152,8 +185,9 @@ function install_velox_deps {
152185
# See https://github.com/facebook/fbthrift/pull/317 for an explanation.
153186
# run_and_time install_thrift
154187
run_and_time install_arrow
155-
run_and_time install_duckdb_clang
188+
run_and_time install_duckdb_clang
156189
run_and_time install_geos
190+
run_and_time install_faiss
157191
}
158192

159193
(return 2> /dev/null) && return # If script was sourced, don't run commands.

scripts/setup-ubuntu.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ function install_adapters {
227227
run_and_time install_hdfs
228228
}
229229

230+
function install_faiss_deps {
231+
sudo apt-get install -y libopenblas-dev libomp-dev
232+
}
233+
230234
function install_velox_deps {
231235
run_and_time install_velox_deps_from_apt
232236
run_and_time install_fmt
@@ -246,6 +250,7 @@ function install_velox_deps {
246250
run_and_time install_xsimd
247251
run_and_time install_simdjson
248252
run_and_time install_geos
253+
run_and_time install_faiss
249254
}
250255

251256
function install_apt_deps {

0 commit comments

Comments
 (0)