-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add a Faiss codec for KNN searches #14178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a4056db
e53d1a0
714ea4f
1d0f666
07c7d75
789dda6
76813de
7b22615
b912600
fa27a84
bbb3bf3
fb8f20c
717012b
c69a0ec
ed2e6d4
ee2c811
56f39d3
30a47b6
52a1acd
3e84788
018d15d
9ed228b
f161716
c56dc95
1f38d8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| name: "Run special checks: module lucene/sandbox" | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
|
|
||
| pull_request: | ||
| branches: | ||
| - '*' | ||
|
|
||
| push: | ||
| branches: | ||
| - 'main' | ||
| - 'branch_10x' | ||
|
|
||
| jobs: | ||
| faiss-tests: | ||
| name: tests for the Faiss codec (v${{ matrix.faiss-version }} with JDK ${{ matrix.java }} on ${{ matrix.os }}) | ||
| timeout-minutes: 15 | ||
|
|
||
| strategy: | ||
| matrix: | ||
| os: [ ubuntu-latest ] | ||
| java: [ '24' ] | ||
| faiss-version: [ '1.11.0' ] | ||
|
|
||
| runs-on: ${{ matrix.os }} | ||
|
|
||
| steps: | ||
| - name: Install Mamba | ||
| uses: conda-incubator/setup-miniconda@835234971496cad1653abb28a638a281cf32541f #v3.2.0 | ||
| with: | ||
| miniforge-version: 'latest' | ||
| auto-activate-base: 'false' | ||
| activate-environment: 'faiss-env' | ||
| # TODO: Use only conda-forge if possible, see https://github.com/conda-forge/faiss-split-feedstock/pull/88 | ||
| channels: 'pytorch,conda-forge' | ||
| conda-remove-defaults: 'true' | ||
|
|
||
| - name: Install Faiss | ||
| run: mamba install faiss-cpu=${{ matrix.faiss-version }} | ||
|
|
||
| - name: Checkout Lucene | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
|
|
||
| - name: Prepare Lucene workspace | ||
| uses: ./.github/actions/prepare-for-build | ||
|
|
||
| - name: Run tests for Faiss codec | ||
| run: > | ||
| LD_LIBRARY_PATH=$CONDA_PREFIX/lib | ||
| ./gradlew -p lucene/sandbox | ||
| -Dtests.faiss.run=true | ||
| test | ||
| --tests "org.apache.lucene.sandbox.codecs.faiss.*" | ||
|
|
||
| defaults: | ||
| run: | ||
| shell: bash -leo pipefail {0} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.lucene.sandbox.codecs.faiss; | ||
|
|
||
| import static org.apache.lucene.util.hnsw.HnswGraphBuilder.DEFAULT_BEAM_WIDTH; | ||
| import static org.apache.lucene.util.hnsw.HnswGraphBuilder.DEFAULT_MAX_CONN; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Locale; | ||
| import org.apache.lucene.codecs.KnnVectorsFormat; | ||
| import org.apache.lucene.codecs.KnnVectorsReader; | ||
| import org.apache.lucene.codecs.KnnVectorsWriter; | ||
| import org.apache.lucene.codecs.hnsw.FlatVectorScorerUtil; | ||
| import org.apache.lucene.codecs.hnsw.FlatVectorsFormat; | ||
| import org.apache.lucene.codecs.lucene99.Lucene99FlatVectorsFormat; | ||
| import org.apache.lucene.index.SegmentReadState; | ||
| import org.apache.lucene.index.SegmentWriteState; | ||
|
|
||
| /** | ||
| * A Faiss-based format to create and search vector indexes, using {@link LibFaissC} to interact | ||
| * with the native library. | ||
| * | ||
| * <p>The Faiss index is configured using its flexible <a | ||
| * href="https://github.com/facebookresearch/faiss/wiki/The-index-factory">index factory</a>, which | ||
| * allows creating arbitrary indexes by "describing" them. These indexes can be tuned by <a | ||
| * href="https://github.com/facebookresearch/faiss/wiki/Index-IO,-cloning-and-hyper-parameter-tuning">setting | ||
| * relevant parameters</a>. | ||
| * | ||
| * <p>A separate Faiss index is created per-segment, and uses the following files: | ||
| * | ||
| * <ul> | ||
| * <li><code>.faissm</code> (metadata file): stores field number, offset and length of actual | ||
| * Faiss index in data file. | ||
| * <li><code>.faissd</code> (data file): stores concatenated Faiss indexes for all fields. | ||
| * <li>All files required by {@link Lucene99FlatVectorsFormat} for storing raw vectors. | ||
| * </ul> | ||
| * | ||
| * <p>Note: Set the {@code $OMP_NUM_THREADS} environment variable to control <a | ||
| * href="https://github.com/facebookresearch/faiss/wiki/Threads-and-asynchronous-calls">internal | ||
| * threading</a>. | ||
| * | ||
| * <p>TODO: There is no guarantee of backwards compatibility! | ||
| * | ||
| * @lucene.experimental | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also add a sentence making it clear there is no promise of backwards compatibility here? That said, does Faiss have any promise? If I write a Faiss HNSW graph with version X, and then upgrade Faiss to version X+1, can the HNSW graph be read/written?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do see some references of backwards compatibility, and an old comment which says that newer versions of Faiss can read older indexes -- but I couldn't find documentation for it.. Further, we may change some internals of the codec making it incompatible with earlier versions -- but I'll add a comment saying there's no guarantee today, and a TODO to figure that out |
||
| */ | ||
| public final class FaissKnnVectorsFormat extends KnnVectorsFormat { | ||
| public static final String NAME = FaissKnnVectorsFormat.class.getSimpleName(); | ||
| static final int VERSION_START = 0; | ||
| static final int VERSION_CURRENT = VERSION_START; | ||
| static final String META_CODEC_NAME = NAME + "Meta"; | ||
| static final String DATA_CODEC_NAME = NAME + "Data"; | ||
| static final String META_EXTENSION = "faissm"; | ||
| static final String DATA_EXTENSION = "faissd"; | ||
|
|
||
| private final String description; | ||
| private final String indexParams; | ||
| private final FlatVectorsFormat rawVectorsFormat; | ||
|
|
||
| /** | ||
| * Constructs an HNSW-based format using default {@code maxConn}={@value | ||
| * org.apache.lucene.util.hnsw.HnswGraphBuilder#DEFAULT_MAX_CONN} and {@code beamWidth}={@value | ||
| * org.apache.lucene.util.hnsw.HnswGraphBuilder#DEFAULT_BEAM_WIDTH}. | ||
| */ | ||
| public FaissKnnVectorsFormat() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment somewhere or maybe a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added to |
||
| this( | ||
| String.format(Locale.ROOT, "IDMap,HNSW%d", DEFAULT_MAX_CONN), | ||
| String.format(Locale.ROOT, "efConstruction=%d", DEFAULT_BEAM_WIDTH)); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a format using the specified index factory string and index parameters (see class | ||
| * docs for more information). | ||
| * | ||
| * @param description the index factory string to initialize Faiss indexes. | ||
| * @param indexParams the index params to set on Faiss indexes. | ||
| */ | ||
| public FaissKnnVectorsFormat(String description, String indexParams) { | ||
| super(NAME); | ||
| this.description = description; | ||
| this.indexParams = indexParams; | ||
| this.rawVectorsFormat = | ||
| new Lucene99FlatVectorsFormat(FlatVectorScorerUtil.getLucene99FlatVectorsScorer()); | ||
| } | ||
|
|
||
| @Override | ||
| public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException { | ||
| return new FaissKnnVectorsWriter( | ||
| description, indexParams, state, rawVectorsFormat.fieldsWriter(state)); | ||
| } | ||
|
|
||
| @Override | ||
| public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException { | ||
| return new FaissKnnVectorsReader(state, rawVectorsFormat.fieldsReader(state)); | ||
| } | ||
|
|
||
| @Override | ||
| public int getMaxDimensions(String fieldName) { | ||
| return DEFAULT_MAX_DIMENSIONS; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format( | ||
| Locale.ROOT, "%s(description=%s indexParams=%s)", NAME, description, indexParams); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm add a
CHANGES.txtentry? I think this can be safely backported to 10.x, after we bake for a week or two inmain? It's sandbox, completely separate from everything else, experimental ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added!
+1 -- I don't think it should affect users who won't use it