-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[ML] handles compressed model stream from native process #58009
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 13 commits into
elastic:master
from
benwtrent:feature/ml-analytics-handle-compressed-model-stream
Jul 1, 2020
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f3ccd19
[ML] handles compressed model stream from native process
benwtrent 7082531
Merge remote-tracking branch 'upstream/master' into feature/ml-analyt…
benwtrent 3a5ce93
fixing after merge
benwtrent 354fc25
adjusting doc storage format
benwtrent ff192a4
fixing model storage
benwtrent ad877b0
Merge remote-tracking branch 'upstream/master' into feature/ml-analyt…
benwtrent 40571e2
unmuting tests
benwtrent 8a340bf
unmuting tests
benwtrent b20fb05
addressing pr comments
benwtrent 3ad327e
Merge remote-tracking branch 'upstream/master' into feature/ml-analyt…
benwtrent 398d9f9
moving boolean flag
benwtrent caed315
fixing test
benwtrent dc25e38
Merge remote-tracking branch 'upstream/master' into feature/ml-analyt…
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
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
130 changes: 130 additions & 0 deletions
130
...usterTest/java/org/elasticsearch/xpack/ml/integration/ChunkedTrainedModelPersisterIT.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,130 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| package org.elasticsearch.xpack.ml.integration; | ||
|
|
||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.action.support.PlainActionFuture; | ||
| import org.elasticsearch.common.collect.Tuple; | ||
| import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
| import org.elasticsearch.license.License; | ||
| import org.elasticsearch.xpack.core.action.util.PageParams; | ||
| import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; | ||
| import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsDest; | ||
| import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsSource; | ||
| import org.elasticsearch.xpack.core.ml.dataframe.analyses.Regression; | ||
| import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; | ||
| import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; | ||
| import org.elasticsearch.xpack.core.ml.inference.TrainedModelDefinition; | ||
| import org.elasticsearch.xpack.core.ml.inference.TrainedModelDefinitionTests; | ||
| import org.elasticsearch.xpack.core.ml.inference.TrainedModelInputTests; | ||
| import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; | ||
| import org.elasticsearch.xpack.ml.MlSingleNodeTestCase; | ||
| import org.elasticsearch.xpack.ml.dataframe.process.ChunkedTrainedModelPersister; | ||
| import org.elasticsearch.xpack.ml.dataframe.process.results.TrainedModelDefinitionChunk; | ||
| import org.elasticsearch.xpack.ml.extractor.DocValueField; | ||
| import org.elasticsearch.xpack.ml.extractor.ExtractedField; | ||
| import org.elasticsearch.xpack.ml.extractor.ExtractedFields; | ||
| import org.elasticsearch.xpack.ml.inference.modelsize.MlModelSizeNamedXContentProvider; | ||
| import org.elasticsearch.xpack.ml.inference.modelsize.ModelSizeInfo; | ||
| import org.elasticsearch.xpack.ml.inference.modelsize.ModelSizeInfoTests; | ||
| import org.elasticsearch.xpack.ml.inference.persistence.TrainedModelProvider; | ||
| import org.elasticsearch.xpack.ml.notifications.DataFrameAnalyticsAuditor; | ||
| import org.junit.Before; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| public class ChunkedTrainedModelPersisterIT extends MlSingleNodeTestCase { | ||
|
|
||
| private TrainedModelProvider trainedModelProvider; | ||
|
|
||
| @Before | ||
| public void createComponents() throws Exception { | ||
| trainedModelProvider = new TrainedModelProvider(client(), xContentRegistry()); | ||
| waitForMlTemplates(); | ||
| } | ||
|
|
||
| public void testStoreModelViaChunkedPersister() throws IOException { | ||
| String modelId = "stored-chunked-model"; | ||
| DataFrameAnalyticsConfig analyticsConfig = new DataFrameAnalyticsConfig.Builder() | ||
| .setId(modelId) | ||
| .setSource(new DataFrameAnalyticsSource(new String[] {"my_source"}, null, null)) | ||
| .setDest(new DataFrameAnalyticsDest("my_dest", null)) | ||
| .setAnalysis(new Regression("foo")) | ||
| .build(); | ||
| List<ExtractedField> extractedFieldList = Collections.singletonList(new DocValueField("foo", Collections.emptySet())); | ||
| TrainedModelConfig.Builder configBuilder = buildTrainedModelConfigBuilder(modelId); | ||
| String compressedDefinition = configBuilder.build().getCompressedDefinition(); | ||
| int totalSize = compressedDefinition.length(); | ||
| List<String> chunks = chunkStringWithSize(compressedDefinition, totalSize/3); | ||
|
|
||
| ChunkedTrainedModelPersister persister = new ChunkedTrainedModelPersister(trainedModelProvider, | ||
| analyticsConfig, | ||
| new DataFrameAnalyticsAuditor(client(), "test-node"), | ||
| (ex) -> { throw new ElasticsearchException(ex); }, | ||
| new ExtractedFields(extractedFieldList, Collections.emptyMap()) | ||
| ); | ||
|
|
||
| //Accuracy for size is not tested here | ||
| ModelSizeInfo modelSizeInfo = ModelSizeInfoTests.createRandom(); | ||
| persister.createAndIndexInferenceModelMetadata(modelSizeInfo); | ||
| for (int i = 0; i < chunks.size(); i++) { | ||
| persister.createAndIndexInferenceModelDoc(new TrainedModelDefinitionChunk(chunks.get(i), i, i == (chunks.size() - 1))); | ||
| } | ||
|
|
||
| PlainActionFuture<Tuple<Long, Set<String>>> getIdsFuture = new PlainActionFuture<>(); | ||
| trainedModelProvider.expandIds(modelId + "*", false, PageParams.defaultParams(), Collections.emptySet(), getIdsFuture); | ||
| Tuple<Long, Set<String>> ids = getIdsFuture.actionGet(); | ||
| assertThat(ids.v1(), equalTo(1L)); | ||
|
|
||
| PlainActionFuture<TrainedModelConfig> getTrainedModelFuture = new PlainActionFuture<>(); | ||
| trainedModelProvider.getTrainedModel(ids.v2().iterator().next(), true, getTrainedModelFuture); | ||
|
|
||
| TrainedModelConfig storedConfig = getTrainedModelFuture.actionGet(); | ||
| assertThat(storedConfig.getCompressedDefinition(), equalTo(compressedDefinition)); | ||
| assertThat(storedConfig.getEstimatedOperations(), equalTo((long)modelSizeInfo.numOperations())); | ||
| assertThat(storedConfig.getEstimatedHeapMemory(), equalTo(modelSizeInfo.ramBytesUsed())); | ||
| } | ||
|
|
||
| private static TrainedModelConfig.Builder buildTrainedModelConfigBuilder(String modelId) { | ||
| TrainedModelDefinition.Builder definitionBuilder = TrainedModelDefinitionTests.createRandomBuilder(); | ||
| long bytesUsed = definitionBuilder.build().ramBytesUsed(); | ||
| long operations = definitionBuilder.build().getTrainedModel().estimatedNumOperations(); | ||
| return TrainedModelConfig.builder() | ||
| .setCreatedBy("ml_test") | ||
| .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder(TargetType.REGRESSION)) | ||
| .setDescription("trained model config for test") | ||
| .setModelId(modelId) | ||
| .setVersion(Version.CURRENT) | ||
| .setLicenseLevel(License.OperationMode.PLATINUM.description()) | ||
| .setEstimatedHeapMemory(bytesUsed) | ||
| .setEstimatedOperations(operations) | ||
| .setInput(TrainedModelInputTests.createRandomInput()); | ||
| } | ||
|
|
||
| public static List<String> chunkStringWithSize(String str, int chunkSize) { | ||
| List<String> subStrings = new ArrayList<>((str.length() + chunkSize - 1) / chunkSize); | ||
| for (int i = 0; i < str.length(); i += chunkSize) { | ||
| subStrings.add(str.substring(i, Math.min(i + chunkSize, str.length()))); | ||
| } | ||
| return subStrings; | ||
| } | ||
|
|
||
| @Override | ||
| public NamedXContentRegistry xContentRegistry() { | ||
| List<NamedXContentRegistry.Entry> namedXContent = new ArrayList<>(); | ||
| namedXContent.addAll(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); | ||
| namedXContent.addAll(new MlModelSizeNamedXContentProvider().getNamedXContentParsers()); | ||
| return new NamedXContentRegistry(namedXContent); | ||
| } | ||
|
|
||
| } |
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
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.
IntStream.range is end exclusive so we will never get to
i == chunks.size() - 1Should it be set to
falseas Eos is set on the last doc in the list in line 223