Buckets:

hf-doc-build/doc / diffusers /v0.43.0 /ko /optimization /data_parallelism.md
mishig's picture
|
download
raw
4.46 kB
# 데이터 병렬
[Data parallelism](https://huggingface.co/spaces/nanotron/ultrascale-playbook?section=data_parallelism)은 동일한 모델을 각 GPU에 복제하고 각 GPU에 서로 다른 프롬프트를 할당합니다. 모델이 이미 단일 GPU에 적합할 때 처리량을 높이며, 단일 프롬프트의 지연 시간은 변하지 않습니다. 아직 시도하지 않았다면, 먼저 단일 GPU에서 [Batch inference](../using-diffusers/batched_inference)를 시도해 보세요. 다른 전략과 비교하려면 [Distributed inference](../training/distributed_inference)를 참고하세요.
[Accelerate](https://huggingface.co/docs/accelerate/index) 또는 [PyTorch Distributed](https://pytorch.org/tutorials/beginner/dist_overview.html)를 직접 사용하여 데이터 병렬성을 설정할 수 있습니다.
## Accelerate
Accelerate는 프로세스를 설정하고 각 프로세스에 GPU를 할당합니다.
다음 명령어로 Accelerate를 설치합니다.
```bash
uv pip install accelerate
```
`run_distributed.py`라는 파일에서 [PartialState](https://huggingface.co/docs/accelerate/v1.15.0/en/package_reference/state#accelerate.PartialState)를 생성하여 분산 환경을 설정합니다. [DiffusionPipeline](/docs/diffusers/v0.43.0/ko/api/pipelines/overview#diffusers.DiffusionPipeline)을 `PartialState.device`로 이동하여 각 프로세스에 GPU를 할당합니다.
```py
import torch
from accelerate import PartialState
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16
)
distributed_state = PartialState()
pipeline.to(distributed_state.device)
```
같은 파일에서 [split_between_processes](https://huggingface.co/docs/accelerate/v1.15.0/en/package_reference/state#accelerate.PartialState.split_between_processes)를 컨텍스트 매니저로 사용하여 프롬프트를 프로세스 간에 분할합니다.
```py
with distributed_state.split_between_processes(["a dog", "a cat"]) as prompt:
result = pipeline(prompt).images[0]
result.save(f"result_{distributed_state.process_index}.png")
```
`accelerate launch`로 스크립트를 실행하고 `--num_processes`를 GPU 수로 설정하세요.
```bash
accelerate launch --num_processes=2 run_distributed.py
```
> [!TIP]
> 완전한 예제는 이 [최소 다중 GPU 추론 스크립트](https://gist.github.com/sayakpaul/cfaebd221820d7b43fae638b4dfa01ba)를 참고하세요. 자세한 내용은 [Distributed inference with Accelerate](https://huggingface.co/docs/accelerate/en/usage_guides/distributed_inference#distributed-inference-with-accelerate) 가이드를 참고하세요.
## PyTorch Distributed
Accelerate 없이 `torch.distributed`를 직접 사용하여 데이터 병렬성을 설정할 수도 있습니다.
분산 환경을 생성하려면 [init_process_group](https://pytorch.org/docs/stable/distributed.html#torch.distributed.init_process_group)을 호출합니다. `torchrun`은 환경 변수를 통해 각 프로세스의 rank와 전체 프로세스 수(world size)를 설정하므로, 직접 전달할 필요가 없습니다.
각 프로세스는 자체 GPU와 자체 프롬프트가 필요합니다. `torchrun`은 `LOCAL_RANK` 환경 변수를 해당 노드에서 프로세스의 GPU 인덱스로 설정하므로, 이를 사용하여 GPU를 선택합니다. 모든 노드에 걸쳐 프로세스를 세는 `get_rank`를 사용하여 프롬프트를 선택합니다.
```py
import os
import torch
import torch.distributed as dist
from diffusers import DiffusionPipeline
def main():
dist.init_process_group("nccl")
rank = dist.get_rank()
local_rank = int(os.environ["LOCAL_RANK"])
device = torch.device(f"cuda:{local_rank}")
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16,
).to(device)
prompts = ["a dog", "a cat"]
prompt = prompts[rank]
image = pipeline(prompt).images[0]
image.save(f"{prompt.replace(' ', '_')}.png")
dist.destroy_process_group()
if __name__ == "__main__":
main()
```
`run_distributed.py`로 스크립트를 저장하고 `torchrun`으로 실행합니다. `--nproc-per-node`를 GPU 수로 설정합니다. `prompts` 리스트에는 프로세스당 하나의 프롬프트가 포함되어 있으므로, 사용하는 GPU마다 프롬프트를 추가해야 합니다.
```bash
torchrun --nproc-per-node 2 run_distributed.py
```

Xet Storage Details

Size:
4.46 kB
·
Xet hash:
01a5be9b3af0f05615a8bb6864dfec4f7349aef51b59e585f1e0570afc9a861c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.