Check if GPU Is Enabled in PyTorch
In this article
Yes, you can check if a GPU is enabled in PyTorch by running torch.cuda.is_available(), which returns True if a CUDA-capable GPU is present and PyTorch was built with CUDA support. For a more detailed check, torch.cuda.get_device_name(0) prints the GPU model, and torch.cuda.device_count() tells you how many GPUs are visible. This article explains how to run these checks, what they mean, and how to fix common issues when a GPU isn't detected.
Why PyTorch Needs an Explicit GPU Check
PyTorch does not automatically use a GPU just because one is installed. By default, tensors are created on the CPU, and operations run there unless you explicitly move them to a GPU device. The torch.cuda module provides the API for GPU interaction, but it only works if both the hardware and software conditions are met. The hardware condition is a CUDA-capable NVIDIA GPU (or, with newer versions, AMD GPUs via ROCm or Apple Silicon via MPS). The software condition is a PyTorch build that includes the appropriate backend—for CUDA, that means a version like torch==2.0.1+cu118 rather than the CPU-only torch==2.0.1+cpu. Without a proper build, torch.cuda.is_available() returns False even if the GPU is physically present.
How to Check GPU Availability in Code
Here is a minimal script that gives a complete picture of your GPU setup:
- Import PyTorch:
import torch - Check availability:
print(torch.cuda.is_available()) - Check the number of GPUs:
print(torch.cuda.device_count()) - Get the current device index:
print(torch.cuda.current_device()) - Get the GPU name:
print(torch.cuda.get_device_name(0))
If is_available() returns True, you can also print the device capability (compute capability) with torch.cuda.get_device_capability(0), which returns a tuple like (8, 6) for a Turing-era GPU. This is useful for checking compatibility with certain operations that require a minimum capability, such as mixed-precision training.
Why Does is_available() Return False?
There are several common reasons, and they are worth troubleshooting in order:
- PyTorch is CPU-only: If you installed via
pip install torchfrom the default PyPI index, you get the CPU-only build. The official PyTorch site provides the correct command for your system, typically with a+cusuffix for CUDA versions. - CUDA drivers are missing or outdated: The NVIDIA driver must support the CUDA version that PyTorch expects. Check with
nvidia-smito see the driver version and the CUDA version it supports. - CUDA toolkit is not installed: For most PyTorch builds, you do not need the full CUDA toolkit—only the driver. But if you compiled PyTorch from source, you need the toolkit.
- GPU is not visible to the system: In a laptop with an integrated and a discrete GPU, the discrete GPU might be disabled in BIOS or not properly connected. In a cloud environment, you might need to request a GPU instance.
- Environment variable CUDA_VISIBLE_DEVICES: This variable can restrict which GPUs PyTorch sees. If it is set to an empty string or a non-existent index,
is_available()returnsFalse.
How to Choose the Right PyTorch Build
To get GPU support, you must install a PyTorch version that matches your CUDA driver. The official website (pytorch.org) offers a selector where you choose your OS, package manager (pip or conda), and CUDA version. For example, for CUDA 11.8, the pip command is pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118. For CUDA 12.1, the URL changes to cu121. If you are unsure which CUDA version your driver supports, run nvidia-smi; the top-right corner shows the highest CUDA version the driver supports. You can install a PyTorch build that requires a lower CUDA version, but not a higher one.
Using the GPU for Tensor Operations
Once you confirm the GPU is available, you need to move tensors to it explicitly. The typical pattern is:
- Create a device object:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') - Move a tensor:
tensor = torch.randn(100, 100).to(device) - Or create it directly on the GPU:
tensor = torch.randn(100, 100, device='cuda')
Models are moved similarly: model = MyModel().to(device). During training, you must also move the input data and labels to the same device. A common mistake is to leave the model on CPU while moving data to GPU, which causes a runtime error about device mismatch.
Common Errors and How to Fix Them
Even when is_available() returns True, you might encounter errors. Here are a few frequent ones:
- CUDA out of memory: This happens when your GPU memory is full. Reduce batch size, use gradient accumulation, or clear the cache with
torch.cuda.empty_cache()(though this is not always effective). - Device mismatch: If you see
Expected all tensors to be on the same device, check that all tensors and model parameters are on the same device. Usetensor.deviceto inspect. - CUDA error: no kernel image is available: This indicates that the PyTorch build does not support your GPU's compute capability. For example, an older GPU might not be supported by a very recent PyTorch version. In that case, install an older PyTorch build or use a CPU-only version.
- Slow performance: If GPU is used but training is slow, check if the GPU is actually being utilized. Use
nvidia-smito monitor GPU utilization. If it is low, you might have a bottleneck in data loading or CPU preprocessing.
Alternatives to CUDA: ROCm and MPS
While CUDA is the most common backend, PyTorch also supports AMD GPUs via ROCm and Apple Silicon via the Metal Performance Shaders (MPS) backend. For ROCm, you can install a PyTorch build from the ROCm repository, and the check is different: torch.cuda.is_available() will still return False because it is not CUDA, but you can check torch.version.hip or simply try to move a tensor to cuda—ROCm emulates the CUDA interface, so torch.cuda.is_available() might actually return True in some builds. For Apple Silicon, you check torch.backends.mps.is_available() and use device = torch.device('mps'). The MPS backend is still maturing, so not all operations are supported, but it works for many standard models.
Frequently Asked Questions
How do I check if PyTorch is using the GPU?
Run torch.cuda.is_available(). If it returns True, PyTorch can use the GPU, but it does not mean it is currently using it. To see if a specific tensor is on GPU, check tensor.is_cuda or tensor.device. For a model, next(model.parameters()).is_cuda tells you if the model is on GPU.
Why does torch.cuda.is_available() return False even though I have an NVIDIA GPU?
Most likely you installed the CPU-only version of PyTorch. Uninstall it and reinstall with the CUDA-enabled version from pytorch.org. Also verify that your NVIDIA driver is installed and that nvidia-smi works. If you are on a laptop, ensure the dedicated GPU is not disabled in the BIOS or by the operating system's power settings.
Can I use PyTorch with an AMD GPU?
Yes, PyTorch supports AMD GPUs through the ROCm backend. You need to install a ROCm-compatible PyTorch build, and you must have a supported AMD GPU and ROCm drivers. The check is similar: torch.cuda.is_available() may return True because ROCm emulates CUDA, but you can also check torch.version.hip to confirm.
What is the difference between torch.cuda.is_available() and torch.device('cuda')?
is_available() returns a boolean indicating whether CUDA is accessible. torch.device('cuda') creates a device object that you can use to move tensors, but it does not check availability. If you use torch.device('cuda') when CUDA is not available, you will get an error when you try to move a tensor.
How can I make PyTorch use the GPU by default?
PyTorch does not have a global flag to force GPU usage. You must explicitly move tensors and models to the GPU in your code. A common pattern is to define a device variable at the start and use it throughout.