### Launch Web Interface Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/README.md Starts the Gradio web interface for the TTS system. ```bash python tts/gradio_api.py # Access at http://127.0.0.1:7860 ``` -------------------------------- ### Checkpoint Directory Structure Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Example layout of checkpoint directories containing configuration files. ```text checkpoints/ ├── diffusion_transformer/ │ ├── config.yaml │ └── model_only_last.ckpt ├── wavvae/ │ ├── config.yaml │ └── decoder.ckpt └── duration_lm/ ├── config.yaml └── model_only_last.ckpt ``` -------------------------------- ### Configure Linux Environment Source: https://github.com/bytedance/megatts3/blob/main/readme.md Setup commands for Linux users, including conda environment creation and dependency installation. ```sh # Create a python 3.10 conda env (you could also use virtualenv) conda create -n megatts3-env python=3.10 conda activate megatts3-env pip install -r requirements.txt # Set the root directory export PYTHONPATH="/path/to/MegaTTS3:$PYTHONPATH" # [Optional] Set GPU export CUDA_VISIBLE_DEVICES=0 # If you encounter bugs with pydantic in inference, you should check if the versions of pydantic and gradio are matched. # [Note] if you encounter bugs related with httpx, please check that whether your environmental variable "no_proxy" has patterns like "::" ``` -------------------------------- ### Inspect state_dict structure Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/07-checkpoint-management.md Example of loading a checkpoint and accessing the state_dict. ```python checkpoint = torch.load('model.ckpt') state_dict = checkpoint['state_dict'] ``` -------------------------------- ### Launch Web Interface Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/00-index.md Start the Gradio web server for local access, remote authentication, or multi-GPU configurations. ```bash # Local access python tts/gradio_api.py # Remote with auth GRADIO_SERVER_NAME=0.0.0.0 \ GRADIO_USERNAME=admin \ GRADIO_PASSWORD=secure_password \ python tts/gradio_api.py # Multi-GPU CUDA_VISIBLE_DEVICES=0,1,2,3 python tts/gradio_api.py ``` -------------------------------- ### ARDurPredictor Usage Example Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Demonstrates initializing the predictor and performing a basic inference call. ```python dur_predictor = ARDurPredictor( hparams, 256, 512, 8, 302, 128 ) durations = dur_predictor.infer( txt_tokens=torch.tensor([[10, 15, 20, 25]]), ling_feas={'tone': torch.zeros(1, 4)}, char_tokens=None, ph2char=None, bert_embed=None, incremental_state={}, ctx_vqcodes=None, return_state=False ) # Output shape: [1, 4] - one duration per phoneme ``` -------------------------------- ### Load configurations with set_hparams Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Examples of loading configurations from files, checkpoints, and applying runtime overrides. ```python from tts.utils.commons.hparams import set_hparams # Load from config file hparams = set_hparams(config='configs/tts_config.yaml') # Load from checkpoint directory hparams = set_hparams(exp_name='./checkpoints/diffusion_transformer') # With runtime overrides hparams = set_hparams( config='configs/tts_config.yaml', hparams_str='batch_size=16,learning_rate=0.0001' ) # Access values print(hparams['sample_rate']) # 24000 print(hparams['batch_size']) # 16 ``` -------------------------------- ### G2P Usage Example Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/03-text-processing.md Demonstrates how to invoke the g2p method within a MegaTTS3DiTInfer instance. ```python # Within MegaTTS3DiTInfer instance ph, tone = self.g2p('你好') # ph: [1, 2] - 2 phonemes # tone: [1, 2] - corresponding tones ``` -------------------------------- ### Define configuration inheritance structure Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Example YAML structure demonstrating base configuration inheritance. ```yaml # configs/tts_config.yaml base_config: ./base_config.yaml sample_rate: 24000 num_mels: 80 encoder_hidden_size: 1024 # configs/base_config.yaml seed: 42 device: cuda ``` -------------------------------- ### Invoke prepare_inputs_for_dit Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/03-text-processing.md Example usage of the prepare_inputs_for_dit method to generate the input dictionary for inference. ```python dit_inputs = self.prepare_inputs_for_dit( mel2ph_ref, mel2ph_pred, ph_ref, tone_ref, ph_pred, tone_pred, vae_latent ) # dit_inputs['phone']: [3, 150] - tripled batch for guidance # dit_inputs['lat_ctx']: [3, 200, 32] - speaker context # Now ready for dit.inference() ``` -------------------------------- ### Run Gradio Interface Locally Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Starts the Gradio server on the default localhost address. ```bash # Default localhost only python tts/gradio_api.py # Access at http://127.0.0.1:7860 ``` -------------------------------- ### Define configuration with variable interpolation Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Example YAML configuration demonstrating math expressions and context variable usage. ```yaml sample_rate: 24000 num_frames: ${sample_rate * 10} # 240000 batch_size: 32 total_samples: ${batch_size * 1000} # 32000 model: hidden_dim: 1024 num_heads: 16 dim_per_head: ${hidden_dim / num_heads} # 64 ``` -------------------------------- ### Launch Web UI Source: https://github.com/bytedance/megatts3/blob/main/readme.md Start the Gradio interface for inference. Remote access requires setting server name and authentication credentials. ```bash python tts/gradio_api.py ``` ```bash GRADIO_SERVER_NAME=0.0.0.0 GRADIO_USERNAME=admin GRADIO_PASSWORD=change_me python tts/gradio_api.py ``` -------------------------------- ### Configure Gradio launch arguments Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Example showing how to set environment variables for local or remote access before calling the configuration builder. ```python # Local access (no auth required) os.environ['GRADIO_SERVER_NAME'] = '127.0.0.1' os.environ['GRADIO_SERVER_PORT'] = '7860' # Remote access (auth required) os.environ['GRADIO_SERVER_NAME'] = '0.0.0.0' os.environ['GRADIO_USERNAME'] = 'admin' os.environ['GRADIO_PASSWORD'] = 'secure_password' kwargs = build_gradio_launch_kwargs() # Returns: # { # 'auth': ('admin', 'secure_password'), # 'server_name': '0.0.0.0', # 'server_port': 7860, # 'debug': False, # 'enable_monitoring': False # } ``` -------------------------------- ### Use torch_load_dist wrapper Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/07-checkpoint-management.md Example of using the torch_load_dist wrapper for optimized distributed checkpoint loading. ```python # In distributed training checkpoint = torch_load_dist('./checkpoints/dit/model_only_last.ckpt', map_location='cpu') ``` -------------------------------- ### Example Usage of decode Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Demonstrates reconstructing a waveform from latents and saving it to a file. ```python # Decode latents waveform = wavvae.decode(latents) # [B, 1, 24000] # Save as audio import scipy.io.wavfile as wavfile wavfile.write('output.wav', 24000, waveform[0, 0].numpy()) ``` -------------------------------- ### Extracting alignment from audio Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/03-text-processing.md Example usage of the align method after loading audio with librosa. ```python import librosa # Load reference audio wav, sr = librosa.load('speaker.wav', sr=24000) # Extract alignment ph_ref, tone_ref, mel2ph = self.align(wav) # ph_ref: [1, N] - phonemes in speaker audio # tone_ref: [1, N] - corresponding tones # mel2ph: [1, T_mel] - which phoneme each mel frame belongs to ``` -------------------------------- ### Execute inference with custom guidance weights Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Example usage of the inference method with specific timesteps and classifier-free guidance weights. ```python with torch.no_grad(): latents = dit.inference( inputs, timesteps=32, seq_cfg_w=[1.6, 2.5] # intelligibility weight, similarity weight ) ``` -------------------------------- ### Diffusion Forward Pass Example Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Demonstrates how to instantiate the Diffusion model and perform a forward pass with dummy input tensors. ```python from tts.modules.llm_dit.dit import Diffusion import torch dit = Diffusion() dit.eval() inputs = { 'lat': torch.randn(2, 100, 32), # 2 samples, 100 latent frames 'lat_ctx': torch.randn(2, 100, 32), # speaker context 'ctx_mask': torch.ones(2, 100, 1), # all frames valid 'phone': torch.randint(1, 302, (2, 50)), # 50 phonemes per sample 'tone': torch.randint(0, 32, (2, 50)), # tone tokens 'mel2ph': torch.randint(1, 51, (2, 100)) # alignment } pred, target = dit.forward(inputs) ``` -------------------------------- ### Configure Windows Environment Source: https://github.com/bytedance/megatts3/blob/main/readme.md Setup commands for Windows users, including specific dependency adjustments and environment variable configuration. ```sh # [The Windows version is currently under testing] # Comment below dependence in requirements.txt: # # WeTextProcessing==1.0.4.1 # Create a python 3.10 conda env (you could also use virtualenv) conda create -n megatts3-env python=3.10 conda activate megatts3-env pip install -r requirements.txt conda install -y -c conda-forge pynini==2.1.5 pip install WeTextProcessing==1.0.3 # [Optional] If you want GPU inference, you may need to install specific version of PyTorch for your GPU from https://pytorch.org/. pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126 # [Note] if you encounter bugs related with `ffprobe` or `ffmpeg`, you can install it through `conda install -c conda-forge ffmpeg` # Set environment variable for root directory set PYTHONPATH="C:\path\to\MegaTTS3;%PYTHONPATH%" # Windows $env:PYTHONPATH="C:\path\to\MegaTTS3;%PYTHONPATH%" # Powershell on Windows conda env config vars set PYTHONPATH="C:\path\to\MegaTTS3;%PYTHONPATH%" # For conda users # [Optional] Set GPU set CUDA_VISIBLE_DEVICES=0 # Windows $env:CUDA_VISIBLE_DEVICES=0 # Powershell on Windows ``` -------------------------------- ### Perform recursive configuration override Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Example of merging a new configuration dictionary into an existing one. ```python old = {'a': 1, 'b': {'c': 2, 'd': 3}} new = {'b': {'d': 99}, 'e': 4} override_config(old, new) # Result: {'a': 1, 'b': {'c': 2, 'd': 99}, 'e': 4} ``` -------------------------------- ### Usage Example for LengthRegulator Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Demonstrates how to instantiate the regulator and map phoneme durations to frame indices. ```python reg = LengthRegulator() # 1 sample, 5 phonemes with durations [4, 3, 5, 2, 6] durations = torch.tensor([[4, 3, 5, 2, 6]]) # total 20 frames # Map output frames to phoneme indices mel2ph = reg.forward(durations) # Output: [1, 20] = [1,1,1,1, 2,2,2, 3,3,3,3,3, 4,4, 5,5,5,5,5,5] ``` -------------------------------- ### Preprocess audio for inference Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/01-megatts3-core-inference.md Examples of using the preprocess method with either pre-extracted latents or on-the-fly VAE encoding. ```python # Load audio file with open('speaker.wav', 'rb') as f: audio_bytes = f.read() # Preprocess with pre-extracted latents context = infer.preprocess( audio_bytes, latent_file='speaker.npy' ) # Or with on-the-fly VAE encoding context = infer.preprocess(audio_bytes) ``` -------------------------------- ### Example Usage of encode_latent Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Demonstrates encoding a 1-second audio tensor into latent representations. ```python wavvae = WavVAE_V3(hparams) wavvae.eval() # 2 samples, 24000 samples each (~1 second) audio = torch.randn(2, 24000) latents = wavvae.encode_latent(audio) # Output shape: [2, 25, 32] - 1 second at 25 Hz ``` -------------------------------- ### Synthesize speech from text Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/01-megatts3-core-inference.md Examples of generating speech audio with different quality settings and saving the result to a file. ```python # After preprocessing context = infer.preprocess(audio_bytes, latent_file='speaker.npy') # Standard synthesis wav_bytes = infer.forward( context, '这是一个测试句子。', time_step=32, p_w=1.6, t_w=2.5 ) # High-quality, similar voice wav_bytes = infer.forward( context, 'This is an English sentence.', time_step=50, p_w=1.8, t_w=3.5 ) # Save result with open('output.wav', 'wb') as f: f.write(wav_bytes) ``` -------------------------------- ### Override configuration via command-line Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Examples of using the --hparams argument to override configuration values at runtime. ```bash # Single value python script.py --hparams="batch_size=16" # Multiple values (comma-separated) python script.py --hparams="batch_size=16,learning_rate=0.0001,use_amp=True" # Nested keys python script.py --hparams="model.num_layers=12,model.hidden_size=768" # List values (space-separated, pipe-separated) python script.py --hparams="decoder_ratios=[5 4 4 3]" ``` -------------------------------- ### Initialize and use model_worker Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Demonstrates starting a worker process on a specific GPU and communicating via multiprocessing queues. ```python import multiprocessing as mp input_q = mp.Queue() output_q = mp.Queue() # Start worker on GPU 0 worker = mp.Process(target=model_worker, args=(input_q, output_q, 0)) worker.start() # Send task input_q.put(('speaker.wav', 'speaker.npy', '你好', 32, 1.6, 2.5)) # Get result result = output_q.get(timeout=60) # WAV bytes or None ``` -------------------------------- ### Invoke main inference function Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Example of calling the main function to trigger synthesis, typically invoked by Gradio UI events. ```python # Called internally by Gradio result = main( 'uploads/speaker.wav', 'uploads/speaker.npy', '这是一个测试', 32, # timesteps 1.4, # p_w 3.0, # t_w processes, input_queue, output_queue ) ``` -------------------------------- ### Use dist_load for distributed checkpoint loading Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/07-checkpoint-management.md Example of using the dist_load context manager to load checkpoints from shared memory in a distributed environment. ```python from tts.utils.commons.ckpt_utils import dist_load, torch_load_dist # Standard load with dist_load(checkpoint_path) as tmp_path: checkpoint = torch.load(tmp_path, map_location='cpu') ``` -------------------------------- ### Execute training with configuration overrides Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Command-line execution demonstrating how to override configuration parameters. ```bash python train.py \ --config configs/megatts3.yaml \ --hparams="batch_size=64,learning_rate=0.0002,model.encoder_layers=32" ``` -------------------------------- ### Initializing MegaTTS3DiTInfer Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/01-megatts3-core-inference.md Demonstrates various ways to instantiate the inference engine, including GPU selection and custom checkpoint path configuration. ```python from tts.infer_cli import MegaTTS3DiTInfer # Initialize with GPU infer = MegaTTS3DiTInfer(device='cuda:0') # Or let it auto-detect infer = MegaTTS3DiTInfer() # With custom checkpoint locations infer = MegaTTS3DiTInfer( ckpt_root='/path/to/checkpoints', dit_exp_name='my_dit_model', precision=torch.float32 ) ``` -------------------------------- ### Initialize WavVAE_V3 Constructor Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Defines the constructor signature for the WavVAE_V3 class. ```python def __init__(self, hparams: Dict[str, Any]) -> None ``` -------------------------------- ### Represent Duration Codes Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/08-types-and-data-structures.md Example of mapping duration codes to frame counts and time durations. ```python duration_codes = [32, 16, 48, 8] # [320ms, 160ms, 480ms, 80ms] # Mel frames = sum = 64 frames ≈ 640ms total ``` -------------------------------- ### Apply variable interpolation with traverse_dict Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Example usage of traverse_dict to resolve string-based expressions within a dictionary. ```python hparams = {'a': 1, 'b': {'c': '${a * 2}'}} traverse_dict(hparams, parse_config, hparams) # Result: {'a': 1, 'b': {'c': 2}} ``` -------------------------------- ### Initialize main execution and Gradio interface Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Sets up multiprocessing for worker processes and launches the Gradio interface using the generated configuration. ```python if __name__ == '__main__': # Setup multiprocessing mp.set_start_method('spawn', force=True) mp_manager = mp.Manager() # Get CUDA devices devices = os.environ.get('CUDA_VISIBLE_DEVICES', '') devices = devices.split(",") if devices else None # Create task queues input_queue = mp_manager.Queue() output_queue = mp_manager.Queue() # Start worker processes num_workers = 1 for i in range(num_workers): device_id = i % len(devices) if devices else None p = mp.Process(target=model_worker, args=(input_queue, output_queue, device_id)) p.start() # Create Gradio interface api_interface = gr.Interface( fn=partial(main, ...), inputs=[...], outputs=[...], title="MegaTTS3", concurrency_limit=1 ) # Launch api_interface.launch(**build_gradio_launch_kwargs()) ``` -------------------------------- ### Configure Docker Environment Source: https://github.com/bytedance/megatts3/blob/main/readme.md Commands for building and running the MegaTTS3 container for GPU or CPU inference. ```sh # [The Docker version is currently under testing] # ! You should download the pretrained checkpoint before running the following command docker build . -t megatts3:latest # For GPU inference (local-only by default) docker run -it -p 127.0.0.1:7860:7860 --gpus all -e CUDA_VISIBLE_DEVICES=0 megatts3:latest # For CPU inference (local-only by default) docker run -it -p 127.0.0.1:7860:7860 megatts3:latest # Remote exposure must explicitly enable auth. docker run -it -p 7860:7860 --gpus all \ -e CUDA_VISIBLE_DEVICES=0 \ -e GRADIO_SERVER_NAME=0.0.0.0 \ -e GRADIO_USERNAME=admin \ -e GRADIO_PASSWORD=change_me \ megatts3:latest # Visit http://127.0.0.1:7860/ for gradio. ``` -------------------------------- ### build_gradio_launch_kwargs Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Constructs the configuration dictionary required to launch the Gradio interface, handling server binding and authentication security rules. ```APIDOC ## build_gradio_launch_kwargs() ### Description Constructs Gradio launch configuration from environment variables. It enforces security rules requiring authentication when binding to non-local addresses. ### Environment Variables - **GRADIO_SERVER_NAME** (str) - Default: '127.0.0.1' - Server bind address - **GRADIO_SERVER_PORT** (str) - Default: '7860' - Server port - **GRADIO_USERNAME** (str) - Optional - Authentication username - **GRADIO_PASSWORD** (str) - Optional - Authentication password ### Returns - **auth** (Tuple[str, str] or None) - Authentication credentials - **debug** (bool) - Always False - **enable_monitoring** (bool) - Always False - **server_name** (str) - Bind address - **server_port** (int) - Port number ### Security Rules - If binding to non-local address (not 127.0.0.1, localhost, ::1), both username AND password must be set. - Raises RuntimeError if authentication is incomplete. ``` -------------------------------- ### Save and Load Optimizer State Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/07-checkpoint-management.md Demonstrates how to bundle model weights and optimizer states into a checkpoint file and restore them for training resumption. ```python # Save optimizer state with model checkpoint = { 'state_dict': model.state_dict(), 'optimizer_states': [optimizer.state_dict()], 'global_step': 12345 } torch.save(checkpoint, 'training_checkpoint.ckpt') # Load optimizer state to resume training optimizer = torch.optim.Adam(model.parameters()) load_ckpt( model, 'training_checkpoint.ckpt', load_opt=True, opts=[optimizer] ) # Optimizer state restored: learning rate schedule, momentum, etc. ``` -------------------------------- ### Define remove_meta_key function signature Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Signature for the utility that recursively strips metadata keys starting with double underscores. ```python def remove_meta_key(d: Dict) -> None ``` -------------------------------- ### Load and Access Hyperparameters Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/00-index.md Initialize the global configuration from files or directories and access values using the hparams dictionary. ```python from tts.utils.commons.hparams import set_hparams, hparams # From config file hparams_dict = set_hparams(config='path/to/config.yaml') # From checkpoint directory hparams_dict = set_hparams(exp_name='./checkpoints/dit') # With overrides hparams_dict = set_hparams( config='config.yaml', hparams_str='batch_size=16,num_layers=12' ) # Access globally sr = hparams['sample_rate'] ``` -------------------------------- ### Load Hyperparameters Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/README.md Configures the experiment environment by loading hyperparameters from a checkpoint directory. ```python from tts.utils.commons.hparams import set_hparams hparams = set_hparams(exp_name='./checkpoints/dit') sr = hparams['sample_rate'] # 24000 ``` -------------------------------- ### Clone MegaTTS3 Repository Source: https://github.com/bytedance/megatts3/blob/main/readme.md Initial steps to download the source code from GitHub. ```sh # Clone the repository git clone https://github.com/bytedance/MegaTTS3 cd MegaTTS3 ``` -------------------------------- ### Define base configuration file Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md YAML structure for the base configuration settings. ```yaml # configs/base.yaml seed: 42 device: cuda debug: False audio: sample_rate: 24000 n_fft: 2048 n_mels: 80 hop_size: 300 model: encoder_dim: 1024 encoder_layers: 24 num_heads: 16 ``` -------------------------------- ### Define build_gradio_launch_kwargs signature Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Function signature for constructing Gradio launch configuration from environment variables. ```python def build_gradio_launch_kwargs() -> Dict[str, Any] ``` -------------------------------- ### MegaTTS3DiTInfer.__init__ Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/01-megatts3-core-inference.md Initializes the MegaTTS3 inference engine, loads all required neural modules, and configures the environment for speech synthesis. ```APIDOC ## MegaTTS3DiTInfer.__init__ ### Description Initializes the inference orchestrator. This constructor loads the DiT, Aligner, WaveVAE, Duration Predictor, and G2P models, and sets up text normalization and audio processing utilities. ### Parameters - **device** (str | None) - Optional - Torch device ('cuda' or 'cpu'). Defaults to auto-detect. - **ckpt_root** (str) - Optional - Root directory containing all model checkpoints. Defaults to './checkpoints'. - **dit_exp_name** (str) - Optional - Directory name for Diffusion Transformer model checkpoint. Defaults to 'diffusion_transformer'. - **frontend_exp_name** (str) - Optional - Directory name for Aligner/Frontend LM checkpoint. Defaults to 'aligner_lm'. - **wavvae_exp_name** (str) - Optional - Directory name for WaveVAE checkpoint. Defaults to 'wavvae'. - **dur_ckpt_path** (str) - Optional - Directory name for Duration Predictor checkpoint. Defaults to 'duration_lm'. - **g2p_exp_name** (str) - Optional - Directory name for Grapheme-to-Phoneme model checkpoint. Defaults to 'g2p'. - **precision** (torch.dtype) - Optional - Inference precision (torch.float16 or torch.float32). Defaults to torch.float16. ``` -------------------------------- ### Run Gradio Interface with Remote Access and Authentication Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Configures the server to accept remote connections and requires password authentication. ```bash # Allow remote connections with password protection GRADIO_SERVER_NAME=0.0.0.0 \ GRADIO_USERNAME=admin \ GRADIO_PASSWORD=your_password \ python tts/gradio_api.py # Access at http://your_server_ip:7860 ``` -------------------------------- ### Run Gradio Interface with Multi-GPU Support Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/05-gradio-web-interface.md Specifies the GPU devices to be used by the application. ```bash # Use GPU 0 and 1 CUDA_VISIBLE_DEVICES=0,1 python tts/gradio_api.py ``` -------------------------------- ### Diffusion.__init__ Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Initializes the Diffusion model with a fixed architecture for acoustic latent prediction. ```APIDOC ## Diffusion.__init__ ### Description Creates a diffusion model with a hardcoded architecture for processing linguistic and speaker information to predict acoustic latents. ### Signature `def __init__(self) -> None` ``` -------------------------------- ### Define derived configuration file Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md YAML structure for configuration that extends the base settings. ```yaml # configs/megatts3.yaml base_config: ./base.yaml audio: f_min: 40 f_max: 7600 model: decoder_dim: 512 decoder_layers: 6 max_seq_len: 16384 training: batch_size: 32 learning_rate: 0.0001 num_epochs: 100 warmup_steps: ${training.batch_size * 1000} ``` -------------------------------- ### View merged configuration result Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md The resulting dictionary after loading the inherited configuration files. ```python { 'seed': 42, 'device': 'cuda', 'sample_rate': 24000, 'num_mels': 80, 'encoder_hidden_size': 1024 } ``` -------------------------------- ### Project Directory Structure Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/00-index.md Overview of the MegaTTS3 repository layout, including core modules, utilities, and checkpoint locations. ```text megatts3/ ├── tts/ │ ├── infer_cli.py # Main inference pipeline │ ├── gradio_api.py # Web interface │ ├── frontend_function.py # G2P, alignment, duration │ │ │ ├── modules/ │ │ ├── llm_dit/ │ │ │ ├── dit.py # Diffusion Transformer │ │ │ ├── cfm.py # Conditional Flow Matcher │ │ │ ├── transformer.py # Transformer backbone │ │ │ └── time_embedding.py # Timestep encoding │ │ │ │ │ ├── ar_dur/ │ │ │ ├── ar_dur_predictor.py # Duration model │ │ │ └── commons/ │ │ │ ├── nar_tts_modules.py # LengthRegulator │ │ │ ├── transformer.py │ │ │ └── rel_transformer.py │ │ │ │ │ ├── wavvae/ │ │ │ └── decoder/ │ │ │ ├── wavvae_v3.py # WaveVAE codec │ │ │ ├── hifigan_modules.py │ │ │ └── seanet_encoder.py │ │ │ │ │ └── aligner/ │ │ └── whisper_small.py # Alignment model │ │ │ └── utils/ │ ├── audio_utils/ │ │ ├── io.py # Audio I/O │ │ ├── align.py # Alignment utilities │ │ └── plot.py # Visualization │ │ │ ├── text_utils/ │ │ ├── text_encoder.py # Token encoder │ │ ├── split_text.py # Text chunking │ │ └── ph_tone_convert.py # Phoneme utils │ │ │ └── commons/ │ ├── hparams.py # Configuration │ ├── ckpt_utils.py # Checkpoint I/O │ └── trainer.py # Training utilities │ ├── checkpoints/ │ ├── diffusion_transformer/ │ │ ├── config.yaml │ │ └── model_only_last.ckpt │ ├── wavvae/ │ │ ├── config.yaml │ │ └── decoder.ckpt │ ├── duration_lm/ │ │ ├── config.yaml │ │ └── model_only_last.ckpt │ ├── aligner_lm/ │ │ ├── config.yaml │ │ └── model.ckpt │ └── g2p/ │ ├── config.json │ ├── pytorch_model.bin │ └── tokenizer.model │ └── requirements.txt ``` -------------------------------- ### Synthesize voice with Python Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/00-index.md Use the MegaTTS3DiTInfer class to preprocess reference audio and generate speech from text. ```python from tts.infer_cli import MegaTTS3DiTInfer # Initialize infer = MegaTTS3DiTInfer(device='cuda:0') # Load reference audio with open('speaker.wav', 'rb') as f: audio_bytes = f.read() # Preprocess speaker context = infer.preprocess(audio_bytes, latent_file='speaker.npy') # Synthesize wav_bytes = infer.forward( context, '你好,这是一个测试。', time_step=32, p_w=1.6, t_w=2.5 ) # Save with open('output.wav', 'wb') as f: f.write(wav_bytes) ``` -------------------------------- ### View resulting configuration object Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md The final state of the configuration dictionary after applying overrides and interpolation. ```python { 'seed': 42, 'device': 'cuda', 'debug': False, 'audio': { 'sample_rate': 24000, 'n_fft': 2048, 'n_mels': 80, 'hop_size': 300, 'f_min': 40, 'f_max': 7600 }, 'model': { 'encoder_dim': 1024, 'encoder_layers': 32, # Overridden 'num_heads': 16, 'decoder_dim': 512, 'decoder_layers': 6, 'max_seq_len': 16384 }, 'training': { 'batch_size': 64, # Overridden 'learning_rate': 0.0002, # Overridden 'num_epochs': 100, 'warmup_steps': 64000 # Computed }, 'work_dir': './checkpoints/megatts3' } ``` -------------------------------- ### save_wav(wav_bytes, path) Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/04-audio-utilities.md Writes WAV bytes to a file on disk, supporting automatic conversion to MP3 if the path extension is .mp3. ```APIDOC ## Function: save_wav ### Description Writes WAV bytes to disk file. Optionally converts to MP3 if path ends with .mp3. ### Parameters - **wav_bytes** (bytes) - Required - Output from to_wav_bytes() - **path** (str) - Required - File path (.wav or .mp3) ``` -------------------------------- ### Run Standard Inference via CLI Source: https://github.com/bytedance/megatts3/blob/main/readme.md Execute speech synthesis using prompt audio and text. Adjust p_w and t_w weights to balance intelligibility and similarity. ```bash python tts/infer_cli.py --input_wav 'assets/Chinese_prompt.wav' --input_text "另一边的桌上,一位读书人嗤之以鼻道,'佛子三藏,神子燕小鱼是什么样的人物,李家的那个李子夜如何与他们相提并论?'" --output_dir ./gen ``` ```bash python tts/infer_cli.py --input_wav 'assets/English_prompt.wav' --input_text 'As his long promised tariff threat turned into reality this week, top human advisers began fielding a wave of calls from business leaders, particularly in the automotive sector, along with lawmakers who were sounding the alarm.' --output_dir ./gen --p_w 2.0 --t_w 3.0 ``` -------------------------------- ### Load Checkpoint Configuration Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Use set_hparams to load configuration settings from a specific checkpoint directory. ```python from tts.utils.commons.hparams import set_hparams # Load checkpoint config hparams_dit = set_hparams( exp_name='./checkpoints/diffusion_transformer', print_hparams=False # Don't print ) # Access checkpoint-specific settings print(hparams_dit['encoder_dim']) # 1024 print(hparams_dit['encoder_layers']) # 24 ``` -------------------------------- ### Load model checkpoints with load_ckpt Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/07-checkpoint-management.md Use this function to load model weights and optionally optimizer states from checkpoint files. It supports both single and multiple model configurations. ```python from tts.utils.commons.ckpt_utils import load_ckpt import torch.nn as nn # Single model model = SomeModel() load_ckpt(model, './checkpoints/dit', model_name='dit', strict=False) # Multiple models models = [dit, g2p, dur_model] model_names = ['dit', 'g2p_model', 'dur_model'] load_ckpt(models, './checkpoints/', model_name=model_names) # With optimizer optimizer = torch.optim.Adam(model.parameters()) load_ckpt( model, './checkpoints/training', model_name='model', load_opt=True, opts=[optimizer] ) ``` -------------------------------- ### Run inference via CLI Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/00-index.md Execute synthesis tasks using the command line with options for accent control and quality adjustments. ```bash # Basic synthesis python tts/infer_cli.py \ --input_wav speaker.wav \ --input_text "Synthesize this text." \ --output_dir ./outputs # With accent control python tts/infer_cli.py \ --input_wav speaker.wav \ --input_text "This has an accent." \ --output_dir ./outputs \ --p_w 1.0 --t_w 3.0 # With quality boost python tts/infer_cli.py \ --input_wav speaker.wav \ --input_text "High quality synthesis." \ --output_dir ./outputs \ --time_step 50 --p_w 2.0 --t_w 3.5 ``` -------------------------------- ### set_hparams(config='', exp_name='', hparams_str='', print_hparams=True, global_hparams=True) Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Loads YAML configuration from a file or checkpoint directory, applies runtime overrides, and returns the merged hyperparameters dictionary. ```APIDOC ## set_hparams ### Description Loads YAML configuration from a file or checkpoint directory, applies runtime overrides, and returns the merged hyperparameters dictionary. ### Parameters - **config** (str) - Optional - Path to config.yaml file. - **exp_name** (str) - Optional - Checkpoint directory path. - **hparams_str** (str) - Optional - Comma-separated runtime overrides (e.g., "lr=0.001,batch_size=32"). - **print_hparams** (bool) - Optional - Print loaded configuration to console (default: True). - **global_hparams** (bool) - Optional - Update global hparams dict if True (default: True). ### Returns - **Dict[str, Any]** - Dictionary containing merged hyperparameters. ``` -------------------------------- ### prepare_inputs_for_dit Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/03-text-processing.md Assembles inputs for the Diffusion Transformer, including classifier-free guidance conditioning by creating three batch copies with varying levels of conditioning. ```APIDOC ## prepare_inputs_for_dit ### Description Assembles inputs for Diffusion Transformer, including classifier-free guidance conditioning. The function concatenates reference and predicted sequences, adjusts tones, and creates three batch copies for CFG (full, text-conditioned, and unconditional). ### Signature `prepare_inputs_for_dit(mel2ph_ref: Tensor, mel2ph_pred: Tensor, ph_ref: Tensor, tone_ref: Tensor, ph_pred: Tensor, tone_pred: Tensor, vae_latent: Tensor) -> Dict[str, Tensor]` ### Parameters - **mel2ph_ref** (Tensor) - Reference mel-to-phoneme alignment [1, T_mel_ref] - **mel2ph_pred** (Tensor) - Predicted mel-to-phoneme alignment [1, T_mel_pred] - **ph_ref** (Tensor) - Reference phoneme sequence [1, T_ph_ref] - **tone_ref** (Tensor) - Reference tone sequence [1, T_tone_ref] - **ph_pred** (Tensor) - Predicted phoneme sequence [1, T_ph_pred] - **tone_pred** (Tensor) - Predicted tone sequence [1, T_tone_pred] - **vae_latent** (Tensor) - Speaker VAE latent [1, T_latent, 32] ### Returns - **phone** (Tensor) - [3, T_phone_total] 3 copies: [full, full, cfg_mask] for guidance - **tone** (Tensor) - [3, T_tone_total] 3 copies: [full, full, cfg_mask] for guidance - **lat_ctx** (Tensor) - [3, T_latent, 32] Speaker context: [latent, latent, 0] for guidance - **ctx_mask** (Tensor) - [3, T_latent, 1] Binary mask marking valid frames - **dur** (Tensor) - [3, T_latent] Duration alignment: [ref+pred, ref+pred, ref+pred] ### Example ```python dit_inputs = self.prepare_inputs_for_dit( mel2ph_ref, mel2ph_pred, ph_ref, tone_ref, ph_pred, tone_pred, vae_latent ) ``` ``` -------------------------------- ### load_config(config_fn, config_chains, loaded_configs) Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/06-configuration-system.md Recursively loads YAML configuration files with support for base configuration inheritance. ```APIDOC ## load_config ### Description Recursively loads YAML configuration with inheritance support. If a 'base_config' key exists, it loads and merges the base configuration first. ### Parameters - **config_fn** (str) - Required - Path to config file. - **config_chains** (List[str]) - Required - List accumulating config file path chain for logging. - **loaded_configs** (Set[str]) - Required - Set preventing circular dependency. ### Returns - **Dict[str, Any]** - Merged configuration dictionary. ``` -------------------------------- ### load_ckpt Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/07-checkpoint-management.md Loads a model checkpoint with options for strict parameter matching and selective component loading. ```APIDOC ## load_ckpt(model, path, strict=True, delete_unmatch=False, model_name=None) ### Description Loads model parameters from a checkpoint file. Supports strict mode for exact architecture matching and non-strict mode for partial loading or architecture evolution. ### Parameters - **model** (nn.Module) - Required - The model instance to load parameters into. - **path** (str) - Required - Path to the checkpoint file. - **strict** (bool) - Optional - If True, requires exact parameter count and shape match. Defaults to True. - **delete_unmatch** (bool) - Optional - If True, skips parameters with shape mismatches. Defaults to False. - **model_name** (str) - Optional - Specifies a component name to load selectively from the checkpoint. ``` -------------------------------- ### TokenTextEncoder.__init__ Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/03-text-processing.md Constructor for the TokenTextEncoder class, used to initialize the vocabulary mapping. ```APIDOC ## TokenTextEncoder.__init__ ### Description Initializes the encoder with a vocabulary mapping between token strings and integer IDs. ### Parameters - **vocab_filename** (Optional[str]) - Optional - Load vocabulary from file (mutually exclusive with vocab_list). - **reverse** (bool) - Optional - Reverse token order during encode/decode. - **vocab_list** (Optional[List[str]]) - Optional - Initialize from Python list (mutually exclusive with vocab_filename). - **replace_oov** (Optional[str]) - Optional - Replace out-of-vocabulary tokens with this string. - **num_reserved_ids** (int) - Optional - Number of reserved IDs (default: 3; PAD=0, EOS=1, UNK=2). ``` -------------------------------- ### List all checkpoints with get_all_ckpts Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/07-checkpoint-management.md Retrieves a list of all checkpoint file paths in a directory, sorted by step number in descending order. ```python all_ckpts = get_all_ckpts('./checkpoints/dit') # Returns: ['...steps_50000.ckpt', '...steps_40000.ckpt', '...steps_30000.ckpt'] # Get oldest checkpoint oldest = all_ckpts[-1] ``` -------------------------------- ### forward(resource_context, input_text, time_step=32, p_w=1.6, t_w=2.5, dur_disturb=0.1, dur_alpha=1.0, **kwargs) Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/01-megatts3-core-inference.md Generates speech audio from target text using speaker characteristics obtained from the preprocess method. ```APIDOC ## forward ### Description Generates speech audio from target text using speaker characteristics from preprocessing. The method performs text normalization, G2P conversion, diffusion inference, and waveform decoding. ### Parameters - **resource_context** (Dict) - Required - Output from preprocess() containing speaker conditioning. - **input_text** (str) - Required - Target text to synthesize (Chinese or English). - **time_step** (int) - Optional - Number of diffusion steps (higher = better quality, slower). - **p_w** (float) - Optional - Intelligibility weight for classifier-free guidance. - **t_w** (float) - Optional - Similarity weight for speaker consistency guidance. - **dur_disturb** (float) - Optional - Duration perturbation magnitude. - **dur_alpha** (float) - Optional - Duration scaling factor. - **kwargs** (dict) - Optional - Additional keyword arguments. ### Returns - **wav_bytes** (bytes) - Synthesized WAV audio at 24kHz ``` -------------------------------- ### Load Model Checkpoint Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/README.md Loads a model checkpoint into the specified model instance. ```python from tts.utils.commons.ckpt_utils import load_ckpt load_ckpt(model, './checkpoints/dit', model_name='dit', strict=False) ``` -------------------------------- ### MegaTTS3DiTInfer.build_model Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/01-megatts3-core-inference.md Explicitly loads and initializes all neural network models onto the specified device. ```APIDOC ## MegaTTS3DiTInfer.build_model ### Description Loads and initializes all neural network models. This method is called automatically during initialization but can be invoked to re-initialize models on a specific device. ### Parameters - **device** (str) - Required - Target device for model placement ('cuda' or 'cpu'). ``` -------------------------------- ### convert_to_wav_bytes(audio_binary) Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/04-audio-utilities.md Converts raw audio bytes in various formats (MP3, M4A, OGG, etc.) into WAV format bytes. ```APIDOC ## Function: convert_to_wav_bytes ### Description Converts audio in any format to WAV format bytes using pydub. ### Parameters - **audio_binary** (bytes) - Required - Raw audio bytes (MP3, M4A, OGG, etc.) ### Returns BytesIO object positioned at start containing WAV data ``` -------------------------------- ### load_ckpt Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/07-checkpoint-management.md Loads model weights from a checkpoint file, handling distributed training and partial loading. It supports loading optimizer states and specific training steps. ```APIDOC ## load_ckpt(cur_model, ckpt_base_dir, model_name='model', force=True, strict=True, silent=False, load_opt=False, opts=None, steps=None, checkpoint=None, ckpt_path='', delete_unmatch=True) ### Description Loads model weights from a checkpoint file. It handles distributed training prefixes, optional optimizer state loading, and model state dict matching. ### Parameters - **cur_model** (Module | List[Module]) - Required - Model(s) to load weights into - **ckpt_base_dir** (str) - Required - Checkpoint directory or file path - **model_name** (str | List[str]) - Optional - State dict key(s) to load (default: 'model') - **force** (bool) - Optional - Raise error if checkpoint not found (default: True) - **strict** (bool) - Optional - Require exact state dict match (default: True) - **silent** (bool) - Optional - Suppress console output (default: False) - **load_opt** (bool) - Optional - Also load optimizer state (default: False) - **opts** (List[Optimizer] | None) - Optional - Optimizer(s) to load state into - **steps** (int | None) - Optional - Specific checkpoint step to load - **checkpoint** (Dict | None) - Optional - Pre-loaded checkpoint dict - **ckpt_path** (str) - Optional - Override checkpoint file path - **delete_unmatch** (bool) - Optional - Delete mismatched parameter keys (default: True) ### Returns - **int** - Global training step from checkpoint, or None if not found ``` -------------------------------- ### Initialize Diffusion Constructor Source: https://github.com/bytedance/megatts3/blob/main/_autodocs/02-neural-modules.md Defines the constructor signature for the Diffusion class. ```python def __init__(self) -> None ```