Diffusers documentation
이미지-이미지
이미지-이미지
이미지-이미지 변환은 텍스트-이미지 변환과 유사하지만, 프롬프트 외에도 확산 과정의 시작점으로 사용할 초기 이미지를 전달할 수 있습니다. 초기 이미지는 잠재 공간으로 인코딩되고 여기에 노이즈가 추가됩니다. 그런 다음 잠재 확산 모델은 프롬프트와 노이즈가 추가된 잠재 이미지를 받아 추가된 노이즈를 예측하고, 초기 잠재 이미지에서 예측된 노이즈를 제거하여 새로운 잠재 이미지를 생성합니다. 마지막으로 디코더가 새로운 잠재 이미지를 다시 이미지로 디코딩합니다.
🤗 Diffusers를 사용하면 1-2-3처럼 간단합니다:
- AutoPipelineForImage2Image 클래스에 체크포인트를 로드합니다. 이 파이프라인은 체크포인트에 기반하여 올바른 파이프라인 클래스를 자동으로 로드합니다.
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import load_image, make_image_grid
pipeline = AutoPipelineForImage2Image.from_pretrained(
"kandinsky-community/kandinsky-2-2-decoder", dtype=torch.float16, use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()가이드 전반에서 메모리를 절약하고 추론 속도를 높이기 위해 enable_model_cpu_offload()와 enable_xformers_memory_efficient_attention()를 사용한다는 점을 확인할 수 있습니다. PyTorch 2.0을 사용하는 경우, 파이프라인에서 enable_xformers_memory_efficient_attention()를 호출할 필요가 없으며, PyTorch 2.0의 네이티브 scaled-dot product attention가 이미 사용되고 있기 때문입니다.
- 파이프라인에 전달할 이미지를 로드합니다:
init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png")- pipeline에 프롬프트와 이미지를 전달하여 이미지를 생성합니다:
prompt = "cat wizard, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney, 8k"
image = pipeline(prompt, image=init_image).images[0]
make_image_grid([init_image, image], rows=1, cols=2)
인기 모델
가장 인기 있는 이미지-투-이미지 모델은 Stable Diffusion v1.5, Stable Diffusion XL (SDXL), Kandinsky 2.2입니다. Stable Diffusion과 Kandinsky 모델의 결과는 아키텍처와 학습 과정의 차이로 인해 달라지며, 일반적으로 SDXL이 Stable Diffusion v1.5보다 더 높은 품질의 이미지를 생성한다고 기대할 수 있습니다. 각 모델을 사용하는 방법과 그 결과를 비교해 보겠습니다.
Stable Diffusion v1.5
Stable Diffusion v1.5는 이전 체크포인트에서 초기화된 잠재 확산 모델로, 512x512 이미지에서 595K 스텝 동안 추가 파인튜닝되었습니다. 이 파이프라인을 이미지-투-이미지에 사용하려면 파이프라인에 전달할 초기 이미지를 준비해야 합니다. 그런 다음 프롬프트와 이미지를 파이프라인에 전달하여 새로운 이미지를 생성할 수 있습니다:
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import make_image_grid, load_image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
# pass prompt and image to pipeline
image = pipeline(prompt, image=init_image).images[0]
make_image_grid([init_image, image], rows=1, cols=2)
Stable Diffusion XL (SDXL)
SDXL은 Stable Diffusion 모델의 더 강력한 버전입니다. 더 큰 기본 모델과 기본 모델의 출력 품질을 높이기 위한 추가 리파이너 모델을 사용합니다. 이 모델의 사용 방법과 고품질 이미지를 생성하기 위해 사용하는 기타 기술에 대한 자세한 내용은 SDXL 가이드를 참고하세요.
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import make_image_grid, load_image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-sdxl-init.png"
init_image = load_image(url)
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
# pass prompt and image to pipeline
image = pipeline(prompt, image=init_image, strength=0.5).images[0]
make_image_grid([init_image, image], rows=1, cols=2)
Kandinsky 2.2
Kandinsky 모델은 이미지 사전 모델(image prior model)을 사용하여 이미지 임베딩을 생성한다는 점에서 Stable Diffusion 모델과 다릅니다. 이 임베딩은 텍스트와 이미지 간의 정렬을 개선하는 데 도움을 주어, 잠재 확산 모델(latent diffusion model)이 더 나은 이미지를 생성할 수 있도록 합니다.
Kandinsky 2.2를 사용하는 가장 간단한 방법은 다음과 같습니다:
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import make_image_grid, load_image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"kandinsky-community/kandinsky-2-2-decoder", dtype=torch.float16, use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
# pass prompt and image to pipeline
image = pipeline(prompt, image=init_image).images[0]
make_image_grid([init_image, image], rows=1, cols=2)
파이프라인 매개변수 구성
pipeline에서 이미지 생성 과정과 이미지 품질에 영향을 미치는 여러 중요한 파라미터를 설정할 수 있습니다. 이러한 파라미터가 어떤 역할을 하는지, 그리고 값을 변경할 때 출력이 어떻게 달라지는지 자세히 살펴보겠습니다.
강도
strength는 고려해야 할 가장 중요한 매개변수 중 하나이며, 생성된 이미지에 큰 영향을 미칩니다. 생성된 이미지가 초기 이미지와 얼마나 유사한지를 결정합니다. 다시 말해:
- 📈
strength값이 높을수록 모델은 초기 이미지와 다른 이미지를 생성하는 데 더 많은 “창의성”을 발휘하며,strength값이 1.0이면 초기 이미지는 거의 무시됩니다 - 📉
strength값이 낮을수록 생성된 이미지가 초기 이미지와 더 유사해집니다
strength와 num_inference_steps 매개변수는 strength가 추가할 노이즈 단계 수를 결정하기 때문에 관련이 있습니다. 예를 들어, num_inference_steps가 50이고 strength가 0.8인 경우, 이는 초기 이미지에 40(50 * 0.8)단계의 노이즈를 추가한 후 40단계의 디노이징을 수행하여 새로 생성된 이미지를 얻는다는 의미입니다.
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import make_image_grid, load_image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
# pass prompt and image to pipeline
image = pipeline(prompt, image=init_image, strength=0.8).images[0]
make_image_grid([init_image, image], rows=1, cols=2)
가이던스 스케일
guidance_scale 매개변수는 생성된 이미지와 텍스트 프롬프트의 정합성을 제어하는 데 사용됩니다. guidance_scale 값이 높을수록 생성된 이미지가 프롬프트와 더 밀접하게 정합되며, guidance_scale 값이 낮을수록 생성된 이미지가 프롬프트에서 더 크게 벗어날 수 있습니다.
guidance_scale와 strength를 조합하여 모델의 표현력에 대해 더 정밀한 제어가 가능합니다. 예를 들어, strength + guidance_scale을 높게 조합하면 최대의 창의성을 발휘할 수 있고, strength와 guidance_scale을 모두 낮게 조합하면 초기 이미지와 유사하지만 프롬프트에 덜 엄격하게 구속된 이미지를 생성할 수 있습니다.
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import make_image_grid, load_image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
# pass prompt and image to pipeline
image = pipeline(prompt, image=init_image, guidance_scale=8.0).images[0]
make_image_grid([init_image, image], rows=1, cols=2)
부정 프롬프트
음수 프롬프트(negative prompt)는 모델이 이미지에서 포함하지 않도록 조건을 설정하는 데 사용되며, 이미지 품질을 개선하거나 이미지를 수정하는 데 활용할 수 있습니다. 예를 들어, “poor details” 또는 “blurry”와 같은 음수 프롬프트를 포함하면 모델이 더 높은 품질의 이미지를 생성하도록 유도하여 이미지 품질을 개선할 수 있습니다. 또는 이미지에서 제외할 요소를 지정하여 이미지를 수정할 수도 있습니다.
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import make_image_grid, load_image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
negative_prompt = "ugly, deformed, disfigured, poor details, bad anatomy"
# pass prompt and image to pipeline
image = pipeline(prompt, negative_prompt=negative_prompt, image=init_image).images[0]
make_image_grid([init_image, image], rows=1, cols=2)
체인된 이미지-대-이미지 파이프라인
이미지 생성(이것도 꽤 멋진 기능이지만) 외에도 이미지-투-이미지 파이프라인을 활용할 수 있는 흥미로운 방법들이 몇 가지 있습니다. 다른 파이프라인과 체이닝하여 한 단계 더 나아갈 수도 있습니다.
텍스트-이미지-이미지
Chaining a text-to-image and image-to-image pipeline allows you to generate an image from text and use the generated image as the initial image for the image-to-image pipeline. This is useful if you want to generate an image entirely from scratch. For example, let’s chain a Stable Diffusion and a Kandinsky model.
텍스트-이미지 파이프라인으로 이미지를 생성하는 것부터 시작합니다:
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
import torch
from diffusers.utils import make_image_grid
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
text2image = pipeline("Astronaut in a jungle, cold color palette, muted colors, detailed, 8k").images[0]
text2image이제 이 생성된 이미지를 image-to-image 파이프라인에 전달할 수 있습니다:
pipeline = AutoPipelineForImage2Image.from_pretrained(
"kandinsky-community/kandinsky-2-2-decoder", dtype=torch.float16, use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
image2image = pipeline("Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", image=text2image).images[0]
make_image_grid([text2image, image2image], rows=1, cols=2)이미지에서 이미지로 이미지
여러 개의 이미지-투-이미지 파이프라인을 체인하여 더 흥미로운 이미지를 생성할 수도 있습니다. 이는 이미지를 반복적으로 스타일 전이하는 작업, 짧은 GIF 생성, 이미지의 색상 복원, 또는 이미지의 누락된 영역 복원에 유용할 수 있습니다.
이미지를 생성하는 것으로 시작합니다:
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import make_image_grid, load_image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
# pass prompt and image to pipeline
image = pipeline(prompt, image=init_image, output_type="latent").images[0]파이프라인에서
output_type="latent"를 지정하는 것이 중요합니다. 이를 통해 모든 출력을 잠재 공간에 유지하여 불필요한 디코딩-인코딩 단계를 피할 수 있습니다. 이 방법은 체인된 파이프라인이 동일한 VAE를 사용하는 경우에만 작동합니다.
이 파이프라인의 latent 출력을 다음 파이프라인으로 전달하여 만화책 아트 스타일의 이미지를 생성합니다:
pipeline = AutoPipelineForImage2Image.from_pretrained(
"ogkalu/Comic-Diffusion", dtype=torch.float16
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# need to include the token "charliebo artstyle" in the prompt to use this checkpoint
image = pipeline("Astronaut in a jungle, charliebo artstyle", image=image, output_type="latent").images[0]Repeat one more time to generate the final image in a pixel art style:
pipeline = AutoPipelineForImage2Image.from_pretrained(
"kohbanye/pixel-art-style", dtype=torch.float16
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# need to include the token "pixelartstyle" in the prompt to use this checkpoint
image = pipeline("Astronaut in a jungle, pixelartstyle", image=image).images[0]
make_image_grid([init_image, image], rows=1, cols=2)이미지-업스케일러-슈퍼레졸루션
이미지-이미지 파이프라인을 체인할 수 있는 또 다른 방법은 업스케일러와 슈퍼 레졸루션 파이프라인을 사용하여 이미지의 디테일 수준을 크게 높이는 것입니다.
이미지-이미지 파이프라인으로 시작합니다:
import torch
from diffusers import AutoPipelineForImage2Image
from diffusers.utils import make_image_grid, load_image
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
# pass prompt and image to pipeline
image_1 = pipeline(prompt, image=init_image, output_type="latent").images[0]파이프라인에서
output_type="latent"를 지정하는 것이 중요합니다. 이를 통해 모든 출력을 latent 공간에 유지하여 불필요한 decode-encode 단계를 피할 수 있습니다. 이 방법은 체인된 파이프라인이 동일한 VAE를 사용하는 경우에만 작동합니다.
이미지 해상도를 높이기 위해 업스케일러 파이프라인에 연결하세요:
from diffusers import StableDiffusionLatentUpscalePipeline
upscaler = StableDiffusionLatentUpscalePipeline.from_pretrained(
"stabilityai/sd-x2-latent-upscaler", dtype=torch.float16, use_safetensors=True
)
upscaler.enable_model_cpu_offload()
upscaler.enable_xformers_memory_efficient_attention()
image_2 = upscaler(prompt, image=image_1).images[0]마지막으로, 해상도를 더 향상시키기 위해 super-resolution 파이프라인에 연결합니다:
from diffusers import StableDiffusionUpscalePipeline
super_res = StableDiffusionUpscalePipeline.from_pretrained(
"stabilityai/stable-diffusion-x4-upscaler", dtype=torch.float16, variant="fp16", use_safetensors=True
)
super_res.enable_model_cpu_offload()
super_res.enable_xformers_memory_efficient_attention()
image_3 = super_res(prompt, image=image_2).images[0]
make_image_grid([init_image, image_3.resize((512, 512))], rows=1, cols=2)이미지 생성 제어
원하는 대로 정확하게 보이는 이미지를 생성하는 것은 어려울 수 있으며, 이것이 제어형 생성 기법과 모델이 매우 유용한 이유입니다. negative_prompt를 사용하여 이미지 생성을 부분적으로 제어할 수 있지만, 프롬프트 가중치(ControlNets)와 같은 더 견고한 방법도 있습니다.
프롬프트 가중치
Prompt weighting을 사용하면 프롬프트 내 각 개념의 표현을 스케일링할 수 있습니다. 예를 들어, “Astronaut in a jungle, cold color palette, muted colors, detailed, 8k”와 같은 프롬프트에서 “astronaut”와 “jungle”의 임베딩을 증가 또는 감소시킬 수 있습니다. Compel 라이브러리는 프롬프트 가중치를 조정하고 임베딩을 생성하기 위한 간단한 구문을 제공합니다. 임베딩을 생성하는 방법은 Prompt weighting 가이드에서 확인할 수 있습니다.
AutoPipelineForImage2Image에는 prompt 파라미터를 대체하는 임베딩을 전달할 수 있는 prompt_embeds(네거티브 프롬프트를 사용하는 경우 negative_prompt_embeds) 파라미터가 있습니다.
from diffusers import AutoPipelineForImage2Image
import torch
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
image = pipeline(prompt_embeds=prompt_embeds, # generated from Compel
negative_prompt_embeds=negative_prompt_embeds, # generated from Compel
image=init_image,
).images[0]ControlNet
ControlNet은 추가 조건 이미지를 사용할 수 있어 이미지 생성을 더 유연하고 정확하게 제어할 수 있는 방법을 제공합니다. 조건 이미지는 canny 이미지, depth map, 이미지 분할, 심지어 낙서일 수도 있습니다. 어떤 유형의 조건 이미지를 선택하든, ControlNet은 해당 이미지에 포함된 정보를 보존하는 이미지를 생성합니다.
예를 들어, 이미지의 공간 정보를 유지하기 위해 깊이 맵으로 이미지를 조건화해 보겠습니다.
from diffusers.utils import load_image, make_image_grid
# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
init_image = init_image.resize((958, 960)) # resize to depth image dimensions
depth_image = load_image("https://huggingface.co/lllyasviel/control_v11f1p_sd15_depth/resolve/main/images/control.png")
make_image_grid([init_image, depth_image], rows=1, cols=2)깊이 맵으로 조건부 설정된 ControlNet 모델과 AutoPipelineForImage2Image를 로드합니다:
from diffusers import ControlNetModel, AutoPipelineForImage2Image
import torch
controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", dtype=torch.float16, variant="fp16", use_safetensors=True)
pipeline = AutoPipelineForImage2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", controlnet=controlnet, dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()depth map, initial image, prompt에 조건을 걸어 새로운 이미지를 생성합니다:
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
image_control_net = pipeline(prompt, image=init_image, control_image=depth_image).images[0]
make_image_grid([init_image, depth_image, image_control_net], rows=1, cols=3)
ControlNet에서 생성된 이미지에 이미지-이미지 파이프라인을 체이닝하여 새로운 스타일을 적용해 보겠습니다:
pipeline = AutoPipelineForImage2Image.from_pretrained(
"nitrosocke/elden-ring-diffusion", dtype=torch.float16,
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()
prompt = "elden ring style astronaut in a jungle" # include the token "elden ring style" in the prompt
negative_prompt = "ugly, deformed, disfigured, poor details, bad anatomy"
image_elden_ring = pipeline(prompt, negative_prompt=negative_prompt, image=image_control_net, strength=0.45, guidance_scale=10.5).images[0]
make_image_grid([init_image, depth_image, image_control_net, image_elden_ring], rows=2, cols=2)
최적화
확산 모델 실행은 계산 비용이 크고 부하가 높지만, 몇 가지 최적화 기법을 활용하면 소비자용 GPU나 무료 티어 GPU에서도 실행할 수 있습니다. 예를 들어, PyTorch 2.0의 scaled-dot product attention이나 xFormers와 같이 메모리 효율이 높은 어텐션 방식을 사용할 수 있습니다(둘 중 하나만 사용하면 되며, 둘 다 사용할 필요는 없습니다). 또한, 다른 파이프라인 구성 요소가 CPU에서 대기하는 동안 모델을 GPU로 오프로드할 수도 있습니다.
+ pipeline.enable_model_cpu_offload()
+ pipeline.enable_xformers_memory_efficient_attention()torch.compile를 사용하여 UNet을 래핑하면 추론 속도를 더욱 향상시킬 수 있습니다:
pipeline.unet = torch.compile(pipeline.unet, mode="reduce-overhead", fullgraph=True)자세한 내용은 메모리 사용량 줄이기 및 추론 가속화 가이드를 참고하세요.
Update on GitHub