Diffusers 文件
AudioLDM 2
並獲得增強的文件體驗
開始使用
AudioLDM 2
AudioLDM 2 由 Haohe Liu 等人在 AudioLDM 2: Learning Holistic Audio Generation with Self-supervised Pretraining 中提出。AudioLDM 2 接收文字提示作為輸入,並預測相應的音訊。它可以生成文字條件的音效、人聲和音樂。
受 Stable Diffusion 的啟發,AudioLDM 2 是一個文字到音訊的潛在擴散模型(LDM),它從文字嵌入中學習連續的音訊表示。兩個文字編碼器模型用於從提示輸入中計算文字嵌入:CLAP 的文字分支和 Flan-T5 的編碼器。這些文字嵌入隨後透過 AudioLDM2ProjectionModel 投射到一個共享的嵌入空間。一個 GPT2 語言模型(LM)用於根據投射的 CLAP 和 Flan-T5 嵌入自迴歸地預測八個新的嵌入向量。生成的嵌入向量和 Flan-T5 文字嵌入在 LDM 中用作交叉注意力條件。AudioLDM 2 的 UNet 的獨特之處在於它接受兩個交叉注意力嵌入,而不是像大多數其他 LDM 中那樣只接受一個交叉注意力條件。
論文摘要如下:
儘管音訊生成在不同型別的音訊(如語音、音樂和音效)之間存在共同點,但為每種型別設計模型需要仔細考慮其特定目標和偏差,這些目標和偏差可能與其他型別顯著不同。為了讓我們更接近音訊生成的統一視角,本文提出了一個框架,該框架對語音、音樂和音效生成使用相同的學習方法。我們的框架引入了一種通用的音訊表示,稱為“音訊語言”(LOA)。任何音訊都可以根據 AudioMAE(一種自監督預訓練表示學習模型)翻譯成 LOA。在生成過程中,我們使用 GPT-2 模型將任何模態翻譯成 LOA,並使用以 LOA 為條件的潛在擴散模型執行自監督音訊生成學習。所提出的框架自然帶來了優勢,如上下文學習能力和可重用的自監督預訓練 AudioMAE 和潛在擴散模型。在文字到音訊、文字到音樂和文字到語音的主要基準測試中進行的實驗表明,其效能達到或超過了現有方法。我們的程式碼、預訓練模型和演示可在 此連結 獲取。
此流水線由 sanchit-gandhi 和 Nguyễn Công Tú Anh 貢獻。原始程式碼庫可在 haoheliu/audioldm2 找到。
提示
選擇檢查點
AudioLDM2 有三種變體。其中兩個檢查點適用於文字到音訊生成的一般任務。第三個檢查點專門用於文字到音樂生成。
所有檢查點共享相同的文字編碼器和 VAE 模型大小。它們在 UNet 的大小和深度上有所不同。有關三個檢查點的詳細資訊,請參見下表。
模型權重 | 任務 | UNet 模型大小 | 總模型大小 | 訓練資料/小時 |
---|---|---|---|---|
audioldm2 | 文字到音訊 | 350M | 1.1B | 1150k |
audioldm2-large | 文字到音訊 | 750M | 1.5B | 1150k |
audioldm2-music | 文字到音樂 | 350M | 1.1B | 665k |
audioldm2-gigaspeech | 文字到語音 | 350M | 1.1B | 10k |
audioldm2-ljspeech | 文字到語音 | 350M | 1.1B |
構建提示
- 描述性提示輸入效果最佳:使用形容詞描述聲音(例如,“高質量”或“清晰”),並使提示具有上下文特異性(例如,“森林中的溪流”而不是“溪流”)。
- 最好使用“貓”或“狗”等通用術語,而不是模型可能不熟悉的特定名稱或抽象物件。
- 使用負面提示可以顯著提高生成波形的質量,透過引導生成遠離與低質量音訊相關的術語。嘗試使用“低質量”的負面提示。
控制推理
- 預測音訊樣本的質量可以透過
num_inference_steps
引數控制;步數越高,音訊質量越高,但推理速度越慢。 - 預測音訊樣本的長度可以透過更改
audio_length_in_s
引數來控制。
評估生成的波形:
- 生成的波形質量會因隨機種子而顯著不同。嘗試使用不同的隨機種子進行生成,直到找到滿意的結果。
- 可以一次生成多個波形:將
num_waveforms_per_prompt
設定為大於1的值。系統將自動對生成的波形和提示文字進行評分,並根據相似度將音訊從最佳到最差進行排名。
以下示例演示瞭如何使用上述提示構建高質量的音樂和語音生成:示例。
AudioLDM2Pipeline
類 diffusers.AudioLDM2Pipeline
< 源 >( vae: AutoencoderKL text_encoder: ClapModel text_encoder_2: typing.Union[transformers.models.t5.modeling_t5.T5EncoderModel, transformers.models.vits.modeling_vits.VitsModel] projection_model: AudioLDM2ProjectionModel language_model: GPT2LMHeadModel tokenizer: typing.Union[transformers.models.roberta.tokenization_roberta.RobertaTokenizer, transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast] tokenizer_2: typing.Union[transformers.models.t5.tokenization_t5.T5Tokenizer, transformers.models.t5.tokenization_t5_fast.T5TokenizerFast, transformers.models.vits.tokenization_vits.VitsTokenizer] feature_extractor: ClapFeatureExtractor unet: AudioLDM2UNet2DConditionModel scheduler: KarrasDiffusionSchedulers vocoder: SpeechT5HifiGan )
引數
- vae (AutoencoderKL) — 用於將影像編碼和解碼為潛在表示的變分自編碼器(VAE)模型。
- text_encoder (ClapModel) — 第一個凍結文字編碼器。AudioLDM2 使用聯合音訊-文字嵌入模型 CLAP,特別是 laion/clap-htsat-unfused 變體。文字分支用於將文字提示編碼為提示嵌入。完整的音訊-文字模型用於透過計算相似度分數對生成的波形與文字提示進行排名。
- text_encoder_2 ([
~transformers.T5EncoderModel
,~transformers.VitsModel
]) — 第二個凍結文字編碼器。AudioLDM2 使用 T5 的編碼器,特別是 google/flan-t5-large 變體。第二個凍結文字編碼器用於 TTS。AudioLDM2 使用 Vits 的編碼器。 - projection_model (AudioLDM2ProjectionModel) — 一個訓練過的模型,用於將第一個和第二個文字編碼器模型的隱藏狀態進行線性投影,並插入學習到的 SOS 和 EOS token 嵌入。來自兩個文字編碼器的投影隱藏狀態被連線起來,作為語言模型的輸入。一個用於 Vits 隱藏狀態的學習位置嵌入。
- language_model (GPT2Model) — 一個自迴歸語言模型,用於生成一系列以兩個文字編碼器的投影輸出為條件的隱藏狀態。
- tokenizer (RobertaTokenizer) — 用於第一個凍結文字編碼器文字分詞的分詞器。
- tokenizer_2 ([
~transformers.T5Tokenizer
,~transformers.VitsTokenizer
]) — 用於第二個凍結文字編碼器文字分詞的分詞器。 - feature_extractor (ClapFeatureExtractor) — 特徵提取器,用於將生成的音訊波形預處理為對數梅爾譜圖,以便進行自動評分。
- unet (UNet2DConditionModel) — 一個
UNet2DConditionModel
模型,用於對編碼後的音訊潛在表示進行去噪。 - scheduler (SchedulerMixin) — 用於與
unet
結合對編碼後的音訊潛在表示進行去噪的排程器。可以是DDIMScheduler、LMSDiscreteScheduler或PNDMScheduler之一。 - vocoder (SpeechT5HifiGan) — 用於將梅爾譜圖潛在表示轉換為最終音訊波形的
SpeechT5HifiGan
類聲碼器。
用於使用 AudioLDM2 進行文字到音訊生成的流水線。
此模型繼承自DiffusionPipeline。請檢視超類文件,瞭解所有流水線通用的方法(下載、儲存、在特定裝置上執行等)。
__call__
< 源 >( prompt: typing.Union[str, typing.List[str]] = None transcription: typing.Union[str, typing.List[str]] = None audio_length_in_s: typing.Optional[float] = None num_inference_steps: int = 200 guidance_scale: float = 3.5 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_waveforms_per_prompt: typing.Optional[int] = 1 eta: 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 negative_prompt_embeds: typing.Optional[torch.Tensor] = None generated_prompt_embeds: typing.Optional[torch.Tensor] = None negative_generated_prompt_embeds: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.LongTensor] = None negative_attention_mask: typing.Optional[torch.LongTensor] = None max_new_tokens: typing.Optional[int] = None return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: typing.Optional[int] = 1 cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None output_type: typing.Optional[str] = 'np' ) → StableDiffusionPipelineOutput 或 tuple
引數
- prompt (
str
或List[str]
,可選) — 用於引導音訊生成的提示。如果未定義,則需要傳遞prompt_embeds
。 - transcription (
str
或List[str]
,可選) — 文字轉語音的轉錄本。 - audio_length_in_s (
int
, 可選, 預設為 10.24) — 生成音訊樣本的長度(秒)。 - num_inference_steps (
int
, 可選, 預設為 200) — 去噪步數。更多的去噪步數通常會帶來更高的音訊質量,但推理速度會更慢。 - guidance_scale (
float
, 可選, 預設為 3.5) — 較高的引導比例值會鼓勵模型生成與文字prompt
密切相關的音訊,但會犧牲較低的聲音質量。當guidance_scale > 1
時啟用引導比例。 - negative_prompt (
str
或List[str]
, 可選) — 用於引導音訊生成中不包含內容的提示。如果未定義,則需要傳遞negative_prompt_embeds
。當不使用引導(guidance_scale < 1
)時忽略。 - num_waveforms_per_prompt (
int
, 可選, 預設為 1) — 每個提示生成波形數量。如果num_waveforms_per_prompt > 1
,則在生成輸出和文字提示之間執行自動評分。此評分根據生成波形與聯合文字-音訊嵌入空間中文字輸入的餘弦相似度對生成波形進行排名。 - eta (
float
, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中將被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 一個torch.Generator
,用於生成確定性結果。 - latents (
torch.Tensor
, 可選) — 預先生成的從高斯分佈中取樣的噪聲潛在變數,用作頻譜圖生成的輸入。可用於使用不同提示調整相同生成。如果未提供,則使用提供的隨機generator
取樣生成潛在張量。 - prompt_embeds (
torch.Tensor
, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入(提示權重)。如果未提供,則從prompt
輸入引數生成文字嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預先生成的負文字嵌入。可用於輕鬆調整文字輸入(提示權重)。如果未提供,則從negative_prompt
輸入引數生成negative_prompt_embeds
。 - generated_prompt_embeds (
torch.Tensor
, 可選) — 來自 GPT2 語言模型的預生成文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,將從prompt
輸入引數生成文字嵌入。 - negative_generated_prompt_embeds (
torch.Tensor
, 可選) — 來自 GPT2 語言模型的預生成負文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,將根據negative_prompt
輸入引數計算 negative_prompt_embeds。 - attention_mask (
torch.LongTensor
, 可選) — 應用於prompt_embeds
的預計算注意力掩碼。如果未提供,注意力掩碼將從prompt
輸入引數計算。 - negative_attention_mask (
torch.LongTensor
, 可選) — 應用於negative_prompt_embeds
的預計算注意力掩碼。如果未提供,注意力掩碼將從negative_prompt
輸入引數計算。 - max_new_tokens (
int
, 可選, 預設為 None) — 使用 GPT2 語言模型生成的新標記數量。如果未提供,標記數量將從模型的配置中獲取。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元組。 - callback (
Callable
, 可選) — 在推理過程中,每callback_steps
步呼叫的函式。該函式以以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可選, 預設為 1) —callback
函式被呼叫的頻率。如果未指定,回撥將在每一步呼叫。 - cross_attention_kwargs (
dict
, 可選) — 如果指定,將作為 kwargs 字典傳遞給self.processor
中定義的AttentionProcessor
。 - output_type (
str
, 可選, 預設為"np"
) — 生成音訊的輸出格式。選擇"np"
返回 NumPynp.ndarray
或"pt"
返回 PyTorchtorch.Tensor
物件。設定為"latent"
返回潛在擴散模型 (LDM) 輸出。
返回
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
為 True
,則返回 StableDiffusionPipelineOutput,否則返回一個 tuple
,其中第一個元素是生成的音訊列表。
用於生成的管道的呼叫函式。
示例
>>> import scipy
>>> import torch
>>> from diffusers import AudioLDM2Pipeline
>>> repo_id = "cvssp/audioldm2"
>>> pipe = AudioLDM2Pipeline.from_pretrained(repo_id, torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> # define the prompts
>>> prompt = "The sound of a hammer hitting a wooden surface."
>>> negative_prompt = "Low quality."
>>> # set the seed for generator
>>> generator = torch.Generator("cuda").manual_seed(0)
>>> # run the generation
>>> audio = pipe(
... prompt,
... negative_prompt=negative_prompt,
... num_inference_steps=200,
... audio_length_in_s=10.0,
... num_waveforms_per_prompt=3,
... generator=generator,
... ).audios
>>> # save the best audio sample (index 0) as a .wav file
>>> scipy.io.wavfile.write("techno.wav", rate=16000, data=audio[0])
#Using AudioLDM2 for Text To Speech
>>> import scipy
>>> import torch
>>> from diffusers import AudioLDM2Pipeline
>>> repo_id = "anhnct/audioldm2_gigaspeech"
>>> pipe = AudioLDM2Pipeline.from_pretrained(repo_id, torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> # define the prompts
>>> prompt = "A female reporter is speaking"
>>> transcript = "wish you have a good day"
>>> # set the seed for generator
>>> generator = torch.Generator("cuda").manual_seed(0)
>>> # run the generation
>>> audio = pipe(
... prompt,
... transcription=transcript,
... num_inference_steps=200,
... audio_length_in_s=10.0,
... num_waveforms_per_prompt=2,
... generator=generator,
... max_new_tokens=512, #Must set max_new_tokens equa to 512 for TTS
... ).audios
>>> # save the best audio sample (index 0) as a .wav file
>>> scipy.io.wavfile.write("tts.wav", rate=16000, data=audio[0])
停用切片 VAE 解碼。如果之前啟用了 enable_vae_slicing
,此方法將返回一步計算解碼。
enable_model_cpu_offload
< 來源 >( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = 'cuda' )
使用 accelerate 將所有模型解除安裝到 CPU,以較低的效能影響降低記憶體使用。與 enable_sequential_cpu_offload
相比,此方法在呼叫 forward
方法時一次將一個完整模型移動到 GPU,並且模型保留在 GPU 中直到下一個模型執行。記憶體節省低於 enable_sequential_cpu_offload
,但由於 unet
的迭代執行,效能要好得多。
啟用切片 VAE 解碼。啟用此選項後,VAE 會將輸入張量分片,分步計算解碼。這有助於節省一些記憶體並允許更大的批次大小。
encode_prompt
< 來源 >( prompt device num_waveforms_per_prompt do_classifier_free_guidance transcription = None negative_prompt = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None generated_prompt_embeds: typing.Optional[torch.Tensor] = None negative_generated_prompt_embeds: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.LongTensor] = None negative_attention_mask: typing.Optional[torch.LongTensor] = None max_new_tokens: typing.Optional[int] = None ) → prompt_embeds (torch.Tensor
)
引數
- prompt (
str
或List[str]
, 可選) — 要編碼的提示 - transcription (
str
或List[str]
) — 文字到語音的轉錄 - device (
torch.device
) — torch 裝置 - num_waveforms_per_prompt (
int
) — 每個提示應生成的波形數量 - do_classifier_free_guidance (
bool
) — 是否使用分類器自由引導 - negative_prompt (
str
或List[str]
, 可選) — 不引導音訊生成的提示。如果未定義,則必須傳遞negative_prompt_embeds
。在使用非引導模式時(即guidance_scale
小於1
時)將被忽略。 - prompt_embeds (
torch.Tensor
, 可選) — 來自 Flan T5 模型的預計算文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數計算。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 來自 Flan T5 模型的預計算負文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供, negative_prompt_embeds 將從negative_prompt
輸入引數計算。 - generated_prompt_embeds (
torch.Tensor
, 可選) — 來自 GPT2 語言模型的預生成文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從prompt
輸入引數生成。 - negative_generated_prompt_embeds (
torch.Tensor
, 可選) — 來自 GPT2 語言模型的預生成負文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供, negative_prompt_embeds 將從negative_prompt
輸入引數計算。 - attention_mask (
torch.LongTensor
, 可選) — 應用於prompt_embeds
的預計算注意力掩碼。如果未提供,注意力掩碼將從prompt
輸入引數計算。 - negative_attention_mask (
torch.LongTensor
, 可選) — 應用於negative_prompt_embeds
的預計算注意力掩碼。如果未提供,注意力掩碼將從negative_prompt
輸入引數計算。 - max_new_tokens (
int
, 可選, 預設為 None) — 使用 GPT2 語言模型生成的新標記數量。
返回
prompt_embeds (torch.Tensor
)
Flan T5 模型的文字嵌入。attention_mask (torch.LongTensor
): 應用於 prompt_embeds
的注意力掩碼。generated_prompt_embeds (torch.Tensor
): 從 GPT2 語言模型生成的文字嵌入。
將提示編碼為文字編碼器隱藏狀態。
示例
>>> import scipy
>>> import torch
>>> from diffusers import AudioLDM2Pipeline
>>> repo_id = "cvssp/audioldm2"
>>> pipe = AudioLDM2Pipeline.from_pretrained(repo_id, torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> # Get text embedding vectors
>>> prompt_embeds, attention_mask, generated_prompt_embeds = pipe.encode_prompt(
... prompt="Techno music with a strong, upbeat tempo and high melodic riffs",
... device="cuda",
... do_classifier_free_guidance=True,
... )
>>> # Pass text embeddings to pipeline for text-conditional audio generation
>>> audio = pipe(
... prompt_embeds=prompt_embeds,
... attention_mask=attention_mask,
... generated_prompt_embeds=generated_prompt_embeds,
... num_inference_steps=200,
... audio_length_in_s=10.0,
... ).audios[0]
>>> # save generated audio sample
>>> scipy.io.wavfile.write("techno.wav", rate=16000, data=audio)
generate_language_model
< 來源 >( inputs_embeds: Tensor = None max_new_tokens: int = 8 **model_kwargs ) → inputs_embeds (torch.Tensor
of shape (batch_size, sequence_length, hidden_size)
)
從語言模型生成隱藏狀態序列,以嵌入輸入為條件。
AudioLDM2ProjectionModel
class diffusers.AudioLDM2ProjectionModel
< 來源 >( text_encoder_dim text_encoder_1_dim langauge_model_dim use_learned_position_embedding = None max_seq_length = None )
一個簡單的線性投影模型,用於將兩個文字嵌入對映到共享潛在空間。它還在每個文字嵌入序列的開始和結束處分別插入學習到的嵌入向量。每個附加 _1
的變數指的是與第二個文字編碼器對應的變數。否則,它來自第一個。
forward
< 來源 >( hidden_states: typing.Optional[torch.Tensor] = None hidden_states_1: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.LongTensor] = None attention_mask_1: typing.Optional[torch.LongTensor] = None )
AudioLDM2UNet2DConditionModel
class diffusers.AudioLDM2UNet2DConditionModel
< 來源 >( sample_size: typing.Optional[int] = None in_channels: int = 4 out_channels: int = 4 flip_sin_to_cos: bool = True freq_shift: int = 0 down_block_types: typing.Tuple[str] = ('CrossAttnDownBlock2D', 'CrossAttnDownBlock2D', 'CrossAttnDownBlock2D', 'DownBlock2D') mid_block_type: typing.Optional[str] = 'UNetMidBlock2DCrossAttn' up_block_types: typing.Tuple[str] = ('UpBlock2D', 'CrossAttnUpBlock2D', 'CrossAttnUpBlock2D', 'CrossAttnUpBlock2D') only_cross_attention: typing.Union[bool, typing.Tuple[bool]] = False block_out_channels: typing.Tuple[int] = (320, 640, 1280, 1280) layers_per_block: typing.Union[int, typing.Tuple[int]] = 2 downsample_padding: int = 1 mid_block_scale_factor: float = 1 act_fn: str = 'silu' norm_num_groups: typing.Optional[int] = 32 norm_eps: float = 1e-05 cross_attention_dim: typing.Union[int, typing.Tuple[int]] = 1280 transformer_layers_per_block: typing.Union[int, typing.Tuple[int]] = 1 attention_head_dim: typing.Union[int, typing.Tuple[int]] = 8 num_attention_heads: typing.Union[int, typing.Tuple[int], NoneType] = None use_linear_projection: bool = False class_embed_type: typing.Optional[str] = None num_class_embeds: typing.Optional[int] = None upcast_attention: bool = False resnet_time_scale_shift: str = 'default' time_embedding_type: str = 'positional' time_embedding_dim: typing.Optional[int] = None time_embedding_act_fn: typing.Optional[str] = None timestep_post_act: typing.Optional[str] = None time_cond_proj_dim: typing.Optional[int] = None conv_in_kernel: int = 3 conv_out_kernel: int = 3 projection_class_embeddings_input_dim: typing.Optional[int] = None class_embeddings_concat: bool = False )
引數
- sample_size (
int
或Tuple[int, int]
, 可選, 預設為None
) — 輸入/輸出樣本的高度和寬度。 - in_channels (
int
, 可選, 預設為 4) — 輸入樣本中的通道數。 - out_channels (
int
, 可選, 預設為 4) — 輸出中的通道數。 - flip_sin_to_cos (
bool
, 可選, 預設為False
) — 是否在時間嵌入中將正弦翻轉為餘弦。 - freq_shift (
int
, 可選, 預設為 0) — 應用於時間嵌入的頻率偏移。 - down_block_types (
Tuple[str]
, 可選, 預設為("CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D")
) — 要使用的下采樣塊的元組。 - mid_block_type (
str
, 可選, 預設為"UNetMidBlock2DCrossAttn"
) — UNet 中間塊的型別,對於 AudioLDM2 只能是UNetMidBlock2DCrossAttn
。 - up_block_types (
Tuple[str]
, 可選, 預設為("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
) — 要使用的上取樣塊的元組。 - only_cross_attention (
bool
或Tuple[bool]
, 可選, 預設為False
) — 是否在基本 Transformer 塊中包含自注意力,參見BasicTransformerBlock
。 - block_out_channels (
Tuple[int]
, 可選, 預設為(320, 640, 1280, 1280)
) — 每個塊的輸出通道元組。 - layers_per_block (
int
, 可選, 預設為 2) — 每個塊的層數。 - downsample_padding (
int
, 可選, 預設為 1) — 下采樣卷積使用的填充。 - mid_block_scale_factor (
float
, 可選, 預設為 1.0) — 中間塊使用的縮放因子。 - act_fn (
str
, 可選, 預設為"silu"
) — 要使用的啟用函式。 - norm_num_groups (
int
, 可選, 預設為 32) — 歸一化使用的組數。如果為None
,則在後處理中跳過歸一化和啟用層。 - norm_eps (
float
, 可選, 預設為 1e-5) — 歸一化使用的 epsilon 值。 - cross_attention_dim (
int
或Tuple[int]
, 可選, 預設為 1280) — 交叉注意力特徵的維度。 - transformer_layers_per_block (
int
或Tuple[int]
, 可選, 預設為 1) —BasicTransformerBlock
型別的 Transformer 塊的數量。僅與~models.unet_2d_blocks.CrossAttnDownBlock2D
、~models.unet_2d_blocks.CrossAttnUpBlock2D
、~models.unet_2d_blocks.UNetMidBlock2DCrossAttn
相關。 - attention_head_dim (
int
, 可選, 預設為 8) — 注意力頭的維度。 - num_attention_heads (
int
, 可選) — 注意力頭的數量。如果未定義,則預設為attention_head_dim
。 - resnet_time_scale_shift (
str
, 可選, 預設為"default"
) — ResNet 塊的時間縮放偏移配置(參見ResnetBlock2D
)。選擇default
或scale_shift
。 - class_embed_type (
str
, 可選, 預設為None
) — 要使用的類嵌入型別,最終與時間嵌入求和。選擇None
、"timestep"
、"identity"
、"projection"
或"simple_projection"
。 - num_class_embeds (
int
, 可選, 預設為None
) — 可學習嵌入矩陣的輸入維度,當class_embed_type
為None
時,該矩陣將投影到time_embed_dim
。 - time_embedding_type (
str
, 可選, 預設為positional
) — 時間步使用的位置嵌入型別。選擇positional
或fourier
。 - time_embedding_dim (
int
, 可選, 預設為None
) — 投影時間嵌入維度的可選覆蓋。 - time_embedding_act_fn (
str
, 可選, 預設為None
) — 在將時間嵌入傳遞給 UNet 的其餘部分之前,僅使用一次的可選啟用函式。選擇silu
、mish
、gelu
和swish
。 - timestep_post_act (
str
, 可選, 預設為None
) — 時間步嵌入中要使用的第二個啟用函式。選擇silu
、mish
和gelu
。 - time_cond_proj_dim (
int
, 可選, 預設為None
) — 時間步嵌入中cond_proj
層的維度。 - conv_in_kernel (
int
, 可選, 預設為3
) —conv_in
層的核大小。 - conv_out_kernel (
int
, 可選, 預設為3
) —conv_out
層的核大小。 - projection_class_embeddings_input_dim (
int
, 可選) — 當class_embed_type="projection"
時,class_labels
輸入的維度。當class_embed_type="projection"
時為必需引數。 - class_embeddings_concat (
bool
, 可選, 預設為False
) — 是否將時間嵌入與類嵌入連線起來。
一個條件 2D UNet 模型,它接受一個噪聲樣本、條件狀態和一個時間步,並返回一個樣本形狀的輸出。與普通的 UNet2DConditionModel 相比,此變體可選地在每個 Transformer 塊中包含一個額外的自注意力層,以及多個交叉注意力層。它還允許最多兩個交叉注意力嵌入,即 encoder_hidden_states
和 encoder_hidden_states_1
。
此模型繼承自 ModelMixin。有關所有模型實現的通用方法(如下載或儲存),請參閱超類文件。
forward
< 原始碼 >( sample: Tensor timestep: typing.Union[torch.Tensor, float, int] encoder_hidden_states: Tensor class_labels: typing.Optional[torch.Tensor] = None timestep_cond: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.Tensor] = None cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None encoder_attention_mask: typing.Optional[torch.Tensor] = None return_dict: bool = True encoder_hidden_states_1: typing.Optional[torch.Tensor] = None encoder_attention_mask_1: typing.Optional[torch.Tensor] = None ) → UNet2DConditionOutput 或 tuple
引數
- sample (
torch.Tensor
) — 具有以下形狀的噪聲輸入張量:(batch, channel, height, width)
。 - timestep (
torch.Tensor
或float
或int
) — 去噪輸入的時間步數。 - encoder_hidden_states (
torch.Tensor
) — 形狀為(batch, sequence_length, feature_dim)
的編碼器隱藏狀態。 - encoder_attention_mask (
torch.Tensor
) — 應用於encoder_hidden_states
的交叉注意力掩碼,形狀為(batch, sequence_length)
。如果為True
,則保留掩碼,否則(如果為False
)丟棄。掩碼將轉換為偏差,這會向對應於“丟棄”令牌的注意力分數新增大的負值。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回 UNet2DConditionOutput 而不是普通元組。 - cross_attention_kwargs (
dict
, 可選) — 一個 kwargs 字典,如果指定,將傳遞給AttnProcessor
。 - encoder_hidden_states_1 (
torch.Tensor
, 可選) — 第二組編碼器隱藏狀態,形狀為(batch, sequence_length_2, feature_dim_2)
。可用於根據不同的嵌入集對模型進行條件化,即encoder_hidden_states
。 - encoder_attention_mask_1 (
torch.Tensor
, 可選) — 應用於encoder_hidden_states_1
的交叉注意力掩碼,形狀為(batch, sequence_length_2)
。如果為True
,則保留掩碼,否則(如果為False
)丟棄。掩碼將轉換為偏差,這會向對應於“丟棄”令牌的注意力分數新增大的負值。
返回
UNet2DConditionOutput 或 tuple
如果 return_dict
為 True,則返回 UNet2DConditionOutput,否則返回一個 tuple
,其中第一個元素是樣本張量。
AudioLDM2UNet2DConditionModel 的 forward 方法。
AudioPipelineOutput
class diffusers.AudioPipelineOutput
< 原始碼 >( audios: ndarray )
音訊流水線的輸出類。