Diffusers 文件

Stable Audio

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

Stable Audio

Stable Audio 由 Zach Evans 等人在《Stable Audio Open》中提出。它以文字提示作為輸入,預測相應的聲音或音樂樣本。

Stable Audio Open 從文字提示生成可變長度(最長47秒)的44.1kHz立體聲音訊。它包含三個元件:一個將波形壓縮成可管理序列長度的自編碼器,一個用於文字條件的基於T5的文字嵌入,以及一個在自編碼器潛在空間中操作的基於Transformer的擴散(DiT)模型。

Stable Audio 在大約48k個音訊錄音語料庫上進行訓練,其中約47k來自 Freesound,其餘來自 Free Music Archive (FMA)。所有音訊檔案均以 CC0、CC BY 或 CC Sampling+ 許可。這些資料用於訓練自編碼器和 DiT。

該論文的摘要如下:開放式生成模型對社群至關重要,允許進行微調並作為新模型的基礎。然而,目前大多數文字到音訊模型都是私有的,藝術家和研究人員無法在其基礎上進行構建。本文介紹了使用知識共享資料訓練的新型開源文字到音訊模型的架構和訓練過程。我們的評估表明,該模型在各種指標上均與最先進的技術具有競爭力。值得注意的是,報告的 FDopenl3 結果(衡量生成的真實性)展示了其在 44.1kHz 下生成高質量立體聲音訊的潛力。

該管道由Yoach Lacombe貢獻。原始程式碼庫可在Stability-AI/stable-audio-tools找到。

提示

在構建提示時,請記住:

  • 描述性提示輸入效果最佳;使用形容詞來描述聲音(例如,“高品質”或“清晰”),並在可能的情況下使提示上下文具體化(例如,“帶有快速節拍和合成器的旋律電子樂”比“電子樂”效果更好)。
  • 使用負面提示可以顯著提高生成音訊的質量。嘗試使用“低質量、普通質量”的負面提示。

在推理過程中

  • 生成音訊樣本的質量可以透過num_inference_steps引數控制;步數越多,音訊質量越高,但推理速度越慢。
  • 可以一次性生成多個波形:將num_waveforms_per_prompt設定為大於1的值即可啟用。將自動對生成的波形和提示文字進行評分,並根據評分從優到劣對音訊進行排序。

量化

量化有助於透過以較低精度資料型別儲存模型權重來減少大型模型的記憶體需求。但是,量化對影片質量的影響可能因影片模型而異。

請參閱量化概述,瞭解有關支援的量化後端以及如何選擇支援您用例的量化後端的更多資訊。以下示例演示瞭如何載入量化後的StableAudioPipeline進行 bitsandbytes 推理。

import torch
from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig, StableAudioDiTModel, StableAudioPipeline
from diffusers.utils import export_to_video
from transformers import BitsAndBytesConfig as BitsAndBytesConfig, T5EncoderModel

quant_config = BitsAndBytesConfig(load_in_8bit=True)
text_encoder_8bit = T5EncoderModel.from_pretrained(
    "stabilityai/stable-audio-open-1.0",
    subfolder="text_encoder",
    quantization_config=quant_config,
    torch_dtype=torch.float16,
)

quant_config = DiffusersBitsAndBytesConfig(load_in_8bit=True)
transformer_8bit = StableAudioDiTModel.from_pretrained(
    "stabilityai/stable-audio-open-1.0",
    subfolder="transformer",
    quantization_config=quant_config,
    torch_dtype=torch.float16,
)

pipeline = StableAudioPipeline.from_pretrained(
    "stabilityai/stable-audio-open-1.0",
    text_encoder=text_encoder_8bit,
    transformer=transformer_8bit,
    torch_dtype=torch.float16,
    device_map="balanced",
)

prompt = "The sound of a hammer hitting a wooden surface."
negative_prompt = "Low quality."
audio = pipeline(
    prompt,
    negative_prompt=negative_prompt,
    num_inference_steps=200,
    audio_end_in_s=10.0,
    num_waveforms_per_prompt=3,
    generator=generator,
).audios

output = audio[0].T.float().cpu().numpy()
sf.write("hammer.wav", output, pipeline.vae.sampling_rate)

StableAudioPipeline

diffusers.StableAudioPipeline

< >

( vae: AutoencoderOobleck text_encoder: T5EncoderModel projection_model: StableAudioProjectionModel tokenizer: typing.Union[transformers.models.t5.tokenization_t5.T5Tokenizer, transformers.models.t5.tokenization_t5_fast.T5TokenizerFast] transformer: StableAudioDiTModel scheduler: EDMDPMSolverMultistepScheduler )

引數

  • vae (AutoencoderOobleck) — 用於將影像編碼和解碼為潛在表示的變分自編碼器(VAE)模型。
  • text_encoder (T5EncoderModel) — 凍結的文字編碼器。StableAudio 使用 T5 的編碼器,特別是 google-t5/t5-base 變體。
  • projection_model (StableAudioProjectionModel) — 一個經過訓練的模型,用於將文字編碼器模型和起始/結束秒的隱藏狀態進行線性投影。來自編碼器和條件秒的投影隱藏狀態被連線起來,作為 Transformer 模型的輸入。
  • tokenizer (T5Tokenizer) — 用於對凍結的文字編碼器文字進行標記的標記器。
  • transformer (StableAudioDiTModel) — 一個用於對編碼音訊潛在值進行去噪的 StableAudioDiTModel
  • scheduler (EDMDPMSolverMultistepScheduler) — 與 transformer 結合使用以對編碼音訊潛在值進行去噪的排程器。

