File size: 4,650 Bytes
3bfe3dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!--Copyright 2025 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
specific language governing permissions and limitations under the License.

โš ๏ธ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->

# ๊ฐ€์†๊ธฐ ์„ ํƒ [[accelerator-selection]]

๋ถ„์‚ฐ ํ›ˆ๋ จ ์ค‘์— ์‚ฌ์šฉํ•  ๊ฐ€์†๊ธฐ(CUDA, XPU, MPS, HPU ๋“ฑ)์˜ ์ˆ˜์™€ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์—ฐ์‚ฐ ์„ฑ๋Šฅ์„ ๊ฐ€์ง„ ๊ฐ€์†๊ธฐ๊ฐ€ ์žˆ๊ณ  ๋” ๋น ๋ฅธ ๊ฐ€์†๊ธฐ๋ฅผ ๋จผ์ € ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€์†๊ธฐ ์ค‘ ์ผ๋ถ€๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ ํƒ ๊ณผ์ •์€ [DistributedDataParallel](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html)๊ณผ [DataParallel](https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html) ๋ชจ๋‘์—์„œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. Accelerate๋‚˜ [DeepSpeed integration](./main_classes/deepspeed)์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ด ๊ฐ€์ด๋“œ๋Š” ์‚ฌ์šฉํ•  ๊ฐ€์†๊ธฐ์˜ ์ˆ˜์™€ ์‚ฌ์šฉ ์ˆœ์„œ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

## ๊ฐ€์†๊ธฐ ์ˆ˜ [[number-of-accelerators]]

์˜ˆ๋ฅผ ๋“ค์–ด, 4๊ฐœ์˜ ๊ฐ€์†๊ธฐ๊ฐ€ ์žˆ๊ณ  ์ฒ˜์Œ 2๊ฐœ๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์„ธ์š”.

<hfoptions id="select-accelerator">
<hfoption id="torchrun">

`--nproc_per_node`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ๊ฐ€์†๊ธฐ ์ˆ˜๋ฅผ ์„ ํƒํ•˜์„ธ์š”.

```bash
torchrun --nproc_per_node=2  trainer-program.py ...
```

</hfoption>
<hfoption id="Accelerate">

`--num_processes`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ๊ฐ€์†๊ธฐ ์ˆ˜๋ฅผ ์„ ํƒํ•˜์„ธ์š”.

```bash
accelerate launch --num_processes 2 trainer-program.py ...
```

</hfoption>
<hfoption id="DeepSpeed">

`--num_gpus`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•  GPU ์ˆ˜๋ฅผ ์„ ํƒํ•˜์„ธ์š”.

```bash
deepspeed --num_gpus 2 trainer-program.py ...
```

</hfoption>
</hfoptions>

## ๊ฐ€์†๊ธฐ ์ˆœ์„œ [[order-of-accelerators]]
์‚ฌ์šฉํ•  ํŠน์ • ๊ฐ€์†๊ธฐ์™€ ๊ทธ ์ˆœ์„œ๋ฅผ ์„ ํƒํ•˜๋ ค๋ฉด ํ•˜๋“œ์›จ์–ด์— ์ ํ•ฉํ•œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”. ์ด๋Š” ๊ฐ ์‹คํ–‰๋งˆ๋‹ค ๋ช…๋ น์ค„์—์„œ ์„ค์ •๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์ง€๋งŒ, `~/.bashrc`๋‚˜ ๋‹ค๋ฅธ ์‹œ์ž‘ ์„ค์ • ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 4๊ฐœ์˜ ๊ฐ€์†๊ธฐ(0, 1, 2, 3)๊ฐ€ ์žˆ๊ณ  ๊ฐ€์†๊ธฐ 0๊ณผ 2๋งŒ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด:

<hfoptions id="accelerator-type">
<hfoption id="CUDA">

```bash
CUDA_VISIBLE_DEVICES=0,2 torchrun trainer-program.py ...
```

GPU 0๊ณผ 2๋งŒ PyTorch์— "๋ณด์ด๋ฉฐ" ๊ฐ๊ฐ `cuda:0`๊ณผ `cuda:1`๋กœ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค.  
์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋ ค๋ฉด (GPU 2๋ฅผ `cuda:0`์œผ๋กœ, GPU 0์„ `cuda:1`๋กœ ์‚ฌ์šฉ):


```bash
CUDA_VISIBLE_DEVICES=2,0 torchrun trainer-program.py ...
```

GPU ์—†์ด ์‹คํ–‰ํ•˜๋ ค๋ฉด:

```bash
CUDA_VISIBLE_DEVICES= python trainer-program.py ...
```

`CUDA_DEVICE_ORDER`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ CUDA ์žฅ์น˜ ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค:

- PCIe ๋ฒ„์Šค ID ์ˆœ์„œ๋กœ ์ •๋ ฌ (`nvidia-smi`์™€ ์ผ์น˜):

    ```bash
$hf_i18n_placeholder21export CUDA_DEVICE_ORDER=PCI_BUS_ID
    ```

- ์—ฐ์‚ฐ ์„ฑ๋Šฅ ์ˆœ์„œ๋กœ ์ •๋ ฌ (๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒƒ๋ถ€ํ„ฐ):

    ```bash
    export CUDA_DEVICE_ORDER=FASTEST_FIRST
    ```

</hfoption>
<hfoption id="Intel XPU">

```bash
ZE_AFFINITY_MASK=0,2 torchrun trainer-program.py ...
```

XPU 0๊ณผ 2๋งŒ PyTorch์— "๋ณด์ด๋ฉฐ" ๊ฐ๊ฐ `xpu:0`๊ณผ `xpu:1`๋กœ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค.  
์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋ ค๋ฉด (XPU 2๋ฅผ `xpu:0`์œผ๋กœ, XPU 0์„ `xpu:1`๋กœ ์‚ฌ์šฉ):

```bash
ZE_AFFINITY_MASK=2,0 torchrun trainer-program.py ...
```


๋‹ค์Œ์œผ๋กœ Intel XPU ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค:

```bash
export ZE_ENABLE_PCI_ID_DEVICE_ORDER=1
```

Intel XPU์˜ ์žฅ์น˜ ์—ด๊ฑฐ ๋ฐ ์ •๋ ฌ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋Š” [Level Zero](https://github.com/oneapi-src/level-zero/blob/master/README.md?plain=1#L87) ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

</hfoption>
</hfoptions>



> [!WARNING]
> ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋Š” ๋ช…๋ น์ค„์— ์ถ”๊ฐ€ํ•˜๋Š” ๋Œ€์‹  exportํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ์„ค์ •๋˜์—ˆ๋Š”์ง€ ์žŠ์–ด๋ฒ„๋ฆฌ๊ณ  ๊ฒฐ๊ตญ ์ž˜๋ชป๋œ ๊ฐ€์†๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ์ˆ˜ ์žˆ์–ด ํ˜ผ๋ž€์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ถŒ์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹ , ๋™์ผํ•œ ๋ช…๋ น์ค„์—์„œ ํŠน์ • ํ›ˆ๋ จ ์‹คํ–‰์— ๋Œ€ํ•ด ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ๊ด€๋ก€์ž…๋‹ˆ๋‹ค.
```