Skip to content

Latest commit

 

History

History
299 lines (191 loc) · 9.29 KB

how-to-use-batch-pipeline-from-job.md

File metadata and controls

299 lines (191 loc) · 9.29 KB
title titleSuffix description services ms.service ms.subservice author ms.author reviewer ms.reviewer ms.topic ms.custom ms.date
How to deploy existing pipeline jobs to a batch endpoint
Azure Machine Learning
Learn how to create pipeline component deployment for Batch Endpoints
machine-learning
machine-learning
inferencing
ccrestana
cacrest
msakande
mopeakande
how-to
how-to
devplatv2
ignite-2023
update-code
11/15/2023

Deploy existing pipeline jobs to batch endpoints

[!INCLUDE ml v2]

Batch endpoints allow you to deploy pipeline components, providing a convenient way to operationalize pipelines in Azure Machine Learning. Batch endpoints accept pipeline components for deployment. However, if you already have a pipeline job that runs successfully, Azure Machine Learning can accept that job as input to your batch endpoint and create the pipeline component automatically for you. In this article, you'll learn how to use your existing pipeline job as input for batch deployment.

You'll learn to:

[!div class="checklist"]

  • Run and create the pipeline job that you want to deploy
  • Create a batch deployment from the existing job
  • Test the deployment

About this example

In this example, we're going to deploy a pipeline consisting of a simple command job that prints "hello world!". Instead of registering the pipeline component before deployment, we indicate an existing pipeline job to use for deployment. Azure Machine Learning will then create the pipeline component automatically and deploy it as a batch endpoint pipeline component deployment.

[!INCLUDE machine-learning-batch-clone]

The files for this example are in:

cd endpoints/batch/deploy-pipelines/hello-batch

Prerequisites

[!INCLUDE machine-learning-batch-prereqs]

Run the pipeline job you want to deploy

In this section, we begin by running a pipeline job:

The following pipeline-job.yml file contains the configuration for the pipeline job:

pipeline-job.yml

:::code language="yaml" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/pipeline-job.yml" :::

Load the pipeline component and instantiate it:

hello_batch = load_component(source="hello-component/hello.yml")
pipeline_job = hello_batch()

Now, configure some run settings to run the test. This article assumes you have a compute cluster named batch-cluster. You can replace the cluster with the name of yours.

pipeline_job.settings.default_compute = "batch-cluster"
pipeline_job.settings.default_datastore = "workspaceblobstore"

Create the pipeline job:

:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deploy-and-run.sh" ID="run_pipeline_job_deployment" :::

pipeline_job_run = ml_client.jobs.create_or_update(
    pipeline_job, experiment_name="hello-batch-pipeline"
)
pipeline_job_run

Create a batch endpoint

Before we deploy the pipeline job, we need to deploy a batch endpoint to host the deployment.

  1. Provide a name for the endpoint. A batch endpoint's name needs to be unique in each region since the name is used to construct the invocation URI. To ensure uniqueness, append any trailing characters to the name specified in the following code.

    :::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deploy-and-run.sh" ID="name_endpoint" :::

    endpoint_name="hello-batch"
  2. Configure the endpoint:

    The endpoint.yml file contains the endpoint's configuration.

    endpoint.yml

    :::code language="yaml" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/endpoint.yml" :::

    endpoint = BatchEndpoint(
        name=endpoint_name,
        description="A hello world endpoint for component deployments",
    )
  3. Create the endpoint:

    :::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deploy-and-run.sh" ID="create_endpoint" :::

    ml_client.batch_endpoints.begin_create_or_update(endpoint).result()
  4. Query the endpoint URI:

    :::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deploy-and-run.sh" ID="query_endpoint" :::

    endpoint = ml_client.batch_endpoints.get(name=endpoint_name)
    print(endpoint)

Deploy the pipeline job

To deploy the pipeline component, we have to create a batch deployment from the existing job.

  1. We need to tell Azure Machine Learning the name of the job that we want to deploy. In our case, that job is indicated in the following variable:

    echo $JOB_NAME
    
    print(job.name)
  2. Configure the deployment.

    The deployment-from-job.yml file contains the deployment's configuration. Notice how we use the key job_definition instead of component to indicate that this deployment is created from a pipeline job:

    deployment-from-job.yml

    :::code language="yaml" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deployment-from-job.yml" :::

    Notice now how we use the property job_definition instead of component:

    deployment = PipelineComponentBatchDeployment(
        name="hello-batch-from-job",
        description="A hello world deployment with a single step. This deployment is created from a pipeline job.",
        endpoint_name=endpoint.name,
        job_definition=pipeline_job_run,
        settings={
            "default_compute": "batch-cluster",
            "continue_on_step_failure": False
        }
    )

    [!TIP] This configuration assumes you have a compute cluster named batch-cluster. You can replace this value with the name of your cluster.

  3. Create the deployment:

    Run the following code to create a batch deployment under the batch endpoint and set it as the default deployment.

    :::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deploy-and-run.sh" ID="create_deployment_from_job" :::

    [!TIP] Notice the use of --set job_definition=azureml:$JOB_NAME. Since job names are unique, the command --set is used here to change the name of the job when you run it in your workspace.

    This command starts the deployment creation and returns a confirmation response while the deployment creation continues.

    ml_client.batch_deployments.begin_create_or_update(deployment).result()

    Once created, let's configure this new deployment as the default one:

    endpoint = ml_client.batch_endpoints.get(endpoint.name)
    endpoint.defaults.deployment_name = deployment.name
    ml_client.batch_endpoints.begin_create_or_update(endpoint).result()
  4. Your deployment is ready for use.

Test the deployment

Once the deployment is created, it's ready to receive jobs. You can invoke the default deployment as follows:

:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deploy-and-run.sh" ID="invoke_deployment_inline" :::

job = ml_client.batch_endpoints.invoke(
    endpoint_name=endpoint.name, 
)

You can monitor the progress of the show and stream the logs using:

:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deploy-and-run.sh" ID="stream_job_logs" :::

ml_client.jobs.get(name=job.name)

To wait for the job to finish, run the following code:

ml_client.jobs.stream(name=job.name)

Clean up resources

Once you're done, delete the associated resources from the workspace:

Run the following code to delete the batch endpoint and its underlying deployment. --yes is used to confirm the deletion.

:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-pipelines/hello-batch/deploy-and-run.sh" ID="delete_endpoint" :::

Delete the endpoint:

ml_client.batch_endpoints.begin_delete(endpoint.name).result()

Next steps