| <! |
|
|
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
|
|
| http://www.apache.org/licenses/LICENSE-2.0 |
|
|
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations under the License. |
| |
|
|
| # Accelerated PyTorch Training on Mac |
|
|
| With PyTorch v1.12 release, developers and researchers can take advantage of Apple silicon GPUs for significantly faster model training. |
| This unlocks the ability to perform machine learning workflows like prototyping and fine-tuning locally, right on Mac. |
| Apple's Metal Performance Shaders (MPS) as a backend for PyTorch enables this and can be used via the new `"mps"` device. |
| This will map computational graphs and primitives on the MPS Graph framework and tuned kernels provided by MPS. |
| For more information please refer official documents [Introducing Accelerated PyTorch Training on Mac](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/) |
| and [MPS BACKEND](https://pytorch.org/docs/stable/notes/mps.html). |
|
|
| ### Benefits of Training and Inference using Apple Silicon Chips |
|
|
| 1. Enables users to train larger networks or batch sizes locally |
| 2. Reduces data retrieval latency and provides the GPU with direct access to the full memory store due to unified memory architecture. |
| Therefore, improving end-to-end performance. |
| 3. Reduces costs associated with cloud-based development or the need for additional local GPUs. |
|
|
| **Pre-requisites**: To install torch with mps support, |
| 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). |
|
|
|
|
| ## How it works out of the box |
|
|
| On your machine(s) just run: |
|
|
| ```bash |
| accelerate config |
| ``` |
|
|
| and answer the questions asked, specifically choose `MPS` for the query: |
|
|
| ``` |
| Which type of machine are you using?. |
| ``` |
|
|
| This will generate a config file that will be used automatically to properly set |
| the default options when doing `accelerate launch`, such as the one shown below: |
|
|
| ```bash |
| compute_environment: LOCAL_MACHINE |
| deepspeed_config: {} |
| distributed_type: MPS |
| downcast_bf16: 'no' |
| fsdp_config: {} |
| machine_rank: 0 |
| main_process_ip: null |
| main_process_port: null |
| main_training_function: main |
| mixed_precision: 'no' |
| num_machines: 1 |
| num_processes: 1 |
| use_cpu: false |
| ``` |
|
|
| After this configuration has been made, here is how you run the CV example |
| (from the root of the repo) with MPS enabled: |
|
|
| ```bash |
| accelerate launch /examples/cv_example.py |
| ``` |
|
|
| ## A few caveats to be aware of |
|
|
| 1. We strongly recommend to install PyTorch >= 1.13 (nightly version at the time of writing) on your MacOS machine. |
| It has major fixes related to model correctness and performance improvements for transformer based models. |
| Please refer to https://github.com/pytorch/pytorch/issues/82707 for more details. |
| 2. Distributed setups `gloo` and `nccl` are not working with `mps` device. |
| This means that currently only single GPU of `mps` device type can be used. |
|
|
| Finally, please, remember that, 🤗 `Accelerate` only integrates MPS backend, therefore if you |
| have any problems or questions with regards to MPS backend usage, please, file an issue with [PyTorch GitHub](https://github.com/pytorch/pytorch/issues). |