Skip to content

Commit

Permalink
Conform to the new interface that uses encodeTo
Browse files Browse the repository at this point in the history
  • Loading branch information
marianotepper committed Dec 19, 2024
1 parent 46bd177 commit d9bc9b0
Showing 1 changed file with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,18 @@ public NVQVectors encodeAll(RandomAccessVectorValues ravv, ForkJoinPool simdExec
@Override
public QuantizedVector encode(VectorFloat<?> vector) {
var tempVector = VectorUtil.sub(vector, globalMean);
return new QuantizedVector(getSubVectors(tempVector), bitsPerDimension, learn);
var qv = QuantizedVector.createEmpty(subvectorSizesAndOffsets, bitsPerDimension);
QuantizedVector.quantizeTo(getSubVectors(tempVector), bitsPerDimension, learn, qv);
return qv;
}

/**
* Encodes the input vector using NVQ.
* @return one subvector per subspace
* Encodes the input vector using NVQ into dest
*/
@Override
public void encodeTo(VectorFloat<?> v, NVQuantization.QuantizedVector dest) {

var tempVector = VectorUtil.sub(v, globalMean);
QuantizedVector.quantizeTo(getSubVectors(tempVector), bitsPerDimension, learn, dest);
}

/**
Expand Down Expand Up @@ -373,10 +375,9 @@ public static class QuantizedVector {
* @param bitsPerDimension the number of bits per dimension
* @param learn whether to use optimization to find the parameters of the nonlinearity
*/
public QuantizedVector(VectorFloat<?>[] subVectors, BitsPerDimension bitsPerDimension, boolean learn) {
this.subVectors = new QuantizedSubVector[subVectors.length];
public static void quantizeTo(VectorFloat<?>[] subVectors, BitsPerDimension bitsPerDimension, boolean learn, QuantizedVector dest) {
for (int i = 0; i < subVectors.length; i++) {
this.subVectors[i] = new QuantizedSubVector(subVectors[i], bitsPerDimension, learn);
QuantizedSubVector.quantizeTo(subVectors[i], bitsPerDimension, learn, dest.subVectors[i]);
}
}

Expand Down Expand Up @@ -487,12 +488,13 @@ public static int compressedVectorSize(int nDims, BitsPerDimension bitsPerDimens
}

/**
* Class constructor.
* Quantize the vector using NVQ into dest
* @param vector the subvector to quantize
* @param bitsPerDimension the number of bits per dimension
* @param learn whether to use optimization to find the parameters of the nonlinearity
* @param dest the destination subvector
*/
public QuantizedSubVector(VectorFloat<?> vector, BitsPerDimension bitsPerDimension, boolean learn) {
public static void quantizeTo(VectorFloat<?> vector, BitsPerDimension bitsPerDimension, boolean learn, QuantizedSubVector dest) {
var minValue = VectorUtil.min(vector);
var maxValue = VectorUtil.max(vector);

Expand Down Expand Up @@ -540,13 +542,13 @@ public QuantizedSubVector(VectorFloat<?> vector, BitsPerDimension bitsPerDimensi
throw new IllegalArgumentException("Unsupported bits per dimension: " + bitsPerDimension);
}

this.bitsPerDimension = bitsPerDimension;
this.minValue = minValue;
this.maxValue = maxValue;
this.growthRate = growthRate;
this.midpoint = midpoint;
this.bytes = quantized;
this.originalDimensions = vector.length();
dest.bitsPerDimension = bitsPerDimension;
dest.minValue = minValue;
dest.maxValue = maxValue;
dest.growthRate = growthRate;
dest.midpoint = midpoint;
dest.bytes = quantized;
dest.originalDimensions = vector.length();
}

/**
Expand Down

0 comments on commit d9bc9b0

Please sign in to comment.