| <!--Copyright 2022 The HuggingFace Team. All rights reserved. | |
| 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 | |
| β οΈ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | |
| rendered properly in your Markdown viewer. | |
| --> | |
| # Apple μ€λ¦¬μ½μμ Pytorch νμ΅ [[PyTorch training on Apple silicon]] | |
| μ΄μ μλ Macμμ λͺ¨λΈμ νμ΅ν λ CPUλ§ μ¬μ©ν μ μμμ΅λλ€. κ·Έλ¬λ μ΄μ PyTorch v1.12μ μΆμλ‘ Appleμ μ€λ¦¬μ½ GPUλ₯Ό μ¬μ©νμ¬ ν¨μ¬ λ λΉ λ₯Έ μ±λ₯μΌλ‘ λͺ¨λΈμ νμ΅ν μ μκ² λμμ΅λλ€. μ΄λ Pytorchμμ Appleμ Metal Performance Shaders (MPS)λ₯Ό λ°±μλλ‘ ν΅ν©νλ©΄μ κ°λ₯ν΄μ‘μ΅λλ€. [MPS λ°±μλ](https://pytorch.org/docs/stable/notes/mps.html)λ Pytorch μ°μ°μ Metal μΈμ΄λλ‘ κ΅¬ννκ³ μ΄ λͺ¨λλ€μ mps μ₯μΉμμ μ€νν μ μλλ‘ μ§μν©λλ€. | |
| <Tip warning={true}> | |
| μΌλΆ Pytorch μ°μ°λ€μ μμ§ MPSμμ μ§μλμ§ μμ μ€λ₯κ° λ°μν μ μμ΅λλ€. μ΄λ₯Ό λ°©μ§νλ €λ©΄ νκ²½ λ³μ `PYTORCH_ENABLE_MPS_FALLBACK=1` λ₯Ό μ€μ νμ¬ CPU 컀λμ λμ μ¬μ©νλλ‘ ν΄μΌ ν©λλ€(μ΄λ `UserWarning`μ΄ μ¬μ ν νμλ μ μμ΅λλ€). | |
| <br> | |
| λ€λ₯Έ μ€λ₯κ° λ°μν κ²½μ° [PyTorch](https://github.com/pytorch/pytorch/issues) 리ν¬μ§ν 리μ μ΄μλ₯Ό λ±λ‘ν΄μ£ΌμΈμ. νμ¬ [`Trainer`]λ MPS λ°±μλλ§ ν΅ν©νκ³ μμ΅λλ€. | |
| </Tip> | |
| `mps` μ₯μΉλ₯Ό μ΄μ©νλ©΄ λ€μκ³Ό κ°μ μ΄μ λ€μ μ»μ μ μμ΅λλ€: | |
| * λ‘컬μμ λ ν° λ€νΈμν¬λ λ°°μΉ ν¬κΈ°λ‘ νμ΅ κ°λ₯ | |
| * GPUμ ν΅ν© λ©λͺ¨λ¦¬ μν€ν μ²λ‘ μΈν΄ λ©λͺ¨λ¦¬μ μ§μ μ κ·Όν μ μμ΄ λ°μ΄ν° λ‘λ© μ§μ° κ°μ | |
| * ν΄λΌμ°λ κΈ°λ° GPUλ μΆκ° GPUκ° νμ μμΌλ―λ‘ λΉμ© μ κ° κ°λ₯ | |
| Pytorchκ° μ€μΉλμ΄ μλμ§ νμΈνκ³ μμνμΈμ. MPS κ°μμ macOS 12.3 μ΄μμμ μ§μλ©λλ€. | |
| ```bash | |
| pip install torch torchvision torchaudio | |
| ``` | |
| [`TrainingArguments`]λ `mps` μ₯μΉκ° μ¬μ© κ°λ₯ν κ²½μ° μ΄λ₯Ό κΈ°λ³Έμ μΌλ‘ μ¬μ©νλ―λ‘ μ₯μΉλ₯Ό λ°λ‘ μ€μ ν νμκ° μμ΅λλ€. μλ₯Ό λ€μ΄, MPS λ°±μλλ₯Ό μλμΌλ‘ νμ±ννμ¬ [run_glue.py](https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py) μ€ν¬λ¦½νΈλ₯Ό μ무 μμ μμ΄ μ€νν μ μμ΅λλ€. | |
| ```diff | |
| export TASK_NAME=mrpc | |
| python examples/pytorch/text-classification/run_glue.py \ | |
| --model_name_or_path google-bert/bert-base-cased \ | |
| --task_name $TASK_NAME \ | |
| - --use_mps_device \ | |
| --do_train \ | |
| --do_eval \ | |
| --max_seq_length 128 \ | |
| --per_device_train_batch_size 32 \ | |
| --learning_rate 2e-5 \ | |
| --num_train_epochs 3 \ | |
| --output_dir /tmp/$TASK_NAME/ \ | |
| --overwrite_output_dir | |
| ``` | |
| `gloco`μ `nccl`κ³Ό κ°μ [λΆμ° νμ΅ λ°±μλ](https://pytorch.org/docs/stable/distributed.html#backends)λ `mps` μ₯μΉμμ μ§μλμ§ μμΌλ―λ‘, MPS λ°±μλμμλ λ¨μΌ GPUλ‘λ§ νμ΅μ΄ κ°λ₯ν©λλ€. | |
| Macμμ κ°μλ PyTorch νμ΅μ λν λ μμΈν λ΄μ©μ [Introducing Accelerated PyTorch Training on Mac](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/) λΈλ‘κ·Έ κ²μλ¬Όμμ νμΈν μ μμ΅λλ€. | |