Skip to content

Commit 5c2b0b7

Browse files
mdouzefacebook-github-bot
authored andcommitted
Enable dynamic dispatch build on Windows (#5127)
Summary: Attempt to enable FAISS dynamic dispatch (DD) mode on Windows/MSVC. Changes: - CMakeLists.txt: Remove if(NOT WIN32) guard from DD section, add MSVC per-file SIMD flags (/arch:AVX2, /arch:AVX512, /bigobj) alongside existing GCC/Clang flags - build-pull-request.yml: Add windows-x86_64-DD-cmake job that runs immediately (no dependency on linux-x86_64-cmake), builds with MSVC and FAISS_OPT_LEVEL=dd, runs C++ tests and Python tests This diff is expected to fail on Windows due to MSVC requiring explicit template specialization declarations (C++ §17.8.3) which GCC/Clang don't enforce. The CI failure will surface the exact errors to guide the fix. Differential Revision: D101649751
1 parent 9cbc8da commit 5c2b0b7

3 files changed

Lines changed: 75 additions & 16 deletions

File tree

.github/workflows/build-pull-request.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,54 @@ jobs:
170170
fetch-tags: true
171171
- name: Build and Package (conda)
172172
uses: ./.github/actions/build_conda
173+
windows-x86_64-DD-cmake:
174+
name: Windows x86_64 Dynamic Dispatch (cmake)
175+
runs-on: windows-2022
176+
steps:
177+
- name: Checkout
178+
uses: actions/checkout@v4
179+
- name: Setup miniconda
180+
uses: conda-incubator/setup-miniconda@v3
181+
with:
182+
python-version: '3.12'
183+
miniforge-version: latest
184+
channels: conda-forge
185+
- name: Install dependencies
186+
shell: pwsh
187+
run: |
188+
conda install -y -q cmake swig "numpy>=2.0,<3.0" scipy pytest gflags setuptools
189+
conda install -y -q mkl=2024.2.2 mkl-devel=2024.2.2
190+
conda install -y -q "pytorch<2.5" -c pytorch
191+
- name: Build
192+
shell: pwsh
193+
run: |
194+
cmake -B build -T v143 -A x64 -G "Visual Studio 17 2022" `
195+
-DBUILD_TESTING=ON `
196+
-DBUILD_SHARED_LIBS=ON `
197+
-DFAISS_ENABLE_GPU=OFF `
198+
-DFAISS_OPT_LEVEL=dd `
199+
-DFAISS_ENABLE_PYTHON=ON `
200+
-DFAISS_ENABLE_C_API=ON `
201+
-DPYTHON_EXECUTABLE="$env:CONDA\python.exe" `
202+
-DBLA_VENDOR=Intel10_64_dyn `
203+
-DCMAKE_BUILD_TYPE=Release `
204+
.
205+
cmake --build build --config Release -j $env:NUMBER_OF_PROCESSORS
206+
- name: C++ tests
207+
shell: pwsh
208+
run: |
209+
cd build
210+
ctest --output-on-failure -C Release
211+
- name: Install Python extension
212+
shell: pwsh
213+
working-directory: build/faiss/python
214+
run: |
215+
& "$env:CONDA\python.exe" setup.py install
216+
- name: Python tests
217+
shell: pwsh
218+
run: |
219+
& "$env:CONDA\python.exe" -m pytest tests/test_*.py
220+
& "$env:CONDA\python.exe" -m pytest tests/torch_*.py
173221
windows-x86_64-conda:
174222
name: Windows x86_64 (conda)
175223
needs: linux-x86_64-cmake

faiss/CMakeLists.txt

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,16 @@ target_compile_definitions(faiss_sve PRIVATE COMPILE_SIMD_ARM_NEON COMPILE_SIMD_
452452
if(FAISS_OPT_LEVEL STREQUAL "dd")
453453
# Add SIMD source files to main faiss target for DD builds
454454
target_sources(faiss PRIVATE ${FAISS_SIMD_SRC})
455-
if(NOT WIN32)
456-
target_compile_definitions(faiss PRIVATE FAISS_ENABLE_DD)
457-
# Architecture-specific SIMD definitions for Dynamic Dispatch
458-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64|amd64|AMD64)")
459-
target_compile_definitions(faiss PRIVATE
460-
COMPILE_SIMD_AVX2 COMPILE_SIMD_AVX512 COMPILE_SIMD_AVX512_SPR)
455+
target_compile_definitions(faiss PRIVATE FAISS_ENABLE_DD)
456+
# Architecture-specific SIMD definitions for Dynamic Dispatch
457+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64|amd64|AMD64)")
458+
target_compile_definitions(faiss PRIVATE
459+
COMPILE_SIMD_AVX2 COMPILE_SIMD_AVX512 COMPILE_SIMD_AVX512_SPR)
460+
if(NOT WIN32)
461461
# Baseline flags for common files (prevents auto-vectorization)
462462
target_compile_options(faiss PRIVATE
463463
$<$<COMPILE_LANGUAGE:CXX>:-mpopcnt -msse4 -mno-avx -mno-avx2>)
464-
# Per-file SIMD flags
464+
# Per-file SIMD flags (GCC/Clang)
465465
set_source_files_properties(${FAISS_SIMD_AVX2_SRC}
466466
TARGET_DIRECTORY faiss
467467
PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c;-mpopcnt"
@@ -471,15 +471,26 @@ if(FAISS_OPT_LEVEL STREQUAL "dd")
471471
PROPERTIES COMPILE_OPTIONS
472472
"-mavx512f;-mavx512cd;-mavx512vl;-mavx512dq;-mavx512bw;-mfma;-mf16c;-mpopcnt"
473473
)
474-
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(aarch64|arm64|ARM64)")
475-
# ARM NEON is always available on aarch64, no special compiler flags needed
476-
target_compile_definitions(faiss PRIVATE COMPILE_SIMD_ARM_NEON COMPILE_SIMD_ARM_SVE)
477-
# Per-file SVE flags (NEON needs no flags on aarch64)
478-
set_source_files_properties(${FAISS_SIMD_SVE_SRC}
474+
else()
475+
# Per-file SIMD flags (MSVC)
476+
add_compile_options(/bigobj)
477+
set_source_files_properties(${FAISS_SIMD_AVX2_SRC}
478+
TARGET_DIRECTORY faiss
479+
PROPERTIES COMPILE_OPTIONS "/arch:AVX2"
480+
)
481+
set_source_files_properties(${FAISS_SIMD_AVX512_SRC}
479482
TARGET_DIRECTORY faiss
480-
PROPERTIES COMPILE_OPTIONS "-march=armv8.2-a+sve"
483+
PROPERTIES COMPILE_OPTIONS "/arch:AVX512"
481484
)
482485
endif()
486+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(aarch64|arm64|ARM64)")
487+
# ARM NEON is always available on aarch64, no special compiler flags needed
488+
target_compile_definitions(faiss PRIVATE COMPILE_SIMD_ARM_NEON COMPILE_SIMD_ARM_SVE)
489+
# Per-file SVE flags (NEON needs no flags on aarch64)
490+
set_source_files_properties(${FAISS_SIMD_SVE_SRC}
491+
TARGET_DIRECTORY faiss
492+
PROPERTIES COMPILE_OPTIONS "-march=armv8.2-a+sve"
493+
)
483494
endif()
484495
endif()
485496

faiss/utils/distances_fused/avx512.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void kernel(
7878
const float* const __restrict xd_0 = x + i * DIM;
7979

8080
// prefetch the next point
81-
_mm_prefetch(xd_0 + DIM * sizeof(float), _MM_HINT_NTA);
81+
_mm_prefetch((char*)(xd_0 + DIM * sizeof(float)), _MM_HINT_NTA);
8282

8383
// load a single point from x
8484
// load -2 * value
@@ -262,10 +262,10 @@ void exhaustive_L2sqr_fused_cmax(
262262
}
263263
}
264264

265-
const size_t nx_p = (nx / NX_POINTS_PER_LOOP) * NX_POINTS_PER_LOOP;
265+
const idx_t nx_p = (nx / NX_POINTS_PER_LOOP) * NX_POINTS_PER_LOOP;
266266
// the main loop.
267267
#pragma omp parallel for schedule(dynamic)
268-
for (size_t i = 0; i < nx_p; i += NX_POINTS_PER_LOOP) {
268+
for (idx_t i = 0; i < nx_p; i += NX_POINTS_PER_LOOP) {
269269
kernel<DIM, NX_POINTS_PER_LOOP, NY_POINTS_PER_LOOP>(
270270
x, y, y_transposed.data(), ny, res, y_norms, i);
271271
}

0 commit comments

Comments
 (0)