pytorch-patterns

Implements PyTorch patterns for training loops, data pipelines, checkpointing, and performance optimization.

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/Femad-6/my-skills --skill pytorch-patterns-femad-6
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytorch-patterns
Source: https://github.com/Femad-6/my-skills/tree/main/.github/skills/pytorch-patterns
Command: npx skills add https://github.com/Femad-6/my-skills --skill pytorch-patterns-femad-6

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing PyTorch code that is device-agnostic, reproducible, and memory-efficient requires knowing many idioms and avoiding subtle bugs like forgetting model.eval(), breaking autograd with in-place operations, or misconfiguring DataLoaders. This Skill provides vetted patterns and anti-patterns so deep learning code is correct and performant from the start. ## Core Features & Use Cases - Training and Evaluation Loops: Complete patterns for train_one_epoch and evaluate functions with mixed precision, gradient clipping, and proper train/eval mode handling. - Data Pipeline Patterns: Custom Dataset classes, optimized DataLoader configuration with pin_memory and persistent_workers, and collate functions for variable-length sequences. - Checkpointing and Optimization: Full checkpoint save/load with optimizer state, gradient checkpointing for large models, and torch.compile for faster execution. - Use Case: When writing a new image classifier training script, apply these patterns to get a device-agnostic, reproducible pipeline with AMP mixed precision and resumable checkpoints instead of debugging shape mismatches and GPU memory errors. ## Quick Start Ask the assistant to write a PyTorch training loop for an image classifier following best practices with mixed precision and checkpointing.

Frequently Asked Questions about pytorch-patterns

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

FAQPage Schema
How do I write a PyTorch training loop with mixed precision?

Use torch.amp.GradScaler with torch.amp.autocast around the forward pass, then call scaler.scale(loss).backward(), scaler.step(optimizer), and scaler.update(). Add gradient clipping with torch.nn.utils.clip_grad_norm_ after unscaling for stable training.

How to make PyTorch experiments reproducible?

Set seeds for torch, torch.cuda, numpy, and random, then set torch.backends.cudnn.deterministic to True and benchmark to False. This ensures identical results across runs at some cost to training speed.

Why does my PyTorch validation give inconsistent results?

The most common cause is forgetting to call model.eval() before validation, which leaves dropout active and BatchNorm using batch statistics. Always call model.eval() and wrap inference in torch.no_grad() or use the @torch.no_grad() decorator.

How do I save and resume PyTorch training checkpoints?

Save a dictionary containing the epoch, model state_dict, optimizer state_dict, and loss with torch.save. Load it with map_location="cpu" and weights_only=True, then restore both model and optimizer states to resume training exactly where it stopped.

How can I reduce GPU memory usage in PyTorch training?

Use gradient checkpointing via torch.utils.checkpoint to recompute activations during backward instead of storing them, enable mixed precision with autocast, and clear gradients with optimizer.zero_grad(set_to_none=True). Profile usage with torch.cuda.memory_summary().

What DataLoader settings speed up PyTorch data loading?

Set num_workers to 4 or more for parallel loading, pin_memory=True for faster CPU-to-GPU transfer, persistent_workers=True to keep workers alive between epochs, and drop_last=True for consistent batch sizes with BatchNorm.