File size: 3,671 Bytes
17c6d62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!--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/) λΈ”λ‘œκ·Έ κ²Œμ‹œλ¬Όμ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.