Diffusers documentation
DreamLite
DreamLite
DreamLite là một mô hình tạo ảnh từ văn bản và chỉnh sửa ảnh của ByteDance. Nó kết hợp một 2D U-Net tùy chỉnh (DreamLiteUNetModel) với bộ mã hóa đa phương thức Qwen3-VL làm bộ mã hóa prompt / hướng dẫn ảnh, và sử dụng VAE AutoencoderTiny (kiểu TAESD) để mã hóa/giải mã latent nhanh chóng.
Có hai pipeline được cung cấp:
| Pipeline | Các chế độ | CFG | Trường hợp sử dụng |
|---|---|---|---|
| DreamLitePipeline | text-to-image và chỉnh sửa ảnh (tự động chọn dựa trên việc image có phải là None hay không) | 3-branch dual CFG (guidance_scale trên nhánh text, image_guidance_scale trên nhánh image, theo phong cách InstructPix2Pix) | Chất lượng cao nhất |
| DreamLiteMobilePipeline | text-to-image và chỉnh sửa ảnh (tự động chọn dựa trên việc image có phải là None hay không) | Không có — đã được chưng cất, chỉ một lần chạy UNet forward cho mỗi bước | Trên thiết bị / độ trễ thấp |
Các checkpoint chính thức:
- Mô hình cơ sở: carlofkl/DreamLite-base
- Mô hình di động đã được chưng cất: carlofkl/DreamLite-mobile
Cả hai pipeline đều tự động phát hiện chế độ text-to-image hay image-editing dựa trên việc có cung cấp đối số
imagehay không. Không có lớpImg2Imgriêng biệt.
Khi nạp ảnh đầu vào để chỉnh sửa, hãy ưu tiên sử dụng
diffusers.utils.load_image(...)thay vìPIL.Image.open(...)thô.load_imagethực thi việc chuyển đổi sang RGB và áp dụng định hướng EXIF, cả hai đều là những giả định của pipeline. Việc sử dụngImage.openđơn thuần trên nguồn RGBA / bảng màu / xoay EXIF sẽ âm thầm tạo ra điều kiện tiềm ẩn khác nhau và làm giảm chất lượng đầu ra.
Văn bản sang hình ảnh (Cơ bản)
import torch
from diffusers import DreamLitePipeline
pipe = DreamLitePipeline.from_pretrained("carlofkl/DreamLite-base", revision="diffusers", dtype=torch.bfloat16)
pipe = pipe.to("cuda") # or "mps", "xpu", "cpu"
image = pipe(
prompt="a dog running on the grass",
negative_prompt="",
height=1024,
width=1024,
num_inference_steps=28,
generator=torch.Generator("cpu").manual_seed(42),
).images[0]
image.save("dreamlite_t2i.png")Chỉnh sửa hình ảnh (Cơ bản)
Truyền một image để vào chế độ chỉnh sửa. Cả guidance_scale (nhánh văn bản) và image_guidance_scale (nhánh hình ảnh) đều được kích hoạt ở đây.
import torch
from diffusers import DreamLitePipeline
from diffusers.utils import load_image
pipe = DreamLitePipeline.from_pretrained("carlofkl/DreamLite-base", revision="diffusers", dtype=torch.bfloat16)
pipe = pipe.to("cuda") # or "mps", "xpu", "cpu"
source = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png")
image = pipe(
prompt="turn the cat into a corgi",
image=source,
height=1024,
width=1024,
num_inference_steps=28,
generator=torch.Generator("cpu").manual_seed(42),
).images[0]
image.save("dreamlite_edit.png")Văn bản sang hình ảnh (Di động)
Pipeline di động được chưng cất và bỏ qua hoàn toàn CFG — chỉ một bước forward của UNet cho mỗi bước. Nó nhận cùng các đối số prompt / height / width / num_inference_steps, nhưng bỏ qua guidance_scale và image_guidance_scale nếu được truyền vào (một cảnh báo sẽ được ghi log).
import torch
from diffusers import DreamLiteMobilePipeline
pipe = DreamLiteMobilePipeline.from_pretrained("carlofkl/DreamLite-mobile", revision="diffusers", dtype=torch.bfloat16)
pipe = pipe.to("cuda") # or "mps", "xpu", "cpu"
image = pipe(
prompt="a dog running on the grass",
height=1024,
width=1024,
num_inference_steps=4,
generator=torch.Generator("cpu").manual_seed(42),
).images[0]
image.save("dreamlite_mobile_t2i.png")Chỉnh sửa hình ảnh (Di động)
import torch
from diffusers import DreamLiteMobilePipeline
from diffusers.utils import load_image
pipe = DreamLiteMobilePipeline.from_pretrained("carlofkl/DreamLite-mobile", revision="diffusers", dtype=torch.bfloat16)
pipe = pipe.to("cuda") # or "mps", "xpu", "cpu"
source = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png")
image = pipe(
prompt="turn the cat into a corgi",
image=source,
height=1024,
width=1024,
num_inference_steps=4,
generator=torch.Generator("cpu").manual_seed(42),
).images[0]
image.save("dreamlite_mobile_edit.png")Ghi chú và hạn chế
- Cả hai pipeline đều ép
batch_size = 1bên trong;num_images_per_promptkiểm soát số lượng mẫu được lấy từ cùng một prompt thay vì xử lý song song theo batch. - Bộ mã hóa prompt là
Qwen3-VL, một mô hình đa phương thức. Do đó, việc tải toàn bộ pipeline đòi hỏi bộ nhớ GPU đủ lớn cho cả U-Net và bộ mã hóa văn bản Qwen3-VL (~4 GB + ~0,7 GB ở bf16 cho bản phát hành cơ sở). - VAE là
AutoencoderTinyvà cóencoder_block_out_channels;vae_scale_factorđược suy ra từ nó khi khởi tạo pipeline.
DreamLitePipeline
lớp diffusers.DreamLitePipeline
< source >( text_encoder: Qwen3VLForConditionalGenerationtokenizer: AutoTokenizerprocessor: Qwen3VLProcessorvae: AutoencoderTinyunet: DreamLiteUNetModelscheduler: FlowMatchEulerDiscreteScheduler )
DreamLite pipeline cho việc tạo ảnh từ văn bản và chỉnh sửa ảnh dựa trên hướng dẫn.
Cùng một pipeline hỗ trợ cả hai chế độ; chế độ hoạt động được tự động phát hiện từ các đầu vào:
image is None-> text-to-image (một CFG duy nhất trên văn bản).image is not None-> chỉnh sửa từ ảnh sang ảnh / theo hướng dẫn (CFG kép: văn bản + ảnh).
Các thành phần: text_encoder ([~transformers.Qwen3VLForConditionalGeneration]): Bộ mã hóa văn bản/thị giác đa phương thức được sử dụng để tạo ra các nhúng điều kiện. tokenizer ([~transformers.AutoTokenizer]): Bộ phân từ cho chế độ chỉ văn bản (generate). processor ([~transformers.Qwen3VLProcessor]): Bộ xử lý đa phương thức cho chế độ chỉnh sửa (mẫu văn bản + hình ảnh). vae ([~diffusers.AutoencoderTiny]): VAE nhỏ thân thiện với thiết bị di động để mã hóa/giải mã ẩn. unet ([~diffusers.DreamLiteUNetModel]): DreamLite UNet (GQA + qk_norm + các lớp tích chập tách biệt theo chiều sâu). scheduler ([~diffusers.FlowMatchEulerDiscreteScheduler]): Trình lập lịch Euler khớp dòng với dịch chuyển động.
Lưu ý: batch_size hiện bị ép thành 1; num_images_per_prompt được hỗ trợ.
__call__
< source >( prompt: typing.Optional[str] = Nonenegative_prompt: typing.Optional[str] = Noneimage: typing.Optional[PIL.Image.Image] = Noneheight: typing.Optional[int] = Nonewidth: typing.Optional[int] = Noneguidance_scale: float = 3.5image_guidance_scale: float = 1.5num_inference_steps: int = 30sigmas: typing.Optional[typing.List[float]] = Nonenum_images_per_prompt: typing.Optional[int] = 1generator: typing.Union[torch.Generator, typing.List[torch.Generator], NoneType] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truemax_sequence_length: int = 200text_pad_embedding: typing.Optional[torch.Tensor] = None )
Tham số
- prompt — Prompt văn bản.
- negative_prompt — Prompt văn bản tiêu cực (mặc định là chuỗi rỗng).
- image — Ảnh đầu vào tùy chọn. Nếu được cung cấp, pipeline sẽ chạy ở chế độ edit / image-to-image với dual classifier-free guidance; nếu không, nó sẽ chạy ở chế độ text-to-image.
- height — Độ phân giải đầu ra (chiều cao). Mặc định là
default_sample_size * vae_scale_factor(1024). Giá trị mặc định này áp dụng cho cả T2I và I2I; truyền một giá trị rõ ràng để ghi đè. - width — Độ phân giải đầu ra (chiều rộng). Mặc định là
default_sample_size * vae_scale_factor(1024). Mặc định này áp dụng cho cả T2I và I2I; truyền một giá trị rõ ràng để ghi đè. - guidance_scale — thang CFG trên nhánh văn bản (cả hai chế độ).
- image_guidance_scale — Thang độ CFG bổ sung trên nhánh ảnh (chỉ ở chế độ chỉnh sửa).
- num_inference_steps — Số bước khử nhiễu.
- sigmas — FlowMatch sigmas tường minh tùy chọn; mặc định là linspace đều.
- num_images_per_prompt — Số ảnh đầu ra cho mỗi prompt (lưu ý:
batch_sizeđược ép về 1). - generator — Bộ tạo ngẫu nhiên.
- output_type —
"pil","np","pt"hoặc"latent". - return_dict — Nếu là True, trả về một DreamLitePipelineOutput; nếu không, trả về một tuple
(images,). - max_sequence_length — Số lượng token tối đa của prompt người dùng được giữ lại sau khi loại bỏ phần tiền tố của chat-template. Chỉ áp dụng cho chế độ
generate(chế độeditsử dụng padding gốc của bộ xử lý đa phương thức). - text_pad_embedding — Embedding pad học được tùy chọn cho các vị trí bị che.
Chạy pipeline DreamLite.
DreamLiteMobilePipeline
lớp diffusers.DreamLiteMobilePipeline
< source >( text_encoder: Qwen3VLForConditionalGenerationtokenizer: AutoTokenizerprocessor: Qwen3VLProcessorvae: AutoencoderTinyunet: DreamLiteUNetModelscheduler: FlowMatchEulerDiscreteScheduler )
Pipeline Mobile của DreamLite: một biến thể đã được chưng cất, không sử dụng classifier-free-guidance của DreamLitePipeline để suy luận nhanh với ít bước (mặc định là 4 bước).
Chế độ hoạt động được tự động phát hiện từ các đầu vào (giống như pipeline cơ sở):
image is None-> text-to-image.image is not None-> chỉnh sửa theo hướng dẫn từ ảnh sang ảnh.
Vì classifier-free guidance đã được loại bỏ, guidance_scale và image_guidance_scale được chấp nhận để đảm bảo tương thích API với DreamLitePipeline nhưng bị bỏ qua trong vòng lặp khử nhiễu. negative_prompt được cố ý không có.
Các thành phần (giống với pipeline cơ sở): text_encoder ([~transformers.Qwen3VLForConditionalGeneration]): Bộ mã hóa văn bản/thị giác đa phương tiện. tokenizer ([~transformers.AutoTokenizer]): Tokenizer cho chế độ chỉ văn bản (generate). processor ([~transformers.Qwen3VLProcessor]): Bộ xử lý đa phương tiện cho chế độ edit. vae ([~diffusers.AutoencoderTiny]): VAE nhỏ thân thiện với thiết bị di động. unet ([~diffusers.DreamLiteUNetModel]): DreamLite UNet. scheduler ([~diffusers.FlowMatchEulerDiscreteScheduler]): Flow-matching Euler scheduler với dynamic shift.
Lưu ý: batch_size hiện bị ép thành 1; num_images_per_prompt được hỗ trợ.
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = Noneimage: typing.Optional[PIL.Image.Image] = Noneheight: typing.Optional[int] = Nonewidth: typing.Optional[int] = Nonenum_inference_steps: int = 4guidance_scale: typing.Optional[float] = Noneimage_guidance_scale: typing.Optional[float] = Nonesigmas: typing.Optional[typing.List[float]] = Nonenum_images_per_prompt: typing.Optional[int] = 1generator: typing.Union[torch.Generator, typing.List[torch.Generator], NoneType] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truemax_sequence_length: int = 200text_pad_embedding: typing.Optional[torch.Tensor] = None )
Tham số
- prompt — Prompt văn bản.
- image — Ảnh đầu vào tùy chọn. Nếu được cung cấp, sẽ chạy ở chế độ edit / image-to-image; nếu không, sẽ chạy ở chế độ text-to-image.
- height — Độ phân giải đầu ra (chiều cao). Mặc định là
default_sample_size * vae_scale_factor(1024). - width — Độ phân giải đầu ra (chiều rộng). Mặc định là
default_sample_size * vae_scale_factor(1024). - num_inference_steps — Số bước khử nhiễu. Mặc định là 4 (đã chưng cất).
- guidance_scale — Được chấp nhận để đảm bảo tương thích API với DreamLitePipeline; bị bỏ qua vì CFG đã được chưng cất.
- image_guidance_scale — Được chấp nhận để đảm bảo tương thích API với DreamLitePipeline; bị bỏ qua vì CFG đã được chưng cất.
- sigmas — FlowMatch sigmas tường minh tùy chọn; mặc định là linspace đều.
- num_images_per_prompt — Số ảnh đầu ra cho mỗi prompt (lưu ý:
batch_sizeđược ép về 1). - generator — Bộ tạo ngẫu nhiên.
- output_type —
"pil","np","pt"hoặc"latent". - return_dict — Nếu là True, trả về một DreamLitePipelineOutput; nếu không, trả về
(images,). - max_sequence_length — Số lượng token tối đa của prompt người dùng được giữ lại sau khi loại bỏ phần tiền tố của chat-template. Chỉ áp dụng cho chế độ
generate(chế độeditsử dụng padding gốc của bộ xử lý đa phương thức). - text_pad_embedding — Embedding pad học được tùy chọn cho các vị trí bị che.
Chạy pipeline DreamLite Mobile đã được chưng cất.
DreamLitePipelineOutput
lớp diffusers.DreamLitePipelineOutput
< source >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
Lớp đầu ra cho các pipeline DreamLite.