-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 2 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,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); | ||
| } | ||
| } |
| 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| public FaissKnnVectorsFormat() { | ||
| this("HNSW32", "efConstruction=200"); | ||
| } | ||
|
|
||
| public FaissKnnVectorsFormat(String description, String indexParams) { | ||
| super(NAME); | ||
| this.description = description; | ||
| this.indexParams = indexParams; | ||
| this.rawVectorsFormat = | ||
| new Lucene99FlatVectorsFormat(FlatVectorScorerUtil.getLucene99FlatVectorsScorer()); | ||
|
Comment on lines
+50
to
+
Contributor
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. since Faiss already stores this information in the index what is the reason to have a RawFlatVectorsFormat?
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. 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)
Contributor
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.
Ref I myself have not explored it much to be really honest, but I was following up on this PR for some time. Here is the PR: facebookresearch/faiss#3487 for more details. Just FYI I am not against having flatvectors in both faiss index and also in lucene. |
||
| } | ||
|
|
||
| @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.
Oh this is maybe the right place to put some nice docs about how to build this?
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.
Makes sense, added!