| <!--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 ์ฌ์ฉ ์์๋ฅผ ์ง์ ์ง์ ํ ์๋ ์์ต๋๋ค. |