bertbertov/claude-stack/build-models
Claude Code and agent skill by bertbertov/claude-stack
- community
Checking that the SKILL.md file is still in the repository.
Package and build custom AI models with Cog for deployment on Replicate. Use when creating a cog.yaml or predict.py, defining model inputs and outputs, loading model weights at setup time, building Docker images for ML models, serving locally with cog serve or cog predict, or porting a HuggingFace, GitHub, or ComfyUI model to run on Replicate. Trigger on phrases like "build a model", "package a model", "create a Cog model", "wrap a model", "containerize an AI model", "predict.py", "cog.yaml", "BasePredictor", or "Cog container", and when referencing cog.run, github.com/replicate/cog, or github.com/replicate/cog-examples. Covers GPU and CUDA setup, pget for fast weight downloads, async predictors with continuous batching, streaming outputs, and cold-boot optimization for image, video, audio, and LLM models. For pushing built models to Replicate, see publish-models. For running existing models, see run-models.
Install
npx vskill@latest install bertbertov/claude-stack/build-modelsSKILL.md
The same SKILL.md was published earlier as replicate/skills/build-models.
Docs
- Cog reference (single file): <https://cog.run/llms.txt>
cog.yamlreference: <https://cog.run/yaml>- Python predictor reference: <https://cog.run/python>
- Examples: <https://github.com/replicate/cog-examples>
- Template: <https://github.com/replicate/cog-template>
When to use this skill
- You have model code, weights, or a HuggingFace/GitHub project you want to host on Replicate.
- You're writing or editing a
cog.yaml,predict.py, ortrain.py. - For pushing a built model to Replicate, see
publish-models. - For running existing Replicate models, see
run-models.
Prerequisites
- Docker running locally.
- Cog installed:
brew install replicate/tap/cogorsh <(curl -fsSL https://cog.run/install.sh). - Optional:
cog initto scaffoldcog.yamlandpredict.py.
Project layout
The canonical Replicate model layout:
cog.yaml essentials
A modern config for a GPU model:
Notes:
- Pin Python to a specific minor version, and pin every line in
requirements.txt. Floating versions break cold boots. - Use
python_requirementsover inlinepython_packagesonce the list grows. cudafollows your torch wheel (e.g.12.8paired withtorch==2.7.1+cu128).- Add
train: train.py:trainif your model is fine-tunable. - Add
image: r8.im/owner/nameto enable barecog push.
For async predictors with continuous batching:
predict.py essentials
Input rules:
- Every input needs a
description. The description shows up in the model schema and on Replicate's web UI. - Use
ge/lefor numeric bounds,choices=[...]for enums,regex=for strings. - Use
cog.Pathfor file inputs and outputs, never raw bytes. - Use
cog.Secretfor any token-like input (HF tokens, API keys), never plainstr. - Provide a default that's inside
choicesfor categorical inputs. - Validate inputs early in
predict()and raiseValueError.
Streaming text output (for LLMs):
Async predictor with continuous batching (paired with concurrency.max in cog.yaml):
Dynamic choices from on-disk assets (e.g. a voices/ directory of audio samples):
Loading weights fast
Cold boot dominates user-perceived latency. Three patterns, ranked by simplicity:
1. Bake weights into the image at build time
Best for small or medium weights (< 5GB) that you want zero-cold-boot for.
For torchvision:
For HuggingFace:
Then download once during cog build (e.g. in a run: step or by running a small fetcher script as part of the build). The weights become part of the image layer.
2. Pull from weights.replicate.delivery with pget
Best for large weights, or when you want to share weights across multiple models. pget is Replicate's parallel HTTP fetcher.
In cog.yaml:
In setup():
For multiple files in one shot:
3. HuggingFace Hub with hf_transfer
Set HF_HUB_ENABLE_HF_TRANSFER=1 and use huggingface_hub.snapshot_download or from_pretrained. Faster than vanilla HF downloads. Use a cog.Secret input for gated models.
Weight cache for user-supplied weights
For LoRAs or any weights URL the user passes at predict time, use a sha256-keyed disk cache with LRU eviction:
See replicate/cog-flux/weights.py for a production version that handles HF, CivitAI, Replicate, and arbitrary .safetensors URLs.
Multi-LoRA composition
Reload only when the URL changes; compose two LoRAs with separate scales:
Cold-boot tricks
From production diffusion models like replicate/cog-flux and replicate/cog-flux-kontext:
- Set perf flags once in
setup():
``python import torch torch.set_float32_matmul_precision("high") torch.backends.cuda.matmul.allow_tf32 = True torch.backends.cudnn.benchmark = True ``
- Compile and warm up:
``python self.model = torch.compile(self.model, dynamic=True) _ = self.predict(prompt="warmup", num_steps=1) # absorbs compile cost in setup ``
- Load big weights with meta device +
assign=Trueto avoid double-allocating:
``python with torch.device("meta"): model = build_model_skeleton() state = torch.load("weights.pt", map_location="cpu") model.load_state_dict(state, assign=True) ``
- Share VAE / text encoder across multiple pipelines (e.g. base + img2img + inpaint) instead of loading three copies.
- For fp8/int8, save quantized weights ahead of time and load directly; don't quantize at boot.
Local development
Building
Tips:
- Use
--separate-weightsfor any model with weights > ~1GB. It speeds up cold boots and registry pushes. - Use
--mount=type=cache,target=/root/.cache/pipinrun:steps to cache pip across builds. - Use
--secretinstead ofARGto keep tokens out of image history. - The default Cog base image (
--use-cog-base-image=true) is faster than rolling your own.
Training
If your model supports fine-tuning, add train: train.py:train to cog.yaml and write a train() function that returns TrainingOutput(weights=Path("model.tar")). The predictor then accepts the URL via setup(self, weights) or the COG_WEIGHTS env var. See <https://cog.run/training> and replicate/flux-fine-tuner for a full example.
Guidelines
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/build-models/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/bertbertov/claude-stack/build-models)