YouTip LogoYouTip

Pytorch Torch Cuda Is_Available

## PyTorch: torch.cuda.is_available() Reference In PyTorch, `torch.cuda.is_available()` is a fundamental utility function used to determine whether your current system environment supports CUDA (Compute Unified Device Architecture). CUDA is a parallel computing platform and application programming interface (API) model created by NVIDIA. By checking if CUDA is available, you can dynamically write code that leverages GPU acceleration when a compatible NVIDIA graphics card and the correct drivers are installed, while gracefully falling back to the CPU when they are not. --- ### Function Definition ```python torch.cuda.is_available() ``` #### Return Value * **`bool`**: Returns `True` if a CUDA-supporting GPU is detected and the PyTorch CUDA library is correctly configured; otherwise, returns `False`. --- ### Basic Usage and Code Example The most common design pattern in PyTorch is to check for CUDA availability at the beginning of your script to define a target `device`. This device object is then used to cast tensors and neural network models to either the GPU or CPU. ```python import torch # 1. Check if CUDA is available cuda_present = torch.cuda.is_available() print(f"Is CUDA available? {cuda_present}") # 2. Define the target device dynamically device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Using device: {device}") # 3. Retrieve additional GPU information if available if torch.cuda.is_available(): print("CUDA is available!") print("Number of GPUs:", torch.cuda.device_count()) print("Primary GPU Name:", torch.cuda.get_device_name(0)) else: print("CUDA is not available. Running on CPU.") ``` --- ### Practical Workflow: Moving Tensors and Models Once you have verified CUDA availability and defined your `device`, you can seamlessly move your PyTorch operations to the GPU. ```python import torch # Define device device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Create a tensor on the host (CPU) x = torch.tensor([1.0, 2.0, 3.0]) # Move the tensor to the selected device (GPU if available, otherwise CPU) x = x.to(device) print(f"Tensor device: {x.device}") # Define a simple model and move it to the device model = torch.nn.Linear(3, 1).to(device) print(f"Model parameters device: {next(model.parameters()).device}") ``` --- ### Troubleshooting: Why does `torch.cuda.is_available()` return `False`? If you expect your GPU to be active but the function returns `False`, check the following common issues: 1. **Missing or Incompatible NVIDIA Drivers**: Ensure that your system has the official NVIDIA proprietary drivers installed and updated to a version that supports your CUDA toolkit. 2. **CPU-only PyTorch Installation**: If you installed PyTorch via a simple `pip install torch` command without specifying the CUDA variant, you might have downloaded the CPU-only version. Verify your installation command on the (https://pytorch.org/). 3. **CUDA Toolkit Version Mismatch**: The CUDA version supported by your installed PyTorch build must be compatible with the CUDA version installed on your system. You can check your PyTorch-compiled CUDA version using: ```python print(torch.version.cuda) ``` 4. **No NVIDIA GPU Present**: CUDA is exclusive to NVIDIA hardware. If you are running on an AMD GPU, Intel GPU, or Apple Silicon (M1/M2/M3), `torch.cuda.is_available()` will return `False`. *(Note: For Apple Silicon, use `torch.backends.mps.is_available()` instead).*
← Pytorch Torch Cuda Manual_SeedPytorch Torch Cuda Empty_Cache β†’