repo-app / transformers /docs /source /ko /gpu_selection.md
ghrua's picture
Initial commit with Dockerfile
8b821fa
<!--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 contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->
# GPU ์„ ํƒํ•˜๊ธฐ [[gpu-selection]]
๋ถ„์‚ฐ ํ•™์Šต ๊ณผ์ •์—์„œ ์‚ฌ์šฉํ•  GPU์˜ ๊ฐœ์ˆ˜์™€ ์ˆœ์„œ๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์„œ๋กœ ๋‹ค๋ฅธ ์—ฐ์‚ฐ ์„ฑ๋Šฅ์„ ๊ฐ€์ง„ GPU๊ฐ€ ์žˆ์„ ๋•Œ ๋” ๋น ๋ฅธ GPU๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ GPU ์ค‘ ์ผ๋ถ€๋งŒ ์„ ํƒํ•˜์—ฌ ํ™œ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ ํƒ ๊ณผ์ •์€ [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 ํ†ตํ•ฉ](./main_classes/deepspeed)์€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
์ด ๊ฐ€์ด๋“œ๋Š” ์‚ฌ์šฉํ•  GPU์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์‚ฌ์šฉ ์ˆœ์„œ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.
## GPU ๊ฐœ์ˆ˜ ์ง€์ • [[number-of-gpus]]
์˜ˆ๋ฅผ ๋“ค์–ด, GPU๊ฐ€ 4๊ฐœ ์žˆ๊ณ  ๊ทธ์ค‘ ์ฒ˜์Œ 2๊ฐœ๋งŒ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์„ธ์š”.
<hfoptions id="select-gpu">
<hfoption id="torchrun">
์‚ฌ์šฉํ•  GPU ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•ด `--nproc_per_node` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์„ธ์š”.
```bash
torchrun --nproc_per_node=2 trainer-program.py ...
```
</hfoption>
<hfoption id="Accelerate">
์‚ฌ์šฉํ•  GPU ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•ด `--num_processes` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์„ธ์š”.
```bash
accelerate launch --num_processes 2 trainer-program.py ...
```
</hfoption>
<hfoption id="DeepSpeed">
์‚ฌ์šฉํ•  GPU ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๊ธฐ ์œ„ํ•ด `--num_gpus` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์„ธ์š”.
```bash
deepspeed --num_gpus 2 trainer-program.py ...
```
</hfoption>
</hfoptions>
### GPU ์ˆœ์„œ [[order-of-gpus]]
์‚ฌ์šฉํ•  GPU์™€ ๊ทธ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๋ ค๋ฉด `CUDA_VISIBLE_DEVICES` ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์„ธ์š”. ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ `~/bashrc` ๋˜๋Š” ๋‹ค๋ฅธ ์‹œ์ž‘ ์„ค์ • ํŒŒ์ผ์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. `CUDA_VISIBLE_DEVICES`๋Š” ์‚ฌ์šฉํ•  GPU๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, GPU๊ฐ€ 4๊ฐœ (0, 1, 2, 3) ์žˆ๊ณ  ๊ทธ์ค‘์—์„œ 0๋ฒˆ๊ณผ 2๋ฒˆ GPU๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
```bash
CUDA_VISIBLE_DEVICES=0,2 torchrun trainer-program.py ...
```
์˜ค์ง ๋‘ ๊ฐœ์˜ ๋ฌผ๋ฆฌ์  GPU(0, 2)๋งŒ PyTorch์—์„œ "๋ณด์ด๋Š”" ์ƒํƒœ๊ฐ€ ๋˜๋ฉฐ, ๊ฐ๊ฐ `cuda:0`๊ณผ `cuda:1`๋กœ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, GPU ์‚ฌ์šฉ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, GPU 0์ด `cuda:1`, GPU 2๊ฐ€ `cuda:0`์œผ๋กœ ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค."
```bash
CUDA_VISIBLE_DEVICES=2,0 torchrun trainer-program.py ...
```
`CUDA_VISIBLE_DEVICES` ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๋นˆ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ GPU๊ฐ€ ์—†๋Š” ํ™˜๊ฒฝ์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
```bash
CUDA_VISIBLE_DEVICES= python trainer-program.py ...
```
> [!WARNING]
> ๋‹ค๋ฅธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, CUDA_VISIBLE_DEVICES๋ฅผ ์ปค๋งจ๋“œ ๋ผ์ธ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋Œ€์‹  exportํ•˜์—ฌ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ์‹์€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ์„ค์ •๋˜์—ˆ๋Š”์ง€๋ฅผ ์žŠ์–ด๋ฒ„๋ฆด ๊ฒฝ์šฐ, ์ž˜๋ชป๋œ GPU๋ฅผ ์‚ฌ์šฉํ•  ์œ„ํ—˜์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํŠน์ • ํ•™์Šต ์‹คํ–‰์— ๋Œ€ํ•ด ๋™์ผํ•œ ์ปค๋งจ๋“œ ๋ผ์ธ์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
`CUDA_DEVICE_ORDER`๋Š” GPU์˜ ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€์ฒด ํ™˜๊ฒฝ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ GPU ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
1. NVIDIA ๋ฐ AMD GPU์˜ PCIe ๋ฒ„์Šค ID๋Š” ๊ฐ๊ฐ [nvidia-smi](https://developer.nvidia.com/nvidia-system-management-interface)์™€ [rocm-smi](https://rocm.docs.amd.com/projects/rocm_smi_lib/en/latest/.doxygen/docBin/html/index.html)์˜ ์ˆœ์„œ์™€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.
```bash
export CUDA_DEVICE_ORDER=PCI_BUS_ID
```
2. GPU ์—ฐ์‚ฐ ๋Šฅ๋ ฅ
```bash
export CUDA_DEVICE_ORDER=FASTEST_FIRST
```
The `CUDA_DEVICE_ORDER` is especially useful if your training setup consists of an older and newer GPU, where the older GPU appears first, but you cannot physically swap the cards to make the newer GPU appear first. In this case, set `CUDA_DEVICE_ORDER=FASTEST_FIRST` to always use the newer and faster GPU first (`nvidia-smi` or `rocm-smi` still reports the GPUs in their PCIe order). Or you could also set `export CUDA_VISIBLE_DEVICES=1,0`.
`CUDA_DEVICE_ORDER`๋Š” ๊ตฌํ˜• GPU์™€ ์‹ ํ˜• GPU๊ฐ€ ํ˜ผํ•ฉ๋œ ํ™˜๊ฒฝ์—์„œ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ตฌํ˜• GPU๊ฐ€ ๋จผ์ € ํ‘œ์‹œ๋˜์ง€๋งŒ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๊ต์ฒดํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, `CUDA_DEVICE_ORDER=FASTEST_FIRST`๋ฅผ ์„ค์ •ํ•˜๋ฉด ํ•ญ์ƒ ์‹ ํ˜• ๋ฐ ๋” ๋น ๋ฅธ GPU๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉ(nvidia-smi ๋˜๋Š” rocm-smi๋Š” PCIe ์ˆœ์„œ๋Œ€๋กœ GPU๋ฅผ ํ‘œ์‹œํ•จ)ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š”, `export CUDA_VISIBLE_DEVICES=1,0`์„ ์„ค์ •ํ•˜์—ฌ GPU ์‚ฌ์šฉ ์ˆœ์„œ๋ฅผ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.