Diffusers 文件
AuraFlow
並獲得增強的文件體驗
開始使用
AuraFlow
AuraFlow 的靈感來自 Stable Diffusion 3,是迄今為止最大的文字到影像生成模型,採用 Apache 2.0 許可證。該模型在 GenEval 基準測試中取得了最先進的結果。
它由 Fal 團隊開發,更多詳細資訊可在 這篇部落格文章 中找到。
AuraFlow 在消費級硬體裝置上執行可能成本很高。但是,您可以執行一系列最佳化,使其執行更快,並更節省記憶體。有關更多詳細資訊,請檢視 本節。
量化
量化有助於透過以較低精度資料型別儲存模型權重來減少大型模型的記憶體需求。但是,量化對影片質量的影響可能因影片模型而異。
請參閱 量化 概述,瞭解有關支援的量化後端以及選擇支援您用例的量化後端的更多資訊。以下示例演示瞭如何使用 bitsandbytes 載入量化 AuraFlowPipeline 進行推理。
import torch
from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig, AuraFlowTransformer2DModel, AuraFlowPipeline
from transformers import BitsAndBytesConfig as BitsAndBytesConfig, T5EncoderModel
quant_config = BitsAndBytesConfig(load_in_8bit=True)
text_encoder_8bit = T5EncoderModel.from_pretrained(
"fal/AuraFlow",
subfolder="text_encoder",
quantization_config=quant_config,
torch_dtype=torch.float16,
)
quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
transformer_8bit = AuraFlowTransformer2DModel.from_pretrained(
"fal/AuraFlow",
subfolder="transformer",
quantization_config=quant_config,
torch_dtype=torch.float16,
)
pipeline = AuraFlowPipeline.from_pretrained(
"fal/AuraFlow",
text_encoder=text_encoder_8bit,
transformer=transformer_8bit,
torch_dtype=torch.float16,
device_map="balanced",
)
prompt = "a tiny astronaut hatching from an egg on the moon"
image = pipeline(prompt).images[0]
image.save("auraflow.png")
還支援載入 GGUF 檢查點
import torch
from diffusers import (
AuraFlowPipeline,
GGUFQuantizationConfig,
AuraFlowTransformer2DModel,
)
transformer = AuraFlowTransformer2DModel.from_single_file(
"https://huggingface.co/city96/AuraFlow-v0.3-gguf/blob/main/aura_flow_0.3-Q2_K.gguf",
quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
torch_dtype=torch.bfloat16,
)
pipeline = AuraFlowPipeline.from_pretrained(
"fal/AuraFlow-v0.3",
transformer=transformer,
torch_dtype=torch.bfloat16,
)
prompt = "a cute pony in a field of flowers"
image = pipeline(prompt).images[0]
image.save("auraflow.png")
支援 torch.compile()
AuraFlow 可以使用 `torch.compile()` 進行編譯,以加快推理延遲,即使對於不同的解析度也適用。首先,按照此處的說明安裝 PyTorch nightly。以下程式碼片段顯示了啟用此功能所需的更改
+ torch.fx.experimental._config.use_duck_shape = False
+ pipeline.transformer = torch.compile(
pipeline.transformer, fullgraph=True, dynamic=True
)
將 `use_duck_shape` 指定為 `False` 會指示編譯器是否應使用相同的符號變數來表示相同大小的輸入。有關更多詳細資訊,請檢視 此評論。
這使得速度提高了 100%(在低解析度下)到 30%(在 1536x1536 解析度下)。
感謝 AstraliteHeart 幫助我們重寫了 AuraFlowTransformer2DModel 類,以便上述內容適用於不同解析度 (PR)。
AuraFlowPipeline
class diffusers.AuraFlowPipeline
< 源 >( tokenizer: T5Tokenizer text_encoder: UMT5EncoderModel vae: AutoencoderKL transformer: AuraFlowTransformer2DModel scheduler: FlowMatchEulerDiscreteScheduler )
引數
- 分詞器 (
T5TokenizerFast
) — T5Tokenizer 類的分詞器。 - text_encoder (
T5EncoderModel
) — 凍結的文字編碼器。AuraFlow 使用 T5,特別是 EleutherAI/pile-t5-xl 變體。 - vae (AutoencoderKL) — 變分自編碼器 (VAE) 模型,用於將影像編碼和解碼為潛在表示。
- transformer (AuraFlowTransformer2DModel) — 用於對編碼影像潛在表示進行去噪的條件 Transformer (MMDiT 和 DiT) 架構。
- 排程器 (FlowMatchEulerDiscreteScheduler) — 與 `transformer` 結合使用的排程器,用於對編碼影像潛在表示進行去噪。
__call__
< 源 >( prompt: typing.Union[str, typing.List[str]] = None negative_prompt: typing.Union[str, typing.List[str]] = None num_inference_steps: int = 50 sigmas: typing.List[float] = None guidance_scale: float = 3.5 num_images_per_prompt: typing.Optional[int] = 1 height: typing.Optional[int] = 1024 width: typing.Optional[int] = 1024 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None max_sequence_length: int = 256 output_type: typing.Optional[str] = 'pil' return_dict: bool = True attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] )
引數
- prompt (
str
或List[str]
, 可選) — 用於引導影像生成的提示詞。如果未定義,則必須傳入 `prompt_embeds`。 - negative_prompt (
str
或List[str]
, 可選) — 不用於引導影像生成的提示詞。如果未定義,則必須傳入 `negative_prompt_embeds`。當不使用引導時(即,如果 `guidance_scale` 小於 `1` 則忽略),此引數將被忽略。 - height (
int
, 可選, 預設為 self.transformer.config.sample_size * self.vae_scale_factor) — 生成影像的畫素高度。為獲得最佳結果,預設設定為 1024。 - width (
int
, 可選, 預設為 self.transformer.config.sample_size * self.vae_scale_factor) — 生成影像的畫素寬度。為獲得最佳結果,預設設定為 1024。 - num_inference_steps (
int
, 可選, 預設為 50) — 去噪步數。更多去噪步數通常會帶來更高質量的影像,但推理速度會變慢。 - sigmas (
List[float]
, 可選) — 用於覆蓋排程器時間步長間距策略的自定義 sigma 值。如果傳入 `sigmas`,則 `num_inference_steps` 和 `timesteps` 必須為 `None`。 - guidance_scale (
float
, 可選, 預設為 5.0) — Classifier-Free Diffusion Guidance 中定義的引導比例。`guidance_scale` 被定義為 Imagen Paper 方程 2 中的 `w`。透過設定 `guidance_scale > 1` 來啟用引導比例。更高的引導比例鼓勵生成與文字 `prompt` 緊密相關的影像,通常以犧牲影像質量為代價。 - num_images_per_prompt (
int
, 可選, 預設為 1) — 每個提示詞生成的影像數量。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 一個或多個 torch 生成器,用於使生成具有確定性。 - latents (
torch.FloatTensor
, 可選) — 預先生成的噪聲潛在表示,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同的提示詞調整相同的生成。如果未提供,將透過使用提供的隨機 `generator` 進行取樣來生成潛在張量。 - prompt_embeds (
torch.FloatTensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,將從 `prompt` 輸入引數生成文字嵌入。 - prompt_attention_mask (
torch.Tensor
, 可選) — 預生成的文字嵌入注意力掩碼。 - negative_prompt_embeds (
torch.FloatTensor
, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,將從 `negative_prompt` 輸入引數生成 negative_prompt_embeds。 - negative_prompt_attention_mask (
torch.Tensor
, 可選) — 負面文字嵌入的預生成注意力掩碼。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影像的輸出格式。在 PIL:PIL.Image.Image
或np.array
之間選擇。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是普通的元組。 - attention_kwargs (
dict
, 可選) — 一個 kwargs 字典,如果指定,則會傳遞給 diffusers.models.attention_processor 中定義的 `self.processor` 下的 `AttentionProcessor`。 - callback_on_step_end (
Callable
, 可選) — 在推理過程中每個去噪步驟結束時呼叫的函式。該函式將使用以下引數呼叫:`callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`。`callback_kwargs` 將包含由 `callback_on_step_end_tensor_inputs` 指定的所有張量列表。 - callback_on_step_end_tensor_inputs (
List
, 可選) — `callback_on_step_end` 函式的張量輸入列表。列表中指定的張量將作為 `callback_kwargs` 引數傳遞。您只能包含管道類 `._callback_tensor_inputs` 屬性中列出的變數。 - max_sequence_length (
int
預設為 256) — 與 `prompt` 一起使用的最大序列長度。
呼叫管道進行生成時呼叫的函式。
示例
>>> import torch
>>> from diffusers import AuraFlowPipeline
>>> pipe = AuraFlowPipeline.from_pretrained("fal/AuraFlow", torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> prompt = "A cat holding a sign that says hello world"
>>> image = pipe(prompt).images[0]
>>> image.save("aura_flow.png")
返回:ImagePipelineOutput 或 tuple
:如果 `return_dict` 為 `True`,則返回 ImagePipelineOutput,否則返回一個 `tuple`,其中第一個元素是包含生成影像的列表。
encode_prompt
< 源 >( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str]] = None do_classifier_free_guidance: bool = True num_images_per_prompt: int = 1 device: typing.Optional[torch.device] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None max_sequence_length: int = 256 lora_scale: typing.Optional[float] = None )
引數
- prompt (
str
或List[str]
, 可選) — 要編碼的提示詞。 - negative_prompt (
str
或List[str]
, 可選) — 不用於引導影像生成的提示詞。如果未定義,則必須傳遞negative_prompt_embeds
。當不使用引導時(即,如果guidance_scale
小於1
時),此引數將被忽略。 - do_classifier_free_guidance (
bool
, 可選, 預設為True
) — 是否使用分類器自由引導。 - num_images_per_prompt (
int
, 可選, 預設為 1) — 每個提示詞應生成的影像數量。 - device — (
torch.device
, 可選): 放置結果嵌入的 torch 裝置。 - prompt_embeds (
torch.Tensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,則將根據prompt
輸入引數生成文字嵌入。 - prompt_attention_mask (
torch.Tensor
, 可選) — 預生成的文字嵌入注意力掩碼。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預生成的負向文字嵌入。 - negative_prompt_attention_mask (
torch.Tensor
, 可選) — 預生成的負向文字嵌入注意力掩碼。 - max_sequence_length (
int
, 預設為 256) — 用於提示詞的最大序列長度。 - lora_scale (
float
, 可選) — 如果載入了 LoRA 層,將應用於文字編碼器所有 LoRA 層的 LoRA 比例。
將提示編碼為文字編碼器隱藏狀態。