Apple Silicon has brought impressive performance gains with great power efficiency. Can we use these chips for Deep Learning as well?
Apple Silicon has delivered impressive performance gains coupled with excellent power efficiency. But can these chips also be utilized for Deep Learning? Absolutely!
In this article, we’ll explore 3 ways in which the Apple Silicon’s GPU can be leveraged for a variety of Deep Learning tasks.
The easiest way to use your GPU for Deep Learning is via the Metal Performance Shaders (MPS). MPS extends the PyTorch framework to leverage GPUs on Mac.
To use the MPS backend, you will need:
- macOS 12.3 or later
- Python 3.7 or later
Let’s create a script to check if MPS is set up correctly. First, we’ll need to create directory with a virtual environment and install PyTorch:
mkdir mps_test
cd mps_test
python3 -m venv .venv
source .venv/bin/activate
pip install torch
Next, we’ll create a Python file, check.py with the following code:
import torchif __name__ == '__main__':
# Check if MPS backend is available in PyTorch
if torch.backends.mps.is_available():
# Create a vector on the MPS device
x = torch.ones(1, device='mps')
print(x)
else:
print('MPS device not found.')
Let’s run it:
python check.py
The output should show:
tensor([1.], device='mps:0')
You can now leverage MPS in any PyTorch code. Usually, you’ll see the following in many PyTorch scripts:
model.to('cuda')
You can replace it with:
model.to('mps')
and enjoy GPU acceleration on your Apple Silicon!
MLX is an array framework for machine learning on Apple Silicon. It is developed by Apple ML research team. MLX is…