-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Non-uniform vector quantization (#374)
Adds support for Non-uniform Vector Quantization (NVQ, pronounced as "new vec"). This new technique quantizes the values in each vector with high accuracy by first applying a nonlinear transformation that is individually fit to each vector. These nonlinearities are designed to be lightweight and have a negligible impact on distance computation performance. --------- Co-authored-by: Joel Knighton <[email protected]> Co-authored-by: Ted Willke <[email protected]>
- Loading branch information
1 parent
431538e
commit e8d5c3c
Showing
47 changed files
with
2,810 additions
and
87 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
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
104 changes: 104 additions & 0 deletions
104
jvector-base/src/main/java/io/github/jbellis/jvector/graph/disk/NVQ.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,104 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed 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 io.github.jbellis.jvector.graph.disk; | ||
|
||
import io.github.jbellis.jvector.disk.RandomAccessReader; | ||
import io.github.jbellis.jvector.graph.similarity.ScoreFunction; | ||
import io.github.jbellis.jvector.quantization.NVQScorer; | ||
import io.github.jbellis.jvector.quantization.NVQuantization; | ||
import io.github.jbellis.jvector.quantization.NVQuantization.QuantizedVector; | ||
import io.github.jbellis.jvector.vector.VectorSimilarityFunction; | ||
import io.github.jbellis.jvector.vector.types.VectorFloat; | ||
|
||
import java.io.DataOutput; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
/** | ||
* Implements the storage of NuVeQ vectors in an on-disk graph index. These can be used for reranking. | ||
*/ | ||
public class NVQ implements Feature { | ||
private final NVQuantization nvq; | ||
private final NVQScorer scorer; | ||
private final ThreadLocal<QuantizedVector> reusableQuantizedVector; | ||
|
||
public NVQ(NVQuantization nvq) { | ||
this.nvq = nvq; | ||
scorer = new NVQScorer(this.nvq); | ||
reusableQuantizedVector = ThreadLocal.withInitial(() -> NVQuantization.QuantizedVector.createEmpty(nvq.subvectorSizesAndOffsets, nvq.bitsPerDimension)); | ||
} | ||
|
||
@Override | ||
public FeatureId id() { | ||
return FeatureId.NVQ_VECTORS; | ||
} | ||
|
||
@Override | ||
public int headerSize() { | ||
return nvq.compressorSize(); | ||
} | ||
|
||
@Override | ||
public int inlineSize() { return nvq.compressedVectorSize();} | ||
|
||
public int dimension() { | ||
return nvq.globalMean.length(); | ||
} | ||
|
||
static NVQ load(CommonHeader header, RandomAccessReader reader) { | ||
try { | ||
return new NVQ(NVQuantization.load(reader)); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeHeader(DataOutput out) throws IOException { | ||
nvq.write(out, OnDiskGraphIndex.CURRENT_VERSION); | ||
} | ||
|
||
@Override | ||
public void writeInline(DataOutput out, Feature.State state_) throws IOException { | ||
var state = (NVQ.State) state_; | ||
state.vector.write(out); | ||
} | ||
|
||
public static class State implements Feature.State { | ||
public final QuantizedVector vector; | ||
|
||
public State(QuantizedVector vector) { | ||
this.vector = vector; | ||
} | ||
} | ||
|
||
ScoreFunction.ExactScoreFunction rerankerFor(VectorFloat<?> queryVector, | ||
VectorSimilarityFunction vsf, | ||
FeatureSource source) { | ||
var function = scorer.scoreFunctionFor(queryVector, vsf); | ||
|
||
return node2 -> { | ||
try { | ||
var reader = source.inlineReaderForNode(node2, FeatureId.NVQ_VECTORS); | ||
QuantizedVector.loadInto(reader, reusableQuantizedVector.get()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return function.similarityTo(reusableQuantizedVector.get()); | ||
}; | ||
} | ||
} |
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
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
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.