-
Notifications
You must be signed in to change notification settings - Fork 56
FEAT: support cosine similarity #90
Changes from 22 commits
99cc16c
6375d49
28f33f9
784e67f
d29cabd
bde898c
0bdf53e
25cafe9
2920f5b
a9388a9
8a58bf6
de8271d
c49efaf
ce2660f
2c10607
65052c8
3eb7a47
aa26cfb
18a8406
5ebb930
635c7b1
83e4a90
e858038
6cbfc79
66cbbe8
c852d84
0394e93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,11 +31,14 @@ | |
import org.elasticsearch.monitor.jvm.JvmInfo; | ||
import org.elasticsearch.monitor.os.OsProbe; | ||
|
||
import java.security.InvalidParameterException; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
@@ -63,6 +66,7 @@ public class KNNSettings { | |
/** | ||
* Settings name | ||
*/ | ||
public static final String KNN_SPACE_TYPE = "index.knn.space_type"; | ||
public static final String KNN_ALGO_PARAM_M = "index.knn.algo_param.m"; | ||
public static final String KNN_ALGO_PARAM_EF_CONSTRUCTION = "index.knn.algo_param.ef_construction"; | ||
public static final String KNN_ALGO_PARAM_EF_SEARCH = "index.knn.algo_param.ef_search"; | ||
|
@@ -80,6 +84,11 @@ public class KNNSettings { | |
* Settings Definition | ||
*/ | ||
|
||
public static final Setting<String> INDEX_KNN_SPACE_TYPE = Setting.simpleString(KNN_SPACE_TYPE, | ||
"l2", | ||
new SpaceTypeValidator(), | ||
IndexScope); | ||
|
||
/** | ||
* M - the number of bi-directional links created for every new element during construction. | ||
* Reasonable range for M is 2-100. Higher M work better on datasets with high intrinsic | ||
|
@@ -252,7 +261,8 @@ public Setting<?> getSetting(String key) { | |
} | ||
|
||
public List<Setting<?>> getSettings() { | ||
List<Setting<?>> settings = Arrays.asList(INDEX_KNN_ALGO_PARAM_M_SETTING, | ||
List<Setting<?>> settings = Arrays.asList(INDEX_KNN_SPACE_TYPE, | ||
INDEX_KNN_ALGO_PARAM_M_SETTING, | ||
INDEX_KNN_ALGO_PARAM_EF_CONSTRUCTION_SETTING, | ||
INDEX_KNN_ALGO_PARAM_EF_SEARCH_SETTING, | ||
KNN_ALGO_PARAM_INDEX_THREAD_QTY_SETTING, | ||
|
@@ -357,6 +367,16 @@ public static int getEfSearchParam(String index) { | |
return getIndexSettingValue(index, KNN_ALGO_PARAM_EF_SEARCH, 512); | ||
} | ||
|
||
/** | ||
* | ||
* @param index Name of the index | ||
* @return spaceType value | ||
*/ | ||
public static String getSpaceType(String index) { | ||
return KNNSettings.state().clusterService.state().getMetaData() | ||
.index(index).getSettings().get(KNN_SPACE_TYPE, SpaceTypes.l2.getValue()); | ||
} | ||
|
||
public static int getIndexSettingValue(String index, String settingName, int defaultValue) { | ||
return KNNSettings.state().clusterService.state().getMetaData() | ||
.index(index).getSettings() | ||
|
@@ -367,4 +387,48 @@ public void setClusterService(ClusterService clusterService) { | |
this.clusterService = clusterService; | ||
} | ||
|
||
static class SpaceTypeValidator implements Setting.Validator<String> { | ||
|
||
private Set<String> types = SpaceTypes.getValues(); | ||
|
||
@Override public void validate(String value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. null check on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vamshin I put a null check here, but it still fails to guard against null value, i.e. for some reason, if index.knn.space_type == null, it will not go through the validate. Similar bug happens to other parameters, e.g.
yields
How about we open a separate issue to track this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah got it. I think we can probably ignore this case. |
||
if (!types.contains(value.toLowerCase())){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this case insensitive? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I intended to set it case insensitive, e.g. L2, Cosinesimil is also valid string. But I can remove this if not necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think case insensitive is good |
||
throw new InvalidParameterException(String.format("Unsupported space type: %s", value)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Enum contains space types for k-NN similarity search | ||
*/ | ||
public enum SpaceTypes { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Move this enum class to a dedicated file |
||
l2("l2"), | ||
cosinesimil("cosinesimil"); | ||
|
||
private String value; | ||
|
||
SpaceTypes(String value) { this.value = value; } | ||
|
||
/** | ||
* Get space type | ||
* | ||
* @return name | ||
*/ | ||
public String getValue() { return value; } | ||
|
||
/** | ||
* Get all space types | ||
* | ||
* @return set of all stat names | ||
*/ | ||
public static Set<String> getValues() { | ||
Set<String> values = new HashSet<>(); | ||
|
||
for (SpaceTypes spaceType : SpaceTypes.values()) { | ||
values.add(spaceType.getValue()); | ||
} | ||
return values; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,12 +110,13 @@ public void close() { | |
* Loads the knn index to memory for querying the neighbours | ||
* | ||
* @param indexPath path where the hnsw index is stored | ||
* @param spaceType space type of the index | ||
* @param algoParams hnsw algorithm parameters | ||
* @return knn index that can be queried for k nearest neighbours | ||
*/ | ||
public static KNNIndex loadIndex(String indexPath, final String[] algoParams) { | ||
public static KNNIndex loadIndex(String indexPath, String spaceType, final String[] algoParams) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: final String spaceType? |
||
long fileSize = computeFileSize(indexPath); | ||
long indexPointer = init(indexPath, algoParams); | ||
long indexPointer = init(indexPath, spaceType, algoParams); | ||
return new KNNIndex(indexPointer, fileSize); | ||
} | ||
|
||
|
@@ -137,13 +138,13 @@ private static long computeFileSize(String indexPath) { | |
} | ||
|
||
// Builds index and writes to disk (no index pointer escapes). | ||
public static native void saveIndex(int[] ids, float[][] data, String indexPath, String[] algoParams); | ||
public static native void saveIndex(int[] ids, float[][] data, String indexPath, String spaceType, String[] algoParams); | ||
|
||
// Queries index (thread safe with other readers, blocked by write lock) | ||
private static native KNNQueryResult[] queryIndex(long indexPointer, float[] query, int k); | ||
|
||
// Loads index and returns pointer to index | ||
private static native long init(String indexPath, String[] algoParams); | ||
private static native long init(String indexPath, String spaceType, String[] algoParams); | ||
|
||
// Deletes memory pointed to by index pointer (needs write lock) | ||
private static native void gc(long indexPointer); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Please add the new function arguments to the end of the existing parameters list. Please take care in other places as well.