Buckets:

mishig's picture
|
download
raw
4.88 kB
# 재현성
Diffusion은 매번 다른 출력을 생성하는 무작위 과정입니다. 테스트 및 결과 재현과 같은 사용 사례에서는 특정 허용 범위 내에서 릴리스와 플랫폼을 불문하고 매번 동일한 결과를 생성하는 것이 필요합니다.
이 가이드에서는 무작위성의 원천을 제어하고 결정론적 알고리즘을 활성화하는 방법을 보여줍니다.
## Generator
Pipelines은 초기 노이즈 텐서를 생성하기 위해 매번 다른 랜덤 시드를 사용하는 [torch.randn](https://pytorch.org/docs/stable/generated/torch.randn.html)에 의존합니다. CPU 또는 GPU에서 동일한 출력을 생성하려면 [Generator](https://docs.pytorch.org/docs/stable/generated/torch.Generator.html)를 사용하여 랜덤 값 생성 방식을 관리하세요.
> [!TIP]
> 재현성이 중요하다면 CPU `Generator`를 사용해야 합니다. 성능 저하는 대부분 무시할 수 있으며 더 유사한 값이 생성됩니다.
재현성이 중요한 경우 CPU `Generator`를 사용하세요. CPU RNG는 머신 간에 더 안정적입니다.
CPU `Generator`를 전달하면 Diffusers의 [randn_tensor()](/docs/diffusers/main/ko/api/utilities#diffusers.utils.torch_utils.randn_tensor)는 CPU에서 샘플링을 수행하고 파이프라인 내부에서 텐서를 GPU로 이동합니다. `randn_tensor`나 `.to("cuda")`를 직접 호출하지 않습니다. GPU `Generator`는 대신 디바이스에서 샘플링을 수행하며 CPU 결과와 달라질 수 있습니다.
시드를 설정하려면 [manual_seed](https://docs.pytorch.org/docs/stable/generated/torch.manual_seed.html)를 사용하세요.
```py
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda" # or "mps", "xpu", "cpu"
)
generator = torch.manual_seed(0)
image = pipeline(
prompt="a red apple on a wooden table",
generator=generator,
num_inference_steps=4,
).images[0]
```
CPU `Generator`를 생성하고 [Generator.manual_seed](https://docs.pytorch.org/docs/stable/generated/torch.Generator.html#torch.Generator.manual_seed)로 시드를 설정합니다.
```py
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cpu"
)
generator = torch.Generator(device="cpu").manual_seed(0)
image = pipeline(
prompt="a red apple on a wooden table",
generator=generator,
num_inference_steps=4,
).images[0]
```
`Generator` 객체를 정수 시드 대신 파이프라인에 전달하세요. `Generator`는 사용 시 소모되고 업데이트되는 무작위 상태를 유지합니다. 이후에는 상태가 변경되었기 때문에, 파이프라인을 넘나들더라도 동일한 객체가 후속 호출에서 다른 결과를 생성합니다. 동일한 시드가 다시 필요할 경우, 각 호출 전에 시드를 재설정하거나 새로운 `Generator`를 생성하세요.
```py
import torch
prompt = "a red apple on a wooden table"
for _ in range(5):
generator = torch.manual_seed(0)
image = pipeline(prompt, generator=generator, num_inference_steps=4).images[0]
```
## 결정론적 알고리즘
PyTorch는 특정 연산에 대해 [결정론적 알고리즘](https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms)(사용 가능한 경우)을 지원하여 동일한 결과를 생성하도록 합니다. 결정론적 알고리즘은 더 느려지고 성능이 저하될 수 있습니다.
Diffusers의 `enable_full_determinism()`를 사용하여 결정론적 알고리즘을 활성화하세요.
```py
from diffusers.utils.torch_utils import enable_full_determinism
enable_full_determinism()
```
`enable_full_determinism`은 다음 방식으로 작동합니다:
- 환경 변수 `CUDA_LAUNCH_BLOCKING`을 `1`(으)로 설정
- cuBLAS가 런타임에서 고정된 워크스페이스 레이아웃을 사용하도록 환경 변수 [CUBLAS_WORKSPACE_CONFIG](https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility)를 `:16:8`으로 설정합니다
- `torch.use_deterministic_algorithms(True)`를 호출합니다
- `torch.backends.cudnn.deterministic = True` 설정
- cuDNN이 실행마다 다른 컨볼루션 알고리즘을 선택하지 않도록 `torch.backends.cudnn.benchmark = False`를 설정합니다
- 더 정확한 전체 정밀도 matmul을 위해 `torch.backends.cuda.matmul.allow_tf32 = False`로 TensorFloat32(TF32)를 비활성화
## 다음 단계
PyTorch 개발자 노트의 [Reproducibility](https://docs.pytorch.org/docs/stable/notes/randomness.html) 섹션을 읽으시는 것을 권장합니다. 무작위성을 제한할 수는 있지만, 동일한 시드를 사용하더라도 *보장*되지 않습니다.

Xet Storage Details

Size:
4.88 kB
·
Xet hash:
e5eec28fe53985917db5600cb5fa154448cb7634ba02c7fd4cdf11fb7742ec59

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