Using Azure Machine Learning from GitHub Actions

Damian

Azure Machine Learning is an Enterprise-grade Machine Learning service that can help you build and deploy your predictive models faster. It also has a number of features to help you mature your machine learning process with MLOps.

One of the important steps a data science team should take when starting down an MLOps path is to put all their code in source control. This allows the team to collaborate effectively and enables continuous integration in much the same way as we see for traditional software development. When any of the training code changes, we can automatically merge it with the rest of the team’s changes, check the quality, and even automate training a new model.

Why not use GitHub Actions for everything?

Automation tools like GitHub Actions are great for continuous integration and continuous delivery/deployment (CI/CD). They’re designed to take your code and build it. They’re also great for running tests, checking quality, and communicating with third party services.

But the tasks we use for traditional software development tend to have limited time requirements. Even long-running builds tend to take minutes rather than hours. For example, the main build for Azure DevOps takes about an hour at most!

By contrast, a training run for a reasonably complicated predictive model might take hours, days, or even weeks. It might require tens or hundreds of virtual machines with multiple GPUs and process terabytes or petabytes of data. GitHub Actions, Azure Pipelines, and other similar products are simply not built for this.

Azure Machine Learning does support long-running, highly data- and compute-intensive pipelines. It’s designed specifically for that use case, with easy access to scalable cloud compute and data sources.

GitHub Actions

So why not use Azure Machine Learning for everything?

A reasonable question! If Azure Machine Learning is designed for these workloads, why not use it in isolation?

The short answer is services like GitHub Actions give greater control over the orchestration of a pipeline in its entirety. While the training run is ideally handled in a service like Azure Machine Learning, GitHub is great at: * Managing your team’s code, * Triggering workflows on code changes, * Controlling the rollout of your model to test and production environments, * Pull request processes, * Branch protection, * Project management tools, * Advanced security features

All of these features can help add another layer of DevOps maturity.

The key here is to use the right tool for the right job. Pipelines that define the training run can be incredibly powerful, and that’s where you should focus your Azure Machine Learning efforts.

Pipelines in Azure Machine Learning

How do we use them together?

There are three ways to work with Azure Machine Learning from GitHub Actions: 1. The Python SDK 2. The Azure ML CLI 3. GitHub Actions for Azure Machine Learning

The best way to see some of these in action is to check out the Azure ML examples on GitHub.

Let’s look at how to run an Azure Machine Learning pipeline from GitHub Actions using each of these methods.

Using the Python SDK

The Python SDK is the most powerful way of integrating, but requires a little more work than the other methods.

In your Actions workflow, you’ll need to set up the correct version of Python, then install the azureml-core python package. Finally, run a python script that makes use of the SDK.

Here’s an example of the job and steps you might have in your workflow to run a training job using the Python SDK:

jobs:
  build:
    runs-on: ubuntu-latest 

    steps:
    - name: check out repo
      uses: actions/checkout@v2

    - name: setup python
      uses: actions/setup-python@v2
      with: 
        python-version: "3.8"

    - name: pip install
      run: pip install azureml-core

    - name: run workflow 
      run: python job.py

There are great examples of how to work with the Python SDK in the azureml-examples repository in GitHub.

Here’s an example of a python script that triggers a training run on the MNIST data set using tensorflow (you can find the file here):

# description: train tensorflow NN model on mnist data

# imports
from pathlib import Path
from azureml.core import Workspace
from azureml.core import ScriptRunConfig, Experiment, Environment

# get workspace
ws = Workspace.from_config()

# get root of git repo
prefix = Path(__file__).parent

# training script
script_dir = str(prefix.joinpath("src"))
script_name = "train.py"

# environment file
environment_file = str(prefix.joinpath("environment.yml"))

# azure ml settings
environment_name = "tf-gpu-example"
experiment_name = "tf-mnist-example"
compute_name = "gpu-cluster"

# create environment
env = Environment.from_conda_specification(environment_name, environment_file)

# create job config
src = ScriptRunConfig(
    source_directory=script_dir,
    script=script_name,
    environment=env,
    compute_target=compute_name,
)

# submit job
run = Experiment(ws, experiment_name).submit(src)
run.wait_for_completion(show_output=True)

Note that in this example, we wait for the experiment to complete. For long-running training jobs, we could just leave out the final line and our GitHub Actions workflow would finish, leaving the experiment to complete in its own time.

The Azure ML CLI

The Azure CLI is also extremely powerful. Because it’s an extension to the cross-platform Azure CLI, it can be used almost anywhere.

The Azure ML CLI documentation links to a great example repository showing how to use the CLI, but it doesn’t specifically talk about using it in the context of CI/CD pipeline. Of course the great thing about GitHub Actions is that if you can script it, you can run it – so we can easily run the CLI from a workflow!

The Azure CLI is already installed on the hosted agents you’ll use in your workflow. That means you just need to install the Azure Machine Learning extension. From there you’ll need a step to log into Azure and you’re ready to go!

Here’s an example of the job and steps you might have in your workflow:

jobs:
  build:
    runs-on: ubuntu-latest 
    steps:
    - name: check out repo
      uses: actions/checkout@v2
    - name: azure login
      uses: azure/login@v1
      with:
        creds: ${{secrets.AZURE_CREDENTIALS}}
    - name: attach workspace
      run: az ml folder attach -w myworkspace -g myresourcegroup
    - name: run pipeline
      run: az ml run submit-pipeline -n myproject-pipeline -y aml_config/pipeline.yml

That final line is a call to Azure Machine Learning to submit a pipeline for execution called myproject-pipeline defined in aml_config/pipeline.yml in the repository.

You can find more details of how this all works in the AML-R-acceleration-template repository in GitHub.

The Azure GitHub Actions for Azure ML

Finally, Microsoft has published a number of GitHub Actions you can use directly in your workflow. You can find them all by [searching the Marketplace for “Azure Machine Learning”(https://github.com/marketplace?type=actions&query=azure+machine+learning).

This is by far the easiest way to use Azure Machine Learning from GitHub Actions, however each Action has a single specific purpose. By contrast, The CLI and Python SDK allow you to perform any operation in Azure Machine Learning.

Here’s an example of a manually-run workflow that uses the Azure ML Actions to deploy a trained model:

name: Deploy my model
on:
  workflow_dispatch:
    inputs:
      name:
        description: 'Model to deploy'
        required: true
        default: 'mymodel'
      version:
        description: 'Version'
        required: true
        default: '1'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: check out repo
      uses: actions/checkout@v2

    - name: connect to workspace
      uses: Azure/aml-workspace@v1
      with:
        azure_credentials: ${{ secrets.AZURE_CREDENTIALS }}

    - name: deploy model
      uses: Azure/aml-deploy@v1
      with:
        azure_credentials: ${{ secrets.AZURE_CREDENTIALS }}
        model_name:  ${{ github.event.inputs.name }}
        model_version: ${{ github.event.inputs.version }}

If you’re interested, you can have a look at the code behind these Actions! For example, here’s the repository with the aml-deploy action.

Get Started

Using GitHub alongside Azure Machine Learning gives you the best of both worlds and lets you use the right tool for the job without compromising.

To get started with Azure Machine Learning, I can highly recommend the Build AI solutions with Azure Machine Learning learning path on Microsoft Learn. It takes you through the product step by step, and best of all, it’s free!

There’s also a great learning path that can teach you how to use GitHub Actions. While it doesn’t have anything to do with machine learning, it will equip you with the skills you need to get started.

0 comments

Comments are closed. Login to edit/delete your existing comments