用於文字到音訊生成的 StableAudio 管道。

該模型繼承自DiffusionPipeline。請檢視超類的文件,瞭解為所有管道實現的通用方法(下載、儲存、在特定裝置上執行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None audio_end_in_s: typing.Optional[float] = None audio_start_in_s: typing.Optional[float] = 0.0 num_inference_steps: int = 100 guidance_scale: float = 7.0 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 initial_audio_waveforms: typing.Optional[torch.Tensor] = None initial_audio_sampling_rate: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.LongTensor] = None negative_attention_mask: typing.Optional[torch.LongTensor] = None return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: typing.Optional[int] = 1 output_type: typing.Optional[str] = 'pt' ) StableDiffusionPipelineOutputtuple

引數

  • prompt (strList[str], 可選) — 用於指導音訊生成的提示。如果未定義,您需要傳遞 prompt_embeds
  • audio_end_in_s (float, 可選, 預設為 47.55) — 音訊結束秒數。
  • audio_start_in_s (float, 可選, 預設為 0) — 音訊開始秒數。
  • num_inference_steps (int, 可選, 預設為 100) — 去噪步數。更多的去噪步數通常會導致更高質量的音訊,但推理速度較慢。
  • guidance_scale (float, 可選, 預設為 7.0) — 較高的指導比例值鼓勵模型生成與文字 prompt 緊密相關的音訊,但音質會降低。當 guidance_scale > 1 時,啟用指導比例。
  • negative_prompt (strList[str], 可選) — 用於指導音訊生成中不包含的內容的提示。如果未定義,您需要傳遞 negative_prompt_embeds。當不使用指導時 (guidance_scale < 1),忽略此引數。
  • num_waveforms_per_prompt (int, 可選, 預設為 1) — 每個提示要生成的波形數量。
  • eta (float, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中忽略。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個 torch.Generator 用於使生成具有確定性。
  • latents (torch.Tensor, 可選) — 從高斯分佈中取樣的預生成噪聲潛在值,用作音訊生成的輸入。可用於使用不同提示調整相同的生成。如果未提供,潛在張量將透過使用提供的隨機 generator 取樣生成。
  • initial_audio_waveforms (torch.Tensor, 可選) — 可選的初始音訊波形,用作生成的初始音訊波形。必須是形狀 (batch_size, num_channels, audio_length)(batch_size, audio_length),其中 batch_size 對應於傳遞給模型的提示數量。
  • initial_audio_sampling_rate (int, 可選) — initial_audio_waveforms 的取樣率(如果提供)。必須與模型相同。
  • prompt_embeds (torch.Tensor, 可選) — 從文字編碼器模型預計算的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,文字嵌入將從 prompt 輸入引數計算。
  • negative_prompt_embeds (torch.Tensor, 可選) — 從文字編碼器模型預計算的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds 將從 negative_prompt 輸入引數計算。
  • attention_mask (torch.LongTensor, 可選) — 預計算的注意力掩碼,將應用於 prompt_embeds。如果未提供,注意力掩碼將從 prompt 輸入引數計算。
  • negative_attention_mask (torch.LongTensor, 可選) — 預計算的注意力掩碼,將應用於 negative_text_audio_duration_embeds
  • return_dict (bool, 可選, 預設為 True) — 是否返回 StableDiffusionPipelineOutput 而不是普通元組。
  • callback (Callable, 可選) — 在推理過程中每 callback_steps 步呼叫的函式。該函式將使用以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可選, 預設為 1) — 呼叫 callback 函式的頻率。如果未指定,則在每個步驟都呼叫回撥。
  • output_type (str, 可選, 預設為 "pt") — 生成音訊的輸出格式。選擇 "np" 返回 NumPy np.ndarray 物件,或選擇 "pt" 返回 PyTorch torch.Tensor 物件。設定為 "latent" 返回潛在擴散模型 (LDM) 輸出。

返回

StableDiffusionPipelineOutputtuple

如果 return_dictTrue,將返回 StableDiffusionPipelineOutput,否則將返回一個 tuple,其中第一個元素是生成的音訊列表。

用於生成的管道的呼叫函式。

示例

>>> import scipy
>>> import torch
>>> import soundfile as sf
>>> from diffusers import StableAudioPipeline

>>> repo_id = "stabilityai/stable-audio-open-1.0"
>>> pipe = StableAudioPipeline.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_end_in_s=10.0,
...     num_waveforms_per_prompt=3,
...     generator=generator,
... ).audios

>>> output = audio[0].T.float().cpu().numpy()
>>> sf.write("hammer.wav", output, pipe.vae.sampling_rate)

disable_vae_slicing

< >

( )

停用切片 VAE 解碼。如果之前啟用了 enable_vae_slicing,此方法將返回一步計算解碼。

enable_vae_slicing

< >

( )

啟用切片 VAE 解碼。啟用此選項後,VAE 會將輸入張量分片,分步計算解碼。這有助於節省一些記憶體並允許更大的批次大小。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.