Cog replicate

From samirpatil2000/skills · View interactive version

Cog replicate is an AI agent skill from the GitHub repository samirpatil2000/skills. Package an ML model (diffusion, video, LLM, any PyTorch/HF model) with Replicate Cog and push it to Replicate, from a Windows machine through WSL2 — toolchain setup (Docker Engine, nvidia-container-toolkit, cog), cog.yaml and predict.py patterns, large weights with pget, gated Hugging Face weights w

How to use this skill

Skill file

Cog → Replicate

Replicate runs a Docker image that Cog builds from cog.yaml + predict.py. The local machine is

usually only a build-and-push box: it does not need a GPU that can run the model. Plan for this split

from the start, because most of the pain is in the build/push/weights logistics, not in the model code.

Read the reference files when you reach the matching step:

Scripts (run them, do not re-invent them):

0. Before you start: facts to collect

Collect these first; each one changes the design:

  1. Machine: OS, local GPU VRAM, RAM, free disk on each drive, whether WSL2/Docker exist.

On Windows, Cog runs only inside WSL2. A 4–8 GB local GPU is fine — it is only used for smoke tests.

  1. Model weights: exact repos, file names, byte sizes (HF API ?blobs=true), license, gated or not.

Pin a code revision (git tag/commit) of the inference repo — upstream main moves (example: LTX-2 main

switched the HDR pipeline to a different model family; the 2.3 LoRA needed tag v1.3.0).

  1. Text encoder: does the pipeline load a big text encoder (LTX-2.3: Gemma 3 12B, 24 GB, gated by

Google)? Does the repo ship precomputed embeddings (LTX HDR LoRAs: *-scene-emb.safetensors)? If the

prompt set is finite (fixed captions, dropdowns), precompute once and keep the encoder out of the

runtime image — patterns.md §7. Hosted chat APIs (AI Studio, Replicate, fal) return text, not the

hidden states that diffusion models condition on.

  1. Target GPU and VRAM need (see references/optimizations.md). Check the model's own VRAM table.
  2. Replicate: owner/model name, public or private (billing differs a lot), API token.
  3. Shared machine? If another Claude session uses the same Docker/WSL, agree who changes shared

state (daemon.json, .wslconfig, WSL restarts, prune) and tell each other before builds/pushes —

two parallel builds/pushes on a 6 GB WSL VM run out of memory.

Write findings to a local docs/RESEARCH.md as you go; long sessions get compacted.

1. Toolchain (one time, WSL2)

Run scripts/wsl_setup.sh as root inside the distro (wsl -d Ubuntu -u root -- bash /mnt/c/.../wsl_setup.sh).

It installs Docker Engine (not Docker Desktop), buildx, nvidia-container-toolkit, cog, and sets

containerd-snapshotter: false (the containerd image store causes 403s on push).

Check: docker run --rm --gpus all nvidia/cuda:12.8.0-base-ubuntu22.04 nvidia-smi -L.

replicate.com/auth/token). This works with the API token:

cat token | docker login r8.im -u <owner> --password-stdin.

2. Project files

Start from references/patterns.md. The decisions that matter:

(parallel downloader) if missing, with exact size checks. Public HF resolve/ URLs work directly.

Take an hf_token: Secret input, verify access on every prediction (HEAD on a gated file), download

on first use, cache the pipeline. cog.Secret is not available in setup().

those as base64 data: URIs instead of uploading them (huge JSON, no download links). The list order is

NOT preserved on Replicate (uploads finish in any order) — give files distinct names.

media info. It is the only way to see where billed GPU time goes.

Use the _CurrentStderrHandler from patterns.md.

input parsing, token check and output writers. This is how you test end-to-end on a small GPU.

pip install -r requirements.txt layer fails to push.

3. Local verification (no big GPU needed)

Do these in order; each catches a different class of bug cheaply:

  1. Resolve deps first (minutes, not a 30-min build): pip install --dry-run --report or

uv pip compile in a matching python container.

  1. cog build -t <tag> (first build 20–40 min). Add a final build.run step that imports the model

package, so import errors fail the build, not the first prediction.

  1. Unit tests inside the image: docker run --rm -v <proj>:/src -w /src <tag> python -m pytest -q tests/.

Test the output writers, sizing rules, and the return type (List[Path]).

  1. cog predict -e <NAME>_MOCK=1 -i video=@sample.mp4 -i hf_token=$(cat .secrets/hf_token).

Also run once with a bad token and confirm a clear error.

  1. HTTP test (scripts/http_predict.py): this is the only way to see predict-time logs locally

(cog predict hides them) and to see exactly what the API returns.

4. Push

client attached (WSL stops an idle VM), and drops page caches (WSL page cache counts as used memory

on Windows). A plain background task in Claude Code can be killed by the memory reaper; Claude Code

then forbids restarting it without the user's approval — ask the user instead of retrying.

COG_PUSH_OCI=1 on a small-RAM machine: it exports the whole image first.

5. Run on Replicate and verify

python scripts/run_replicate.py <owner/model> -i video=@samples/clip.mp4 -s hf_token=@.secrets/hf_token

The runner cancels a prediction that stays starting for 5 minutes (--start-timeout, default 300). A stall at

starting means the container never booted (image pull or start), so setup logs never appear. Check the image size

first (docker history), not the weights.

boot (image pull + setup()); record metrics.predict_time vs total_time.

HTTP 200 AND the files pass a content check (ffprobe: codec, frame count, color tags; image: size).

at frames at negative exposure. A file with the right tags can still be SDR inside.

6. Report back

Tell the user: version id, prediction ids, predict/total time, cost estimate (predict_time × $/s),

output checks, what is still untested. Update docs/RESEARCH.md with any new error mode, and add it to

references/errors.md of this skill if it is generic.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.