import time import argparse import json import logging import math import os import yaml from pathlib import Path import diffusers import datasets import numpy as np import pandas as pd import wandb import transformers import torch from accelerate import Accelerator from accelerate.logging import get_logger from accelerate.utils import set_seed from datasets import load_dataset from torch.utils.data import Dataset, DataLoader from tqdm.auto import tqdm from transformers import SchedulerType, get_scheduler from datasets import load_dataset, Audio from tangoflux.utils import Text2AudioDataset, read_wav_file, pad_wav from module.data_utils import ( TextAudioSpeakerLoader, TextAudioSpeakerCollate, ) from module.models import SynthesizerTrn from module.models_s1 import NARPredictor from diffusers import AutoencoderOobleck import torchaudio import utils as vits_utils logger = get_logger(__name__) def parse_args(): parser = argparse.ArgumentParser( description="Rectified flow for text to audio generation task." ) parser.add_argument( "--num_examples", type=int, default=-1, help="How many examples to use for training and validation.", ) parser.add_argument( "--text_column", type=str, default="captions", help="The name of the column in the datasets containing the input texts.", ) parser.add_argument( "--audio_column", type=str, default="location", help="The name of the column in the datasets containing the audio paths.", ) parser.add_argument( "--adam_beta1", type=float, default=0.9, help="The beta1 parameter for the Adam optimizer.", ) parser.add_argument( "--adam_beta2", type=float, default=0.95, help="The beta2 parameter for the Adam optimizer.", ) parser.add_argument( "--config", type=str, default="tangoflux_config.yaml", help="Config file defining the model size as well as other hyper parameter.", ) parser.add_argument( "--prefix", type=str, default="", help="Add prefix in text prompts.", ) parser.add_argument( "--learning_rate", type=float, default=3e-5, help="Initial learning rate (after the potential warmup period) to use.", ) parser.add_argument( "--weight_decay", type=float, default=1e-8, help="Weight decay to use." ) parser.add_argument( "--max_train_steps", type=int, default=None, help="Total number of training steps to perform. If provided, overrides num_train_epochs.", ) parser.add_argument( "--lr_scheduler_type", type=SchedulerType, default="linear", help="The scheduler type to use.", choices=[ "linear", "cosine", "cosine_with_restarts", "polynomial", "constant", "constant_with_warmup", ], ) parser.add_argument( "--num_warmup_steps", type=int, default=0, help="Number of steps for the warmup in the lr scheduler.", ) parser.add_argument( "--adam_epsilon", type=float, default=1e-08, help="Epsilon value for the Adam optimizer", ) parser.add_argument( "--adam_weight_decay", type=float, default=1e-2, help="Epsilon value for the Adam optimizer", ) parser.add_argument( "--seed", type=int, default=None, help="A seed for reproducible training." ) parser.add_argument( "--checkpointing_steps", type=str, default="best", help="Whether the various states should be saved at the end of every 'epoch' or 'best' whenever validation loss decreases.", ) parser.add_argument( "--save_every", type=int, default=5, help="Save model after every how many epochs when checkpointing_steps is set to best.", ) parser.add_argument( "--resume_from_checkpoint", type=str, default=None, help="If the training should continue from a local checkpoint folder.", ) parser.add_argument( "--load_from_checkpoint", type=str, default=None, help="Whether to continue training from a model weight", ) args = parser.parse_args() return args def evaluate(model, vae, dataloader, accelerator,dir): model.eval() unwrapped_vae = accelerator.unwrap_model(vae) unwrapped_model = accelerator.unwrap_model(model) with torch.no_grad(): for step, batch in enumerate(dataloader): with torch.no_grad(): device = model.device batch = [b.to(device) for b in batch] ssl, spec, spec_lengths, y, y_lengths, text, text_lengths, bert,spk_emb_padded,attn_priors = batch latent_gt, x_mask = unwrapped_vae.encode( text, text_lengths,spec, spec_lengths,ssl, attn_priors ) latent_pred = unwrapped_model.infer(text, bert, spk_emb_padded) rec = unwrapped_vae.decode(latent_gt, text, text_lengths, spec, spec_lengths) pred = unwrapped_vae.decode(latent_pred, text, text_lengths, spec, spec_lengths) out_path_rec = os.path.join(dir, f"rec_{step}.wav") out_path_pred = os.path.join(dir, f"pred_{step}.wav") import soundfile as sf sf.write(out_path_rec, rec[0,0].cpu().numpy(), 32000) sf.write(out_path_pred, pred[0,0].cpu().numpy(), 32000) model.train() def main(): args = parse_args() accelerator_log_kwargs = {} def load_config(config_path): with open(config_path, "r") as file: return yaml.safe_load(file) config = load_config(args.config) learning_rate = float(config["training"]["learning_rate"]) num_train_epochs = int(config["training"]["num_train_epochs"]) num_warmup_steps = int(config["training"]["num_warmup_steps"]) per_device_batch_size = int(config["training"]["per_device_batch_size"]) gradient_accumulation_steps = int(config["training"]["gradient_accumulation_steps"]) output_dir = config["paths"]["output_dir"] accelerator = Accelerator( gradient_accumulation_steps=gradient_accumulation_steps, **accelerator_log_kwargs, ) # Make one log on every process with the configuration for debugging. logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) logger.info(accelerator.state, main_process_only=False) datasets.utils.logging.set_verbosity_error() diffusers.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed) # Handle output directory creation and wandb tracking if accelerator.is_main_process: if output_dir is None or output_dir == "": output_dir = "saved/" + str(int(time.time())) if not os.path.exists("saved"): os.makedirs("saved") os.makedirs(output_dir, exist_ok=True) elif output_dir is not None: os.makedirs(output_dir, exist_ok=True) os.makedirs("{}/{}".format(output_dir, "outputs"), exist_ok=True) with open("{}/summary.jsonl".format(output_dir), "a") as f: f.write(json.dumps(dict(vars(args))) + "\n\n") accelerator.project_configuration.automatic_checkpoint_naming = False wandb.init( project="tts s1 Flow matching", settings=wandb.Settings(_disable_stats=True), ) accelerator.wait_for_everyone() # model = TangoFlux(config=config["model"]) model = NARPredictor() config_path = 'configs/vits.json' hps = vits_utils.get_hparams_from_file(config_path) vae = SynthesizerTrn( hps.data.filter_length // 2 + 1, hps.train.segment_size // hps.data.hop_length, n_speakers=hps.data.n_speakers, **hps.model) model_path = vits_utils.latest_checkpoint_path(hps.s2_ckpt_dir, "G_*.pth") vits_utils.load_checkpoint(model_path, vae, None, False) ## Freeze vae for param in vae.parameters(): param.requires_grad = False vae.eval() collate_fn = TextAudioSpeakerCollate() with accelerator.main_process_first(): train_dataset = TextAudioSpeakerLoader(hps.data.training_files, hps.data) eval_dataset = TextAudioSpeakerLoader(hps.data.validation_files, hps.data, val=True) accelerator.print( "Num instances in train: {}, validation: {}".format( len(train_dataset), len(eval_dataset), ) ) train_dataloader = DataLoader( train_dataset, shuffle=True, batch_size=config["training"]["per_device_batch_size"], collate_fn=collate_fn, num_workers=6, ) eval_dataloader = DataLoader( eval_dataset, shuffle=True, batch_size=1, collate_fn=collate_fn, ) # Optimizer optimizer_parameters = list(model.parameters()) num_trainable_parameters = sum( p.numel() for p in model.parameters() if p.requires_grad ) accelerator.print("Num trainable parameters: {}".format(num_trainable_parameters)) if args.load_from_checkpoint: from safetensors.torch import load_file w1 = load_file(args.load_from_checkpoint) model.load_state_dict(w1, strict=False) logger.info("Weights loaded from{}".format(args.load_from_checkpoint)) optimizer = torch.optim.AdamW( optimizer_parameters, lr=learning_rate, betas=(args.adam_beta1, args.adam_beta2), weight_decay=args.adam_weight_decay, eps=args.adam_epsilon, ) # Scheduler and math around the number of training steps. overrode_max_train_steps = False num_update_steps_per_epoch = math.ceil( len(train_dataloader) / gradient_accumulation_steps ) if args.max_train_steps is None: args.max_train_steps = num_train_epochs * num_update_steps_per_epoch overrode_max_train_steps = True lr_scheduler = get_scheduler( name=args.lr_scheduler_type, optimizer=optimizer, num_warmup_steps=num_warmup_steps * gradient_accumulation_steps * accelerator.num_processes, num_training_steps=args.max_train_steps * gradient_accumulation_steps, ) # Prepare everything with our `accelerator`. vae, model, optimizer, lr_scheduler = accelerator.prepare( vae, model, optimizer, lr_scheduler ) train_dataloader, eval_dataloader = accelerator.prepare( train_dataloader, eval_dataloader ) # We need to recalculate our total training steps as the size of the training dataloader may have changed. num_update_steps_per_epoch = math.ceil( len(train_dataloader) / gradient_accumulation_steps ) if overrode_max_train_steps: args.max_train_steps = num_train_epochs * num_update_steps_per_epoch # Afterwards we recalculate our number of training epochs num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch) # Figure out how many steps we should save the Accelerator states checkpointing_steps = args.checkpointing_steps if checkpointing_steps is not None and checkpointing_steps.isdigit(): checkpointing_steps = int(checkpointing_steps) # We need to initialize the trackers we use, and also store our configuration. # The trackers initializes automatically on the main process. # Train! total_batch_size = ( per_device_batch_size * accelerator.num_processes * gradient_accumulation_steps ) logger.info("***** Running training *****") logger.info(f" Num examples = {len(train_dataset)}") logger.info(f" Num Epochs = {num_train_epochs}") logger.info(f" Instantaneous batch size per device = {per_device_batch_size}") logger.info( f" Total train batch size (w. parallel, distributed & accumulation) = {total_batch_size}" ) logger.info(f" Gradient Accumulation steps = {gradient_accumulation_steps}") logger.info(f" Total optimization steps = {args.max_train_steps}") # Only show the progress bar once on each machine. progress_bar = tqdm( range(args.max_train_steps), disable=not accelerator.is_local_main_process ) completed_steps = 0 starting_epoch = 0 # Potentially load in the weights and states from a previous save resume_from_checkpoint = config["paths"]["resume_from_checkpoint"] if resume_from_checkpoint != "": accelerator.load_state(resume_from_checkpoint) accelerator.print(f"Resumed from local checkpoint: {resume_from_checkpoint}") # Duration of the audio clips in seconds best_loss = np.inf for epoch in range(starting_epoch, num_train_epochs): model.train() total_loss, total_val_loss = 0, 0 for step, batch in enumerate(train_dataloader): with accelerator.accumulate(model): optimizer.zero_grad() device = model.device batch = [b.to(device) for b in batch] ssl, spec, spec_lengths, y, y_lengths, text, text_lengths, bert,spk_emb_padded,attn_priors = batch with torch.no_grad(): unwrapped_vae = accelerator.unwrap_model(vae) audio_latent, x_mask = unwrapped_vae.encode( text, text_lengths,spec, spec_lengths,ssl, attn_priors ) loss= model(text, x_mask, bert, spk_emb_padded, audio_latent) total_loss += loss.detach().float() accelerator.backward(loss) if accelerator.sync_gradients: progress_bar.update(1) completed_steps += 1 optimizer.step() lr_scheduler.step() if completed_steps % 10 == 0 and accelerator.is_main_process: total_norm = 0.0 for p in model.parameters(): if p.grad is not None: param_norm = p.grad.data.norm(2) total_norm += param_norm.item() ** 2 total_norm = total_norm**0.5 logger.info( f"Step {completed_steps}, Loss: {loss.item()}, Grad Norm: {total_norm}" ) lr = lr_scheduler.get_last_lr()[0] result = { "train_loss": loss.item(), "grad_norm": total_norm, "learning_rate": lr, } # result["val_loss"] = round(total_val_loss.item()/len(eval_dataloader), 4) wandb.log(result, step=completed_steps) # Checks if the accelerator has performed an optimization step behind the scenes if isinstance(checkpointing_steps, int): if completed_steps % checkpointing_steps == 0: output_dir = f"step_{completed_steps }" if output_dir is not None: output_dir = os.path.join(output_dir, output_dir) accelerator.save_state(output_dir) evaluate(model, vae, eval_dataloader, accelerator, output_dir) if completed_steps >= args.max_train_steps: break if accelerator.is_main_process: result = {} result["epoch"] = float(epoch + 1) result["epoch/train_loss"] = round( total_loss.item() / len(train_dataloader), 4 ) wandb.log(result, step=completed_steps) result_string = "Epoch: {}, Loss Train: {}\n".format( epoch, result["epoch/train_loss"] ) accelerator.print(result_string) with open("{}/summary.jsonl".format(output_dir), "a") as f: f.write(json.dumps(result) + "\n\n") logger.info(result) accelerator.wait_for_everyone() if __name__ == "__main__": main()