-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add binary index support for Lucene engine
Signed-off-by: Jay Deng <jayd0104@gmail.com>
- Loading branch information
Showing
13 changed files
with
259 additions
and
20 deletions.
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
107 changes: 107 additions & 0 deletions
107
src/main/java/org/opensearch/knn/index/codec/KNN990Codec/KNN990BinaryVectorScorer.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,107 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.codec.KNN990Codec; | ||
|
||
import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; | ||
import org.apache.lucene.index.VectorSimilarityFunction; | ||
import org.apache.lucene.util.Bits; | ||
import org.apache.lucene.util.VectorUtil; | ||
import org.apache.lucene.util.hnsw.RandomAccessVectorValues; | ||
import org.apache.lucene.util.hnsw.RandomVectorScorer; | ||
import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; | ||
|
||
import java.io.IOException; | ||
|
||
public class KNN990BinaryVectorScorer implements FlatVectorsScorer { | ||
@Override | ||
public RandomVectorScorerSupplier getRandomVectorScorerSupplier( | ||
VectorSimilarityFunction vectorSimilarityFunction, | ||
RandomAccessVectorValues randomAccessVectorValues | ||
) throws IOException { | ||
assert randomAccessVectorValues instanceof RandomAccessVectorValues.Bytes; | ||
if (randomAccessVectorValues instanceof RandomAccessVectorValues.Bytes) { | ||
return new BinaryRandomVectorScorerSupplier((RandomAccessVectorValues.Bytes) randomAccessVectorValues); | ||
} | ||
throw new IllegalArgumentException("vectorValues must be an instance of RandomAccessVectorValues.Bytes"); | ||
} | ||
|
||
@Override | ||
public RandomVectorScorer getRandomVectorScorer( | ||
VectorSimilarityFunction vectorSimilarityFunction, | ||
RandomAccessVectorValues randomAccessVectorValues, | ||
float[] queryVector | ||
) throws IOException { | ||
throw new IllegalArgumentException("binary vectors do not support float[] targets"); | ||
} | ||
|
||
@Override | ||
public RandomVectorScorer getRandomVectorScorer( | ||
VectorSimilarityFunction vectorSimilarityFunction, | ||
RandomAccessVectorValues randomAccessVectorValues, | ||
byte[] queryVector | ||
) throws IOException { | ||
assert randomAccessVectorValues instanceof RandomAccessVectorValues.Bytes; | ||
if (randomAccessVectorValues instanceof RandomAccessVectorValues.Bytes) { | ||
return new BinaryRandomVectorScorer((RandomAccessVectorValues.Bytes) randomAccessVectorValues, queryVector); | ||
} | ||
throw new IllegalArgumentException("vectorValues must be an instance of RandomAccessVectorValues.Bytes"); | ||
} | ||
|
||
static class BinaryRandomVectorScorer implements RandomVectorScorer { | ||
private final RandomAccessVectorValues.Bytes vectorValues; | ||
private final int bitDimensions; | ||
private final byte[] queryVector; | ||
|
||
BinaryRandomVectorScorer(RandomAccessVectorValues.Bytes vectorValues, byte[] query) { | ||
this.queryVector = query; | ||
this.bitDimensions = vectorValues.dimension() * Byte.SIZE; | ||
this.vectorValues = vectorValues; | ||
} | ||
|
||
@Override | ||
public float score(int node) throws IOException { | ||
return (bitDimensions - VectorUtil.xorBitCount(queryVector, vectorValues.vectorValue(node))) / (float) bitDimensions; | ||
} | ||
|
||
@Override | ||
public int maxOrd() { | ||
return vectorValues.size(); | ||
} | ||
|
||
@Override | ||
public int ordToDoc(int ord) { | ||
return vectorValues.ordToDoc(ord); | ||
} | ||
|
||
@Override | ||
public Bits getAcceptOrds(Bits acceptDocs) { | ||
return vectorValues.getAcceptOrds(acceptDocs); | ||
} | ||
} | ||
|
||
static class BinaryRandomVectorScorerSupplier implements RandomVectorScorerSupplier { | ||
protected final RandomAccessVectorValues.Bytes vectorValues; | ||
protected final RandomAccessVectorValues.Bytes vectorValues1; | ||
protected final RandomAccessVectorValues.Bytes vectorValues2; | ||
|
||
public BinaryRandomVectorScorerSupplier(RandomAccessVectorValues.Bytes vectorValues) throws IOException { | ||
this.vectorValues = vectorValues; | ||
this.vectorValues1 = vectorValues.copy(); | ||
this.vectorValues2 = vectorValues.copy(); | ||
} | ||
|
||
@Override | ||
public RandomVectorScorer scorer(int ord) throws IOException { | ||
byte[] queryVector = vectorValues1.vectorValue(ord); | ||
return new BinaryRandomVectorScorer(vectorValues2, queryVector); | ||
} | ||
|
||
@Override | ||
public RandomVectorScorerSupplier copy() throws IOException { | ||
return new BinaryRandomVectorScorerSupplier(vectorValues.copy()); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/org/opensearch/knn/index/codec/KNN990Codec/KNN990HnswBinaryVectorsFormat.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,97 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.codec.KNN990Codec; | ||
|
||
import org.apache.lucene.codecs.KnnVectorsFormat; | ||
import org.apache.lucene.codecs.KnnVectorsReader; | ||
import org.apache.lucene.codecs.KnnVectorsWriter; | ||
import org.apache.lucene.codecs.hnsw.FlatVectorsFormat; | ||
import org.apache.lucene.codecs.lucene99.Lucene99FlatVectorsFormat; | ||
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsReader; | ||
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsWriter; | ||
import org.apache.lucene.index.SegmentReadState; | ||
import org.apache.lucene.index.SegmentWriteState; | ||
import org.apache.lucene.search.TaskExecutor; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
public class KNN990HnswBinaryVectorsFormat extends KnnVectorsFormat { | ||
|
||
private final int maxConn; | ||
private final int beamWidth; | ||
private static final FlatVectorsFormat flatVectorsFormat = new Lucene99FlatVectorsFormat(new KNN990BinaryVectorScorer()); | ||
private final int numMergeWorkers; | ||
private final TaskExecutor mergeExec; | ||
|
||
private static final String NAME = "KNN990HnswBinaryVectorsFormat"; | ||
|
||
public KNN990HnswBinaryVectorsFormat() { | ||
this(16, 100, 1, (ExecutorService) null); | ||
} | ||
|
||
public KNN990HnswBinaryVectorsFormat(int maxConn, int beamWidth) { | ||
this(maxConn, beamWidth, 1, (ExecutorService) null); | ||
} | ||
|
||
public KNN990HnswBinaryVectorsFormat(int maxConn, int beamWidth, int numMergeWorkers, ExecutorService mergeExec) { | ||
super(NAME); | ||
if (maxConn > 0 && maxConn <= 512) { | ||
if (beamWidth > 0 && beamWidth <= 3200) { | ||
this.maxConn = maxConn; | ||
this.beamWidth = beamWidth; | ||
if (numMergeWorkers == 1 && mergeExec != null) { | ||
throw new IllegalArgumentException("No executor service is needed as we'll use single thread to merge"); | ||
} else { | ||
this.numMergeWorkers = numMergeWorkers; | ||
if (mergeExec != null) { | ||
this.mergeExec = new TaskExecutor(mergeExec); | ||
} else { | ||
this.mergeExec = null; | ||
} | ||
|
||
} | ||
} else { | ||
throw new IllegalArgumentException("beamWidth must be positive and less than or equal to 3200; beamWidth=" + beamWidth); | ||
} | ||
} else { | ||
throw new IllegalArgumentException("maxConn must be positive and less than or equal to 512; maxConn=" + maxConn); | ||
} | ||
} | ||
|
||
@Override | ||
public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException { | ||
return new Lucene99HnswVectorsWriter( | ||
state, | ||
this.maxConn, | ||
this.beamWidth, | ||
flatVectorsFormat.fieldsWriter(state), | ||
this.numMergeWorkers, | ||
this.mergeExec | ||
); | ||
} | ||
|
||
@Override | ||
public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException { | ||
return new Lucene99HnswVectorsReader(state, flatVectorsFormat.fieldsReader(state)); | ||
} | ||
|
||
@Override | ||
public int getMaxDimensions(String fieldName) { | ||
return 1024; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "KNN990HnswBinaryVectorsFormat(name=KNN990HnswBinaryVectorsFormat, maxConn=" | ||
+ this.maxConn | ||
+ ", beamWidth=" | ||
+ this.beamWidth | ||
+ ", flatVectorFormat=" | ||
+ flatVectorsFormat | ||
+ ")"; | ||
} | ||
} |
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
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
Oops, something went wrong.