pytorch-patterns

Apply idiomatic PyTorch patterns for device-agnostic training and reproducible checkpoints.

Updated Sep 13, 2025
One-click install
npx skills add https://github.com/llmh333/employee_management_spring --skill pytorch-patterns-llmh333
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytorch-patterns
Source: https://github.com/llmh333/employee_management_spring/tree/main/.gemini/skills/pytorch-patterns
Command: npx skills add https://github.com/llmh333/employee_management_spring --skill pytorch-patterns-llmh333

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you avoid common PyTorch failures (non-reproducible results, device-specific bugs, shape mistakes, broken training/eval behavior, and fragile checkpointing) by standardizing correct patterns for real training pipelines.

Core Features & Use Cases

  • Device-agnostic development: Ensures your models and tensors move correctly across CPU/GPU without hardcoding .cuda() or device assumptions.
  • Reproducibility controls: Provides a complete seeding approach to make experiments repeatable.
  • Correct model & training structure: Guides clean nn.Module design, explicit tensor shape management, and safe train/eval switching.
  • Production-minded training loops: Covers mixed precision (AMP), gradient clipping, and efficient validation with torch.no_grad() and model.eval().
  • Efficient data pipelines & checkpointing: Shows custom Dataset/collate_fn patterns, optimized DataLoader settings, and resumable checkpoints with optimizer state.

Quick Start

Use the pytorch-patterns skill to review or rewrite a PyTorch training script so it uses device-agnostic code, reproducible seeding, correct train/eval modes, efficient data loading, and resumable checkpoints.

Frequently Asked Questions about pytorch-patterns

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

FAQPage Schema
How do I make PyTorch training reproducible across different runs?

Make PyTorch training reproducible by applying comprehensive seeding across random number generators. This Skill provides patterns for complete seeding controls to ensure your experiments are repeatable and eliminate non-deterministic behavior.

What is the best way to handle device-agnostic code in PyTorch for CPU and GPU?

Device-agnostic PyTorch code uses `.to(device)` mappings without hardcoding `.cuda()`. This Skill provides patterns to ensure models and tensors move correctly across CPU and GPU environments, preventing device-specific training bugs.

How do I implement safe mixed precision and gradient clipping in a PyTorch training loop?

Implement safe mixed precision and gradient clipping in a PyTorch training loop using automatic mixed precision (AMP) patterns. This Skill guides production-minded loops with efficient validation via `torch.no_grad()` and `model.eval()`.

Why does my PyTorch model produce different results during validation versus training?

PyTorch models produce different results when `model.train()` and `model.eval()` modes are handled incorrectly. This Skill ensures proper train/eval switching and explicit tensor shape management to maintain consistent behavior.

How do I save and resume PyTorch checkpoints with optimizer state?

Save and resume PyTorch checkpoints with optimizer state by applying resumable checkpointing patterns. This Skill covers safe checkpointing practices to preserve optimizer state and seamlessly resume training pipelines.

Can I optimize PyTorch DataLoader settings for custom datasets and collate functions?

Optimize PyTorch DataLoader settings by using custom `Dataset` and `collate_fn` patterns. This Skill provides efficient data pipeline patterns to prevent bottlenecks and improve data loading performance.