-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: John Mazanec <[email protected]>
- Loading branch information
1 parent
1eebcf6
commit 95b7cda
Showing
19 changed files
with
1,486 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/test/java/org/opensearch/knn/index/engine/AbstractMethodResolverTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.engine; | ||
|
||
import org.opensearch.knn.KNNTestCase; | ||
import org.opensearch.knn.index.SpaceType; | ||
import org.opensearch.knn.index.VectorDataType; | ||
import org.opensearch.knn.index.mapper.CompressionLevel; | ||
import org.opensearch.knn.index.mapper.Mode; | ||
|
||
import java.util.Map; | ||
|
||
import static org.opensearch.knn.common.KNNConstants.METHOD_ENCODER_PARAMETER; | ||
import static org.opensearch.knn.common.KNNConstants.METHOD_HNSW; | ||
|
||
public class AbstractMethodResolverTests extends KNNTestCase { | ||
|
||
private final static String ENCODER_NAME = "test"; | ||
private final static CompressionLevel DEFAULT_COMPRESSION = CompressionLevel.x8; | ||
|
||
private final static AbstractMethodResolver TEST_RESOLVER = new AbstractMethodResolver() { | ||
@Override | ||
public ResolvedMethodContext resolveMethod( | ||
KNNMethodContext knnMethodContext, | ||
KNNMethodConfigContext knnMethodConfigContext, | ||
boolean shouldRequireTraining, | ||
SpaceType spaceType | ||
) { | ||
return null; | ||
} | ||
}; | ||
|
||
private final static Encoder TEST_ENCODER = new Encoder() { | ||
|
||
@Override | ||
public MethodComponent getMethodComponent() { | ||
return MethodComponent.Builder.builder(ENCODER_NAME).build(); | ||
} | ||
|
||
@Override | ||
public CompressionLevel calculateCompressionLevel( | ||
MethodComponentContext encoderContext, | ||
KNNMethodConfigContext knnMethodConfigContext | ||
) { | ||
return DEFAULT_COMPRESSION; | ||
} | ||
}; | ||
|
||
private final static Map<String, Encoder> ENCODER_MAP = Map.of(ENCODER_NAME, TEST_ENCODER); | ||
|
||
public void testResolveCompressionLevelFromMethodContext() { | ||
assertEquals( | ||
CompressionLevel.NOT_CONFIGURED, | ||
TEST_RESOLVER.resolveCompressionLevelFromMethodContext( | ||
new KNNMethodContext(KNNEngine.DEFAULT, SpaceType.DEFAULT, MethodComponentContext.EMPTY), | ||
KNNMethodConfigContext.builder().build(), | ||
ENCODER_MAP | ||
) | ||
); | ||
assertEquals( | ||
DEFAULT_COMPRESSION, | ||
TEST_RESOLVER.resolveCompressionLevelFromMethodContext( | ||
new KNNMethodContext( | ||
KNNEngine.DEFAULT, | ||
SpaceType.DEFAULT, | ||
new MethodComponentContext( | ||
METHOD_HNSW, | ||
Map.of(METHOD_ENCODER_PARAMETER, new MethodComponentContext(ENCODER_NAME, Map.of())) | ||
) | ||
), | ||
KNNMethodConfigContext.builder().build(), | ||
ENCODER_MAP | ||
) | ||
); | ||
} | ||
|
||
public void testIsEncoderSpecified() { | ||
assertFalse(TEST_RESOLVER.isEncoderSpecified(null)); | ||
assertFalse( | ||
TEST_RESOLVER.isEncoderSpecified(new KNNMethodContext(KNNEngine.DEFAULT, SpaceType.DEFAULT, MethodComponentContext.EMPTY)) | ||
); | ||
assertFalse( | ||
TEST_RESOLVER.isEncoderSpecified( | ||
new KNNMethodContext(KNNEngine.DEFAULT, SpaceType.DEFAULT, new MethodComponentContext(METHOD_HNSW, Map.of())) | ||
) | ||
); | ||
assertTrue( | ||
TEST_RESOLVER.isEncoderSpecified( | ||
new KNNMethodContext( | ||
KNNEngine.DEFAULT, | ||
SpaceType.DEFAULT, | ||
new MethodComponentContext(METHOD_HNSW, Map.of(METHOD_ENCODER_PARAMETER, "test")) | ||
) | ||
) | ||
); | ||
} | ||
|
||
public void testShouldEncoderBeResolved() { | ||
assertFalse( | ||
TEST_RESOLVER.shouldEncoderBeResolved( | ||
new KNNMethodContext( | ||
KNNEngine.DEFAULT, | ||
SpaceType.DEFAULT, | ||
new MethodComponentContext(METHOD_HNSW, Map.of(METHOD_ENCODER_PARAMETER, "test")) | ||
), | ||
KNNMethodConfigContext.builder().build() | ||
) | ||
); | ||
assertFalse( | ||
TEST_RESOLVER.shouldEncoderBeResolved(null, KNNMethodConfigContext.builder().compressionLevel(CompressionLevel.x1).build()) | ||
); | ||
assertFalse( | ||
TEST_RESOLVER.shouldEncoderBeResolved( | ||
null, | ||
KNNMethodConfigContext.builder().compressionLevel(CompressionLevel.x1).mode(Mode.ON_DISK).build() | ||
) | ||
); | ||
assertFalse( | ||
TEST_RESOLVER.shouldEncoderBeResolved( | ||
null, | ||
KNNMethodConfigContext.builder().compressionLevel(CompressionLevel.NOT_CONFIGURED).mode(Mode.IN_MEMORY).build() | ||
) | ||
); | ||
assertFalse( | ||
TEST_RESOLVER.shouldEncoderBeResolved( | ||
null, | ||
KNNMethodConfigContext.builder() | ||
.compressionLevel(CompressionLevel.NOT_CONFIGURED) | ||
.mode(Mode.ON_DISK) | ||
.vectorDataType(VectorDataType.BINARY) | ||
.build() | ||
) | ||
); | ||
assertTrue( | ||
TEST_RESOLVER.shouldEncoderBeResolved( | ||
null, | ||
KNNMethodConfigContext.builder() | ||
.compressionLevel(CompressionLevel.NOT_CONFIGURED) | ||
.mode(Mode.ON_DISK) | ||
.vectorDataType(VectorDataType.FLOAT) | ||
.build() | ||
) | ||
); | ||
assertTrue( | ||
TEST_RESOLVER.shouldEncoderBeResolved( | ||
null, | ||
KNNMethodConfigContext.builder() | ||
.compressionLevel(CompressionLevel.x32) | ||
.mode(Mode.ON_DISK) | ||
.vectorDataType(VectorDataType.FLOAT) | ||
.build() | ||
) | ||
); | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
src/test/java/org/opensearch/knn/index/engine/EngineResolverTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.engine; | ||
|
||
import org.opensearch.knn.KNNTestCase; | ||
import org.opensearch.knn.index.SpaceType; | ||
import org.opensearch.knn.index.mapper.CompressionLevel; | ||
import org.opensearch.knn.index.mapper.Mode; | ||
|
||
public class EngineResolverTests extends KNNTestCase { | ||
|
||
private static final EngineResolver ENGINE_RESOLVER = EngineResolver.INSTANCE; | ||
|
||
public void testResolveEngine_whenEngineSpecifiedInMethod_thenThatEngine() { | ||
assertEquals( | ||
KNNEngine.LUCENE, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().build(), | ||
new KNNMethodContext(KNNEngine.LUCENE, SpaceType.DEFAULT, MethodComponentContext.EMPTY), | ||
false | ||
) | ||
); | ||
} | ||
|
||
public void testResolveEngine_whenRequiresTraining_thenFaiss() { | ||
assertEquals(KNNEngine.FAISS, ENGINE_RESOLVER.resolveEngine(KNNMethodConfigContext.builder().build(), null, true)); | ||
} | ||
|
||
public void testResolveEngine_whenModeAndCompressionAreFalse_thenDefault() { | ||
assertEquals(KNNEngine.DEFAULT, ENGINE_RESOLVER.resolveEngine(KNNMethodConfigContext.builder().build(), null, false)); | ||
assertEquals( | ||
KNNEngine.DEFAULT, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().build(), | ||
new KNNMethodContext(KNNEngine.DEFAULT, SpaceType.UNDEFINED, MethodComponentContext.EMPTY, false), | ||
false | ||
) | ||
); | ||
} | ||
|
||
public void testResolveEngine_whenModeSpecifiedAndCompressionIsNotSpecified_thenDefault() { | ||
assertEquals(KNNEngine.DEFAULT, ENGINE_RESOLVER.resolveEngine(KNNMethodConfigContext.builder().build(), null, false)); | ||
assertEquals( | ||
KNNEngine.DEFAULT, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.IN_MEMORY).build(), | ||
new KNNMethodContext(KNNEngine.DEFAULT, SpaceType.UNDEFINED, MethodComponentContext.EMPTY, false), | ||
false | ||
) | ||
); | ||
} | ||
|
||
public void testResolveEngine_whenCompressionIs1x_thenEngineBasedOnMode() { | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.ON_DISK).compressionLevel(CompressionLevel.x1).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.DEFAULT, | ||
ENGINE_RESOLVER.resolveEngine(KNNMethodConfigContext.builder().compressionLevel(CompressionLevel.x1).build(), null, false) | ||
); | ||
} | ||
|
||
public void testResolveEngine_whenCompressionIs4x_thenEngineIsLucene() { | ||
assertEquals( | ||
KNNEngine.LUCENE, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.ON_DISK).compressionLevel(CompressionLevel.x4).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.LUCENE, | ||
ENGINE_RESOLVER.resolveEngine(KNNMethodConfigContext.builder().compressionLevel(CompressionLevel.x4).build(), null, false) | ||
); | ||
} | ||
|
||
public void testResolveEngine_whenConfiguredForBQ_thenEngineIsFaiss() { | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.ON_DISK).compressionLevel(CompressionLevel.x2).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.IN_MEMORY).compressionLevel(CompressionLevel.x2).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.ON_DISK).compressionLevel(CompressionLevel.x8).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.IN_MEMORY).compressionLevel(CompressionLevel.x8).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.ON_DISK).compressionLevel(CompressionLevel.x16).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.IN_MEMORY).compressionLevel(CompressionLevel.x16).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.ON_DISK).compressionLevel(CompressionLevel.x32).build(), | ||
null, | ||
false | ||
) | ||
); | ||
assertEquals( | ||
KNNEngine.FAISS, | ||
ENGINE_RESOLVER.resolveEngine( | ||
KNNMethodConfigContext.builder().mode(Mode.IN_MEMORY).compressionLevel(CompressionLevel.x32).build(), | ||
null, | ||
false | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.