-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persist model definition in model metadata #1527
Changes from 5 commits
98ef3dd
0ba728e
d24baad
70a2f31
cf049ea
1bf49c2
a1f9e81
bd723e2
4dcdaee
f3a5521
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,21 +14,25 @@ | |
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import org.apache.commons.lang.math.NumberUtils; | ||
import org.opensearch.Version; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.common.io.stream.Writeable; | ||
import org.opensearch.core.xcontent.ToXContentFragment; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.index.mapper.MapperParsingException; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import org.apache.commons.lang.builder.EqualsBuilder; | ||
import org.apache.commons.lang.builder.HashCodeBuilder; | ||
import org.opensearch.knn.indices.ModelMetadata; | ||
|
||
import static org.opensearch.knn.common.KNNConstants.NAME; | ||
import static org.opensearch.knn.common.KNNConstants.PARAMETERS; | ||
|
@@ -41,6 +45,11 @@ | |
@RequiredArgsConstructor | ||
public class MethodComponentContext implements ToXContentFragment, Writeable { | ||
|
||
public static final MethodComponentContext DEFAULT = new MethodComponentContext("", Collections.emptyMap()); | ||
|
||
private static final String DELIMITER = ";"; | ||
private static final String DELIMITER_PLACEHOLDER = "$%$"; | ||
|
||
@Getter | ||
private final String name; | ||
private final Map<String, Object> parameters; | ||
|
@@ -161,6 +170,15 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
return builder; | ||
} | ||
|
||
public static MethodComponentContext fromXContent(XContentParser xContentParser) throws IOException { | ||
// If it is a fresh parser, move to the first token | ||
if (xContentParser.currentToken() == null) { | ||
xContentParser.nextToken(); | ||
} | ||
Map<String, Object> parsedMap = xContentParser.map(); | ||
return MethodComponentContext.parse(parsedMap); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
|
@@ -193,6 +211,102 @@ public Map<String, Object> getParameters() { | |
return parameters; | ||
} | ||
|
||
/** | ||
* | ||
* Provides a String representation of MethodComponentContext | ||
* Sample return: | ||
* {name=ivf;parameters=[nlist=4;type=fp16;encoder={name=sq;parameters=[nprobes=2;clip=false;]};]} | ||
* | ||
* @return string representation | ||
*/ | ||
public String toClusterStateString() { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append("{name=").append(name).append(DELIMITER); | ||
stringBuilder.append("parameters=["); | ||
if (Objects.nonNull(parameters)) { | ||
for (Map.Entry<String, Object> entry : parameters.entrySet()) { | ||
stringBuilder.append(entry.getKey()).append("="); | ||
ryanbogan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Object objectValue = entry.getValue(); | ||
String value; | ||
if (objectValue instanceof MethodComponentContext) { | ||
value = ((MethodComponentContext) objectValue).toClusterStateString(); | ||
} else { | ||
value = entry.getValue().toString(); | ||
} | ||
// Model Metadata uses a delimiter to split the input string in its fromString method | ||
ryanbogan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// https://github.com/opensearch-project/k-NN/blob/2.12/src/main/java/org/opensearch/knn/indices/ModelMetadata.java#L265 | ||
// If any of the values in the method component context contain this delimiter, | ||
ryanbogan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// then the method will not work correctly. Therefore, we replace the delimiter with an uncommon | ||
// sequence that is very unlikely to appear in the value itself. | ||
// https://github.com/opensearch-project/k-NN/issues/1337 | ||
value = value.replace(ModelMetadata.DELIMITER, DELIMITER_PLACEHOLDER); | ||
stringBuilder.append(value).append(DELIMITER); | ||
} | ||
} | ||
stringBuilder.append("]}"); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
/** | ||
ryanbogan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* This method converts a string created by the toClusterStateString() method of MethodComponentContext | ||
* to a MethodComponentContext object. | ||
* | ||
* @param in a string representation of MethodComponentContext | ||
* @return a MethodComponentContext object | ||
*/ | ||
public static MethodComponentContext fromString(String in) { | ||
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. This method will need a few comments describing what it will do. Also, lets add java doc 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. Added a java doc with a small description. Let me know if I need to add more 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. name this fromClusterStateString for consistency. |
||
int index = 0; | ||
String[] outerMethodComponentContextArray = in.split("\\{", -1); | ||
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 do we need "\"? 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. The split method takes a regex as an input. { is a special character in regex so the \ escapes it to just split by { 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. Can we avoid this by just confirming the first and last character are {} and then building a substring from then which is processed. 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. The split still needs to be there for the recursion to work. Otherwise, I can't pass the whole substring of a nested MethodComponentContext because it will be split by ; |
||
if (outerMethodComponentContextArray[index].isEmpty()) { | ||
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. Can we split this into a few methods? maybe parse name and parse parameters? 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. Sure, I'll try to see if it works. 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. Added a parse parameters method. Didn't add a parse name method because it's only two lines |
||
index++; | ||
} | ||
String[] innerMethodComponentContextArray = outerMethodComponentContextArray[index].split(DELIMITER, -1); | ||
index++; | ||
String name = innerMethodComponentContextArray[0].substring(innerMethodComponentContextArray[0].indexOf("=") + 1); | ||
Map<String, Object> parameters = parseParameters(innerMethodComponentContextArray, outerMethodComponentContextArray, index); | ||
|
||
return new MethodComponentContext(name, parameters); | ||
} | ||
|
||
private static Map<String, Object> parseParameters( | ||
String[] innerMethodComponentContextArray, | ||
String[] outerMethodComponentContextArray, | ||
int index | ||
) { | ||
Map<String, Object> parameters = new HashMap<>(); | ||
if (innerMethodComponentContextArray.length > 2) { | ||
for (int i = 1; i < innerMethodComponentContextArray.length; i++) { | ||
String substring = innerMethodComponentContextArray[i]; | ||
if (i == 1) { | ||
substring = substring.substring(substring.indexOf("=") + 2); | ||
} | ||
if (substring.charAt(0) == ']') { | ||
break; | ||
} | ||
String key = substring.substring(0, substring.indexOf("=")); | ||
String stringValue = substring.substring(substring.indexOf("=") + 1); | ||
Object value; | ||
// Parameters will always be a MethodComponentContext, String, integer, or boolean | ||
// https://github.com/opensearch-project/k-NN/blob/2.12/src/main/java/org/opensearch/knn/index/Parameter.java | ||
if (stringValue.isEmpty()) { | ||
value = fromString(outerMethodComponentContextArray[index]); | ||
} else if (NumberUtils.isNumber(stringValue)) { | ||
value = Integer.parseInt(stringValue); | ||
} else if (stringValue.equals("true") || stringValue.equals("false")) { | ||
value = Boolean.parseBoolean(stringValue); | ||
} else { | ||
stringValue = stringValue.replace(DELIMITER_PLACEHOLDER, ModelMetadata.DELIMITER); | ||
value = stringValue; | ||
ryanbogan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
parameters.put(key, value); | ||
} | ||
} else { | ||
parameters = Collections.emptyMap(); | ||
} | ||
return parameters; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(this.name); | ||
|
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.
why do we need { and } at beginning and end? can we not skip it?
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.
I added those to help with the recursion if there is a nested MethodComponentContext object. Otherwise, there is no way to differentiate the object from a normal field.