Class DefaultESVectorUtilSupport
- All Implemented Interfaces:
ESVectorUtilSupport
-
Field Summary
Fields inherited from interface org.elasticsearch.simdvec.internal.vectorization.ESVectorUtilSupport
B_QUERY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbFloat16ToFloat(byte[] bfBytes, int bfOffset, float[] floats, int floatOffset, int floatCount, ByteOrder byteOrder) Converts bfloat16s to floatsvoidcalculateOSQGridPoints(float[] target, int[] quantize, int points, float[] pts) floatcalculateOSQLoss(float[] target, float low, float high, float step, float invStep, float norm2, float lambda, int[] quantize) voidcenterAndCalculateOSQStatsDp(byte[] target, byte[] centroid, float[] centered, float[] stats) voidcenterAndCalculateOSQStatsDp(float[] target, float[] centroid, float[] centered, float[] stats) voidcenterAndCalculateOSQStatsEuclidean(byte[] target, byte[] centroid, float[] centered, float[] stats) voidcenterAndCalculateOSQStatsEuclidean(float[] target, float[] centroid, float[] centered, float[] stats) intcodePointCount(org.apache.lucene.util.BytesRef bytesRef) booleancontains(byte[] value, int valueOffset, int valueLength, byte[] term, int termOffset, int termLength) floatcosine(byte[] a, byte[] b) Calculates the cosine of the given byte arrays.voidcosineBulk(byte[] query, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances) floatdotProduct(byte[] a, byte[] b) Calculates the dot product of the given byte arrays.floatdotProduct(byte[] a, byte[] b, int offset, int length) Calculates the dot product over[offset, offset + length).floatdotProduct(float[] a, float[] b) Calculates the dot product of the given float arrays.floatdotProduct(float[] a, float[] b, int offset, int length) Calculates the dot product over[offset, offset + length).voiddotProductBulk(byte[] query, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances) voiddotProductBulk(float[] query, float[] v0, float[] v1, float[] v2, float[] v3, int distancesOffset, float[] distances) voidfloatToBFloat16(float[] floats, int floatOffset, byte[] bfBytes, int bfOffset, int floatCount, ByteOrder byteOrder) Converts floats to bfloat16sintindexOf(byte[] bytes, int offset, int length, byte marker) voidinRangeBitmask(long[] values, long lowerValue, long upperValue, long[] matches) longipByteBinByte(byte[] q, byte[] d) Compute dot product betweenqanddstatic longipByteBinByteImpl(byte[] q, byte[] d) Returns the inner product (aka dot product) between the query vectorq, and the data vectord.intipByteBit(byte[] q, byte[] d) static intipByteBitImpl(byte[] q, byte[] d) static intipByteBitImpl(byte[] q, byte[] d, int start) floatipFloatBit(float[] q, byte[] d) static floatipFloatBitImpl(float[] q, byte[] d) floatipFloatByte(float[] q, byte[] d) static floatipFloatByteImpl(float[] q, byte[] d) voidl2Normalize(byte[] v, int offset, int length) L2-normalizesv[offset:offset + length)in place using signed byte values as real components.voidl2Normalize(float[] v, int offset, int length) L2-normalizesv[offset:offset + length)in place.voidlinearCombination(float scaleOther, byte[] other, float[] dest) voidlinearCombination(float scaleOther, byte[] other, float scaleDest, float[] dest) voidlinearCombination(float scaleOther, float[] other, float[] dest) voidlinearCombination(float scaleOther, float[] other, float scaleDest, float[] dest) floatlogSumExpNQT(float[] vector) floatlogSumExpNQTDiff(float[] v1, float[] v2, float eps) floatmaxSimDotProduct(MultiBFloat16VectorsSource source, float[][] query, float[] scoresScratch) floatmaxSimDotProduct(MultiByteVectorsSource source, byte[][] query, float[] scoresScratch) floatmaxSimDotProduct(MultiFloatVectorsSource source, float[][] query, float[] scoresScratch) voidpackAsBinary(int[] vector, byte[] packed) static voidpackAsBinaryImpl(int[] vector, byte[] packed) voidpackDibit(int[] vector, byte[] packed) static voidpackDibitImpl(int[] vector, byte[] packed) Packs two bit vector (values 0-3) into a byte array with lower bits first.voidpackDibitQuad(int[] vector, byte[] packed) static voidpackDibitQuadImpl(int[] vector, byte[] packed) voidpow2DiffAndScaleNQT(float[] v1, float[] v2, float a, float eps, float[] result) intquantizeVectorWithIntervals(float[] vector, int[] destination, float lowInterval, float upperInterval, byte bits) floatsoarDistance(byte[] v1, byte[] centroid, float[] originalResidual, float soarLambda, float rnorm) floatsoarDistance(float[] v1, float[] centroid, float[] originalResidual, float soarLambda, float rnorm) voidsoarDistanceBulk(byte[] v1, byte[] c0, byte[] c1, byte[] c2, byte[] c3, float[] originalResidual, float soarLambda, float rnorm, float[] distances) voidsoarDistanceBulk(float[] v1, float[] c0, float[] c1, float[] c2, float[] c3, float[] originalResidual, float soarLambda, float rnorm, float[] distances) floatsquareDistance(byte[] a, byte[] b) Returns the sum of squared differences of the two vectors.floatsquareDistance(byte[] a, byte[] b, int offset, int length) Returns the sum of squared differences of the two byte vectors over a sub-range.floatsquareDistance(float[] a, float[] b) Returns the sum of squared differences of the two vectors.floatsquareDistance(float[] a, float[] b, int offset, int length) Returns the sum of squared differences over[offset, offset + length).voidsquareDistanceBulk(byte[] query, int vectorOffset, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances, int length) voidsquareDistanceBulk(float[] query, int vectorOffset, float[] v0, float[] v1, float[] v2, float[] v3, int distancesOffset, float[] distances, int length) voidtransposeHalfByte(int[] q, byte[] quantQueryByte) static voidtransposeHalfByteImpl(int[] q, byte[] quantQueryByte)
-
Constructor Details
-
DefaultESVectorUtilSupport
public DefaultESVectorUtilSupport()
-
-
Method Details
-
floatToBFloat16
public void floatToBFloat16(float[] floats, int floatOffset, byte[] bfBytes, int bfOffset, int floatCount, ByteOrder byteOrder) Description copied from interface:ESVectorUtilSupportConverts floats to bfloat16s- Specified by:
floatToBFloat16in interfaceESVectorUtilSupport
-
bFloat16ToFloat
public void bFloat16ToFloat(byte[] bfBytes, int bfOffset, float[] floats, int floatOffset, int floatCount, ByteOrder byteOrder) Description copied from interface:ESVectorUtilSupportConverts bfloat16s to floats- Specified by:
bFloat16ToFloatin interfaceESVectorUtilSupport
-
dotProduct
public float dotProduct(float[] a, float[] b) Description copied from interface:ESVectorUtilSupportCalculates the dot product of the given float arrays.- Specified by:
dotProductin interfaceESVectorUtilSupport
-
dotProduct
public float dotProduct(float[] a, float[] b, int offset, int length) Description copied from interface:ESVectorUtilSupportCalculates the dot product over[offset, offset + length).- Specified by:
dotProductin interfaceESVectorUtilSupport
-
l2Normalize
public void l2Normalize(float[] v, int offset, int length) Description copied from interface:ESVectorUtilSupportL2-normalizesv[offset:offset + length)in place. A zero prefix is a no-op.- Specified by:
l2Normalizein interfaceESVectorUtilSupport
-
squareDistance
public float squareDistance(float[] a, float[] b) Description copied from interface:ESVectorUtilSupportReturns the sum of squared differences of the two vectors.- Specified by:
squareDistancein interfaceESVectorUtilSupport
-
squareDistance
public float squareDistance(float[] a, float[] b, int offset, int length) Description copied from interface:ESVectorUtilSupportReturns the sum of squared differences over[offset, offset + length).- Specified by:
squareDistancein interfaceESVectorUtilSupport
-
cosine
public float cosine(byte[] a, byte[] b) Description copied from interface:ESVectorUtilSupportCalculates the cosine of the given byte arrays.- Specified by:
cosinein interfaceESVectorUtilSupport
-
dotProduct
public float dotProduct(byte[] a, byte[] b) Description copied from interface:ESVectorUtilSupportCalculates the dot product of the given byte arrays.- Specified by:
dotProductin interfaceESVectorUtilSupport
-
dotProduct
public float dotProduct(byte[] a, byte[] b, int offset, int length) Description copied from interface:ESVectorUtilSupportCalculates the dot product over[offset, offset + length).- Specified by:
dotProductin interfaceESVectorUtilSupport
-
l2Normalize
public void l2Normalize(byte[] v, int offset, int length) Description copied from interface:ESVectorUtilSupportL2-normalizesv[offset:offset + length)in place using signed byte values as real components.- Specified by:
l2Normalizein interfaceESVectorUtilSupport
-
squareDistance
public float squareDistance(byte[] a, byte[] b) Description copied from interface:ESVectorUtilSupportReturns the sum of squared differences of the two vectors.- Specified by:
squareDistancein interfaceESVectorUtilSupport
-
squareDistance
public float squareDistance(byte[] a, byte[] b, int offset, int length) Description copied from interface:ESVectorUtilSupportReturns the sum of squared differences of the two byte vectors over a sub-range.- Specified by:
squareDistancein interfaceESVectorUtilSupport
-
maxSimDotProduct
public float maxSimDotProduct(MultiFloatVectorsSource source, float[][] query, float[] scoresScratch) - Specified by:
maxSimDotProductin interfaceESVectorUtilSupport
-
maxSimDotProduct
public float maxSimDotProduct(MultiBFloat16VectorsSource source, float[][] query, float[] scoresScratch) - Specified by:
maxSimDotProductin interfaceESVectorUtilSupport
-
maxSimDotProduct
- Specified by:
maxSimDotProductin interfaceESVectorUtilSupport
-
ipByteBinByte
public long ipByteBinByte(byte[] q, byte[] d) Description copied from interface:ESVectorUtilSupportCompute dot product betweenqandd- Specified by:
ipByteBinBytein interfaceESVectorUtilSupport- Parameters:
q- query vector,ESVectorUtilSupport.B_QUERY-bit quantized and striped (seeESVectorUtil.transposeHalfByte)d- data vector, 1-bit quantized
-
ipByteBit
public int ipByteBit(byte[] q, byte[] d) - Specified by:
ipByteBitin interfaceESVectorUtilSupport
-
ipFloatBit
public float ipFloatBit(float[] q, byte[] d) - Specified by:
ipFloatBitin interfaceESVectorUtilSupport
-
ipFloatByte
public float ipFloatByte(float[] q, byte[] d) - Specified by:
ipFloatBytein interfaceESVectorUtilSupport
-
calculateOSQLoss
public float calculateOSQLoss(float[] target, float low, float high, float step, float invStep, float norm2, float lambda, int[] quantize) - Specified by:
calculateOSQLossin interfaceESVectorUtilSupport
-
calculateOSQGridPoints
public void calculateOSQGridPoints(float[] target, int[] quantize, int points, float[] pts) - Specified by:
calculateOSQGridPointsin interfaceESVectorUtilSupport
-
centerAndCalculateOSQStatsEuclidean
public void centerAndCalculateOSQStatsEuclidean(float[] target, float[] centroid, float[] centered, float[] stats) - Specified by:
centerAndCalculateOSQStatsEuclideanin interfaceESVectorUtilSupport
-
centerAndCalculateOSQStatsEuclidean
public void centerAndCalculateOSQStatsEuclidean(byte[] target, byte[] centroid, float[] centered, float[] stats) - Specified by:
centerAndCalculateOSQStatsEuclideanin interfaceESVectorUtilSupport
-
centerAndCalculateOSQStatsDp
public void centerAndCalculateOSQStatsDp(float[] target, float[] centroid, float[] centered, float[] stats) - Specified by:
centerAndCalculateOSQStatsDpin interfaceESVectorUtilSupport
-
centerAndCalculateOSQStatsDp
public void centerAndCalculateOSQStatsDp(byte[] target, byte[] centroid, float[] centered, float[] stats) - Specified by:
centerAndCalculateOSQStatsDpin interfaceESVectorUtilSupport
-
soarDistance
public float soarDistance(float[] v1, float[] centroid, float[] originalResidual, float soarLambda, float rnorm) - Specified by:
soarDistancein interfaceESVectorUtilSupport
-
ipByteBitImpl
public static int ipByteBitImpl(byte[] q, byte[] d) -
ipByteBitImpl
public static int ipByteBitImpl(byte[] q, byte[] d, int start) -
ipFloatBitImpl
public static float ipFloatBitImpl(float[] q, byte[] d) -
ipByteBinByteImpl
public static long ipByteBinByteImpl(byte[] q, byte[] d) Returns the inner product (aka dot product) between the query vectorq, and the data vectord.The query vector should be
ESVectorUtilSupport.B_QUERY-bit quantized and striped, so that the firstnbits of the array are the initial bits of each of thenvector dimensions; the nextnbits are the second bits of each of thenvector dimensions, and so on (this algorithm is only valid for vectors with dimensions a multiple of 8). The striping is usually done byESVectorUtil.transposeHalfByte.The data vector should be single-bit quantized.
Dot products with bit quantization
The dot product of any vector with a bit vector is a simple selector - each query vector dimension is multiplied by the 0 or 1 in the corresponding data vector dimension; the result is that each dimension value is either kept or ignored, with the dimensions that are kept then summed together.The algorithm
The transposition already applied to the query vector ensures there's a 1-to-1 correspondence between the data vector bits and query vector bits (seeESVectorUtil.transposeHalfByte); this means we can use a bitwise&to keep only the bits of the vector elements we want to sum. Essentially, the data vector is used as a selector for each of the striped bits of each vector dimension as stored, concatenated together, inq.The final dot product result can be obtained by observing that the sum of each stripe of
nbits can be computed using the bit count of that stripe. Similar to long multiplication, the result of each stripe ofnbits can be added together by shifting the valuesbits to the left, wheresis the stripe number (0-3), then adding to the overall result. Any carry is handled by the add operation.- Parameters:
q- query vector,ESVectorUtilSupport.B_QUERY-bit quantized and striped (seeESVectorUtil.transposeHalfByte)d- data vector, 1-bit quantized- Returns:
- inner product result
-
ipFloatByteImpl
public static float ipFloatByteImpl(float[] q, byte[] d) -
quantizeVectorWithIntervals
public int quantizeVectorWithIntervals(float[] vector, int[] destination, float lowInterval, float upperInterval, byte bits) - Specified by:
quantizeVectorWithIntervalsin interfaceESVectorUtilSupport
-
dotProductBulk
public void dotProductBulk(float[] query, float[] v0, float[] v1, float[] v2, float[] v3, int distancesOffset, float[] distances) - Specified by:
dotProductBulkin interfaceESVectorUtilSupport
-
squareDistanceBulk
public void squareDistanceBulk(float[] query, int vectorOffset, float[] v0, float[] v1, float[] v2, float[] v3, int distancesOffset, float[] distances, int length) - Specified by:
squareDistanceBulkin interfaceESVectorUtilSupport
-
dotProductBulk
public void dotProductBulk(byte[] query, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances) - Specified by:
dotProductBulkin interfaceESVectorUtilSupport
-
cosineBulk
public void cosineBulk(byte[] query, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances) - Specified by:
cosineBulkin interfaceESVectorUtilSupport
-
squareDistanceBulk
public void squareDistanceBulk(byte[] query, int vectorOffset, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances, int length) - Specified by:
squareDistanceBulkin interfaceESVectorUtilSupport
-
soarDistanceBulk
public void soarDistanceBulk(float[] v1, float[] c0, float[] c1, float[] c2, float[] c3, float[] originalResidual, float soarLambda, float rnorm, float[] distances) - Specified by:
soarDistanceBulkin interfaceESVectorUtilSupport
-
soarDistanceBulk
public void soarDistanceBulk(byte[] v1, byte[] c0, byte[] c1, byte[] c2, byte[] c3, float[] originalResidual, float soarLambda, float rnorm, float[] distances) - Specified by:
soarDistanceBulkin interfaceESVectorUtilSupport
-
soarDistance
public float soarDistance(byte[] v1, byte[] centroid, float[] originalResidual, float soarLambda, float rnorm) - Specified by:
soarDistancein interfaceESVectorUtilSupport
-
packDibit
public void packDibit(int[] vector, byte[] packed) - Specified by:
packDibitin interfaceESVectorUtilSupport
-
packDibitQuad
public void packDibitQuad(int[] vector, byte[] packed) - Specified by:
packDibitQuadin interfaceESVectorUtilSupport
-
packAsBinary
public void packAsBinary(int[] vector, byte[] packed) - Specified by:
packAsBinaryin interfaceESVectorUtilSupport
-
packDibitImpl
public static void packDibitImpl(int[] vector, byte[] packed) Packs two bit vector (values 0-3) into a byte array with lower bits first. The striding is similar to transposeHalfByte- Parameters:
vector- the input vector with values 0-3packed- the output packed byte array
-
packDibitQuadImpl
public static void packDibitQuadImpl(int[] vector, byte[] packed) -
packAsBinaryImpl
public static void packAsBinaryImpl(int[] vector, byte[] packed) -
transposeHalfByte
public void transposeHalfByte(int[] q, byte[] quantQueryByte) - Specified by:
transposeHalfBytein interfaceESVectorUtilSupport
-
transposeHalfByteImpl
public static void transposeHalfByteImpl(int[] q, byte[] quantQueryByte) -
indexOf
public int indexOf(byte[] bytes, int offset, int length, byte marker) - Specified by:
indexOfin interfaceESVectorUtilSupport
-
codePointCount
public int codePointCount(org.apache.lucene.util.BytesRef bytesRef) - Specified by:
codePointCountin interfaceESVectorUtilSupport
-
contains
public boolean contains(byte[] value, int valueOffset, int valueLength, byte[] term, int termOffset, int termLength) - Specified by:
containsin interfaceESVectorUtilSupport
-
inRangeBitmask
public void inRangeBitmask(long[] values, long lowerValue, long upperValue, long[] matches) - Specified by:
inRangeBitmaskin interfaceESVectorUtilSupport
-
linearCombination
public void linearCombination(float scaleOther, float[] other, float scaleDest, float[] dest) - Specified by:
linearCombinationin interfaceESVectorUtilSupport
-
linearCombination
public void linearCombination(float scaleOther, float[] other, float[] dest) - Specified by:
linearCombinationin interfaceESVectorUtilSupport
-
linearCombination
public void linearCombination(float scaleOther, byte[] other, float scaleDest, float[] dest) - Specified by:
linearCombinationin interfaceESVectorUtilSupport
-
linearCombination
public void linearCombination(float scaleOther, byte[] other, float[] dest) - Specified by:
linearCombinationin interfaceESVectorUtilSupport
-
logSumExpNQT
public float logSumExpNQT(float[] vector) - Specified by:
logSumExpNQTin interfaceESVectorUtilSupport
-
logSumExpNQTDiff
public float logSumExpNQTDiff(float[] v1, float[] v2, float eps) - Specified by:
logSumExpNQTDiffin interfaceESVectorUtilSupport
-
pow2DiffAndScaleNQT
public void pow2DiffAndScaleNQT(float[] v1, float[] v2, float a, float eps, float[] result) - Specified by:
pow2DiffAndScaleNQTin interfaceESVectorUtilSupport
-