TopNotchNote
Notes that matter
basics
pip install torch --index-url https://download.pytorch.org/whl/cu121| https://pytorch.org/get-started/locally/ | installs torch with a CUDA build matching your driver -- pick the cuXXX tag that matches `nvidia-smi`'s reported CUDA version, not necessarily the newest one |'pt_inst1'
python -c "import torch; print(torch.__version__, torch.cuda.is_available())"| | prints the torch version and whether CUDA actually initializes -- a mismatched driver/CUDA build silently falls back to CPU instead of erroring |'pt_inst2'
device & tensors
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')| https://pytorch.org/docs/stable/tensor_attributes.html#torch.device | standard device-selection idiom -- write code once, run on GPU when available and CPU otherwise |'pt_dev1'
model.to(device); batch = batch.to(device)| | both the model AND every tensor batch must be moved to the same device, or you get a "Expected all tensors to be on the same device" error |'pt_dev2'
datasets & dataloaders
from torch.utils.data import Dataset, DataLoader, ConcatDataset| https://pytorch.org/docs/stable/data.html | Dataset defines __len__/__getitem__ for one data source; DataLoader batches+shuffles it; ConcatDataset pools several Datasets (e.g. one per symbol/file) without mixing their internal indices |'pt_data1'
loader = DataLoader(dataset, batch_size=256, shuffle=True)| | shuffle=True for training, shuffle=False for validation/test so evaluation order stays reproducible |'pt_data2'
save & load
torch.save(model.state_dict(), 'model.pt')| https://pytorch.org/tutorials/beginner/saving_loading_models.html | save only the learned weights (state_dict), not the whole model object -- portable across code changes as long as the architecture matches |'pt_save1'
model.load_state_dict(torch.load('model.pt', map_location='cpu')); model.eval()| | map_location='cpu' lets you load a GPU-trained checkpoint on a machine with no GPU; call .eval() before inference to disable dropout/batchnorm training behavior |'pt_save2'
going further
For GPU-specific optimization — mixed precision, torch.compile, profiling, the caching allocator, multi-GPU — see PyTorch on GPU.
related topics
PyTorch on GPU — what changes once this code needs to run on a GPU.
Deep Learning & PyTorch Engineering — the deeper, conceptual track this cheat sheet is a quick-reference for.
ML Foundations — the ML concepts this PyTorch code is usually implementing.
GPU Libraries: BLAS, cuDNN/MIOpen, NCCL/RCCL & More — the libraries PyTorch calls into underneath.
Deep Learning & PyTorch Engineering — the deeper, conceptual track this cheat sheet is a quick-reference for.
ML Foundations — the ML concepts this PyTorch code is usually implementing.
GPU Libraries: BLAS, cuDNN/MIOpen, NCCL/RCCL & More — the libraries PyTorch calls into underneath.