पाठ 2 / 33

Installing PyTorch and choosing a device

यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।

Install PyTorch in a virtual environment, verify the install, and write code that picks CUDA, Apple MPS or CPU automatically.

लेखक: · 15 साल से वेब डेवलपर

What an install actually needs

The official PyTorch website has an install selector: you choose your OS, package manager (pip or conda) and compute platform (CPU, a CUDA version, or ROCm for AMD), and it prints the exact command. Use it rather than guessing, because the right wheel depends on your hardware.

For NVIDIA GPUs the pip wheels bundle the CUDA runtime libraries they need. You do not have to install the full CUDA toolkit; you need an NVIDIA driver recent enough for the CUDA version of the wheel. On Apple silicon Macs, the standard install includes the MPS backend, which runs tensors on the Mac's GPU through Metal. Without any GPU, PyTorch runs on the CPU, which is fine for learning and small models.

Install into a virtual environment so projects do not fight over versions. If you have no GPU at all, free notebook services such as Google Colab and Kaggle come with PyTorch preinstalled and offer limited GPU time.

Create an environment and verify

The last command prints the installed version and whether a CUDA GPU is usable. False on a machine with an NVIDIA card usually means a CPU-only wheel or an old driver.

python -m venv .venv
source .venv/bin/activate          # Windows: .venv\Scripts\activate
pip install torch torchvision
python -c "import torch; print(torch.__version__, torch.cuda.is_available())"

Pick the best available device

Write this once at the top of a script. Everything later uses device, so the same code runs on a laptop, a Mac and a GPU server.

import torch

if torch.cuda.is_available():
    device = torch.device("cuda")
elif torch.backends.mps.is_available():
    device = torch.device("mps")
else:
    device = torch.device("cpu")
print("Using", device)

When cuda.is_available() says False

Check three things in order: you installed a CUDA build (the selector command, not a plain CPU wheel), nvidia-smi works and shows your GPU, and the driver is new enough for that CUDA version. Do not pip install CUDA packages separately to "fix" it. On Colab, switch the runtime type to a GPU first.

त्वरित जाँच: You install a CUDA-enabled PyTorch wheel with pip on Linux. What must already be on the machine for the GPU to work?

  • The full CUDA toolkit including nvcc
  • A matching cuDNN download from the NVIDIA website
  • Nothing; PyTorch ships its own GPU driver
  • A compatible NVIDIA driver
Answer

A compatible NVIDIA driver — The wheels bundle the CUDA runtime and cuDNN libraries. The one thing they cannot ship is the kernel driver, so a compatible NVIDIA driver must be installed.