Skip to main content

Introduction

This tutorial covers migrating workloads from Replicate to Cerebrium in less than 5 minutes. This example migrates the SDXL-Lightning-4step model from ByteDance. Find it on Replicate here. Follow along with the code in the GitHub repo. Start by creating the Cerebrium project.
Cerebrium and Replicate both use a setup file: cog.yaml and cerebrium.toml for Replicate and Cerebrium respectively. Based on the cog.yaml, add/change the following in cerebrium.toml
The configuration above:
  • Uses an Nvidia base image with CUDA libraries (Cuda 12). You can see other images here.
  • Sets hardware based on CPU/GPU requirements. You can see the available options in the GPU guide and CPU and memory guide.
  • Copies the required pip packages
  • Downloads pget (used by Replicate for model weights) via curl and shell commands in cerebrium.toml
The hardware and environment setup now matches. The cog.yaml indicates the endpoint file — in this case, predict.py. Cerebrium’s equivalent entry file is main.py. Start by copying all import statements and constant variables unrelated to Replicate/Cog:
Replicate uses classes, while Cerebrium runs standard Python code and makes each function an endpoint. Remove all self. references throughout the code. The repo contains a “feature-extractor” folder needed in the Cerebrium project. Since it’s small, copy the folder contents directly: Folder Structure Replicate’s setup function runs on each cold start (each new app instantiation). Define it as top-level code below the import statements.
The code downloads model weights if they don’t exist and instantiates the models. To persist files/data on Cerebrium, store them at /persistent-storage. Update the paths:
Copy the remaining functions, run_safety_checker() and predict(). In Cerebrium, function parameters map directly to the expected JSON request data:
The above returns a path to the generated images. To return base64-encoded images for instant rendering, use the following. Alternatively, upload images to a storage bucket.
Run cerebrium deploy. The app builds in under 90 seconds. It should output the curl statement to run your app: Curl Request Replace the end of the URL with /predict (the target function) and send the required JSON data. Example result:
Read more about Cerebrium functionality: