Agent skill
pytorch-onnx
Exporting PyTorch models to ONNX format for cross-platform deployment. Includes handling dynamic axes, graph optimization in ONNX Runtime, and INT8 model quantization. (onnx, onnxruntime, torch.onnx.export, dynamic_axes, constant-folding, edge-deployment)
Install this agent skill to your Project
npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/pytorch-onnx
SKILL.md
Overview
ONNX (Open Neural Network Exchange) is an open format built to represent machine learning models. Exporting PyTorch models to ONNX allows them to be executed in environments without Python or PyTorch, using high-performance engines like ONNX Runtime.
When to Use
Use ONNX for cross-language deployment (C++, Java, C#), edge deployment (mobile/IoT), or to leverage specialized hardware accelerators (like TensorRT) that support ONNX as an input format.
Decision Tree
- Does your model accept variable batch sizes?
- SPECIFY:
dynamic_axesin thetorch.onnx.exportcall.
- SPECIFY:
- Do you need the fastest possible inference on a CPU?
- APPLY: Quantization using the ONNX Runtime quantization tool.
- Are you deploying to a C++ environment without Python?
- EXPORT: To ONNX and load using the ONNX Runtime C++ API.
Workflows
-
Exporting a Model for Cross-Platform Deployment
- Instantiate the PyTorch model and set it to
.eval(). - Create a dummy input tensor matching the input shape.
- Call
torch.onnx.export()specifying input/output names and dynamic axes. - Verify the resulting
.onnxfile using a tool like Netron.
- Instantiate the PyTorch model and set it to
-
Optimizing ONNX Models for Inference
- Load the
.onnxmodel into an ONNX RuntimeInferenceSession. - Choose an appropriate Execution Provider (e.g.,
'CUDAExecutionProvider','TensorrtExecutionProvider'). - Enable graph optimizations like constant folding and node fusion.
- Run inference using the
session.run()method with input dictionaries.
- Load the
-
Reducing Model Footprint via Quantization
- Export the model to standard ONNX format.
- Use the ONNX Runtime quantization tool to convert FP32 weights to INT8.
- Calibrate the model using a representative dataset to minimize accuracy loss.
- Deploy the quantized
.onnxmodel to edge devices for lower latency.
Non-Obvious Insights
- Static vs. Dynamic: By default,
torch.onnx.exportcaptures the shape of the dummy input as a static shape. If your application handles varying inputs, you must explicitly define these as dynamic axes. - Graph Optimization: ONNX Runtime performs "constant folding," which pre-computes parts of the graph that rely on constant values, effectively stripping unnecessary computation before inference starts.
- Serialization Choice: While TorchScript is also an option for PyTorch deployment, ONNX is often preferred for cross-vendor compatibility (e.g., running a model on a Web browser using ONNX.js).
Evidence
- "The first step is to export your PyTorch model to ONNX format using the PyTorch ONNX exporter: torch.onnx.export(model, PATH, example)." (https://onnxruntime.ai/docs/tutorials/accelerate-pytorch/pytorch.html)
- "ONNXRuntime applies a series of optimizations to the ONNX graph, combining nodes where possible and factoring out constant values (constant folding)." (https://onnxruntime.ai/docs/tutorials/accelerate-pytorch/pytorch.html)
Scripts
scripts/pytorch-onnx_tool.py: Script to export a model with dynamic axes support.scripts/pytorch-onnx_tool.js: Node.js interface to run inference via ONNX Runtime.
Dependencies
- torch
- onnx
- onnxruntime
References
- PyTorch ONNX 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?