Repository navigation
Add int8 / byte-vector similarity support #709
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
Open
r-devulap
wants to merge
33
commits into
main
Choose a base branch
from
int8-support
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.
+4,657
−228
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
844a738
Add random-access int8 vector value support
r-devulap 58fb530
Add byte-similarity methods to VectorUtilSupport / VectorUtil / Defau…
r-devulap ddffd5b
Add ByteVectorSimilarityFunction enum
r-devulap 0fdc211
Add BuildScoreProvider.byteVectorScoreProvider and searchProviderFor(…
r-devulap 69e8664
Wire byteVectorScoreProvider into GraphIndexBuilder
r-devulap 4e78cda
Vectorize ByteSequence similarity metrics with SIMD
r-devulap 70640c0
Add InlineByteVectors feature for native int8 on-disk storage
r-devulap c34baa2
Add INT8 end-to-end tutorial (Int8Example)
r-devulap 45d8efc
Add int8 test coverage for byte-vector similarity, RABVV, BSP, SIMD, …
r-devulap dac9219
Remove dead offset locals from byte similarity methods
r-devulap 0cd3286
Fix maxMagnitude calculation for ByteVectorSimilarityFunction.DOT_PRO…
r-devulap 3814e37
Extract asRandomAccessSupplier with type check in BuildScoreProvider
r-devulap c840eb1
Add INT8 dataset configs for wikipedia-2023-11-en-mxbai and wiki-mult…
r-devulap 4c3ff2d
Fix build and test failures in base, native and tests modules
r-devulap f4d3f24
Add end-to-end int8/byte-vector support to the BenchYAML benchmark pi…
r-devulap b2ed9e2
Fix dot_product_i8 and cosine_i8 odd accumulator loss on ARM SVE/NEON
r-devulap 29f392f
Address review feedback on int8 vector support
r-devulap ab4180b
Address second round of review feedback on int8 vector support
r-devulap 9aff7cf
Use integer accumulation for int8 defaults and reuse a scratch buffer…
r-devulap ddc304f
Add and update tests for int8 review feedback
r-devulap d55e908
Preserve score spacing in int8 DOT_PRODUCT at high dimensions
r-devulap 70cb804
Get rid of local paths
r-devulap 7c17675
Mark new int8 vector APIs as @Experimental
r-devulap 97107ae
Add getVectorInto to the common VectorValues interface
r-devulap e9f8415
Require explicit reranking/fusedGraph config instead of assuming defa…
r-devulap d10614b
Fix Int8Example comments: siftsmall ships with the repo
r-devulap 9c1e2b6
Make GraphIndexBuilder.build read the supplied int8 vectors like the …
r-devulap eec0077
Add 4.1.0 release note for int8 vector support
r-devulap c0f344c
Fix flaky int8 cosine tests on all-zero random vectors
r-devulap 58ad458
Register an InlineByteVectors FeatureProvider for int8 datasets
r-devulap 21be937
update on disk version to v7. Update all test yamls to v7. Add unit t…
MarkWolters ee6455c
Add default implementations for int8 methods in VectorUtilSupport
r-devulap b696b49
Check dimensions match in int8 dotProduct and squareL2Distance
r-devulap 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| ### Native Int8 (Byte) Vector Support | ||
|
|
||
| **Description** | ||
| JVector can now build, store and search signed int8 vectors natively, without converting them to float32. | ||
| This is useful when an embedding model already emits int8 vectors, or when vectors have been scaled to | ||
| int8 up front: they take 4x less space on disk and in memory than float32, and all scoring is done | ||
| byte-by-byte. | ||
|
|
||
| * `RandomAccessByteVectorValues` (and `ListRandomAccessByteVectorValues`) provide random access to | ||
| `ByteSequence` vectors. It and `RandomAccessVectorValues` now share a common `VectorValues<V>` | ||
| interface, which also provides `getVectorInto(node, destination, offset)` for reading a | ||
| vector into a caller-owned buffer. | ||
| * `ByteVectorSimilarityFunction` scores two int8 vectors: `EUCLIDEAN` is `1 / (1 + squaredL2)`, | ||
| `DOT_PRODUCT` is `dot < 0 ? 1 / (1 - dot) : dot + 1`, and `COSINE` is `(1 + cosine) / 2`. The scores are | ||
| not normalised by dimension, so distinct integer distances stay distinct at high dimensions. | ||
| `DOT_PRODUCT` scores are positive but not bounded above by 1. | ||
| * `GraphIndexBuilder.builder(RandomAccessByteVectorValues, ByteVectorSimilarityFunction, M)` builds a graph | ||
| from int8 vectors, and `GraphIndexBuilder.build(VectorValues<?>)` accepts either vector type. | ||
| * The `InlineByteVectors` on-disk feature (`FeatureId.INLINE_BYTE_VECTORS`) stores each vector as `dimension` | ||
| raw bytes. `OnDiskGraphIndex.View.byteVectorRerankerFor(...)` scores candidates directly from disk, and | ||
| `getByteVectorInto(...)` reads a vector into a reusable buffer. | ||
| * Int8 distance kernels (squared L2, dot product, cosine) are available in the default, Panama and native | ||
| SIMD implementations, and accumulate in integers so all execution paths return identical results. | ||
| * `BenchYAML` and `AutoBenchYAML` support int8 datasets: mark a dataset with `is_int8: true` in | ||
| `dataset-metadata.yml`. A runnable example is in `Int8Example` (`-Dtutorial=int8`). | ||
|
|
||
| **How to Enable** | ||
| No configuration is required; the float32 APIs are unchanged. To use int8 vectors, build with the byte-vector | ||
| builder and write the index with the `InlineByteVectors` feature: | ||
|
|
||
| ```java | ||
| RandomAccessByteVectorValues vectors = new ListRandomAccessByteVectorValues(byteVectors, dimension); | ||
| try (var builder = GraphIndexBuilder.builder(vectors, ByteVectorSimilarityFunction.EUCLIDEAN, 32).build()) { | ||
| var graph = builder.build(vectors); | ||
| try (var writer = GraphIndexWriter.getBuilderFor(GraphIndexWriterTypes.RANDOM_ACCESS_PARALLEL, graph, path) | ||
| .with(new InlineByteVectors(dimension)).build()) { | ||
| writer.write(Map.of(FeatureId.INLINE_BYTE_VECTORS, n -> new InlineByteVectors.State(vectors.getVector(n)))); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Notes** | ||
| * The new interfaces and classes are marked `@Experimental` and the API may change in later releases. | ||
| * Only signed int8 (-128..127) is supported. Unsigned uint8 data, such as conventional SIFT1B/BIGANN `.bvecs` | ||
| files, must be converted first; it is not detected, and reading it as signed silently changes distances. | ||
| * Quantization and compression (PQ, BQ, NVQ, fused graphs) are not supported for int8 vectors. Traversal | ||
| already scores exactly, so a separate reranking step is not needed. `rerankK` still controls how much of the | ||
| graph is searched. | ||
| * Zero vectors are not supported with `COSINE` (the result is undefined); do not index them. | ||
| * Existing float32 callers keep working: `RandomAccessVectorValues.getVector` keeps its `VectorFloat<?>` | ||
| signature and `GraphIndexBuilder.build(RandomAccessVectorValues)` is retained as a forwarding overload. | ||
| * Int8 indexes require on-disk format version 7 or later. JVector 4.0.x reads formats up to 6 and rejects | ||
| version 7 files; a version 6 writer rejects `INLINE_BYTE_VECTORS`. |
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
72 changes: 72 additions & 0 deletions
72
...-base/src/main/java/io/github/jbellis/jvector/graph/ListRandomAccessByteVectorValues.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,72 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import io.github.jbellis.jvector.annotations.Experimental; | ||
| import io.github.jbellis.jvector.vector.types.ByteSequence; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A List-backed implementation of the {@link RandomAccessByteVectorValues} interface. | ||
| * <p> | ||
| * It is acceptable to provide this class to a GraphBuilder, and then continue | ||
| * to add vectors to the backing List as you add to the graph. | ||
| * <p> | ||
| * This will be as threadsafe as the provided List. | ||
| */ | ||
| @Experimental | ||
| public class ListRandomAccessByteVectorValues implements RandomAccessByteVectorValues { | ||
| private final List<ByteSequence<?>> vectors; | ||
| private final int dimension; | ||
|
|
||
| /** | ||
| * Construct a new instance of {@link ListRandomAccessByteVectorValues}. | ||
| * | ||
| * @param vectors a (potentially mutable) list of byte vectors. | ||
| * @param dimension the dimension of the vectors. | ||
| */ | ||
| public ListRandomAccessByteVectorValues(List<ByteSequence<?>> vectors, int dimension) { | ||
| this.vectors = vectors; | ||
| this.dimension = dimension; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return vectors.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public int dimension() { | ||
| return dimension; | ||
| } | ||
|
|
||
| @Override | ||
| public ByteSequence<?> getVector(int nodeId) { | ||
| return vectors.get(nodeId); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isValueShared() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public ListRandomAccessByteVectorValues copy() { | ||
| return this; | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
jvector-base/src/main/java/io/github/jbellis/jvector/graph/RandomAccessByteVectorValues.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,68 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import io.github.jbellis.jvector.annotations.Experimental; | ||
| import io.github.jbellis.jvector.util.ExplicitThreadLocal; | ||
| import io.github.jbellis.jvector.vector.types.ByteSequence; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Provides random access to byte (int8) vectors by dense ordinal. | ||
| * <p> | ||
| * This is the byte-vector parallel to {@link RandomAccessVectorValues}. | ||
| * Both extend the common super-interface {@link VectorValues}. | ||
| * It is used by graph-based index builders and searchers that operate natively | ||
| * on int8 vectors without a float32 round-trip. | ||
| */ | ||
| @Experimental | ||
| public interface RandomAccessByteVectorValues extends VectorValues<ByteSequence<?>> { | ||
|
|
||
| /** | ||
| * Retrieve the vector associated with a given node, and store it in the destination vector at the given offset. | ||
| * The default implementation copies from {@link #getVector}; implementations that can read directly into the | ||
| * destination (e.g. from disk) should override it. | ||
| */ | ||
| @Override | ||
| default void getVectorInto(int node, ByteSequence<?> destinationVector, int offset) { | ||
| destinationVector.copyFrom(getVector(node), 0, offset, dimension()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new copy of this {@link RandomAccessByteVectorValues}. | ||
| * Un-shared implementations may simply return {@code this}. | ||
| */ | ||
| @Override | ||
| RandomAccessByteVectorValues copy(); | ||
|
|
||
| /** | ||
| * Returns a supplier of thread-local copies of the RAVV. | ||
| */ | ||
| @Override | ||
| default Supplier<RandomAccessByteVectorValues> threadLocalSupplier() { | ||
| if (!isValueShared()) { | ||
| return () -> this; | ||
| } | ||
|
|
||
| if (this instanceof AutoCloseable) { | ||
| VectorValues.LOG.warning("VectorValues is shared and implements AutoCloseable; threadLocalSupplier() may lead to leaks"); | ||
| } | ||
| var tl = ExplicitThreadLocal.withInitial(this::copy); | ||
| return tl::get; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.