pytorch-patterns

Standardize device handling, seeding, and checkpointing in PyTorch training workflows.

2|Updated Apr 15, 2026
One-click install
npx skills add https://github.com/mdnaimul22/human-skills --skill pytorch-patterns-mdnaimul22
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pytorch-patterns
Source: https://github.com/mdnaimul22/human-skills/tree/main/skills/pytorch-patterns
Command: npx skills add https://github.com/mdnaimul22/human-skills --skill pytorch-patterns-mdnaimul22

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill solves the problem of brittle, non-reproducible PyTorch code by standardizing device handling, training/validation correctness, data loading efficiency, and checkpointing practices.

Core Features & Use Cases

  • Device-agnostic execution: Write code that runs on CPU or GPU without hardcoded .cuda() calls, improving portability and reducing runtime failures.
  • Reproducible experiments: Apply deterministic seeding across PyTorch, CUDA, NumPy, and Python to make results consistent run-to-run.
  • Correct training and evaluation loops: Enforce model.train() vs model.eval(), use torch.no_grad() for validation, and prevent common autograd mistakes like calling .item() before backprop.
  • Efficient data pipelines: Configure DataLoader for performance (e.g., pin_memory, persistent_workers) and support variable-length inputs with collate_fn.
  • Safe checkpoints and performance optimization: Save full training state for resumability and use AMP, gradient clipping, torch.compile, and checkpointing for memory/throughput gains.

Quick Start

Implement device-agnostic models, a reproducible seeding function, and a paired train/validation loop for your next PyTorch project to immediately avoid the most common reliability pitfalls.

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 CPU and GPU?

Make PyTorch training reproducible by applying deterministic seeding across PyTorch, CUDA, NumPy, and Python, and by using device-agnostic `.to(device)` calls instead of hardcoded `.cuda()` to ensure consistent results run-to-run.

What is the correct way to structure PyTorch training and validation loops?

Correct PyTorch training and validation loops require enforcing `model.train()` versus `model.eval()`, using `torch.no_grad()` for validation, and avoiding autograd mistakes like calling `.item()` before backprop to ensure reliable gradient computation.

How do I configure PyTorch DataLoader for optimal performance?

Configure PyTorch DataLoader for performance by enabling settings like `pin_memory` and `persistent_workers`, and support variable-length inputs using custom `collate_fn` to build efficient data pipelines.

How to save and resume PyTorch training from checkpoints?

Save and resume PyTorch training by saving the full training state in checkpoints, enabling restartable workflows for deep learning projects and ensuring resumability without losing progress.

Does this PyTorch skill include mixed precision and memory optimization?

Yes, this PyTorch skill includes mixed precision (AMP), gradient clipping, `torch.compile`, and checkpointing techniques to achieve memory and throughput gains during model training.

Why does my PyTorch model fail when moving between CPU and GPU?

PyTorch models fail when moving between CPU and GPU due to hardcoded `.cuda()` calls; using device-agnostic `.to(device)` execution improves portability and reduces runtime failures.