### Programmatic Distributed Training Setup Source: https://context7.com/nvidia/waveglow/llms.txt This snippet shows how to programmatically initialize distributed training utilities within your training script, including setting up ranks, GPUs, and backend. ```python from distributed import init_distributed, apply_gradient_allreduce, reduce_tensor init_distributed(rank=0, num_gpus=4, group_name='group_2024', dist_backend='nccl', dist_url='tcp://localhost:54321') model = apply_gradient_allreduce(model) # wraps backward hook for allreduce reduced_loss = reduce_tensor(loss.data, num_gpus=4).item() ``` -------------------------------- ### Train WaveGlow Model (Single GPU, FP32) Source: https://context7.com/nvidia/waveglow/llms.txt Use this command to start training a WaveGlow model on a single GPU using FP32 precision. Ensure your configuration file is correctly set up. ```bash python train.py -c config.json ``` -------------------------------- ### Batch Audio Synthesis (FP16 Inference) Source: https://context7.com/nvidia/waveglow/llms.txt Perform audio synthesis using FP16 inference for increased speed, especially on GPUs like V100. This requires the Apex library to be installed. ```bash python3 inference.py \ -f mel_files.txt \ -w waveglow_256channels.pt \ -o synthesized/ \ --is_fp16 \ --sigma 0.6 ``` -------------------------------- ### Batch Audio Synthesis with Denoising Source: https://context7.com/nvidia/waveglow/llms.txt Synthesize audio with an optional denoising step. Adjust the --denoiser_strength parameter starting from 0.1 to control the level of denoising. ```bash python3 inference.py \ -f mel_files.txt \ -w waveglow_256channels.pt \ -o synthesized/ \ --sigma 0.6 \ --denoiser_strength 0.1 \ --sampling_rate 22050 ``` -------------------------------- ### Prepare Training Files for WaveGlow Source: https://github.com/nvidia/waveglow/blob/master/README.md Create lists of .wav file names for training and testing datasets. This is a prerequisite for training your own WaveGlow model. ```bash ls data/*.wav | tail -n+10 > train_files.txt ls data/*.wav | head -n10 > test_files.txt ``` -------------------------------- ### Clone WaveGlow Repository and Initialize Submodules Source: https://github.com/nvidia/waveglow/blob/master/README.md Use these commands to clone the WaveGlow repository and initialize its submodules, which is the first step in setting up the project. ```bash git clone https://github.com/NVIDIA/waveglow.git cd waveglow git submodule init git submodule update ``` -------------------------------- ### Prepare Training and Test File Lists Source: https://context7.com/nvidia/waveglow/llms.txt Shell commands to create text files listing WAV files for training and testing. `train_files.txt` contains all WAVs except the first 10, while `test_files.txt` contains the first 10. ```bash # Prepare file lists ls data/*.wav | tail -n+10 > train_files.txt ls data/*.wav | head -n10 > test_files.txt ``` -------------------------------- ### WaveGlow Core Model: Training and Inference (`glow.py`) Source: https://context7.com/nvidia/waveglow/llms.txt Demonstrates instantiation of the WaveGlow model, a forward pass for training using mel-spectrograms and audio, and an inference pass to generate audio from mel-spectrograms. Ensure the model is moved to CUDA and weight normalization is removed for inference speed. ```python import torch from glow import WaveGlow, WaveGlowLoss # --- Instantiate the model --- waveglow_config = { "n_mel_channels": 80, "n_flows": 12, "n_group": 8, "n_early_every": 4, "n_early_size": 2, "WN_config": { "n_layers": 8, "n_channels": 256, "kernel_size": 3 } } model = WaveGlow(**waveglow_config).cuda() # --- Training forward pass --- # mel: (batch, n_mel_channels, mel_frames) # audio: (batch, audio_samples) mel = torch.randn(2, 80, 62).cuda() audio = torch.randn(2, 16000).cuda() z, log_s_list, log_det_W_list = model((mel, audio)) # z shape: (batch, n_group, time_groups) criterion = WaveGlowLoss(sigma=1.0) loss = criterion((z, log_s_list, log_det_W_list)) loss.backward() # --- Inference (mel → audio) --- model.eval() model = WaveGlow.remove_weightnorm(model) # fuse weight norm for speed with torch.no_grad(): mel_input = torch.randn(1, 80, 62).cuda() audio_out = model.infer(mel_input, sigma=0.6) # audio_out shape: (1, audio_samples) — values in [-1, 1] print(audio_out.shape) # e.g., torch.Size([1, 15872]) ``` -------------------------------- ### CLI: Precompute Mel-Spectrograms with Mel2Samp Source: https://context7.com/nvidia/waveglow/llms.txt Command-line interface for precomputing mel-spectrograms for a list of WAV files. Specify input file list, output directory, and configuration file. ```bash python mel2samp.py \ -f test_files.txt \ -o mel_spectrograms/ \ -c config.json ``` -------------------------------- ### Generate Audio with Pre-existing Model Source: https://github.com/nvidia/waveglow/blob/master/README.md Generate audio samples from mel-spectrograms using a published WaveGlow model. Ensure you have downloaded the model and mel-spectrograms beforehand. ```bash python3 inference.py -f <(ls mel_spectrograms/*.pt) -w waveglow_256channels.pt -o . --is_fp16 -s 0.6 ``` -------------------------------- ### WaveGlow Configuration File (`config.json`) Source: https://context7.com/nvidia/waveglow/llms.txt This JSON file controls the entire WaveGlow system, including training parameters, data preprocessing settings, distributed training configuration, and model architecture details. ```json { "train_config": { "fp16_run": true, "output_directory": "checkpoints", "epochs": 100000, "learning_rate": 1e-4, "sigma": 1.0, "iters_per_checkpoint": 2000, "batch_size": 12, "seed": 1234, "checkpoint_path": "", "with_tensorboard": false }, "data_config": { "training_files": "train_files.txt", "segment_length": 16000, "sampling_rate": 22050, "filter_length": 1024, "hop_length": 256, "win_length": 1024, "mel_fmin": 0.0, "mel_fmax": 8000.0 }, "dist_config": { "dist_backend": "nccl", "dist_url": "tcp://localhost:54321" }, "waveglow_config": { "n_mel_channels": 80, "n_flows": 12, "n_group": 8, "n_early_every": 4, "n_early_size": 2, "WN_config": { "n_layers": 8, "n_channels": 256, "kernel_size": 3 } } } ``` -------------------------------- ### Train WaveGlow Network Source: https://github.com/nvidia/waveglow/blob/master/README.md Initiate the training process for WaveGlow networks using a configuration file. For multi-GPU training, use 'distributed.py'. Mixed precision training can be enabled by setting 'fp16_run' to true in the config file. ```bash mkdir checkpoints python train.py -c config.json ``` -------------------------------- ### Post-Process Audio with Denoiser Source: https://context7.com/nvidia/waveglow/llms.txt Applies a denoiser to remove spectral bias introduced by WaveGlow, reducing background hiss. Load the WaveGlow model and then initialize the Denoiser with it. The `strength` parameter controls the aggressiveness of the bias removal. ```python import torch from glow import WaveGlow from denoiser import Denoiser from scipy.io.wavfile import write # Load model checkpoint = torch.load('waveglow_256channels.pt') waveglow = WaveGlow.remove_weightnorm(checkpoint['model']) waveglow.cuda().eval() # Build denoiser from the same model (uses zeros mode by default) denoiser = Denoiser(waveglow, filter_length=1024, n_overlap=4, win_length=1024, mode='zeros').cuda() mel = torch.load('mel_spectrograms/LJ001-0001.wav.pt').cuda().unsqueeze(0) with torch.no_grad(): audio = waveglow.infer(mel, sigma=0.6) # raw synthesis audio_denoised = denoiser(audio, strength=0.1) # remove bias audio_denoised = (audio_denoised * 32768.0).squeeze().cpu().numpy().astype('int16') write('output_denoised.wav', 22050, audio_denoised) ``` -------------------------------- ### WaveGlow Inference: Load Checkpoint and Synthesize Audio Source: https://context7.com/nvidia/waveglow/llms.txt Loads a pre-trained WaveGlow model from a checkpoint file and prepares it for inference. The `remove_weightnorm` function should be called to fuse weight normalization layers for faster inference. ```python import torch from glow import WaveGlow # Load a pre-trained checkpoint checkpoint = torch.load('waveglow_256channels.pt', map_location='cpu') waveglow = checkpoint['model'] waveglow = WaveGlow.remove_weightnorm(waveglow) waveglow.cuda().eval() ``` -------------------------------- ### Launch Multi-GPU Training with distributed.py Source: https://context7.com/nvidia/waveglow/llms.txt Use distributed.py to launch data-parallel multi-GPU training. It automatically detects CUDA devices and spawns subprocesses for each GPU, synchronizing gradients using NCCL. ```bash python distributed.py \ -c config.json \ -s stdout_logs/ ``` -------------------------------- ### WaveGlow Training Loss Calculation Source: https://context7.com/nvidia/waveglow/llms.txt Demonstrates how to use WaveGlowLoss for training WaveGlow. This loss computes the negative log-likelihood of the audio. Ensure the model and criterion are on CUDA and gradients are zeroed before backward pass. ```python import torch from glow import WaveGlow, WaveGlowLoss model = WaveGlow(**waveglow_config).cuda() criterion = WaveGlowLoss(sigma=1.0) optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) mel = torch.randn(4, 80, 62).cuda() audio = torch.randn(4, 16000).cuda() model.zero_grad() outputs = model((mel, audio)) # returns (z, log_s_list, log_det_W_list) loss = criterion(outputs) # scalar negative log-likelihood loss.backward() optimizer.step() print(f"Loss: {loss.item():.6f}") ``` -------------------------------- ### Batch Audio Synthesis (FP32) Source: https://context7.com/nvidia/waveglow/llms.txt Generate audio files from pre-computed mel-spectrograms using FP32 precision. The output files are saved to the specified output directory. ```bash python3 inference.py \ -f <(ls mel_spectrograms/*.pt) \ -w waveglow_256channels.pt \ -o synthesized/ \ --sigma 0.6 ``` -------------------------------- ### Convert Legacy WaveGlow Checkpoint Source: https://context7.com/nvidia/waveglow/llms.txt Use convert_model.py to migrate old WaveGlow checkpoints to the current fused architecture. This script handles the fusion of residual/skip connections. ```bash python convert_model.py old_waveglow_checkpoint.pt new_waveglow_checkpoint.pt ``` -------------------------------- ### Perform Inference with Trained WaveGlow Network Source: https://github.com/nvidia/waveglow/blob/master/README.md Generate audio using your custom-trained WaveGlow network. This command assumes you have trained a model and generated mel-spectrograms for inference. ```bash ls *.pt > mel_files.txt python3 inference.py -f mel_files.txt -w checkpoints/waveglow_10000 -o . --is_fp16 -s 0.6 ``` -------------------------------- ### Mel2Samp Dataset and Mel-Spectrogram Extraction Source: https://context7.com/nvidia/waveglow/llms.txt Shows how to use Mel2Samp for creating a dataset of audio segments and their corresponding mel-spectrograms. It can be used for training or standalone to precompute mel-spectrograms for inference. ```python from mel2samp import Mel2Samp, load_wav_to_torch from torch.utils.data import DataLoader # Dataset for training dataset = Mel2Samp( training_files="train_files.txt", # text file with one WAV path per line segment_length=16000, filter_length=1024, hop_length=256, win_length=1024, sampling_rate=22050, mel_fmin=0.0, mel_fmax=8000.0 ) loader = DataLoader(dataset, batch_size=8, shuffle=True, num_workers=2, drop_last=True) mel, audio = next(iter(loader)) # mel: torch.Size([8, 80, 63]) # audio: torch.Size([8, 16000]) — normalized to [-1, 1] # Standalone: compute and save mel-spectrogram for a single WAV audio_tensor, sr = load_wav_to_torch('data/LJ001-0001.wav') mel_spec = dataset.get_mel(audio_tensor) # mel_spec shape: (80, T) — ready to save with torch.save() import torch torch.save(mel_spec, 'LJ001-0001.wav.pt') ``` -------------------------------- ### Programmatic Checkpoint Conversion Source: https://context7.com/nvidia/waveglow/llms.txt This snippet demonstrates how to programmatically update a WaveGlow model checkpoint using the `update_model` function from `convert_model.py`. ```python import torch from convert_model import update_model checkpoint = torch.load('old_checkpoint.pt', map_location='cpu') checkpoint['model'] = update_model(checkpoint['model']) torch.save(checkpoint, 'new_checkpoint.pt') print("Conversion complete.") ``` -------------------------------- ### Generate Mel-Spectrograms for Test Set Source: https://github.com/nvidia/waveglow/blob/master/README.md Convert audio files from the test set into mel-spectrograms using the specified configuration. This step is necessary before performing inference on your trained model. ```bash python mel2samp.py -f test_files.txt -o . -c config.json ``` -------------------------------- ### Infer Audio with WaveGlow Source: https://context7.com/nvidia/waveglow/llms.txt Loads a pre-computed mel-spectrogram and uses WaveGlow to synthesize audio. Ensure the mel-spectrogram is loaded and moved to CUDA, then unsqueeze to add a batch dimension. The output audio is scaled to int16 range. ```python import torch mel = torch.load('mel_spectrograms/LJ001-0001.wav.pt') mel = mel.cuda().unsqueeze(0) # add batch dimension → (1, 80, T) with torch.no_grad(): audio = waveglow.infer(mel, sigma=0.6) # sigma=0.6 recommended for quality # Scale to int16 range audio = audio * 32768.0 audio = audio.squeeze().cpu().numpy().astype('int16') from scipy.io.wavfile import write write('output.wav', 22050, audio) ``` -------------------------------- ### Optimize WaveGlow Model for Inference Source: https://context7.com/nvidia/waveglow/llms.txt The `remove_weightnorm` static method strips weight normalization layers, fusing them into the model weights for faster inference. This operation is irreversible and should only be called after training. ```python import torch from glow import WaveGlow checkpoint = torch.load('waveglow_256channels.pt', map_location='cpu') model = checkpoint['model'] # Remove weight norm for inference (irreversible — do NOT call during training) model = WaveGlow.remove_weightnorm(model) model.cuda().eval() # Verify: WN layers no longer have weight_g / weight_v parameters for name, param in model.WN[0].named_parameters(): print(name, param.shape) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.