-
Notifications
You must be signed in to change notification settings - Fork 138
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
add more parameters for text embedding model #640
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ client/build/ | |
common/build/ | ||
ml-algorithms/build/ | ||
plugin/build/ | ||
.DS_Store |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,12 +33,19 @@ public class TextEmbeddingModelConfig extends MLModelConfig { | |
|
||
public static final String EMBEDDING_DIMENSION_FIELD = "embedding_dimension"; | ||
public static final String FRAMEWORK_TYPE_FIELD = "framework_type"; | ||
public static final String POOLING_METHOD_FIELD = "pooling_method"; | ||
public static final String NORMALIZE_RESULT_FIELD = "normalize_result"; | ||
public static final String MODEL_MAX_LENGTH_FIELD = "model_max_length"; | ||
|
||
private Integer embeddingDimension; | ||
private FrameworkType frameworkType; | ||
private final Integer embeddingDimension; | ||
private final FrameworkType frameworkType; | ||
private final PoolingMethod poolingMethod; | ||
private final boolean normalizeResult; | ||
private final Integer modelMaxLength; | ||
|
||
@Builder(toBuilder = true) | ||
public TextEmbeddingModelConfig(String modelType, Integer embeddingDimension, FrameworkType frameworkType, String allConfig) { | ||
public TextEmbeddingModelConfig(String modelType, Integer embeddingDimension, FrameworkType frameworkType, String allConfig, | ||
PoolingMethod poolingMethod, boolean normalizeResult, Integer modelMaxLength) { | ||
super(modelType, allConfig); | ||
if (embeddingDimension == null) { | ||
throw new IllegalArgumentException("embedding dimension is null"); | ||
|
@@ -48,13 +55,23 @@ public TextEmbeddingModelConfig(String modelType, Integer embeddingDimension, Fr | |
} | ||
this.embeddingDimension = embeddingDimension; | ||
this.frameworkType = frameworkType; | ||
if (poolingMethod != null) { | ||
this.poolingMethod = poolingMethod; | ||
} else { | ||
this.poolingMethod = PoolingMethod.MEAN; | ||
} | ||
this.normalizeResult = normalizeResult; | ||
this.modelMaxLength = modelMaxLength; | ||
} | ||
|
||
public static TextEmbeddingModelConfig parse(XContentParser parser) throws IOException { | ||
String modelType = null; | ||
Integer embeddingDimension = null; | ||
FrameworkType frameworkType = null; | ||
String allConfig = null; | ||
PoolingMethod poolingMethod = PoolingMethod.MEAN; | ||
boolean normalizeResult = false; | ||
Integer modelMaxLength = null; | ||
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 don't we have a default value for modelMaxLength? 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. We depend on DJL engine to set the default value. |
||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
|
@@ -74,12 +91,21 @@ public static TextEmbeddingModelConfig parse(XContentParser parser) throws IOExc | |
case ALL_CONFIG_FIELD: | ||
allConfig = parser.text(); | ||
break; | ||
case POOLING_METHOD_FIELD: | ||
poolingMethod = PoolingMethod.from(parser.text().toUpperCase(Locale.ROOT)); | ||
break; | ||
case NORMALIZE_RESULT_FIELD: | ||
normalizeResult = parser.booleanValue(); | ||
break; | ||
case MODEL_MAX_LENGTH_FIELD: | ||
modelMaxLength = parser.intValue(); | ||
break; | ||
default: | ||
parser.skipChildren(); | ||
break; | ||
} | ||
} | ||
return new TextEmbeddingModelConfig(modelType, embeddingDimension, frameworkType, allConfig); | ||
return new TextEmbeddingModelConfig(modelType, embeddingDimension, frameworkType, allConfig, poolingMethod, normalizeResult, modelMaxLength); | ||
} | ||
|
||
@Override | ||
|
@@ -91,13 +117,19 @@ public TextEmbeddingModelConfig(StreamInput in) throws IOException{ | |
super(in); | ||
embeddingDimension = in.readInt(); | ||
frameworkType = in.readEnum(FrameworkType.class); | ||
poolingMethod = in.readEnum(PoolingMethod.class); | ||
normalizeResult = in.readBoolean(); | ||
modelMaxLength = in.readOptionalInt(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeInt(embeddingDimension); | ||
out.writeEnum(frameworkType); | ||
out.writeEnum(poolingMethod); | ||
out.writeBoolean(normalizeResult); | ||
out.writeOptionalInt(modelMaxLength); | ||
} | ||
|
||
@Override | ||
|
@@ -115,13 +147,31 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
if (allConfig != null) { | ||
builder.field(ALL_CONFIG_FIELD, allConfig); | ||
} | ||
if (modelMaxLength != null) { | ||
builder.field(MODEL_MAX_LENGTH_FIELD, modelMaxLength); | ||
} | ||
builder.field(POOLING_METHOD_FIELD, poolingMethod); | ||
builder.field(NORMALIZE_RESULT_FIELD, normalizeResult); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public enum PoolingMethod { | ||
MEAN, | ||
CLS; | ||
|
||
public static PoolingMethod from(String value) { | ||
try { | ||
return PoolingMethod.valueOf(value); | ||
} catch (Exception e) { | ||
throw new IllegalArgumentException("Wrong pooling method"); | ||
} | ||
} | ||
} | ||
public enum FrameworkType { | ||
HUGGINGFACE_TRANSFORMERS, | ||
SENTENCE_TRANSFORMERS; | ||
SENTENCE_TRANSFORMERS, | ||
HUGGINGFACE_TRANSFORMERS_NEURON; | ||
|
||
public static FrameworkType from(String value) { | ||
try { | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If we set the default value below, We don't need this else branch.
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 guess you mean if we set default value in line 72
PoolingMethod poolingMethod = null;
, we don't need line 61this.poolingMethod = PoolingMethod.MEAN;
?I think we still need this. This is constructor method, user can create a new object directly without calling
parse
method