Spaces:
Runtime error
Runtime error
File size: 14,994 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 |
<!--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]]
[`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, use_safetensors=True)
```
ใใใงไฝฟ็จใใใใญใณใใใฎไพใฏๅนด่ใใๆฆๅฃซใฎ้ทใฎ่ๅ็ปใงใใใใ่ช็ฑใซๅคๆดใใฆใใ ใใ๏ผ
```python
prompt = "portrait photo of a old warrior chief"
```
## Speed
<Tip>
๐ก GPUใๅฉ็จใงใใชใๅ ดๅใฏใ[Colab](https://colab.research.google.com/)ใฎใใใชGPUใใญใใคใใผใใ็กๆใงๅฉ็จใงใใพใ๏ผ
</Tip>
็ปๅ็ๆใ้ซ้ๅใใๆใ็ฐกๅใชๆนๆณใฎ1ใคใฏใPyTorchใขใธใฅใผใซใจๅใใใใซGPUไธใซใใคใใฉใคใณใ้
็ฝฎใใใใจใงใ๏ผ
```python
pipeline = pipeline.to("cuda")
```
ๅใใคใกใผใธใไฝฟใฃใฆๆน่ฏใงใใใใใซใใใซใฏใ[`Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html)ใไฝฟใใ[reproducibility](./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`]ใฏๅฎๅ
จใช`float32`็ฒพๅบฆใง็ๆใ50ในใใใๅฎ่กใใพใใfloat16`ใฎใใใชไฝใ็ฒพๅบฆใซๅคๆดใใใใๆจ่ซในใใใๆฐใๆธใใใใจใง้ซ้ๅใใใใจใใงใใพใใ
ใพใใฏ `float16` ใงใขใใซใใญใผใใใฆ็ปๅใ็ๆใใฆใฟใพใใใ๏ผ
```python
import torch
pipeline = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, use_safetensors=True)
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ใขใใซใฏใใใฉใซใใง[`PNDMScheduler`]ใไฝฟ็จใใพใใใใฎในใฑใธใฅใผใฉใฏ้ๅธธ~50ใฎๆจ่ซในใใใใๅฟ
่ฆใจใใพใใใ[`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็งใซ็ญ็ธฎใใใใจใซๆๅใใ๏ผโก๏ธ
## ใกใขใชใผ
ใใคใใฉใคใณใฎใใใฉใผใใณในใๅไธใใใใใ1ใคใฎ้ตใฏใๆถ่ฒปใกใขใชใๅฐใชใใใใใจใงใใไธๅบฆใซ็ๆใงใใ็ปๅใฎๆฐใ็ขบ่ชใใๆใ็ฐกๅใชๆนๆณใฏใ`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}
```
`batch_size=4`ใง้ๅงใใใฉใใ ใใกใขใชใๆถ่ฒปใใใใ็ขบ่ชใใพใ๏ผ
```python
from diffusers.utils import make_image_grid
images = pipeline(**get_inputs(batch_size=4)).images
make_image_grid(images, 2, 2)
```
ๅคงๅฎน้ใฎRAMใๆญ่ผใใGPUใงใชใ้ใใไธ่จใฎใณใผใใฏใใใใ`OOM`ใจใฉใผใ่ฟใใใฏใใงใ๏ผใกใขใชใฎๅคงๅใฏใฏใญในใขใใณใทใงใณใฌใคใคใผใๅ ใใฆใใพใใใใฎๅฆ็ใใใใใงๅฎ่กใใไปฃใใใซใ้ๆฌกๅฎ่กใใใใจใงใกใขใชใๅคงๅน
ใซ็ฏ็ดใงใใพใใๅฟ
่ฆใชใฎใฏใ[`~DiffusionPipeline.enable_attention_slicing`]้ขๆฐใไฝฟ็จใใใใจใ ใใงใ๏ผ
```python
pipeline.enable_attention_slicing()
```
ไปๅบฆใฏ`batch_size`ใ8ใซใใฆใฟใฆใใ ใใ๏ผ
```python
images = pipeline(**get_inputs(batch_size=8)).images
make_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ๆใฎ็ปๅใฎใใใใ็ๆใใใใจใใใงใใพใใใงใใใใไปใงใฏ8ๆใฎ็ปๅใฎใใใใ1ๆใใใ๏ฝ3.5็งใง็ๆใงใใพใ๏ผใใใฏใใใใใๅ่ณชใ็ ็ฒใซใใใใจใชใT4 GPUใงใงใใๆ้ใฎๅฆ็้ๅบฆใงใใ
## ๅ่ณช
ๅใฎ2ใคใฎใปใฏใทใงใณใงใฏใ`fp16` ใไฝฟใฃใฆใใคใใฉใคใณใฎ้ๅบฆใๆ้ฉๅใใๆนๆณใใใใใใฉใผใใณ ในใชในใฑใธใฅใผใฉใผใไฝฟใฃใฆ็ๆในใใใๆฐใๆธใใๆนๆณใใขใใณใทใงใณในใฉใคในใๆๅน ใซใใฆใกใขใชๆถ่ฒป้ใๆธใใๆนๆณใซใคใใฆๅญฆใณใพใใใไปๅบฆใฏใ็ๆใใใ็ปๅใฎๅ่ณชใๅไธใใใๆนๆณใซ็ฆ็นใๅฝใฆใพใใ
### ใใ่ฏใใใงใใฏใใคใณใ
ๆใๅ็ดใชในใใใใฏใใใ่ฏใใใงใใฏใใคใณใใไฝฟใใใจใงใใStable Diffusionใขใใซใฏ่ฏใๅบ็บ็นใงใใใๅ
ฌๅผ็บ่กจไปฅๆฅใใใใคใใฎๆน่ฏ็ใใชใชใผในใใใฆใใพใใใใใใๆฐใใใใผใธใงใณใไฝฟใฃใใใใจใใฃใฆใ่ชๅ็ใซ่ฏใ็ตๆใๅพใใใใใใงใฏใใใพใใใๆ่ฏใฎ็ตๆใๅพใใใใซใฏใ่ชๅใงใใพใใพใชใใงใใฏใใคใณใใ่ฉฆใใฆใฟใใใใกใใฃใจใใ็ ็ฉถ๏ผ[ใใฌใใฃใใใญใณใใ](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
make_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>
### ใใ่ฏใใใญใณใใใปใจใณใธใใขใชใณใฐ
็ปๅใ็ๆใใใใใซไฝฟ็จใใๆ็ซ ใฏใ*ใใญใณใใใจใณใธใใขใชใณใฐ*ใจๅผใฐใใๅ้ใไฝใใใใปใฉใ้ๅธธใซ้่ฆใงใใใใญใณใใใปใจใณใธใใขใชใณใฐใง่ๆ
ฎใในใ็นใฏไปฅไธใฎ้ใใงใ๏ผ
- ็ๆใใใ็ปๅใใใฎ้กไผผ็ปๅใฏใใคใณใฟใผใใใไธใซใฉใฎใใใซไฟๅญใใใฆใใใ๏ผ
- ็งใๆใในใฟใคใซใซใขใใซใ่ชๅฐใใใใใซใใฉใฎใใใช่ฟฝๅ ่ฉณ็ดฐใไธใใในใใ๏ผ
ใใฎใใจใๅฟต้ ญใซ็ฝฎใใฆใใใญใณใใใซ่ฒใใใ่ณชใฎ้ซใใใฃใใผใซใๅซใใใใใซๆน่ฏใใฆใฟใพใใใ๏ผ
```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
make_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`ใซๅฏพๅฟใใ2็ช็ฎใฎ็ปๅใซใ่ขซๅไฝใฎๅนด้ฝขใซ้ขใใใใญในใใ่ฟฝๅ ใใฆใใใๅฐใๆใๅ ใใฆใฟใพใใใ๏ผ
```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
make_image_grid(images, 2, 2)
```
<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 ใจ็ธๆงใ่ฏใใ้ซ้ๅใจใกใขใชๆถ่ฒป้ใฎๅๆธใๅๆใซๅฎ็พใใพใใ
- ใขใใซใฎใชใใญใผใใชใฉใใใฎไปใฎๆ้ฉๅใใฏใใใฏใฏ [this guide](./optimization/fp16) ใงใซใใผใใใฆใใพใใ
|