microsoft / dstoolkit-mlopsv2-with-azure-ml

MLOps with Azure Machine Learning - An Example Implementation of a Deploy-Code Approach
MIT License
2 stars 1 forks source link

Welcome to the MLOps Playground!

This repository serves as a playground for exploring and experimenting with MLOps using a deploy-code approach. Discover how to implement key components of MLOps in a modularized fashion, ensuring code quality through validation tests, and deploying models with parametrized CI/CD pipelines.

What's Inside? πŸ“¦

Deploy-Code MLOps Reference Architecture πŸ—οΈ

The implementation example provided in this repository follows a deploy-code MLOps reference architecture as shown below:

Deploy-Code MLOps Reference Architecture

The reference architecture outlines the interactions between three key components of MLOps: Data, Model, and Code. The deploy-code MLOps pattern emphasizes the importance of code quality and reproducibility, promoting the use of code for managing the ML workflow across the Development, Staging, and Production environments. Let's take a closer look at the high-level steps to exercise this implementation pattern:

Implementation Tips πŸ“

Before diving into the code, here are some implementation tips to make maintaining the MLOps process a less daunting task:

Getting Started 🏁

Now, let's get started with some implementation details involved in three key development tasks: Experiment, Test, and Operationalize.

Experiment "Locally"

Follow the following steps to experiment with the modelling code "locally" on your dev machine:

  1. Clone the repository to your dev machine (consider using Azure ML compute instance as your "local" dev machine):
    git clone https://github.com/microsoft/dstoolkit-mlopsv2-with-azure-ml.git
    cd dstoolkit-mlopsv2-with-azure-ml
  2. Create a Conda environment:
    conda env create -n credit_default -f environment/conda.yml
    conda activate credit_default

    If you don't have Conda installed, you can install it from here.

  3. Explore the src folder, which contains Python scripts for data preprocessing, model training, and evaluation etc. The usage example is provided as comments in the script. For example, you can run the following command to prepare the training and test data:
    cd src
    python data_prep.py --data_input_dir ../data/input --data_output_dir ../data/output --config_file ../config/modelling.json --no_logging

    Run the following command to train the model:

    python train.py --data_dir ../data/output --model_dir ../models --config_file ../config/modelling.json --no_logging

    Run the following command to evaluate the model:

    python evaluate.py --data_dir ../data/output --model_dir ../models --eval_dir ../eval --config_file ../config/modelling.json --no_logging

    Run the following command to register the model:

    python register.py --model_dir ../models --eval_dir ../eval --registry_dir ../registry --config_file ../config/modelling.json

Test before Pull Request

Before submitting a pull request or merging code changes, it is recommended to test your changes to ensure code quality and identify any potential issues. The tests folder provides unit test examples that you can run on your dev machine:

  1. Install the required Python packages to support testing:
    pip install -r environment/unit_test_requirements.txt
  2. Run the unit tests:
    pytest -v tests

    In addition to running unit tests, you can perform additional checks to ensure code quality and security:

Operationalize on the Cloud

Having successfully tested the code artifacts locally, it's time to take your ML workflow to the cloud and operationalize it for enhanced scalability and efficiency. The operations folder in this repository contains example YAML scripts that will guide you in creating the following pipelines on Azure through Azure DevOps:

Pull Request Validation Pipeline

The Pull Request Validation Pipeline is triggered when a pull request to a specified branch (e.g., the main branch) is submitted. It performs a series of checks, including code formatting and security checks, as well as running unit tests, integration tests (e.g., to test the end-to-end modeling workflow), and smoke test. The purpose of this pipeline is to ensure that the code changes meet the quality standards before merging them into the target branch. You can find an example implementation of a PR validation pipeline in azure_pipelines/pr_validation_pipeline.yml.

Information: Pull request trigger with Azure Repos Git is configured through Branch Policies. You can refer to Azure Repos Branch policies and settings for detailed configuration steps.

Build and Release Pipeline

The Build and Release Pipeline is responsible for building and deploying the ML/AI solution. It is often triggered when a release branch is created or updated. The pipeline consists of two stages:

  1. Build: This stage runs the end-to-end modeling workflow, including training and evaluating the ML model using the code in the "Release" branch. It trains the model on the production data and evaluate the model before proceeding to the deployment stage.

  2. Deploy: Upon successful completion of the modeling workflow in the Build stage, the Deploy stage deploys the ML model to the specified "Production" environment. Pre-deployment manual approval can be configured by defining an approval condition in Azure DevOps for the target deployment environment. For detailed configuration steps, you can refer to the how-to guide here.

An example definition of the Build and Release Pipeline can be found in azure_pipelines/cicd.yml.

Information: To create Azure Pipelines with the provided YAML pipeline scripts, follow the step-by-step guide available here.

Repository Structure πŸ“‚

Here's a brief overview of the folders included in this repository:

Feel free to explore each folder and leverage the provided structure to organize your project effectively.

Let's Get Started! πŸš€

Feel free to explore the provided examples, experiment with the MLOps reference architecture, and adapt it to your specific needs and requirements.