How to Check if GPU is Available in PyTorch
Use
torch.cuda.is_available() to check if a GPU is available in PyTorch. It returns True if a GPU is ready to use, otherwise False.Syntax
The function torch.cuda.is_available() returns a boolean value indicating if a GPU is available for PyTorch to use.
torch: The PyTorch library.cuda: The CUDA module in PyTorch for GPU support.is_available(): Function that checks GPU availability.
python
import torch # Check if GPU is available gpu_available = torch.cuda.is_available()
Example
This example shows how to check if a GPU is available and print a message accordingly.
python
import torch def check_gpu(): if torch.cuda.is_available(): print("GPU is available! You can use CUDA for faster computation.") else: print("GPU is not available. Using CPU instead.") check_gpu()
Output
GPU is available! You can use CUDA for faster computation.
Common Pitfalls
Some common mistakes when checking GPU availability:
- Not importing
torchbefore callingtorch.cuda.is_available(). - Assuming GPU is always available on any machine.
- Not handling the case when GPU is not available, which can cause errors if you try to move tensors to CUDA.
Always check GPU availability before using CUDA features.
python
import torch # Wrong way: assuming GPU is available without check # tensor = torch.tensor([1, 2, 3]).cuda() # This can cause error if no GPU # Right way: check first if torch.cuda.is_available(): tensor = torch.tensor([1, 2, 3]).cuda() else: tensor = torch.tensor([1, 2, 3])
Quick Reference
Summary tips for checking GPU availability in PyTorch:
- Use
torch.cuda.is_available()to detect GPU presence. - Use conditional code to switch between CPU and GPU.
- Remember that GPU availability depends on hardware and drivers.
Key Takeaways
Use torch.cuda.is_available() to check if a GPU is ready for PyTorch.
Always check GPU availability before moving tensors or models to CUDA.
Handle the case when GPU is not available to avoid runtime errors.
GPU availability depends on your system hardware and CUDA drivers.
Use conditional logic to switch between CPU and GPU in your code.