|
| 1 | +<!--Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +# Dia |
| 18 | + |
| 19 | +<div style="float: right;"> |
| 20 | + <div class="flex flex-wrap space-x-1"> |
| 21 | + <img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 22 | + <img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat"> |
| 23 | + <img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 24 | + </div> |
| 25 | +</div> |
| 26 | + |
| 27 | +## Overview |
| 28 | + |
| 29 | +Dia is an opensource text-to-speech (TTS) model (1.6B parameters) developed by [Nari Labs](https://huggingface.co/nari-labs). |
| 30 | +It can generate highly realistic dialogue from transcript including nonverbal communications such as laughter and coughing. |
| 31 | +Furthermore, emotion and tone control is also possible via audio conditioning (voice cloning). |
| 32 | + |
| 33 | +**Model Architecture:** |
| 34 | +Dia is an encoder-decoder transformer based on the original transformer architecture. However, some more modern features such as |
| 35 | +rotational positional embeddings (RoPE) are also included. For its text portion (encoder), a byte tokenizer is utilized while |
| 36 | +for the audio portion (decoder), a pretrained codec model [DAC](./dac.md) is used - DAC encodes speech into discrete codebook |
| 37 | +tokens and decodes them back into audio. |
| 38 | + |
| 39 | +## Usage Tips |
| 40 | + |
| 41 | +### Generation with Text |
| 42 | + |
| 43 | +```python |
| 44 | +from transformers import AutoProcessor, DiaForConditionalGeneration |
| 45 | + |
| 46 | +torch_device = "cuda" |
| 47 | +model_checkpoint = "buttercrab/dia-v1-1.6b" |
| 48 | + |
| 49 | +text = ["[S1] Dia is an open weights text to dialogue model."] |
| 50 | +processor = AutoProcessor.from_pretrained(model_checkpoint) |
| 51 | +inputs = processor(text=text, padding=True, return_tensors="pt").to(torch_device) |
| 52 | + |
| 53 | +model = DiaForConditionalGeneration.from_pretrained(model_checkpoint).to(torch_device) |
| 54 | +outputs = model.generate(**inputs, max_new_tokens=256) # corresponds to around ~2s |
| 55 | + |
| 56 | +# save audio to a file |
| 57 | +outputs = processor.batch_decode(outputs) |
| 58 | +processor.save_audio(outputs, "example.wav") |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +### Generation with Text and Audio (Voice Cloning) |
| 63 | + |
| 64 | +```python |
| 65 | +from datasets import load_dataset, Audio |
| 66 | +from transformers import AutoProcessor, DiaForConditionalGeneration |
| 67 | + |
| 68 | +torch_device = "cuda" |
| 69 | +model_checkpoint = "buttercrab/dia-v1-1.6b" |
| 70 | + |
| 71 | +ds = load_dataset("hf-internal-testing/dailytalk-dummy", split="train") |
| 72 | +ds = ds.cast_column("audio", Audio(sampling_rate=44100)) |
| 73 | +audio = ds[-1]["audio"]["array"] |
| 74 | +# text is a transcript of the audio + additional text you want as new audio |
| 75 | +text = ["[S1] I know. It's going to save me a lot of money, I hope. [S2] I sure hope so for you."] |
| 76 | + |
| 77 | +processor = AutoProcessor.from_pretrained(model_checkpoint) |
| 78 | +inputs = processor(text=text, audio=audio, padding=True, return_tensors="pt").to(torch_device) |
| 79 | +prompt_len = processor.get_audio_prompt_len(inputs["decoder_attention_mask"]) |
| 80 | + |
| 81 | +model = DiaForConditionalGeneration.from_pretrained(model_checkpoint).to(torch_device) |
| 82 | +outputs = model.generate(**inputs, max_new_tokens=256) # corresponds to around ~2s |
| 83 | + |
| 84 | +# retrieve actually generated audio and save to a file |
| 85 | +outputs = processor.batch_decode(outputs, audio_prompt_len=prompt_len) |
| 86 | +processor.save_audio(outputs, "example_with_audio.wav") |
| 87 | +``` |
| 88 | + |
| 89 | +### Training |
| 90 | + |
| 91 | +```python |
| 92 | +from datasets import load_dataset, Audio |
| 93 | +from transformers import AutoProcessor, DiaForConditionalGeneration |
| 94 | + |
| 95 | +torch_device = "cuda" |
| 96 | +model_checkpoint = "buttercrab/dia-v1-1.6b" |
| 97 | + |
| 98 | +ds = load_dataset("hf-internal-testing/dailytalk-dummy", split="train") |
| 99 | +ds = ds.cast_column("audio", Audio(sampling_rate=44100)) |
| 100 | +audio = ds[-1]["audio"]["array"] |
| 101 | +# text is a transcript of the audio |
| 102 | +text = ["[S1] I know. It's going to save me a lot of money, I hope."] |
| 103 | + |
| 104 | +processor = AutoProcessor.from_pretrained(model_checkpoint) |
| 105 | +inputs = processor( |
| 106 | + text=text, |
| 107 | + audio=audio, |
| 108 | + generation=False, |
| 109 | + output_labels=True, |
| 110 | + padding=True, |
| 111 | + return_tensors="pt" |
| 112 | +).to(torch_device) |
| 113 | + |
| 114 | +model = DiaForConditionalGeneration.from_pretrained(model_checkpoint).to(torch_device) |
| 115 | +out = model(**inputs) |
| 116 | +out.loss.backward() |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +This model was contributed by [Jaeyong Sung](https://huggingface.co/buttercrab), [Arthur Zucker](https://huggingface.co/ArthurZ), |
| 121 | +and [Anton Vlasjuk](https://huggingface.co/AntonV). The original code can be found [here](https://github.com/nari-labs/dia/). |
| 122 | + |
| 123 | + |
| 124 | +## DiaConfig |
| 125 | + |
| 126 | +[[autodoc]] DiaConfig |
| 127 | + |
| 128 | +## DiaDecoderConfig |
| 129 | + |
| 130 | +[[autodoc]] DiaDecoderConfig |
| 131 | + |
| 132 | +## DiaEncoderConfig |
| 133 | + |
| 134 | +[[autodoc]] DiaEncoderConfig |
| 135 | + |
| 136 | +## DiaTokenizer |
| 137 | + |
| 138 | +[[autodoc]] DiaTokenizer |
| 139 | + - __call__ |
| 140 | + |
| 141 | +## DiaFeatureExtractor |
| 142 | + |
| 143 | +[[autodoc]] DiaFeatureExtractor |
| 144 | + - __call__ |
| 145 | + |
| 146 | +## DiaProcessor |
| 147 | + |
| 148 | +[[autodoc]] DiaProcessor |
| 149 | + - __call__ |
| 150 | + - batch_decode |
| 151 | + - decode |
| 152 | + |
| 153 | +## DiaModel |
| 154 | + |
| 155 | +[[autodoc]] DiaModel |
| 156 | + - forward |
| 157 | + |
| 158 | +## DiaForConditionalGeneration |
| 159 | + |
| 160 | +[[autodoc]] DiaForConditionalGeneration |
| 161 | + - forward |
| 162 | + - generate |
0 commit comments