Diffusers 文件
Lumina-T2X
並獲得增強的文件體驗
開始使用
Lumina-T2X
Lumina-Next:使用 Next-DiT 讓 Lumina-T2X 更強更快,來自 Alpha-VLLM、OpenGVLab、上海人工智慧實驗室。
論文摘要如下:
Lumina-T2X 是一個基於流的大型擴散變換器(Flag-DiT)的新興系列,它建立了一個統一的框架,用於將噪聲轉化為各種模態,例如影像和影片,並以文字指令為條件。儘管 Lumina-T2X 具有良好的前景,但它仍然面臨著訓練不穩定、推理緩慢和外推偽影等挑戰。本文中,我們介紹了 Lumina-Next,它是 Lumina-T2X 的改進版本,展示了更強的生成效能,並提高了訓練和推理效率。我們首先對 Flag-DiT 架構進行了全面的分析,並確定了幾個次優元件,我們透過引入具有 3D RoPE 和三明治歸一化的 Next-DiT 架構來解決這些問題。為了實現更好的解析度外推,我們徹底比較了應用於文字到影像生成與 3D RoPE 的不同上下文外推方法,並提出了針對擴散變換器量身定製的頻率和時間感知縮放 RoPE。此外,我們引入了 sigmoid 時間離散化排程以減少求解流 ODE 的取樣步驟,以及上下文丟棄方法以合併冗餘視覺標記以實現更快的網路評估,從而有效提高了整體取樣速度。得益於這些改進,Lumina-Next 不僅提高了基本文字到影像生成的質量和效率,還展示了卓越的解析度外推能力和使用基於解碼器的 LLM 作為文字編碼器的多語言生成能力,所有這些都以零樣本方式實現。為了進一步驗證 Lumina-Next 作為一個多功能生成框架,我們將其應用於各種任務,包括視覺識別、多檢視、音訊、音樂和點雲生成,在這些領域都展示了強大的效能。透過在 https://github.com/Alpha-VLLM/Lumina-T2X 釋出所有程式碼和模型權重,我們旨在推動能夠進行通用建模的下一代生成式 AI 的發展。
亮點:Lumina-Next 是一款下一代擴散變換器,透過引入 Next-DiT 架構、3D RoPE 以及頻率和時間感知的 RoPE 等改進,顯著增強了文字到影像生成、多語言生成和多工效能。
Lumina-Next 具有以下元件:
- 它以更少、更快的步驟提高了取樣效率。
- 它使用 Next-DiT 作為變換器骨幹,具有 Sandwichnorm 3D RoPE 和分組查詢注意力。
- 它使用頻率和時間感知的縮放 RoPE。
Lumina-T2X:透過基於流的大型擴散變換器將文字轉化為任何模態、解析度和持續時間,來自 Alpha-VLLM、OpenGVLab、上海人工智慧實驗室。
論文摘要如下:
Sora 揭示了擴充套件擴散變換器在生成任意解析度、寬高比和持續時間的光學真實影像和影片方面的潛力,但它仍然缺乏足夠的實現細節。在本技術報告中,我們介紹了 Lumina-T2X 系列——一個配備零初始化注意力的基於流的大型擴散變換器 (Flag-DiT) 系列,作為一個統一框架,旨在將噪聲轉化為影像、影片、多檢視 3D 物件和音訊剪輯,並以文字指令為條件。透過對潛在時空進行標記化並結合可學習的佔位符,例如 [nextline] 和 [nextframe] 標記,Lumina-T2X 無縫地統一了不同模態在各種時空解析度上的表示。這種統一的方法使得可以在單個框架內訓練不同模態,並允許在推理過程中靈活生成任何解析度、寬高比和長度的多模態資料。RoPE、RMSNorm 和流匹配等先進技術增強了 Flag-DiT 的穩定性、靈活性和可伸縮性,使 Lumina-T2X 的模型能夠擴充套件到 70 億個引數,並將上下文視窗擴充套件到 128K 標記。這對於使用我們的 Lumina-T2I 模型建立超高畫質影像以及使用我們的 Lumina-T2V 模型建立長 720p 影片特別有益。值得注意的是,由 50 億引數 Flag-DiT 提供支援的 Lumina-T2I 僅需要 6 億引數樸素 DiT 35% 的訓練計算成本。我們進一步的全面分析強調了 Lumina-T2X 在解析度外推、高解析度編輯、生成一致 3D 檢視和合成具有無縫過渡的影片方面的初步能力。我們期望 Lumina-T2X 的開源將進一步促進生成式 AI 社群的創造力、透明度和多樣性。
您可以在 Alpha-VLLM 找到原始程式碼庫,在 Alpha-VLLM Lumina Family 找到所有可用的檢查點。
亮點:Lumina-T2X 支援任何模態、解析度和持續時間。
Lumina-T2X 具有以下元件:
- 它使用基於流的大型擴散變換器作為骨幹
- 它支援具有一個骨幹和相應編碼器、解碼器的不同模態。
此管道由 PommesPeter 貢獻。原始程式碼庫可在此處找到。原始權重可在hf.co/Alpha-VLLM下找到。
推理(文字到影像)
使用 torch.compile
減少推理延遲。
首先,載入管道
from diffusers import LuminaPipeline
import torch
pipeline = LuminaPipeline.from_pretrained(
"Alpha-VLLM/Lumina-Next-SFT-diffusers", torch_dtype=torch.bfloat16
).to("cuda")
然後將管道 transformer
和 vae
元件的記憶體佈局更改為 torch.channels-last
。
pipeline.transformer.to(memory_format=torch.channels_last) pipeline.vae.to(memory_format=torch.channels_last)
最後,編譯元件並執行推理。
pipeline.transformer = torch.compile(pipeline.transformer, mode="max-autotune", fullgraph=True)
pipeline.vae.decode = torch.compile(pipeline.vae.decode, mode="max-autotune", fullgraph=True)
image = pipeline(prompt="Upper body of a young woman in a Victorian-era outfit with brass goggles and leather straps. Background shows an industrial revolution cityscape with smoky skies and tall, metal structures").images[0]
量化
量化有助於透過以較低精度資料型別儲存模型權重來減少大型模型的記憶體需求。但是,量化對影片質量的影響可能因影片模型而異。
請參閱量化概述,瞭解更多支援的量化後端以及如何選擇適合您用例的量化後端。以下示例演示如何載入量化的 LuminaPipeline 以使用 bitsandbytes 進行推理。
import torch
from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig, Transformer2DModel, LuminaPipeline
from transformers import BitsAndBytesConfig as BitsAndBytesConfig, T5EncoderModel
quant_config = BitsAndBytesConfig(load_in_8bit=True)
text_encoder_8bit = T5EncoderModel.from_pretrained(
"Alpha-VLLM/Lumina-Next-SFT-diffusers",
subfolder="text_encoder",
quantization_config=quant_config,
torch_dtype=torch.float16,
)
quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
transformer_8bit = Transformer2DModel.from_pretrained(
"Alpha-VLLM/Lumina-Next-SFT-diffusers",
subfolder="transformer",
quantization_config=quant_config,
torch_dtype=torch.float16,
)
pipeline = LuminaPipeline.from_pretrained(
"Alpha-VLLM/Lumina-Next-SFT-diffusers",
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("lumina.png")
LuminaPipeline
class diffusers.LuminaPipeline
< 源 >( transformer: LuminaNextDiT2DModel scheduler: FlowMatchEulerDiscreteScheduler vae: AutoencoderKL text_encoder: GemmaPreTrainedModel tokenizer: typing.Union[transformers.models.gemma.tokenization_gemma.GemmaTokenizer, transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast] )
引數
- vae (AutoencoderKL) — 用於將影像編碼和解碼為潛在表示的變分自編碼器 (VAE) 模型。
- text_encoder (
GemmaPreTrainedModel
) — 凍結的 Gemma 文字編碼器。 - tokenizer (
GemmaTokenizer
或GemmaTokenizerFast
) — Gemma 分詞器。 - transformer (Transformer2DModel) — 用於對編碼影像潛變數進行去噪的文字條件
Transformer2DModel
。 - scheduler (SchedulerMixin) — 用於與
transformer
結合以對編碼影像潛變數進行去噪的排程器。
使用 Lumina-T2I 進行文字到影像生成的管道。
此模型繼承自 DiffusionPipeline。請檢視超類文件,瞭解庫為所有管道實現的通用方法(例如下載或儲存、在特定裝置上執行等)。
__call__
< 源 >( prompt: typing.Union[str, typing.List[str]] = None width: typing.Optional[int] = None height: typing.Optional[int] = None num_inference_steps: int = 30 guidance_scale: float = 4.0 negative_prompt: typing.Union[str, typing.List[str]] = None sigmas: typing.List[float] = None num_images_per_prompt: typing.Optional[int] = 1 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 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 output_type: typing.Optional[str] = 'pil' return_dict: bool = True clean_caption: bool = True max_sequence_length: int = 256 scaling_watershed: typing.Optional[float] = 1.0 proportional_attn: typing.Optional[bool] = True 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'] ) → ImagePipelineOutput 或 tuple
引數
- prompt (
str
或List[str]
, 可選) — 用於引導影像生成的提示詞或提示詞列表。如果未定義,則必須傳遞prompt_embeds
。 - negative_prompt (
str
或List[str]
, 可選) — 不用於引導影像生成的提示詞或提示詞列表。如果未定義,則必須傳遞negative_prompt_embeds
。當不使用引導時(即,如果guidance_scale
小於1
則忽略),此引數將被忽略。 - num_inference_steps (
int
, 可選, 預設為 30) — 去噪步驟的數量。更多的去噪步驟通常會生成更高質量的影像,但推理速度會變慢。 - sigmas (
List[float]
, 可選) — 用於去噪過程的自定義 sigmas,適用於其set_timesteps
方法支援sigmas
引數的排程器。如果未定義,將使用傳遞num_inference_steps
時的預設行為。 - guidance_scale (
float
, 可選, 預設為 4.0) — Classifier-Free Diffusion Guidance 中定義的引導比例。guidance_scale
定義為 Imagen Paper 方程 2 中的w
。透過設定guidance_scale > 1
啟用引導比例。較高的引導比例鼓勵生成與文字prompt
緊密相關的影像,通常以犧牲較低影像質量為代價。 - num_images_per_prompt (
int
, 可選, 預設為 1) — 每個提示詞生成的影像數量。 - height (
int
, 可選, 預設為 self.unet.config.sample_size) — 生成影像的高度(畫素)。 - width (
int
, 可選, 預設為 self.unet.config.sample_size) — 生成影像的寬度(畫素)。 - eta (
float
, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η):https://huggingface.co/papers/2010.02502。僅適用於 schedulers.DDIMScheduler,對其他排程器將被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 一個或多個 torch 生成器,用於使生成具有確定性。 - latents (
torch.Tensor
, 可選) — 預先生成的噪聲潛在變數,從高斯分佈中取樣,用作影像生成的輸入。可用於透過不同的提示調整相同的生成。如果未提供,將使用提供的隨機generator
取樣生成潛在變數張量。 - prompt_embeds (
torch.Tensor
, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示加權。如果未提供,文字嵌入將從prompt
輸入引數生成。 - prompt_attention_mask (
torch.Tensor
, 可選) — 文字嵌入的預生成注意力掩碼。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預先生成的負面文字嵌入。對於 Lumina-T2I,此負面提示應為""。如果未提供,負面提示嵌入將從negative_prompt
輸入引數生成。 - negative_prompt_attention_mask (
torch.Tensor
, 可選) — 負面文字嵌入的預生成注意力掩碼。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影像的輸出格式。在 PIL:PIL.Image.Image
或np.array
之間選擇。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回~pipelines.stable_diffusion.IFPipelineOutput
而不是普通元組。 - clean_caption (
bool
, 可選, 預設為True
) — 在建立嵌入之前是否清理標題。需要安裝beautifulsoup4
和ftfy
。如果未安裝依賴項,嵌入將從原始提示建立。 - max_sequence_length (
int
預設為 120) — 用於prompt
的最大序列長度。 - 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
屬性中列出的變數。
返回
ImagePipelineOutput 或 tuple
如果 return_dict
為 True
,則返回 ImagePipelineOutput,否則返回一個 tuple
,其中第一個元素是生成的影像列表。
呼叫管道進行生成時呼叫的函式。
示例
>>> import torch
>>> from diffusers import LuminaPipeline
>>> pipe = LuminaPipeline.from_pretrained("Alpha-VLLM/Lumina-Next-SFT-diffusers", torch_dtype=torch.bfloat16)
>>> # Enable memory optimizations.
>>> pipe.enable_model_cpu_offload()
>>> prompt = "Upper body of a young woman in a Victorian-era outfit with brass goggles and leather straps. Background shows an industrial revolution cityscape with smoky skies and tall, metal structures"
>>> image = pipe(prompt).images[0]
encode_prompt
< source >( prompt: typing.Union[str, typing.List[str]] do_classifier_free_guidance: bool = True negative_prompt: typing.Union[str, typing.List[str]] = None 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 clean_caption: bool = False **kwargs )
引數
- prompt (
str
或List[str]
, 可選) — 要編碼的提示。 - negative_prompt (
str
或List[str]
, 可選) — 不用於引導影像生成的提示。如果未定義,則必須傳遞negative_prompt_embeds
。當不使用引導時(即,如果guidance_scale
小於1
時),將被忽略。對於 Lumina-T2I,這應為""。 - do_classifier_free_guidance (
bool
, 可選, 預設為True
) — 是否使用無分類器引導。 - num_images_per_prompt (
int
, 可選, 預設為 1) — 每個提示應生成的影像數量。 - device — (
torch.device
, 可選): 將生成的嵌入放置在哪個 torch 裝置上。 - prompt_embeds (
torch.Tensor
, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示加權。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預先生成的負面文字嵌入。對於 Lumina-T2I,它應該是""字串的嵌入。 - clean_caption (
bool
, 預設為False
) — 如果為True
,函式將在編碼前預處理和清理提供的標題。 - max_sequence_length (
int
, 預設為 256) — 用於提示的最大序列長度。
將提示編碼為文字編碼器隱藏狀態。