debug-pytorch_skill

This skill helps you diagnose and fix PyTorch errors efficiently by guiding memory, shape, device, and grad issues across models.

6

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill snakeo/claude-debug-and-refactor-skills-plugin --skill debug-pytorch

  • SKILL.md20.1 KB

Overview

This skill helps you debug PyTorch issues systematically across training and inference. It targets common failures like CUDA OOM, shape mismatches, NaNs in loss/gradients, device or dtype conflicts, DataLoader worker problems, and autograd graph errors. It gives actionable checks, fixes, and tools to reproduce, isolate, and resolve problems quickly.

How this skill works

The skill walks you through four debugging phases: reproduce and isolate, validate data pipeline, validate model architecture, and inspect training dynamics. It provides concrete code snippets and diagnostics for memory profiling, anomaly detection, gradient inspection, device/dtype consistency, and DataLoader debugging. It also recommends targeted fixes such as gradient clipping, mixed precision, checkpointing, and tensor/device/dtype hygiene.

When to use it

  • When you hit CUDA out of memory or unexplained GPU memory growth
  • When encountering shape/size mismatch errors between layers or tensors
  • When loss or gradients become NaN or infinite during training
  • When runtime errors complain about CPU/GPU or dtype mismatches
  • When DataLoader workers crash or you see broken pipe/fork errors
  • When autograd errors mention in-place ops, missing grad_fn, or retain_graph issues

Best practices

  • Reproduce failures with a minimal deterministic example and fixed random seeds
  • Print or assert tensor shapes and devices at key model stages to catch mismatches early
  • Use torch.autograd.set_detect_anomaly and check gradients after backward to locate NaNs/infs
  • Prefer stable numerics: add eps where needed, use logits for CrossEntropyLoss, and clip gradients
  • Manage GPU memory: reduce batch size, empty cache, use AMP/mixed precision and checkpointing
  • Run DataLoader with num_workers=0 when debugging and set proper start method for CUDA in multiprocessing

Example use cases

  • Resolve a CUDA OOM by reducing batch size, enabling torch.cuda.empty_cache, and switching to autocast with GradScaler
  • Track down a shape mismatch by printing shapes in the model forward or using einops/rearrange for explicit reshaping
  • Find the source of NaN loss using anomaly detection, gradient checks, small lr, eps safeguards, and gradient clipping
  • Fix device/dtype errors by moving model and inputs to the same device, using map_location when loading checkpoints, and casting tensors to the correct dtype
  • Debug DataLoader crashes by lowering num_workers, using persistent_workers, and running with spawn start method in multiprocessing

FAQ

Print next(model.parameters()).device for model and tensor.device for inputs/targets; also use torch.cuda.memory_summary() for GPU usage.

When should I enable anomaly detection?

Enable it when you see NaNs or strange backward errors to get stack traces of the offending operation; disable in production because it is slow and memory intensive.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
debug-pytorch skill by snakeo/claude-debug-and-refactor-skills-plugin | VeilStrat