BleachNick's picture
upload required packages
87d40d2
<!--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')"
```