Agent skill
pytorch-core
Core PyTorch fundamentals including tensor operations, autograd, nn.Module architecture, and training loop orchestration. Covers optimizations like pin_memory and lazy module initialization. (pytorch, tensor, autograd, nn.Module, optimizer, training loop, state_dict, pin_memory, lazylinear, requires_grad)
Install this agent skill to your Project
npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/pytorch-core
SKILL.md
Overview
Core PyTorch provides the fundamental building blocks for deep learning, focusing on tensor computation with strong GPU acceleration and a deep-learning-oriented autograd system. It emphasizes a "define-by-run" approach where models are standard Python objects.
When to Use
Use PyTorch Core when you need granular control over model architecture, custom training loops, or specific hardware optimizations like pinned memory for data transfers.
Decision Tree
- Do you know the input dimensions of your data?
- YES: Use standard layers (e.g.,
nn.Linear). - NO: Use Lazy modules (e.g.,
nn.LazyLinear) to defer initialization.
- YES: Use standard layers (e.g.,
- Is your bottleneck data transfer to the GPU?
- YES: Enable
pin_memory=Truein yourDataLoader. - NO: Standard data loading suffices.
- YES: Enable
- Are you fine-tuning a model?
- YES: Set
requires_grad=Falsefor frozen parameters. - NO: Keep
requires_grad=Truefor full training.
- YES: Set
Workflows
-
Standard Training Iteration
- Load a batch of data from the
DataLoader. - Zero the gradients using
optimizer.zero_grad(). - Perform a forward pass through the
nn.Module. - Compute the loss using a criterion (e.g.,
nn.CrossEntropyLoss). - Execute a backward pass with
loss.backward()to compute gradients. - Update model parameters using
optimizer.step().
- Load a batch of data from the
-
Model Persistence and Checkpointing
- Capture the state of the model and optimizer using
.state_dict(). - Save the dictionaries to a file using
torch.save(). - Restore the model by instantiating the class and calling
.load_state_dict(). - Ensure
.eval()is called before inference to handle Dropout and BatchNorm correctly.
- Capture the state of the model and optimizer using
-
Deferred Architecture Initialization
- Define the model using Lazy modules (e.g.,
nn.LazyLinear). - Initialize the model on the desired device.
- Run a dummy input or the first real batch through the model.
- PyTorch automatically infers and sets the weight shapes based on the input.
- Define the model using Lazy modules (e.g.,
Non-Obvious Insights
- Lazy Initialization: Using
LazyLinearorLazyConv2dsimplifies architecture definitions where input dimensions are unknown, preventing manual shape calculation errors. - Data Transfer Optimization: Using
pin_memory()in DataLoaders is a critical optimization for faster data transfer between CPU and GPU. - Dynamic Gradient Control: The
requires_gradattribute can be toggled on-the-fly to freeze parameters during fine-tuning or transfer learning without re-instantiating the model.
Evidence
- "Most machine learning workflows involve working with data, creating models, optimizing model parameters, and saving the trained models." (https://pytorch.org/tutorials/beginner/basics/intro.html)
- "Lazy modules like LazyLinear allow for deferred initialization of input dimensions until the first forward pass." (https://pytorch.org/docs/stable/nn.html)
Scripts
scripts/pytorch-core_tool.py: Provides a standard training loop skeleton and lazy initialization examples.scripts/pytorch-core_tool.js: Node.js wrapper for invoking PyTorch training scripts.
Dependencies
- torch
- torchvision (optional for datasets)
- numpy
References
- PyTorch Core 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?