/pytorch-opt-skills:pytorch-tweaksfrom pytorch-opt-skills
One-line PyTorch settings that speed up training or inference or cut GPU memory, each a flag, env var or context manager: TF32, autocast, cudnn.benchmark, PYTORCH_ALLOC_CONF allocator options, inference_mode, fill_uninitialized_memory. Use when reviewing a script for cheap wins or when one of these settings comes up.
Ships in pytorch-opt-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.
None: SKILL.md is the whole skill.
Most of these are off by default for a reason: they are experimental, or trade precision, reproducibility or debuggability for speed. Read the caveats, change one at a time, and time the step before and after. Each heading links the PyTorch 2.15 docs, checked 2026-12-02.
torch.backends.cuda.matmul.fp32_precision = "tf32"
torch.backends.cudnn.conv.fp32_precision = "tf32"
Runs FP32 matmuls and convolutions on tensor cores (Ampere or newer), with inputs rounded to 10 mantissa bits; tensors stay FP32. Matmuls default to full FP32; convolutions already allow TF32.
allow_tf32 flags, which are being deprecated.with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
output = model(input)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
Matmuls and convolutions run in bf16; softmax, norms, reductions and most losses stay in FP32 automatically. Usually a bigger win than TF32.
backward() goes outside.GradScaler (reasoning:
the docs say so only for CPU). Without dtype, CUDA uses fp16, which needs
torch.amp.GradScaler("cuda").torch.cuda.amp.* is deprecated. BCELoss raises; use BCEWithLogitsLoss.torch.backends.cudnn.benchmark = True
Times several convolution algorithms and keeps the fastest.
PYTORCH_ALLOC_CONF=expandable_segments:True,garbage_collection_threshold:0.8 python train.py
PYTORCH_CUDA_ALLOC_CONF is the older alias.
expandable_segments (experimental) grows one segment per stream instead
of one per allocation. For an OOM with large "reserved but unallocated"
memory, typically from shapes that vary between steps.garbage_collection_threshold (0–1, off at 1.0) frees cached blocks once
usage passes that fraction, before an allocation fails and forces a costly
sync-and-free-all. For jobs near the memory limit whose
torch.cuda.memory_stats()["num_alloc_retries"] keeps rising.with torch.inference_mode():
y = model(x)
Like no_grad, minus view tracking and version-counter bumps. Also a
decorator: @torch.inference_mode().
no_grad there.model.eval().torch.use_deterministic_algorithms(True)
torch.utils.deterministic.fill_uninitialized_memory = False
With deterministic algorithms on, torch.empty-style ops and resize_ fill
new memory (NaN for floats, max value for ints) by default, which costs time.
What SKILL.md sets; muted values are Claude Code's defaults
pytorch-opt-skills@mila-skills is listed by mila-iqia/skills — review the source before installing
npx claudepluginhub mila-iqia/skills --plugin pytorch-opt-skills5 skills in this plugin
/pytorch-compileDiagnose and fix torch.compile problems: graph breaks, recompilation, slow compile time, and compile-mode / runtime tradeoffs. Use when torch.compile is slow to compile, slower than eager, stalls for minutes on step 1 or on every new shape, or when you see graph break, "Recompiling function", guard failure, cache_size_limit / recompile_limit, dynamic shapes, mark_dynamic, tlparse, TORCH_TRACE, TORCH_LOGS=recompiles/graph_breaks, Dynamo, Inductor, Triton autotune, max-autotune, coordinate_descent_tuning, specialize_float, CUDA graphs / reduce-overhead, cudagraph_mark_step_begin, allow_in_graph, torch.compiler.disable, or "accessing tensor output of CUDAGraphs". Also for reusing compiled artifacts across jobs or nodes — megacache / Mega-Cache, save_cache_artifacts, load_cache_artifacts, cold vs warm compile, TORCHINDUCTOR_CACHE_DIR — for profiling a compiled model (kernels are fused and invisible to module hooks), for measurement hygiene when benchmarking compiled vs eager, and for the optimizer step outside the compiled model — fused=True, foreach, fused Adam / AdamW / SGD, compiling optimizer.step, LR scheduler recompiles./pytorch-dataloaderDiagnose and fix PyTorch input pipelines that starve the GPU: DataLoader wait, low or bursty GPU utilization, periodic or rare multi-second stalls, eval slower than train, slow time to first step, worker host-memory blowups. Use when tuning num_workers, prefetch_factor, pin_memory, non_blocking, persistent_workers or in_order; when next() wait may be a CUDA sync; when samples come from mmap, Arrow / HF datasets, LMDB, HDF5, .npy or shards on network storage (Lustre, BeeGFS, VAST/NFS; Mila and Alliance/DRAC clusters) or are staged to $SLURM_TMPDIR; when data outgrows page cache or local disk; for chunk shuffling or shuffle buffers; when scaling to more nodes or faster GPUs; or when one DDP rank waits on another's late batch.Other plugins' skills for the same job