File size: 9,350 Bytes
ef4d689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!--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
specific language governing permissions and limitations under the License.
-->

# μž¬ν˜„ κ°€λŠ₯ν•œ νŒŒμ΄ν”„λΌμΈ μƒμ„±ν•˜κΈ°

[[open-in-colab]]

μž¬ν˜„μ„±μ€ ν…ŒμŠ€νŠΈ, κ²°κ³Ό μž¬ν˜„, 그리고 [이미지 퀄리티 높이기](resuing_seeds)μ—μ„œ μ€‘μš”ν•©λ‹ˆλ‹€.
κ·ΈλŸ¬λ‚˜ diffusion λͺ¨λΈμ˜ λ¬΄μž‘μœ„μ„±μ€ 맀번 λͺ¨λΈμ΄ λŒμ•„κ°ˆ λ•Œλ§ˆλ‹€ νŒŒμ΄ν”„λΌμΈμ΄ λ‹€λ₯Έ 이미지λ₯Ό 생성할 수 μžˆλ„λ‘ ν•˜λŠ” 이유둜 ν•„μš”ν•©λ‹ˆλ‹€.
ν”Œλž«νΌ 간에 μ •ν™•ν•˜κ²Œ λ™μΌν•œ κ²°κ³Όλ₯Ό 얻을 μˆ˜λŠ” μ—†μ§€λ§Œ, νŠΉμ • ν—ˆμš© λ²”μœ„ λ‚΄μ—μ„œ 릴리슀 및 ν”Œλž«νΌ 간에 κ²°κ³Όλ₯Ό μž¬ν˜„ν•  μˆ˜λŠ” μžˆμŠ΅λ‹ˆλ‹€.
κ·ΈλŸΌμ—λ„ diffusion νŒŒμ΄ν”„λΌμΈκ³Ό μ²΄ν¬ν¬μΈνŠΈμ— 따라 ν—ˆμš© μ˜€μ°¨κ°€ λ‹¬λΌμ§‘λ‹ˆλ‹€.

diffusion λͺ¨λΈμ—μ„œ λ¬΄μž‘μœ„μ„±μ˜ μ›μ²œμ„ μ œμ–΄ν•˜κ±°λ‚˜ 결정둠적 μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜λŠ” 방법을 μ΄ν•΄ν•˜λŠ” 것이 μ€‘μš”ν•œ μ΄μœ μž…λ‹ˆλ‹€.

<Tip>

