Skip to main content

MPS

MPS stands for Metal Performance Shader. It's the name of the shader language used by GPUs on MacOS. If you are familiar with OpenGL, DirectX or Vulkan, well Metal is Apple's version of that.

To pick a backend, we use the following code snippet.

device = "cpu"
if platform.system() == "Linux":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
elif platform.system() == "Darwin":
device = "mps"

# device = "cpu" # Uncomment this line to force execution on a GPU.

print("Using computing backend:",device)

The idea being that if mps or cuda are available, we use them. If not, we use the CPU as a fallback.