Instructions to use AbdulElahGwaith/transformers with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AbdulElahGwaith/transformers with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("AbdulElahGwaith/transformers", device_map="auto") - Notebooks
- Google Colab
- Kaggle
|
Download docs/source/ko/perf_infer_gpu_multi.md from AbdulElahGwaith/transformers: direct link, hf CLI and curl.
- Browser
- Download file 18.3 kB
-
https://huggingface.co/AbdulElahGwaith/transformers/resolve/main/docs/source/ko/perf_infer_gpu_multi.md
- Command line
-
hf download hf://AbdulElahGwaith/transformers/docs/source/ko/perf_infer_gpu_multi.md
-
curl -L -o perf_infer_gpu_multi.md https://huggingface.co/AbdulElahGwaith/transformers/resolve/main/docs/source/ko/perf_infer_gpu_multi.md
18.3 kB
| <!--Copyright 2024 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. | |
| --> | |
| # ๋ถ์ฐ ์ถ๋ก [[distributed-inference]] | |
| ๋ชจ๋ธ์ด ๋จ์ผ GPU์ ์ฌ๋ผ๊ฐ์ง ์๋ ๊ฒฝ์ฐ, [ํ ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ](./perf_train_gpu_many#tensor-parallelism)๋ฅผ ์ฌ์ฉํ ๋ถ์ฐ ์ถ๋ก ์ด ๋์์ด ๋ ์ ์์ต๋๋ค. ํ ์ ๋ณ๋ ฌํ๋ ๋ชจ๋ธ์ ์ฌ๋ฌ ๊ฐ์๊ธฐ(CUDA GPU, Intel XPU ๋ฑ)์ ๋ถํ ํ์ฌ ํ๋ ฌ ๊ณฑ์ ๊ณผ ๊ฐ์ ๊ณ์ฐ์ ๋ณ๋ ฌํํฉ๋๋ค. ์ด๋ฅผ ํตํด ๋ ํฐ ๋ชจ๋ธ์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆด ์ ์์ผ๋ฉฐ, ๊ฐ ๊ฐ์๊ธฐ๊ฐ ํ ์์ ์ผ๋ถ๋ฅผ ์ฒ๋ฆฌํ๋ฏ๋ก ์ถ๋ก ์๋๊ฐ ํฅ์๋ฉ๋๋ค. | |
| ๊ทธ๋ฌ๋ ํ ์ ๋ณ๋ ฌํ๋ ํต์ ์ค๋ฒํค๋๋ฅผ ๋ฐ์์ํค๋ฏ๋ก, ๋น ๋ฅธ ๋ ธ๋ ๋ด ํต์ ์ ํ์ฉํ ์ ์๋ ๋ค์ค ๊ฐ์๊ธฐ ํ๊ฒฝ์์ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฅ ํจ๊ณผ์ ์ ๋๋ค. ๋ค์ค ๋ ธ๋ ํ์ต ํ๊ฒฝ์์๋ ์ฌ์ฉ ์ฌ๋ก์ ๋ฐ๋ผ ํ์ดํ๋ผ์ธ ๋ณ๋ ฌํ๋ ๋ฐ์ดํฐ ๋ณ๋ ฌํ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์ผ ์ ์์ต๋๋ค. | |
| > [!TIP] | |
| > ํ ์ ๋ณ๋ ฌํ์ ๋ํด ๋ ์์ธํ ์์๋ณด๋ ค๋ฉด [Ultra-Scale Playbook](https://huggingface.co/spaces/nanotron/ultrascale-playbook?section=tensor_parallelism)์ ํ ์ ๋ณ๋ ฌํ ์น์ ์ ์ฐธ์กฐํ์ธ์. | |
| ์๋ ๋ชฉ๋ก์์ ํ ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ํ๋ ๋ชจ๋ธ์ ํ์ธํ ์ ์์ต๋๋ค. ์๋ก์ด ๋ชจ๋ธ์ ๋ํ ์ง์์ ์ถ๊ฐํ๋ ค๋ฉด GitHub ์ด์๋ ํ ๋ฆฌํ์คํธ๋ฅผ ์ด์ด์ฃผ์ธ์. | |
| <details> | |
| <summary>์ง์๋๋ ๋ชจ๋ธ ๋ณด๊ธฐ</summary> | |
| * [Cohere](./model_doc/cohere) ๋ฐ [Cohere 2](./model_doc/cohere2) | |
| * [Gemma](./model_doc/gemma) ๋ฐ [Gemma 2](./model_doc/gemma2) | |
| * [GLM](./model_doc/glm) | |
| * [Granite](./model_doc/granite) | |
| * [Llama](./model_doc/llama) | |
| * [Mistral](./model_doc/mistral) | |
| * [Mixtral](./model_doc/mixtral) | |
| * [OLMo](./model_doc/olmo) ๋ฐ [OLMo2](./model_doc/olmo2) | |
| * [Phi](./model_doc/phi) ๋ฐ [Phi-3](./model_doc/phi3) | |
| * [Qwen2](./model_doc/qwen2), [Qwen2Moe](./model_doc/qwen2_moe), ๋ฐ [Qwen2-VL](./model_doc/qwen2_5_vl) | |
| * [Starcoder2](./model_doc/starcoder2) | |
| </details> | |
| ์ด ๊ฐ์ด๋๋ Transformers์์ ๋ค์ํ ๋ถํ ์ ๋ต์ ์ฌ์ฉํ์ฌ ํ ์ ๋ณ๋ ฌํ๋ฅผ ํ์ฑํํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํฉ๋๋ค. | |
| ## ๋ชจ๋ธ ๋ถํ [[partitioning-a-model]] | |
| Transformers๋ `tp_plan`๋งค๊ฐ๋ณ์๋ฅผ ํ์ฉํ ์ ์๋ ๋ชจ๋ธ์ ๋ํด ํ ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ฅผ ์ง์ํฉ๋๋ค. ๋ชจ๋ธ ๋ถํ ๋ฐฉ์์ ๋ ๊ฐ์ง๊ฐ ์์ต๋๋ค. | |
| - `auto` ํ ์ ๋ณ๋ ฌํ ๊ณํ์ ์ฌ์ ์ ์๋ ๊ตฌ์ฑ์ ๊ธฐ๋ฐ์ผ๋ก ๋ชจ๋ธ(์์ ์ธ๊ธ๋ ์ง์ ๋ชจ๋ธ)์ ์๋์ผ๋ก ๋ถํ ํฉ๋๋ค. | |
| - ์ฌ์ฉ์ ์ง์ ๋ถํ ๊ณํ์ ์ง์ ์ ์ํ์ฌ [~PreTrainedModel.from_pretrained] ๋ฉ์๋์ `tp_plan` ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ์ ์์ต๋๋ค. | |
| <hfoptions id="sharding"> | |
| <hfoption id="auto plan"> | |
| ```py | |
| import os | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| # model_id = "meta-llama/Llama-4-Scout-17B-16E-Instruct" # ๋ชจ๋ ๊ฐ๋ฅํ ์ ๋ต์ ์๊ฐํํ๊ธฐ์ ๋ ์ข์ | |
| model_id = "meta-llama/Meta-Llama-3-8B-Instruct" # ์ ์ ์์ GPU์ ๋ ์ข์ | |
| model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, tp_plan="auto") | |
| print(model._tp_plan) | |
| tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct") | |
| prompt = "Can I help" | |
| inputs = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device) | |
| # ๋ถ์ฐ ์คํ | |
| outputs = model(inputs) | |
| ``` | |
| ์์ ์ถ๋ก ์คํฌ๋ฆฝํธ๋ฅผ GPU๋น 4๊ฐ ํ๋ก์ธ์ค๋ก [torchrun](https://pytorch.org/docs/stable/elastic/run.html)์์ ์คํํ์ธ์. | |
| ```bash | |
| torchrun --nproc-per-node 4 demo.py | |
| ``` | |
| </hfoption> | |
| <hfoption id="manual plan"> | |
| ๊ฐ ๋ ์ด์ด์ ๋ํ ํ ์ ๋ณ๋ ฌ ๊ณํ์ `tp_plan`์ ์ ์ํ ํ [`~PreTrainedModel.from_pretrained`]์ ์ ๋ฌํ์ธ์. ์๋ ์์๋ ์ด ๋ฐ ํ ๋ถํ ์ ์กฐํฉํ์ฌ ์ฌ์ฉํฉ๋๋ค. ์ง์๋๋ ๋ค๋ฅธ ๋ถํ ์ ๋ต์ [๋ถํ ์ ๋ต](#partitioning-strategies) ์น์ ์ ์ฐธ๊ณ ํ์ธ์. | |
| > [!WARNING] | |
| > ์ฌ์ฉ์ ์ง์ ๋ถํ ๊ณํ์ ์๋์ผ๋ก ์ง์ ํ๋ ค๋ฉด ๋ชจ๋ธ ์ํคํ ์ฒ์ ๋ถํ ์ ๋ต์ด ํจ๊ป ์ํธ ์์ฉํ๋ ๋ฐฉ์์ ๋ํ ์ถฉ๋ถํ ์ดํด๊ฐ ํ์ํฉ๋๋ค. ๋ถํ ์ ๋ต์ ์๋ชป ์ค์ ํ๋ฉด ๋ชจ๋ธ์ด ๋งค์ฐ ๋๋ ค์ง๊ฑฐ๋, ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ฑฐ๋, ๋ถ์ ํํ ๊ฒฐ๊ณผ๋ฅผ ๋ผ ์ ์์ต๋๋ค. ์์ธํ ์์๋ณด๋ ค๋ฉด [Ultra-Scale Playbook](https://huggingface.co/spaces/nanotron/ultrascale-playbook?section=tensor_parallelism)์ ์ฐธ๊ณ ํ์ธ์. | |
| ```py | |
| from transformers import AutoModelForCausalLM | |
| tp_plan = { | |
| "model.layers.*.self_attn.q_proj": "colwise", | |
| "model.layers.*.self_attn.k_proj": "colwise", | |
| "model.layers.*.self_attn.v_proj": "colwise", | |
| "model.layers.*.self_attn.o_proj": "rowwise", | |
| ... | |
| } | |
| model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, tp_plan=tp_plan) | |
| print(model._tp_plan) | |
| ``` | |
| </hfoption> | |
| </hfoptions> | |
| ## ๋ถํ ์ ๋ต[[partitioning-strategies]] | |
| ๋ชจ๋ ๋ถํ ์ ๋ต์ ๋ฌธ์์ด์ ์ ๋ต ๊ตฌํ์ ๋งคํํ๋ [`ParallelInterface`] ํด๋์ค์์ ์ ์๋ฉ๋๋ค. ๋ชจ๋ ์ ๋ต์ [`~PreTrainedModel.from_pretrained`]์ `tp_plan`์ ํตํด ์ค์ ๋๋ฏ๋ก ์ด ํด๋์ค์ ์ง์ ์ํธ ์์ฉํ ํ์๋ ์์ง๋ง, ์ด๋ค ์ ๋ต์ ์ฌ์ฉํ ์ ์๋์ง ํ์ธํ ๋ ์ ์ฉํฉ๋๋ค. | |
| ```py | |
| class ParallelInterface(MutableMapping): | |
| """ | |
| ํ์ฉ๋ ์ดํ ์ ํจ์๋ฅผ ์ถ์ ํ๋ ๋์ ๋๋ฆฌ ๊ฐ์ ๊ฐ์ฒด์ ๋๋ค. `register()` ํธ์ถ๋ก ์๋ก์ด ์ดํ ์ ํจ์๋ฅผ ์ฝ๊ฒ ์ถ๊ฐํ ์ ์์ต๋๋ค. | |
| ๋ชจ๋ธ์ด ๊ธฐ์กด ์ดํ ์ ํจ์(์: `sdpa`)๋ฅผ ๋ก์ปฌ์์ ๋ฎ์ด์ฐ๋ ค๋ฉด `modeling_<model>.py` ๋ด๋ถ์์ ์ด ํด๋์ค์ ์ ์ธ์คํด์ค๋ฅผ ์ ์ธํ๊ณ | |
| ํด๋น ์ธ์คํด์ค์์ ์ ์ธํด์ผ ํฉ๋๋ค. | |
| """ | |
| _global_mapping = { | |
| "colwise": ColwiseParallel(), | |
| "rowwise": RowwiseParallel(), | |
| "colwise_rep": ColwiseParallel(output_layouts=Replicate()), | |
| "rowwise_rep": RowwiseParallel(input_layouts=Replicate()), | |
| "local_colwise": ColwiseParallel(use_dtensor=False), | |
| "local_rowwise": RowwiseParallel(use_dtensor=False), | |
| "local": IsolatedParallel(), | |
| "gather": GatherParallel(), | |
| "local_packed_rowwise": PackedRowwiseParallel(use_dtensor=False), | |
| "sequence_parallel": SequenceParallel(), | |
| "replicate": ReplicateParallel(), | |
| } | |
| ``` | |
| ๊ฐ ์ ๋ต์ ๋ํด ์์ธํ ์์๋ณด๋ ค๋ฉด ์๋ ํ๋ฅผ ์ฐธ๊ณ ํ์ธ์. | |
| | ์ ๋ต | ์ค๋ช | | |
| |---|---| | |
| | `ColwiseParallel` | ๊ฐ์ค์น์ ํธํฅ์ ์ด ๋ฐฉํฅ ๋ถํ . | | |
| | `RowwiseParallel` | ๊ฐ์ค์น์ ํธํฅ์ ํ ๋ฐฉํฅ ๋ถํ . `nn.Embedding` ๋ชจ๋ ๋ถํ ๋ ์ง์. | | |
| | `SequenceParallel` | `LayerNorm`๊ณผ `Dropout` ๋ ์ด์ด๋ฅผ ์ง์ํ๋ ์ํ์ค ๋ณ๋ ฌ ๊ตฌํ. [RMSNorm](https://github.com/facebookresearch/llama/blob/main/llama/model.py#L34)์ Python ๊ตฌํ๋ ์ง์. | | |
| | `PackedColwiseParallel` | ํจํน๋ ๊ฐ์ค์น๋ฅผ ์ง์ํ๋ `ColwiseParallel`์ ๋ณํ(์: `up_proj`์ `gate_proj`๋ฅผ ํจ๊ป ํจํน). ์์ธํ ๋ด์ฉ์ [์ฝ๋](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/tensor_parallel.py#L79-#L108)๋ฅผ ์ฐธ์กฐํ์ธ์. | | |
| | `PackedRowwiseParallel` | ํจํน๋ ๊ฐ์ค์น๋ฅผ ์ง์ํ๋ `RowwiseParallel`์ ๋ณํ([์ฝ๋](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/tensor_parallel.py#L79-#L108) ์ฐธ์กฐ). | | |
| | `GatherParallel` | ๊ธฐ๊ธฐ ๊ฐ ๋ชจ๋์ ์ถ๋ ฅ์ ์์ง. | | |
| | `IsolatedParallel` | Mixture-of-Experts(MoE) ๋ ์ด์ด์ ์ ๋ฌธ๊ฐ์ ์ฌ์ฉ๋์ด ๋ค๋ฅธ ๊ธฐ๊ธฐ๋ก๋ถํฐ ๋ชจ๋์ ๊ฒฉ๋ฆฌ. | | |
| | `ReplicateParallel` | ๋ถ๋ถ์ ์ผ๋ก ๋ถํ ๋ ๋ชจ๋ธ๋ก ์ธํด `torch.distributed` API๊ฐ ์ค๋จ๋๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํด ๋ชจ๋ ๊ธฐ๊ธฐ์ ๋ชจ๋์ ๋ณต์ . | | |
| ### ํจํน๋ ์ ๋ต[[packed-strategies]] | |
| ๊ฐ์ค์น ํจํน์ ์ฌ๋ฌ ์ ํ ๋ ์ด์ด๋ฅผ ํ๋์ ๋ ํฐ ๋ ์ด์ด๋ก ํฉ์น๋ ๊ธฐ๋ฒ์ ๋๋ค. ํจํน๋ ์ ๋ต์ธ `PackedColwiseParallel`๊ณผ `PackedRowwiseParallel`์ ํจํน๋ ๊ฐ์ค์น๋ฅผ ๋ถํ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ๊ธฐ๋ณธ์ ์ธ `ColwiseParallel`์ด๋ `RowwiseParallel`์ ํจํน๋ ๊ฐ์ค์น๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๋ถํ ํ์ง ๋ชปํฉ๋๋ค. | |
| ์๋ ์์๋ `up_proj`์ `gate_proj`๋ฅผ ๋จ์ผ `gate_up_proj` ๋ชจ๋๋ก ํจํนํ๊ณ `gate_up_proj`๋ฅผ ๋ถํ ํ๊ธฐ ์ํด `PackedRowwiseParallel` ์ ๋ต์ด ํ์ํฉ๋๋ค. | |
| ```python | |
| class Llama4TextExperts(nn.Module): | |
| ... | |
| self.gate_up_proj = nn.Parameter(torch.zeros(self.num_experts, self.hidden_size, 2 * self.expert_dim)) | |
| ``` | |
| ๋ฐฐ์น ํ๋ ฌ ๊ณฑ์ ์ `forward` ํจ์ค์์ ์ฌ์ฉํ์ฌ `gate_up_proj` ๋ชจ๋์ ์ถ๋ ฅ์ ๊ณ์ฐํ ์ ์์ต๋๋ค. | |
| ```python | |
| def forward(self, hidden_states): | |
| ... | |
| gate_up = torch.bmm(hidden_states, self.gate_up_proj) # gate_up_proj ๋ชจ๋์ ์ถ๋ ฅ ๊ณ์ฐ | |
| gate, up = gate_up.chunk(2, dim=-1) # ์ถ๋ ฅ์ gate์ up์ผ๋ก ๋ถํ | |
| ``` | |
| > [!TIP] | |
| > `Packed*`๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ์ด์ ์ ๋ํ ์๊ฐ์ ํํ์ [์ด ์ฃผ์](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/tensor_parallel.py#L79-#L108)์ ์ฐธ๊ณ ํ์ธ์. | |
| ### ๋ก์ปฌ ์ ๋ต[[local-strategies]] | |
| ๋ก์ปฌ ์ ๋ต(`local_colwise`, `local_rowwise`, `local_packed_rowwise`)์ [torch.chunk](https://docs.pytorch.org/docs/stable/generated/torch.chunk.html)์ ๊ฐ์ ์ผ๋ถ ์ฐ์ฐ์์ ์ง์๋์ง ์๊ธฐ ๋๋ฌธ์ [DTensor](https://docs.pytorch.org/docs/stable/distributed.tensor.html)๋ฅผ ์ฌ์ฉํ์ง ์์ต๋๋ค. ๋์ ๋ก์ปฌ ์ ๋ต์ ๊ธฐ๋ณธ [torch.Tensor](https://docs.pytorch.org/docs/stable/tensors.html)๋ฅผ ์ฌ์ฉํ๊ณ ์ผ๋ถ ๋ถ์ฐ ๋ก์ง์ ์๋์ผ๋ก ์ํํฉ๋๋ค. | |
| <!-- | |
| Readd this when I get the exact error message | |
| > [!TIP] | |
| > ์ฌ์ฉ์ ์ ์ ๋ถํ ์ ๋ต์ ์ฌ์ฉํ๋๋ฐ `... is not supported` ์ค๋ฅ๋ก ์๋ํ์ง ์๋ ๊ฒฝ์ฐ, `local*` ์ ๋ต์ ์ฌ์ฉํด์ ๋ ์ ์๋ํ๋์ง ์๋ํด๋ณด์ธ์. | |
| --> | |
| ## ์ฌ์ฉ์ ์ ์ ๋ถํ ์ ๋ต[[custom-partitioning-strategies]] | |
| ์ฌ์ฉ์ ์ ์ ๋ถํ ์ ๋ต์ [`TensorParallelLayer`](https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/tensor_parallel.py)๋ฅผ ์์ํ๊ณ `partition_tensor`, `_prepare_input_fn`, `_prepare_output_fn`์ ๊ตฌํํด์ผ ํฉ๋๋ค. | |
| ๊ทธ๋ฐ ๋ค์ `tp_plan`์์ ํด๋น ์ ๋ต์ ์ง์ ํ์ ๋ ๋์คํจ์นญ ๋ก์ง์ด ์ฐพ์ ์ ์๋๋ก `ParallelInterface` ๋งคํ์ ๋ฑ๋กํด์ผ ํฉ๋๋ค. | |
| ์๋ ์์๋ ์ด ์ํฌํ๋ก์ฐ๋ก `ColwiseParallel`์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. | |
| 1. `TensorParallelLayer`๋ฅผ ์์ํฉ๋๋ค. `__init__` ๋ฉ์๋์์ ์ ๋ ฅ ๋ฐ ์ถ๋ ฅ ํ ์๊ฐ ๊ธฐ๊ธฐ์ ์ด๋ป๊ฒ ๋ฐฐ์น๋์ด์ผ ํ๋์ง ์ค๋ช ํ๋ `input_layouts`๊ณผ `output_layouts`์ ์ ์ํฉ๋๋ค. `desired_input_layouts` ์์ฑ์ ์ ๋ ฅ์ด ๊ธฐ๊ธฐ์ ์ด๋ป๊ฒ ๋ฐฐ์น*๋์ด์ผ๋ง* ํ๋์ง๋ฅผ ๋ช ์ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. | |
| ```python | |
| class ColwiseParallel(TensorParallelLayer): | |
| def __init__( | |
| self, | |
| *, | |
| input_layouts: Optional[Placement] = None, # ์ด์ ๋ ์ด์ด์์ ์ค๋ ์ ๋ ฅ ๋ ์ด์์ | |
| output_layouts: Optional[Placement] = None, # ๋ฌ์ฑํ๊ณ ์ ํ๋ ์ถ๋ ฅ ๋ ์ด์์ | |
| use_local_output: bool = True, # ๋ก์ปฌ ์ถ๋ ฅ ์ฌ์ฉ ์ฌ๋ถ | |
| use_dtensor=True, # DTensor ์ฌ์ฉ ์ฌ๋ถ | |
| ): | |
| self.input_layouts = (input_layouts or Replicate(),) # ์ด์ ๋ ์ด์ด์์ ์ค๋ ์ ๋ ฅ ๋ถํ | |
| self.output_layouts = (output_layouts or Shard(-1),) # ์ํ๋ ์ถ๋ ฅ ๋ถํ | |
| self.desired_input_layouts = (Replicate(),) # ์ํ๋ ์ ๋ ฅ ๋ถํ , ์ ๋ ฅ์ GPU ๊ฐ์ ๋ณต์ ๋์ด์ผ ํจ | |
| self.use_local_output = use_local_output | |
| self.use_dtensor = use_dtensor | |
| ``` | |
| 2. `partition_tensor`, `_prepare_input_fn`, `_prepare_output_fn` ๋ฉ์๋๋ฅผ ๊ตฌํํฉ๋๋ค. | |
| `partition_tensor` ๋ฉ์๋๋ ํ ์๋ฅผ ๋ถํ ํ๊ณ ๋ถํ ๋ ํ ์๋ก `empty_param`์ ์ฑ์๋๋ค. ์ ํธ๋ฆฌํฐ ํจ์ `get_tensor_shard`๋ฅผ ์ฌ์ฉํ์ฌ ์ฃผ์ด์ง ๋ญํฌ์ ๋ํ ์๋ณธ ๋งค๊ฐ๋ณ์์ ์ฌ๋ฐ๋ฅธ ๋ถํ ์ ์ป๊ณ , ํจํน๋ ๊ฐ์ค์น์ ๋ํด์๋ `get_packed_weights`๋ฅผ ์ฌ์ฉํ์ธ์. | |
| ```python | |
| def partition_tensor( | |
| self, | |
| param, # ๋งค๊ฐ๋ณ์์ ์ ์ฒด ํ ์ | |
| empty_param, # ๋งค๊ฐ๋ณ์์ ๋น ํ ์, ๋ถํ ๋ ํ ์๋ก ์ฑ์์ง | |
| param_type, # ๋งค๊ฐ๋ณ์ ์ ํ, `bias` ๋๋ `weight` | |
| param_casting_dtype, # ๋งค๊ฐ๋ณ์๋ฅผ ์บ์คํ ํ ์ ํ | |
| to_contiguous, # ํ ์๋ฅผ ์ฐ์์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๋ ์ด์์์ผ๋ก ๋ณํํ ์ง ์ฌ๋ถ | |
| rank, # ํ์ฌ ๊ธฐ๊ธฐ์ ๋ญํฌ | |
| device_mesh, # ๊ธฐ๊ธฐ ๋ฉ์ | |
| ) -> nn.Parameter: # ๋ถํ ๋ ๋งค๊ฐ๋ณ์ ๋ฐํ | |
| ... | |
| ``` | |
| `_prepare_input_fn`๊ณผ `_prepare_output_fn` ๋ฉ์๋๋ [์ฌ์ ํฌ์๋](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_pre_hook.html) ๋ฐ [ํฌ์๋](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.module.register_module_forward_hook.html) ํ ์์ ์ฌ์ฉ๋ฉ๋๋ค. `__init__`์์ ์ง์ ๋ ๋๋ก ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ ์ํ๋ ๋ ์ด์์์ผ๋ก ์ฌ๋ถ๋ฐฐํฉ๋๋ค. | |
| ```python | |
| def _prepare_input_fn(input_layouts, desired_input_layouts, mod, inputs, device_mesh): | |
| ... | |
| # ์ฌ์ฉ์ ์ ์ ๋ก์ง ์ํ, DTensor๋ก ์บ์คํ ๋ฑ. | |
| ... | |
| return inputs.redistribute(placements=desired_input_layouts, device_mesh=device_mesh) | |
| def _prepare_output_fn(output_layouts, use_local_output, mod, outputs, device_mesh): | |
| ... | |
| # ์ฌ์ฉ์ ์ ์ ๋ก์ง ์ํ, DTensor๋ก ์บ์คํ ๋ฑ. | |
| ... | |
| return outputs.redistribute(placements=output_layouts, device_mesh=device_mesh) | |
| ``` | |
| 3. `tp_plan`๊ณผ ํจ๊ป ์ฌ์ฉํ ์ ์๋๋ก ์ ๋ต์ [`ParallelInterface`]์ ๋ฑ๋กํฉ๋๋ค. | |
| ```python | |
| from transformers.integrations.tensor_parallel import ParallelInterface | |
| ParallelInterface.register_strategy("colwise_custom", ColwiseParallel) | |
| tp_plan = { | |
| "model.layers.*.self_attn.q_proj": "colwise_custom", | |
| ... | |
| } | |
| model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, tp_plan=tp_plan) | |
| ``` | |
| ## ๋ฒค์น๋งํฌ[[benchmarks]] | |
| ํ ์ ๋ณ๋ ฌํ๋ ํนํ ํฐ ๋ฐฐ์น ํฌ๊ธฐ๋ ๊ธด ์ํ์ค๋ฅผ ๊ฐ์ง ์ ๋ ฅ์ ๋ํ ์ถ๋ก ์๋๋ฅผ ํฌ๊ฒ ํฅ์์ํฌ ์ ์์ต๋๋ค. | |
| ์ํ์ค ๊ธธ์ด๊ฐ 512์ธ [Llama](./model_doc/llama)์์ ๋จ์ผ ํฌ์๋ ํจ์ค์ ๋ํ ์์ ์๋ ํฅ์ ์์น๋ ์๋ ์ฐจํธ๋ฅผ ์ฐธ์กฐํ์ธ์. | |
| <div style="text-align: center"> | |
| <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/Meta-Llama-3-8B-Instruct%2C%20seqlen%20%3D%20512%2C%20python%2C%20w_%20compile.png"> | |
| </div> | |
| ## ์ค๊ณ ๊ตฌํ[[design-implementation]] | |
| Transformers ํ ์ ๋ณ๋ ฌํ ๊ตฌํ์ ํ๋ ์์ํฌ์ ๊ตฌ์ ๋ฐ์ง ์์ง๋ง, ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ ์ํด์๋ [DeviceMesh](https://docs.pytorch.org/tutorials/recipes/distributed_device_mesh.html)์ [torch.distributed](https://docs.pytorch.org/tutorials/beginner/dist_overview.html)์ [DTensor](https://docs.pytorch.org/docs/stable/distributed.tensor.html)์ ์์กดํ์ฌ ๊ฐ๋จํ๊ณ ํ์ฅ ๊ฐ๋ฅํ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํฉ๋๋ค. | |
| ### DeviceMesh[[devicemesh]] | |
| `DeviceMesh`๋ฅผ ํจ๊ป ํต์ ํ๋ ๊ธฐ๊ธฐ๋ค์ ๋ค์ฐจ์ ๊ทธ๋ฆฌ๋๋ก ์์ํด๋ณด์ธ์. ๋ณ๋ ฌ ์ฒ๋ฆฌ ์ ๋ต๋ง๋ค ๊ฐ๊ธฐ ๋ค๋ฅธ ํต์ ํจํด์ด ํ์ํ๋ฏ๋ก, ์ฌ๋ฌ ํ์ ๋ฉ์๋ฅผ ๊ฐ์ง `DeviceMesh`๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค. | |
| ```python | |
| from torch.distributed.device_mesh import init_device_mesh | |
| # 4๊ฐ GPU์ 1D ๋ฉ์ ์์ฑ | |
| device_mesh = init_device_mesh("cuda", (4,), mesh_dim_names=["tp"]) | |
| ``` | |
| `torch.distributed`์์ ์ ์๋ ๋๋ถ๋ถ์ ๋ณ๋ ฌํ ์ ๋ต์ ๋ฉ์ ์์ฒด๋ ํ์ ๋ฉ์์ ์ ์ฉํ ์ ์์ผ๋ฉฐ, ์๋์ผ๋ก ํต์ ํจํด์ ์ฒ๋ฆฌํฉ๋๋ค. | |
| ### DTensor[[dtensor]] | |
| `DTensor`(๋ถ์ฐ ํ ์)๋ ์ผ๋ฐ์ ์ธ ํ ์ ์ฐ์ฐ ์์ ๋ถ์ฐ ๋ก์ง์ ์ฒ๋ฆฌํ๋ ํ ์ ํ์ ํด๋์ค์ ๋๋ค. ํ ์ ๋ณ๋ ฌํ์ ๋๋ถ๋ถ์ ๋ชจ๋ธ ๊ฐ์ค์น๋ `DTensor` ํํ๋ก ์ ์ฅ๋ฉ๋๋ค. | |
| DTensor์ ๊ฐ์ฅ ์ค์ํ ๋ถ๋ถ์ `placement` ์์ฑ์ ๋๋ค. ์ด๋ PyTorch์๊ฒ ํ ์๊ฐ `DeviceMesh`์ ๊ธฐ๊ธฐ์ ์ด๋ป๊ฒ ๋ฐฐ์น๋๋์ง ์๋ ค์ฃผ๊ธฐ ๋๋ฌธ์ ๋๋ค. `placement` ์์ฑ์ ๋ค์ ๊ฐ์ ๊ฐ์ง ์ ์์ต๋๋ค. | |
| - `Shard(dimension)` - `DTensor`๊ฐ ๊ตฌ์ฑ๋ `DeviceMesh`์์ ์ฃผ์ด์ง ์ฐจ์์ ๊ฑธ์ณ ์ด๋ป๊ฒ ๋ถํ ๋๋์ง ๋ํ๋ ๋๋ค. ์๋ ์์๋ ์ด ๋ฐฉํฅ ๋ถํ ์ ์ํด ๋ค์ํ ์ฐจ์์ ๊ฑธ์ณ ๊ฐ์ค์น๋ฅผ ๋ถํ ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. | |
| ```python | |
| weight = ... | |
| weight = DTensor.from_local(weight, device_mesh["tp"], placements=[Shard(0)]) # ์ฒซ ๋ฒ์งธ(์ด ๋ฐฉํฅ) ์ฐจ์์ ๊ฑธ์ณ ๋ถํ | |
| bias = ... | |
| bias = DTensor.from_local(bias, device_mesh["tp"], placements=[Shard(-1)]) # ์ ์ผํ ์ฐจ์์ ๊ฑธ์ณ ๋ถํ | |
| ``` | |
| ์ด ์์๋ ํ ๋ฐฉํฅ ๋ถํ ์ ์ํด ์ฌ๋ฌ ์ฐจ์์ ๊ฑธ์ณ ๊ฐ์ค์น๋ฅผ ๋ถํ ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. | |
| ```python | |
| weight = ... | |
| weight = DTensor.from_local(weight, device_mesh["tp"], placements=[Shard(1)]) # ๋ ๋ฒ์งธ(ํ ๋ฐฉํฅ) ์ฐจ์์ ๊ฑธ์ณ ๋ถํ | |
| bias = ... | |
| bias = DTensor.from_local(bias, device_mesh["tp"], placements=[Replicate()]) # ๋ชจ๋ GPU์ ํธํฅ ๋ณต์ | |
| ``` | |
| - `Replicate()` - `DTensor`๊ฐ `DeviceMesh`์ ๊ฑธ์ณ ๋ณต์ ๋จ์ ๋ํ๋ ๋๋ค. ๊ฐ ๊ธฐ๊ธฐ์ ํ ์์ ์ ์ฒด ์ฌ๋ณธ๋ง ์์ฑํฉ๋๋ค. | |
| ```py | |
| bias = ... | |
| bias = DTensor.from_local(bias, device_mesh["tp"], placements=[Replicate()]) # ๋ชจ๋ GPU์ ํธํฅ ๋ณต์ | |
| ``` | |
| - `Partial()` - ํ ์๊ฐ ๊ฐ์ ์ฐ์ฐ์ ๊ธฐ๋ค๋ฆฌ๊ณ ์๋ ์ํ์์ ๋ํ๋ ๋๋ค (์ผ๋ฐ์ ์ผ๋ก Transformers์์์ ์ฌ์ฉ ์ฌ๋ก์๋ ์ง์ ์ ์ธ ๊ด๋ จ์ด ์ ์ต๋๋ค). | |