Table of contents
This guide will show you how to set up continuous integration and deployment for your Cog model, so you can safely and easily publish new versions of your model as part of your GitHub-based development workflow.
CI/CD stands for continuous integration and deployment. It's a well-known process for building, testing, and deploying software. It has been a popular practice in the software industry for a long time, but it's just as applicable when developing machine learning models. After all, AI models are just software.
CI/CD has many benefits:
In this guide, you'll set up a CI/CD pipeline for your Cog model using GitHub Actions and github.com/replicate/cog-safe-push, an open-source project we use internally at Replicate to test and deploy our production models.
Your pipeline will:
Fuzz testing is a technique for finding bugs in software by passing invalid, unexpected, or random data as inputs to the software and seeing if it crashes. In this guide, we'll use Anthropic's Claude API to generate random inputs to test your model.
Language models are great for generating plain text, but they can also be used to generate JSON data. We'll take advantage of that here to generate structured data that matches the input schema of your model.
Before starting this guide, you'll need the following:
GitHub Actions is a feature built into GitHub that makes it easy to add scripts to your repository, and run those scripts on GitHub's servers. You can use GitHub Actions to do all kinds of automation, like running tests, deploying your model, and more.
To use GitHub Actions, you define workflows using YAML files stored in the .github/workflows
directory of your GitHub repository.
Create a new file in your repository called .github/workflows/cog-safe-push.yml
.
Then paste the following code into the file:
name: Cog Safe Push
on:
workflow_dispatch:
inputs:
model:
description: 'The name of the model to push, in the format owner/model-name'
type: string
jobs:
cog-safe-push:
# Tip: Create custom runners in your GitHub organization for faster builds
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.12"
- name: Install Cog
run: |
sudo curl -o /usr/local/bin/cog -L https://github.com/replicate/cog/releases/latest/download/cog_`uname -s`_`uname -m`
sudo chmod +x /usr/local/bin/cog
- name: cog login
run: |
echo ${{ secrets.COG_TOKEN }} | cog login --token-stdin
- name: Install cog-safe-push
run: |
pip install git+https://github.com/replicate/cog-safe-push.git
- name: Push selected models
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
REPLICATE_API_TOKEN: ${{ secrets.REPLICATE_API_TOKEN }}
run: |
cog-safe-push ${{ inputs.model }}
Commit this file to your Git repository and push it to GitHub.
Before you can run your GitHub Actions workflow, you'll need to set a Replicate API token as a secret in your GitHub repository.
REPLICATE_API_TOKEN
.You'll also need to set a Cog API token as a secret in your GitHub repository, so the cog-safe-push
script can push your model to Replicate.
COG_TOKEN
.You'll also need to set an Anthropic API key as a secret in your GitHub repository, so the cog-safe-push
script can generate structured JSON inputs to test your model.
ANTHROPIC_API_KEY
.Now that you've created your workflow and configured the necessary secrets, you can run the workflow manually.
If you've configured your workflow correctly, you should see a green checkmark next to the workflow run, and output like the following:
owner/model-name
.🎉 Success! You've just run your CI/CD pipeline. If all the steps passed, you've now got a newly published version of your model on Replicate.
The cog-safe-push
command has sensible defaults and will work without any specific configuration, but you'll probably want to customize it to your needs.
If you create a file named cog-safe-push.yaml
in your Cog directory, it will be used automatically.
Here's an example configuration that specifies a model, a test model, hardware, and a set of test cases:
# This is the model that the deployment will use
model: owner/my-model
# A private model to run the test against before pushing
test_model: owner/my-model-test
test_hardware: gpu-l40s
# Define the predict section to run predictions against the test model
predict:
compare_outputs: false
predict_timeout: 700
test_cases:
- inputs:
prompt: A formula one car
seed: 1
- inputs:
prompt: A cat
duration: 4
negative_prompt: Ginger cat
frame_image_url: https://tjzk.replicate.delivery/models_models_featured_image/afce5fa4-5f8a-45db-95c0-bf62ec6958e7/output-59.webp
seed: 2
To learn more about the available options, see the cog-safe-push README.
Now that you have a CI/CD pipeline for your Cog model, you can use it to publish new versions of your model as part of your GitHub-based development workflow.
The Actions workflow you created is triggered manually. This is a good starting point, as it lets you run the workflow repeatedly until you get everything working the way you want.
Once you've got things configured just right, consider updating the workflow to run automatically when you push to a branch of your repository, or whenever you open a pull request.
Remember that CI/CD is an iterative process, and you'll need to customize the workflow and testing parameters to match your needs. The investment is worth it though: CI/CD will help you ship new versions of your model more frequently, and your team will enjoy faster development velocity and fewer operational headaches. Ship it! 🚀