Skip to content

Commit 31bddeb

Browse files
algoriddlefacebook-github-bot
authored andcommitted
add faiss_gpu lib (facebookresearch#3222)
Summary: 1. Split the GPU code into a separate static library to reduce build times. 2. faiss-gpu now depends on pytorch-cuda to stop clobbering of CUDA libraries when Faiss and Pytorch installed in the same environment 3. Add CUDA 12 (we support both 11.8 and 12.1) 4. Add Python 3.12 Pull Request resolved: facebookresearch#3222 Reviewed By: mlomeli1 Differential Revision: D53215033 Pulled By: algoriddle fbshipit-source-id: d48c5707af20b7e5ff72c3aa27b0e677259f22f8
1 parent 2817344 commit 31bddeb

File tree

5 files changed

+72
-49
lines changed

5 files changed

+72
-49
lines changed

.circleci/config.yml

+48-29
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ executors:
99
environment:
1010
CONDA_ARCH: Linux-x86_64
1111
machine:
12-
image: linux-cuda-11:default
12+
image: linux-cuda-12:default
1313
resource_class: gpu.nvidia.medium
1414
linux-arm64-cpu:
1515
environment:
1616
CONDA_ARCH: Linux-aarch64
1717
machine:
18-
image: ubuntu-2004:current
18+
image: ubuntu-2204:current
1919
resource_class: arm.medium
2020
macosx-arm64-cpu:
2121
environment:
@@ -138,10 +138,9 @@ jobs:
138138
name: Conda build (GPU)
139139
no_output_timeout: 60m
140140
command: |
141-
sudo update-alternatives --set cuda /usr/local/cuda-<<parameters.cuda>>
142141
cd conda
143142
conda build faiss-gpu --variants '{ "cudatoolkit": "<<parameters.cuda>>", "c_compiler_version": "<<parameters.compiler_version>>", "cxx_compiler_version": "<<parameters.compiler_version>>" }' \
144-
-c pytorch -c nvidia
143+
-c pytorch -c nvidia/label/cuda-<<parameters.cuda>>
145144
- when:
146145
condition:
147146
and:
@@ -153,10 +152,9 @@ jobs:
153152
name: Conda build (GPU) w/ anaconda upload
154153
no_output_timeout: 60m
155154
command: |
156-
sudo update-alternatives --set cuda /usr/local/cuda-<<parameters.cuda>>
157155
cd conda
158156
conda build faiss-gpu --variants '{ "cudatoolkit": "<<parameters.cuda>>", "c_compiler_version": "<<parameters.compiler_version>>", "cxx_compiler_version": "<<parameters.compiler_version>>" }' \
159-
--user pytorch --label <<parameters.label>> -c pytorch -c nvidia
157+
--user pytorch --label <<parameters.label>> -c pytorch -c nvidia/label/cuda-<<parameters.cuda>>
160158
- when:
161159
condition:
162160
and:
@@ -168,7 +166,6 @@ jobs:
168166
name: Conda build (GPU w/ RAFT)
169167
no_output_timeout: 60m
170168
command: |
171-
sudo update-alternatives --set cuda /usr/local/cuda-<<parameters.cuda>>
172169
cd conda
173170
conda build faiss-gpu-raft --variants '{ "cudatoolkit": "<<parameters.cuda>>", "c_compiler_version": "<<parameters.compiler_version>>", "cxx_compiler_version": "<<parameters.compiler_version>>" }' \
174171
-c pytorch -c nvidia -c rapidsai-nightly -c conda-forge
@@ -183,10 +180,9 @@ jobs:
183180
name: Conda build (GPU w/ RAFT) w/ anaconda upload
184181
no_output_timeout: 60m
185182
command: |
186-
sudo update-alternatives --set cuda /usr/local/cuda-<<parameters.cuda>>
187183
cd conda
188184
conda build faiss-gpu-raft --variants '{ "cudatoolkit": "<<parameters.cuda>>", "c_compiler_version": "<<parameters.compiler_version>>", "cxx_compiler_version": "<<parameters.compiler_version>>" }' \
189-
--user pytorch --label <<parameters.label>> -c pytorch -c nvidia -c rapidsai-nightly -c conda-forge
185+
--user pytorch --label <<parameters.label>> -c pytorch -c nvidia/label/cuda-<<parameters.cuda>> -c rapidsai-nightly -c conda-forge
190186
191187
build_cmake:
192188
parameters:
@@ -216,27 +212,35 @@ jobs:
216212
bash miniconda.sh -b -p $HOME/miniconda
217213
~/miniconda/bin/conda init
218214
fi
219-
- when:
220-
condition:
221-
equal: [ "ON", << parameters.gpu >> ]
222-
steps:
223-
- run:
224-
name: Configure CUDA
225-
command: sudo update-alternatives --set cuda /usr/local/cuda-11.4
226215
- run:
227216
name: Set up environment
228217
command: |
229218
conda config --set solver libmamba
230219
conda update -y -q conda
231-
conda install -y -q python=3.11 cmake make swig mkl=2023 mkl-devel=2023 numpy scipy pytest gxx_linux-64 sysroot_linux-64
220+
- when:
221+
condition:
222+
equal: [ "OFF", << parameters.raft >> ]
223+
steps:
224+
- run:
225+
name: Install env using main channel
226+
command: |
227+
conda install -y -q python=3.11 cmake make swig=4.0.2 mkl=2023 mkl-devel=2023 numpy scipy pytest gxx_linux-64 sysroot_linux-64
232228
- when:
233229
condition:
234230
equal: [ "ON", << parameters.raft >> ]
235231
steps:
236232
- run:
237-
name: Install libraft
233+
name: Install env using conda-forge channel
238234
command: |
239-
conda install -y -q libraft cuda-version=11.4 -c rapidsai-nightly -c nvidia -c pkgs/main -c conda-forge
235+
conda install -y -q python=3.11 cmake make swig=4.0.2 mkl=2023 mkl-devel=2023 numpy scipy pytest gxx_linux-64 sysroot_linux-64=2.28 libraft cuda-version=11.8 -c rapidsai-nightly -c "nvidia/label/cuda-11.8.0" -c conda-forge
236+
- when:
237+
condition:
238+
equal: [ "ON", << parameters.gpu >> ]
239+
steps:
240+
- run:
241+
name: Install CUDA
242+
command: |
243+
conda install -y -q cuda-nvcc cuda-cudart-dev libcublas libcublas-dev cuda-toolkit -c "nvidia/label/cuda-11.8.0"
240244
- run:
241245
name: Build all targets
242246
no_output_timeout: 30m
@@ -245,7 +249,7 @@ jobs:
245249
conda activate
246250
cmake -B build \
247251
-DBUILD_TESTING=ON \
248-
-DBUILD_SHARED_LIBS=OFF \
252+
-DBUILD_SHARED_LIBS=ON \
249253
-DFAISS_ENABLE_GPU=<< parameters.gpu >> \
250254
-DFAISS_ENABLE_RAFT=<< parameters.raft >> \
251255
-DFAISS_OPT_LEVEL=<< parameters.opt_level >> \
@@ -283,7 +287,7 @@ jobs:
283287
- run:
284288
name: Python tests (CPU + GPU)
285289
command: |
286-
conda install -y -q pytorch pytorch-cuda=11 -c pytorch -c nvidia
290+
conda install -y -q pytorch pytorch-cuda=11 -c pytorch -c nvidia/label/cuda-11.8.0
287291
pytest --junitxml=test-results/pytest/results.xml tests/test_*.py
288292
pytest --junitxml=test-results/pytest/results-torch.xml tests/torch_*.py
289293
cp tests/common_faiss_tests.py faiss/gpu/test
@@ -350,10 +354,10 @@ workflows:
350354
branches:
351355
ignore: /.*/
352356
- build_conda:
353-
name: Linux x86_64 GPU packages (CUDA 11.4)
357+
name: Linux x86_64 GPU packages (CUDA 11.8)
354358
exec: linux-x86_64-gpu
355359
label: main
356-
cuda: "11.4"
360+
cuda: "11.8"
357361
cuda_archs: "60-real;61-real;62-real;70-real;72-real;75-real;80;86-real"
358362
compiler_version: "11.2"
359363
filters:
@@ -362,11 +366,11 @@ workflows:
362366
branches:
363367
ignore: /.*/
364368
- build_conda:
365-
name: Linux x86_64 GPU w/ RAFT packages (CUDA 11.4)
369+
name: Linux x86_64 GPU w/ RAFT packages (CUDA 11.8)
366370
exec: linux-x86_64-gpu
367371
label: main
368372
raft: "ON"
369-
cuda: "11.4"
373+
cuda: "11.8"
370374
cuda_archs: "60-real;61-real;62-real;70-real;72-real;75-real;80;86-real"
371375
compiler_version: "11.2"
372376
filters:
@@ -416,17 +420,32 @@ workflows:
416420
exec: linux-x86_64-cpu
417421
label: nightly
418422
- build_conda:
419-
name: Linux x86_64 GPU nightlies (CUDA 11.4)
423+
name: Linux x86_64 GPU nightlies (CUDA 11.8)
424+
exec: linux-x86_64-gpu
425+
cuda: "11.8.0"
426+
cuda_archs: "60-real;61-real;62-real;70-real;72-real;75-real;80;86-real"
427+
compiler_version: "11.2"
428+
label: nightly
429+
- build_conda:
430+
name: Linux x86_64 GPU w/ RAFT nightlies (CUDA 11.8.0)
431+
exec: linux-x86_64-gpu
432+
raft: "ON"
433+
cuda: "11.8.0"
434+
cuda_archs: "60-real;61-real;62-real;70-real;72-real;75-real;80;86-real"
435+
compiler_version: "11.2"
436+
label: nightly
437+
- build_conda:
438+
name: Linux x86_64 GPU nightlies (CUDA 12.1.0)
420439
exec: linux-x86_64-gpu
421-
cuda: "11.4"
440+
cuda: "12.1.0"
422441
cuda_archs: "60-real;61-real;62-real;70-real;72-real;75-real;80;86-real"
423442
compiler_version: "11.2"
424443
label: nightly
425444
- build_conda:
426-
name: Linux x86_64 GPU w/ RAFT nightlies (CUDA 11.4)
445+
name: Linux x86_64 GPU w/ RAFT nightlies (CUDA 12.1.0)
427446
exec: linux-x86_64-gpu
428447
raft: "ON"
429-
cuda: "11.4"
448+
cuda: "12.1.0"
430449
cuda_archs: "60-real;61-real;62-real;70-real;72-real;75-real;80;86-real"
431450
compiler_version: "11.2"
432451
label: nightly

conda/conda_build_config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ python:
22
- 3.9
33
- 3.10
44
- 3.11
5+
- 3.12

conda/faiss-gpu-raft/meta.yaml

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ outputs:
4444
- cmake >=3.23.1
4545
- make # [not win]
4646
- mkl-devel =2023 # [x86_64]
47+
- cuda-toolkit {{ cudatoolkit }}
48+
- pytorch-cuda
4749
host:
4850
- mkl =2023 # [x86_64]
4951
- openblas # [not x86_64]
50-
- cuda-version {{ cudatoolkit }}
5152
- libraft =24.02
5253
run:
5354
- mkl =2023 # [x86_64]
5455
- openblas # [not x86_64]
55-
- {{ pin_compatible('cuda-version', max_pin='x') }}
56+
- {{ pin_compatible('pytorch-cuda', max_pin='x') }}
5657
- libraft =24.02
5758
test:
5859
requires:
@@ -91,8 +92,6 @@ outputs:
9192
- numpy
9293
- scipy
9394
- pytorch
94-
- pytorch-cuda =11.8
95-
- cuda-version =11.8
9695
commands:
9796
- python -X faulthandler -m unittest discover -v -s tests/ -p "test_*"
9897
- python -X faulthandler -m unittest discover -v -s tests/ -p "torch_*"

conda/faiss-gpu/meta.yaml

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ outputs:
4444
- cmake >=3.23.1
4545
- make # [not win]
4646
- mkl-devel =2023 # [x86_64]
47+
- cuda-toolkit {{ cudatoolkit }}
48+
- pytorch-cuda
4749
host:
4850
- mkl =2023 # [x86_64]
4951
- openblas # [not x86_64]
50-
- cudatoolkit {{ cudatoolkit }}
5152
run:
5253
- mkl =2023 # [x86_64]
5354
- openblas # [not x86_64]
54-
- {{ pin_compatible('cudatoolkit', max_pin='x') }}
55+
- {{ pin_compatible('pytorch-cuda', max_pin='x') }}
5556
test:
5657
requires:
5758
- conda-build
@@ -89,8 +90,6 @@ outputs:
8990
- numpy
9091
- scipy
9192
- pytorch
92-
- pytorch-cuda =11.8
93-
- cudatoolkit =11.8
9493
commands:
9594
- python -X faulthandler -m unittest discover -v -s tests/ -p "test_*"
9695
- python -X faulthandler -m unittest discover -v -s tests/ -p "torch_*"

faiss/gpu/CMakeLists.txt

+17-12
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,29 @@ if(FAISS_ENABLE_RAFT)
244244
list(APPEND FAISS_GPU_SRC
245245
impl/RaftFlatIndex.cu
246246
impl/RaftIVFFlat.cu)
247+
endif()
248+
249+
add_library(faiss_gpu STATIC ${FAISS_GPU_SRC})
250+
set_target_properties(faiss_gpu PROPERTIES
251+
POSITION_INDEPENDENT_CODE ON
252+
WINDOWS_EXPORT_ALL_SYMBOLS ON
253+
)
254+
target_include_directories(faiss_gpu PUBLIC
255+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>)
247256

257+
if(FAISS_ENABLE_RAFT)
248258
target_compile_definitions(faiss PUBLIC USE_NVIDIA_RAFT=1)
249259
target_compile_definitions(faiss_avx2 PUBLIC USE_NVIDIA_RAFT=1)
250260
target_compile_definitions(faiss_avx512 PUBLIC USE_NVIDIA_RAFT=1)
261+
target_compile_definitions(faiss_gpu PUBLIC USE_NVIDIA_RAFT=1)
251262
endif()
252263

253264
# Export FAISS_GPU_HEADERS variable to parent scope.
254265
set(FAISS_GPU_HEADERS ${FAISS_GPU_HEADERS} PARENT_SCOPE)
255266

256-
target_sources(faiss PRIVATE ${FAISS_GPU_SRC})
257-
target_sources(faiss_avx2 PRIVATE ${FAISS_GPU_SRC})
258-
target_sources(faiss_avx512 PRIVATE ${FAISS_GPU_SRC})
267+
target_link_libraries(faiss PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,faiss_gpu>")
268+
target_link_libraries(faiss_avx2 PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,faiss_gpu>")
269+
target_link_libraries(faiss_avx512 PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,faiss_gpu>")
259270

260271
foreach(header ${FAISS_GPU_HEADERS})
261272
get_filename_component(dir ${header} DIRECTORY )
@@ -277,14 +288,8 @@ __nv_relfatbin : { *(__nv_relfatbin) }
277288
}
278289
]=]
279290
)
280-
target_link_options(faiss PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/fatbin.ld")
281-
target_link_options(faiss_avx2 PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/fatbin.ld")
282-
target_link_options(faiss_avx512 PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/fatbin.ld")
291+
target_link_options(faiss_gpu PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/fatbin.ld")
283292

284293
find_package(CUDAToolkit REQUIRED)
285-
target_link_libraries(faiss PRIVATE CUDA::cudart CUDA::cublas $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::raft> $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::compiled> $<$<BOOL:${FAISS_ENABLE_RAFT}>:nvidia::cutlass::cutlass>)
286-
target_link_libraries(faiss_avx2 PRIVATE CUDA::cudart CUDA::cublas $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::raft> $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::compiled> $<$<BOOL:${FAISS_ENABLE_RAFT}>:nvidia::cutlass::cutlass>)
287-
target_link_libraries(faiss_avx512 PRIVATE CUDA::cudart CUDA::cublas $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::raft> $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::compiled> $<$<BOOL:${FAISS_ENABLE_RAFT}>:nvidia::cutlass::cutlass>)
288-
target_compile_options(faiss PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xfatbin=-compress-all --expt-extended-lambda --expt-relaxed-constexpr>)
289-
target_compile_options(faiss_avx2 PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xfatbin=-compress-all --expt-extended-lambda --expt-relaxed-constexpr>)
290-
target_compile_options(faiss_avx512 PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xfatbin=-compress-all --expt-extended-lambda --expt-relaxed-constexpr>)
294+
target_link_libraries(faiss_gpu PRIVATE CUDA::cudart CUDA::cublas $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::raft> $<$<BOOL:${FAISS_ENABLE_RAFT}>:raft::compiled> $<$<BOOL:${FAISS_ENABLE_RAFT}>:nvidia::cutlass::cutlass>)
295+
target_compile_options(faiss_gpu PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xfatbin=-compress-all --expt-extended-lambda --expt-relaxed-constexpr>)

0 commit comments

Comments
 (0)