/major-ai-skills:pytorchfrom major-ai-skills
Build reliable PyTorch training and inference loops: device placement, train/eval modes, AMP with torch.amp, safe torch.load (weights_only), checkpoints, and torch.compile caveats.
Ships in major-ai-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
PyTorch provides tensor compute + autograd, nn.Module for models, and DataLoader for batches. A training step is forward → loss → backward → optimizer; inference uses eval() and torch.inference_mode(). Modern stacks use torch.amp for mixed precision and must handle torch.load(..., weights_only=True) defaults (PyTorch 2.6+). Agents keep device placement consistent, checkpoint reproducibly (model + optimizer + scaler), and never confuse train-time Dropout/BN with eval behavior.
┌─────────────────────────────────────────────────────────────┐
│ PyTorch train / infer loop │
│ │
│ Dataset → DataLoader → batch.to(device) │
│ │ │
│ ▼ │
│ nn.Module on device │
│ ├── train(): Dropout/BN training behavior │
│ └── eval() + inference_mode(): serving / val │
│ │
│ AMP (optional): autocast + GradScaler("cuda") │
│ Checkpoint: state_dict + optimizer + scaler + epoch │
│ Optional: torch.compile (watch AMP backward semantics) │
└─────────────────────────────────────────────────────────────┘
Use when
Do not use when
@huggingface-transformers.@vllm after weights exist.@scikit-learn.model.train() for training; model.eval() for validation/inference—always.torch.inference_mode() (prefer over bare no_grad for speed/correctness intent).device; never leave labels on CPU by accident.state_dicts (model, optimizer, scaler, epoch, RNG if needed)—not pickled full modules from untrusted sources.torch.load: default weights_only=True since 2.6—use weights_only=False only for trusted full pickles; prefer pure state_dict checkpoints.torch.autocast + torch.amp.GradScaler("cuda") (not deprecated torch.cuda.amp.* in new code). Stabilize FP32 first.optimizer.zero_grad(set_to_none=True) unless a library requires otherwise.@mlflow when experiments matter.from __future__ import annotations
from pathlib import Path
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
use_amp = device.type == "cuda"
model = nn.Sequential(nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 10)).to(device)
opt = torch.optim.AdamW(model.parameters(), lr=3e-4)
criterion = nn.CrossEntropyLoss()
scaler = torch.amp.GradScaler("cuda", enabled=use_amp)
def train_one_epoch(loader: DataLoader) -> float:
model.train()
total = 0.0
for xb, yb in loader:
xb, yb = xb.to(device, non_blocking=True), yb.to(device, non_blocking=True)
opt.zero_grad(set_to_none=True)
with torch.autocast(device_type=device.type, dtype=torch.float16, enabled=use_amp):
logits = model(xb)
loss = criterion(logits, yb)
scaler.scale(loss).backward()
scaler.step(opt)
scaler.update()
total += float(loss.detach())
return total / max(len(loader), 1)
@torch.inference_mode()
def evaluate(loader: DataLoader) -> float:
model.eval()
correct = total = 0
for xb, yb in loader:
xb, yb = xb.to(device), yb.to(device)
pred = model(xb).argmax(dim=-1)
correct += int((pred == yb).sum())
total += yb.numel()
return correct / max(total, 1)
def save_ckpt(path: Path, epoch: int) -> None:
torch.save(
{
"epoch": epoch,
"model": model.state_dict(),
"optimizer": opt.state_dict(),
"scaler": scaler.state_dict(),
},
path,
)
def load_ckpt(path: Path) -> int:
# state_dict-only payload → weights_only=True is appropriate
ckpt = torch.load(path, map_location=device, weights_only=True)
model.load_state_dict(ckpt["model"])
opt.load_state_dict(ckpt["optimizer"])
if "scaler" in ckpt:
scaler.load_state_dict(ckpt["scaler"])
return int(ckpt["epoch"])
Optional compile (after FP32/AMP baseline works):
# If backward runs outside autocast (recommended AMP pattern), set:
# torch._functorch.config.backward_pass_autocast = "off"
# model = torch.compile(model)
| Signature | Likely cause | Fix path |
|---|---|---|
Expected all tensors on same device | Batch/model device skew | .to(device) both; watch non_blocking + pinned memory |
| Val/infer worse than train inexplicably | Forgot eval() | Disable Dropout/BN train behavior |
| Loss NaN / exploding | LR too high; bad inputs; FP16 overflow | Lower LR; check data; try BF16 or GradScaler path; verify AMP compatibility |
| VRAM grows each step | Holding graphs / storing tensors with grad | detach; don't append loss tensors to lists with graph; inference_mode on val |
weights_only UnpicklingError | Full checkpoint needs pickle | Trusted source only → weights_only=False, or resave as state_dicts |
AMP + torch.compile numeric weirdness | Autocast backward semantics | Set backward_pass_autocast per AMP+compile docs; compile after stable baseline |
| Non-deterministic eval | GPU algos / unordered reductions | Seeds help but not guarantee; document nondeterminism |
| Resume diverges | Missing optimizer/scaler state | Restore full training checkpoint bundle |
state_dict serialization; version code + data independently of weights.non_blocking=True with pinned host memory for CUDA input pipelines when profiling shows benefit.@huggingface-transformers; for serving LLMs, hand off to @vllm.@huggingface-transformers — pretrained NLP/vision stacks on PyTorch@mlflow — experiment tracking and model registry@vllm — production LLM inference engines@jupyter — exploratory loops that should still use eval / checkpoints@scikit-learn — non-deep baselines for comparisonMANDATORY: Match train/eval modes to the phase. Keep tensors and modules on one device. Checkpoint state_dicts including optimizer/scaler. Respect
weights_onlyload defaults; never load untrusted pickles. Introduce AMP andtorch.compileonly after a stable baseline. Do not invent credentials for data or Hub access.
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub alivirgo/major-ai-skills --plugin major-ai-skills412 skills in this plugin
/3d-slicerAutomate 3D Slicer research workflows with MRML, VTK/ITK, DICOM data, and Segment Editor. Use for software workflows, not clinical interpretation./abbreviated-git-statusInspect Git branch, staging, and working-tree changes with short or porcelain status output when a compact repository check is needed./ableton-liveAutomate Ableton Live sessions with Python MIDI Remote Scripts, Max for Live, AbletonOSC, and Link synchronization.Other plugins' skills for the same job
/act-as-a-personal-tutorExplain a subject step by step, assess prior knowledge, and use short exercises to check understanding before advancing.