How to Check if GPU is Available for PyTorch in AI for Beginners
Use torch.cuda.is_available() to detect CUDA-capable GPUs, then instantiate torch.device("cuda" if torch.cuda.is_available() else "cpu") to automatically route tensors and models to the accelerator when running the Microsoft AI-For-Beginners curriculum.
The Microsoft AI-For-Beginners repository provides hands-on deep learning lessons that rely heavily on PyTorch for computer vision and natural language processing tasks. Before executing training loops, every lesson follows a standardized pattern to check if GPU is available for PyTorch, ensuring examples run efficiently on CUDA hardware while gracefully falling back to CPU when necessary.
Detecting CUDA Hardware with torch.cuda.is_available()
The foundational check occurs through PyTorch's built-in CUDA runtime inspection. In lessons/4-ComputerVision/07-ConvNets/pytorchcv.py at line 17, the curriculum implements:
default_device = 'cuda' if torch.cuda.is_available() else 'cpu'
This boolean evaluation returns True only when a compatible NVIDIA GPU driver and CUDA toolkit are properly installed on the host system. When the function returns False, all subsequent operations default to CPU execution, preventing runtime errors on machines without discrete graphics hardware.
Instantiating a torch.device for Cross-Platform Compatibility
Rather than hardcoding device strings, the repository consistently uses the device object abstraction. In lessons/5-NLP/14-Embeddings/torchnlp.py at line 7, the pattern appears as:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Creating a torch.device decouples the hardware detection logic from the training code, allowing you to pass a single device variable to tensor constructors and model initialization routines.
Migrating Models and Tensors to the Accelerator
Once the device object exists, the curriculum moves data using the .to() method. In lessons/4-ComputerVision/07-ConvNets/pytorchcv.py (lines 33-35), the implementation transfers models and input batches:
model = model.to(default_device)
images = images.to(default_device)
Similarly, lessons/5-NLP/14-Embeddings/torchnlp.py (lines 48-51) demonstrates moving embedding layers and tensors for natural language processing workloads. This explicit transfer ensures that compute-intensive operations execute on the GPU while maintaining code that remains executable on CPU-only environments.
Complete Verification Script
The following self-contained snippet replicates the exact pattern used across the AI-For-Beginners lessons to check if GPU is available for PyTorch and verify device placement:
import torch
# Detect CUDA-capable GPU
if torch.cuda.is_available():
print("✅ CUDA GPU is available!")
print(f"GPU name: {torch.cuda.get_device_name(0)}")
else:
print("⚠️ No CUDA GPU detected; falling back to CPU.")
# Create device object
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Move tensor and model to selected device
x = torch.randn(4, 3).to(device)
model = torch.nn.Linear(3, 2).to(device)
# Verify device placement
print(f"Tensor device: {x.device}")
print(f"Model device: {next(model.parameters()).device}")
Key Implementation Files in the Repository
The following source files demonstrate the canonical approach to GPU detection:
lessons/4-ComputerVision/07-ConvNets/pytorchcv.py: Definesdefault_deviceusingtorch.cuda.is_available()and migrates convolutional networks with.to(default_device).lessons/5-NLP/14-Embeddings/torchnlp.py: Creates atorch.deviceinstance for embedding-based natural language processing models.lessons/4-ComputerVision/08-TransferLearning/pytorchcv.py: Repeats the device selection pattern for transfer learning experiments.lessons/5-NLP/16-RNN/torchnlp.py: Implements GPU checking for recurrent neural network architectures.
Summary
- Use
torch.cuda.is_available()to programmatically detect CUDA-capable hardware before initializing models. - Instantiate
torch.device("cuda" if torch.cuda.is_available() else "cpu")to create a reusable device handle that works across CPU and GPU environments. - Move tensors and models using
.to(device)rather than hardcoded.cuda()calls to maintain cross-platform compatibility. - Reference
pytorchcv.pyandtorchnlp.pyin the AI-For-Beginners repository for production-ready implementation patterns.
Frequently Asked Questions
How do I check if GPU is available for PyTorch in AI for Beginners?
Call torch.cuda.is_available() after importing PyTorch. According to the source code in lessons/4-ComputerVision/07-ConvNets/pytorchcv.py, this function returns a boolean indicating whether the host machine has a compatible NVIDIA GPU and properly configured CUDA drivers.
What happens if I run the AI for Beginners code without a GPU?
The repository gracefully handles CPU-only environments. When torch.cuda.is_available() returns False, the device selection logic defaults to "cpu", and all tensor operations execute on the processor without requiring code modifications.
How do I verify that my model is actually running on the GPU?
After moving a model to the device using .to(device), inspect next(model.parameters()).device or check the .device attribute of any output tensor. If the string contains "cuda:0", the model is executing on the GPU; "cpu" indicates processor execution.
Does the AI for Beginners curriculum support Apple Silicon (MPS) GPUs?
The repository primarily targets CUDA devices using torch.cuda.is_available(). For Apple Silicon compatibility, you would need to modify the device selection logic to check torch.backends.mps.is_available() and instantiate torch.device("mps") instead of the CUDA-specific patterns found in the current lesson files.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →