Agent skill
torchserve
Model serving engine for PyTorch. Focuses on MAR packaging, custom handlers for preprocessing/inference, and management of multi-GPU worker scaling. (torchserve, mar-file, handler, basehandler, model-archiver, inference-api)
Install this agent skill to your Project
npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/torchserve
SKILL.md
Overview
TorchServe is a flexible and easy-to-use tool for serving PyTorch models. It provides capabilities for packaging models, scaling workers based on hardware availability, and managing multiple model versions via a REST/gRPC API.
When to Use
Use TorchServe when you need a production-ready inference server that handles multi-GPU load balancing, request batching, and custom preprocessing/postprocessing logic via Python handlers.
Decision Tree
- Do you need custom logic for image resizing or JSON parsing before model inference?
- OVERRIDE:
preprocess()in a class inheriting fromBaseHandler.
- OVERRIDE:
- Do you have multiple GPUs available?
- RELY: On TorchServe's round-robin assignment; check the
gpu_idin the handler context.
- RELY: On TorchServe's round-robin assignment; check the
- Do you want to deploy to a system with limited resources?
- CAUTION: TorchServe is in limited maintenance; check environment compatibility.
Workflows
-
Packaging and Serving a Model
- Write a custom handler or use a default one (e.g., 'image_classifier').
- Use
torch-model-archiverto package the model, weights, and handler into a.marfile. - Start TorchServe specifying the model store and the initial models to load.
- Test the endpoint using
curlor a gRPC client.
-
Customizing Inference Logic
- Define a class inheriting from
BaseHandler. - Override
preprocess()to handle incoming JSON/Image data. - Override
inference()orpostprocess()to customize output formatting. - Package this script as the
--handlerin the model archiver.
- Define a class inheriting from
-
Scaling Inference Capacity
- Use the Management API (typically on port 8081) to adjust the number of workers.
- Send a
PUTrequest to/models/{model_name}?min_worker=N. - Monitor logs to ensure new workers are successfully initialized on the available hardware.
Non-Obvious Insights
- A/B Testing: TorchServe naturally supports multiple model versions simultaneously, making it trivial to perform A/B testing by routing requests to different model endpoints.
- GPU Round-Robin: Workers are assigned GPUs in a round-robin fashion. Handlers must use the
gpu_idprovided in thecontextto ensure the model is loaded onto the correct physical device. - The MAR Format: The Model Archive (
.mar) file is a self-contained ZIP that includes the model definition, state dictionary, and the handler script, ensuring that the deployment environment exactly matches the development environment.
Evidence
- "Archive the model by using the model archiver: torch-model-archiver --model-name densenet161 --version 1.0..." (https://pytorch.org/serve/getting_started.html)
- "In case of multiple GPUs TorchServe selects the gpu device in round-robin fashion and passes on this device id to the model handler in context." (https://pytorch.org/serve/custom_service.html)
Scripts
scripts/torchserve_tool.py: Skeleton for a custom TorchServe handler.scripts/torchserve_tool.js: Script to send inference requests to a running TorchServe instance.
Dependencies
- torchserve
- torch-model-archiver
References
- TorchServe Reference
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
prompt-engineering
Comprehensive prompt engineering techniques for Claude models. Use this skill when crafting, optimizing, or debugging prompts for Claude API, Claude Code, or any Claude-powered application. Covers system prompts, role prompting, multishot examples, chain of thought, XML structuring, long context handling, extended thinking, prompt chaining, Claude 4.x-specific best practices, and agentic orchestration including subagents, agent loops, skills, MCP integration, and multi-agent workflows.
adk-rag-agent
Build RAG (Retrieval-Augmented Generation) agents with Google ADK and Vertex AI RAG Engine. Use when implementing document Q&A, knowledge base search, or citation-backed responses. Covers VertexAiRagRetrieval tool, corpus setup, and citation formatting.
headless-cli-agents
Build agentic systems using Claude CLI in headless mode or the Claude Agent SDK. Use when building automation pipelines, CI/CD integrations, multi-agent orchestration, or programmatic Claude interactions. Covers CLI flags (-p, --output-format), session management (--resume, --continue), Python SDK (claude-agent-sdk), custom tools, and agent loop patterns.
notion-knowledge-capture
Capture conversations and decisions into structured Notion pages; use when turning chats/notes into wiki entries, how-tos, decisions, or FAQs with proper linking.
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
gh-fix-ci
Inspect GitHub PR checks with gh, pull failing GitHub Actions logs, summarize failure context, then create a fix plan and implement after user approval. Use when a user asks to debug or fix failing PR CI/CD checks on GitHub Actions and wants a plan + code changes; for external checks (e.g., Buildkite), only report the details URL and mark them out of scope.
Didn't find tool you were looking for?