Class DefaultESVectorUtilSupport

java.lang.Object
org.elasticsearch.simdvec.internal.vectorization.DefaultESVectorUtilSupport
All Implemented Interfaces:
ESVectorUtilSupport

public final class DefaultESVectorUtilSupport extends Object implements ESVectorUtilSupport
  • Field Summary

    Fields inherited from interface org.elasticsearch.simdvec.internal.vectorization.ESVectorUtilSupport

    B_QUERY
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    bFloat16ToFloat(byte[] bfBytes, int bfOffset, float[] floats, int floatOffset, int floatCount, ByteOrder byteOrder)
    Converts bfloat16s to floats
    void
    calculateOSQGridPoints(float[] target, int[] quantize, int points, float[] pts)
     
    float
    calculateOSQLoss(float[] target, float low, float high, float step, float invStep, float norm2, float lambda, int[] quantize)
     
    void
    centerAndCalculateOSQStatsDp(byte[] target, byte[] centroid, float[] centered, float[] stats)
     
    void
    centerAndCalculateOSQStatsDp(float[] target, float[] centroid, float[] centered, float[] stats)
     
    void
    centerAndCalculateOSQStatsEuclidean(byte[] target, byte[] centroid, float[] centered, float[] stats)
     
    void
    centerAndCalculateOSQStatsEuclidean(float[] target, float[] centroid, float[] centered, float[] stats)
     
    int
    codePointCount(org.apache.lucene.util.BytesRef bytesRef)
     
    boolean
    contains(byte[] value, int valueOffset, int valueLength, byte[] term, int termOffset, int termLength)
     
    float
    cosine(byte[] a, byte[] b)
    Calculates the cosine of the given byte arrays.
    void
    cosineBulk(byte[] query, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances)
     
    float
    dotProduct(byte[] a, byte[] b)
    Calculates the dot product of the given byte arrays.
    float
    dotProduct(byte[] a, byte[] b, int offset, int length)
    Calculates the dot product over [offset, offset + length).
    float
    dotProduct(float[] a, float[] b)
    Calculates the dot product of the given float arrays.
    float
    dotProduct(float[] a, float[] b, int offset, int length)
    Calculates the dot product over [offset, offset + length).
    void
    dotProductBulk(byte[] query, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances)
     
    void
    dotProductBulk(float[] query, float[] v0, float[] v1, float[] v2, float[] v3, int distancesOffset, float[] distances)
     
    void
    floatToBFloat16(float[] floats, int floatOffset, byte[] bfBytes, int bfOffset, int floatCount, ByteOrder byteOrder)
    Converts floats to bfloat16s
    int
    indexOf(byte[] bytes, int offset, int length, byte marker)
     
    void
    inRangeBitmask(long[] values, long lowerValue, long upperValue, long[] matches)
     
    long
    ipByteBinByte(byte[] q, byte[] d)
    Compute dot product between q and d
    static long
    ipByteBinByteImpl(byte[] q, byte[] d)
    Returns the inner product (aka dot product) between the query vector q, and the data vector d.
    int
    ipByteBit(byte[] q, byte[] d)
     
    static int
    ipByteBitImpl(byte[] q, byte[] d)
     
    static int
    ipByteBitImpl(byte[] q, byte[] d, int start)
     
    float
    ipFloatBit(float[] q, byte[] d)
     
    static float
    ipFloatBitImpl(float[] q, byte[] d)
     
    float
    ipFloatByte(float[] q, byte[] d)
     
    static float
    ipFloatByteImpl(float[] q, byte[] d)
     
    void
    l2Normalize(byte[] v, int offset, int length)
    L2-normalizes v[offset:offset + length) in place using signed byte values as real components.
    void
    l2Normalize(float[] v, int offset, int length)
    L2-normalizes v[offset:offset + length) in place.
    void
    linearCombination(float scaleOther, byte[] other, float[] dest)
     
    void
    linearCombination(float scaleOther, byte[] other, float scaleDest, float[] dest)
     
    void
    linearCombination(float scaleOther, float[] other, float[] dest)
     
    void
    linearCombination(float scaleOther, float[] other, float scaleDest, float[] dest)
     
    float
    logSumExpNQT(float[] vector)
     
    float
    logSumExpNQTDiff(float[] v1, float[] v2, float eps)
     
    float
    maxSimDotProduct(MultiBFloat16VectorsSource source, float[][] query, float[] scoresScratch)
     
    float
    maxSimDotProduct(MultiByteVectorsSource source, byte[][] query, float[] scoresScratch)
     
    float
    maxSimDotProduct(MultiFloatVectorsSource source, float[][] query, float[] scoresScratch)
     
    void
    packAsBinary(int[] vector, byte[] packed)
     
    static void
    packAsBinaryImpl(int[] vector, byte[] packed)
     
    void
    packDibit(int[] vector, byte[] packed)
     
    static void
    packDibitImpl(int[] vector, byte[] packed)
    Packs two bit vector (values 0-3) into a byte array with lower bits first.
    void
    packDibitQuad(int[] vector, byte[] packed)
     
    static void
    packDibitQuadImpl(int[] vector, byte[] packed)
     
    void
    pow2DiffAndScaleNQT(float[] v1, float[] v2, float a, float eps, float[] result)
     
    int
    quantizeVectorWithIntervals(float[] vector, int[] destination, float lowInterval, float upperInterval, byte bits)
     
    float
    soarDistance(byte[] v1, byte[] centroid, float[] originalResidual, float soarLambda, float rnorm)
     
    float
    soarDistance(float[] v1, float[] centroid, float[] originalResidual, float soarLambda, float rnorm)
     
    void
    soarDistanceBulk(byte[] v1, byte[] c0, byte[] c1, byte[] c2, byte[] c3, float[] originalResidual, float soarLambda, float rnorm, float[] distances)
     
    void
    soarDistanceBulk(float[] v1, float[] c0, float[] c1, float[] c2, float[] c3, float[] originalResidual, float soarLambda, float rnorm, float[] distances)
     
    float
    squareDistance(byte[] a, byte[] b)
    Returns the sum of squared differences of the two vectors.
    float
    squareDistance(byte[] a, byte[] b, int offset, int length)
    Returns the sum of squared differences of the two byte vectors over a sub-range.
    float
    squareDistance(float[] a, float[] b)
    Returns the sum of squared differences of the two vectors.
    float
    squareDistance(float[] a, float[] b, int offset, int length)
    Returns the sum of squared differences over [offset, offset + length).
    void
    squareDistanceBulk(byte[] query, int vectorOffset, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances, int length)
     
    void
    squareDistanceBulk(float[] query, int vectorOffset, float[] v0, float[] v1, float[] v2, float[] v3, int distancesOffset, float[] distances, int length)
     
    void
    transposeHalfByte(int[] q, byte[] quantQueryByte)
     
    static void
    transposeHalfByteImpl(int[] q, byte[] quantQueryByte)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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: ESVectorUtilSupport
      Converts floats to bfloat16s
      Specified by:
      floatToBFloat16 in interface ESVectorUtilSupport
    • bFloat16ToFloat

      public void bFloat16ToFloat(byte[] bfBytes, int bfOffset, float[] floats, int floatOffset, int floatCount, ByteOrder byteOrder)
      Description copied from interface: ESVectorUtilSupport
      Converts bfloat16s to floats
      Specified by:
      bFloat16ToFloat in interface ESVectorUtilSupport
    • dotProduct

      public float dotProduct(float[] a, float[] b)
      Description copied from interface: ESVectorUtilSupport
      Calculates the dot product of the given float arrays.
      Specified by:
      dotProduct in interface ESVectorUtilSupport
    • dotProduct

      public float dotProduct(float[] a, float[] b, int offset, int length)
      Description copied from interface: ESVectorUtilSupport
      Calculates the dot product over [offset, offset + length).
      Specified by:
      dotProduct in interface ESVectorUtilSupport
    • l2Normalize

      public void l2Normalize(float[] v, int offset, int length)
      Description copied from interface: ESVectorUtilSupport
      L2-normalizes v[offset:offset + length) in place. A zero prefix is a no-op.
      Specified by:
      l2Normalize in interface ESVectorUtilSupport
    • squareDistance

      public float squareDistance(float[] a, float[] b)
      Description copied from interface: ESVectorUtilSupport
      Returns the sum of squared differences of the two vectors.
      Specified by:
      squareDistance in interface ESVectorUtilSupport
    • squareDistance

      public float squareDistance(float[] a, float[] b, int offset, int length)
      Description copied from interface: ESVectorUtilSupport
      Returns the sum of squared differences over [offset, offset + length).
      Specified by:
      squareDistance in interface ESVectorUtilSupport
    • cosine

      public float cosine(byte[] a, byte[] b)
      Description copied from interface: ESVectorUtilSupport
      Calculates the cosine of the given byte arrays.
      Specified by:
      cosine in interface ESVectorUtilSupport
    • dotProduct

      public float dotProduct(byte[] a, byte[] b)
      Description copied from interface: ESVectorUtilSupport
      Calculates the dot product of the given byte arrays.
      Specified by:
      dotProduct in interface ESVectorUtilSupport
    • dotProduct

      public float dotProduct(byte[] a, byte[] b, int offset, int length)
      Description copied from interface: ESVectorUtilSupport
      Calculates the dot product over [offset, offset + length).
      Specified by:
      dotProduct in interface ESVectorUtilSupport
    • l2Normalize

      public void l2Normalize(byte[] v, int offset, int length)
      Description copied from interface: ESVectorUtilSupport
      L2-normalizes v[offset:offset + length) in place using signed byte values as real components.
      Specified by:
      l2Normalize in interface ESVectorUtilSupport
    • squareDistance

      public float squareDistance(byte[] a, byte[] b)
      Description copied from interface: ESVectorUtilSupport
      Returns the sum of squared differences of the two vectors.
      Specified by:
      squareDistance in interface ESVectorUtilSupport
    • squareDistance

      public float squareDistance(byte[] a, byte[] b, int offset, int length)
      Description copied from interface: ESVectorUtilSupport
      Returns the sum of squared differences of the two byte vectors over a sub-range.
      Specified by:
      squareDistance in interface ESVectorUtilSupport
    • maxSimDotProduct

      public float maxSimDotProduct(MultiFloatVectorsSource source, float[][] query, float[] scoresScratch)
      Specified by:
      maxSimDotProduct in interface ESVectorUtilSupport
    • maxSimDotProduct

      public float maxSimDotProduct(MultiBFloat16VectorsSource source, float[][] query, float[] scoresScratch)
      Specified by:
      maxSimDotProduct in interface ESVectorUtilSupport
    • maxSimDotProduct

      public float maxSimDotProduct(MultiByteVectorsSource source, byte[][] query, float[] scoresScratch)
      Specified by:
      maxSimDotProduct in interface ESVectorUtilSupport
    • ipByteBinByte

      public long ipByteBinByte(byte[] q, byte[] d)
      Description copied from interface: ESVectorUtilSupport
      Compute dot product between q and d
      Specified by:
      ipByteBinByte in interface ESVectorUtilSupport
      Parameters:
      q - query vector, ESVectorUtilSupport.B_QUERY-bit quantized and striped (see ESVectorUtil.transposeHalfByte)
      d - data vector, 1-bit quantized
    • ipByteBit

      public int ipByteBit(byte[] q, byte[] d)
      Specified by:
      ipByteBit in interface ESVectorUtilSupport
    • ipFloatBit

      public float ipFloatBit(float[] q, byte[] d)
      Specified by:
      ipFloatBit in interface ESVectorUtilSupport
    • ipFloatByte

      public float ipFloatByte(float[] q, byte[] d)
      Specified by:
      ipFloatByte in interface ESVectorUtilSupport
    • calculateOSQLoss

      public float calculateOSQLoss(float[] target, float low, float high, float step, float invStep, float norm2, float lambda, int[] quantize)
      Specified by:
      calculateOSQLoss in interface ESVectorUtilSupport
    • calculateOSQGridPoints

      public void calculateOSQGridPoints(float[] target, int[] quantize, int points, float[] pts)
      Specified by:
      calculateOSQGridPoints in interface ESVectorUtilSupport
    • centerAndCalculateOSQStatsEuclidean

      public void centerAndCalculateOSQStatsEuclidean(float[] target, float[] centroid, float[] centered, float[] stats)
      Specified by:
      centerAndCalculateOSQStatsEuclidean in interface ESVectorUtilSupport
    • centerAndCalculateOSQStatsEuclidean

      public void centerAndCalculateOSQStatsEuclidean(byte[] target, byte[] centroid, float[] centered, float[] stats)
      Specified by:
      centerAndCalculateOSQStatsEuclidean in interface ESVectorUtilSupport
    • centerAndCalculateOSQStatsDp

      public void centerAndCalculateOSQStatsDp(float[] target, float[] centroid, float[] centered, float[] stats)
      Specified by:
      centerAndCalculateOSQStatsDp in interface ESVectorUtilSupport
    • centerAndCalculateOSQStatsDp

      public void centerAndCalculateOSQStatsDp(byte[] target, byte[] centroid, float[] centered, float[] stats)
      Specified by:
      centerAndCalculateOSQStatsDp in interface ESVectorUtilSupport
    • soarDistance

      public float soarDistance(float[] v1, float[] centroid, float[] originalResidual, float soarLambda, float rnorm)
      Specified by:
      soarDistance in interface ESVectorUtilSupport
    • 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 vector q, and the data vector d.

      The query vector should be ESVectorUtilSupport.B_QUERY-bit quantized and striped, so that the first n bits of the array are the initial bits of each of the n vector dimensions; the next n bits are the second bits of each of the n vector dimensions, and so on (this algorithm is only valid for vectors with dimensions a multiple of 8). The striping is usually done by ESVectorUtil.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 (see ESVectorUtil.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, in q.

      The final dot product result can be obtained by observing that the sum of each stripe of n bits can be computed using the bit count of that stripe. Similar to long multiplication, the result of each stripe of n bits can be added together by shifting the value s bits to the left, where s is 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 (see ESVectorUtil.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:
      quantizeVectorWithIntervals in interface ESVectorUtilSupport
    • dotProductBulk

      public void dotProductBulk(float[] query, float[] v0, float[] v1, float[] v2, float[] v3, int distancesOffset, float[] distances)
      Specified by:
      dotProductBulk in interface ESVectorUtilSupport
    • squareDistanceBulk

      public void squareDistanceBulk(float[] query, int vectorOffset, float[] v0, float[] v1, float[] v2, float[] v3, int distancesOffset, float[] distances, int length)
      Specified by:
      squareDistanceBulk in interface ESVectorUtilSupport
    • dotProductBulk

      public void dotProductBulk(byte[] query, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances)
      Specified by:
      dotProductBulk in interface ESVectorUtilSupport
    • cosineBulk

      public void cosineBulk(byte[] query, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances)
      Specified by:
      cosineBulk in interface ESVectorUtilSupport
    • squareDistanceBulk

      public void squareDistanceBulk(byte[] query, int vectorOffset, byte[] v0, byte[] v1, byte[] v2, byte[] v3, int distancesOffset, float[] distances, int length)
      Specified by:
      squareDistanceBulk in interface ESVectorUtilSupport
    • soarDistanceBulk

      public void soarDistanceBulk(float[] v1, float[] c0, float[] c1, float[] c2, float[] c3, float[] originalResidual, float soarLambda, float rnorm, float[] distances)
      Specified by:
      soarDistanceBulk in interface ESVectorUtilSupport
    • soarDistanceBulk

      public void soarDistanceBulk(byte[] v1, byte[] c0, byte[] c1, byte[] c2, byte[] c3, float[] originalResidual, float soarLambda, float rnorm, float[] distances)
      Specified by:
      soarDistanceBulk in interface ESVectorUtilSupport
    • soarDistance

      public float soarDistance(byte[] v1, byte[] centroid, float[] originalResidual, float soarLambda, float rnorm)
      Specified by:
      soarDistance in interface ESVectorUtilSupport
    • packDibit

      public void packDibit(int[] vector, byte[] packed)
      Specified by:
      packDibit in interface ESVectorUtilSupport
    • packDibitQuad

      public void packDibitQuad(int[] vector, byte[] packed)
      Specified by:
      packDibitQuad in interface ESVectorUtilSupport
    • packAsBinary

      public void packAsBinary(int[] vector, byte[] packed)
      Specified by:
      packAsBinary in interface ESVectorUtilSupport
    • 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-3
      packed - 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:
      transposeHalfByte in interface ESVectorUtilSupport
    • transposeHalfByteImpl

      public static void transposeHalfByteImpl(int[] q, byte[] quantQueryByte)
    • indexOf

      public int indexOf(byte[] bytes, int offset, int length, byte marker)
      Specified by:
      indexOf in interface ESVectorUtilSupport
    • codePointCount

      public int codePointCount(org.apache.lucene.util.BytesRef bytesRef)
      Specified by:
      codePointCount in interface ESVectorUtilSupport
    • contains

      public boolean contains(byte[] value, int valueOffset, int valueLength, byte[] term, int termOffset, int termLength)
      Specified by:
      contains in interface ESVectorUtilSupport
    • inRangeBitmask

      public void inRangeBitmask(long[] values, long lowerValue, long upperValue, long[] matches)
      Specified by:
      inRangeBitmask in interface ESVectorUtilSupport
    • linearCombination

      public void linearCombination(float scaleOther, float[] other, float scaleDest, float[] dest)
      Specified by:
      linearCombination in interface ESVectorUtilSupport
    • linearCombination

      public void linearCombination(float scaleOther, float[] other, float[] dest)
      Specified by:
      linearCombination in interface ESVectorUtilSupport
    • linearCombination

      public void linearCombination(float scaleOther, byte[] other, float scaleDest, float[] dest)
      Specified by:
      linearCombination in interface ESVectorUtilSupport
    • linearCombination

      public void linearCombination(float scaleOther, byte[] other, float[] dest)
      Specified by:
      linearCombination in interface ESVectorUtilSupport
    • logSumExpNQT

      public float logSumExpNQT(float[] vector)
      Specified by:
      logSumExpNQT in interface ESVectorUtilSupport
    • logSumExpNQTDiff

      public float logSumExpNQTDiff(float[] v1, float[] v2, float eps)
      Specified by:
      logSumExpNQTDiff in interface ESVectorUtilSupport
    • pow2DiffAndScaleNQT

      public void pow2DiffAndScaleNQT(float[] v1, float[] v2, float a, float eps, float[] result)
      Specified by:
      pow2DiffAndScaleNQT in interface ESVectorUtilSupport