Diffusers 文件
Hunyuan-DiT
並獲得增強的文件體驗
開始使用
Hunyuan-DiT
Hunyuan-DiT:一個強大的多解析度擴散Transformer,具有細粒度中文理解能力,來自騰訊混元。
論文摘要如下:
我們推出了Hunyuan-DiT,一個文字到影像擴散Transformer,能夠細粒度地理解英語和中文。為了構建Hunyuan-DiT,我們精心設計了Transformer結構、文字編碼器和位置編碼。我們還從頭開始構建了一個完整的資料管道,用於迭代模型最佳化中的資料更新和評估。為了實現細粒度的語言理解,我們訓練了一個多模態大語言模型來完善影像的說明文字。最後,Hunyuan-DiT可以與使用者進行多輪多模態對話,根據上下文生成和細化影像。透過我們由50多名專業人工評估員進行的整體人工評估協議,Hunyuan-DiT在中文到影像生成方面,相較於其他開源模型,樹立了新的SOTA。
您可以在Tencent/HunyuanDiT找到原始程式碼庫,在Tencent-Hunyuan找到所有可用檢查點。
亮點:HunyuanDiT支援中/英文到影像、多解析度生成。
HunyuanDiT 包含以下元件
- 它使用擴散Transformer作為骨幹
- 它結合了兩個文字編碼器:一個雙語CLIP和一個多語言T5編碼器
請務必檢視[排程器](guide)指南,瞭解如何在排程器速度和質量之間進行權衡,並參閱[在管道之間重用元件](reuse components across pipelines)部分,瞭解如何高效地將相同元件載入到多個管道中。
您可以透過將`HungyuanDiTPipeline`生成的影像傳遞給SDXL 精煉模型來進一步提高生成質量。
最佳化
您可以使用 `torch.compile` 和前饋分塊來最佳化管道的執行時和記憶體消耗。要了解其他最佳化方法,請查閱加速推理和減少記憶體使用指南。
推理
使用 torch.compile
減少推理延遲。
首先,載入管道
from diffusers import HunyuanDiTPipeline
import torch
pipeline = HunyuanDiTPipeline.from_pretrained(
"Tencent-Hunyuan/HunyuanDiT-Diffusers", torch_dtype=torch.float16
).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="一個宇航員在騎馬").images[0]
在 80GB A100 機器上的基準測試結果如下:
With torch.compile(): Average inference time: 12.470 seconds. Without torch.compile(): Average inference time: 20.570 seconds.
記憶體最佳化
透過載入 8 位 T5 文字編碼器,您可以在不到 6 GB 的 GPU 視訊記憶體中執行管道。有關詳細資訊,請參閱此指令碼。
此外,您可以使用 enable_forward_chunking() 方法來減少記憶體使用。前饋分塊會以迴圈方式而不是一次性地執行 Transformer 塊中的前饋層。這可在記憶體消耗和推理執行時之間提供一個權衡。
+ pipeline.transformer.enable_forward_chunking(chunk_size=1, dim=1)
HunyuanDiTPipeline
class diffusers.HunyuanDiTPipeline
< 來源 >( vae: AutoencoderKL text_encoder: BertModel tokenizer: BertTokenizer transformer: HunyuanDiT2DModel scheduler: DDPMScheduler safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor requires_safety_checker: bool = True text_encoder_2: typing.Optional[transformers.models.t5.modeling_t5.T5EncoderModel] = None tokenizer_2: typing.Optional[transformers.models.mt5.tokenization_mt5.MT5Tokenizer] = None )
引數
- vae (AutoencoderKL) — 用於將影像編碼和解碼為潛在表示的變分自編碼器 (VAE) 模型。我們使用 `sdxl-vae-fp16-fix`。
- text_encoder (可選[
~transformers.BertModel
,~transformers.CLIPTextModel
]) — 凍結的文字編碼器(clip-vit-large-patch14)。HunyuanDiT 使用一個經過微調的[雙語 CLIP]。 - tokenizer (可選[
~transformers.BertTokenizer
,~transformers.CLIPTokenizer
]) — 用於標記文字的 `BertTokenizer` 或 `CLIPTokenizer`。 - transformer (HunyuanDiT2DModel) — 騰訊混元設計的 HunyuanDiT 模型。
- text_encoder_2 (
T5EncoderModel
) — mT5 嵌入器。具體來說,它是“t5-v1_1-xxl”。 - tokenizer_2 (
MT5Tokenizer
) — 用於 mT5 嵌入器的分詞器。 - scheduler (DDPMScheduler) — 與 HunyuanDiT 結合使用的排程器,用於對編碼影像的潛在空間進行去噪。
用於使用 HunyuanDiT 進行英語/中文到影像生成的管道。
此模型繼承自DiffusionPipeline。有關庫為所有管道實現的通用方法(例如下載或儲存、在特定裝置上執行等),請查閱超類文件。
HunyuanDiT 使用兩個文字編碼器:mT5 和 [雙語 CLIP](我們自己微調的)
__call__
< 來源 >( prompt: typing.Union[str, typing.List[str]] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: typing.Optional[int] = 50 guidance_scale: typing.Optional[float] = 5.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 eta: typing.Optional[float] = 0.0 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_embeds_2: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds_2: typing.Optional[torch.Tensor] = None prompt_attention_mask: typing.Optional[torch.Tensor] = None prompt_attention_mask_2: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask_2: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: 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'] guidance_rescale: float = 0.0 original_size: typing.Optional[typing.Tuple[int, int]] = (1024, 1024) target_size: typing.Optional[typing.Tuple[int, int]] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) use_resolution_binning: bool = True ) → StableDiffusionPipelineOutput 或 tuple
引數
- prompt (
str
或List[str]
, *可選*) — 用於引導影像生成的提示詞。如果未定義,您需要傳遞 `prompt_embeds`。 - height (
int
) — 生成影像的高度(畫素)。 - width (
int
) — 生成影像的寬度(畫素)。 - num_inference_steps (
int
, *可選*, 預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高質量的影像,但代價是推理速度變慢。此引數受 `strength` 調製。 - guidance_scale (
float
, *可選*, 預設為 7.5) — 較高的引導比例值鼓勵模型生成與文字 `prompt` 緊密相關的影像,但代價是影像質量較低。當 `guidance_scale > 1` 時啟用引導比例。 - negative_prompt (
str
或List[str]
, *可選*) — 用於引導影像生成中不包含的內容的提示詞。如果未定義,您需要改為傳遞 `negative_prompt_embeds`。當不使用引導時 (`guidance_scale < 1`),此引數將被忽略。 - num_images_per_prompt (
int
, 可選, 預設為 1) — 每個提示詞生成的影像數量。 - eta (
float
, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 一個torch.Generator
,用於使生成具有確定性。 - prompt_embeds (
torch.Tensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入(提示權重)。如果未提供,則從prompt
輸入引數生成文字嵌入。 - prompt_embeds_2 (
torch.Tensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入(提示權重)。如果未提供,則從prompt
輸入引數生成文字嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入(提示權重)。如果未提供,negative_prompt_embeds
將從negative_prompt
輸入引數生成。 - negative_prompt_embeds_2 (
torch.Tensor
, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入(提示權重)。如果未提供,negative_prompt_embeds
將從negative_prompt
輸入引數生成。 - prompt_attention_mask (
torch.Tensor
, 可選) — 提示詞的注意力掩碼。當直接傳入prompt_embeds
時,此項為必需。 - prompt_attention_mask_2 (
torch.Tensor
, 可選) — 提示詞的注意力掩碼。當直接傳入prompt_embeds_2
時,此項為必需。 - negative_prompt_attention_mask (
torch.Tensor
, 可選) — 負面提示詞的注意力掩碼。當直接傳入negative_prompt_embeds
時,此項為必需。 - negative_prompt_attention_mask_2 (
torch.Tensor
, 可選) — 負面提示詞的注意力掩碼。當直接傳入negative_prompt_embeds_2
時,此項為必需。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影像的輸出格式。在PIL.Image
或np.array
之間選擇。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通的元組。 - callback_on_step_end (
Callable[[int, int, Dict], None]
,PipelineCallback
,MultiPipelineCallbacks
, 可選) — 在每個去噪步驟結束時呼叫的回撥函式或回撥函式列表。 - callback_on_step_end_tensor_inputs (
List[str]
, 可選) — 應該傳遞給回撥函式的張量輸入列表。如果未定義,則將傳遞所有張量輸入。 - guidance_rescale (
float
, 可選, 預設為 0.0) — 根據guidance_rescale
重新縮放 noise_cfg。基於 Common Diffusion Noise Schedules and Sample Steps are Flawed 的發現。參見第 3.4 節。 - original_size (
Tuple[int, int]
, 可選, 預設為(1024, 1024)
) — 影像的原始尺寸。用於計算時間 ID。 - target_size (
Tuple[int, int]
, 可選) — 影像的目標尺寸。用於計算時間 ID。 - crops_coords_top_left (
Tuple[int, int]
, 可選, 預設為(0, 0)
) — 裁剪的左上角座標。用於計算時間 ID。 - use_resolution_binning (
bool
, 可選, 預設為True
) — 是否使用解析度分箱。如果為True
,輸入解析度將對映到最接近的標準解析度。支援的解析度為 1024x1024、1280x1280、1024x768、1152x864、1280x960、768x1024、864x1152、960x1280、1280x768 和 768x1280。建議將其設定為True
。
返回
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
為 True
,則返回 StableDiffusionPipelineOutput,否則返回一個 tuple
,其中第一個元素是生成的影像列表,第二個元素是指示相應生成的影像是否包含“不適合工作”(nsfw)內容的 bool
列表。
使用 HunyuanDiT 生成的管線呼叫函式。
示例
>>> import torch
>>> from diffusers import HunyuanDiTPipeline
>>> pipe = HunyuanDiTPipeline.from_pretrained(
... "Tencent-Hunyuan/HunyuanDiT-Diffusers", torch_dtype=torch.float16
... )
>>> pipe.to("cuda")
>>> # You may also use English prompt as HunyuanDiT supports both English and Chinese
>>> # prompt = "An astronaut riding a horse"
>>> prompt = "一個宇航員在騎馬"
>>> image = pipe(prompt).images[0]
encode_prompt
< 源 >( prompt: str device: device = None dtype: dtype = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: typing.Optional[str] = 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: typing.Optional[int] = None text_encoder_index: int = 0 )
引數
- prompt (
str
或List[str]
, 可選) — 待編碼的提示詞。 - device — (
torch.device
): torch 裝置。 - dtype (
torch.dtype
) — torch 資料型別。 - num_images_per_prompt (
int
) — 每個提示詞應生成的影像數量。 - do_classifier_free_guidance (
bool
) — 是否使用分類器自由引導。 - negative_prompt (
str
或List[str]
, 可選) — 不用於指導影像生成的提示詞。如果未定義,則必須傳入negative_prompt_embeds
。當不使用引導時(即,如果guidance_scale
小於1
),則忽略。 - prompt_embeds (
torch.Tensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds
將從negative_prompt
輸入引數生成。 - prompt_attention_mask (
torch.Tensor
, 可選) — 提示詞的注意力掩碼。當直接傳入prompt_embeds
時,此項為必需。 - negative_prompt_attention_mask (
torch.Tensor
, 可選) — 負面提示詞的注意力掩碼。當直接傳入negative_prompt_embeds
時,此項為必需。 - max_sequence_length (
int
, 可選) — 提示詞使用的最大序列長度。 - text_encoder_index (
int
, 可選) — 要使用的文字編碼器索引。0
代表 clip,1
代表 T5。
將提示編碼為文字編碼器隱藏狀態。