Continuous Machine Learning (CML)

repository·main·Indexed 11 days ago

https://github.com/iterative/cml

An open-source CLI tool for MLOps that automates machine learning workflows—including training, evaluation, and experiment tracking—within standard CI/CD pipelines such as GitHub, GitLab, and Bitbucket. CML enables automated report generation in pull requests, cloud compute resource allocation via `cml runner launch`, and integrates with DVC for data and metrics management. Version 0.20.6.

Tokens
17.4K
Snippets
55
Records
72
Agent score
87%

What's inside CML

  1. What is CML?

    main

    Continuous Machine Learning (CML) is an open-source CLI tool designed for implementing CI/CD with a focus on MLOps. It automates development workflows such as machine provisioning, model training, evaluation, comparing ML experiments across project history, and monitoring changing datasets.

    Key principles include:

    • GitFlow for data science: Using Git providers (GitHub, GitLab, Bitbucket) to manage experiments and tracking changes via DVC instead of pushing large data/models directly to Git.
    • Auto reports for ML experiments: Automatically generating reports containing metrics and plots within Git pull requests.
    • No additional services: Building an ML platform using existing Git providers and cloud storage without needing complex databases or external services.
  2. Use CML Docker images

    main

    CML provides pre-configured Docker images (ghcr.io/iterative/cml or iterativeai/cml) containing Python, CUDA, git, node, and other data science essentials.

    Image tags follow the pattern: {CML_VER}-dvc{DVC_VER}-base{BASE_VER}[-gpu].

    Available {BASE_VER} options:

    • 0: Ubuntu 18.04, Python 2.7 (CUDA 10.1, CuDNN 7)
    • 1: Ubuntu 20.04, Python 3.8 (CUDA 11.2, CuDNN 8)

    Example tags:

    • ghcr.io/iterative/cml:0-dvc2-base1-gpu (GPU enabled)
    • ghcr.io/iterative/cml:0-dvc2-base1 (Standard)
  3. Quickstart: Set up a CML workflow on GitHub

    main

    To get started with CML on GitHub, follow these steps:

    1. Clone a fork: Clone your fork of an example repository to your local machine.
    2. Create a workflow file: Create .github/workflows/cml.yaml in your repository.
    3. Configure the workflow: Use actions/setup-python@v4 and iterative/setup-cml@v1 to prepare the environment. Ensure you pass the REPO_TOKEN environment variable using ${{ secrets.GITHUB_TOKEN }}.
    4. Generate and post a report: In your workflow's run step, train your model, append metrics and images to a markdown file (e.g., report.md), and call cml comment create report.md.

    Example workflow structure:

    name: model-training
    on: [push]
    jobs:
      run:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - uses: actions/setup-python@v4
          - uses: iterative/setup-cml@v1
          - name: Train model
            env:
              REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            run: |
              pip install -r requirements.txt
              python train.py
    
              cat metrics.txt >> report.md
              echo "![](./plot.png)" >> report.md
              cml comment create report.md
    name: model-training
    on: [push]
    jobs:
      run:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - uses: actions/setup-python@v4
          - uses: iterative/setup-cml@v1
          - name: Train model
            env:
              REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            run: |
              pip install -r requirements.txt
              python train.py
    
              cat metrics.txt >> report.md
              echo "![](./plot.png)" >> report.md
              cml comment create report.md
  4. Install CML as a local package

    main

    You can install CML globally using npm. Alternatively, you can download standalone binaries for your system from the GitHub releases page.

    npm install --location=global @dvcorg/cml
  5. Use CML with DVC for Data and Metrics

    main

    CML integrates with DVC to handle large datasets and visualize metric/plot differences between Git commits.

    To use them together:

    1. Use a CML Docker image: Use container: ghcr.io/iterative/cml:0-dvc2-base1 in your GitHub Action to have NodeJS, Python 3, DVC, and CML pre-installed.
    2. Pull data: Use dvc pull data --run-cache to retrieve datasets from remote storage.
    3. Compare metrics: Use dvc metrics diff main --show-md >> report.md to append a markdown comparison of metrics against the main branch to your report.
    4. Compare plots: Use dvc plots diff to generate visualizations (like Vega JSON) and convert them to images (e.g., using vl2png) to include in your report.

    Example workflow snippet:

    jobs:
      run:
        runs-on: ubuntu-latest
        container: ghcr.io/iterative/cml:0-dvc2-base1
        steps:
          - uses: actions/checkout@v3
          - name: Train model
            env:
              REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
              AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
            run: |
              pip install -r requirements.txt
              dvc pull data --run-cache
              dvc repro
    
              echo "## Metrics" >> report.md
              git fetch --prune
              dvc metrics diff main --show-md >> report.md
    
              cml comment create report.md
    name: model-training
    on: [push]
    jobs:
      run:
        runs-on: ubuntu-latest
        container: ghcr.io/iterative/cml:0-dvc2-base1
        steps:
          - uses: actions/checkout@v3
          - name: Train model
            env:
              REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
              AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
            run: |
              # Install requirements
              pip install -r requirements.txt
    
              # Pull data & run-cache from S3 and reproduce pipeline
              dvc pull data --run-cache
              dvc repro
    
              # Report metrics
              echo "## Metrics" >> report.md
              git fetch --prune
              dvc metrics diff main --show-md >> report.md
    
              # ... (plot diff logic) ...
    
              cml comment create report.md
  6. Setup CML with GitHub Actions

    main

    To use CML with GitHub, you must create a workflow file at .github/workflows/cml.yaml. The workflow typically involves checking out the code, setting up the CML environment using iterative/setup-cml@v1, running your training script, and then using the cml comment create command to post results as a comment in the Pull Request.

    For self-hosted runners, you may need to manually set up Node.js and Python. You can also optionally use a pre-configured container image that includes Ubuntu LTS, DVC, and CML.

    name: your-workflow-name
    on: [push]
    jobs:
      run:
        runs-on: ubuntu-latest
        # optionally use a convenient Ubuntu LTS + DVC + CML image
        # container: ghcr.io/iterative/cml:0-dvc2-base1
        steps:
          - uses: actions/checkout@v3
          # may need to setup NodeJS & Python3 on e.g. self-hosted
          # - uses: actions/setup-node@v3
          #   with:
          #     node-version: '16'
          # - uses: actions/setup-python@v4
          #   with:
          #     python-version: '3.x'
          - uses: iterative/setup-cml@v1
          - name: Train model
            run: |
              # Your ML workflow goes here
              pip install -r requirements.txt
              python train.py
          - name: Write CML report
            env:
              REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
            run: |
              # Post reports as comments in GitHub PRs
              cat results.txt >> report.md
              cml comment create report.md
  7. Install NodeJS

    main

    CML and Vega-Lite require NodeJS. Installation methods vary by environment:

    GitHub Actions: Use the actions/setup-node action.

    GitLab (Direct Installation): Use the NodeSource setup script and apt-get.

    # GitHub Actions
    uses: actions/setup-node@v3
    with:
      node-version: '16'
    # GitLab / Ubuntu
    curl -sL https://deb.nodesource.com/setup_16.x | bash
    apt-get update
    apt-get install -y nodejs
  8. Set up on-premise (local) runners

    main

    To use your own local machines or on-premise GPU clusters as CML runners, install CML as a package and use the cml runner launch command with your repository URL and a Personal Access Token (PAT).

    # Install CML
    npm install --location=global @dvcorg/cml
    
    # Launch local runner
    cml runner launch \
      --repo=$your_project_repository_url \
      --token=$PERSONAL_ACCESS_TOKEN \
      --labels="local,runner" \
      --idle-timeout=180
  9. Create CML Reports with Markdown

    main

    CML reports are written in standard Markdown (supporting GitHub, GitLab, or Bitbucket flavors). You can include text, tables, and images. If an image is produced by your ML workflow, CML can automatically upload and include it in the report.

    To include text, append it to a markdown file:

    cat results.txt >> report.md

    To include images, use standard markdown image syntax. If graph.png is an output of your training script, you can include it like this:

    echo "![](./graph.png)" >> report.md
    cml comment create report.md
    echo "![](./graph.png)" >> report.md
    cml comment create report.md
  10. Allocate cloud compute resources with `cml runner launch`

    main

    CML can automatically allocate cloud instances (AWS, Azure, GCP, or Kubernetes) to run computationally intensive jobs, such as those requiring GPUs. When a job finishes, the instance is automatically shut down. cml runner also provides automatic job restarts in case of GitHub Actions timeouts or cloud spot instance interruptions.

    To use cloud allocation, you must provide cloud service credentials (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) as environment variables in your workflow and a REPO_TOKEN (Personal Access Token) with repository read/write access.

    name: Train-in-the-cloud
    on: [push]
    jobs:
      deploy-runner:
        runs-on: ubuntu-latest
        steps:
          - uses: iterative/setup-cml@v1
          - uses: actions/checkout@v3
          - name: Deploy runner on EC2
            env:
              REPO_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
              AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
              AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
            run: |
              cml runner launch \
                --cloud=aws \
                --cloud-region=us-west \
                --cloud-type=g4dn.xlarge \
                --labels=cml-gpu
      train-model:
        needs: deploy-runner
        runs-on: [self-hosted, cml-gpu]
        # ... rest of job
  11. How to use CML in CI/CD pipelines

    main

    CML (Continuous Machine Learning) is designed to run in CI/CD environments like GitHub Actions, GitLab CI, and Bitbucket Pipelines to automate machine learning workflows, such as training models and reporting results.

    Supported Platforms

    • GitHub Actions
    • GitLab CI
    • Bitbucket Pipelines

    Environment Setup

    To use CML, you typically need a runtime environment with both Python 3 and Node.js installed. If you are using self-hosted runners, you may need to install these manually.

    For a convenient setup on Ubuntu LTS, you can use the official CML Docker image which comes pre-configured with DVC and CML: ghcr.io/iterative/cml:0-dvc2-base1

    Example: GitHub Actions Setup

    When using GitHub Actions, you can use standard setup actions to configure your environment:

    - uses: actions/setup-node@v3
      with:
        node-version: '16'
    
    - uses: actions/setup-python@v4
    # Example GitHub Actions configuration
    - uses: actions/setup-node@v3
      with:
        node-version: '16'
    
    - uses: actions/setup-python@v4