Diffusers documentation
Hiệu suất cơ bản
Hiệu suất cơ bản
Diffusion là một quá trình ngẫu nhiên và đòi hỏi nhiều về mặt tính toán. Bạn có thể cần chạy DiffusionPipeline nhiều lần trước khi có được đầu ra mong muốn. Đó là lý do tại sao việc cân bằng cẩn thận giữa tốc độ tạo và mức sử dụng bộ nhớ là rất quan trọng để lặp lại nhanh hơn,
Hướng dẫn này đề xuất một số mẹo hiệu suất cơ bản khi sử dụng DiffusionPipeline. Tham khảo tài liệu phần Tối ưu hóa suy luận như Accelerate inference hoặc Giảm mức sử dụng bộ nhớ để có các hướng dẫn hiệu suất chi tiết hơn.
Lượng bộ nhớ sử dụng
Việc giảm lượng bộ nhớ sử dụng sẽ gián tiếp tăng tốc độ sinh và có thể giúp mô hình vừa với thiết bị.
Phương thức enable_model_cpu_offload() di chuyển mô hình sang CPU khi nó không được sử dụng để tiết kiệm bộ nhớ 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")Tốc độ suy luận
Denoising là quá trình đòi hỏi nhiều tài nguyên tính toán nhất trong diffusion. Các phương pháp tối ưu hóa quá trình này sẽ tăng tốc độ suy luận. Hãy thử các phương pháp sau để tăng tốc.
- Thêm
device_map="cuda"để đặt pipeline trên GPU. Việc đặt mô hình trên bộ tăng tốc, chẳng hạn như GPU, sẽ tăng tốc độ vì nó thực hiện các phép tính song song. - Đặt
dtype=torch.bfloat16để thực thi pipeline ở độ chính xác nửa. Giảm độ chính xác của kiểu dữ liệu sẽ tăng tốc độ vì việc thực hiện các phép tính ở độ chính xác thấp hơn mất ít thời gian hơn.
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
)- Sử dụng trình lập lịch nhanh hơn, chẳng hạn như DPMSolverMultistepScheduler, chỉ cần khoảng 20-25 bước.
- Đặt
num_inference_stepsthành một giá trị thấp hơn. Việc giảm số bước suy luận sẽ làm giảm tổng số phép tính. Tuy nhiên, điều này có thể dẫn đến chất lượng sinh thành thấp hơn.
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")Chất lượng sinh
Nhiều mô hình khuếch tán hiện đại tạo ra hình ảnh chất lượng cao ngay khi sử dụng. Tuy nhiên, bạn vẫn có thể cải thiện chất lượng tạo hình bằng cách thử các phương pháp sau.
Hãy thử một prompt chi tiết và mô tả hơn. Bao gồm các chi tiết như phương tiện hình ảnh, chủ thể, phong cách và thẩm mỹ. Một negative prompt cũng có thể hữu ích bằng cách hướng mô hình tránh các đặc điểm không mong muốn bằng cách sử dụng các từ như low quality hoặc blurry.
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]Để biết thêm chi tiết về cách tạo các prompt tốt hơn, hãy xem tài liệu Kỹ thuật prompt.
Thử một trình lập lịch khác, chẳng hạn như HeunDiscreteScheduler hoặc LMSDiscreteScheduler, chấp nhận giảm tốc độ tạo để đổi lấy chất lượng.
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]
Các bước tiếp theo
Diffusers cung cấp các tối ưu hóa nâng cao và mạnh mẽ hơn như group-offloading và regional compilation. Để tìm hiểu cách tối đa hóa hiệu suất, hãy xem phần Tối ưu hóa suy luận.
Update on GitHub