| 1 |
<!--Copyright 2022 The HuggingFace Team. All rights reserved. |
| 2 |
|
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 |
the License. You may obtain a copy of the License at |
| 5 |
|
| 6 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
|
| 8 |
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 |
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 |
specific language governing permissions and limitations under the License. |
| 11 |
|
| 12 |
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 |
rendered properly in your Markdown viewer. |
| 14 |
--> |
| 15 |
|
| 16 |
# Accelerated PyTorch Training on Mac |
| 17 |
|
| 18 |
With PyTorch v1.12 release, developers and researchers can take advantage of Apple silicon GPUs for significantly faster model training. |
| 19 |
This unlocks the ability to perform machine learning workflows like prototyping and fine-tuning locally, right on Mac. |
| 20 |
Apple's Metal Performance Shaders (MPS) as a backend for PyTorch enables this and can be used via the new `"mps"` device. |
| 21 |
This will map computational graphs and primitives on the MPS Graph framework and tuned kernels provided by MPS. |
| 22 |
For more information please refer official documents [Introducing Accelerated PyTorch Training on Mac](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/) |
| 23 |
and [MPS BACKEND](https://pytorch.org/docs/stable/notes/mps.html). |
| 24 |
|
| 25 |
### Benefits of Training and Inference using Apple Silicon Chips |
| 26 |
|
| 27 |
1. Enables users to train larger networks or batch sizes locally |
| 28 |
2. Reduces data retrieval latency and provides the GPU with direct access to the full memory store due to unified memory architecture. |
| 29 |
Therefore, improving end-to-end performance. |
| 30 |
3. Reduces costs associated with cloud-based development or the need for additional local GPUs. |
| 31 |
|
| 32 |
**Pre-requisites**: To install torch with mps support, |
| 33 |
please follow this nice medium article [GPU-Acceleration Comes to PyTorch on M1 Macs](https://medium.com/towards-data-science/gpu-acceleration-comes-to-pytorch-on-m1-macs-195c399efcc1). |
| 34 |
|
| 35 |
|
| 36 |
## How it works out of the box |
| 37 |
It is enabled by default on MacOs machines with MPS enabled Apple Silicon GPUs. |
| 38 |
To disable it, pass `--cpu` flag to `accelerate launch` command or answer the corresponding question when answering the `accelerate config` questionnaire. |
| 39 |
|
| 40 |
You can directly run the following script to test it out on MPS enabled Apple Silicon machines: |
| 41 |
```bash |
| 42 |
accelerate launch /examples/cv_example.py --data_dir images |
| 43 |
``` |
| 44 |
|
| 45 |
## A few caveats to be aware of |
| 46 |
|
| 47 |
1. Distributed setups `gloo` and `nccl` are not working with `mps` device. |
| 48 |
This means that currently only single GPU of `mps` device type can be used. |
| 49 |
|
| 50 |
Finally, please, remember that, `Accelerate` only integrates MPS backend, therefore if you |
| 51 |
have any problems or questions with regards to MPS backend usage, please, file an issue with [PyTorch GitHub](https://github.com/pytorch/pytorch/issues). |