Skip to content

Commit

Permalink
Fix MutableBQVectors parameterization. Add basic test coverage. (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkni authored Feb 19, 2025
1 parent 219465f commit d59232f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public int hashCode() {
public String toString() {
return "BQVectors{" +
"bq=" + bq +
", count=" + compressedVectors.length +
", count=" + count() +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package io.github.jbellis.jvector.quantization;

import io.github.jbellis.jvector.vector.types.VectorFloat;

@SuppressWarnings("unused")
public class MutableBQVectors extends BQVectors implements MutableCompressedVectors<long[]> {
public class MutableBQVectors extends BQVectors implements MutableCompressedVectors<VectorFloat<?>> {
private static final int INITIAL_CAPACITY = 1024;
private static final float GROWTH_FACTOR = 1.5f;

Expand All @@ -44,9 +46,9 @@ private void ensureCapacity(int ordinal) {
}

@Override
public void encodeAndSet(int ordinal, long[] vector) {
public void encodeAndSet(int ordinal, VectorFloat<?> vector) {
ensureCapacity(ordinal);
compressedVectors[ordinal] = vector;
compressedVectors[ordinal] = bq.encode(vector);
vectorCount = Math.max(vectorCount, ordinal + 1);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.quantization;

import org.junit.Test;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import io.github.jbellis.jvector.TestUtil;
import io.github.jbellis.jvector.graph.ListRandomAccessVectorValues;
import io.github.jbellis.jvector.vector.VectorSimilarityFunction;

import static io.github.jbellis.jvector.TestUtil.createRandomVectors;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
public class TestBinaryQuantization extends RandomizedTest
{
@Test
public void testMutableImmutableBQEquality()
{
var vectors = createRandomVectors(512, 64);
var ravv = new ListRandomAccessVectorValues(vectors, 64);
var bq = new BinaryQuantization(ravv.dimension());
var immutableCompressedVectors = bq.encodeAll(ravv);
var mutableCompressedVectors = new MutableBQVectors(bq);
for (int i = 0; i < ravv.size(); i++)
{
mutableCompressedVectors.encodeAndSet(i, ravv.getVector(i));
}
assertEquals(mutableCompressedVectors.count(), immutableCompressedVectors.count());
var randomVector = TestUtil.randomVector(getRandom(), 64);
for (VectorSimilarityFunction vsf : VectorSimilarityFunction.values())
{
var immutableScoreFunction = immutableCompressedVectors.scoreFunctionFor(randomVector, vsf);
var mutableScoreFunction = mutableCompressedVectors.scoreFunctionFor(randomVector, vsf);
for (int i = 0; i < ravv.size(); i++)
{
assertEquals(immutableScoreFunction.similarityTo(i), mutableScoreFunction.similarityTo(i));
}
}
}
}

0 comments on commit d59232f

Please sign in to comment.