Skip to main content

Containers for machine learning

Project description

Cog: Containers for machine learning

Cog is an open-source tool that lets you package machine learning models in a standard, production-ready container.

You can deploy your packaged model to your own infrastructure, or to Replicate.

Highlights

  • ๐Ÿ“ฆ Docker containers without the pain. Writing your own Dockerfile can be a bewildering process. With Cog, you define your environment with a simple configuration file and it generates a Docker image with all the best practices: Nvidia base images, efficient caching of dependencies, installing specific Python versions, sensible environment variable defaults, and so on.

  • ๐Ÿคฌ๏ธ No more CUDA hell. Cog knows which CUDA/cuDNN/PyTorch/Tensorflow/Python combos are compatible and will set it all up correctly for you.

  • โœ… Define the inputs and outputs for your model with standard Python. Then, Cog generates an OpenAPI schema and validates the inputs and outputs.

  • ๐ŸŽ Automatic HTTP inference server: Your model's types are used to dynamically generate a RESTful HTTP API using a high-performance Rust/Axum server.

  • ๐Ÿš€ Ready for production. Deploy your model anywhere that Docker images run. Your own infrastructure, or Replicate.

How it works

Define the Docker environment your model runs in with cog.yaml:

build:
  gpu: true
  system_packages:
    - "libgl1"
    - "libglib2.0-0"
  python_version: "3.13"
  python_requirements: requirements.txt
run: "run.py:Runner"

Define how your model runs with run.py:

from cog import BaseRunner, Input, Path
import torch

class Runner(BaseRunner):
    def setup(self):
        """Load the model into memory to make running multiple inferences efficient"""
        self.model = torch.load("./weights.pth")

    # The arguments and types the model takes as input
    def run(self,
          image: Path = Input(description="Grayscale input image")
    ) -> Path:
        """Run the model"""
        processed_image = preprocess(image)
        output = self.model(processed_image)
        return postprocess(output)

In the above we accept a path to the image as an input, and return a path to our transformed image after running it through our model.

Now, you can run the model:

$ cog run -i image=@input.jpg
--> Building Docker image...
--> Running...
--> Output written to output.jpg

Or, build a Docker image for deployment:

$ cog build -t my-classification-model
--> Building Docker image...
--> Built my-classification-model:latest

$ docker run -d -p 5000:5000 --gpus all my-classification-model

$ curl http://localhost:5000/predictions -X POST \
    -H 'Content-Type: application/json' \
    -d '{"input": {"image": "https://.../input.jpg"}}'

Or, combine build and run via the serve command:

$ cog serve -p 8080

$ curl http://localhost:8080/predictions -X POST \
    -H 'Content-Type: application/json' \
    -d '{"input": {"image": "https://.../input.jpg"}}'

Why are we building this?

It's really hard for researchers to ship machine learning models to production.

Part of the solution is Docker, but it is so complex to get it to work: Dockerfiles, pre-/post-processing, Flask servers, CUDA versions. More often than not the researcher has to sit down with an engineer to get the damn thing deployed.

Andreas and Ben created Cog. Andreas used to work at Spotify, where he built tools for building and deploying ML models with Docker. Ben worked at Docker, where he created Docker Compose.

We realized that, in addition to Spotify, other companies were also using Docker to build and deploy machine learning models. Uber and others have built similar systems. So, we're making an open source version so other people can do this too.

Hit us up if you're interested in using it or want to collaborate with us. We're on Discord or email us at team@replicate.com.

Prerequisites

  • macOS, Linux or Windows 11. Cog works on macOS, Linux and Windows 11 with WSL 2
  • Docker. Cog uses Docker to create a container for your model. You'll need to install Docker before you can run Cog. If you install Docker Engine instead of Docker Desktop, you will need to install Buildx as well.

Install

If you're using macOS, you can install Cog using Homebrew:

brew install replicate/tap/cog

You can also download and install the latest release using our install script:

