Skip to main content

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

Choose your platform for installation instructions.

macOS

The easiest way to install Cog on macOS is with Homebrew:

brew install replicate/tap/cog

You can also use the 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

Or install manually:

sudo curl -o /usr/local/bin/cog -L "https://github.com/replicate/cog/releases/latest/download/cog_$(uname -s)_$(uname -m | sed 's/aarch64/arm64/')"
sudo chmod +x /usr/local/bin/cog
sudo xattr -d com.apple.quarantine /usr/local/bin/cog 2>/dev/null || true

If you see a Gatekeeper warning saying the binary "cannot be opened because the developer cannot be verified", run:

sudo xattr -d com.apple.quarantine /usr/local/bin/cog
Linux

You can install Cog using the 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

Or install manually:

sudo curl -o /usr/local/bin/cog -L "https://github.com/replicate/cog/releases/latest/download/cog_$(uname -s)_$(uname -m | sed 's/aarch64/arm64/')"
sudo chmod +x /usr/local/bin/cog
Windows

Cog does not natively support Windows, but you can run it on Windows 11 using WSL 2. Once WSL 2 is set up, follow the Linux installation instructions above.

Docker

To install Cog inside a Docker image:

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!

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.22.0.tar.gz (19.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.22.0-py3-none-any.whl (42.4 kB view details)

Uploaded Python 3

File details

Details for the file cog-0.22.0.tar.gz.

File metadata

  • Download URL: cog-0.22.0.tar.gz
  • Upload date:
  • Size: 19.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for cog-0.22.0.tar.gz
Algorithm Hash digest
SHA256 23f9134ec454c429ce03962be8d76faf3b22f49c22bb9ce9de12419a4b14b737
MD5 ccb7d0d407a39f96fb37535787041df4
BLAKE2b-256 25b913852c951c2db765d6deafaaec6fb4795a91bf654ae667cdf0856b91fb92

See more details on using hashes here.

Provenance

The following attestation bundles were made for cog-0.22.0.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.22.0-py3-none-any.whl.

File metadata

  • Download URL: cog-0.22.0-py3-none-any.whl
  • Upload date:
  • Size: 42.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for cog-0.22.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44d80378fb481cbf82991c9828b8895363602d1663408388864c76264e43046f
MD5 72499f49ca3f1cc9ffd6cb2ec7794e1f
BLAKE2b-256 d907a8dbda2e15eb12412e55ca9471dc18b61fd01b668801ae1426b300f7e95d

See more details on using hashes here.

Provenance

The following attestation bundles were made for cog-0.22.0-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.

Release history Release notifications | RSS feed

This release

0.22.0 This release

2 files

0.21.0

2 files

0.20.0

2 files

0.19.3

2 files

0.19.2

2 files

0.19.1

2 files

0.19.0

2 files

0.18.0

2 files

0.17.2

2 files

0.17.1

2 files

0.17.0

2 files

0.16.12

2 files

0.16.11

2 files

0.16.10

2 files

0.16.8

2 files

0.16.7

2 files

0.16.6

2 files

0.16.5

2 files

0.16.4

2 files

0.16.3

2 files

0.16.2

2 files

0.16.1

2 files

0.16.0

2 files

0.15.11

2 files

0.15.10

2 files

0.15.9

2 files

0.15.8

2 files

0.15.7

2 files

0.15.6

2 files

0.15.5

2 files

0.15.4

2 files

0.15.3

2 files

0.15.2

2 files

0.15.1

2 files

0.15.0

2 files

0.14.12

2 files

0.14.11

2 files

0.14.10

2 files

0.14.9

2 files

0.14.8

2 files

0.14.7

2 files

0.14.6

2 files

0.14.5

2 files

0.14.4

2 files

0.14.3

2 files

0.14.2

2 files

0.14.1

2 files

0.14.0

2 files

0.13.7

2 files

0.13.6

2 files

0.13.3

2 files

0.13.2

2 files

0.13.0

2 files

0.12.1

2 files

0.12.0

2 files

0.11.6

2 files

0.11.5

2 files

0.11.4

2 files

0.11.3

2 files

0.11.2

2 files

0.11.1

2 files

0.11.0

2 files

0.9.26

2 files

0.9.25

2 files

0.9.24

2 files

0.9.23

2 files

0.9.22

2 files

0.9.21

2 files

0.9.20

2 files

0.9.19

2 files

0.9.16

1 file

0.9.13

1 file

0.9.12

1 file

0.9.9

1 file

0.9.8

1 file

0.9.7

1 file

0.9.6

1 file

0.9.5

1 file

0.9.4

1 file

0.9.3

1 file

0.9.2

1 file

0.9.0

1 file

0.8.6

1 file

0.8.5

1 file

0.8.4

1 file

0.8.3

1 file

0.8.2

1 file

0.8.1

1 file

0.8.0

1 file

0.7.2

1 file

0.7.1

1 file

0.7.0

1 file

0.6.1

1 file

0.6.0

1 file

0.5.1

1 file

0.5.0

1 file

0.4.4

1 file

0.4.3

1 file

0.0.3

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page