Diffusers documentation

基本性能

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

基本性能

扩散(Diffusion)是一种计算开销较大的随机过程。您可能需要多次运行 DiffusionPipeline 才能获得理想的输出。因此,仔细平衡生成速度和内存使用量以加快迭代速度至关重要,

本指南推荐了一些使用 DiffusionPipeline 时的基础性能技巧。如需更详细的性能指南,请参阅推理优化部分的文档,例如 Accelerate 推理 或 减少内存使用。

内存使用

减少内存占用可以间接加快生成速度,并有助于模型适配设备。

enable_model_cpu_offload() 方法会在模型未使用时将其移至 CPU,以节省 GPU 内存。

import torch
from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
  "stabilityai/stable-diffusion-xl-base-1.0",
  dtype=torch.bfloat16,
  device_map="cuda"  # or "mps", "xpu", "cpu"
)
pipeline.enable_model_cpu_offload()

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]
print(f"Max memory reserved: {torch.cuda.max_memory_allocated() / 1024**3:.2f} GB")

推理速度

去噪是扩散过程中计算量最大的环节。优化该过程的方法可以加速推理速度。请尝试以下方法以提升速度。

  • 添加 device_map="cuda" 可将流水线放置在 GPU 上。将模型放置在 GPU 等加速器上可以加快处理速度,因为它能够并行执行计算。
  • 设置 dtype=torch.bfloat16 以半精度执行流水线。降低数据类型精度可提高速度,因为在较低精度下执行计算所需的时间更短。
import torch
import time
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler

pipeline = DiffusionPipeline.from_pretrained(
  "stabilityai/stable-diffusion-xl-base-1.0",
  dtype=torch.bfloat16,
  device_map="cuda
)
  • 使用更快的调度器,例如 DPMSolverMultistepScheduler,它仅需约 20-25 步。
  • 将 num_inference_steps 设置为更低的值。减少推理步数会降低整体计算量。但这样做可能会导致生成质量下降。
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)

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
"""

start_time = time.perf_counter()
image = pipeline(prompt).images[0]
end_time = time.perf_counter()

print(f"Image generation took {end_time - start_time:.3f} seconds")

生成质量

许多现代扩散模型开箱即可生成高质量图像。不过,你仍可通过尝试以下方法来进一步提升生成质量。

  • 尝试使用更详细、更具描述性的提示词。包含诸如图像媒介、主体、风格和美学等细节。负面提示词也有助于引导模型避免不理想的特征,例如使用“低质量”或“模糊”等词语。

    import torch
    from diffusers import DiffusionPipeline
    
    pipeline = DiffusionPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0",
        dtype=torch.bfloat16,
        device_map="cuda"  # or "mps", "xpu", "cpu"
    )
    
    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
    """
    negative_prompt = "low quality, blurry, ugly, poor details"
    pipeline(prompt, negative_prompt=negative_prompt).images[0]

    有关创建更优质提示词的详细信息,请参阅Prompt techniques文档。

  • 尝试使用不同的调度器,例如 HeunDiscreteScheduler 或 LMSDiscreteScheduler,它们以牺牲生成速度为代价来提升质量。

    import torch
    from diffusers import DiffusionPipeline, HeunDiscreteScheduler
    
    pipeline = DiffusionPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0",
        dtype=torch.bfloat16,
        device_map="cuda"  # or "mps", "xpu", "cpu"
    )
    pipeline.scheduler = HeunDiscreteScheduler.from_config(pipeline.scheduler.config)
    
    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
    """
    negative_prompt = "low quality, blurry, ugly, poor details"
    pipeline(prompt, negative_prompt=negative_prompt).images[0]

后续步骤

Diffusers 提供了更高级、更强大的优化功能,例如 group-offloading 和 regional compilation。如需了解如何最大化性能,请参阅推理优化部分。

Update on GitHub