File size: 14,796 Bytes
87d40d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!--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.
-->
                                                               
# ํšจ๊ณผ์ ์ด๊ณ  ํšจ์œจ์ ์ธ Diffusion

[[open-in-colab]]

ํŠน์ • ์Šคํƒ€์ผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์›ํ•˜๋Š” ๋‚ด์šฉ์„ ํฌํ•จํ•˜๋„๋ก[`DiffusionPipeline`]์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ๊นŒ๋‹ค๋กœ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ข…์ข… ๋งŒ์กฑ์Šค๋Ÿฌ์šด ์ด๋ฏธ์ง€๋ฅผ ์–ป๊ธฐ๊นŒ์ง€ [`DiffusionPipeline`]์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌด์—์„œ ์œ ๋ฅผ ์ฐฝ์กฐํ•˜๋Š” ๊ฒƒ์€ ํŠนํžˆ ์ถ”๋ก ์„ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ๊ณ„์‚ฐ ์ง‘์•ฝ์ ์ธ ํ”„๋กœ์„ธ์Šค์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ดํ”„๋ผ์ธ์—์„œ *๊ณ„์‚ฐ*(์†๋„) ๋ฐ *๋ฉ”๋ชจ๋ฆฌ*(GPU RAM) ํšจ์œจ์„ฑ์„ ๊ทน๋Œ€ํ™”ํ•˜์—ฌ ์ถ”๋ก  ์ฃผ๊ธฐ ์‚ฌ์ด์˜ ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•˜์—ฌ ๋” ๋น ๋ฅด๊ฒŒ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” [`DiffusionPipeline`]์„ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๋น ๋ฅด๊ณ  ํšจ๊ณผ์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค.

