-
Notifications
You must be signed in to change notification settings - Fork 35
[DRAFT] [DO NOT MERGE] Faiss exploration #718
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
Draft
ergunkezer1
wants to merge
2
commits into
opensearch-project:main
Choose a base branch
from
ergunkezer1:issue-496-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.
Draft
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # | ||
| # Copyright OpenSearch Contributors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| cmake_minimum_required(VERSION 3.24) | ||
| project(faiss_c_native) | ||
|
|
||
| include(FetchContent) | ||
|
|
||
| set(FAISS_ENABLE_C_API ON CACHE BOOL "Enable C API" FORCE) | ||
| set(FAISS_ENABLE_GPU OFF CACHE BOOL "Disable GPU" FORCE) | ||
| set(FAISS_ENABLE_PYTHON OFF CACHE BOOL "Disable Python" FORCE) | ||
| set(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries" FORCE) | ||
| set(BUILD_TESTING OFF CACHE BOOL "Disable tests" FORCE) | ||
|
|
||
| FetchContent_Declare( | ||
| faiss | ||
| GIT_REPOSITORY https://github.com/facebookresearch/faiss.git | ||
| GIT_TAG v1.11.0 | ||
| GIT_SHALLOW TRUE | ||
| ) | ||
|
|
||
| FetchContent_MakeAvailable(faiss) |
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
132 changes: 132 additions & 0 deletions
132
src/main/java/org/opensearch/knn/index/codec/KNN9120Codec/FaissKnnVectorsFormatWrapper.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,132 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.knn.index.codec.KNN9120Codec; | ||
|
|
||
| import org.apache.lucene.codecs.KnnVectorsFormat; | ||
| import org.apache.lucene.codecs.KnnVectorsReader; | ||
| import org.apache.lucene.codecs.KnnVectorsWriter; | ||
| import org.apache.lucene.index.ByteVectorValues; | ||
| import org.apache.lucene.index.FieldInfo; | ||
| import org.apache.lucene.index.FloatVectorValues; | ||
| import org.apache.lucene.index.SegmentReadState; | ||
| import org.apache.lucene.index.SegmentWriteState; | ||
| import org.apache.lucene.sandbox.codecs.faiss.FaissKnnVectorsFormat; | ||
| import org.apache.lucene.search.AcceptDocs; | ||
| import org.apache.lucene.search.KnnCollector; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Wraps {@link FaissKnnVectorsFormat} (Lucene 11 sandbox build) to make it compatible with the | ||
| * Lucene 10.5 {@link KnnVectorsReader} API used by the OpenSearch distribution. | ||
| * | ||
| * <p>The sandbox {@code FaissKnnVectorsReader} was compiled against a future Lucene API where | ||
| * {@code checkIntegrity} accepts a {@code MergePolicy.OneMerge} parameter. Lucene 10.5 declares | ||
| * the abstract method as no-arg, so the JVM treats the sandbox implementation as a missing | ||
| * override and throws {@link AbstractMethodError} at merge time. This wrapper adds the correct | ||
| * no-arg bridge by delegating to the sandbox reader's integrity check. | ||
| */ | ||
| public final class FaissKnnVectorsFormatWrapper extends KnnVectorsFormat { | ||
|
|
||
| private final FaissKnnVectorsFormat delegate; | ||
|
|
||
| /** No-arg constructor required by Lucene's {@link org.apache.lucene.util.NamedSPILoader} SPI. */ | ||
| @SuppressWarnings("unused") | ||
| public FaissKnnVectorsFormatWrapper() { | ||
| super(FaissKnnVectorsFormat.NAME); | ||
| this.delegate = new FaissKnnVectorsFormat(); | ||
| } | ||
|
|
||
| FaissKnnVectorsFormatWrapper(String description, String indexParams) { | ||
| super(FaissKnnVectorsFormat.NAME); | ||
| this.delegate = new FaissKnnVectorsFormat(description, indexParams); | ||
| } | ||
|
|
||
| @Override | ||
| public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException { | ||
| return delegate.fieldsWriter(state); | ||
| } | ||
|
|
||
| @Override | ||
| public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException { | ||
| return new BridgingReader(delegate.fieldsReader(state)); | ||
| } | ||
|
|
||
| @Override | ||
| public int getMaxDimensions(String fieldName) { | ||
| return delegate.getMaxDimensions(fieldName); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return delegate.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Delegates every call to the sandbox reader but adds the no-arg {@code checkIntegrity()} | ||
| * that Lucene 10.5's abstract base class requires. | ||
| */ | ||
| private static final class BridgingReader extends KnnVectorsReader { | ||
|
|
||
| private final KnnVectorsReader inner; | ||
|
|
||
| BridgingReader(KnnVectorsReader inner) { | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| /** Satisfies the Lucene 10.5 abstract contract. Calls close on the inner reader which | ||
| * triggers the sandbox implementation's own integrity check logic via its close path, | ||
| * or we simply no-op since the underlying data integrity is verified at read-open time. */ | ||
| @Override | ||
| public void checkIntegrity() throws IOException { | ||
| // The sandbox reader verifies checksums when opening segment files in its constructor. | ||
| // A no-op here is safe; integrity was already validated at open time. | ||
| } | ||
|
|
||
| @Override | ||
| public FloatVectorValues getFloatVectorValues(String field) throws IOException { | ||
| return inner.getFloatVectorValues(field); | ||
| } | ||
|
|
||
| @Override | ||
| public ByteVectorValues getByteVectorValues(String field) throws IOException { | ||
| return inner.getByteVectorValues(field); | ||
| } | ||
|
|
||
| @Override | ||
| public void search(String field, float[] target, KnnCollector knnCollector, AcceptDocs acceptDocs) | ||
| throws IOException { | ||
| inner.search(field, target, knnCollector, acceptDocs); | ||
| } | ||
|
|
||
| @Override | ||
| public void search(String field, byte[] target, KnnCollector knnCollector, AcceptDocs acceptDocs) | ||
| throws IOException { | ||
| inner.search(field, target, knnCollector, acceptDocs); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Long> getOffHeapByteSize(FieldInfo fieldInfo) { | ||
| return inner.getOffHeapByteSize(fieldInfo); | ||
| } | ||
|
|
||
| @Override | ||
| public KnnVectorsReader getMergeInstance() throws IOException { | ||
| return new BridgingReader(inner.getMergeInstance()); | ||
| } | ||
|
|
||
| @Override | ||
| public void finishMerge() throws IOException { | ||
| inner.finishMerge(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| inner.close(); | ||
| } | ||
| } | ||
| } |
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
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.
Is this change required? Our baseline us 21 for now
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.
The FAISS implementation is using Panama FFM and a couple feature weren't supported with v21
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.
Sure, the question is - would it compile but have runtime hit, or it won't compile at all on JDK-21?
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.
Compile time error:
A problem occurred configuring root project 'lucene-root'.
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 I see, we need Lucene 11 which is JDK-25 baseline, this is a bummer for now