πŸ’‘ Pytorch의 [μž¬ν˜„μ„±μ— λŒ€ν•œ μ„ μ–Έ](https://pytorch.org/docs/stable/notes/randomness.html)λ₯Ό κΌ­ 읽어보길 μΆ”μ²œν•©λ‹ˆλ‹€:

> μ™„μ „ν•˜κ²Œ μž¬ν˜„κ°€λŠ₯ν•œ κ²°κ³ΌλŠ” Pytorch 배포, κ°œλ³„μ μΈ 컀밋, ν˜Ήμ€ λ‹€λ₯Έ ν”Œλž«νΌλ“€μ—μ„œ 보μž₯λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
> λ˜ν•œ, κ²°κ³ΌλŠ” CPU와 GPU 싀행간에 심지어 같은 seedλ₯Ό μ‚¬μš©ν•  λ•Œλ„ μž¬ν˜„ κ°€λŠ₯ν•˜μ§€ μ•Šμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

</Tip>

## λ¬΄μž‘μœ„μ„± μ œμ–΄ν•˜κΈ°

μΆ”λ‘ μ—μ„œ, νŒŒμ΄ν”„λΌμΈμ€ λ…Έμ΄μ¦ˆλ₯Ό 쀄이기 μœ„ν•΄ κ°€μš°μ‹œμ•ˆ λ…Έμ΄μ¦ˆλ₯Ό μƒμ„±ν•˜κ±°λ‚˜ μŠ€μΌ€μ€„λ§ 단계에 λ…Έμ΄μ¦ˆλ₯Ό λ”ν•˜λŠ” λ“±μ˜ 랜덀 μƒ˜ν”Œλ§ 싀행에 크게 μ˜μ‘΄ν•©λ‹ˆλ‹€,

[DDIMPipeline](https://huggingface.co/docs/diffusers/v0.18.0/en/api/pipelines/ddim#diffusers.DDIMPipeline)μ—μ„œ 두 μΆ”λ‘  단계 μ΄ν›„μ˜ ν…μ„œ 값을 μ‚΄νŽ΄λ³΄μ„Έμš”:

```python
from diffusers import DDIMPipeline
import numpy as np

model_id = "google/ddpm-cifar10-32"

# λͺ¨λΈκ³Ό μŠ€μΌ€μ€„λŸ¬λ₯Ό 뢈러였기
ddim = DDIMPipeline.from_pretrained(model_id)

# 두 개의 단계에 λŒ€ν•΄μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜κ³  numpy tensor둜 값을 λ°˜ν™˜ν•˜κΈ°
image = ddim(num_inference_steps=2, output_type="np").images
print(np.abs(image).sum())
```

μœ„μ˜ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ ν•˜λ‚˜μ˜ 값이 λ‚˜μ˜€μ§€λ§Œ, λ‹€μ‹œ μ‹€ν–‰ν•˜λ©΄ λ‹€λ₯Έ 값이 λ‚˜μ˜΅λ‹ˆλ‹€. 무슨 일이 μΌμ–΄λ‚˜κ³  μžˆλŠ” κ±ΈκΉŒμš”?

νŒŒμ΄ν”„λΌμΈμ΄ 싀행될 λ•Œλ§ˆλ‹€, [torch.randn](https://pytorch.org/docs/stable/generated/torch.randn.html)은
λ‹¨κ³„μ μœΌλ‘œ λ…Έμ΄μ¦ˆ μ œκ±°λ˜λŠ” κ°€μš°μ‹œμ•ˆ λ…Έμ΄μ¦ˆκ°€ μƒμ„±ν•˜κΈ° μœ„ν•œ λ‹€λ₯Έ 랜덀 seedλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ λ™μΌν•œ 이미지λ₯Ό μ•ˆμ •μ μœΌλ‘œ 생성해야 ν•˜λŠ” κ²½μš°μ—λŠ” CPUμ—μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜λŠ”μ§€ GPUμ—μ„œ μ‹€ν–‰ν•˜λŠ”μ§€μ— 따라 λ‹¬λΌμ§‘λ‹ˆλ‹€.

### CPU

CPUμ—μ„œ μž¬ν˜„ κ°€λŠ₯ν•œ κ²°κ³Όλ₯Ό μƒμ„±ν•˜λ €λ©΄, PyTorch [Generator](https://pytorch.org/docs/stable/generated/torch.randn.html)둜 seedλ₯Ό κ³ μ •ν•©λ‹ˆλ‹€:

```python
import torch
from diffusers import DDIMPipeline
import numpy as np

model_id = "google/ddpm-cifar10-32"

# λͺ¨λΈκ³Ό μŠ€μΌ€μ€„λŸ¬ 뢈러였기
ddim = DDIMPipeline.from_pretrained(model_id)

# μž¬ν˜„μ„±μ„ μœ„ν•΄ generator λ§Œλ“€κΈ°
generator = torch.Generator(device="cpu").manual_seed(0)

# 두 개의 단계에 λŒ€ν•΄μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜κ³  numpy tensor둜 값을 λ°˜ν™˜ν•˜κΈ°
image = ddim(num_inference_steps=2, output_type="np", generator=generator).images
print(np.abs(image).sum())
```

이제 μœ„μ˜ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ seedλ₯Ό 가진 `Generator` 객체가 νŒŒμ΄ν”„λΌμΈμ˜ λͺ¨λ“  랜덀 ν•¨μˆ˜μ— μ „λ‹¬λ˜λ―€λ‘œ 항상 `1491.1711` 값이 좜λ ₯λ©λ‹ˆλ‹€.

νŠΉμ • ν•˜λ“œμ›¨μ–΄ 및 PyTorch λ²„μ „μ—μ„œ 이 μ½”λ“œ 예제λ₯Ό μ‹€ν–‰ν•˜λ©΄ λ™μΌν•˜μ§€λŠ” μ•Šλ”λΌλ„ μœ μ‚¬ν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

<Tip>

πŸ’‘ μ²˜μŒμ—λŠ” μ‹œλ“œλ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜κ°’ λŒ€μ‹ μ— `Generator` 개체λ₯Ό νŒŒμ΄ν”„λΌμΈμ— μ „λ‹¬ν•˜λŠ” 것이 μ•½κ°„ 비직관적일 수 μžˆμ§€λ§Œ,
`Generator`λŠ” 순차적으둜 μ—¬λŸ¬ νŒŒμ΄ν”„λΌμΈμ— 전달될 수 μžˆλŠ” \λžœλ€μƒνƒœ\이기 λ•Œλ¬Έμ— PyTorchμ—μ„œ ν™•λ₯ λ‘ μ  λͺ¨λΈμ„ λ‹€λ£° λ•Œ ꢌμž₯λ˜λŠ” μ„€κ³„μž…λ‹ˆλ‹€.

</Tip>

### GPU

예λ₯Ό λ“€λ©΄, GPU μƒμ—μ„œ 같은 μ½”λ“œ μ˜ˆμ‹œλ₯Ό μ‹€ν–‰ν•˜λ©΄:

```python
import torch
from diffusers import DDIMPipeline
import numpy as np

model_id = "google/ddpm-cifar10-32"

# λͺ¨λΈκ³Ό μŠ€μΌ€μ€„λŸ¬ 뢈러였기
ddim = DDIMPipeline.from_pretrained(model_id)
ddim.to("cuda")

# μž¬ν˜„μ„±μ„ μœ„ν•œ generator λ§Œλ“€κΈ°
generator = torch.Generator(device="cuda").manual_seed(0)

# 두 개의 단계에 λŒ€ν•΄μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜κ³  numpy tensor둜 값을 λ°˜ν™˜ν•˜κΈ°
image = ddim(num_inference_steps=2, output_type="np", generator=generator).images
print(np.abs(image).sum())
```

GPUκ°€ CPU와 λ‹€λ₯Έ λ‚œμˆ˜ 생성기λ₯Ό μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— λ™μΌν•œ μ‹œλ“œλ₯Ό μ‚¬μš©ν•˜λ”λΌλ„ κ²°κ³Όκ°€ 같지 μ•ŠμŠ΅λ‹ˆλ‹€.

이 문제λ₯Ό ν”Όν•˜κΈ° μœ„ν•΄ 🧨 DiffusersλŠ” CPU에 μž„μ˜μ˜ λ…Έμ΄μ¦ˆλ₯Ό μƒμ„±ν•œ λ‹€μŒ ν•„μš”μ— 따라 ν…μ„œλ₯Ό GPU둜 μ΄λ™μ‹œν‚€λŠ”
[randn_tensor()](https://huggingface.co/docs/diffusers/v0.18.0/en/api/utilities#diffusers.utils.randn_tensor)κΈ°λŠ₯을 가지고 μžˆμŠ΅λ‹ˆλ‹€.
`randn_tensor` κΈ°λŠ₯은 νŒŒμ΄ν”„λΌμΈ λ‚΄λΆ€ μ–΄λ””μ—μ„œλ‚˜ μ‚¬μš©λ˜λ―€λ‘œ νŒŒμ΄ν”„λΌμΈμ΄ GPUμ—μ„œ μ‹€ν–‰λ˜λ”λΌλ„ **항상** CPU `Generator`λ₯Ό 톡과할 수 μžˆμŠ΅λ‹ˆλ‹€.

이제 결과에 훨씬 더 λ‹€κ°€μ™”μŠ΅λ‹ˆλ‹€!

```python
import torch
from diffusers import DDIMPipeline
import numpy as np

model_id = "google/ddpm-cifar10-32"

# λͺ¨λΈκ³Ό μŠ€μΌ€μ€„λŸ¬ 뢈러였기
ddim = DDIMPipeline.from_pretrained(model_id)
ddim.to("cuda")

#μž¬ν˜„μ„±μ„ μœ„ν•œ generator λ§Œλ“€κΈ° (GPU에 μ˜¬λ¦¬μ§€ μ•Šλ„λ‘ μ‘°μ‹¬ν•œλ‹€!)
generator = torch.manual_seed(0)

# 두 개의 단계에 λŒ€ν•΄μ„œ νŒŒμ΄ν”„λΌμΈμ„ μ‹€ν–‰ν•˜κ³  numpy tensor둜 값을 λ°˜ν™˜ν•˜κΈ°
image = ddim(num_inference_steps=2, output_type="np", generator=generator).images
print(np.abs(image).sum())
```

<Tip>

πŸ’‘ μž¬ν˜„μ„±μ΄ μ€‘μš”ν•œ κ²½μš°μ—λŠ” 항상 CPU generatorλ₯Ό μ „λ‹¬ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
μ„±λŠ₯ 손싀은 λ¬΄μ‹œν•  수 μ—†λŠ” κ²½μš°κ°€ 많으며 νŒŒμ΄ν”„λΌμΈμ΄ GPUμ—μ„œ μ‹€ν–‰λ˜μ—ˆμ„ λ•Œλ³΄λ‹€ 훨씬 더 λΉ„μŠ·ν•œ 값을 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.

</Tip>

λ§ˆμ§€λ§‰μœΌλ‘œ [UnCLIPPipeline](https://huggingface.co/docs/diffusers/v0.18.0/en/api/pipelines/unclip#diffusers.UnCLIPPipeline)κ³Ό 같은
더 λ³΅μž‘ν•œ νŒŒμ΄ν”„λΌμΈμ˜ 경우, 이듀은 μ’…μ’… μ •λ°€ 였차 μ „νŒŒμ— κ·Ήλ„λ‘œ μ·¨μ•½ν•©λ‹ˆλ‹€.
λ‹€λ₯Έ GPU ν•˜λ“œμ›¨μ–΄ λ˜λŠ” PyTorch λ²„μ „μ—μ„œ μœ μ‚¬ν•œ κ²°κ³Όλ₯Ό κΈ°λŒ€ν•˜μ§€ λ§ˆμ„Έμš”.
이 경우 μ™„μ „ν•œ μž¬ν˜„μ„±μ„ μœ„ν•΄ μ™„μ „νžˆ λ™μΌν•œ ν•˜λ“œμ›¨μ–΄ 및 PyTorch 버전을 μ‹€ν–‰ν•΄μ•Ό ν•©λ‹ˆλ‹€.

## 결정둠적 μ•Œκ³ λ¦¬μ¦˜

결정둠적 μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜μ—¬ μž¬ν˜„ κ°€λŠ₯ν•œ νŒŒμ΄ν”„λΌμΈμ„ μƒμ„±ν•˜λ„λ‘ PyTorchλ₯Ό ꡬ성할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
κ·ΈλŸ¬λ‚˜ 결정둠적 μ•Œκ³ λ¦¬μ¦˜μ€ 비결정둠적 μ•Œκ³ λ¦¬μ¦˜λ³΄λ‹€ 느리고 μ„±λŠ₯이 μ €ν•˜λ  수 μžˆμŠ΅λ‹ˆλ‹€.
ν•˜μ§€λ§Œ μž¬ν˜„μ„±μ΄ μ€‘μš”ν•˜λ‹€λ©΄, 이것이 μ΅œμ„ μ˜ λ°©λ²•μž…λ‹ˆλ‹€!

λ‘˜ μ΄μƒμ˜ CUDA μŠ€νŠΈλ¦Όμ—μ„œ μž‘μ—…μ΄ μ‹œμž‘λ  λ•Œ 비결정둠적 λ™μž‘μ΄ λ°œμƒν•©λ‹ˆλ‹€.
이 문제λ₯Ό λ°©μ§€ν•˜λ €λ©΄ ν™˜κ²½ λ³€μˆ˜ [CUBLAS_WORKSPACE_CONFIG](https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility)λ₯Ό `:16:8`둜 μ„€μ •ν•΄μ„œ
λŸ°νƒ€μž„ 쀑에 였직 ν•˜λ‚˜μ˜ 버퍼 크리만 μ‚¬μš©ν•˜λ„λ‘ μ„€μ •ν•©λ‹ˆλ‹€.

PyTorchλŠ” 일반적으둜 κ°€μž₯ λΉ λ₯Έ μ•Œκ³ λ¦¬μ¦˜μ„ μ„ νƒν•˜κΈ° μœ„ν•΄ μ—¬λŸ¬ μ•Œκ³ λ¦¬μ¦˜μ„ λ²€μΉ˜λ§ˆν‚Ήν•©λ‹ˆλ‹€.
ν•˜μ§€λ§Œ μž¬ν˜„μ„±μ„ μ›ν•˜λŠ” 경우, λ²€μΉ˜λ§ˆν¬κ°€ 맀 μˆœκ°„ λ‹€λ₯Έ μ•Œκ³ λ¦¬μ¦˜μ„ 선택할 수 있기 λ•Œλ¬Έμ— 이 κΈ°λŠ₯을 μ‚¬μš©ν•˜μ§€ μ•Šλ„λ‘ μ„€μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€.
λ§ˆμ§€λ§‰μœΌλ‘œ, [torch.use_deterministic_algorithms](https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html)에
`True`λ₯Ό ν†΅κ³Όμ‹œμΌœ 결정둠적 μ•Œκ³ λ¦¬μ¦˜μ΄ ν™œμ„±ν™” λ˜λ„λ‘ ν•©λ‹ˆλ‹€.

```py
import os

os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8"

torch.backends.cudnn.benchmark = False
torch.use_deterministic_algorithms(True)
```

이제 λ™μΌν•œ νŒŒμ΄ν”„λΌμΈμ„ λ‘λ²ˆ μ‹€ν–‰ν•˜λ©΄ λ™μΌν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

```py
import torch
from diffusers import DDIMScheduler, StableDiffusionPipeline
import numpy as np

model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id).to("cuda")
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
g = torch.Generator(device="cuda")

prompt = "A bear is playing a guitar on Times Square"

g.manual_seed(0)
result1 = pipe(prompt=prompt, num_inference_steps=50, generator=g, output_type="latent").images

g.manual_seed(0)
result2 = pipe(prompt=prompt, num_inference_steps=50, generator=g, output_type="latent").images

print("L_inf dist = ", abs(result1 - result2).max())
"L_inf dist =  tensor(0., device='cuda:0')"
```