[`runwayml/stable-diffusion-v1-5`](https://huggingface.co/runwayml/stable-diffusion-v1-5) ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์™€์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค:

```python
from diffusers import DiffusionPipeline

model_id = "runwayml/stable-diffusion-v1-5"
pipeline = DiffusionPipeline.from_pretrained(model_id)
```

์˜ˆ์ œ ํ”„๋กฌํ”„ํŠธ๋Š” "portrait of an old warrior chief" ์ด์ง€๋งŒ, ์ž์œ ๋กญ๊ฒŒ ์ž์‹ ๋งŒ์˜ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค:

```python
prompt = "portrait photo of a old warrior chief"
```

## ์†๋„

<Tip>

๐Ÿ’ก GPU์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ GPU ์ œ๊ณต์—…์ฒด์—์„œ ๋ฌด๋ฃŒ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!. [Colab](https://colab.research.google.com/)

</Tip>

์ถ”๋ก  ์†๋„๋ฅผ ๋†’์ด๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” Pytorch ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ๋•Œ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ GPU์— ํŒŒ์ดํ”„๋ผ์ธ์„ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค:

```python
pipeline = pipeline.to("cuda")
```

๋™์ผํ•œ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด [`Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html)๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  [์žฌํ˜„์„ฑ](./using-diffusers/reusing_seeds)์— ๋Œ€ํ•œ ์‹œ๋“œ๋ฅผ ์„ค์ •ํ•˜์„ธ์š”:

```python
import torch

generator = torch.Generator("cuda").manual_seed(0)
```

์ด์ œ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

```python
image = pipeline(prompt, generator=generator).images[0]
image
```

<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_1.png">
</div>

์ด ํ”„๋กœ์„ธ์Šค๋Š” T4 GPU์—์„œ ์•ฝ 30์ดˆ๊ฐ€ ์†Œ์š”๋˜์—ˆ์Šต๋‹ˆ๋‹ค(ํ• ๋‹น๋œ GPU๊ฐ€ T4๋ณด๋‹ค ๋‚˜์€ ๊ฒฝ์šฐ ๋” ๋น ๋ฅผ ์ˆ˜ ์žˆ์Œ). ๊ธฐ๋ณธ์ ์œผ๋กœ [`DiffusionPipeline`]์€ 50๊ฐœ์˜ ์ถ”๋ก  ๋‹จ๊ณ„์— ๋Œ€ํ•ด ์ „์ฒด `float32` ์ •๋ฐ€๋„๋กœ ์ถ”๋ก ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. `float16`๊ณผ ๊ฐ™์€ ๋” ๋‚ฎ์€ ์ •๋ฐ€๋„๋กœ ์ „ํ™˜ํ•˜๊ฑฐ๋‚˜ ์ถ”๋ก  ๋‹จ๊ณ„๋ฅผ ๋” ์ ๊ฒŒ ์‹คํ–‰ํ•˜์—ฌ ์†๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

`float16`์œผ๋กœ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๊ณ  ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค:


```python
import torch

pipeline = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")
generator = torch.Generator("cuda").manual_seed(0)
image = pipeline(prompt, generator=generator).images[0]
image
```

<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_2.png">
</div>

์ด๋ฒˆ์—๋Š” ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์•ฝ 11์ดˆ๋ฐ–์— ๊ฑธ๋ฆฌ์ง€ ์•Š์•„ ์ด์ „๋ณด๋‹ค 3๋ฐฐ ๊ฐ€๊นŒ์ด ๋นจ๋ผ์กŒ์Šต๋‹ˆ๋‹ค!

<Tip>

๐Ÿ’ก ํŒŒ์ดํ”„๋ผ์ธ์€ ํ•ญ์ƒ `float16`์—์„œ ์‹คํ–‰ํ•  ๊ฒƒ์„ ๊ฐ•๋ ฅํžˆ ๊ถŒ์žฅํ•˜๋ฉฐ, ์ง€๊ธˆ๊นŒ์ง€ ์ถœ๋ ฅ ํ’ˆ์งˆ์ด ์ €ํ•˜๋˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ์˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค.

</Tip>

๋˜ ๋‹ค๋ฅธ ์˜ต์…˜์€ ์ถ”๋ก  ๋‹จ๊ณ„์˜ ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ณด๋‹ค ํšจ์œจ์ ์ธ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์„ ํƒํ•˜๋ฉด ์ถœ๋ ฅ ํ’ˆ์งˆ ์ €ํ•˜ ์—†์ด ๋‹จ๊ณ„ ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๋ฐ ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๋ชจ๋ธ๊ณผ ํ˜ธํ™˜๋˜๋Š” ์Šค์ผ€์ค„๋Ÿฌ๋Š” `compatibles` ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ [`DiffusionPipeline`]์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

```python
pipeline.scheduler.compatibles
[
    diffusers.schedulers.scheduling_lms_discrete.LMSDiscreteScheduler,
    diffusers.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler,
    diffusers.schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteScheduler,
    diffusers.schedulers.scheduling_deis_multistep.DEISMultistepScheduler,
    diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler,
    diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler,
    diffusers.schedulers.scheduling_ddpm.DDPMScheduler,
    diffusers.schedulers.scheduling_dpmsolver_singlestep.DPMSolverSinglestepScheduler,
    diffusers.schedulers.scheduling_k_dpm_2_ancestral_discrete.KDPM2AncestralDiscreteScheduler,
    diffusers.schedulers.scheduling_heun_discrete.HeunDiscreteScheduler,
    diffusers.schedulers.scheduling_pndm.PNDMScheduler,
    diffusers.schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteScheduler,
    diffusers.schedulers.scheduling_ddim.DDIMScheduler,
]
```

Stable Diffusion ๋ชจ๋ธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•ฝ 50๊ฐœ์˜ ์ถ”๋ก  ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•œ [`PNDMScheduler`]๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€๋งŒ, [`DPMSolverMultistepScheduler`]์™€ ๊ฐ™์ด ์„ฑ๋Šฅ์ด ๋” ๋›ฐ์–ด๋‚œ ์Šค์ผ€์ค„๋Ÿฌ๋Š” ์•ฝ 20๊ฐœ ๋˜๋Š” 25๊ฐœ์˜ ์ถ”๋ก  ๋‹จ๊ณ„๋งŒ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ๋กœ๋“œํ•˜๋ ค๋ฉด [`ConfigMixin.from_config`] ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค:

```python
from diffusers import DPMSolverMultistepScheduler

pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
```

`num_inference_steps`๋ฅผ 20์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค:

```python
generator = torch.Generator("cuda").manual_seed(0)
image = pipeline(prompt, generator=generator, num_inference_steps=20).images[0]
image
```

<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_3.png">
</div>

์ถ”๋ก ์‹œ๊ฐ„์„ 4์ดˆ๋กœ ๋‹จ์ถ•ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค! โšก๏ธ

## ๋ฉ”๋ชจ๋ฆฌ

ํŒŒ์ดํ”„๋ผ์ธ ์„ฑ๋Šฅ ํ–ฅ์ƒ์˜ ๋˜ ๋‹ค๋ฅธ ํ•ต์‹ฌ์€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๋Š” ๊ฒƒ์ธ๋ฐ, ์ดˆ๋‹น ์ƒ์„ฑ๋˜๋Š” ์ด๋ฏธ์ง€ ์ˆ˜๋ฅผ ์ตœ๋Œ€ํ™”ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„์ ‘์ ์œผ๋กœ ๋” ๋น ๋ฅธ ์†๋„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํ•œ ๋ฒˆ์— ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฏธ์ง€ ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ `OutOfMemoryError`(OOM)์ด ๋ฐœ์ƒํ•  ๋•Œ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ๋ฐฐ์น˜ ํฌ๊ธฐ๋ฅผ ์‹œ๋„ํ•ด ๋ณด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ”„๋กฌํ”„ํŠธ ๋ชฉ๋ก๊ณผ `Generators`์—์„œ ์ด๋ฏธ์ง€ ๋ฐฐ์น˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ข‹์€ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ `Generator`์— ์‹œ๋“œ๋ฅผ ํ• ๋‹นํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

```python
def get_inputs(batch_size=1):
    generator = [torch.Generator("cuda").manual_seed(i) for i in range(batch_size)]
    prompts = batch_size * [prompt]
    num_inference_steps = 20

    return {"prompt": prompts, "generator": generator, "num_inference_steps": num_inference_steps}
```

๋˜ํ•œ ๊ฐ ์ด๋ฏธ์ง€ ๋ฐฐ์น˜๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค:

```python
from PIL import Image


def image_grid(imgs, rows=2, cols=2):
    w, h = imgs[0].size
    grid = Image.new("RGB", size=(cols * w, rows * h))

    for i, img in enumerate(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))
    return grid
```
 
`batch_size=4`๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์†Œ๋น„ํ–ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค:

```python
images = pipeline(**get_inputs(batch_size=4)).images
image_grid(images)
```

RAM์ด ๋” ๋งŽ์€ GPU๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์œ„์˜ ์ฝ”๋“œ์—์„œ `OOM` ์˜ค๋ฅ˜๊ฐ€ ๋ฐ˜ํ™˜๋˜์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค! ๋Œ€๋ถ€๋ถ„์˜ ๋ฉ”๋ชจ๋ฆฌ๋Š” cross-attention ๋ ˆ์ด์–ด๊ฐ€ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ๋ฐฐ์น˜๋กœ ์‹คํ–‰ํ•˜๋Š” ๋Œ€์‹  ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋ฉด ์ƒ๋‹นํ•œ ์–‘์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ตฌ์„ฑํ•˜์—ฌ [`~DiffusionPipeline.enable_attention_slicing`] ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค:


```python
pipeline.enable_attention_slicing()
```

์ด์ œ `batch_size`๋ฅผ 8๋กœ ๋Š˜๋ ค๋ณด์„ธ์š”!

```python
images = pipeline(**get_inputs(batch_size=8)).images
image_grid(images, rows=2, cols=4)
```

<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_5.png">
</div>

์ด์ „์—๋Š” 4๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ๋ฐฐ์น˜๋กœ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์—†์—ˆ์ง€๋งŒ, ์ด์ œ๋Š” ์ด๋ฏธ์ง€๋‹น ์•ฝ 3.5์ดˆ ๋งŒ์— 8๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ๋ฐฐ์น˜๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ด๋Š” ์•„๋งˆ๋„ ํ’ˆ์งˆ ์ €ํ•˜ ์—†์ด T4 GPU์—์„œ ๊ฐ€์žฅ ๋น ๋ฅธ ์†๋„์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

## ํ’ˆ์งˆ

์ง€๋‚œ ๋‘ ์„น์…˜์—์„œ๋Š” `fp16`์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ์˜ ์†๋„๋ฅผ ์ตœ์ ํ™”ํ•˜๊ณ , ๋” ์„ฑ๋Šฅ์ด ์ข‹์€ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๋ก  ๋‹จ๊ณ„์˜ ์ˆ˜๋ฅผ ์ค„์ด๊ณ , attention slicing์„ ํ™œ์„ฑํ™”ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€์˜ ํ’ˆ์งˆ์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ง‘์ค‘์ ์œผ๋กœ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


### ๋” ๋‚˜์€ ์ฒดํฌํฌ์ธํŠธ

๊ฐ€์žฅ ํ™•์‹คํ•œ ๋‹จ๊ณ„๋Š” ๋” ๋‚˜์€ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. Stable Diffusion ๋ชจ๋ธ์€ ์ข‹์€ ์ถœ๋ฐœ์ ์ด๋ฉฐ, ๊ณต์‹ ์ถœ์‹œ ์ดํ›„ ๋ช‡ ๊ฐ€์ง€ ๊ฐœ์„ ๋œ ๋ฒ„์ „๋„ ์ถœ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ตœ์‹  ๋ฒ„์ „์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด์„œ ์ž๋™์œผ๋กœ ๋” ๋‚˜์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ์—ฌ์ „ํžˆ ๋‹ค์–‘ํ•œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ง์ ‘ ์‹คํ—˜ํ•ด๋ณด๊ณ , [negative prompts](https://minimaxir.com/2022/11/stable-diffusion-negative-prompt/) ์‚ฌ์šฉ ๋“ฑ ์•ฝ๊ฐ„์˜ ์กฐ์‚ฌ๋ฅผ ํ†ตํ•ด ์ตœ์ƒ์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ถ„์•ผ๊ฐ€ ์„ฑ์žฅํ•จ์— ๋”ฐ๋ผ ํŠน์ • ์Šคํƒ€์ผ์„ ์—ฐ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ธ๋ฐ€ํ•˜๊ฒŒ ์กฐ์ •๋œ ๊ณ ํ’ˆ์งˆ ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ์ ์  ๋” ๋งŽ์•„์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. [Hub](https://huggingface.co/models?library=diffusers&sort=downloads)์™€ [Diffusers Gallery](https://huggingface.co/spaces/huggingface-projects/diffusers-gallery)๋ฅผ ๋‘˜๋Ÿฌ๋ณด๊ณ  ๊ด€์‹ฌ ์žˆ๋Š” ๊ฒƒ์„ ์ฐพ์•„๋ณด์„ธ์š”!


### ๋” ๋‚˜์€ ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์„ฑ ์š”์†Œ

ํ˜„์žฌ ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ๊ต์ฒดํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Stability AI์˜ ์ตœ์‹  [autodecoder](https://huggingface.co/stabilityai/stable-diffusion-2-1/tree/main/vae)๋ฅผ ํŒŒ์ดํ”„๋ผ์ธ์— ๋กœ๋“œํ•˜๊ณ  ๋ช‡ ๊ฐ€์ง€ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค:


```python
from diffusers import AutoencoderKL

vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16).to("cuda")
pipeline.vae = vae
images = pipeline(**get_inputs(batch_size=8)).images
image_grid(images, rows=2, cols=4)
```

<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_6.png">
</div>

### ๋” ๋‚˜์€ ํ”„๋กฌํ”„ํŠธ ์—”์ง€๋‹ˆ์–ด๋ง

์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋Š” *prompt engineering*์ด๋ผ๊ณ  ํ•  ์ •๋„๋กœ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กฌํ”„ํŠธ ์—”์ง€๋‹ˆ์–ด๋ง ์‹œ ๊ณ ๋ คํ•ด์•ผ ํ•  ๋ช‡ ๊ฐ€์ง€ ์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

- ์ƒ์„ฑํ•˜๋ ค๋Š” ์ด๋ฏธ์ง€ ๋˜๋Š” ์œ ์‚ฌํ•œ ์ด๋ฏธ์ง€๊ฐ€ ์ธํ„ฐ๋„ท์— ์–ด๋–ป๊ฒŒ ์ €์žฅ๋˜์–ด ์žˆ๋Š”๊ฐ€?
- ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์Šคํƒ€์ผ๋กœ ๋ชจ๋ธ์„ ์œ ๋„ํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ์ถ”๊ฐ€ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?

์ด๋ฅผ ์—ผ๋‘์— ๋‘๊ณ  ์ƒ‰์ƒ๊ณผ ๋” ๋†’์€ ํ’ˆ์งˆ์˜ ๋””ํ…Œ์ผ์„ ํฌํ•จํ•˜๋„๋ก ํ”„๋กฌํ”„ํŠธ๋ฅผ ๊ฐœ์„ ํ•ด ๋ด…์‹œ๋‹ค:


```python
prompt += ", tribal panther make up, blue on red, side profile, looking away, serious eyes"
prompt += " 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta"
```

์ƒˆ๋กœ์šด ํ”„๋กฌํ”„ํŠธ๋กœ ์ด๋ฏธ์ง€ ๋ฐฐ์น˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค:

```python
images = pipeline(**get_inputs(batch_size=8)).images
image_grid(images, rows=2, cols=4)
```

<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_7.png">
</div>

๊ฝค ์ธ์ƒ์ ์ž…๋‹ˆ๋‹ค! `1`์˜ ์‹œ๋“œ๋ฅผ ๊ฐ€์ง„ `Generator`์— ํ•ด๋‹นํ•˜๋Š” ๋‘ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์— ํ”ผ์‚ฌ์ฒด์˜ ๋‚˜์ด์— ๋Œ€ํ•œ ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์กฐ๊ธˆ ๋” ์กฐ์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค:

```python
prompts = [
    "portrait photo of the oldest warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta",
    "portrait photo of a old warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta",
    "portrait photo of a warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta",
    "portrait photo of a young warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta",
]

generator = [torch.Generator("cuda").manual_seed(1) for _ in range(len(prompts))]
images = pipeline(prompt=prompts, generator=generator, num_inference_steps=25).images
image_grid(images)
```

<div class="flex justify-center">
    <img src="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_8.png">
</div>

## ๋‹ค์Œ ๋‹จ๊ณ„

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ๊ณ„์‚ฐ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ ๋†’์ด๊ณ  ์ƒ์„ฑ๋œ ์ถœ๋ ฅ์˜ ํ’ˆ์งˆ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด [`DiffusionPipeline`]์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ํŒŒ์ดํ”„๋ผ์ธ์„ ๋” ๋น ๋ฅด๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐ ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด ๋‹ค์Œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”:

- [PyTorch 2.0](./optimization/torch2.0) ๋ฐ [`torch.compile`](https://pytorch.org/docs/stable/generated/torch.compile.html)์ด ์–ด๋–ป๊ฒŒ ์ถ”๋ก  ์†๋„๋ฅผ 5~300% ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด์„ธ์š”. A100 GPU์—์„œ๋Š” ์ถ”๋ก  ์†๋„๊ฐ€ ์ตœ๋Œ€ 50%๊นŒ์ง€ ๋นจ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
- PyTorch 2๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, [xFormers](./optimization/xformers)๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ธ ์–ดํ…์…˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ PyTorch 1.13.1๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ์†๋„๊ฐ€ ๋นจ๋ผ์ง€๊ณ  ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๊ฐ€ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค.
- ๋ชจ๋ธ ์˜คํ”„๋กœ๋”ฉ๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ์ตœ์ ํ™” ๊ธฐ๋ฒ•์€ [์ด ๊ฐ€์ด๋“œ](./optimization/fp16)์—์„œ ๋‹ค๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.