# bash, zsh, and other shells
sh <(curl -fsSL https://cog.run/install.sh)

# fish shell
sh (curl -fsSL https://cog.run/install.sh | psub)

# download with wget and run in a separate command
wget -qO- https://cog.run/install.sh
sh ./install.sh

You can manually install the latest release of Cog directly from GitHub by running the following commands in a terminal:

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

Or if you are on docker:

RUN sh -c "INSTALL_DIR=\"/usr/local/bin\" SUDO=\"\" $(curl -fsSL https://cog.run/install.sh)"

Upgrade

If you're using macOS and you previously installed Cog with Homebrew, run the following:

brew upgrade replicate/tap/cog

Otherwise, you can upgrade to the latest version by running the same commands you used to install it.

Development

See CONTRIBUTING.md for how to set up a development environment and build from source.

Next steps

Need help?

Join us in #cog on Discord.

Ask DeepWiki

Contributors โœจ

Thanks goes to these wonderful people (emoji key):

Ben Firshman
Ben Firshman

๐Ÿ’ป ๐Ÿ“–
Andreas Jansson
Andreas Jansson

๐Ÿ’ป ๐Ÿ“– ๐Ÿšง
Zeke Sikelianos
Zeke Sikelianos

๐Ÿ’ป ๐Ÿ“– ๐Ÿ”ง
Rory Byrne
Rory Byrne

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
Michael Floering
Michael Floering

๐Ÿ’ป ๐Ÿ“– ๐Ÿค”
Ben Evans
Ben Evans

๐Ÿ“–
shashank agarwal
shashank agarwal

๐Ÿ’ป ๐Ÿ“–
VictorXLR
VictorXLR

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
hung anna
hung anna

๐Ÿ›
Brian Whitman
Brian Whitman

๐Ÿ›
JimothyJohn
JimothyJohn

๐Ÿ›
ericguizzo
ericguizzo

๐Ÿ›
Dominic Baggott
Dominic Baggott

๐Ÿ’ป โš ๏ธ
Dashiell Stander
Dashiell Stander

๐Ÿ› ๐Ÿ’ป โš ๏ธ
Shuwei Liang
Shuwei Liang

๐Ÿ› ๐Ÿ’ฌ
Eric Allam
Eric Allam

๐Ÿค”
Ivรกn Perdomo
Ivรกn Perdomo

๐Ÿ›
Charles Frye
Charles Frye

๐Ÿ“–
Luan Pham
Luan Pham

๐Ÿ› ๐Ÿ“–
TommyDew
TommyDew

๐Ÿ’ป
Jesse Andrews
Jesse Andrews

๐Ÿ’ป ๐Ÿ“– โš ๏ธ
Nick Stenning
Nick Stenning

๐Ÿ’ป ๐Ÿ“– ๐ŸŽจ ๐Ÿš‡ โš ๏ธ
Justin Merrell
Justin Merrell

๐Ÿ“–
Rurik Ylรค-Onnenvuori
Rurik Ylรค-Onnenvuori

๐Ÿ›
Youka
Youka

๐Ÿ›
Clay Mullis
Clay Mullis

๐Ÿ“–
Mattt
Mattt

๐Ÿ’ป ๐Ÿ“– ๐Ÿš‡
Eng Zer Jun
Eng Zer Jun

โš ๏ธ
BB
BB

๐Ÿ’ป
williamluer
williamluer

๐Ÿ“–
Simon Eskildsen
Simon Eskildsen

๐Ÿ’ป
F
F

๐Ÿ› ๐Ÿ’ป
Philip Potter
Philip Potter

๐Ÿ› ๐Ÿ’ป
Joanne Chen
Joanne Chen

๐Ÿ“–
technillogue
technillogue

๐Ÿ’ป
Aron Carroll
Aron Carroll

๐Ÿ“– ๐Ÿ’ป ๐Ÿค”
Bohdan Mykhailenko
Bohdan Mykhailenko

๐Ÿ“– ๐Ÿ›
Daniel Radu
Daniel Radu

๐Ÿ“– ๐Ÿ›
Itay Etelis
Itay Etelis

๐Ÿ’ป
Gennaro Schiano
Gennaro Schiano

๐Ÿ“–
Andrรฉ Knรถrig
Andrรฉ Knรถrig

๐Ÿ“–
Dan Fairs
Dan Fairs

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cog-0.21.0rc3.tar.gz (14.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cog-0.21.0rc3-py3-none-any.whl (41.8 kB view details)

Uploaded Python 3

File details

Details for the file cog-0.21.0rc3.tar.gz.

File metadata

  • Download URL: cog-0.21.0rc3.tar.gz
  • Upload date:
  • Size: 14.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cog-0.21.0rc3.tar.gz
Algorithm Hash digest
SHA256 bed745040b426413b2cc76b1c7961488cc3ccdf2f630b732257707ebabcc5fd6
MD5 8c07574f44d904cf20a64cd0180510e1
BLAKE2b-256 8cf30741a8f7cd4b03109e61149b7deddc4cf0b4ced88eda245cf28d6a4a210f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cog-0.21.0rc3.tar.gz:

Publisher: release-publish.yaml on replicate/cog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cog-0.21.0rc3-py3-none-any.whl.

File metadata

  • Download URL: cog-0.21.0rc3-py3-none-any.whl
  • Upload date:
  • Size: 41.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cog-0.21.0rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 a3f14b4c78a7fcab7d23b67874ef1338fcc56065ce8e99be2a6f0f38f82d5708
MD5 aa4c904459b54e695623de9db68c04cc
BLAKE2b-256 e1fc5d4add4e83b2003b73425aa8d2a9e4b7ab5f8bfd29006613913bbe36dd6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cog-0.21.0rc3-py3-none-any.whl:

Publisher: release-publish.yaml on replicate/cog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page