Skip to content

Commit

Permalink
fix bwc tests
Browse files Browse the repository at this point in the history
Signed-off-by: AnnTian Shao <[email protected]>
  • Loading branch information
AnnTian Shao committed Jan 29, 2025
1 parent 277e011 commit 9887948
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void testHNSWSQFP16_onUpgradeWhenClipToFp16isTrueAndIndexedWithOutOfFP16R
List<Integer> efConstructionValues = ImmutableList.of(16, 32, 64, 128);
List<Integer> efSearchValues = ImmutableList.of(16, 32, 64, 128);

int dimension = 2;
int dimension = 128;

// Create an index
/**
Expand Down Expand Up @@ -198,16 +198,35 @@ public void testHNSWSQFP16_onUpgradeWhenClipToFp16isTrueAndIndexedWithOutOfFP16R

createKnnIndex(testIndex, mapping);
assertEquals(new TreeMap<>(mappingMap), new TreeMap<>(getIndexMappingAsMap(testIndex)));
Float[] vector1 = { -65523.76f, 65504.2f };
Float[] vector2 = { -270.85f, 65514.2f };
Float[] vector3 = { -150.9f, 65504.0f };
Float[] vector4 = { -20.89f, 100000000.0f };

Float[] vector1 = new Float[dimension];
Float[] vector2 = new Float[dimension];
Float[] vector3 = new Float[dimension];
Float[] vector4 = new Float[dimension];
float[] queryVector = new float[dimension];
int halfDimension = dimension / 2;

for (int i = 0; i < dimension; i++) {
if (i < halfDimension) {
vector1[i] = -65523.76f;
vector2[i] = -270.85f;
vector3[i] = -150.9f;
vector4[i] = -20.89f;
queryVector[i] = -10.5f;
} else {
vector1[i] = 65504.2f;
vector2[i] = 65514.2f;
vector3[i] = 65504.0f;
vector4[i] = 100000000.0f;
queryVector[i] = 25.48f;
}
}

addKnnDoc(testIndex, "1", TEST_FIELD, vector1);
addKnnDoc(testIndex, "2", TEST_FIELD, vector2);
addKnnDoc(testIndex, "3", TEST_FIELD, vector3);
addKnnDoc(testIndex, "4", TEST_FIELD, vector4);

float[] queryVector = { -10.5f, 25.48f };
int k = 4;
Response searchResponse = searchKNNIndex(testIndex, new KNNQueryBuilder(TEST_FIELD, queryVector, k), k);
List<KNNResult> results = parseSearchResponse(EntityUtils.toString(searchResponse.getEntity()), TEST_FIELD);
Expand Down
55 changes: 43 additions & 12 deletions src/test/java/org/opensearch/knn/index/FaissIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ public void testHNSWSQFP16_whenIndexedWithOutOfFP16Range_thenThrowException() {
List<Integer> efConstructionValues = ImmutableList.of(16, 32, 64, 128);
List<Integer> efSearchValues = ImmutableList.of(16, 32, 64, 128);

int dimension = 2;
int dimension = 128;

// Create an index
XContentBuilder builder = XContentFactory.jsonBuilder()
Expand Down Expand Up @@ -830,7 +830,23 @@ public void testHNSWSQFP16_whenIndexedWithOutOfFP16Range_thenThrowException() {

createKnnIndex(indexName, mapping);
assertEquals(new TreeMap<>(mappingMap), new TreeMap<>(getIndexMappingAsMap(indexName)));
Float[] vector = { -10.76f, 65504.2f };

Float[] vector = new Float[dimension];
Float[] vector1 = new Float[dimension];
Float[] vector2 = new Float[dimension];
int halfDimension = dimension / 2;

for (int i = 0; i < dimension; i++) {
if (i < halfDimension) {
vector[i] = -10.76f;
vector1[i] = -65506.84f;
vector2[i] = -65526.4567f;
} else {
vector[i] = 65504.2f;
vector1[i] = 12.56f;
vector2[i] = 65526.4567f;
}
}

ResponseException ex = expectThrows(ResponseException.class, () -> addKnnDoc(indexName, "1", fieldName, vector));
assertTrue(
Expand All @@ -847,8 +863,6 @@ public void testHNSWSQFP16_whenIndexedWithOutOfFP16Range_thenThrowException() {
)
);

Float[] vector1 = { -65506.84f, 12.56f };

ResponseException ex1 = expectThrows(ResponseException.class, () -> addKnnDoc(indexName, "2", fieldName, vector1));
assertTrue(
ex1.getMessage()
Expand All @@ -864,8 +878,6 @@ public void testHNSWSQFP16_whenIndexedWithOutOfFP16Range_thenThrowException() {
)
);

Float[] vector2 = { -65526.4567f, 65526.4567f };

ResponseException ex2 = expectThrows(ResponseException.class, () -> addKnnDoc(indexName, "3", fieldName, vector2));
assertTrue(
ex2.getMessage()
Expand Down Expand Up @@ -893,7 +905,7 @@ public void testHNSWSQFP16_whenClipToFp16isTrueAndIndexedWithOutOfFP16Range_then
List<Integer> efConstructionValues = ImmutableList.of(16, 32, 64, 128);
List<Integer> efSearchValues = ImmutableList.of(16, 32, 64, 128);

int dimension = 2;
int dimension = 128;

// Create an index
XContentBuilder builder = XContentFactory.jsonBuilder()
Expand Down Expand Up @@ -928,16 +940,35 @@ public void testHNSWSQFP16_whenClipToFp16isTrueAndIndexedWithOutOfFP16Range_then

createKnnIndex(indexName, mapping);
assertEquals(new TreeMap<>(mappingMap), new TreeMap<>(getIndexMappingAsMap(indexName)));
Float[] vector1 = { -65523.76f, 65504.2f };
Float[] vector2 = { -270.85f, 65514.2f };
Float[] vector3 = { -150.9f, 65504.0f };
Float[] vector4 = { -20.89f, 100000000.0f };

Float[] vector1 = new Float[dimension];
Float[] vector2 = new Float[dimension];
Float[] vector3 = new Float[dimension];
Float[] vector4 = new Float[dimension];
float[] queryVector = new float[dimension];
int halfDimension = dimension / 2;

for (int i = 0; i < dimension; i++) {
if (i < halfDimension) {
vector1[i] = -65523.76f;
vector2[i] = -270.85f;
vector3[i] = -150.9f;
vector4[i] = -20.89f;
queryVector[i] = -10.5f;
} else {
vector1[i] = 65504.2f;
vector2[i] = 65514.2f;
vector3[i] = 65504.0f;
vector4[i] = 100000000.0f;
queryVector[i] = 25.48f;
}
}

addKnnDoc(indexName, "1", fieldName, vector1);
addKnnDoc(indexName, "2", fieldName, vector2);
addKnnDoc(indexName, "3", fieldName, vector3);
addKnnDoc(indexName, "4", fieldName, vector4);

float[] queryVector = { -10.5f, 25.48f };
int k = 4;
Response searchResponse = searchKNNIndex(indexName, new KNNQueryBuilder(fieldName, queryVector, k), k);
List<KNNResult> results = parseSearchResponse(EntityUtils.toString(searchResponse.getEntity()), fieldName);
Expand Down

0 comments on commit 9887948

Please sign in to comment.