Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require that float vector components are smaller than 1E17 to prevent overflowing to Infinity #12373

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static Query newVectorQuery(String field, float[] queryVector, int k) {
public KnnFloatVectorField(
String name, float[] vector, VectorSimilarityFunction similarityFunction) {
super(name, createType(vector, similarityFunction));
fieldsData = VectorUtil.checkFinite(vector); // null check done above
fieldsData = VectorUtil.checkInBounds(vector); // null check done above
}

/**
Expand Down Expand Up @@ -143,7 +143,7 @@ public KnnFloatVectorField(String name, float[] vector, FieldType fieldType) {
throw new IllegalArgumentException(
"The number of vector dimensions does not match the field type");
}
fieldsData = VectorUtil.checkFinite(vector);
fieldsData = VectorUtil.checkInBounds(vector);
}

/** Return the vector value of this field */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public abstract class FloatVectorValues extends DocIdSetIterator {
/** The maximum length of a vector */
public static final int MAX_DIMENSIONS = 1024;

/**
* This is the largest float vector value that we allow.
*
* <p>The largest float32 that you can square without overflowing is about 1.8E19. We reduce that
* further to accommodate the addition of multiple such components in similarity computations in
* vectors up to MAX_DIMENSIONS in length.
*/
public static final float MAX_FLOAT32_COMPONENT = 1E17f;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

squareDistance is very different than dotProduct and enforcing the same limitations on both seems strange.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true, but there's also something to be said for "here's a simple rule to follow" vs "look up the docs on every distance function to see what the limit is"


/** Sole constructor */
protected FloatVectorValues() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public KnnFloatVectorQuery(String field, float[] target, int k) {
*/
public KnnFloatVectorQuery(String field, float[] target, int k, Query filter) {
super(field, k, filter);
this.target = VectorUtil.checkFinite(Objects.requireNonNull(target, "target"));
this.target = VectorUtil.checkInBounds(Objects.requireNonNull(target, "target"));
}

@Override
Expand Down
11 changes: 9 additions & 2 deletions lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.lucene.util;

import org.apache.lucene.index.FloatVectorValues;

/** Utilities for computations with numeric arrays */
public final class VectorUtil {

Expand Down Expand Up @@ -162,17 +164,22 @@ public static float dotProductScore(byte[] a, byte[] b) {
}

/**
* Checks if a float vector only has finite components.
* Checks if a float vector only has components with absolute value less than
* MAX_FLOAT32_COMPONENT. NaN is not allowed.
*
* @param v bytes containing a vector
* @return the vector for call-chaining
* @throws IllegalArgumentException if any component of vector is not finite
*/
public static float[] checkFinite(float[] v) {
public static float[] checkInBounds(float[] v) {
for (int i = 0; i < v.length; i++) {
if (!Float.isFinite(v[i])) {
throw new IllegalArgumentException("non-finite value at vector[" + i + "]=" + v[i]);
}

if (Math.abs(v[i]) > FloatVectorValues.MAX_FLOAT32_COMPONENT) {
throw new IllegalArgumentException("Out-of-bounds value at vector[" + i + "]=" + v[i]);
}
}
return v;
}
Expand Down
13 changes: 13 additions & 0 deletions lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
*/
package org.apache.lucene.util;

import static org.apache.lucene.index.FloatVectorValues.MAX_FLOAT32_COMPONENT;

import java.util.Arrays;
import java.util.Random;
import org.apache.lucene.index.FloatVectorValues;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.TestUtil;

Expand Down Expand Up @@ -262,4 +266,13 @@ public void testOrthogonalCosineBytes() {
u[1] = -v[0];
assertEquals(0, VectorUtil.cosine(u, v), DELTA);
}

public void testLargeVectorSimilarities() {
float[] v = new float[FloatVectorValues.MAX_DIMENSIONS];
Arrays.fill(v, MAX_FLOAT32_COMPONENT);

assertTrue(Float.isFinite(VectorUtil.cosine(v, v)));
assertTrue(Float.isFinite(VectorUtil.dotProduct(v, v)));
assertTrue(Float.isFinite(VectorUtil.squareDistance(v, v)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ConstKnnFloatValueSource extends ValueSource {
private final float[] vector;

public ConstKnnFloatValueSource(float[] constVector) {
this.vector = VectorUtil.checkFinite(Objects.requireNonNull(constVector, "constVector"));
this.vector = VectorUtil.checkInBounds(Objects.requireNonNull(constVector, "constVector"));
}

@Override
Expand Down