Be the first user to complete this post

  • 0
Add to List
Beginner

9. GPU Support in PyTorch for NVIDIA and MacOs

  • NVIDIA GPUs (CUDA): This code works for NVIDIA GPUs because it checks for CUDA availability using torch.cuda.is_available(). If a CUDA-enabled GPU is available, it sets the device to "cuda", allowing PyTorch to use the GPU for tensor operations.

  • Mac GPUs:

    • Metal API: On macOS, GPUs are generally accessed via Apple’s Metal API. As of now, PyTorch does not natively support Metal. However, Apple Silicon (M1/M2 chips) can be utilized for accelerated computations through the mps (Metal Performance Shaders) backend in PyTorch.

Cross-Platform Device Selection

If you want to write code that is cross-platform and can use CUDA on NVIDIA GPUs, mps on Apple Silicon, and CPU otherwise, you can do something like this:

 

Summary

  • cuda: Specific to NVIDIA GPUs with CUDA support.
  • mps: Used for Apple Silicon GPUs via the Metal API.
  • cpu: Used when no GPU is available or supported.

Click here to read for TensorFlow device selection