|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding=utf8 -*- |
| 3 | + |
| 4 | +import logging |
| 5 | +import os |
| 6 | +import platform |
| 7 | +import re |
| 8 | +from collections import OrderedDict |
| 9 | +from dataclasses import dataclass |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import torch |
| 13 | +import yaml |
| 14 | +from pytorch_lightning import Trainer |
| 15 | +from pytorch_lightning import seed_everything |
| 16 | +from pytorch_lightning.callbacks import ModelCheckpoint |
| 17 | +from pytorch_lightning.loggers import TensorBoardLogger |
| 18 | +from pytorch_lightning.strategies import DDPStrategy |
| 19 | + |
| 20 | +from src.easevoice.soundstorm.auto_reg.data.data_module import Text2SemanticDataModule |
| 21 | +from src.easevoice.soundstorm.auto_reg.models.t2s_lightning_module import Text2SemanticLightningModule |
| 22 | +from src.utils.config import gpt_config_path, train_output, cfg, gpt_pretrained_model_path, semantic_output, text_output_name, train_gpt_logs_output |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class GPTTrainParams: |
| 27 | + batch_size: int = 12 |
| 28 | + total_epochs: int = 15 |
| 29 | + save_every_epoch: int = 5 |
| 30 | + if_dpo: bool = False |
| 31 | + if_save_latest: bool = True |
| 32 | + if_save_every_weights: bool = True |
| 33 | + gpu_ids: str = "0" |
| 34 | + model_path: str = gpt_pretrained_model_path |
| 35 | + processing_path: str = "" |
| 36 | + normalize_path: str = "" |
| 37 | + output_model_name: str = "gpt" |
| 38 | + |
| 39 | + |
| 40 | +class GPTCheckpoint(ModelCheckpoint): |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + config, |
| 44 | + if_save_latest, |
| 45 | + if_save_every_weights, |
| 46 | + half_weights_save_dir, |
| 47 | + output_name, |
| 48 | + **kwargs |
| 49 | + ): |
| 50 | + super().__init__(**kwargs) |
| 51 | + self.if_save_latest = if_save_latest |
| 52 | + self.if_save_every_weights = if_save_every_weights |
| 53 | + self.half_weights_save_dir = half_weights_save_dir |
| 54 | + self.output_name = output_name |
| 55 | + self.config = config |
| 56 | + |
| 57 | + def on_train_epoch_end(self, trainer, pl_module): |
| 58 | + if self._should_save_on_train_epoch_end(trainer): |
| 59 | + monitor_candidates = self._monitor_candidates(trainer) |
| 60 | + if ( |
| 61 | + self._every_n_epochs >= 1 |
| 62 | + and (trainer.current_epoch + 1) % self._every_n_epochs == 0 |
| 63 | + ): |
| 64 | + to_clean = [] |
| 65 | + if ( |
| 66 | + self.if_save_latest == True |
| 67 | + ): |
| 68 | + to_clean = list(os.listdir(self.dirpath)) |
| 69 | + self._save_topk_checkpoint(trainer, monitor_candidates) |
| 70 | + if self.if_save_latest: |
| 71 | + for name in to_clean: |
| 72 | + try: |
| 73 | + os.remove("%s/%s" % (self.dirpath, name)) |
| 74 | + except: |
| 75 | + pass |
| 76 | + if self.if_save_every_weights: |
| 77 | + to_save_od = OrderedDict() |
| 78 | + to_save_od["weight"] = OrderedDict() |
| 79 | + state_dict = trainer.strategy._lightning_module.state_dict() |
| 80 | + for key in state_dict: |
| 81 | + to_save_od["weight"][key] = state_dict[key].half() |
| 82 | + to_save_od["config"] = self.config |
| 83 | + to_save_od["info"] = "GPT-e%s" % (trainer.current_epoch + 1) |
| 84 | + if os.environ.get("LOCAL_RANK", "0") == "0": |
| 85 | + new_path = os.path.join( |
| 86 | + self.half_weights_save_dir, |
| 87 | + "%s-e%s.ckpt" % (self.output_name, trainer.current_epoch + 1), |
| 88 | + ) |
| 89 | + torch.save(to_save_od, new_path) |
| 90 | + self._save_last_checkpoint(trainer, monitor_candidates) |
| 91 | + |
| 92 | + |
| 93 | +class GPTTrain(object): |
| 94 | + def __init__(self, params: GPTTrainParams): |
| 95 | + logging.getLogger("numba").setLevel(logging.WARNING) |
| 96 | + logging.getLogger("matplotlib").setLevel(logging.WARNING) |
| 97 | + torch.set_float32_matmul_precision("high") |
| 98 | + with open(gpt_config_path, "r") as f: |
| 99 | + data = f.read() |
| 100 | + self.config = yaml.load(data, Loader=yaml.FullLoader) |
| 101 | + self.processing_path = params.processing_path |
| 102 | + self.normalize_path = params.normalize_path |
| 103 | + self.train_output = os.path.join(params.normalize_path, train_output) |
| 104 | + self.train_logs_output = os.path.join(params.normalize_path, train_gpt_logs_output) |
| 105 | + self.train_ckpts_output = os.path.join(self.train_logs_output, "ckpt") |
| 106 | + os.makedirs(self.train_output, exist_ok=True) |
| 107 | + os.makedirs(self.train_logs_output, exist_ok=True) |
| 108 | + os.makedirs(self.train_ckpts_output, exist_ok=True) |
| 109 | + self.cfg = cfg |
| 110 | + if not self.cfg.is_half: |
| 111 | + self.config["train"]["precision"] = "32" |
| 112 | + params.batch_size = max(1, params.batch_size // 2) |
| 113 | + self.config["train"]["batch_size"] = params.batch_size |
| 114 | + self.config["train"]["epochs"] = params.total_epochs |
| 115 | + self.config["train"]["save_every_n_epoch"] = params.save_every_epoch |
| 116 | + self.config["train"]["if_dpo"] = params.if_dpo |
| 117 | + self.config["train"]["if_save_latest"] = params.if_save_latest |
| 118 | + self.config["train"]["if_save_every_weights"] = params.if_save_every_weights |
| 119 | + self.config["pretrained_s1"] = params.model_path |
| 120 | + self.config["train"]["half_weights_save_dir"] = self.train_output |
| 121 | + self.config["train_semantic_path"] = os.path.join(params.normalize_path, semantic_output) |
| 122 | + self.config["train_phoneme_path"] = os.path.join(params.normalize_path, text_output_name) |
| 123 | + self.config["logs_output_dir"] = self.train_logs_output |
| 124 | + self.config["train"]["output_name"] = params.output_model_name |
| 125 | + os.environ["hz"] = "25hz" |
| 126 | + seed_everything(self.config["train"]["seed"], workers=True) |
| 127 | + ckpt_callback: ModelCheckpoint = GPTCheckpoint( |
| 128 | + config=self.config, |
| 129 | + if_save_latest=self.config["train"]["if_save_latest"], |
| 130 | + if_save_every_weights=self.config["train"]["if_save_every_weights"], |
| 131 | + half_weights_save_dir=self.config["train"]["half_weights_save_dir"], |
| 132 | + output_name=self.config["train"]["output_name"], |
| 133 | + save_top_k=-1, |
| 134 | + monitor="top_3_acc", |
| 135 | + mode="max", |
| 136 | + save_on_train_epoch_end=True, |
| 137 | + every_n_epochs=self.config["train"]["save_every_n_epoch"], |
| 138 | + dirpath=self.train_ckpts_output, |
| 139 | + ) |
| 140 | + logger = TensorBoardLogger(name="log", save_dir=self.train_logs_output) |
| 141 | + os.environ["MASTER_ADDR"] = "localhost" |
| 142 | + self.trainer: Trainer = Trainer( |
| 143 | + max_epochs=self.config["train"]["epochs"], |
| 144 | + accelerator=self.cfg.device, |
| 145 | + limit_val_batches=0, |
| 146 | + devices=-1 if torch.cuda.is_available() else 1, |
| 147 | + benchmark=False, |
| 148 | + fast_dev_run=False, |
| 149 | + strategy=DDPStrategy( |
| 150 | + process_group_backend="nccl" if platform.system() != "Windows" else "gloo" |
| 151 | + ) if torch.cuda.is_available() else "auto", |
| 152 | + precision=self.config["train"]["precision"], |
| 153 | + logger=logger, |
| 154 | + num_sanity_val_steps=0, |
| 155 | + callbacks=[ckpt_callback], |
| 156 | + use_distributed_sampler=False, |
| 157 | + ) |
| 158 | + self.model: Text2SemanticLightningModule = Text2SemanticLightningModule( |
| 159 | + self.config, Path(self.train_logs_output) |
| 160 | + ) |
| 161 | + |
| 162 | + self.data_module: Text2SemanticDataModule = Text2SemanticDataModule( |
| 163 | + self.config, |
| 164 | + train_semantic_path=self.config["train_semantic_path"], |
| 165 | + train_phoneme_path=self.config["train_phoneme_path"], |
| 166 | + ) |
| 167 | + trainer_ckpt_path = self._get_newest_ckpt(os.listdir(self.train_ckpts_output)) |
| 168 | + self.trainer_ckpt_path = os.path.join(self.train_ckpts_output, trainer_ckpt_path) if trainer_ckpt_path else None |
| 169 | + |
| 170 | + def train(self): |
| 171 | + self.trainer.fit(self.model, self.data_module, ckpt_path=self.trainer_ckpt_path) |
| 172 | + |
| 173 | + @staticmethod |
| 174 | + def _get_newest_ckpt(file_list: []): |
| 175 | + if file_list is None or len(file_list) == 0: |
| 176 | + return None |
| 177 | + |
| 178 | + pattern = r'epoch=(\d+)-step=(\d+)\.ckpt' |
| 179 | + extracted_info = [] |
| 180 | + for string in file_list: |
| 181 | + match = re.match(pattern, string) |
| 182 | + if match: |
| 183 | + epoch = int(match.group(1)) |
| 184 | + step = int(match.group(2)) |
| 185 | + extracted_info.append((epoch, step, string)) |
| 186 | + sorted_info = sorted( |
| 187 | + extracted_info, key=lambda x: (x[0], x[1]), reverse=True) |
| 188 | + newest_ckpt = sorted_info[0][2] |
| 189 | + return newest_ckpt |
0 commit comments