323-pytorch

Optimize PyTorch training and inference with AMP, torch.compile, and safe checkpointing.

Updated May 21, 2026
One-click install
npx skills add https://github.com/ulf1/trading-regime --skill 323-pytorch
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: 323-pytorch
Source: https://github.com/ulf1/trading-regime/tree/main/.agent/skills/323-pytorch
Command: npx skills add https://github.com/ulf1/trading-regime --skill 323-pytorch

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes assets (resource) and references (resource) components.

What problem does it solve?

This Skill prevents slow, brittle, and error-prone PyTorch training/inference implementations by enforcing production-grade patterns for device handling, mixed precision, data loading, and safe checkpointing.

Core Features & Use Cases

  • High-performance training loops: Apply AMP via torch.amp.autocast/GradScaler, correct gradient handling, and fast optimizer patterns like zero_grad(set_to_none=True).
  • Reliable model architecture & deployment hygiene: Keep forward() pure, register parameters/buffers correctly, document tensor shapes, and save/load state_dict safely with map_location.
  • Data pipeline and memory stability: Use DataLoader best practices (pin_memory, num_workers, persistent_workers), avoid hidden-state graph growth (detach), and profile before optimizing.

Use case example: You’re training a neural model for daily retraining and hit GPU OOMs and inconsistent performance—use this Skill’s checklist to fix device transfers, enable AMP correctly, stabilize the training loop, and validate that checkpoints load cleanly.

Quick Start

Use the 323-pytorch skill to optimize and debug your PyTorch training code for correctness, AMP compatibility, and performance while ensuring safe checkpointing.

Frequently Asked Questions about 323-pytorch

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
What's the best way to configure PyTorch DataLoader for high performance?

Optimize PyTorch DataLoader by setting pin_memory=True for CUDA transfers, adjusting num_workers for parallel data loading, and enabling persistent_workers to avoid reinitializing worker processes. Profile data loading to identify pipeline bottlenecks before modifying parameters.

Why do my PyTorch checkpoints fail to load across different devices?

PyTorch checkpoint loading fails across devices when map_location is not specified in torch.load. Safe state_dict checkpointing requires setting map_location to correctly remap storage tensors to the target device, whether CPU or CUDA.

How do I use torch.compile to speed up PyTorch training loops?

Apply torch.compile to your PyTorch model before the training loop to optimize execution graphs and reduce kernel overhead. Ensure your forward function remains pure with no hidden state side effects for the compilation to succeed.

Does zero_grad(set_to_none=True) improve PyTorch memory performance?

Using zero_grad(set_to_none=True) in PyTorch improves memory performance by setting gradients to None instead of zeroing them, allowing more efficient memory allocation during backpropagation. This avoids unnecessary memory writes and reduces optimizer overhead.

How to prevent GPU out of memory errors during PyTorch model retraining?

Prevent GPU OOM errors during PyTorch retraining by minimizing host-device transfers, enabling automatic mixed precision, detaching hidden states to avoid graph growth, and ensuring explicit device placement for tensors and models before execution.