Cog Documentation
repository·main·Indexed 27 days ago
https://github.com/replicate/cogAn open-source tool for packaging machine learning models into production-ready Docker containers. It includes Coglet, a Rust-based prediction server that provides process isolation, concurrent slot management, and high-performance IPC via Unix domain sockets. The system features coglet-python PyO3 bindings to bridge Python predictor classes to the Rust runtime, supporting both synchronous and asynchronous prediction handlers with native cancellation and routed logging.
What's inside Cog
- Coglet is the Rust-based prediction server that powers Cog's subprocess isolation model. It provides process isolation, concurrent slot management, and high-performance Inter-Process Communication (IPC) for running machine learning predictions. It utilizes a parent process (containing an HTTP server and an orchestrator) and a worker subprocess (containing the Python runtime and a Tokio runtime).
Overview of coglet-python
maincoglet-pythonprovides PyO3 bindings that bridge the Rustcogletlibrary to Python. It implements thePredictHandlertrait by wrapping Python predictor classes, allowing Python-based machine learning models to be served using the Coglet infrastructure.Overview of coglet
maincoglet is a core Rust library designed for the coglet prediction server. It is written in pure Rust and contains no Python dependencies. Python bindings for this library are provided separately in thecoglet-pythonpackage. It provides the architecture for a parent-side orchestrator and a child-side worker to manage machine learning model predictions via IPC (Unix sockets) and HTTP.Understand Managed Weights OCI Format
mainManaged weights are named sets of files (model weights, configs, tokenizers, etc.) stored as OCI artifacts in a container registry. They are delivered independently from the model image and map to a target directory in the running container.
Key characteristics:
- Immutable Layers: Once pushed, layers are fixed and identified by their digest.
- Order-Invariant Extraction: Layers are independent and disjoint. They can be downloaded and extracted in any order to produce identical results. Unlike Docker layers, they do not use overlay/union filesystem semantics.
- No File Splitting: Each file is contained entirely within a single layer. Files are never split across multiple layers.
Understand the Cog Build System Flow
mainThe Cog build system transforms your model source (consisting of
cog.yaml,run.py, and model weights) into a production-ready OCI container image.The build process follows these stages:
- Config Parsing & Validation: Reads
cog.yaml, validates Python versions (3.10-3.13), and auto-detects CUDA/cuDNN versions based on your PyTorch or TensorFlow requirements. - Dockerfile Generation: Generates a Dockerfile including a base image, system packages, Python packages, Cog SDK/coglet wheels, user run commands, and your source code.
- Docker Build: Executes the build using Buildkit.
- Post-Build: Generates an OpenAPI schema, runs
pip freezeto capture dependencies, and applies metadata labels to the image.
- Config Parsing & Validation: Reads
Understand the Cog Model Schema
mainThe Cog schema is an OpenAPI 3.0.2 specification that defines the contract for a model's interface. While every Cog model uses a fixed prediction API envelope, the schema specifically defines the model-specific
inputandoutputfields.Consumers use this schema to:
- Replicate platform: Generate web UI input forms and validate requests.
- HTTP server (coglet): Validate incoming JSON and reject malformed requests.
- CLI (
cog run): Parse-i key=valueflags into correctly-typed Python objects. - Docker labels: Extract the model interface without running the container.
- API clients: Determine required inputs, types, constraints (min/max, required fields), and output structures.
Understand the Cog Container Runtime Architecture
mainCog uses a two-process architecture to run machine learning models. This design isolates the user's model code from the HTTP server to improve stability, resource management, and shutdown handling.
- Parent Process (Rust HTTP Server): Acts as the orchestrator. It handles the HTTP API (on port 5000), request validation, input/output file management, webhook delivery, and the lifecycle of the worker subprocess.
- Worker Subprocess (Python): Executes the actual model code. It loads the user's predictor, runs the
setup()method, and executes therun()(or legacypredict()) method.
Communication between the processes occurs via a Control Channel (stdin/stdout) for lifecycle messages and a Slot Channel (Unix sockets) for per-prediction data to avoid head-of-line blocking.
Understand the Coglet Architecture
mainCoglet is the Rust runtime for Cog, consisting of two primary components:
- coglet (core library): A pure Rust library that manages the lifecycle of workers, routes messages, and handles concurrency via a
PermitPool. It includes an Axum-based HTTP server for prediction transport. - coglet-python (PyO3 bindings): A bridge that allows Python predictors to run within the Coglet runtime. It handles the wrapping of Python predictor classes, input processing, and log routing.
- coglet (core library): A pure Rust library that manages the lifecycle of workers, routes messages, and handles concurrency via a
Core Cog Components
mainCog consists of several key components that facilitate the model lifecycle:
- Model Source: The user-provided
cog.yaml(environment config), aRunnerclass (containingsetup()andrun()methods), and model weights. - Python SDK: The
cogpackage used to build models. It providesBaseRunner, a type system (Input,Path,Secret,ConcatenateIterator), and the entry point for the server. - Schema: An OpenAPI specification automatically generated from the predictor's type hints, defining inputs and outputs.
- Prediction API: An HTTP interface using a fixed
PredictionRequest/PredictionResponseenvelope format. - Container Runtime: A Rust HTTP server (Axum) that manages worker processes via subprocess isolation and executes predictions using PyO3 bindings.
- Build System: Converts
cog.yamland code into a Docker image with appropriate Python, CUDA, and dependency configurations. - CLI: The primary command-line tool for building, testing, and deploying models.
- Model Source: The user-provided
Understand Cog Architecture Overview
mainCog packages machine learning models into production-ready OCI images. The workflow involves providing model code and acog.yamlconfiguration, which the Cog CLI and Python SDK use to build a container image. This image contains a Rust-based server (Coglet) that serves a Prediction API via an HTTP interface.Initialize a Cog project
mainTo start using Cog with your own machine learning model, you need to generate the necessary configuration files:
cog.yaml(for system and Python dependencies) andrun.py(to define the model's execution interface). Use thecog initcommand within your model's directory to create these files.$ cd path/to/your/model $ cog initInstall Docker Desktop with WSL 2 support
main- Download and install Docker Desktop for Windows.
- Open Docker Desktop and navigate to Settings → General.
- Ensure Use the WSL 2 based engine is checked.
- Click Apply & Restart and reboot your computer.