Skip to content

Commit 89b3660

Browse files
committed
Add space type conversion to support Faiss cosine similarity
Signed-off-by: owenhalpert <[email protected]>
1 parent 294b4c7 commit 89b3660

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

Diff for: src/main/java/org/opensearch/knn/index/engine/faiss/Faiss.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public boolean supportsRemoteIndexBuild(MethodComponentContext methodComponentCo
138138
@Override
139139
public RemoteIndexParameters createRemoteIndexingParameters(KNNMethodContext knnMethodContext) {
140140
if (METHOD_HNSW.equals(knnMethodContext.getMethodComponentContext().getName())) {
141-
return FaissHNSWMethod.createRemoteIndexingParameters(knnMethodContext);
141+
FaissHNSWMethod method = (FaissHNSWMethod) METHODS.get(METHOD_HNSW);
142+
return method.createRemoteIndexingParameters(knnMethodContext);
142143
}
143144
throw new IllegalArgumentException("Unsupported method for remote indexing");
144145
}

Diff for: src/main/java/org/opensearch/knn/index/engine/faiss/FaissHNSWMethod.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,11 @@ protected Function<TrainingConfigValidationInput, TrainingConfigValidationOutput
158158
* @param knnMethodContext to parse
159159
* @return Map of parameters to be used as "index_parameters"
160160
*/
161-
public static RemoteIndexParameters createRemoteIndexingParameters(KNNMethodContext knnMethodContext) {
161+
public RemoteIndexParameters createRemoteIndexingParameters(KNNMethodContext knnMethodContext) {
162162
RemoteFaissHNSWIndexParameters.RemoteFaissHNSWIndexParametersBuilder<?, ?> builder = RemoteFaissHNSWIndexParameters.builder();
163163
builder.algorithm(METHOD_HNSW);
164-
builder.spaceType(knnMethodContext.getSpaceType().getValue());
164+
SpaceType spaceType = convertUserToMethodSpaceType(knnMethodContext.getSpaceType());
165+
builder.spaceType(spaceType.getValue());
165166

166167
MethodComponentContext methodComponentContext = knnMethodContext.getMethodComponentContext();
167168
Map<String, Object> methodParams = methodComponentContext.getParameters();

Diff for: src/test/java/org/opensearch/knn/index/engine/KNNEngineTests.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public void testSupportsRemoteIndexBuild() {
125125
}
126126

127127
public void testCreateRemoteIndexingParameters_Success() {
128-
RemoteIndexParameters result = FaissHNSWMethod.createRemoteIndexingParameters(createMockMethodContext());
128+
FaissHNSWMethod method = new FaissHNSWMethod();
129+
RemoteIndexParameters result = method.createRemoteIndexingParameters(createMockMethodContext());
129130

130131
assertNotNull(result);
131132
assertTrue(result instanceof RemoteFaissHNSWIndexParameters);
@@ -139,6 +140,21 @@ public void testCreateRemoteIndexingParameters_Success() {
139140
assertEquals(14, hnswParams.getM());
140141
}
141142

143+
public void testCreateRemoteIndexingParameters_CosineSpaceType() {
144+
FaissHNSWMethod method = new FaissHNSWMethod();
145+
KNNMethodContext context = createMockMethodContextWithCosineSimil();
146+
147+
RemoteIndexParameters result = method.createRemoteIndexingParameters(context);
148+
149+
assertNotNull(result);
150+
assertTrue(result instanceof RemoteFaissHNSWIndexParameters);
151+
152+
RemoteFaissHNSWIndexParameters hnswParams = (RemoteFaissHNSWIndexParameters) result;
153+
154+
// Test that cosine space type is converted to inner product space type for Faiss
155+
assertEquals(SpaceType.INNER_PRODUCT.getValue(), hnswParams.getSpaceType());
156+
}
157+
142158
public static KNNMethodContext createFaissIVFMethodContext() {
143159
MethodComponentContext encoder = new MethodComponentContext(ENCODER_SQ, Map.of());
144160
Map<String, Object> encoderMap = Map.of(METHOD_ENCODER_PARAMETER, encoder);
@@ -178,4 +194,20 @@ public static KNNMethodContext createMockMethodContext() {
178194
return new KNNMethodContext(KNNEngine.FAISS, SpaceType.L2, methodComponentContext);
179195
}
180196

197+
public static KNNMethodContext createMockMethodContextWithCosineSimil() {
198+
MethodComponentContext encoder = new MethodComponentContext(KNNConstants.ENCODER_FLAT, Map.of());
199+
Map<String, Object> parameters = Map.of(
200+
KNNRemoteConstants.METHOD_PARAMETER_EF_SEARCH,
201+
89,
202+
KNNRemoteConstants.METHOD_PARAMETER_EF_CONSTRUCTION,
203+
94,
204+
KNNRemoteConstants.METHOD_PARAMETER_M,
205+
14,
206+
METHOD_PARAMETER_ENCODER,
207+
encoder
208+
);
209+
MethodComponentContext methodComponentContext = new MethodComponentContext(KNNConstants.METHOD_HNSW, parameters);
210+
return new KNNMethodContext(KNNEngine.FAISS, SpaceType.COSINESIMIL, methodComponentContext);
211+
}
212+
181213
}

0 commit comments

Comments
 (0)