-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] adds new trained model alias API to simplify trained model updates and deployments #68922
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
Merged
benwtrent
merged 15 commits into
elastic:master
from
benwtrent:feature/ml-trained-model-alias
Feb 18, 2021
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b4a20f1
[ML] adds new trained model aliases API to simplify trained model upd…
benwtrent d1956c0
fixing docs for model alias
benwtrent 59e00ba
fixing tests
benwtrent 0da41c9
Merge remote-tracking branch 'upstream/master' into feature/ml-traine…
benwtrent b1733ca
fixing tests
benwtrent 4dd7993
addressing PR comments and updating URL
benwtrent 9e3db18
Merge remote-tracking branch 'upstream/master' into feature/ml-traine…
benwtrent ca3985f
fixing reference count bug and unnecessary model evictions
benwtrent 4c8a6b3
fixing test
benwtrent 9627f14
Merge branch 'master' into feature/ml-trained-model-alias
elasticmachine ab3b131
addressing PR comments
benwtrent 9b8af9e
Merge branch 'feature/ml-trained-model-alias' of github.com:benwtrent…
benwtrent 6cc0e3f
fixing exception message
benwtrent 7b66376
Merge remote-tracking branch 'upstream/master' into feature/ml-traine…
benwtrent 67a6733
fixing test
benwtrent 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 hidden or 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 hidden or 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
89 changes: 89 additions & 0 deletions
89
docs/reference/ml/df-analytics/apis/put-trained-models-aliases.asciidoc
This file contains hidden or 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,89 @@ | ||
| [role="xpack"] | ||
| [testenv="platinum"] | ||
| [[put-trained-models-aliases]] | ||
| = Put Trained Models Aliases API | ||
| [subs="attributes"] | ||
| ++++ | ||
| <titleabbrev>Put Trained Models Aliases</titleabbrev> | ||
| ++++ | ||
|
|
||
| Creates a trained models alias. These model aliases can be used instead of the trained model ID | ||
| when referencing the model in the stack. Model aliases must be unique, and a trained model can have | ||
| more than one model alias referring to it. But a model alias can only refer to a single trained model. | ||
|
|
||
| beta::[] | ||
|
|
||
| [[ml-put-trained-models-aliases-request]] | ||
| == {api-request-title} | ||
|
|
||
| `PUT _ml/trained_models/<model_id>/model_aliases/<model_alias>` | ||
|
|
||
|
|
||
| [[ml-put-trained-models-aliases-prereq]] | ||
| == {api-prereq-title} | ||
|
|
||
| If the {es} {security-features} are enabled, you must have the following | ||
| built-in roles and privileges: | ||
|
|
||
| * `machine_learning_admin` | ||
|
|
||
| For more information, see <<built-in-roles>>, <<security-privileges>>, and | ||
| {ml-docs-setup-privileges}. | ||
|
|
||
| [[ml-put-trained-models-aliases-desc]] | ||
| == {api-description-title} | ||
|
|
||
| This API creates a new model alias to refer to trained models, or updates an existing | ||
| trained model's alias. | ||
|
|
||
| When updating an existing model alias to a new model ID, this API will return a warning if the models | ||
| are of different inference types. Example, if attempting to put the model alias | ||
| `flights-delay-prediction` from a regression model to a classification model, the API will error. | ||
|
|
||
| The API will return a warning if there are very few input fields in common between the old | ||
| and new models for the model alias. | ||
|
|
||
| [[ml-put-trained-models-aliases-path-params]] | ||
| == {api-path-parms-title} | ||
|
|
||
| `model_id`:: | ||
| (Required, string) | ||
| The trained model ID to which the model alias should refer. | ||
|
|
||
| `model_alias`:: | ||
| (Required, string) | ||
| The model alias to create or update. The model_alias cannot end in numbers. | ||
|
|
||
| [[ml-put-trained-models-aliases-query-params]] | ||
| == {api-query-parms-title} | ||
|
|
||
| `reassign`:: | ||
| (Optional, boolean) | ||
| Should the `model_alias` get reassigned to the provided `model_id` if it is already | ||
| assigned to a model. Defaults to false. The API will return an error if the `model_alias` | ||
| is already assigned to a model but this parameter is `false`. | ||
|
|
||
| [[ml-put-trained-models-aliases-example]] | ||
| == {api-examples-title} | ||
|
|
||
| [[ml-put-trained-models-aliases-example-new-alias]] | ||
| === Creating a new model alias | ||
|
|
||
| The following example shows how to create a new model alias for a trained model ID. | ||
|
|
||
| [source,console] | ||
| -------------------------------------------------- | ||
| PUT _ml/trained_models/flight-delay-prediction-1574775339910/model_aliases/flight_delay_model | ||
| -------------------------------------------------- | ||
| // TEST[skip:setup kibana sample data] | ||
|
|
||
| [[ml-put-trained-models-aliases-example-put-alias]] | ||
| === Updating an existing model alias | ||
|
|
||
| The following example shows how to reassign an existing model alias for a trained model ID. | ||
|
|
||
| [source,console] | ||
| -------------------------------------------------- | ||
| PUT _ml/trained_models/flight-delay-prediction-1580004349800/model_aliases/flight_delay_model?reassign=true | ||
| -------------------------------------------------- | ||
| // TEST[skip:setup kibana sample data] | ||
This file contains hidden or 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 hidden or 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 hidden or 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
119 changes: 119 additions & 0 deletions
119
...core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasAction.java
This file contains hidden or 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,119 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.ml.action; | ||
|
|
||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.action.ActionType; | ||
| import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
| import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; | ||
| import org.elasticsearch.xpack.core.ml.job.messages.Messages; | ||
| import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Locale; | ||
| import java.util.Objects; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static org.elasticsearch.action.ValidateActions.addValidationError; | ||
| import static org.elasticsearch.xpack.core.ml.job.messages.Messages.INVALID_MODEL_ALIAS; | ||
|
|
||
| public class PutTrainedModelAliasAction extends ActionType<AcknowledgedResponse> { | ||
|
|
||
| // NOTE this is similar to our valid ID check. The difference here is that model_aliases cannot end in numbers | ||
| // This is to protect our automatic model naming conventions from hitting weird model_alias conflicts | ||
| private static final Pattern VALID_MODEL_ALIAS_CHAR_PATTERN = Pattern.compile("[a-z0-9](?:[a-z0-9_\\-\\.]*[a-z])?"); | ||
|
|
||
| public static final PutTrainedModelAliasAction INSTANCE = new PutTrainedModelAliasAction(); | ||
| public static final String NAME = "cluster:admin/xpack/ml/inference/model_aliases/put"; | ||
|
|
||
| private PutTrainedModelAliasAction() { | ||
| super(NAME, AcknowledgedResponse::readFrom); | ||
| } | ||
|
|
||
| public static class Request extends AcknowledgedRequest<Request> { | ||
|
|
||
| public static final String MODEL_ALIAS = "model_alias"; | ||
| public static final String REASSIGN = "reassign"; | ||
|
|
||
| private final String modelAlias; | ||
| private final String modelId; | ||
| private final boolean reassign; | ||
|
|
||
| public Request(String modelAlias, String modelId, boolean reassign) { | ||
| this.modelAlias = ExceptionsHelper.requireNonNull(modelAlias, MODEL_ALIAS); | ||
| this.modelId = ExceptionsHelper.requireNonNull(modelId, TrainedModelConfig.MODEL_ID); | ||
| this.reassign = reassign; | ||
| } | ||
|
|
||
| public Request(StreamInput in) throws IOException { | ||
| super(in); | ||
| this.modelAlias = in.readString(); | ||
| this.modelId = in.readString(); | ||
| this.reassign = in.readBoolean(); | ||
| } | ||
|
|
||
| public String getModelAlias() { | ||
| return modelAlias; | ||
| } | ||
|
|
||
| public String getModelId() { | ||
| return modelId; | ||
| } | ||
|
|
||
| public boolean isReassign() { | ||
| return reassign; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeString(modelAlias); | ||
| out.writeString(modelId); | ||
| out.writeBoolean(reassign); | ||
| } | ||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| ActionRequestValidationException validationException = null; | ||
| if (modelAlias.equals(modelId)) { | ||
| validationException = addValidationError( | ||
| String.format( | ||
| Locale.ROOT, | ||
| "model_alias [%s] cannot equal model_id [%s]", | ||
| modelAlias, | ||
| modelId | ||
| ), | ||
| validationException | ||
| ); | ||
| } | ||
| if (VALID_MODEL_ALIAS_CHAR_PATTERN.matcher(modelAlias).matches() == false) { | ||
| validationException = addValidationError(Messages.getMessage(INVALID_MODEL_ALIAS, modelAlias), validationException); | ||
| } | ||
| return validationException; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Request request = (Request) o; | ||
| return Objects.equals(modelAlias, request.modelAlias) | ||
| && Objects.equals(modelId, request.modelId) | ||
| && Objects.equals(reassign, request.reassign); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(modelAlias, modelId, reassign); | ||
| } | ||
|
|
||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.