Buckets:
개요
Diffusion 추론은 계산 비용이 높으며, 원하는 결과를 얻기까지 DiffusionPipeline을 여러 번 실행하는 경우가 많습니다. 이 페이지에서는 Diffusers의 주요 최적화 기법, 각 기법의 역할, 그리고 사용 시점에 대해 개요를 제공합니다.
시작 경로
모델이 GPU 1개에 들어갈 수 있는 경우, 이 기본 로딩으로 시작하세요. dtype을 설정하고 파이프라인을 가속기에 배치합니다. 메모리가 부족할 때만 모델 CPU 오프로딩을 사용하세요. 또한, 단계 수를 줄이거나 더 빠른 스케줄러를 사용하여 추론 속도를 높일 수도 있습니다.
dtype를 생략하면 Diffusers는 구성 요소를 float32로 로드합니다. dtype=torch.bfloat16를 전달하고(또는 bfloat16이 지원되지 않는 경우 torch.float16을 전달하고), pipeline.to("cuda")를 사용하여 파이프라인을 가속기에 배치합니다.
import torch
from diffusers import DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
dtype=torch.bfloat16,
)
pipeline.to("cuda") # or "mps", "xpu"
prompt = """
cinematic film still of a cat sipping a margarita in a pool in Palm Springs, California
highly detailed, high budget hollywood movie, cinemascope, moody, epic, gorgeous, film grain
"""
pipeline(prompt).images[0]
파이프라인이 적합하지 않거나 메모리가 부족하면, 모든 것을 GPU에 유지하는 대신 enable_model_cpu_offload()를 호출하세요. 이 함수는 활성 모델을 GPU에 배치하고 다른 구성 요소를 CPU에 유지합니다.
모델이 메모리에 들어갈 수 있는 경우 건너뛰세요. 오프로딩이 필요하지 않을 때 오프로딩은 더 느립니다.
추가 오프로딩 옵션에 대해서는 메모리 사용량 줄이기를 참조하세요.
pipeline = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
dtype=torch.bfloat16,
)
pipeline.enable_model_cpu_offload()
num_inference_steps를 줄이거나 DPMSolverMultistepScheduler와 같은 더 빠른 스케줄러를 사용하면 지연 시간을 줄일 수 있습니다. 이는 일반적으로 생성 속도를 높이지만, 느리지만 고품질인 스케줄러에 비해 이미지 품질이 떨어질 수 있습니다. 더 많은 속도 최적화 기법은 아래 최적화 기법을 참고하세요.
import time
from diffusers import DPMSolverMultistepScheduler
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
start_time = time.perf_counter()
image = pipeline(prompt, num_inference_steps=25).images[0]
end_time = time.perf_counter()
print(f"Image generation took {end_time - start_time:.3f} seconds")
최적화 기법
스타터 경로가 충분하지 않을 때는 다음 기법을 사용하세요. 추론이 너무 느리면 torch.compile, 캐싱 또는 어텐션 백엔드부터 시작하세요. 메모리가 부족하면 오프로딩 또는 양자화부터 시작하세요.
더 빠른 추론:
- torch.compile — UNet, transformer 또는 VAE를 최적화된 커널로 컴파일합니다.
- 지역별 컴파일 — 반복되는 블록을 컴파일하여
torch.compile의 콜드 스타트 지연을 줄이고 컴파일된 산출물을 재사용합니다. - 캐싱 — 더 높은 속도를 원하고 메모리를 사용할 수 있는 경우, 디노이징 단계 간에 중간 결과를 재사용합니다.
- Attention backends — attention이 병목인 경우, 통합된 API를 통해 Diffusers의 attention 구현을 교체합니다.
- Kernels — 표준 PyTorch를 넘어선 하드웨어별 속도 향상이 필요할 때 최적화된 Hub compute kernels(attention 및 RMSNorm, RoPE와 같은 custom CUDA ops)을 로드합니다.
메모리 사용량 감소:
- Offloading — CPU, model 또는 group offloading을 사용하여 비활성 모델이나 레이어를 CPU로 이동합니다.
- Quantization — 더 작은 가중치를 로드하여 메모리를 줄입니다(일부 백엔드는 추론 속도도 향상시킵니다). GGUF는 일반적인 시작점입니다.
- VAE slicing 및 VAE tiling — 큰 배치나 고해상도 이미지를 조각으로 나누어 디코딩하여 피크 메모리를 낮춥니다.
둘 다:
- 양자화, 컴파일, 오프로드 — 하나의 기법만으로는 충분하지 않을 때 양자화,
torch.compile, 오프로딩을 결합합니다.
Xet Storage Details
- Size:
- 5.12 kB
- Xet hash:
- 10e754d20ff03d7ab55e181c34352a8d0af6c8e4d5c77db3d2bc9a016ecd0378
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.