Skip to content

Commit

Permalink
Fix a flaky unit test:testMultiFieldsKnnIndex, which was failing due …
Browse files Browse the repository at this point in the history
…to inconsistent merge behaviors (opensearch-project#1924)

Signed-off-by: Navneet Verma <navneev@amazon.com>

Quantization Framework Implementation with 1bit, 2bit and 4bit Binary Quantizer
  • Loading branch information
navneet1v authored and Vikasht34 committed Aug 2, 2024
1 parent 93d3112 commit c0e464a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Infrastructure
### Documentation
### Maintenance
* Fix a flaky unit test:testMultiFieldsKnnIndex, which was failing due to inconsistent merge behaviors [#1924](https://github.com/opensearch-project/k-NN/pull/1924)
### Refactoring
* Introduce KNNVectorValues interface to iterate on different types of Vector values during indexing and search [#1897](https://github.com/opensearch-project/k-NN/pull/1897)
* Clean up parsing for query [#1824](https://github.com/opensearch-project/k-NN/pull/1824)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
* SamplingFactory is a factory class for creating instances of Sampler.
* It uses the factory design pattern to encapsulate the creation logic for different types of samplers.
*/
public class SamplingFactory {
public final class SamplingFactory {

/**
* Private constructor to prevent instantiation of this class.
* The class is not meant to be instantiated, as it provides static methods only.
*/
private SamplingFactory() {

}

/**
* SamplerType is an enumeration of the different types of samplers that can be created by the factory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import org.opensearch.knn.quantization.models.quantizationParams.QuantizationParams;
import org.opensearch.knn.quantization.models.quantizationState.QuantizationState;

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

/**
* QuantizationStateSerializer is a utility class that provides methods for serializing and deserializing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,27 @@ public void testMultiFieldsKnnIndex(Codec codec) throws Exception {
Document doc = new Document();
doc.add(vectorField);
writer.addDocument(doc);
writer.close();
// ensuring the refresh happens, to create the segment and hnsw file
writer.flush();

/**
* Add doc with field "my_vector"
*/
IndexWriterConfig iwc1 = newIndexWriterConfig();
iwc1.setMergeScheduler(new SerialMergeScheduler());
iwc1.setCodec(ACTUAL_CODEC);
writer = new RandomIndexWriter(random(), dir, iwc1);
float[] array1 = { 6.0f, 14.0f };
VectorField vectorField1 = new VectorField("my_vector", array1, sampleFieldType);
Document doc1 = new Document();
doc1.add(vectorField1);
writer.addDocument(doc1);
// ensuring the refresh happens, to create the segment and hnsw file
writer.flush();
IndexReader reader = writer.getReader();
writer.close();
ResourceWatcherService resourceWatcherService = createDisabledResourceWatcherService();
NativeMemoryLoadStrategy.IndexLoadStrategy.initialize(resourceWatcherService);
List<String> hnswfiles = Arrays.stream(dir.listAll()).filter(x -> x.contains("hnsw")).collect(Collectors.toList());

// there should be 2 hnsw index files created. one for test_vector and one for my_vector
assertEquals(hnswfiles.size(), 2);
assertEquals(2, hnswfiles.size());
assertEquals(hnswfiles.stream().filter(x -> x.contains("test_vector")).collect(Collectors.toList()).size(), 1);
assertEquals(hnswfiles.stream().filter(x -> x.contains("my_vector")).collect(Collectors.toList()).size(), 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@

package org.opensearch.knn.quantization.factory;

<<<<<<< HEAD
import org.junit.Before;
=======
import org.junit.BeforeClass;
import org.junit.Test;
>>>>>>> b93c2a3 (Quantization Framework Implementation with 1bit, 2bit and 4bit Binary Quantizer)
import org.opensearch.knn.KNNTestCase;
import org.opensearch.knn.quantization.enums.SQTypes;
import org.opensearch.knn.quantization.models.quantizationParams.SQParams;
import org.opensearch.knn.quantization.quantizer.MultiBitScalarQuantizer;
import org.opensearch.knn.quantization.quantizer.OneBitScalarQuantizer;
import org.opensearch.knn.quantization.quantizer.Quantizer;

<<<<<<< HEAD
import java.lang.reflect.Field;

public class QuantizerFactoryTests extends KNNTestCase {
Expand All @@ -36,35 +30,27 @@ public void test_Lazy_Registration() {
Quantizer<?, ?> quantizer = QuantizerFactory.getQuantizer(params);
assertTrue(quantizer instanceof OneBitScalarQuantizer);
assertTrue(isRegisteredFieldAccessible());
=======
public class QuantizerFactoryTests extends KNNTestCase {

public void testLazyRegistration() {
SQParams params = new SQParams(SQTypes.ONE_BIT);
Quantizer<?, ?> quantizer = QuantizerFactory.getQuantizer(params);
assertTrue(quantizer instanceof OneBitScalarQuantizer);
>>>>>>> b93c2a3 (Quantization Framework Implementation with 1bit, 2bit and 4bit Binary Quantizer)
}

public void testGetQuantizer_withOneBitSQParams() {
public void testGetQuantizer_withOneBitSQParams () {
SQParams params = new SQParams(SQTypes.ONE_BIT);
Quantizer<?, ?> quantizer = QuantizerFactory.getQuantizer(params);
assertTrue(quantizer instanceof OneBitScalarQuantizer);
}

public void testGetQuantizer_withTwoBitSQParams() {
public void testGetQuantizer_withTwoBitSQParams () {
SQParams params = new SQParams(SQTypes.TWO_BIT);
Quantizer<?, ?> quantizer = QuantizerFactory.getQuantizer(params);
assertTrue(quantizer instanceof MultiBitScalarQuantizer);
}

public void testGetQuantizer_withFourBitSQParams() {
public void testGetQuantizer_withFourBitSQParams () {
SQParams params = new SQParams(SQTypes.FOUR_BIT);
Quantizer<?, ?> quantizer = QuantizerFactory.getQuantizer(params);
assertTrue(quantizer instanceof MultiBitScalarQuantizer);
}

public void testGetQuantizer_withUnsupportedType() {
public void testGetQuantizer_withUnsupportedType () {
SQParams params = new SQParams(SQTypes.UNSUPPORTED_TYPE);
try {
QuantizerFactory.getQuantizer(params);
Expand All @@ -73,11 +59,7 @@ public void testGetQuantizer_withUnsupportedType() {
assertTrue(e.getMessage().contains("No quantizer registered for type identifier"));
}
}
<<<<<<< HEAD

=======
>>>>>>> b93c2a3 (Quantization Framework Implementation with 1bit, 2bit and 4bit Binary Quantizer)
public void testGetQuantizer_withNullParams() {
public void testGetQuantizer_withNullParams () {
try {
QuantizerFactory.getQuantizer(null);
fail("Expected IllegalArgumentException");
Expand All @@ -86,12 +68,7 @@ public void testGetQuantizer_withNullParams() {
}
}

<<<<<<< HEAD

public void test_Concurrent_Registration() throws InterruptedException {
=======
public void testConcurrentRegistration() throws InterruptedException {
>>>>>>> b93c2a3 (Quantization Framework Implementation with 1bit, 2bit and 4bit Binary Quantizer)
public void testConcurrentRegistration () throws InterruptedException {
Runnable task = () -> {
SQParams params = new SQParams(SQTypes.ONE_BIT);
QuantizerFactory.getQuantizer(params);
Expand All @@ -103,12 +80,10 @@ >>>>>>> b93c2a3 (Quantization Framework Implementation with 1bit, 2bit and 4bit
thread2.start();
thread1.join();
thread2.join();

<<<<<<< HEAD
assertTrue(isRegisteredFieldAccessible());
}

private boolean isRegisteredFieldAccessible() {
private boolean isRegisteredFieldAccessible () {
try {
Field field = QuantizerFactory.class.getDeclaredField("isRegistered");
field.setAccessible(true);
Expand All @@ -117,8 +92,5 @@ private boolean isRegisteredFieldAccessible() {
fail("Failed to access isRegistered field.");
return false;
}
=======
// No need to check isRegistered flag, as it doesn't exist anymore
>>>>>>> b93c2a3 (Quantization Framework Implementation with 1bit, 2bit and 4bit Binary Quantizer)
}
}

0 comments on commit c0e464a

Please sign in to comment.