| <!--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. | |
| --> | |
| # ๋ค์ค CPU์์ ํจ์จ์ ์ผ๋ก ํ๋ จํ๊ธฐ [[efficient-training-on-multiple-cpus]] | |
| ํ๋์ CPU์์ ํ๋ จํ๋ ๊ฒ์ด ๋๋ฌด ๋๋ฆด ๋๋ ๋ค์ค CPU๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด ๊ฐ์ด๋๋ PyTorch ๊ธฐ๋ฐ์ DDP๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ์ฐ CPU ํ๋ จ์ ํจ์จ์ ์ผ๋ก ์ํํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ค๋ช ํฉ๋๋ค. | |
| ## PyTorch์ฉ Intelยฎ oneCCL ๋ฐ์ธ๋ฉ [[intel-oneccl-bindings-for-pytorch]] | |
| [Intelยฎ oneCCL](https://github.com/oneapi-src/oneCCL) (collective communications library)์ allreduce, allgather, alltoall๊ณผ ๊ฐ์ ์งํฉ ํต์ (collective communications)์ ๊ตฌํํ ํจ์จ์ ์ธ ๋ถ์ฐ ๋ฅ๋ฌ๋ ํ๋ จ์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. oneCCL์ ๋ํ ์์ธํ ์ ๋ณด๋ [oneCCL ๋ฌธ์](https://spec.oneapi.com/versions/latest/elements/oneCCL/source/index.html)์ [oneCCL ์ฌ์](https://spec.oneapi.com/versions/latest/elements/oneCCL/source/index.html)์ ์ฐธ์กฐํ์ธ์. | |
| `oneccl_bindings_for_pytorch` ๋ชจ๋ (`torch_ccl`์ ๋ฒ์ 1.12 ์ด์ ์ ์ฌ์ฉ)์ PyTorch C10D ProcessGroup API๋ฅผ ๊ตฌํํ๋ฉฐ, ์ธ๋ถ ProcessGroup๋ก ๋์ ์ผ๋ก ๊ฐ์ ธ์ฌ ์ ์์ผ๋ฉฐ ํ์ฌ Linux ํ๋ซํผ์์๋ง ์๋ํฉ๋๋ค. | |
| [oneccl_bind_pt](https://github.com/intel/torch-ccl)์์ ๋ ์์ธํ ์ ๋ณด๋ฅผ ํ์ธํ์ธ์. | |
| ### PyTorch์ฉ Intelยฎ oneCCL ๋ฐ์ธ๋ฉ ์ค์น: [[intel-oneccl-bindings-for-pytorch-installation]] | |
| ๋ค์ Python ๋ฒ์ ์ ๋ํ Wheel ํ์ผ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. | |
| | Extension Version | Python 3.6 | Python 3.7 | Python 3.8 | Python 3.9 | Python 3.10 | | |
| | :---------------: | :--------: | :--------: | :--------: | :--------: | :---------: | | |
| | 1.13.0 | | โ | โ | โ | โ | | |
| | 1.12.100 | | โ | โ | โ | โ | | |
| | 1.12.0 | | โ | โ | โ | โ | | |
| | 1.11.0 | | โ | โ | โ | โ | | |
| | 1.10.0 | โ | โ | โ | โ | | | |
| ```bash | |
| pip install oneccl_bind_pt=={pytorch_version} -f https://developer.intel.com/ipex-whl-stable-cpu | |
| ``` | |
| `{pytorch_version}`์ 1.13.0๊ณผ ๊ฐ์ด PyTorch ๋ฒ์ ์ ๋ํ๋ ๋๋ค. | |
| [oneccl_bind_pt ์ค์น](https://github.com/intel/torch-ccl)์ ๋ํ ๋ ๋ง์ ์ ๊ทผ ๋ฐฉ๋ฒ์ ํ์ธํด ๋ณด์ธ์. | |
| oneCCL๊ณผ PyTorch์ ๋ฒ์ ์ ์ผ์นํด์ผ ํฉ๋๋ค. | |
| <Tip warning={true}> | |
| oneccl_bindings_for_pytorch 1.12.0 ๋ฒ์ ์ ๋ฏธ๋ฆฌ ๋น๋๋ Wheel ํ์ผ์ PyTorch 1.12.1๊ณผ ํธํ๋์ง ์์ต๋๋ค(PyTorch 1.12.0์ฉ์ ๋๋ค). | |
| PyTorch 1.12.1์ oneccl_bindings_for_pytorch 1.12.10 ๋ฒ์ ๊ณผ ํจ๊ป ์ฌ์ฉํด์ผ ํฉ๋๋ค. | |
| </Tip> | |
| ## Intelยฎ MPI ๋ผ์ด๋ธ๋ฌ๋ฆฌ [[intel-mpi-library]] | |
| ์ด ํ์ค ๊ธฐ๋ฐ MPI ๊ตฌํ์ ์ฌ์ฉํ์ฌ Intelยฎ ์ํคํ ์ฒ์์ ์ ์ฐํ๊ณ ํจ์จ์ ์ด๋ฉฐ ํ์ฅ ๊ฐ๋ฅํ ํด๋ฌ์คํฐ ๋ฉ์์ง์ ์ ๊ณตํ์ธ์. ์ด ๊ตฌ์ฑ ์์๋ Intelยฎ oneAPI HPC Toolkit์ ์ผ๋ถ์ ๋๋ค. | |
| oneccl_bindings_for_pytorch๋ MPI ๋๊ตฌ ์ธํธ์ ํจ๊ป ์ค์น๋ฉ๋๋ค. ์ฌ์ฉํ๊ธฐ ์ ์ ํ๊ฒฝ์ ์์ค๋ก ์ง์ ํด์ผ ํฉ๋๋ค. | |
| Intelยฎ oneCCL ๋ฒ์ 1.12.0 ์ด์์ธ ๊ฒฝ์ฐ | |
| ```bash | |
| oneccl_bindings_for_pytorch_path=$(python -c "from oneccl_bindings_for_pytorch import cwd; print(cwd)") | |
| source $oneccl_bindings_for_pytorch_path/env/setvars.sh | |
| ``` | |
| Intelยฎ oneCCL ๋ฒ์ ์ด 1.12.0 ๋ฏธ๋ง์ธ ๊ฒฝ์ฐ | |
| ```bash | |
| torch_ccl_path=$(python -c "import torch; import torch_ccl; import os; print(os.path.abspath(os.path.dirname(torch_ccl.__file__)))") | |
| source $torch_ccl_path/env/setvars.sh | |
| ``` | |
| #### IPEX ์ค์น: [[ipex-installation]] | |
| IPEX๋ Float32์ BFloat16์ ๋ชจ๋ ์ฌ์ฉํ๋ CPU ํ๋ จ์ ์ํ ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ ๊ณตํฉ๋๋ค. [single CPU section](./perf_train_cpu)์ ์ฐธ์กฐํ์ธ์. | |
| ์ด์ด์ ๋์ค๋ "Trainer์์์ ์ฌ์ฉ"์ Intelยฎ MPI ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ mpirun์ ์๋ก ๋ค์์ต๋๋ค. | |
| ## Trainer์์์ ์ฌ์ฉ [[usage-in-trainer]] | |
| Trainer์์ ccl ๋ฐฑ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉํฐ CPU ๋ถ์ฐ ํ๋ จ์ ํ์ฑํํ๋ ค๋ฉด ๋ช ๋ น ์ธ์์ **`--ddp_backend ccl`**์ ์ถ๊ฐํด์ผ ํฉ๋๋ค. | |
| [์ง์ ์๋ต ์์ ](https://github.com/huggingface/transformers/tree/main/examples/pytorch/question-answering)๋ฅผ ์ฌ์ฉํ ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. | |
| ๋ค์ ๋ช ๋ น์ ํ Xeon ๋ ธ๋์์ 2๊ฐ์ ํ๋ก์ธ์ค๋ก ํ๋ จ์ ํ์ฑํํ๋ฉฐ, ๊ฐ ์์ผ๋น ํ๋์ ํ๋ก์ธ์ค๊ฐ ์คํ๋ฉ๋๋ค. OMP_NUM_THREADS/CCL_WORKER_COUNT ๋ณ์๋ ์ต์ ์ ์ฑ๋ฅ์ ์ํด ์กฐ์ ํ ์ ์์ต๋๋ค. | |
| ```shell script | |
| export CCL_WORKER_COUNT=1 | |
| export MASTER_ADDR=127.0.0.1 | |
| mpirun -n 2 -genv OMP_NUM_THREADS=23 \ | |
| python3 run_qa.py \ | |
| --model_name_or_path google-bert/bert-large-uncased \ | |
| --dataset_name squad \ | |
| --do_train \ | |
| --do_eval \ | |
| --per_device_train_batch_size 12 \ | |
| --learning_rate 3e-5 \ | |
| --num_train_epochs 2 \ | |
| --max_seq_length 384 \ | |
| --doc_stride 128 \ | |
| --output_dir /tmp/debug_squad/ \ | |
| --no_cuda \ | |
| --ddp_backend ccl \ | |
| --use_ipex | |
| ``` | |
| ๋ค์ ๋ช ๋ น์ ๋ ๊ฐ์ Xeon(๋ ธ๋0 ๋ฐ ๋ ธ๋1, ์ฃผ ํ๋ก์ธ์ค๋ก ๋ ธ๋0์ ์ฌ์ฉ)์์ ์ด 4๊ฐ์ ํ๋ก์ธ์ค๋ก ํ๋ จ์ ํ์ฑํํ๋ฉฐ, ๊ฐ ์์ผ๋น ํ๋์ ํ๋ก์ธ์ค๊ฐ ์คํ๋ฉ๋๋ค. OMP_NUM_THREADS/CCL_WORKER_COUNT ๋ณ์๋ ์ต์ ์ ์ฑ๋ฅ์ ์ํด ์กฐ์ ํ ์ ์์ต๋๋ค. | |
| ๋ ธ๋0์์๋ ๊ฐ ๋ ธ๋์ IP ์ฃผ์๋ฅผ ํฌํจํ๋ ๊ตฌ์ฑ ํ์ผ(์: hostfile)์ ์์ฑํ๊ณ ํด๋น ๊ตฌ์ฑ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ธ์๋ก ์ ๋ฌํด์ผ ํฉ๋๋ค. | |
| ```shell script | |
| cat hostfile | |
| xxx.xxx.xxx.xxx #node0 ip | |
| xxx.xxx.xxx.xxx #node1 ip | |
| ``` | |
| ์ด์ ๋ ธ๋0์์ ๋ค์ ๋ช ๋ น์ ์คํํ๋ฉด **4DDP**๊ฐ ๋ ธ๋0 ๋ฐ ๋ ธ๋1์์ BF16 ์๋ ํผํฉ ์ ๋ฐ๋๋ก ํ์ฑํ๋ฉ๋๋ค. | |
| ```shell script | |
| export CCL_WORKER_COUNT=1 | |
| export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip | |
| mpirun -f hostfile -n 4 -ppn 2 \ | |
| -genv OMP_NUM_THREADS=23 \ | |
| python3 run_qa.py \ | |
| --model_name_or_path google-bert/bert-large-uncased \ | |
| --dataset_name squad \ | |
| --do_train \ | |
| --do_eval \ | |
| --per_device_train_batch_size 12 \ | |
| --learning_rate 3e-5 \ | |
| --num_train_epochs 2 \ | |
| --max_seq_length 384 \ | |
| --doc_stride 128 \ | |
| --output_dir /tmp/debug_squad/ \ | |
| --no_cuda \ | |
| --ddp_backend ccl \ | |
| --use_ipex \ | |
| --bf16 | |
| ``` | |