pytorch
Installation
SKILL.md
PyTorch
Overview
PyTorch is a deep learning framework for building and training neural networks with dynamic computation graphs and automatic differentiation. It provides tensor operations with GPU acceleration, nn.Module for defining architectures, DataLoader for efficient data loading, mixed precision training for performance, and export tools (torch.export, ONNX) for production deployment.
Instructions
- Install with pip from the selector at pytorch.org/get-started/locally (it prints the right index URL for your CUDA version or CPU-only). PyTorch 2.9 and later need Python 3.10+.
- Pick the device once:
device = "cuda" if torch.cuda.is_available() else "cpu"(Apple Silicon:"mps"). - When defining models, subclass
nn.Modulewith__init__for layers andforwardfor computation, usingnn.Sequentialfor simple stacks. - When training, run the standard loop:
optimizer.zero_grad(), forward pass, loss,loss.backward(),clip_grad_norm_,optimizer.step(). Callmodel.train()before the loop andmodel.eval()for validation. - When loading data, subclass
Datasetwith__len__and__getitem__, then useDataLoaderwithnum_workers=4,pin_memory=Trueandpersistent_workers=Truefor GPU training. - When optimizing performance, use
torch.compile(model)(first call is slow while it compiles), mixed precision withtorch.autocast(device_type="cuda", dtype=torch.bfloat16), andDistributedDataParallellaunched withtorchrunfor multi-GPU. With float16 also usetorch.amp.GradScaler("cuda"); bfloat16 does not need it. - When doing transfer learning, load pretrained weights with the
weights=enum (ResNet50_Weights.DEFAULT; the oldpretrained=Truewas removed) or from Hugging Face, freeze the backbone, and replace the classifier head. - When saving, store
model.state_dict(); load withtorch.load(path, map_location=device, weights_only=True).weights_only=Trueis the default since 2.6 and rejects arbitrary pickled objects. - When deploying, use
torch.export.export(model, example_args)for a graph you can ship, andtorch.onnx.export(model, example_args, "model.onnx", dynamo=True)for ONNX (the dynamo exporter is the default since 2.9). TorchScript (torch.jit.trace/script) is deprecated; keep it only for legacy code. - For INT8 or lower-precision inference, use the
torchaopackage (torchao.quantization.quantize_).torch.ao.quantizationis being phased out in favour of it.