-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Refactor Faiss-based vector format for easier backport #14934
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 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 hidden or 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 hidden or 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 hidden or 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
83 changes: 83 additions & 0 deletions
83
lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/faiss/FaissLibrary.java
This file contains hidden or 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,83 @@ | ||
| /* | ||
| * 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.Closeable; | ||
| import java.lang.invoke.MethodHandle; | ||
| import java.lang.invoke.MethodHandles; | ||
| import java.lang.invoke.MethodType; | ||
| import org.apache.lucene.index.FloatVectorValues; | ||
| import org.apache.lucene.index.VectorSimilarityFunction; | ||
| import org.apache.lucene.search.KnnCollector; | ||
| import org.apache.lucene.store.IndexInput; | ||
| import org.apache.lucene.store.IndexOutput; | ||
| import org.apache.lucene.util.Bits; | ||
| import org.apache.lucene.util.hnsw.IntToIntFunction; | ||
|
|
||
| /** | ||
| * Minimal interface to create and query Faiss indexes. | ||
| * | ||
| * @lucene.experimental | ||
| */ | ||
| interface FaissLibrary { | ||
| FaissLibrary INSTANCE = lookup(); | ||
|
|
||
| // TODO: Use vectorized version where available | ||
| String NAME = "faiss_c"; | ||
| String VERSION = "1.11.0"; | ||
|
|
||
| private static FaissLibrary lookup() { | ||
|
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. Why do we have that dynamic code in main branch? Please replace by a simple
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. Makes sense, updated |
||
| final MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
|
|
||
| final Class<?> cls; | ||
| try { | ||
| cls = lookup.findClass("org.apache.lucene.sandbox.codecs.faiss.FaissLibraryNativeImpl"); | ||
| } catch (ClassNotFoundException | IllegalAccessException e) { | ||
| throw new LinkageError("FaissLibraryNativeImpl class is missing or inaccessible", e); | ||
| } | ||
|
|
||
| final MethodHandle constr; | ||
| try { | ||
| constr = lookup.findConstructor(cls, MethodType.methodType(void.class)); | ||
| } catch (NoSuchMethodException | IllegalAccessException e) { | ||
| throw new LinkageError("FaissLibraryNativeImpl constructor is missing or inaccessible", e); | ||
| } | ||
|
|
||
| try { | ||
| return (FaissLibrary) constr.invoke(); | ||
| } catch (RuntimeException | Error e) { | ||
| throw e; | ||
| } catch (Throwable t) { | ||
| throw new AssertionError("Should not throw checked exceptions", t); | ||
| } | ||
| } | ||
|
|
||
| interface Index extends Closeable { | ||
| void search(float[] query, KnnCollector knnCollector, Bits acceptDocs); | ||
|
|
||
| void write(IndexOutput output); | ||
| } | ||
|
|
||
| Index createIndex( | ||
| String description, | ||
| String indexParams, | ||
| VectorSimilarityFunction function, | ||
| FloatVectorValues floatVectorValues, | ||
| IntToIntFunction oldToNewDocId); | ||
|
|
||
| Index readIndex(IndexInput input); | ||
| } | ||
Oops, something went wrong.
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.
Hmm what does this
TODOmean again? Is "vectorized" meaning "SIMD instructions"? This term (vector) is overloaded! https://youtu.be/fVq4_HhBK8YMaybe clarify to
// TODO: use SIMD Faiss API versions where availableor so (if that's what it really means)? I think this is necessary because the Faiss build process will produce specific dynamic library for specific SIMD targets (AVX-512 vs AVX-128 etc.)?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.
Yes sorry, I meant using SIMD instructions wherever available..
Today, the shared library of Faiss' C API (
libfaiss_c.so) is linked to the non-SIMD version of the base library (libfaiss.so) by default, but a user can still "point" to the correct SIMD version by changing its dependencies using:# patchelf --replace-needed OLD_DEPENDENCY NEW_DEPENDENCY SHARED_LIBRARY patchelf --replace-needed libfaiss.so libfaiss_{avx2,avx512,sve}.so libfaiss_c.soHowever, we'd ideally want to do this automatically (either propose a change to upstream Faiss, or something else from Lucene) -- but I wasn't sure how to do it right now..
I'll update the comment soon!