Search in sources :

Example 1 with MLTrainingTaskRequest

use of org.opensearch.ml.common.transport.training.MLTrainingTaskRequest in project ml-commons by opensearch-project.

the class TrainingITTests method testTrainingWithEmptyDataset.

// Train a model with empty dataset.
public void testTrainingWithEmptyDataset() {
    SearchSourceBuilder searchSourceBuilder = generateSearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchQuery("noSuchName", ""));
    MLInputDataset inputDataset = new SearchQueryInputDataset(Collections.singletonList(TESTING_INDEX_NAME), searchSourceBuilder);
    MLInput mlInput = MLInput.builder().algorithm(FunctionName.KMEANS).inputDataset(inputDataset).build();
    MLTrainingTaskRequest trainingRequest = new MLTrainingTaskRequest(mlInput, false);
    expectThrows(IllegalArgumentException.class, () -> client().execute(MLTrainingTaskAction.INSTANCE, trainingRequest).actionGet());
}
Also used : SearchQueryInputDataset(org.opensearch.ml.common.dataset.SearchQueryInputDataset) MLInput(org.opensearch.ml.common.parameter.MLInput) MLTrainingTaskRequest(org.opensearch.ml.common.transport.training.MLTrainingTaskRequest) MLInputDataset(org.opensearch.ml.common.dataset.MLInputDataset) IntegTestUtils.generateSearchSourceBuilder(org.opensearch.ml.utils.IntegTestUtils.generateSearchSourceBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 2 with MLTrainingTaskRequest

use of org.opensearch.ml.common.transport.training.MLTrainingTaskRequest in project ml-commons by opensearch-project.

the class MachineLearningNodeClient method trainAndPredict.

@Override
public void trainAndPredict(MLInput mlInput, ActionListener<MLOutput> listener) {
    validateMLInput(mlInput, true);
    MLTrainingTaskRequest request = MLTrainingTaskRequest.builder().mlInput(mlInput).build();
    client.execute(MLTrainAndPredictionTaskAction.INSTANCE, request, getMlPredictionTaskResponseActionListener(listener));
}
Also used : MLTrainingTaskRequest(org.opensearch.ml.common.transport.training.MLTrainingTaskRequest)

Example 3 with MLTrainingTaskRequest

use of org.opensearch.ml.common.transport.training.MLTrainingTaskRequest in project ml-commons by opensearch-project.

the class TransportTrainAndPredictionTaskAction method doExecute.

@Override
protected void doExecute(Task task, ActionRequest request, ActionListener<MLTaskResponse> listener) {
    MLTrainingTaskRequest trainingRequest = MLTrainingTaskRequest.fromActionRequest(request);
    mlTrainAndPredictTaskRunner.run(trainingRequest, transportService, listener);
}
Also used : MLTrainingTaskRequest(org.opensearch.ml.common.transport.training.MLTrainingTaskRequest)

Example 4 with MLTrainingTaskRequest

use of org.opensearch.ml.common.transport.training.MLTrainingTaskRequest in project ml-commons by opensearch-project.

the class MachineLearningNodeClient method train.

@Override
public void train(MLInput mlInput, boolean asyncTask, ActionListener<MLOutput> listener) {
    validateMLInput(mlInput, true);
    MLTrainingTaskRequest trainingTaskRequest = MLTrainingTaskRequest.builder().mlInput(mlInput).async(asyncTask).build();
    client.execute(MLTrainingTaskAction.INSTANCE, trainingTaskRequest, getMlPredictionTaskResponseActionListener(listener));
}
Also used : MLTrainingTaskRequest(org.opensearch.ml.common.transport.training.MLTrainingTaskRequest)

Example 5 with MLTrainingTaskRequest

use of org.opensearch.ml.common.transport.training.MLTrainingTaskRequest in project ml-commons by opensearch-project.

the class MLTrainingTaskRunner method createMLTaskAndTrain.

public void createMLTaskAndTrain(MLTrainingTaskRequest request, ActionListener<MLTaskResponse> listener) {
    MLInputDataType inputDataType = request.getMlInput().getInputDataset().getInputDataType();
    Instant now = Instant.now();
    MLTask mlTask = MLTask.builder().taskType(MLTaskType.TRAINING).inputType(inputDataType).functionName(request.getMlInput().getFunctionName()).state(MLTaskState.CREATED).workerNode(clusterService.localNode().getId()).createTime(now).lastUpdateTime(now).async(request.isAsync()).build();
    if (request.isAsync()) {
        mlTaskManager.createMLTask(mlTask, ActionListener.wrap(r -> {
            String taskId = r.getId();
            mlTask.setTaskId(taskId);
            listener.onResponse(new MLTaskResponse(new MLTrainingOutput(null, taskId, mlTask.getState().name())));
            ActionListener<MLTaskResponse> internalListener = ActionListener.wrap(res -> {
                String modelId = ((MLTrainingOutput) res.getOutput()).getModelId();
                log.info("ML model trained successfully, task id: {}, model id: {}", taskId, modelId);
                mlTask.setModelId(modelId);
                handleAsyncMLTaskComplete(mlTask);
            }, ex -> {
                log.error("Failed to train ML model for task " + taskId);
                handleAsyncMLTaskFailure(mlTask, ex);
            });
            startTrainingTask(mlTask, request.getMlInput(), internalListener);
        }, e -> {
            log.error("Failed to create ML task", e);
            listener.onFailure(e);
        }));
    } else {
        mlTask.setTaskId(UUID.randomUUID().toString());
        startTrainingTask(mlTask, request.getMlInput(), listener);
    }
}
Also used : IndexResponse(org.opensearch.action.index.IndexResponse) ToXContent(org.opensearch.common.xcontent.ToXContent) ThreadPool(org.opensearch.threadpool.ThreadPool) StatNames.modelCountStat(org.opensearch.ml.stats.StatNames.modelCountStat) MLTaskState(org.opensearch.ml.common.parameter.MLTaskState) MLInput(org.opensearch.ml.common.parameter.MLInput) MLInputDatasetHandler(org.opensearch.ml.indices.MLInputDatasetHandler) ThreadedActionListener(org.opensearch.action.support.ThreadedActionListener) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) MLTask(org.opensearch.ml.common.parameter.MLTask) ML_EXECUTING_TASK_COUNT(org.opensearch.ml.stats.StatNames.ML_EXECUTING_TASK_COUNT) WriteRequest(org.opensearch.action.support.WriteRequest) ActionListener(org.opensearch.action.ActionListener) MLModel(org.opensearch.ml.common.parameter.MLModel) MLIndicesHandler(org.opensearch.ml.indices.MLIndicesHandler) ML_TOTAL_MODEL_COUNT(org.opensearch.ml.stats.StatNames.ML_TOTAL_MODEL_COUNT) MLTaskResponse(org.opensearch.ml.common.transport.MLTaskResponse) MLInputDataType(org.opensearch.ml.common.dataset.MLInputDataType) Client(org.opensearch.client.Client) MLStats(org.opensearch.ml.stats.MLStats) ActionName(org.opensearch.ml.stats.ActionName) DataFrame(org.opensearch.ml.common.dataframe.DataFrame) UUID(java.util.UUID) Instant(java.time.Instant) TransportService(org.opensearch.transport.TransportService) StatNames.requestCountStat(org.opensearch.ml.stats.StatNames.requestCountStat) MLTrainingTaskAction(org.opensearch.ml.common.transport.training.MLTrainingTaskAction) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MLEngine(org.opensearch.ml.engine.MLEngine) StatNames.failureCountStat(org.opensearch.ml.stats.StatNames.failureCountStat) MLTrainingOutput(org.opensearch.ml.common.parameter.MLTrainingOutput) ML_MODEL_INDEX(org.opensearch.ml.indices.MLIndicesHandler.ML_MODEL_INDEX) Model(org.opensearch.ml.common.parameter.Model) ML_TOTAL_FAILURE_COUNT(org.opensearch.ml.stats.StatNames.ML_TOTAL_FAILURE_COUNT) MLTaskType(org.opensearch.ml.common.parameter.MLTaskType) MLCircuitBreakerService(org.opensearch.ml.common.breaker.MLCircuitBreakerService) Log4j2(lombok.extern.log4j.Log4j2) ActionListenerResponseHandler(org.opensearch.action.ActionListenerResponseHandler) ClusterService(org.opensearch.cluster.service.ClusterService) TASK_THREAD_POOL(org.opensearch.ml.plugin.MachineLearningPlugin.TASK_THREAD_POOL) ML_TOTAL_REQUEST_COUNT(org.opensearch.ml.stats.StatNames.ML_TOTAL_REQUEST_COUNT) XContentType(org.opensearch.common.xcontent.XContentType) IndexRequest(org.opensearch.action.index.IndexRequest) DataFrameInputDataset(org.opensearch.ml.common.dataset.DataFrameInputDataset) MLTrainingTaskRequest(org.opensearch.ml.common.transport.training.MLTrainingTaskRequest) MLTaskResponse(org.opensearch.ml.common.transport.MLTaskResponse) MLTrainingOutput(org.opensearch.ml.common.parameter.MLTrainingOutput) ThreadedActionListener(org.opensearch.action.support.ThreadedActionListener) ActionListener(org.opensearch.action.ActionListener) Instant(java.time.Instant) MLInputDataType(org.opensearch.ml.common.dataset.MLInputDataType) MLTask(org.opensearch.ml.common.parameter.MLTask)

Aggregations

MLTrainingTaskRequest (org.opensearch.ml.common.transport.training.MLTrainingTaskRequest)10 MLInput (org.opensearch.ml.common.parameter.MLInput)6 MLTaskResponse (org.opensearch.ml.common.transport.MLTaskResponse)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 XContentParser (org.opensearch.common.xcontent.XContentParser)2 MLTrainingOutput (org.opensearch.ml.common.parameter.MLTrainingOutput)2 Instant (java.time.Instant)1 UUID (java.util.UUID)1 Log4j2 (lombok.extern.log4j.Log4j2)1 ActionListener (org.opensearch.action.ActionListener)1 ActionListenerResponseHandler (org.opensearch.action.ActionListenerResponseHandler)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 ThreadedActionListener (org.opensearch.action.support.ThreadedActionListener)1 WriteRequest (org.opensearch.action.support.WriteRequest)1 Client (org.opensearch.client.Client)1 ClusterService (org.opensearch.cluster.service.ClusterService)1 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)1 ToXContent (org.opensearch.common.xcontent.ToXContent)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1