Home Knowledge Base Cog

Cog is an open-source tool by Replicate that packages machine learning models into standard, production-ready Docker containers — solving the "works on my machine" problem by using a simple cog.yaml configuration file to automatically generate Dockerfiles with correct CUDA drivers, Python versions, system dependencies, and a standardized HTTP prediction API, turning any Python model into a deployable container without writing a single line of Docker configuration.

What Is Cog?

How Cog Works

StepWhat You DoWhat Cog Does
1. Define dependenciesWrite cog.yaml with Python version + packagesGenerates multi-stage Dockerfile
2. Write predict functionPython class with setup() and predict() methodsCreates HTTP /predictions endpoint
3. BuildRun cog buildBuilds Docker image with CUDA, dependencies
4. Test locallyRun cog predict -i [email protected]Runs prediction in container
5. DeployPush to Replicate or any Docker hostInstant API hosting

cog.yaml Example

build:
  gpu: true
  python_version: "3.10"
  python_packages:
    - torch==2.1
    - transformers==4.36
  system_packages:
    - ffmpeg

predict: "predict.py:Predictor"

predict.py Example

from cog import BasePredictor, Input, Path

class Predictor(BasePredictor):
    def setup(self):
        """Load model into memory (runs once on startup)"""
        self.model = load_model("weights/model.pt")

    def predict(self, image: Path = Input(description="Input image")) -> Path:
        """Run inference on an input image"""
        output = self.model(image)
        return Path(output)

Cog vs Alternatives

ToolApproachStrengthsLimitations
CogYAML + predict class → DockerSimplest path to container, Replicate integrationReplicate-specific ecosystem
BentoMLPython decorators → Bento → containerMore flexible, multi-model supportMore complex API
Docker (manual)Write Dockerfile from scratchFull controlRequires Docker expertise, CUDA pain
TorchServe / TF ServingFramework-specific serverOptimized for specific frameworkFramework lock-in
TritonNVIDIA inference serverBest GPU performanceComplex configuration

Cog is the fastest path from ML model to production Docker container — eliminating the DevOps complexity of CUDA drivers, system dependencies, and HTTP API setup through a simple YAML configuration and Python prediction class, enabling data scientists to package any model into a standardized, deployable container without Docker expertise.

cogcontainerpredict

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.