Add Metal GPU backend for Apple Silicon (IndexFlat) #5144
Conversation
|
@trang-nm-nguyen has imported this pull request. If you are a Meta employee, you can view this in D102284936. |
|
Thanks for this contribution! Github Actions supports apple silicon. Let's add a new Github Actions runner with We can add to build pull request, similar to this block: faiss/.github/workflows/build-pull-request.yml Lines 88 to 98 in c627334 |
|
@mnorris11 Thank you for taking a look at my PR, I have added a new Github Actions Runner for macos-15 in my latest commit |
| } | ||
|
|
||
| void MetalIndexFlat::add_with_ids(idx_t n, const float* x, const idx_t* xids) { | ||
| if (n == 0) { |
There was a problem hiding this comment.
We can just FAISS_THROW_IF_NOT_MSG(!ids, "add_with_ids not supported"); to start with, to match existing behavior. Right now copyTo doesn't preserve the ids.
| cmake -B build \ | ||
| -DFAISS_ENABLE_METAL=ON \ | ||
| -DFAISS_ENABLE_GPU=OFF \ | ||
| -DFAISS_ENABLE_PYTHON=OFF \ | ||
| -DBUILD_TESTING=ON \ | ||
| -DCMAKE_BUILD_TYPE=Release \ | ||
| -DCMAKE_PREFIX_PATH="$(brew --prefix libomp)" \ | ||
| . | ||
| cmake --build build --target faiss_metal TestMetalIndexFlat -j$(sysctl -n hw.logicalcpu) | ||
| - name: Test | ||
| run: cd build && ctest -R TestMetalIndexFlat --output-on-failure |
There was a problem hiding this comment.
Can we use the existing file ./.github/actions/build_cmake and update it with any required commands?
|
@mnorris11 I addressed your comments in the latest commits, I replaced |
@Evandabest you might need to update the cmake a bit, looks like an error: |
af374fe to
503e84d
Compare
@mnorris11 Thank you for bringing that up, I added gflags to the brew install |
|
Thanks for the PR. |
| // @lint-ignore-every LICENSELINT | ||
| /** | ||
| * Copyright (c) Meta Platforms, 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. | ||
| * | ||
| * Unified name for flat GPU index when Metal backend is built. | ||
| * Include this when using the Metal backend for API parity with | ||
| * faiss::gpu::GpuIndexFlat. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <faiss/gpu_metal/MetalIndexFlat.h> | ||
|
|
||
| namespace faiss { | ||
|
|
||
| /// When FAISS is built with Metal backend, GpuIndexFlat is MetalIndexFlat. | ||
| using GpuIndexFlat = gpu_metal::MetalIndexFlat; | ||
|
|
||
| } // namespace faiss |
There was a problem hiding this comment.
Team discussed this PR: Let's remove the GpuIndexFlat file in this PR. AMD ROCm is based on CUDA, so it makes sense to share the GpuIndexFlat with NVIDIA for classic Faiss GPU (non-cuVS), but Metal will be a different implementation, so users can simply use MetalIndexFlat.
There was a problem hiding this comment.
Thank you guys for reviewing this. I have removed GpuIndexFlat file from this pr. Makes sense since Metal's implementation is separate from Cuda/ROCm, will keep this in mind for future implementations (MetalIndexIVFFlat, MetalIndexIVFPQ, etc.)
7081695 to
78997b6
Compare
78997b6 to
7081695
Compare
|
@mnorris11 I am failing some checks after pushing my changes. after removing the |
No worries about the internal linter, I can just format that before we merge. |
|
@mnorris11 merged this pull request in 66cea52. |
Summary
Adds a new Metal compute backend so FAISS can run GPU-accelerated similarity search on Apple Silicon (M1/M2/M3/M4/M5). This is just an initial pr, it introduces the build system integration, core Metal infrastructure, and a working
IndexFlatwith L2 and inner product search. I have some follow up prs with more functionality that I plan to submit.Addresses #2386 — FAISS currently has no GPU acceleration path on Apple Silicon, forcing CPU-only execution. This Metal backend enables GPU-accelerated search on M-series chips, with follow-up PRs adding IVF indexes, and kernel optimizations.
All Metal code lives in a new
faiss/gpu_metal/directory, keepingfaiss/gpu/(CUDA/ROCm) completely untouched. The backend is opt-in viacmake -DFAISS_ENABLE_METAL=ON -DFAISS_ENABLE_GPU=OFF.What's included
FAISS_ENABLE_METALCMake option. When enabled, requires Apple platform, links Metal/MetalKit/MPS/Foundation frameworks, and buildslibfaiss_metal.a. Three small additions to rootCMakeLists.txt.MTLDevice,MTLCommandQueue, and buffer allocation/deallocation. MirrorsGpuResourcesroles.GpuIndex). Single-device only (device 0).MTLBuffer. Supportsadd,add_with_ids,reset,searchfor bothMETRIC_L2andMETRIC_INNER_PRODUCT.l2_squared_matrix,ip_matrix(distance computation), andtopk(sorted insertion, k ≤ 256). Embedded as a compile-time string inMetalFlatKernels.mm.get_num_gpus(),index_cpu_to_metal_gpu(),index_metal_gpu_to_cpu(),StandardMetalResources, and aGpuIndexFlattypedef for API parity.TestMetalIndexFlat.mm— L2/IP search correctness vs CPUIndexFlat,add_with_ids, reset, empty search,get_num_gpus, and CPU↔Metal cloning in both directions. All tests skip gracefully if no Metal device is available.Planned follow up prs
Build and test