Skip to content

Commit

Permalink
[Bug-Fix] Fixing the Quantization to clone the quantized vector, prep…
Browse files Browse the repository at this point in the history
…are the quantized vector correctly.

Signed-off-by: Navneet Verma <navneev@amazon.com>
  • Loading branch information
navneet1v committed Sep 8, 2024
1 parent e9e989b commit 67ed4b7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ static Object processAndReturnVector(KNNVectorValues<?> knnVectorValues, IndexBu
knnVectorValues.getVector(),
indexBuildSetup.getQuantizationOutput()
);
return indexBuildSetup.getQuantizationOutput().getQuantizedVector();
// We should get the cloned Vector because Quantization output always have the same reference
return indexBuildSetup.getQuantizationOutput().cloneQuantizedVector();
} else {
return knnVectorValues.conditionalCloneVector();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public boolean isPrepared(int vectorLength) {
return vectorLength == currentVectorLength && quantizedVector != null;
}

@Override
public byte[] cloneQuantizedVector() {
byte[] clonedByteArray = new byte[quantizedVector.length];
System.arraycopy(quantizedVector, 0, clonedByteArray, 0, quantizedVector.length);
return clonedByteArray;
}

/**
* Returns the quantized vector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public interface QuantizationOutput<T> {
* @return true if the quantized vector is already prepared, false otherwise.
*/
boolean isPrepared(int vectorLength);

T cloneQuantizedVector();
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public void quantize(final float[] vector, final QuantizationState state, final
if (thresholds == null || thresholds.length != vectorLength) {
throw new IllegalArgumentException("Thresholds must not be null and must match the dimension of the vector.");
}
if (!output.isPrepared(vectorLength)) output.prepareQuantizedVector(vectorLength);
// This will ensure that we are preparing the Quantized Vector always. aka setting all the bits to 0.
output.prepareQuantizedVector(vectorLength);
BitPacker.quantizeAndPackBits(vector, thresholds, output.getQuantizedVector());
}

Expand Down

0 comments on commit 67ed4b7

Please sign in to comment.