{"id":211,"library":"torch","title":"PyTorch","description":"Deep learning framework with GPU-accelerated tensor operations. Current version is 2.10.0 (Jan 2026). Install command varies by CUDA version — plain pip install torch gives CPU-only build. torch.load weights_only default changed to True in 2.6, breaking thousands of existing checkpoints. TorchScript deprecated in 2.10.","status":"active","version":"2.10.0","language":"python","source_language":"en","source_url":"https://pytorch.org/blog/pytorch-2-6/","tags":["deep-learning","ml","gpu","cuda","neural-networks","autograd"],"install":[{"cmd":"pip install torch","lang":"bash","label":"CPU-only (default PyPI build)"},{"cmd":"pip install torch --index-url https://download.pytorch.org/whl/cu128","lang":"bash","label":"CUDA 12.8 (recommended for NVIDIA GPUs)"},{"cmd":"pip install torch --index-url https://download.pytorch.org/whl/cu124","lang":"bash","label":"CUDA 12.4"},{"cmd":"pip install torch --index-url https://download.pytorch.org/whl/rocm6.2","lang":"bash","label":"AMD ROCm 6.2"}],"dependencies":[{"reason":"Vision models and transforms. Must match torch version: torch 2.10 → torchvision 0.25. Install with same --index-url.","package":"torchvision","optional":true},{"reason":"Audio processing. Must match torch version. Install with same --index-url.","package":"torchaudio","optional":true},{"reason":"Required for torch.compile on Linux CUDA. Installed automatically on Linux.","package":"triton","optional":true}],"imports":[{"note":"torch.load weights_only parameter flipped from False to True default in 2.6. All existing torch.load() calls without explicit weights_only= raise UnpicklingError if the checkpoint contains non-tensor objects.","wrong":"model.load_state_dict(torch.load('model.pt'))  # raises UnpicklingError in 2.6+ — weights_only now defaults to True","symbol":"torch.load","correct":"# For trusted checkpoints (your own models):\nmodel.load_state_dict(torch.load('model.pt', weights_only=True))\n\n# For checkpoints with non-tensor objects (optimizer states, custom classes):\ncheckpoint = torch.load('checkpoint.pt', weights_only=False)  # only for trusted files"},{"note":"Always use .to(device) with a device variable rather than hardcoding .cuda(). .cuda() raises RuntimeError on machines without CUDA.","wrong":"model = MyModel().cuda()  # crashes on CPU-only machines with no CUDA\ntensor = tensor.cuda()","symbol":"device","correct":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel = MyModel().to(device)\ntensor = tensor.to(device)"}],"quickstart":{"code":"import torch\nimport torch.nn as nn\n\n# Device setup\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Simple model\nmodel = nn.Sequential(\n    nn.Linear(10, 64),\n    nn.ReLU(),\n    nn.Linear(64, 1)\n).to(device)\n\n# Training step\noptimizer = torch.optim.Adam(model.parameters(), lr=1e-3)\nloss_fn = nn.MSELoss()\n\nmodel.train()\nfor x, y in dataloader:\n    x, y = x.to(device), y.to(device)\n    optimizer.zero_grad()\n    loss = loss_fn(model(x), y)\n    loss.backward()\n    optimizer.step()\n\n# Inference\nmodel.eval()\nwith torch.no_grad():\n    predictions = model(test_x.to(device))\n\n# Save / load\ntorch.save(model.state_dict(), 'model.pt')\nmodel.load_state_dict(torch.load('model.pt', weights_only=True))","lang":"python","description":"Standard training loop and inference pattern. Always use model.eval() + torch.no_grad() for inference."},"warnings":[{"fix":"For state_dict-only checkpoints: torch.load(path, weights_only=True). For full checkpoints with optimizer etc: torch.load(path, weights_only=False) — only on trusted files. To allowlist specific types: torch.serialization.add_safe_globals([MyClass]).","message":"torch.load() weights_only default changed from False to True in PyTorch 2.6. All existing torch.load() calls without explicit weights_only= will raise UnpicklingError if the checkpoint contains optimizer states, custom classes, or numpy arrays. Broke thousands of projects.","severity":"breaking","affected_versions":">= 2.6"},{"fix":"Use the PyTorch install selector: https://pytorch.org/get-started/locally/. For CUDA 12.8: pip install torch --index-url https://download.pytorch.org/whl/cu128","message":"Plain pip install torch installs CPU-only build. CUDA builds require a custom --index-url. LLM-generated install instructions almost never include this. torch.cuda.is_available() returns False after CPU-only install.","severity":"breaking","affected_versions":"all"},{"fix":"Install all PyTorch ecosystem packages together with the same --index-url: pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128","message":"torchvision, torchaudio version must exactly match torch version. Installing latest torch with mismatched torchvision versions causes ImportError or silent incorrect behavior.","severity":"breaking","affected_versions":"all"},{"fix":"Migrate to torch.export.export() for export/deployment. torch.jit still works but will receive no new features and will eventually be removed.","message":"TorchScript (torch.jit.script, torch.jit.trace) deprecated in PyTorch 2.10. The PyTorch team recommends migrating to torch.export for model deployment.","severity":"deprecated","affected_versions":">= 2.10"},{"fix":"Always call model.eval() before inference. Pair with torch.no_grad() to disable gradient computation: with torch.no_grad(): output = model(x)","message":"Forgetting model.eval() during inference causes BatchNorm and Dropout layers to behave as if training — different results each run and incorrect predictions.","severity":"gotcha","affected_versions":"all"},{"fix":"Call optimizer.zero_grad() at the start of each training step, before the forward pass. Or use optimizer.zero_grad(set_to_none=True) for slightly better memory performance.","message":"optimizer.zero_grad() must be called before loss.backward() each step. Forgetting it accumulates gradients across batches — silent training bug.","severity":"gotcha","affected_versions":"all"},{"fix":"Move all tensors to the same device: x, y = x.to(device), y.to(device) at the start of each training step.","message":"Tensors on different devices cannot be combined. CPU tensor + CUDA tensor raises RuntimeError. Common when target labels stay on CPU while model outputs are on CUDA.","severity":"gotcha","affected_versions":"all"},{"fix":"Verify your Python version, OS, and architecture are officially supported by PyTorch. Consult the PyTorch install selector (https://pytorch.org/get-started/locally/) to find the correct installation command, which might involve using a specific `--index-url`, a different Python environment, or a different base image if using Docker.","message":"ERROR: No matching distribution found for torch often indicates that PyTorch wheels are not available for your specific Python version, operating system, or architecture (e.g., very new Python versions, Alpine Linux, or unusual hardware).","severity":"breaking","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-12T10:38:18.352Z","next_check":"2026-06-25T00:00:00.000Z","problems":[{"fix":"Install the correct CUDA-enabled version of PyTorch by following the instructions on the official PyTorch website (e.g., `pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118` for CUDA 11.8).","cause":"The default `pip install torch` command installs the CPU-only version of PyTorch, even if a CUDA-enabled GPU is present.","error":"torch.cuda.is_available() returns False"},{"fix":"Reduce the batch size, decrease model complexity, free up unused tensors explicitly using `del` and `torch.cuda.empty_cache()`, or switch to a GPU with more memory.","cause":"The model, batch size, or intermediate tensors require more GPU memory than is available on the device.","error":"RuntimeError: CUDA out of memory. Tried to allocate X MiB (GPU Y; Z GiB total capacity; W GiB already allocated; V MiB free; U GiB reserved in total by PyTorch)"},{"fix":"When loading, explicitly map the tensors to the CPU using `torch.load(path, map_location='cpu')`.","cause":"You are trying to load a PyTorch model or tensor that was saved while on a CUDA-enabled device, into an environment where CUDA is not available or detected.","error":"RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load(..., map_location='cpu') to load the tensor to CPU."},{"fix":"Refactor the TorchScripted part of your code to only use supported types (tensors, lists/tuples of tensors, primitive types) as inputs and intermediate values, or consider using `torch.export` for newer PyTorch versions.","cause":"TorchScript (used by `torch.jit.script` or `torch.jit.trace`) has limitations on the types of inputs and operations it can handle, and complex types like dictionaries are often not directly supported as inputs or intermediate values in a traced graph.","error":"TypeError: Only tensors, lists, tuples of tensors, or ints, floats, bools, None are valid inputs for JIT functions. Got a <class 'dict'> at position 1."}],"ecosystem":"pypi","meta_description":null,"install_score":0,"install_tag":"stale","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"4.6G"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"5.1G"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"4.6G"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"5.0G"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"4.6G"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"5.0G"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"6.7G"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"4.6G"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"5.0G"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"6.7G"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"6.4G"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"5.0G"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":-1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":-1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":-1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":-1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":-1}]}}