```
├── .github/
├── workflows/
├── ci.yaml
├── .gitignore
├── .python-version
├── LICENSE
├── README.md
├── app.py
├── cli.py
├── dia/
├── __init__.py
├── audio.py
├── config.py
├── layers.py
├── model.py
├── state.py
├── static/
├── images/
├── banner.png
├── docker/
├── Dockerfile.cpu
├── Dockerfile.gpu
├── example/
├── simple.py
├── voice_clone.py
├── example_prompt.mp3
├── pyproject.toml
├── uv.lock
```
## /.github/workflows/ci.yaml
```yaml path="/.github/workflows/ci.yaml"
name: Continuous Integration
on:
pull_request:
branches:
- main
jobs:
lint_and_format:
runs-on: ubuntu-latest
name: Lint and Format
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
version: latest
- name: Check Lint using Ruff
run: ruff check
- name: Check Format using Ruff
run: ruff format --check --diff
```
## /.gitignore
```gitignore path="/.gitignore"
# Python-generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info
# Virtual environments
.venv
.gradio
**/*.pth
**/*.mp3
!example_prompt.mp3
**/*.txt
.ruff_cache
.ipynb_checkpoints
config.json
```
## /.python-version
```python-version path="/.python-version"
3.10
```
## /LICENSE
``` path="/LICENSE"
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2025 Nari Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
## /README.md
Dia is a 1.6B parameter text to speech model created by Nari Labs.
Dia **directly generates highly realistic dialogue from a transcript**. You can condition the output on audio, enabling emotion and tone control. The model can also produce nonverbal communications like laughter, coughing, clearing throat, etc.
To accelerate research, we are providing access to pretrained model checkpoints and inference code. The model weights are hosted on [Hugging Face](https://huggingface.co/nari-labs/Dia-1.6B). The model only supports English generation at the moment.
We also provide a [demo page](https://yummy-fir-7a4.notion.site/dia) comparing our model to [ElevenLabs Studio](https://elevenlabs.io/studio) and [Sesame CSM-1B](https://github.com/SesameAILabs/csm).
- (Update) We have a ZeroGPU Space running! Try it now [here](https://huggingface.co/spaces/nari-labs/Dia-1.6B). Thanks to the HF team for the support :)
- Join our [discord server](https://discord.gg/yBrqQ9Dd) for community support and access to new features.
- Play with a larger version of Dia: generate fun conversations, remix content, and share with friends. 🔮 Join the [waitlist](https://tally.so/r/meokbo) for early access.
## ⚡️ Quickstart
### Install via pip
```bash
# Install directly from GitHub
pip install git+https://github.com/nari-labs/dia.git
```
### Run the Gradio UI
This will open a Gradio UI that you can work on.
```bash
git clone https://github.com/nari-labs/dia.git
cd dia && uv run app.py
```
or if you do not have `uv` pre-installed:
```bash
git clone https://github.com/nari-labs/dia.git
cd dia
python -m venv .venv
source .venv/bin/activate
pip install -e .
python app.py
```
Note that the model was not fine-tuned on a specific voice. Hence, you will get different voices every time you run the model.
You can keep speaker consistency by either adding an audio prompt (a guide coming VERY soon - try it with the second example on Gradio for now), or fixing the seed.
## Features
- Generate dialogue via `[S1]` and `[S2]` tag
- Generate non-verbal like `(laughs)`, `(coughs)`, etc.
- Below verbal tags will be recognized, but might result in unexpected output.
- `(laughs), (clears throat), (sighs), (gasps), (coughs), (singing), (sings), (mumbles), (beep), (groans), (sniffs), (claps), (screams), (inhales), (exhales), (applause), (burps), (humming), (sneezes), (chuckle), (whistles)`
- Voice cloning. See [`example/voice_clone.py`](example/voice_clone.py) for more information.
- In the Hugging Face space, you can upload the audio you want to clone and place its transcript before your script. Make sure the transcript follows the required format. The model will then output only the content of your script.
## ⚙️ Usage
### As a Python Library
```python
from dia.model import Dia
model = Dia.from_pretrained("nari-labs/Dia-1.6B", compute_dtype="float16")
text = "[S1] Dia is an open weights text to dialogue model. [S2] You get full control over scripts and voices. [S1] Wow. Amazing. (laughs) [S2] Try it now on Git hub or Hugging Face."
output = model.generate(text, use_torch_compile=True, verbose=True)
model.save_audio("simple.mp3", output)
```
A pypi package and a working CLI tool will be available soon.
## 💻 Hardware and Inference Speed
Dia has been tested on only GPUs (pytorch 2.0+, CUDA 12.6). CPU support is to be added soon.
The initial run will take longer as the Descript Audio Codec also needs to be downloaded.
These are the speed we benchmarked in RTX 4090.
| precision | realtime factor w/ compile | realtime factor w/o compile | VRAM |
|:-:|:-:|:-:|:-:|
| `bfloat16` | x2.1 | x1.5 | ~10GB |
| `float16` | x2.2 | x1.3 | ~10GB |
| `float32` | x1 | x0.9 | ~13GB |
We will be adding a quantized version in the future.
If you don't have hardware available or if you want to play with bigger versions of our models, join the waitlist [here](https://tally.so/r/meokbo).
## 🪪 License
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
## ⚠️ Disclaimer
This project offers a high-fidelity speech generation model intended for research and educational use. The following uses are **strictly forbidden**:
- **Identity Misuse**: Do not produce audio resembling real individuals without permission.
- **Deceptive Content**: Do not use this model to generate misleading content (e.g. fake news)
- **Illegal or Malicious Use**: Do not use this model for activities that are illegal or intended to cause harm.
By using this model, you agree to uphold relevant legal standards and ethical responsibilities. We **are not responsible** for any misuse and firmly oppose any unethical usage of this technology.
## 🔭 TODO / Future Work
- Docker support for ARM architecture and MacOS.
- Optimize inference speed.
- Add quantization for memory efficiency.
## 🤝 Contributing
We are a tiny team of 1 full-time and 1 part-time research-engineers. We are extra-welcome to any contributions!
Join our [Discord Server](https://discord.gg/yBrqQ9Dd) for discussions.
## 🤗 Acknowledgements
- We thank the [Google TPU Research Cloud program](https://sites.research.google/trc/about/) for providing computation resources.
- Our work was heavily inspired by [SoundStorm](https://arxiv.org/abs/2305.09636), [Parakeet](https://jordandarefsky.com/blog/2024/parakeet/), and [Descript Audio Codec](https://github.com/descriptinc/descript-audio-codec).
- Hugging Face for providing the ZeroGPU Grant.
- "Nari" is a pure Korean word for lily.
- We thank Jason Y. for providing help with data filtering.
## ⭐ Star History
## /app.py
```py path="/app.py"
import argparse
import tempfile
import time
from pathlib import Path
from typing import Optional, Tuple
import gradio as gr
import numpy as np
import soundfile as sf
import torch
from dia.model import Dia
# --- Global Setup ---
parser = argparse.ArgumentParser(description="Gradio interface for Nari TTS")
parser.add_argument("--device", type=str, default=None, help="Force device (e.g., 'cuda', 'mps', 'cpu')")
parser.add_argument("--share", action="store_true", help="Enable Gradio sharing")
args = parser.parse_args()
# Determine device
if args.device:
device = torch.device(args.device)
elif torch.cuda.is_available():
device = torch.device("cuda")
# Simplified MPS check for broader compatibility
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
# Basic check is usually sufficient, detailed check can be problematic
device = torch.device("mps")
else:
device = torch.device("cpu")
print(f"Using device: {device}")
# Load Nari model and config
print("Loading Nari model...")
try:
# Use the function from inference.py
model = Dia.from_pretrained("nari-labs/Dia-1.6B", compute_dtype="float16", device=device)
except Exception as e:
print(f"Error loading Nari model: {e}")
raise
def run_inference(
text_input: str,
audio_prompt_input: Optional[Tuple[int, np.ndarray]],
max_new_tokens: int,
cfg_scale: float,
temperature: float,
top_p: float,
cfg_filter_top_k: int,
speed_factor: float,
):
"""
Runs Nari inference using the globally loaded model and provided inputs.
Uses temporary files for text and audio prompt compatibility with inference.generate.
"""
global model, device # Access global model, config, device
if not text_input or text_input.isspace():
raise gr.Error("Text input cannot be empty.")
temp_txt_file_path = None
temp_audio_prompt_path = None
output_audio = (44100, np.zeros(1, dtype=np.float32))
try:
prompt_path_for_generate = None
if audio_prompt_input is not None:
sr, audio_data = audio_prompt_input
# Check if audio_data is valid
if audio_data is None or audio_data.size == 0 or audio_data.max() == 0: # Check for silence/empty
gr.Warning("Audio prompt seems empty or silent, ignoring prompt.")
else:
# Save prompt audio to a temporary WAV file
with tempfile.NamedTemporaryFile(mode="wb", suffix=".wav", delete=False) as f_audio:
temp_audio_prompt_path = f_audio.name # Store path for cleanup
# Basic audio preprocessing for consistency
# Convert to float32 in [-1, 1] range if integer type
if np.issubdtype(audio_data.dtype, np.integer):
max_val = np.iinfo(audio_data.dtype).max
audio_data = audio_data.astype(np.float32) / max_val
elif not np.issubdtype(audio_data.dtype, np.floating):
gr.Warning(f"Unsupported audio prompt dtype {audio_data.dtype}, attempting conversion.")
# Attempt conversion, might fail for complex types
try:
audio_data = audio_data.astype(np.float32)
except Exception as conv_e:
raise gr.Error(f"Failed to convert audio prompt to float32: {conv_e}")
# Ensure mono (average channels if stereo)
if audio_data.ndim > 1:
if audio_data.shape[0] == 2: # Assume (2, N)
audio_data = np.mean(audio_data, axis=0)
elif audio_data.shape[1] == 2: # Assume (N, 2)
audio_data = np.mean(audio_data, axis=1)
else:
gr.Warning(
f"Audio prompt has unexpected shape {audio_data.shape}, taking first channel/axis."
)
audio_data = (
audio_data[0] if audio_data.shape[0] < audio_data.shape[1] else audio_data[:, 0]
)
audio_data = np.ascontiguousarray(audio_data) # Ensure contiguous after slicing/mean
# Write using soundfile
try:
sf.write(
temp_audio_prompt_path, audio_data, sr, subtype="FLOAT"
) # Explicitly use FLOAT subtype
prompt_path_for_generate = temp_audio_prompt_path
print(f"Created temporary audio prompt file: {temp_audio_prompt_path} (orig sr: {sr})")
except Exception as write_e:
print(f"Error writing temporary audio file: {write_e}")
raise gr.Error(f"Failed to save audio prompt: {write_e}")
# 3. Run Generation
start_time = time.time()
# Use torch.inference_mode() context manager for the generation call
with torch.inference_mode():
output_audio_np = model.generate(
text_input,
max_tokens=max_new_tokens,
cfg_scale=cfg_scale,
temperature=temperature,
top_p=top_p,
cfg_filter_top_k=cfg_filter_top_k, # Pass the value here
use_torch_compile=False, # Keep False for Gradio stability
audio_prompt=prompt_path_for_generate,
)
end_time = time.time()
print(f"Generation finished in {end_time - start_time:.2f} seconds.")
# 4. Convert Codes to Audio
if output_audio_np is not None:
# Get sample rate from the loaded DAC model
output_sr = 44100
# --- Slow down audio ---
original_len = len(output_audio_np)
# Ensure speed_factor is positive and not excessively small/large to avoid issues
speed_factor = max(0.1, min(speed_factor, 5.0))
target_len = int(original_len / speed_factor) # Target length based on speed_factor
if target_len != original_len and target_len > 0: # Only interpolate if length changes and is valid
x_original = np.arange(original_len)
x_resampled = np.linspace(0, original_len - 1, target_len)
resampled_audio_np = np.interp(x_resampled, x_original, output_audio_np)
output_audio = (
output_sr,
resampled_audio_np.astype(np.float32),
) # Use resampled audio
print(f"Resampled audio from {original_len} to {target_len} samples for {speed_factor:.2f}x speed.")
else:
output_audio = (
output_sr,
output_audio_np,
) # Keep original if calculation fails or no change
print(f"Skipping audio speed adjustment (factor: {speed_factor:.2f}).")
# --- End slowdown ---
print(f"Audio conversion successful. Final shape: {output_audio[1].shape}, Sample Rate: {output_sr}")
# Explicitly convert to int16 to prevent Gradio warning
if output_audio[1].dtype == np.float32 or output_audio[1].dtype == np.float64:
audio_for_gradio = np.clip(output_audio[1], -1.0, 1.0)
audio_for_gradio = (audio_for_gradio * 32767).astype(np.int16)
output_audio = (output_sr, audio_for_gradio)
print("Converted audio to int16 for Gradio output.")
else:
print("\nGeneration finished, but no valid tokens were produced.")
# Return default silence
gr.Warning("Generation produced no output.")
except Exception as e:
print(f"Error during inference: {e}")
import traceback
traceback.print_exc()
# Re-raise as Gradio error to display nicely in the UI
raise gr.Error(f"Inference failed: {e}")
finally:
# 5. Cleanup Temporary Files defensively
if temp_txt_file_path and Path(temp_txt_file_path).exists():
try:
Path(temp_txt_file_path).unlink()
print(f"Deleted temporary text file: {temp_txt_file_path}")
except OSError as e:
print(f"Warning: Error deleting temporary text file {temp_txt_file_path}: {e}")
if temp_audio_prompt_path and Path(temp_audio_prompt_path).exists():
try:
Path(temp_audio_prompt_path).unlink()
print(f"Deleted temporary audio prompt file: {temp_audio_prompt_path}")
except OSError as e:
print(f"Warning: Error deleting temporary audio prompt file {temp_audio_prompt_path}: {e}")
return output_audio
# --- Create Gradio Interface ---
css = """
#col-container {max-width: 90%; margin-left: auto; margin-right: auto;}
"""
# Attempt to load default text from example.txt
default_text = "[S1] Dia is an open weights text to dialogue model. \n[S2] You get full control over scripts and voices. \n[S1] Wow. Amazing. (laughs) \n[S2] Try it now on Git hub or Hugging Face."
example_txt_path = Path("./example.txt")
if example_txt_path.exists():
try:
default_text = example_txt_path.read_text(encoding="utf-8").strip()
if not default_text: # Handle empty example file
default_text = "Example text file was empty."
except Exception as e:
print(f"Warning: Could not read example.txt: {e}")
# Build Gradio UI
with gr.Blocks(css=css) as demo:
gr.Markdown("# Nari Text-to-Speech Synthesis")
with gr.Row(equal_height=False):
with gr.Column(scale=1):
text_input = gr.Textbox(
label="Input Text",
placeholder="Enter text here...",
value=default_text,
lines=5, # Increased lines
)
audio_prompt_input = gr.Audio(
label="Audio Prompt (Optional)",
show_label=True,
sources=["upload", "microphone"],
type="numpy",
)
with gr.Accordion("Generation Parameters", open=False):
max_new_tokens = gr.Slider(
label="Max New Tokens (Audio Length)",
minimum=860,
maximum=3072,
value=model.config.data.audio_length, # Use config default if available, else fallback
step=50,
info="Controls the maximum length of the generated audio (more tokens = longer audio).",
)
cfg_scale = gr.Slider(
label="CFG Scale (Guidance Strength)",
minimum=1.0,
maximum=5.0,
value=3.0, # Default from inference.py
step=0.1,
info="Higher values increase adherence to the text prompt.",
)
temperature = gr.Slider(
label="Temperature (Randomness)",
minimum=1.0,
maximum=1.5,
value=1.3, # Default from inference.py
step=0.05,
info="Lower values make the output more deterministic, higher values increase randomness.",
)
top_p = gr.Slider(
label="Top P (Nucleus Sampling)",
minimum=0.80,
maximum=1.0,
value=0.95, # Default from inference.py
step=0.01,
info="Filters vocabulary to the most likely tokens cumulatively reaching probability P.",
)
cfg_filter_top_k = gr.Slider(
label="CFG Filter Top K",
minimum=15,
maximum=50,
value=30,
step=1,
info="Top k filter for CFG guidance.",
)
speed_factor_slider = gr.Slider(
label="Speed Factor",
minimum=0.8,
maximum=1.0,
value=0.94,
step=0.02,
info="Adjusts the speed of the generated audio (1.0 = original speed).",
)
run_button = gr.Button("Generate Audio", variant="primary")
with gr.Column(scale=1):
audio_output = gr.Audio(
label="Generated Audio",
type="numpy",
autoplay=False,
)
# Link button click to function
run_button.click(
fn=run_inference,
inputs=[
text_input,
audio_prompt_input,
max_new_tokens,
cfg_scale,
temperature,
top_p,
cfg_filter_top_k,
speed_factor_slider,
],
outputs=[audio_output], # Add status_output here if using it
api_name="generate_audio",
)
# Add examples (ensure the prompt path is correct or remove it if example file doesn't exist)
example_prompt_path = "./example_prompt.mp3" # Adjust if needed
examples_list = [
[
"[S1] Oh fire! Oh my goodness! What's the procedure? What to we do people? The smoke could be coming through an air duct! \n[S2] Oh my god! Okay.. it's happening. Everybody stay calm! \n[S1] What's the procedure... \n[S2] Everybody stay fucking calm!!!... Everybody fucking calm down!!!!! \n[S1] No! No! If you touch the handle, if its hot there might be a fire down the hallway! ",
None,
3072,
3.0,
1.3,
0.95,
35,
0.94,
],
[
"[S1] Open weights text to dialogue model. \n[S2] You get full control over scripts and voices. \n[S1] I'm biased, but I think we clearly won. \n[S2] Hard to disagree. (laughs) \n[S1] Thanks for listening to this demo. \n[S2] Try it now on Git hub and Hugging Face. \n[S1] If you liked our model, please give us a star and share to your friends. \n[S2] This was Nari Labs.",
example_prompt_path if Path(example_prompt_path).exists() else None,
3072,
3.0,
1.3,
0.95,
35,
0.94,
],
]
if examples_list:
gr.Examples(
examples=examples_list,
inputs=[
text_input,
audio_prompt_input,
max_new_tokens,
cfg_scale,
temperature,
top_p,
cfg_filter_top_k,
speed_factor_slider,
],
outputs=[audio_output],
fn=run_inference,
cache_examples=False,
label="Examples (Click to Run)",
)
else:
gr.Markdown("_(No examples configured or example prompt file missing)_")
# --- Launch the App ---
if __name__ == "__main__":
print("Launching Gradio interface...")
# set `GRADIO_SERVER_NAME`, `GRADIO_SERVER_PORT` env vars to override default values
# use `GRADIO_SERVER_NAME=0.0.0.0` for Docker
demo.launch(share=args.share)
```
## /cli.py
```py path="/cli.py"
import argparse
import os
import random
import numpy as np
import soundfile as sf
import torch
from dia.model import Dia
def set_seed(seed: int):
"""Sets the random seed for reproducibility."""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# Ensure deterministic behavior for cuDNN (if used)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def main():
parser = argparse.ArgumentParser(description="Generate audio using the Dia model.")
parser.add_argument("text", type=str, help="Input text for speech generation.")
parser.add_argument(
"--output", type=str, required=True, help="Path to save the generated audio file (e.g., output.wav)."
)
parser.add_argument(
"--repo-id",
type=str,
default="nari-labs/Dia-1.6B",
help="Hugging Face repository ID (e.g., nari-labs/Dia-1.6B).",
)
parser.add_argument(
"--local-paths", action="store_true", help="Load model from local config and checkpoint files."
)
parser.add_argument(
"--config", type=str, help="Path to local config.json file (required if --local-paths is set)."
)
parser.add_argument(
"--checkpoint", type=str, help="Path to local model checkpoint .pth file (required if --local-paths is set)."
)
parser.add_argument(
"--audio-prompt", type=str, default=None, help="Path to an optional audio prompt WAV file for voice cloning."
)
gen_group = parser.add_argument_group("Generation Parameters")
gen_group.add_argument(
"--max-tokens",
type=int,
default=None,
help="Maximum number of audio tokens to generate (defaults to config value).",
)
gen_group.add_argument(
"--cfg-scale", type=float, default=3.0, help="Classifier-Free Guidance scale (default: 3.0)."
)
gen_group.add_argument(
"--temperature", type=float, default=1.3, help="Sampling temperature (higher is more random, default: 0.7)."
)
gen_group.add_argument("--top-p", type=float, default=0.95, help="Nucleus sampling probability (default: 0.95).")
infra_group = parser.add_argument_group("Infrastructure")
infra_group.add_argument("--seed", type=int, default=None, help="Random seed for reproducibility.")
infra_group.add_argument(
"--device",
type=str,
default="cuda" if torch.cuda.is_available() else "cpu",
help="Device to run inference on (e.g., 'cuda', 'cpu', default: auto).",
)
args = parser.parse_args()
# Validation for local paths
if args.local_paths:
if not args.config:
parser.error("--config is required when --local-paths is set.")
if not args.checkpoint:
parser.error("--checkpoint is required when --local-paths is set.")
if not os.path.exists(args.config):
parser.error(f"Config file not found: {args.config}")
if not os.path.exists(args.checkpoint):
parser.error(f"Checkpoint file not found: {args.checkpoint}")
# Set seed if provided
if args.seed is not None:
set_seed(args.seed)
print(f"Using random seed: {args.seed}")
# Determine device
device = torch.device(args.device)
print(f"Using device: {device}")
# Load model
print("Loading model...")
if args.local_paths:
print(f"Loading from local paths: config='{args.config}', checkpoint='{args.checkpoint}'")
try:
model = Dia.from_local(args.config, args.checkpoint, device=device)
except Exception as e:
print(f"Error loading local model: {e}")
exit(1)
else:
print(f"Loading from Hugging Face Hub: repo_id='{args.repo_id}'")
try:
model = Dia.from_pretrained(args.repo_id, device=device)
except Exception as e:
print(f"Error loading model from Hub: {e}")
exit(1)
print("Model loaded.")
# Generate audio
print("Generating audio...")
try:
sample_rate = 44100 # Default assumption
output_audio = model.generate(
text=args.text,
audio_prompt=args.audio_prompt,
max_tokens=args.max_tokens,
cfg_scale=args.cfg_scale,
temperature=args.temperature,
top_p=args.top_p,
)
print("Audio generation complete.")
print(f"Saving audio to {args.output}...")
os.makedirs(os.path.dirname(args.output) or ".", exist_ok=True)
sf.write(args.output, output_audio, sample_rate)
print(f"Audio successfully saved to {args.output}")
except Exception as e:
print(f"Error during audio generation or saving: {e}")
exit(1)
if __name__ == "__main__":
main()
```
## /dia/__init__.py
```py path="/dia/__init__.py"
from .model import Dia
__all__ = [
"Dia",
]
```
## /dia/audio.py
```py path="/dia/audio.py"
import typing as tp
import torch
def build_delay_indices(B: int, T: int, C: int, delay_pattern: tp.List[int]) -> tp.Tuple[torch.Tensor, torch.Tensor]:
"""
Precompute (t_idx_BxTxC, indices_BTCx3) so that out[t, c] = in[t - delay[c], c].
Negative t_idx => BOS; t_idx >= T => PAD.
"""
delay_arr = torch.tensor(delay_pattern, dtype=torch.int32)
t_idx_BxT = torch.broadcast_to(
torch.arange(T, dtype=torch.int32)[None, :],
[B, T],
)
t_idx_BxTx1 = t_idx_BxT[..., None]
t_idx_BxTxC = t_idx_BxTx1 - delay_arr.view(1, 1, C)
b_idx_BxTxC = torch.broadcast_to(
torch.arange(B, dtype=torch.int32).view(B, 1, 1),
[B, T, C],
)
c_idx_BxTxC = torch.broadcast_to(
torch.arange(C, dtype=torch.int32).view(1, 1, C),
[B, T, C],
)
# We must clamp time indices to [0..T-1] so gather_nd equivalent won't fail
t_clamped_BxTxC = torch.clamp(t_idx_BxTxC, 0, T - 1)
indices_BTCx3 = torch.stack(
[
b_idx_BxTxC.reshape(-1),
t_clamped_BxTxC.reshape(-1),
c_idx_BxTxC.reshape(-1),
],
dim=1,
).long() # Ensure indices are long type for indexing
return t_idx_BxTxC, indices_BTCx3
def apply_audio_delay(
audio_BxTxC: torch.Tensor,
pad_value: int,
bos_value: int,
precomp: tp.Tuple[torch.Tensor, torch.Tensor],
) -> torch.Tensor:
"""
Applies the delay pattern to batched audio tokens using precomputed indices,
inserting BOS where t_idx < 0 and PAD where t_idx >= T.
Args:
audio_BxTxC: [B, T, C] int16 audio tokens (or int32/float)
pad_value: the padding token
bos_value: the BOS token
precomp: (t_idx_BxTxC, indices_BTCx3) from build_delay_indices
Returns:
result_BxTxC: [B, T, C] delayed audio tokens
"""
device = audio_BxTxC.device # Get device from input tensor
t_idx_BxTxC, indices_BTCx3 = precomp
t_idx_BxTxC = t_idx_BxTxC.to(device) # Move precomputed indices to device
indices_BTCx3 = indices_BTCx3.to(device)
# Equivalent of tf.gather_nd using advanced indexing
# Ensure indices are long type if not already (build_delay_indices should handle this)
gathered_flat = audio_BxTxC[indices_BTCx3[:, 0], indices_BTCx3[:, 1], indices_BTCx3[:, 2]]
gathered_BxTxC = gathered_flat.view(audio_BxTxC.shape)
# Create masks on the correct device
mask_bos = t_idx_BxTxC < 0 # => place bos_value
mask_pad = t_idx_BxTxC >= audio_BxTxC.shape[1] # => place pad_value
# Create scalar tensors on the correct device
bos_tensor = torch.tensor(bos_value, dtype=audio_BxTxC.dtype, device=device)
pad_tensor = torch.tensor(pad_value, dtype=audio_BxTxC.dtype, device=device)
# If mask_bos, BOS; else if mask_pad, PAD; else original gather
# All tensors should now be on the same device
result_BxTxC = torch.where(mask_bos, bos_tensor, torch.where(mask_pad, pad_tensor, gathered_BxTxC))
return result_BxTxC
def build_revert_indices(B: int, T: int, C: int, delay_pattern: tp.List[int]) -> tp.Tuple[torch.Tensor, torch.Tensor]:
"""
Precompute indices for the revert operation using PyTorch.
Returns:
A tuple (t_idx_BxTxC, indices_BTCx3) where:
- t_idx_BxTxC is a tensor of shape [B, T, C] computed as time indices plus the delay.
- indices_BTCx3 is a tensor of shape [B*T*C, 3] used for gathering, computed from:
batch indices, clamped time indices, and channel indices.
"""
# Use default device unless specified otherwise; assumes inputs might define device later
device = None # Or determine dynamically if needed, e.g., from a model parameter
delay_arr = torch.tensor(delay_pattern, dtype=torch.int32, device=device)
t_idx_BT1 = torch.broadcast_to(torch.arange(T, device=device).unsqueeze(0), [B, T])
t_idx_BT1 = t_idx_BT1.unsqueeze(-1)
t_idx_BxTxC = torch.minimum(
t_idx_BT1 + delay_arr.view(1, 1, C),
torch.tensor(T - 1, device=device),
)
b_idx_BxTxC = torch.broadcast_to(torch.arange(B, device=device).view(B, 1, 1), [B, T, C])
c_idx_BxTxC = torch.broadcast_to(torch.arange(C, device=device).view(1, 1, C), [B, T, C])
indices_BTCx3 = torch.stack(
[
b_idx_BxTxC.reshape(-1),
t_idx_BxTxC.reshape(-1),
c_idx_BxTxC.reshape(-1),
],
axis=1,
).long() # Ensure indices are long type
return t_idx_BxTxC, indices_BTCx3
def revert_audio_delay(
audio_BxTxC: torch.Tensor,
pad_value: int,
precomp: tp.Tuple[torch.Tensor, torch.Tensor],
T: int,
) -> torch.Tensor:
"""
Reverts a delay pattern from batched audio tokens using precomputed indices (PyTorch version).
Args:
audio_BxTxC: Input delayed audio tensor
pad_value: Padding value for out-of-bounds indices
precomp: Precomputed revert indices tuple containing:
- t_idx_BxTxC: Time offset indices tensor
- indices_BTCx3: Gather indices tensor for original audio
T: Original sequence length before padding
Returns:
Reverted audio tensor with same shape as input
"""
t_idx_BxTxC, indices_BTCx3 = precomp
device = audio_BxTxC.device # Get device from input tensor
# Move precomputed indices to the same device as audio_BxTxC if they aren't already
t_idx_BxTxC = t_idx_BxTxC.to(device)
indices_BTCx3 = indices_BTCx3.to(device)
# Using PyTorch advanced indexing (equivalent to tf.gather_nd or np equivalent)
gathered_flat = audio_BxTxC[indices_BTCx3[:, 0], indices_BTCx3[:, 1], indices_BTCx3[:, 2]]
gathered_BxTxC = gathered_flat.view(audio_BxTxC.size()) # Use .size() for robust reshaping
# Create pad_tensor on the correct device
pad_tensor = torch.tensor(pad_value, dtype=audio_BxTxC.dtype, device=device)
# Create T tensor on the correct device for comparison
T_tensor = torch.tensor(T, device=device)
result_BxTxC = torch.where(t_idx_BxTxC >= T_tensor, pad_tensor, gathered_BxTxC) # Changed np.where to torch.where
return result_BxTxC
@torch.no_grad()
@torch.inference_mode()
def decode(
model,
audio_codes,
):
"""
Decodes the given frames into an output audio waveform
"""
if len(audio_codes) != 1:
raise ValueError(f"Expected one frame, got {len(audio_codes)}")
try:
audio_values = model.quantizer.from_codes(audio_codes)
audio_values = model.decode(audio_values[0])
return audio_values
except Exception as e:
print(f"Error in decode method: {str(e)}")
raise
```
## /dia/config.py
```py path="/dia/config.py"
"""Configuration management module for the Dia model.
This module provides comprehensive configuration management for the Dia model,
utilizing Pydantic for validation. It defines configurations for data processing,
model architecture (encoder and decoder), and training settings.
Key components:
- DataConfig: Parameters for data loading and preprocessing.
- EncoderConfig: Architecture details for the encoder module.
- DecoderConfig: Architecture details for the decoder module.
- ModelConfig: Combined model architecture settings.
- TrainingConfig: Training hyperparameters and settings.
- DiaConfig: Master configuration combining all components.
"""
import os
from typing import Annotated
from pydantic import BaseModel, BeforeValidator, Field
class DataConfig(BaseModel, frozen=True):
"""Configuration for data loading and preprocessing.
Attributes:
text_length: Maximum length of text sequences (must be multiple of 128).
audio_length: Maximum length of audio sequences (must be multiple of 128).
channels: Number of audio channels.
text_pad_value: Value used for padding text sequences.
audio_eos_value: Value representing the end of audio sequences.
audio_bos_value: Value representing the beginning of audio sequences.
audio_pad_value: Value used for padding audio sequences.
delay_pattern: List of delay values for each audio channel.
"""
text_length: Annotated[int, BeforeValidator(lambda x: (x + 127) // 128 * 128)] = Field(gt=0, multiple_of=128)
audio_length: Annotated[int, BeforeValidator(lambda x: (x + 127) // 128 * 128)] = Field(gt=0, multiple_of=128)
channels: int = Field(default=9, gt=0, multiple_of=1)
text_pad_value: int = Field(default=0)
audio_eos_value: int = Field(default=1024)
audio_pad_value: int = Field(default=1025)
audio_bos_value: int = Field(default=1026)
delay_pattern: list[Annotated[int, Field(ge=0)]] = Field(default_factory=lambda: [0, 8, 9, 10, 11, 12, 13, 14, 15])
def __hash__(self) -> int:
"""Generate a hash based on all fields of the config."""
return hash(
(
self.text_length,
self.audio_length,
self.channels,
self.text_pad_value,
self.audio_pad_value,
self.audio_bos_value,
self.audio_eos_value,
tuple(self.delay_pattern),
)
)
class EncoderConfig(BaseModel, frozen=True):
"""Configuration for the encoder component of the Dia model.
Attributes:
n_layer: Number of transformer layers.
n_embd: Embedding dimension.
n_hidden: Hidden dimension size in the MLP layers.
n_head: Number of attention heads.
head_dim: Dimension per attention head.
"""
n_layer: int = Field(gt=0)
n_embd: int = Field(gt=0)
n_hidden: int = Field(gt=0)
n_head: int = Field(gt=0)
head_dim: int = Field(gt=0)
class DecoderConfig(BaseModel, frozen=True):
"""Configuration for the decoder component of the Dia model.
Attributes:
n_layer: Number of transformer layers.
n_embd: Embedding dimension.
n_hidden: Hidden dimension size in the MLP layers.
gqa_query_heads: Number of query heads for grouped-query self-attention.
kv_heads: Number of key/value heads for grouped-query self-attention.
gqa_head_dim: Dimension per query head for grouped-query self-attention.
cross_query_heads: Number of query heads for cross-attention.
cross_head_dim: Dimension per cross-attention head.
"""
n_layer: int = Field(gt=0)
n_embd: int = Field(gt=0)
n_hidden: int = Field(gt=0)
gqa_query_heads: int = Field(gt=0)
kv_heads: int = Field(gt=0)
gqa_head_dim: int = Field(gt=0)
cross_query_heads: int = Field(gt=0)
cross_head_dim: int = Field(gt=0)
class ModelConfig(BaseModel, frozen=True):
"""Main configuration container for the Dia model architecture.
Attributes:
encoder: Configuration for the encoder component.
decoder: Configuration for the decoder component.
src_vocab_size: Size of the source (text) vocabulary.
tgt_vocab_size: Size of the target (audio code) vocabulary.
dropout: Dropout probability applied within the model.
normalization_layer_epsilon: Epsilon value for normalization layers (e.g., LayerNorm).
weight_dtype: Data type for model weights (e.g., "float32", "bfloat16").
rope_min_timescale: Minimum timescale for Rotary Positional Embeddings (RoPE).
rope_max_timescale: Maximum timescale for Rotary Positional Embeddings (RoPE).
"""
encoder: EncoderConfig
decoder: DecoderConfig
src_vocab_size: int = Field(default=128, gt=0)
tgt_vocab_size: int = Field(default=1028, gt=0)
dropout: float = Field(default=0.0, ge=0.0, lt=1.0)
normalization_layer_epsilon: float = Field(default=1.0e-5, ge=0.0)
weight_dtype: str = Field(default="float32", description="Weight precision")
rope_min_timescale: int = Field(default=1, description="Timescale For global Attention")
rope_max_timescale: int = Field(default=10_000, description="Timescale For global Attention")
class TrainingConfig(BaseModel, frozen=True):
pass
class DiaConfig(BaseModel, frozen=True):
"""Master configuration for the Dia model.
Combines all sub-configurations into a single validated object.
Attributes:
version: Configuration version string.
model: Model architecture configuration.
training: Training process configuration (precision settings).
data: Data loading and processing configuration.
"""
version: str = Field(default="1.0")
model: ModelConfig
# TODO: remove training. this is just for backward compatibility
training: TrainingConfig | None = Field(default=None)
data: DataConfig
def save(self, path: str) -> None:
"""Save the current configuration instance to a JSON file.
Ensures the parent directory exists and the file has a .json extension.
Args:
path: The target file path to save the configuration.
Raises:
ValueError: If the path is not a file with a .json extension.
"""
os.makedirs(os.path.dirname(path), exist_ok=True)
config_json = self.model_dump_json(indent=2)
with open(path, "w") as f:
f.write(config_json)
@classmethod
def load(cls, path: str) -> "DiaConfig | None":
"""Load and validate a Dia configuration from a JSON file.
Args:
path: The path to the configuration file.
Returns:
A validated DiaConfig instance if the file exists and is valid,
otherwise None if the file is not found.
Raises:
ValueError: If the path does not point to an existing .json file.
pydantic.ValidationError: If the JSON content fails validation against the DiaConfig schema.
"""
try:
with open(path, "r") as f:
content = f.read()
return cls.model_validate_json(content)
except FileNotFoundError:
return None
```
## /dia/layers.py
```py path="/dia/layers.py"
import torch
import torch.nn as nn
import torch.nn.functional as F
from huggingface_hub import PyTorchModelHubMixin
from torch import Tensor
from torch.nn import RMSNorm
from .config import DiaConfig
from .state import DecoderInferenceState, EncoderInferenceState, KVCache
def _normalize_axes(axes: tuple[int, ...], ndim: int) -> tuple[int, ...]:
return tuple(ax if ax >= 0 else ndim + ax for ax in axes)
class DenseGeneral(nn.Module):
"""
PyTorch equivalent of flax.linen.DenseGeneral with shapes defined at init.
Stores weights (`kernel`) in the same layout as Jax and uses torch.tensordot
for the generalized matrix multiplication. Weight/bias shapes are calculated
and parameters created during initialization based on config.
`load_weights` validates shapes and copies data.
Attributes:
axis (Tuple[int, ...]): Input axis or axes to contract.
in_shapes (Tuple[int, ...]): Sizes of the input dimensions specified by `axis`.
out_features (Tuple[int, ...]): Shape of the output features (non-contracted dims).
use_bias (bool): Whether to add a bias term.
weight (nn.Parameter): The kernel parameter.
bias (Optional[nn.Parameter]): The bias parameter (if use_bias=True).
"""
def __init__(
self,
in_shapes: tuple[int, ...],
out_features: tuple[int, ...],
axis: tuple[int, ...] = (-1,),
weight_dtype: torch.dtype | None = None,
device: torch.device | None = None,
):
super().__init__()
self.in_shapes = in_shapes
self.out_features = out_features
self.axis = axis
self.kernel_shape = self.in_shapes + self.out_features
factory_kwargs = {"device": device, "dtype": weight_dtype}
self.weight = nn.Parameter(torch.empty(self.kernel_shape, **factory_kwargs))
def forward(self, inputs: Tensor) -> Tensor:
norm_axis = _normalize_axes(self.axis, inputs.ndim)
kernel_contract_axes = tuple(range(len(norm_axis)))
output = torch.tensordot(
inputs.to(self.weight.dtype),
self.weight,
dims=(norm_axis, kernel_contract_axes),
).to(inputs.dtype)
return output
class MlpBlock(nn.Module):
"""MLP block using DenseGeneral."""
def __init__(self, embed_dim: int, intermediate_dim: int, compute_dtype: torch.dtype):
super().__init__()
self.dtype = compute_dtype
self.wi_fused = DenseGeneral(
in_shapes=(embed_dim,),
out_features=(2, intermediate_dim),
axis=(-1,),
weight_dtype=compute_dtype,
)
self.wo = DenseGeneral(
in_shapes=(intermediate_dim,),
out_features=(embed_dim,),
axis=(-1,),
weight_dtype=compute_dtype,
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass."""
fused_x = self.wi_fused(x)
gate = fused_x[..., 0, :]
up = fused_x[..., 1, :]
hidden = torch.mul(F.silu(gate), up).to(self.dtype)
output = self.wo(hidden)
return output
class RotaryEmbedding(nn.Module):
"""Rotary Position Embedding (RoPE) implementation in PyTorch."""
def __init__(
self,
embedding_dims: int,
min_timescale: int = 1,
max_timescale: int = 10000,
dtype: torch.dtype = torch.float32,
):
super().__init__()
if embedding_dims % 2 != 0:
raise ValueError("Embedding dim must be even for RoPE.")
self.embedding_dims = embedding_dims
self.min_timescale = min_timescale
self.max_timescale = max_timescale
self.compute_dtype = dtype
half_embedding_dim = embedding_dims // 2
fraction = (2.0 * torch.arange(0, half_embedding_dim)) / embedding_dims
timescale = (self.min_timescale * (self.max_timescale / self.min_timescale) ** fraction).to(torch.float32)
self.register_buffer("timescale", timescale, persistent=False)
def forward(self, inputs: torch.Tensor, position: torch.Tensor):
"""Applies RoPE."""
position = position.unsqueeze(-1).unsqueeze(-1)
sinusoid_inp = position / self.timescale
sin = torch.sin(sinusoid_inp)
cos = torch.cos(sinusoid_inp)
first_half, second_half = torch.chunk(inputs.to(torch.float32), 2, dim=-1)
first_part = first_half * cos - second_half * sin
second_part = second_half * cos + first_half * sin
return torch.cat((first_part.to(self.compute_dtype), second_part.to(self.compute_dtype)), dim=-1)
class Attention(nn.Module):
"""Attention using DenseGeneral."""
def __init__(
self,
config: DiaConfig,
q_embed_dim: int,
kv_embed_dim: int,
num_query_heads: int,
num_kv_heads: int,
head_dim: int,
compute_dtype: torch.dtype,
is_cross_attn: bool = False,
out_embed_dim: int | None = None,
):
super().__init__()
self.num_query_heads = num_query_heads
self.num_kv_heads = num_kv_heads
self.head_dim = head_dim
self.is_cross_attn = is_cross_attn
self.output_dim = out_embed_dim if out_embed_dim is not None else q_embed_dim
self.projected_query_dim = num_query_heads * head_dim
if num_query_heads % num_kv_heads != 0:
raise ValueError(f"num_query_heads ({num_query_heads}) must be divisible by num_kv_heads ({num_kv_heads})")
self.num_gqa_groups = num_query_heads // num_kv_heads
# --- Projection Layers using DenseGeneral ---
self.q_proj = DenseGeneral(
in_shapes=(q_embed_dim,),
out_features=(num_query_heads, head_dim),
axis=(-1,),
weight_dtype=compute_dtype,
)
self.k_proj = DenseGeneral(
in_shapes=(kv_embed_dim,),
out_features=(num_kv_heads, head_dim),
axis=(-1,),
weight_dtype=compute_dtype,
)
self.v_proj = DenseGeneral(
in_shapes=(kv_embed_dim,),
out_features=(num_kv_heads, head_dim),
axis=(-1,),
weight_dtype=compute_dtype,
)
self.o_proj = DenseGeneral(
in_shapes=(num_query_heads, head_dim),
out_features=(self.output_dim,),
axis=(-2, -1),
weight_dtype=compute_dtype,
)
# --- Rotary Embedding ---
self.rotary_emb = RotaryEmbedding(
embedding_dims=self.head_dim,
min_timescale=config.model.rope_min_timescale,
max_timescale=config.model.rope_max_timescale,
dtype=compute_dtype,
)
def forward(
self,
Xq: torch.Tensor, # (B, T, D) T = 1 in AR generation
Xkv: torch.Tensor, # (B, S, E) S = 1 in AR generation
q_positions: torch.Tensor, # (B, T)
kv_positions: torch.Tensor | None = None, # (B, S)
attn_mask: torch.Tensor | None = None, # None in Decoder Self Attention, Valid mask in Others
cache: KVCache | None = None, # None in Encoder, KVCache in Decoder
prefill: bool = False,
is_causal: bool = False,
) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor] | None]:
"""
Performs attention calculation with optional KV caching.
Args:
Xq: Query tensor (B, T, D). T=1 during single-step decoding.
Xkv: Key/Value source tensor (B, S, E). S=1 during single-step decoding for self-attn.
q_positions: Positions for queries (B, T).
kv_positions: Positions for keys/values (B, S). If None, uses q_positions.
attn_mask: Attention mask.
cache: KVCache.
prefill: If True, use prefill mode.
Returns:
A tuple containing:
- output: The attention output tensor (B, T, output_dim).
- present_kv: The K/V state to be cached for the next step ((B, N, S_new, H), (B, N, S_new, H)). For self-attn, S_new = S_past + S. For cross-attn, S_new = S_kv.
"""
if kv_positions is None:
kv_positions = q_positions
original_dtype = Xq.dtype
Xq_BxTxNxH = self.q_proj(Xq)
Xq_BxTxNxH = self.rotary_emb(Xq_BxTxNxH, position=q_positions)
Xq_BxNxTxH = Xq_BxTxNxH.transpose(1, 2)
attn_k: torch.Tensor | None = None
attn_v: torch.Tensor | None = None
if self.is_cross_attn:
attn_k, attn_v = cache.k, cache.v
else:
Xk_BxSxKxH = self.k_proj(Xkv) # (B, S, K, H)
Xv_BxSxKxH = self.v_proj(Xkv) # (B, S, K, H)
Xk_BxSxKxH = self.rotary_emb(Xk_BxSxKxH, position=kv_positions) # (B, S, K, H)
Xk_BxKxSxH = Xk_BxSxKxH.transpose(1, 2) # (B, K, S, H)
Xv_BxKxSxH = Xv_BxSxKxH.transpose(1, 2) # (B, K, S, H)
if cache is None:
attn_k = Xk_BxKxSxH
attn_v = Xv_BxKxSxH
else:
if prefill:
attn_k, attn_v = Xk_BxKxSxH, Xv_BxKxSxH
cache.prefill(attn_k, attn_v)
else:
attn_k, attn_v = cache.update(Xk_BxKxSxH, Xv_BxKxSxH)
attn_output = F.scaled_dot_product_attention(
Xq_BxNxTxH,
attn_k,
attn_v,
attn_mask=attn_mask,
scale=1.0,
enable_gqa=self.num_gqa_groups > 1,
is_causal=is_causal,
)
attn_output = attn_output.transpose(1, 2).contiguous() # (B, T, N, H)
output = self.o_proj(attn_output)
return output.to(original_dtype)
class EncoderLayer(nn.Module):
"""Transformer Encoder Layer using DenseGeneral."""
def __init__(self, config: DiaConfig, compute_dtype: torch.dtype):
super().__init__()
self.config = config
model_config = config.model
enc_config = config.model.encoder
embed_dim = enc_config.n_embd
self.compute_dtype = compute_dtype
self.pre_sa_norm = RMSNorm(
embed_dim,
eps=model_config.normalization_layer_epsilon,
dtype=torch.float32,
)
self.self_attention = Attention(
config,
q_embed_dim=embed_dim,
kv_embed_dim=embed_dim,
num_query_heads=enc_config.n_head,
num_kv_heads=enc_config.n_head,
head_dim=enc_config.head_dim,
compute_dtype=compute_dtype,
is_cross_attn=False,
out_embed_dim=embed_dim,
)
self.post_sa_norm = RMSNorm(
embed_dim,
eps=model_config.normalization_layer_epsilon,
dtype=torch.float32,
)
self.mlp = MlpBlock(embed_dim=embed_dim, intermediate_dim=enc_config.n_hidden, compute_dtype=compute_dtype)
def forward(
self,
x: torch.Tensor,
state: EncoderInferenceState,
) -> torch.Tensor:
residual = x
x_norm = self.pre_sa_norm(x).to(self.compute_dtype)
sa_out = self.self_attention(
Xq=x_norm,
Xkv=x_norm,
q_positions=state.positions,
kv_positions=state.positions,
attn_mask=state.attn_mask,
)
x = residual + sa_out
residual = x
x_norm = self.post_sa_norm(x).to(self.compute_dtype)
mlp_out = self.mlp(x_norm)
x = residual + mlp_out
return x
class Encoder(nn.Module):
"""Transformer Encoder Stack using DenseGeneral."""
def __init__(self, config: DiaConfig, compute_dtype: torch.dtype):
super().__init__()
self.config = config
model_config = config.model
enc_config = config.model.encoder
self.compute_dtype = compute_dtype
self.embedding = nn.Embedding(
model_config.src_vocab_size,
enc_config.n_embd,
dtype=compute_dtype,
)
self.layers = nn.ModuleList([EncoderLayer(config, compute_dtype) for _ in range(enc_config.n_layer)])
self.norm = RMSNorm(
enc_config.n_embd,
eps=model_config.normalization_layer_epsilon,
dtype=torch.float32,
)
def forward(
self,
x_ids: torch.Tensor,
state: EncoderInferenceState,
) -> torch.Tensor:
x = self.embedding(x_ids)
for layer in self.layers:
x = layer(x, state)
x = self.norm(x).to(self.compute_dtype)
return x
class DecoderLayer(nn.Module):
"""Transformer Decoder Layer using DenseGeneral."""
def __init__(self, config: DiaConfig, compute_dtype: torch.dtype):
super().__init__()
self.config = config
model_config = config.model
dec_config = config.model.decoder
enc_config = config.model.encoder
dec_embed_dim = dec_config.n_embd
enc_embed_dim = enc_config.n_embd
self.compute_dtype = compute_dtype
# Norms
self.pre_sa_norm = RMSNorm(
dec_embed_dim,
eps=model_config.normalization_layer_epsilon,
dtype=torch.float32,
)
self.pre_ca_norm = RMSNorm(
dec_embed_dim,
eps=model_config.normalization_layer_epsilon,
dtype=torch.float32,
)
self.pre_mlp_norm = RMSNorm(
dec_embed_dim,
eps=model_config.normalization_layer_epsilon,
dtype=torch.float32,
)
# Self-Attention (GQA) with Causal Masking
self.self_attention = Attention(
config,
q_embed_dim=dec_embed_dim,
kv_embed_dim=dec_embed_dim,
num_query_heads=dec_config.gqa_query_heads,
num_kv_heads=dec_config.kv_heads,
head_dim=dec_config.gqa_head_dim,
compute_dtype=compute_dtype,
is_cross_attn=False,
out_embed_dim=dec_embed_dim,
)
# Cross-Attention (MHA)
self.cross_attention = Attention(
config=config,
q_embed_dim=dec_embed_dim,
kv_embed_dim=enc_embed_dim, # Note kv_embed_dim
num_query_heads=dec_config.cross_query_heads,
num_kv_heads=dec_config.cross_query_heads,
head_dim=dec_config.cross_head_dim,
compute_dtype=compute_dtype,
is_cross_attn=True,
out_embed_dim=dec_embed_dim,
)
# MLP
self.mlp = MlpBlock(
embed_dim=dec_embed_dim,
intermediate_dim=dec_config.n_hidden,
compute_dtype=compute_dtype,
)
def forward(
self,
x: torch.Tensor,
state: DecoderInferenceState,
self_attn_cache: KVCache | None = None,
cross_attn_cache: KVCache | None = None,
prefill: bool = False,
) -> torch.Tensor:
residual = x
x_norm = self.pre_sa_norm(x).to(self.compute_dtype)
sa_out = self.self_attention(
Xq=x_norm, # (2, 1, D)
Xkv=x_norm, # (2, 1, D)
q_positions=state.dec_positions, # (2, 1)
kv_positions=state.dec_positions, # (2, 1)
attn_mask=None,
cache=self_attn_cache,
prefill=prefill,
is_causal=prefill,
)
x = residual + sa_out
residual = x
x_norm = self.pre_ca_norm(x).to(self.compute_dtype)
ca_out = self.cross_attention(
Xq=x_norm,
Xkv=state.enc_out,
q_positions=state.dec_positions,
kv_positions=state.enc_positions,
attn_mask=state.dec_cross_attn_mask,
cache=cross_attn_cache,
)
x = residual + ca_out
residual = x
x_norm = self.pre_mlp_norm(x).to(self.compute_dtype)
mlp_out = self.mlp(x_norm)
x = residual + mlp_out
return x
class Decoder(nn.Module):
"""Transformer Decoder Stack using DenseGeneral."""
def __init__(self, config: DiaConfig, compute_dtype: torch.dtype):
super().__init__()
self.config = config
model_config = config.model
dec_config = config.model.decoder
data_config = config.data
self.num_channels = data_config.channels
self.num_layers = dec_config.n_layer
self.embeddings = nn.ModuleList(
[
nn.Embedding(model_config.tgt_vocab_size, dec_config.n_embd, dtype=compute_dtype)
for _ in range(self.num_channels)
]
)
self.layers = nn.ModuleList(
[DecoderLayer(config=config, compute_dtype=compute_dtype) for _ in range(self.num_layers)]
)
self.norm = RMSNorm(
dec_config.n_embd,
eps=model_config.normalization_layer_epsilon,
dtype=torch.float32,
)
self.logits_dense = DenseGeneral(
in_shapes=(dec_config.n_embd,),
out_features=(self.num_channels, model_config.tgt_vocab_size),
axis=(-1,),
weight_dtype=compute_dtype,
)
def precompute_cross_attn_cache(
self,
enc_out: torch.Tensor, # (B, S, E)
enc_positions: torch.Tensor, # (B, S)
) -> list[KVCache]:
"""
Computes the Key and Value tensors for cross-attention for each layer from the encoder output.
"""
per_layer_kv_cache: list[KVCache] = []
for layer in self.layers:
cross_attn_module = layer.cross_attention
k_proj = cross_attn_module.k_proj(enc_out)
v_proj = cross_attn_module.v_proj(enc_out)
k_proj = cross_attn_module.rotary_emb(k_proj, position=enc_positions)
k = k_proj.transpose(1, 2)
v = v_proj.transpose(1, 2)
per_layer_kv_cache.append(KVCache.from_kv(k, v))
return per_layer_kv_cache
def decode_step(
self,
tgt_ids_Bx1xC: torch.Tensor, # [B, 1, C]
state: DecoderInferenceState,
) -> torch.Tensor:
"""
Performs a single decoding step, managing KV caches layer by layer.
Returns:
A tuple containing:
- logits_Bx1xCV: The final output logits for the current step (B, 1, C*V), cast to float32.
"""
x = None
for i in range(self.num_channels):
channel_tokens = tgt_ids_Bx1xC[..., i]
channel_embed = self.embeddings[i](channel_tokens)
x = channel_embed if x is None else x + channel_embed
for i, layer in enumerate(self.layers):
self_cache = state.self_attn_cache[i]
cross_cache = state.cross_attn_cache[i]
x = layer(
x, # (2, 1, D)
state,
self_attn_cache=self_cache,
cross_attn_cache=cross_cache,
)
x = self.norm(x)
logits_Bx1xCxV = self.logits_dense(x)
return logits_Bx1xCxV.to(torch.float32)
def forward(self, tgt_ids_BxTxC: torch.Tensor, state: DecoderInferenceState) -> torch.Tensor:
"""
Forward pass for the Decoder stack, managing KV caches.
Args:
tgt_ids_BxTxC: Target token IDs (B, T, C).
encoder_out: Output from the encoder (B, S, E).
tgt_positions: Positions for target sequence (B, T).
src_positions: Positions for source sequence (B, S).
self_attn_mask: Mask for self-attention.
cross_attn_mask: Mask for cross-attention.
past_key_values: List containing the self-attention KV cache for each layer
from the previous decoding step. `len(past_key_values)` should
equal `num_layers`.
precomputed_cross_attn_kv: A single tuple containing the pre-computed K/V cache
derived from `encoder_out`. This is passed identically
to all layers.
Returns:
A tuple containing:
- logits: The final output logits (B, T, C * V), cast to float32.
- present_key_values: A list containing the updated self-attention KV cache
for each layer for the *current* decoding step.
"""
_, _, num_channels_in = tgt_ids_BxTxC.shape
assert num_channels_in == self.num_channels, "Input channels mismatch"
# Embeddings
x = None
for i in range(self.num_channels):
channel_tokens = tgt_ids_BxTxC[..., i]
channel_embed = self.embeddings[i](channel_tokens)
x = channel_embed if x is None else x + channel_embed
for i, layer in enumerate(self.layers):
self_cache = state.self_attn_cache[i]
cross_cache = state.cross_attn_cache[i]
x = layer(x, state, self_attn_cache=self_cache, cross_attn_cache=cross_cache, prefill=True)
# Final Norm
x = self.norm(x)
logits_BxTxCxV = self.logits_dense(x)
return logits_BxTxCxV.to(torch.float32)
class DiaModel(
nn.Module,
PyTorchModelHubMixin,
repo_url="https://github.com/nari-labs/dia",
pipeline_tag="text-to-speech",
license="apache-2.0",
coders={
DiaConfig: (
lambda x: x.model_dump(),
lambda data: DiaConfig.model_validate(data),
),
},
):
"""PyTorch Dia Model using DenseGeneral."""
def __init__(self, config: DiaConfig, compute_dtype: torch.dtype):
super().__init__()
self.config = config
self.encoder = Encoder(config, compute_dtype)
self.decoder = Decoder(config, compute_dtype)
```
## /dia/model.py
```py path="/dia/model.py"
import time
from enum import Enum
import dac
import numpy as np
import torch
import torchaudio
from .audio import apply_audio_delay, build_delay_indices, build_revert_indices, decode, revert_audio_delay
from .config import DiaConfig
from .layers import DiaModel
from .state import DecoderInferenceState, DecoderOutput, EncoderInferenceState
DEFAULT_SAMPLE_RATE = 44100
def _get_default_device():
if torch.cuda.is_available():
return torch.device("cuda")
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return torch.device("mps")
return torch.device("cpu")
def _sample_next_token(
logits_BCxV: torch.Tensor,
temperature: float,
top_p: float,
cfg_filter_top_k: int | None = None,
) -> torch.Tensor:
if temperature == 0.0:
return torch.argmax(logits_BCxV, dim=-1)
logits_BCxV = logits_BCxV / temperature
if cfg_filter_top_k is not None:
_, top_k_indices_BCxV = torch.topk(logits_BCxV, k=cfg_filter_top_k, dim=-1)
mask = torch.ones_like(logits_BCxV, dtype=torch.bool)
mask.scatter_(dim=-1, index=top_k_indices_BCxV, value=False)
logits_BCxV = logits_BCxV.masked_fill(mask, -torch.inf)
if top_p < 1.0:
probs_BCxV = torch.softmax(logits_BCxV, dim=-1)
sorted_probs_BCxV, sorted_indices_BCxV = torch.sort(probs_BCxV, dim=-1, descending=True)
cumulative_probs_BCxV = torch.cumsum(sorted_probs_BCxV, dim=-1)
sorted_indices_to_remove_BCxV = cumulative_probs_BCxV > top_p
sorted_indices_to_remove_BCxV[..., 1:] = sorted_indices_to_remove_BCxV[..., :-1].clone()
sorted_indices_to_remove_BCxV[..., 0] = 0
indices_to_remove_BCxV = torch.zeros_like(sorted_indices_to_remove_BCxV)
indices_to_remove_BCxV.scatter_(dim=-1, index=sorted_indices_BCxV, src=sorted_indices_to_remove_BCxV)
logits_BCxV = logits_BCxV.masked_fill(indices_to_remove_BCxV, -torch.inf)
final_probs_BCxV = torch.softmax(logits_BCxV, dim=-1)
sampled_indices_BC = torch.multinomial(final_probs_BCxV, num_samples=1)
sampled_indices_C = sampled_indices_BC.squeeze(-1)
return sampled_indices_C
class ComputeDtype(str, Enum):
FLOAT32 = "float32"
FLOAT16 = "float16"
BFLOAT16 = "bfloat16"
def to_dtype(self) -> torch.dtype:
if self == ComputeDtype.FLOAT32:
return torch.float32
elif self == ComputeDtype.FLOAT16:
return torch.float16
elif self == ComputeDtype.BFLOAT16:
return torch.bfloat16
else:
raise ValueError(f"Unsupported compute dtype: {self}")
class Dia:
def __init__(
self,
config: DiaConfig,
compute_dtype: str | ComputeDtype = ComputeDtype.FLOAT32,
device: torch.device | None = None,
):
"""Initializes the Dia model.
Args:
config: The configuration object for the model.
device: The device to load the model onto. If None, will automatically select the best available device.
Raises:
RuntimeError: If there is an error loading the DAC model.
"""
super().__init__()
self.config = config
self.device = device if device is not None else _get_default_device()
if isinstance(compute_dtype, str):
compute_dtype = ComputeDtype(compute_dtype)
self.compute_dtype = compute_dtype.to_dtype()
self.model = DiaModel(config, self.compute_dtype)
self.dac_model = None
@classmethod
def from_local(
cls,
config_path: str,
checkpoint_path: str,
compute_dtype: str | ComputeDtype = ComputeDtype.FLOAT32,
device: torch.device | None = None,
) -> "Dia":
"""Loads the Dia model from local configuration and checkpoint files.
Args:
config_path: Path to the configuration JSON file.
checkpoint_path: Path to the model checkpoint (.pth) file.
device: The device to load the model onto. If None, will automatically select the best available device.
Returns:
An instance of the Dia model loaded with weights and set to eval mode.
Raises:
FileNotFoundError: If the config or checkpoint file is not found.
RuntimeError: If there is an error loading the checkpoint.
"""
config = DiaConfig.load(config_path)
if config is None:
raise FileNotFoundError(f"Config file not found at {config_path}")
dia = cls(config, compute_dtype, device)
try:
state_dict = torch.load(checkpoint_path, map_location=dia.device)
dia.model.load_state_dict(state_dict)
except FileNotFoundError:
raise FileNotFoundError(f"Checkpoint file not found at {checkpoint_path}")
except Exception as e:
raise RuntimeError(f"Error loading checkpoint from {checkpoint_path}") from e
dia.model.to(dia.device)
dia.model.eval()
dia._load_dac_model()
return dia
@classmethod
def from_pretrained(
cls,
model_name: str = "nari-labs/Dia-1.6B",
compute_dtype: str | ComputeDtype = ComputeDtype.FLOAT32,
device: torch.device | None = None,
) -> "Dia":
"""Loads the Dia model from a Hugging Face Hub repository.
Downloads the configuration and checkpoint files from the specified
repository ID and then loads the model.
Args:
model_name: The Hugging Face Hub repository ID (e.g., "nari-labs/Dia-1.6B").
compute_dtype: The computation dtype to use.
device: The device to load the model onto. If None, will automatically select the best available device.
Returns:
An instance of the Dia model loaded with weights and set to eval mode.
Raises:
FileNotFoundError: If config or checkpoint download/loading fails.
RuntimeError: If there is an error loading the checkpoint.
"""
if isinstance(compute_dtype, str):
compute_dtype = ComputeDtype(compute_dtype)
loaded_model = DiaModel.from_pretrained(model_name, compute_dtype=compute_dtype.to_dtype())
config = loaded_model.config
dia = cls(config, compute_dtype, device)
dia.model = loaded_model
dia.model.to(dia.device)
dia.model.eval()
dia._load_dac_model()
return dia
def _load_dac_model(self):
try:
dac_model_path = dac.utils.download()
dac_model = dac.DAC.load(dac_model_path).to(self.device)
except Exception as e:
raise RuntimeError("Failed to load DAC model") from e
self.dac_model = dac_model
def _prepare_text_input(self, text: str) -> torch.Tensor:
"""Encodes text prompt, pads, and creates attention mask and positions."""
text_pad_value = self.config.data.text_pad_value
max_len = self.config.data.text_length
byte_text = text.encode("utf-8")
replaced_bytes = byte_text.replace(b"[S1]", b"\x01").replace(b"[S2]", b"\x02")
text_tokens = list(replaced_bytes)
current_len = len(text_tokens)
padding_needed = max_len - current_len
if padding_needed <= 0:
text_tokens = text_tokens[:max_len]
padded_text_np = np.array(text_tokens, dtype=np.uint8)
else:
padded_text_np = np.pad(
text_tokens,
(0, padding_needed),
mode="constant",
constant_values=text_pad_value,
).astype(np.uint8)
src_tokens = torch.from_numpy(padded_text_np).to(torch.long).to(self.device).unsqueeze(0) # [1, S]
return src_tokens
def _prepare_audio_prompt(self, audio_prompt: torch.Tensor | None) -> tuple[torch.Tensor, int]:
num_channels = self.config.data.channels
audio_bos_value = self.config.data.audio_bos_value
audio_pad_value = self.config.data.audio_pad_value
delay_pattern = self.config.data.delay_pattern
max_delay_pattern = max(delay_pattern)
prefill = torch.full(
(1, num_channels),
fill_value=audio_bos_value,
dtype=torch.int,
device=self.device,
)
prefill_step = 1
if audio_prompt is not None:
prefill_step += audio_prompt.shape[0]
prefill = torch.cat([prefill, audio_prompt], dim=0)
delay_pad_tensor = torch.full(
(max_delay_pattern, num_channels), fill_value=-1, dtype=torch.int, device=self.device
)
prefill = torch.cat([prefill, delay_pad_tensor], dim=0)
delay_precomp = build_delay_indices(
B=1,
T=prefill.shape[0],
C=num_channels,
delay_pattern=delay_pattern,
)
prefill = apply_audio_delay(
audio_BxTxC=prefill.unsqueeze(0),
pad_value=audio_pad_value,
bos_value=audio_bos_value,
precomp=delay_precomp,
).squeeze(0)
return prefill, prefill_step
def _prepare_generation(self, text: str, audio_prompt: str | torch.Tensor | None, verbose: bool):
enc_input_cond = self._prepare_text_input(text)
enc_input_uncond = torch.zeros_like(enc_input_cond)
enc_input = torch.cat([enc_input_uncond, enc_input_cond], dim=0)
if isinstance(audio_prompt, str):
audio_prompt = self.load_audio(audio_prompt)
prefill, prefill_step = self._prepare_audio_prompt(audio_prompt)
if verbose:
print("generate: data loaded")
enc_state = EncoderInferenceState.new(self.config, enc_input_cond)
encoder_out = self.model.encoder(enc_input, enc_state)
dec_cross_attn_cache = self.model.decoder.precompute_cross_attn_cache(encoder_out, enc_state.positions)
dec_state = DecoderInferenceState.new(
self.config, enc_state, encoder_out, dec_cross_attn_cache, self.compute_dtype
)
dec_output = DecoderOutput.new(self.config, self.device)
dec_output.prefill(prefill, prefill_step)
dec_step = prefill_step - 1
if dec_step > 0:
dec_state.prepare_step(0, dec_step)
tokens_BxTxC = dec_output.get_tokens_at(0, dec_step).unsqueeze(0).expand(2, -1, -1)
self.model.decoder.forward(tokens_BxTxC, dec_state)
return dec_state, dec_output
def _decoder_step(
self,
tokens_Bx1xC: torch.Tensor,
dec_state: DecoderInferenceState,
cfg_scale: float,
temperature: float,
top_p: float,
cfg_filter_top_k: int,
) -> torch.Tensor:
audio_eos_value = self.config.data.audio_eos_value
logits_Bx1xCxV = self.model.decoder.decode_step(tokens_Bx1xC, dec_state)
logits_last_BxCxV = logits_Bx1xCxV[:, -1, :, :]
uncond_logits_CxV = logits_last_BxCxV[0, :, :]
cond_logits_CxV = logits_last_BxCxV[1, :, :]
logits_CxV = cond_logits_CxV + cfg_scale * (cond_logits_CxV - uncond_logits_CxV)
logits_CxV[:, audio_eos_value + 1 :] = -torch.inf
logits_CxV[1:, audio_eos_value:] = -torch.inf
pred_C = _sample_next_token(
logits_CxV.float(),
temperature=temperature,
top_p=top_p,
cfg_filter_top_k=cfg_filter_top_k,
)
return pred_C
def _generate_output(self, generated_codes: torch.Tensor) -> np.ndarray:
num_channels = self.config.data.channels
seq_length = generated_codes.shape[0]
delay_pattern = self.config.data.delay_pattern
audio_pad_value = self.config.data.audio_pad_value
max_delay_pattern = max(delay_pattern)
revert_precomp = build_revert_indices(
B=1,
T=seq_length,
C=num_channels,
delay_pattern=delay_pattern,
)
codebook = revert_audio_delay(
audio_BxTxC=generated_codes.unsqueeze(0),
pad_value=audio_pad_value,
precomp=revert_precomp,
T=seq_length,
)[:, :-max_delay_pattern, :]
min_valid_index = 0
max_valid_index = 1023
invalid_mask = (codebook < min_valid_index) | (codebook > max_valid_index)
codebook[invalid_mask] = 0
audio = decode(self.dac_model, codebook.transpose(1, 2))
return audio.squeeze().cpu().numpy()
def load_audio(self, audio_path: str) -> torch.Tensor:
audio, sr = torchaudio.load(audio_path, channels_first=True) # C, T
if sr != DEFAULT_SAMPLE_RATE:
audio = torchaudio.functional.resample(audio, sr, DEFAULT_SAMPLE_RATE)
audio = audio.to(self.device).unsqueeze(0) # 1, C, T
audio_data = self.dac_model.preprocess(audio, DEFAULT_SAMPLE_RATE)
_, encoded_frame, _, _, _ = self.dac_model.encode(audio_data) # 1, C, T
return encoded_frame.squeeze(0).transpose(0, 1)
def save_audio(self, path: str, audio: np.ndarray):
import soundfile as sf
sf.write(path, audio, DEFAULT_SAMPLE_RATE)
@torch.inference_mode()
def generate(
self,
text: str,
max_tokens: int | None = None,
cfg_scale: float = 3.0,
temperature: float = 1.3,
top_p: float = 0.95,
use_torch_compile: bool = False,
cfg_filter_top_k: int = 35,
audio_prompt: str | torch.Tensor | None = None,
audio_prompt_path: str | None = None,
use_cfg_filter: bool | None = None,
verbose: bool = False,
) -> np.ndarray:
audio_eos_value = self.config.data.audio_eos_value
audio_pad_value = self.config.data.audio_pad_value
delay_pattern = self.config.data.delay_pattern
max_tokens = self.config.data.audio_length if max_tokens is None else max_tokens
max_delay_pattern = max(delay_pattern)
self.model.eval()
if audio_prompt_path:
print("Warning: audio_prompt_path is deprecated. Use audio_prompt instead.")
audio_prompt = audio_prompt_path
if use_cfg_filter is not None:
print("Warning: use_cfg_filter is deprecated.")
if verbose:
total_start_time = time.time()
dec_state, dec_output = self._prepare_generation(text, audio_prompt, verbose)
dec_step = dec_output.prefill_step - 1
bos_countdown = max_delay_pattern
eos_detected = False
eos_countdown = -1
if use_torch_compile:
step_fn = torch.compile(self._decoder_step, mode="default")
else:
step_fn = self._decoder_step
if verbose:
print("generate: starting generation loop")
if use_torch_compile:
print("generate: by using use_torch_compile=True, the first step would take long")
start_time = time.time()
while dec_step < max_tokens:
dec_state.prepare_step(dec_step)
tokens_Bx1xC = dec_output.get_tokens_at(dec_step).unsqueeze(0).expand(2, -1, -1)
pred_C = step_fn(
tokens_Bx1xC,
dec_state,
cfg_scale,
temperature,
top_p,
cfg_filter_top_k,
)
if (not eos_detected and pred_C[0] == audio_eos_value) or dec_step == max_tokens - max_delay_pattern - 1:
eos_detected = True
eos_countdown = max_delay_pattern
if eos_countdown > 0:
step_after_eos = max_delay_pattern - eos_countdown
for i, d in enumerate(delay_pattern):
if step_after_eos == d:
pred_C[i] = audio_eos_value
elif step_after_eos > d:
pred_C[i] = audio_pad_value
eos_countdown -= 1
bos_countdown = max(0, bos_countdown - 1)
dec_output.update_one(pred_C, dec_step + 1, bos_countdown > 0)
if eos_countdown == 0:
break
dec_step += 1
if verbose and dec_step % 86 == 0:
duration = time.time() - start_time
print(
f"generate step {dec_step}: speed={86 / duration:.3f} tokens/s, realtime factor={1 / duration:.3f}x"
)
start_time = time.time()
if dec_output.prefill_step >= dec_step + 1:
print("Warning: Nothing generated")
return None
generated_codes = dec_output.generated_tokens[dec_output.prefill_step : dec_step + 1, :]
if verbose:
total_step = dec_step + 1 - dec_output.prefill_step
total_duration = time.time() - total_start_time
print(f"generate: total step={total_step}, total duration={total_duration:.3f}s")
return self._generate_output(generated_codes)
```
## /dia/state.py
```py path="/dia/state.py"
from dataclasses import dataclass
import torch
from .config import DiaConfig
def create_attn_mask(
q_padding_mask_1d: torch.Tensor,
k_padding_mask_1d: torch.Tensor,
device: torch.device,
is_causal: bool = False,
) -> torch.Tensor:
"""
Creates the attention mask (self or cross) mimicking JAX segment ID logic.
"""
B1, Tq = q_padding_mask_1d.shape
B2, Tk = k_padding_mask_1d.shape
assert B1 == B2, "Query and key batch dimensions must match"
p_mask_q = q_padding_mask_1d.unsqueeze(2) # Shape [B, Tq, 1]
p_mask_k = k_padding_mask_1d.unsqueeze(1) # Shape [B, 1, Tk]
# Condition A: Non-padding query attends to non-padding key
non_pad_attends_non_pad = p_mask_q & p_mask_k # Shape [B, Tq, Tk]
# Condition B: Padding query attends to padding key
pad_attends_pad = (~p_mask_q) & (~p_mask_k) # Shape [B, Tq, Tk]
# Combine: True if padding status is compatible (both non-pad OR both pad)
mask = non_pad_attends_non_pad | pad_attends_pad # Shape [B, Tq, Tk]
if is_causal:
assert Tq == Tk, "Causal mask requires query and key sequence lengths to be equal"
causal_mask_2d = torch.tril(torch.ones((Tq, Tk), dtype=torch.bool, device=device)) # Shape [Tq, Tk]
causal_mask = mask & causal_mask_2d # Shape [B, Tq, Tk]
return causal_mask.unsqueeze(1) # Shape [B, 1, Tq, Tk]
else:
return mask.unsqueeze(1) # Shape [B, 1, Tq, Tk]
@dataclass
class EncoderInferenceState:
"""Parameters specifically for encoder inference."""
max_seq_len: int
device: torch.device
positions: torch.Tensor
padding_mask: torch.Tensor
attn_mask: torch.Tensor
@classmethod
def new(cls, config: DiaConfig, cond_src: torch.Tensor) -> "EncoderInferenceState":
"""Creates EtorchrInferenceParams from DiaConfig and a device."""
device = cond_src.device
positions = (
torch.arange(config.data.text_length, dtype=torch.float32, device=device).unsqueeze(0).expand(2, -1)
)
padding_mask = (cond_src != config.data.text_pad_value).to(device).expand(2, -1)
attn_mask = create_attn_mask(padding_mask, padding_mask, device, is_causal=False)
return cls(
max_seq_len=config.data.text_length,
device=device,
positions=positions,
padding_mask=padding_mask,
attn_mask=attn_mask,
)
class KVCache:
def __init__(
self,
num_heads: int,
max_len: int,
head_dim: int,
dtype: torch.dtype,
device: torch.device,
k: torch.Tensor | None = None,
v: torch.Tensor | None = None,
):
self.k = torch.zeros((2, num_heads, max_len, head_dim), dtype=dtype, device=device) if k is None else k
self.v = torch.zeros((2, num_heads, max_len, head_dim), dtype=dtype, device=device) if v is None else v
self.current_idx = torch.tensor(0)
@classmethod
def from_kv(cls, k: torch.Tensor, v: torch.Tensor) -> "KVCache":
return cls(
num_heads=k.shape[1],
max_len=k.shape[2],
head_dim=k.shape[3],
dtype=k.dtype,
device=k.device,
k=k,
v=v,
)
def update(self, k: torch.Tensor, v: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
self.k[:, :, self.current_idx : self.current_idx + 1, :] = k
self.v[:, :, self.current_idx : self.current_idx + 1, :] = v
self.current_idx += 1
return self.k[:, :, : self.current_idx, :], self.v[:, :, : self.current_idx, :]
def prefill(self, k: torch.Tensor, v: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
prefill_len = k.shape[2]
self.k[:, :, :prefill_len, :] = k
self.v[:, :, :prefill_len, :] = v
self.current_idx = prefill_len - 1
@dataclass
class DecoderInferenceState:
"""Parameters specifically for decoder inference."""
device: torch.device
dtype: torch.dtype
enc_out: torch.Tensor
enc_positions: torch.Tensor
dec_positions: torch.Tensor
dec_cross_attn_mask: torch.Tensor
self_attn_cache: list[KVCache]
cross_attn_cache: list[KVCache]
@classmethod
def new(
cls,
config: DiaConfig,
enc_state: EncoderInferenceState,
enc_out: torch.Tensor,
dec_cross_attn_cache: list[KVCache],
compute_dtype: torch.dtype,
) -> "DecoderInferenceState":
"""Creates DecoderInferenceParams from DiaConfig and a device."""
device = enc_out.device
max_audio_len = config.data.audio_length
dec_positions = torch.full((2, 1), fill_value=0, dtype=torch.long, device=device)
tgt_padding_mask = torch.ones((2, 1), dtype=torch.bool, device=device)
dec_cross_attn_mask = create_attn_mask(tgt_padding_mask, enc_state.padding_mask, device, is_causal=False)
self_attn_cache = [
KVCache(
config.model.decoder.kv_heads,
max_audio_len,
config.model.decoder.gqa_head_dim,
compute_dtype,
device,
)
for _ in range(config.model.decoder.n_layer)
]
return cls(
device=device,
dtype=compute_dtype,
enc_out=enc_out,
enc_positions=enc_state.positions,
dec_positions=dec_positions,
dec_cross_attn_mask=dec_cross_attn_mask,
self_attn_cache=self_attn_cache,
cross_attn_cache=dec_cross_attn_cache,
)
def prepare_step(self, step_from: int, step_to: int | None = None) -> None:
if step_to is None:
step_to = step_from + 1
self.dec_positions = (
torch.arange(step_from, step_to, dtype=torch.float32, device=self.device).unsqueeze(0).expand(2, -1)
)
@dataclass
class DecoderOutput:
generated_tokens: torch.Tensor
prefill_step: int
@classmethod
def new(cls, config: DiaConfig, device: torch.device) -> "DecoderOutput":
max_audio_len = config.data.audio_length
return cls(
generated_tokens=torch.full(
(max_audio_len, config.data.channels),
fill_value=-1,
dtype=torch.int,
device=device,
),
prefill_step=0,
)
def get_tokens_at(self, step_from: int, step_to: int | None = None) -> torch.Tensor:
if step_to is None:
step_to = step_from + 1
return self.generated_tokens[step_from:step_to, :]
def update_one(self, dec_out: torch.Tensor, step: int, apply_mask: bool = False):
if apply_mask:
mask = self.generated_tokens[step : step + 1, :] == -1
self.generated_tokens[step : step + 1, :] = torch.where(
mask, dec_out, self.generated_tokens[step : step + 1, :]
)
else:
self.generated_tokens[step : step + 1, :] = dec_out
def prefill(self, dec_out: torch.Tensor, prefill_step: int):
length = dec_out.shape[0]
self.generated_tokens[0:length, :] = dec_out
self.prefill_step = prefill_step
```
## /dia/static/images/banner.png
Binary file available at https://raw.githubusercontent.com/nari-labs/dia/refs/heads/main/dia/static/images/banner.png
## /docker/Dockerfile.cpu
```cpu path="/docker/Dockerfile.cpu"
# Dockerfile.cpu - CPU-only deployment for DIA
# --------------------------------------------------
# Build: docker build . -f docker/Dockerfile.cpu -t dia-cpu
# Run: docker run --rm -p 7860:7860 dia-cpu
FROM python:3.10-slim
# Set non-interactive frontend
ENV DEBIAN_FRONTEND=noninteractive
# Install venv, and system dependencies
RUN apt-get update && apt-get install -y \
python3-venv \
libsndfile1 \
ffmpeg \
curl \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Create non-root user and set up directories
RUN useradd -m -u 1001 appuser && \
mkdir -p /app/outputs /app && \
chown -R appuser:appuser /app
USER appuser
WORKDIR /app
# Copy all code (including pyproject.toml)
COPY --chown=appuser:appuser . .
# Create and activate virtual environment
RUN python3 -m venv /app/venv
ENV PATH="/app/venv/bin:$PATH"
# Install all project dependencies (CPU-only PyTorch)
RUN pip install --upgrade pip && \
pip install torch torchaudio --index-url https://download.pytorch.org/whl/cpu && \
pip install --no-cache-dir -e .[dev]
# Set environment variables
ENV PYTHONUNBUFFERED=1 \
PYTHONPATH=/app
# Expose Gradio default port
ENV GRADIO_SERVER_NAME="0.0.0.0"
EXPOSE 7860
# Entrypoint
CMD ["python3", "app.py"]
```
## /docker/Dockerfile.gpu
```gpu path="/docker/Dockerfile.gpu"
# Dockerfile.gpu - GPU deployment for DIA
# --------------------------------------------------
# Build: docker build . -f docker/Dockerfile.gpu -t dia-gpu
# Run: docker run --rm --gpus all -p 7860:7860 dia-gpu
# Requires NVIDIA Container Toolkit on host.
FROM pytorch/pytorch:2.1.2-cuda12.1-cudnn8-runtime
# Set non-interactive frontend
ENV DEBIAN_FRONTEND=noninteractive
# Install venv, and system dependencies
RUN apt-get update && apt-get install -y \
python3-venv \
libsndfile1 \
ffmpeg \
curl \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Create non-root user and set up directories
RUN useradd -m -u 1001 appuser && \
mkdir -p /app/outputs /app && \
chown -R appuser:appuser /app
USER appuser
WORKDIR /app
# Copy all code (including pyproject.toml)
COPY --chown=appuser:appuser . .
# Create and activate virtual environment
RUN python3 -m venv /app/venv
ENV PATH="/app/venv/bin:$PATH"
# Install all project dependencies
RUN pip install --upgrade pip && pip install --no-cache-dir .
# Set environment variables
ENV PYTHONUNBUFFERED=1 \
PYTHONPATH=/app \
USE_GPU=true \
LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda-12.1/lib64:${LD_LIBRARY_PATH}
# Expose Gradio default port
ENV GRADIO_SERVER_NAME="0.0.0.0"
EXPOSE 7860
# Entrypoint
CMD ["python3", "app.py"]
```
## /example/simple.py
```py path="/example/simple.py"
from dia.model import Dia
model = Dia.from_pretrained("nari-labs/Dia-1.6B", compute_dtype="float16")
text = "[S1] Dia is an open weights text to dialogue model. [S2] You get full control over scripts and voices. [S1] Wow. Amazing. (laughs) [S2] Try it now on Git hub or Hugging Face."
output = model.generate(text, use_torch_compile=True, verbose=True)
model.save_audio("simple.mp3", output)
```
## /example/voice_clone.py
```py path="/example/voice_clone.py"
from dia.model import Dia
model = Dia.from_pretrained("nari-labs/Dia-1.6B", compute_dtype="float16")
# You should put the transcript of the voice you want to clone
# We will use the audio created by running simple.py as an example.
# Note that you will be REQUIRED TO RUN simple.py for the script to work as-is.
clone_from_text = "[S1] Dia is an open weights text to dialogue model. [S2] You get full control over scripts and voices. [S1] Wow. Amazing. (laughs) [S2] Try it now on Git hub or Hugging Face."
clone_from_audio = "simple.mp3"
# For your custom needs, replace above with below and add your audio file to this directory:
# clone_from_text = "[S1] ... [S2] ... [S1] ... corresponding to your_audio_name.mp3"
# clone_from_audio = "your_audio_name.mp3"
# Text to generate
text_to_generate = "[S1] Hello, how are you? [S2] I'm good, thank you. [S1] What's your name? [S2] My name is Dia. [S1] Nice to meet you. [S2] Nice to meet you too."
# It will only return the audio from the text_to_generate
output = model.generate(
clone_from_text + text_to_generate, audio_prompt=clone_from_audio, use_torch_compile=True, verbose=True
)
model.save_audio("voice_clone.mp3", output)
```
## /example_prompt.mp3
Binary file available at https://raw.githubusercontent.com/nari-labs/dia/refs/heads/main/example_prompt.mp3
## /pyproject.toml
```toml path="/pyproject.toml"
[project]
name = "nari-tts"
version = "0.1.0"
description = "Dia - A text-to-speech model for dialogue generation"
readme = "README.md"
requires-python = ">=3.10"
license = {file = "LICENSE"}
authors = [
{name = "Nari Labs", email = "contact@narilabs.ai"}
]
dependencies = [
"descript-audio-codec>=1.0.0",
"gradio>=5.25.2",
"huggingface-hub>=0.30.2",
"numpy>=2.2.4",
"pydantic>=2.11.3",
"safetensors>=0.5.3",
"soundfile>=0.13.1",
"torch==2.6.0",
"torchaudio==2.6.0",
"triton==3.2.0 ; sys_platform == 'linux'",
"triton-windows==3.2.0.post18 ; sys_platform == 'win32'",
]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project.urls]
"Homepage" = "https://github.com/nari-labs/dia"
"Bug Tracker" = "https://github.com/nari-labs/dia/issues"
[tool.hatch.build.targets.wheel]
packages = ["dia"]
[tool.ruff]
# Never enforce `E501` (line length violations).
lint.ignore = ["C901", "E501", "E741", "W605"]
lint.select = ["C", "E", "F", "I", "W"]
line-length = 119
# Ignore import violations in all `__init__.py` files.
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402", "F401", "F403", "F811"]
[tool.ruff.lint.isort]
lines-after-imports = 2
[tool.uv.sources]
torch = [
{ index = "pytorch-cu126", marker = "sys_platform == 'linux' or sys_platform == 'win32'" },
]
torchaudio = [
{ index = "pytorch-cu126", marker = "sys_platform == 'linux' or sys_platform == 'win32'" },
]
[[tool.uv.index]]
name = "pytorch-cu126"
url = "https://download.pytorch.org/whl/cu126"
explicit = true
[dependency-groups]
dev = [
"ninja>=1.11.1.4",
"packaging>=25.0",
]
```
The content has been capped at 50000 tokens, and files over NaN bytes have been omitted. The user could consider applying other filters to refine the result. The better and more specific the context, the better the LLM can follow instructions. If the context seems verbose, the user can refine the filter using uithub. Thank you for using https://uithub.com - Perfect LLM context for any GitHub repo.