-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add a Faiss codec for KNN searches #14178
Open
kaivalnp
wants to merge
3
commits into
apache:main
Choose a base branch
from
kaivalnp:faiss
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+890
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...andbox/src/java/org/apache/lucene/sandbox/codecs/faiss/FaissKnnVectorsFormatProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.util.Arrays; | ||
import java.util.logging.Logger; | ||
import org.apache.lucene.codecs.KnnVectorsFormat; | ||
import org.apache.lucene.codecs.KnnVectorsReader; | ||
import org.apache.lucene.codecs.KnnVectorsWriter; | ||
import org.apache.lucene.index.SegmentReadState; | ||
import org.apache.lucene.index.SegmentWriteState; | ||
|
||
/** | ||
* Wraps <a href="https://github.com/facebookresearch/faiss">Faiss</a> to create and search vector | ||
* indexes. This class is mainly for backwards compatibility with older versions of Java (<22), | ||
* use underlying format directly after upgrade. | ||
* | ||
* @lucene.experimental | ||
*/ | ||
public class FaissKnnVectorsFormatProvider extends KnnVectorsFormat { | ||
private final KnnVectorsFormat delegate; | ||
|
||
public FaissKnnVectorsFormatProvider() { | ||
this(new Object[0]); | ||
} | ||
|
||
public FaissKnnVectorsFormatProvider(Object... args) { | ||
super(FaissKnnVectorsFormatProvider.class.getSimpleName()); | ||
|
||
KnnVectorsFormat delegate; | ||
try { | ||
Class<?> cls = | ||
MethodHandles.lookup() | ||
.findClass("org.apache.lucene.sandbox.codecs.faiss.FaissKnnVectorsFormat"); | ||
|
||
MethodType methodType = | ||
MethodType.methodType( | ||
void.class, Arrays.stream(args).map(Object::getClass).toArray(Class<?>[]::new)); | ||
|
||
delegate = | ||
(KnnVectorsFormat) | ||
MethodHandles.lookup().findConstructor(cls, methodType).invokeWithArguments(args); | ||
|
||
} catch ( | ||
@SuppressWarnings("unused") | ||
ClassNotFoundException e) { | ||
|
||
delegate = null; | ||
Logger.getLogger(getClass().getName()) | ||
.warning("FaissKnnVectorsFormat class missing, this object is unusable!"); | ||
|
||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
throw new LinkageError("FaissKnnVectorsFormat is missing correctly typed constructor", e); | ||
} catch (Throwable t) { | ||
throw new RuntimeException(t); | ||
} | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException { | ||
return delegate.fieldsWriter(state); | ||
} | ||
|
||
@Override | ||
public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException { | ||
return delegate.fieldsReader(state); | ||
} | ||
|
||
@Override | ||
public int getMaxDimensions(String fieldName) { | ||
return delegate.getMaxDimensions(fieldName); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/faiss/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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. | ||
*/ | ||
/** | ||
* Wraps <a href="https://github.com/facebookresearch/faiss">Faiss</a> to create and search vector | ||
* indexes via {@link org.apache.lucene.sandbox.codecs.faiss.FaissKnnVectorsFormatProvider}. | ||
*/ | ||
package org.apache.lucene.sandbox.codecs.faiss; |
76 changes: 76 additions & 0 deletions
76
lucene/sandbox/src/java22/org/apache/lucene/sandbox/codecs/faiss/FaissKnnVectorsFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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 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.lucene99.Lucene99FlatVectorsFormat; | ||
import org.apache.lucene.index.SegmentReadState; | ||
import org.apache.lucene.index.SegmentWriteState; | ||
|
||
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 KnnVectorsFormat rawVectorsFormat; | ||
|
||
@SuppressWarnings("unused") | ||
public FaissKnnVectorsFormat() { | ||
this("IDMap,HNSW32", "efConstruction=200"); | ||
} | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
since Faiss already stores this information in the index what is the reason to have a RawFlatVectorsFormat?
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.
Not all Faiss indexes store the original vectors (eg PQ) -- and trying to "reconstruct" vectors may be lossy..
This primarily affects merges, where vectors in smaller segments are read back to create a fresh one -- so we'd keep losing information with each merge
In the ideal scenario we could use Faiss if the index stores full vectors (eg HNSWFlat), and only add raw vectors to Lucene for other indexes. For now I wasn't 100% sure on how to determine this, so I'm storing them in Lucene in all cases
Note that these would only be loaded into memory during indexing, and not search (they aren't accessed by Faiss)
Another point to note is that reading back vectors from Faiss has its own cost (latency if we read them one-by-one, or memory in case of a bulk read and we may need some sort of batching)