• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _MPS-Backend:
2
3MPS backend
4===========
5
6:mod:`mps` device enables high-performance
7training on GPU for MacOS devices with Metal programming framework.  It
8introduces a new device to map Machine Learning computational graphs and
9primitives on highly efficient Metal Performance Shaders Graph framework and
10tuned kernels provided by Metal Performance Shaders framework respectively.
11
12The new MPS backend extends the PyTorch ecosystem and provides existing scripts
13capabilities to setup and run operations on GPU.
14
15To get started, simply move your Tensor and Module to the ``mps`` device:
16
17.. code:: python
18
19    # Check that MPS is available
20    if not torch.backends.mps.is_available():
21        if not torch.backends.mps.is_built():
22            print("MPS not available because the current PyTorch install was not "
23                  "built with MPS enabled.")
24        else:
25            print("MPS not available because the current MacOS version is not 12.3+ "
26                  "and/or you do not have an MPS-enabled device on this machine.")
27
28    else:
29        mps_device = torch.device("mps")
30
31        # Create a Tensor directly on the mps device
32        x = torch.ones(5, device=mps_device)
33        # Or
34        x = torch.ones(5, device="mps")
35
36        # Any operation happens on the GPU
37        y = x * 2
38
39        # Move your model to mps just like any other device
40        model = YourFavoriteNet()
41        model.to(mps_device)
42
43        # Now every call runs on the GPU
44        pred = model(x)
45