Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+54
- Docker/damo.sha256Added+3
- Docker/download.pyAdded+5
- Docker/download.shAdded+11
- Docker/links.sha256Added+12
- Docker/links.txtAdded+34
- GPT_SoVITS/AR/__init__.pyAdded
- GPT_SoVITS/AR/data/__init__.pyAdded
- GPT_SoVITS/AR/data/bucket_sampler.pyAdded+163
- GPT_SoVITS/AR/data/data_module.pyAdded+76
- GPT_SoVITS/AR/data/dataset.pyAdded+321
- GPT_SoVITS/AR/models/__init__.pyAdded
- GPT_SoVITS/AR/models/t2s_lightning_module.pyAdded+141
- GPT_SoVITS/AR/models/t2s_lightning_module_onnx.pyAdded+107
- GPT_SoVITS/AR/models/t2s_model.pyAdded+901
- GPT_SoVITS/AR/models/t2s_model_onnx.pyAdded+338
- GPT_SoVITS/AR/models/utils.pyAdded+229
- GPT_SoVITS/AR/modules/__init__.pyAdded
- GPT_SoVITS/AR/modules/activation.pyAdded+428
- GPT_SoVITS/AR/modules/activation_onnx.pyAdded+178
- GPT_SoVITS/AR/modules/embedding.pyAdded+81
- GPT_SoVITS/AR/modules/embedding_onnx.pyAdded+63
- GPT_SoVITS/AR/modules/lr_schedulers.pyAdded+83
- GPT_SoVITS/AR/modules/optim.pyAdded+622
- GPT_SoVITS/AR/modules/patched_mha_with_cache.pyAdded+465
- GPT_SoVITS/AR/modules/patched_mha_with_cache_onnx.pyAdded+92
- GPT_SoVITS/AR/modules/scaling.pyAdded+335
- GPT_SoVITS/AR/modules/transformer.pyAdded+378
- GPT_SoVITS/AR/modules/transformer_onnx.pyAdded+292
- GPT_SoVITS/AR/text_processing/__init__.pyAdded
- GPT_SoVITS/AR/text_processing/phonemizer.pyAdded+79
- GPT_SoVITS/AR/text_processing/symbols.pyAdded+10
- GPT_SoVITS/AR/utils/__init__.pyAdded+37
- GPT_SoVITS/AR/utils/initialize.pyAdded+38
- GPT_SoVITS/AR/utils/io.pyAdded+34
- GPT_SoVITS/TTS_infer_pack/TTS.pyAdded+1034
- GPT_SoVITS/TTS_infer_pack/TextPreprocessor.pyAdded+244
- GPT_SoVITS/TTS_infer_pack/__init__.pyAdded+1
- GPT_SoVITS/TTS_infer_pack/text_segmentation_method.pyAdded+173
- GPT_SoVITS/configs/.gitignoreAdded+1
- GPT_SoVITS/configs/s1.yamlAdded+31
- GPT_SoVITS/configs/s1big.yamlAdded+31
- GPT_SoVITS/configs/s1big2.yamlAdded+31
- GPT_SoVITS/configs/s1longer-v2.yamlAdded+31
- GPT_SoVITS/configs/s1longer.yamlAdded+31
- GPT_SoVITS/configs/s1mq.yamlAdded+77
- GPT_SoVITS/configs/s2.jsonAdded+98
- GPT_SoVITS/configs/train.yamlAdded+32
- GPT_SoVITS/configs/tts_infer.yamlAdded+24
- GPT_SoVITS/feature_extractor/__init__.pyAdded+6
707 unmodified lines708709710707 unmodified linesdata8/wavs/174.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/152.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/13.wav filter=lfs diff=lfs merge=lfs -text707 unmodified lines708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764707 unmodified linesdata8/wavs/174.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/152.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/13.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/115.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/117.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/205.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/104.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/199.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/171.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/217.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/146.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/226.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/107.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/141.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/195.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/106.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/103.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/137.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/227.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/110.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/192.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/126.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/109.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/149.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/25.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/112.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/182.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/15.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/108.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/12.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/116.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/179.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/132.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/114.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/176.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/139.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/18.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/180.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/187.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/136.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/11.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/147.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/144.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/125.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/105.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/111.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/150.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/145.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/123.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/113.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/101.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/100.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/102.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/10.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/1.wav filter=lfs diff=lfs merge=lfs -textdata8/wavs/0.wav filter=lfs diff=lfs merge=lfs -textGPT_SoVITS/text/ja_userdic/userdict.csv filter=lfs diff=lfs merge=lfs -text
1235bba782a5e9196166233b9ab12ba04cadff9ef9212b4ff6153ed9290ff679025 /workspace/tools/damo_asr/models/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/model.pbb3be75be477f0780277f3bae0fe489f48718f585f3a6e45d7dd1fbb1a4255fc5 /workspace/tools/damo_asr/models/speech_fsmn_vad_zh-cn-16k-common-pytorch/model.pba5818bb9d933805a916eebe41eb41648f7f9caad30b4bd59d56f3ca135421916 /workspace/tools/damo_asr/models/punc_ct-transformer_zh-cn-common-vocab272727-pytorch/model.pb12345# Download moda ASR related modelsfrom modelscope import snapshot_downloadmodel_dir = snapshot_download('damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch',revision="v2.0.4")model_dir = snapshot_download('damo/speech_fsmn_vad_zh-cn-16k-common-pytorch',revision="v2.0.4")model_dir = snapshot_download('damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch',revision="v2.0.4")1234567891011#!/usr/bin/env bash
set -Eeuo pipefail
echo "Downloading models..."
aria2c --disable-ipv6 --input-file /workspace/Docker/links.txt --dir /workspace --continue
echo "Checking SHA256..."
parallel --will-cite -a /workspace/Docker/links.sha256 "echo -n {} | sha256sum -c"123456789101112b1c1e17e9c99547a89388f72048cd6e1b41b5a18b170e86a46dfde0324d63eb1 /workspace/GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckptfc579c1db3c1e21b721001cf99d7a584214280df19b002e200b630a34fa06eb8 /workspace/GPT_SoVITS/pretrained_models/s2D488k.pth020a014e1e01e550e510f2f61fae5e5f5b6aab40f15c22f1f12f724df507e835 /workspace/GPT_SoVITS/pretrained_models/s2G488k.pth24164f129c66499d1346e2aa55f183250c223161ec2770c0da3d3b08cf432d3c /workspace/GPT_SoVITS/pretrained_models/chinese-hubert-base/pytorch_model.bine53a693acc59ace251d143d068096ae0d7b79e4b1b503fa84c9dcf576448c1d8 /workspace/GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large/pytorch_model.bin39796caa5db18d7f9382d8ac997ac967bfd85f7761014bb807d2543cc844ef05 /workspace/tools/uvr5/uvr5_weights/HP2_all_vocals.pth45e6b65199e781b4a6542002699be9f19cd3d1cb7d1558bc2bfbcd84674dfe28 /workspace/tools/uvr5/uvr5_weights/HP3_all_vocals.pth5908891829634926119720241e8573d97cbeb8277110a7512bdb0bd7563258ee /workspace/tools/uvr5/uvr5_weights/HP5_only_main_vocal.pth8c8fd1582f9aabc363e47af62ddb88df6cae7e064cae75bbf041a067a5e0aee2 /workspace/tools/uvr5/uvr5_weights/VR-DeEchoAggressive.pth01376dd2a571bf3cb9cced680732726d2d732609d09216a610b0d110f133febe /workspace/tools/uvr5/uvr5_weights/VR-DeEchoDeReverb.pth56aba59db3bcdd14a14464e62f3129698ecdea62eee0f003b9360923eb3ac79e /workspace/tools/uvr5/uvr5_weights/VR-DeEchoNormal.pth233bb5c6aaa365e568659a0a81211746fa881f8f47f82d9e864fce1f7692db80 /workspace/tools/uvr5/uvr5_weights/onnx_dereverb_By_FoxJoy/vocals.onnx12345678910111213141516171819202122232425262728293031323334# GPT-SoVITS modelshttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/s1bert25hz-2kh-longer-epoch%3D68e-step%3D50232.ckpt out=GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpthttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/s2D488k.pth out=GPT_SoVITS/pretrained_models/s2D488k.pthhttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/s2G488k.pth out=GPT_SoVITS/pretrained_models/s2G488k.pthhttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/chinese-hubert-base/config.json out=GPT_SoVITS/pretrained_models/chinese-hubert-base/config.jsonhttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/chinese-hubert-base/preprocessor_config.json out=GPT_SoVITS/pretrained_models/chinese-hubert-base/preprocessor_config.jsonhttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/chinese-hubert-base/pytorch_model.bin out=GPT_SoVITS/pretrained_models/chinese-hubert-base/pytorch_model.binhttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/chinese-roberta-wwm-ext-large/config.json out=GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large/config.jsonhttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/chinese-roberta-wwm-ext-large/pytorch_model.bin out=GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large/pytorch_model.binhttps://huggingface.co/lj1995/GPT-SoVITS/resolve/main/chinese-roberta-wwm-ext-large/tokenizer.json out=GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large/tokenizer.json# UVR5https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/uvr5_weights/HP2_all_vocals.pth out=tools/uvr5/uvr5_weights/HP2_all_vocals.pthhttps://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/uvr5_weights/HP3_all_vocals.pth out=tools/uvr5/uvr5_weights/HP3_all_vocals.pthhttps://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/uvr5_weights/HP5_only_main_vocal.pth out=tools/uvr5/uvr5_weights/HP5_only_main_vocal.pthhttps://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/uvr5_weights/VR-DeEchoAggressive.pth out=tools/uvr5/uvr5_weights/VR-DeEchoAggressive.pthhttps://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/uvr5_weights/VR-DeEchoDeReverb.pth out=tools/uvr5/uvr5_weights/VR-DeEchoDeReverb.pthhttps://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/uvr5_weights/VR-DeEchoNormal.pth out=tools/uvr5/uvr5_weights/VR-DeEchoNormal.pthhttps://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/uvr5_weights/onnx_dereverb_By_FoxJoy/vocals.onnx out=tools/uvr5/uvr5_weights/onnx_dereverb_By_FoxJoy/vocals.onnx123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/data/bucket_sampler.py# reference: https://github.com/lifeiteng/vall-eimport itertoolsimport mathimport randomfrom random import shufflefrom typing import Iteratorfrom typing import Optionalfrom typing import TypeVar
import torchimport torch.distributed as distfrom torch.utils.data import Datasetfrom torch.utils.data import Sampler
__all__ = [ "DistributedBucketSampler",]
T_co = TypeVar("T_co", covariant=True)
class DistributedBucketSampler(Sampler[T_co]): r""" sort the dataset wrt. input length divide samples into buckets sort within buckets divide buckets into batches sort batches """
def __init__( self, dataset: Dataset, num_replicas: Optional[int] = None, rank: Optional[int] = None, shuffle: bool = True, seed: int = 0, drop_last: bool = False, batch_size: int = 32, ) -> None: if num_replicas is None: if not dist.is_available(): raise RuntimeError("Requires distributed package to be available") num_replicas = dist.get_world_size() if torch.cuda.is_available() else 1 if rank is None: if not dist.is_available(): raise RuntimeError("Requires distributed package to be available") rank = dist.get_rank() if torch.cuda.is_available() else 0 if torch.cuda.is_available(): torch.cuda.set_device(rank) if rank >= num_replicas or rank < 0: raise ValueError( "Invalid rank {}, rank should be in the interval" " [0, {}]".format(rank, num_replicas - 1) ) self.dataset = dataset self.num_replicas = num_replicas self.rank = rank self.epoch = 0 self.drop_last = drop_last # If the dataset length is evenly divisible by # of replicas, then there # is no need to drop any data, since the dataset will be split equally. if ( self.drop_last and len(self.dataset) % self.num_replicas != 0 ): # type: ignore[arg-type] # Split to nearest available length that is evenly divisible. # This is to ensure each rank receives the same amount of data when # using this Sampler. self.num_samples = math.ceil( (len(self.dataset) - self.num_replicas) / self.num_replicas # type: ignore[arg-type] ) else: self.num_samples = math.ceil( len(self.dataset) / self.num_replicas ) # type: ignore[arg-type] self.total_size = self.num_samples * self.num_replicas self.shuffle = shuffle self.seed = seed self.batch_size = batch_size self.id_with_length = self._get_sample_lengths() self.id_buckets = self.make_buckets(bucket_width=2.0)
def _get_sample_lengths(self): id_with_lengths = [] for i in range(len(self.dataset)): id_with_lengths.append((i, self.dataset.get_sample_length(i))) id_with_lengths.sort(key=lambda x: x[1]) return id_with_lengths
def make_buckets(self, bucket_width: float = 2.0): buckets = [] cur = [] max_sec = bucket_width for id, sec in self.id_with_length: if sec < max_sec: cur.append(id) else: buckets.append(cur) cur = [id] max_sec += bucket_width if len(cur) > 0: buckets.append(cur) return buckets
def __iter__(self) -> Iterator[T_co]: if self.shuffle: # deterministically shuffle based on epoch and seed g = torch.Generator() g.manual_seed(self.seed + self.epoch) random.seed(self.epoch + self.seed) shuffled_bucket = [] for buc in self.id_buckets: buc_copy = buc.copy() shuffle(buc_copy) shuffled_bucket.append(buc_copy) grouped_batch_size = self.batch_size * self.num_replicas shuffled_bucket = list(itertools.chain(*shuffled_bucket)) n_batch = int(math.ceil(len(shuffled_bucket) / grouped_batch_size)) batches = [ shuffled_bucket[b * grouped_batch_size : (b + 1) * grouped_batch_size] for b in range(n_batch) ] shuffle(batches) indices = list(itertools.chain(*batches)) else: # type: ignore[arg-type] indices = list(range(len(self.dataset)))
if not self.drop_last: # add extra samples to make it evenly divisible padding_size = self.total_size - len(indices) if padding_size <= len(indices): indices += indices[:padding_size] else: indices += (indices * math.ceil(padding_size / len(indices)))[ :padding_size ] else: # remove tail of data to make it evenly divisible. indices = indices[: self.total_size] assert len(indices) == self.total_size
# subsample indices = indices[self.rank : self.total_size : self.num_replicas] assert len(indices) == self.num_samples
return iter(indices)
def __len__(self) -> int: return self.num_samples
def set_epoch(self, epoch: int) -> None: r""" Sets the epoch for this sampler. When :attr:`shuffle=True`, this ensures all replicas use a different random ordering for each epoch. Otherwise, the next iteration of this sampler will yield the same ordering.
Args: epoch (int): Epoch number. """ self.epoch = epoch12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/data/data_module.py# reference: https://github.com/lifeiteng/vall-efrom pytorch_lightning import LightningDataModulefrom AR.data.bucket_sampler import DistributedBucketSamplerfrom AR.data.dataset import Text2SemanticDatasetfrom torch.utils.data import DataLoader
class Text2SemanticDataModule(LightningDataModule): def __init__( self, config, train_semantic_path, train_phoneme_path, dev_semantic_path=None, dev_phoneme_path=None, ): super().__init__() self.config = config self.train_semantic_path = train_semantic_path self.train_phoneme_path = train_phoneme_path self.dev_semantic_path = dev_semantic_path self.dev_phoneme_path = dev_phoneme_path self.num_workers = self.config["data"]["num_workers"]
def prepare_data(self): pass
def setup(self, stage=None, output_logs=False): self._train_dataset = Text2SemanticDataset( phoneme_path=self.train_phoneme_path, semantic_path=self.train_semantic_path, max_sec=self.config["data"]["max_sec"], pad_val=self.config["data"]["pad_val"], ) self._dev_dataset = self._train_dataset # self._dev_dataset = Text2SemanticDataset( # phoneme_path=self.dev_phoneme_path, # semantic_path=self.dev_semantic_path, # max_sample=self.config['data']['max_eval_sample'], # max_sec=self.config['data']['max_sec'], # pad_val=self.config['data']['pad_val'])
def train_dataloader(self): batch_size=self.config["train"]["batch_size"]//2 if self.config["train"].get("if_dpo",False)==True else self.config["train"]["batch_size"] batch_size = max(min(batch_size,len(self._train_dataset)//4),1)#防止不保存 sampler = DistributedBucketSampler(self._train_dataset, batch_size=batch_size) return DataLoader( self._train_dataset, batch_size=batch_size, sampler=sampler, collate_fn=self._train_dataset.collate, num_workers=self.num_workers, persistent_workers=True, prefetch_factor=16, )
def val_dataloader(self): return DataLoader( self._dev_dataset, batch_size=1, shuffle=False, collate_fn=self._train_dataset.collate, num_workers=max(self.num_workers, 12), persistent_workers=True, prefetch_factor=16, )
# 这个会使用到嘛? def test_dataloader(self): return DataLoader( self._dev_dataset, batch_size=1, shuffle=False, collate_fn=self._train_dataset.collate, )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/data/dataset.py# reference: https://github.com/lifeiteng/vall-eimport pdbimport sys
# sys.path.append("/data/docker/liujing04/gpt-vits/mq-vits-s1bert_no_bert")import traceback, osfrom typing import Dictfrom typing import List
import numpy as npimport pandas as pdimport torch, jsonfrom torch.utils.data import DataLoaderfrom torch.utils.data import Datasetfrom transformers import AutoTokenizer
version = os.environ.get('version',None)
from text import cleaned_text_to_sequencefrom text.text_processing import text_to_sequencefrom text.symbols import symbols
# from config import exp_dir
def batch_sequences(sequences: List[np.array], axis: int = 0, pad_value: int = 0): seq = sequences[0] ndim = seq.ndim if axis < 0: axis += ndim dtype = seq.dtype pad_value = dtype.type(pad_value) seq_lengths = [seq.shape[axis] for seq in sequences] max_length = np.max(seq_lengths)
padded_sequences = [] for seq, length in zip(sequences, seq_lengths): padding = ( [(0, 0)] * axis + [(0, max_length - length)] + [(0, 0)] * (ndim - axis - 1) ) padded_seq = np.pad(seq, padding, mode="constant", constant_values=pad_value) padded_sequences.append(padded_seq) batch = np.stack(padded_sequences) return batch
class Text2SemanticDataset(Dataset): """dataset class for text tokens to semantic model training."""
def __init__( self, phoneme_path: str, semantic_path: str, max_sample: int = None, max_sec: int = 100, pad_val: int = 1024, # min value of phoneme/sec min_ps_ratio: int = 3, # max value of phoneme/sec max_ps_ratio: int = 25, ) -> None: super().__init__()
self.semantic_data = pd.read_csv( semantic_path, delimiter="\t", encoding="utf-8" ) # get dict self.path2 = phoneme_path # "%s/2-name2text.txt"%exp_dir#phoneme_path self.path3 = "%s/3-bert" % ( os.path.dirname(phoneme_path) ) # "%s/3-bert"%exp_dir#bert_dir self.path6 = semantic_path # "%s/6-name2semantic.tsv"%exp_dir#semantic_path assert os.path.exists(self.path2) assert os.path.exists(self.path6) self.phoneme_data = {} with open(self.path2, "r", encoding="utf8") as f: lines = f.read().strip("\n").split("\n")
for line in lines: # Split by pipe instead of tab tmp = line.split("|") if len(tmp) == 2: # filename and phoneme sequence filename = tmp[0] phoneme = tmp[1] # Use phoneme sequence for both word2ph and text since they're not provided self.phoneme_data[filename] = [phoneme, "1", phoneme] elif len(tmp) == 4: # Original format with 4 fields filename = tmp[0] self.phoneme_data[filename] = [tmp[1], tmp[2], tmp[3]]
# self.phoneme_data = np.load(phoneme_path, allow_pickle=True).item() # pad for semantic tokens self.PAD: int = pad_val # self.hz = 25 # with open("/data/docker/liujing04/gpt-vits/mq-vits-s1bert_no_bert/configs/s2.json", "r") as f:data = f.read() # data=json.loads(data)["model"]["semantic_frame_rate"]#50hz # self.hz=int(data[:-2])# self.hz = int(os.environ.get("hz", "25hz")[:-2])
# max seconds of semantic token self.max_sec = max_sec self.min_ps_ratio = min_ps_ratio self.max_ps_ratio = max_ps_ratio
if max_sample is not None: self.semantic_data = self.semantic_data[:max_sample]
# {idx: (semantic, phoneme)} # semantic list, phoneme list self.semantic_phoneme = [] self.item_names = []
self.inited = False
if not self.inited: # 调用初始化函数 self.init_batch() self.inited = True del self.semantic_data del self.phoneme_data # self.tokenizer = AutoTokenizer.from_pretrained("hfl/chinese-roberta-wwm-ext-large") # self.tokenizer = AutoTokenizer.from_pretrained("/data/docker/liujing04/bert-vits2/Bert-VITS2-master20231106/bert/chinese-roberta-wwm-ext-large")
def init_batch(self): semantic_data_len = len(self.semantic_data) phoneme_data_len = len(self.phoneme_data.keys()) print("semantic_data_len:", semantic_data_len) print("phoneme_data_len:", phoneme_data_len) print(self.semantic_data) idx = 0 num_not_in = 0 num_deleted_bigger = 0 num_deleted_ps = 0 for i in range(semantic_data_len): # get str item_name = self.semantic_data.iloc[i,0] try: phoneme, word2ph, text = self.phoneme_data[item_name] except Exception: traceback.print_exc() num_not_in += 1 continue
# Convert Hindi text to semantic IDs using text_to_sequence semantic_str = self.semantic_data.iloc[i,1] # First convert to phonemes phonemes = text_to_sequence(semantic_str, language='hi') # Map phonemes to indices in the symbols list symbol_to_id = {s: i for i, s in enumerate(symbols)} semantic_ids = [symbol_to_id[p] for p in phonemes]
# Filter out too long samples if len(semantic_ids) > self.max_sec * self.hz: num_deleted_bigger += 1 continue
phoneme = phoneme.split(" ")
try: phoneme_ids = cleaned_text_to_sequence(phoneme, version) except: traceback.print_exc() num_not_in += 1 continue
if len(phoneme_ids) > self.max_sec * self.hz / 2.5: num_deleted_ps += 1 continue
ps_ratio = len(phoneme_ids) / (len(semantic_ids) / self.hz)
if ps_ratio > self.max_ps_ratio or ps_ratio < self.min_ps_ratio: num_deleted_ps += 1 continue
self.semantic_phoneme.append((semantic_ids, phoneme_ids)) idx += 1 self.item_names.append(item_name)
min_num = 100 # 20直接不补#30补了也不存ckpt leng = len(self.semantic_phoneme) if leng < min_num: tmp1 = self.semantic_phoneme tmp2 = self.item_names self.semantic_phoneme = [] self.item_names = [] for _ in range(max(2, int(min_num / leng))): self.semantic_phoneme += tmp1 self.item_names += tmp2 if num_not_in > 0: print(f"there are {num_not_in} semantic datas not in phoneme datas") if num_deleted_bigger > 0: print( f"deleted {num_deleted_bigger} audios who's duration are bigger than {self.max_sec} seconds" ) if num_deleted_ps > 0: # 4702 for LibriTTS, LirbriTTS 是标注数据, 是否需要筛?=> 需要,有值为 100 的极端值 print( f"deleted {num_deleted_ps} audios who's phoneme/sec are bigger than {self.max_ps_ratio} or smaller than {self.min_ps_ratio}" ) """ there are 31 semantic datas not in phoneme datas deleted 34 audios who's duration are bigger than 54 seconds deleted 3190 audios who's phoneme/sec are bigger than 25 or smaller than 3 dataset.__len__(): 366463
""" # 345410 for LibriTTS print("dataset.__len__():", self.__len__())
def __get_item_names__(self) -> List[str]: return self.item_names
def __len__(self) -> int: return len(self.semantic_phoneme)
def __getitem__(self, idx: int) -> Dict: semantic_ids, phoneme_ids = self.semantic_phoneme[idx] item_name = self.item_names[idx] phoneme_ids_len = len(phoneme_ids) # semantic tokens target semantic_ids_len = len(semantic_ids)
flag = 0 path_bert = "%s/%s.pt" % (self.path3, item_name) if os.path.exists(path_bert) == True: bert_feature = torch.load(path_bert, map_location="cpu") else: flag = 1 if flag == 1: # bert_feature=torch.zeros_like(phoneme_ids,dtype=torch.float32) bert_feature = None else: assert bert_feature.shape[-1] == len(phoneme_ids) return { "idx": idx, "phoneme_ids": phoneme_ids, "phoneme_ids_len": phoneme_ids_len, "semantic_ids": semantic_ids, "semantic_ids_len": semantic_ids_len, "bert_feature": bert_feature, }
def get_sample_length(self, idx: int): semantic_ids = self.semantic_phoneme[idx][0] sec = 1.0 * len(semantic_ids) / self.hz return sec
def collate(self, examples: List[Dict]) -> Dict: sample_index: List[int] = [] phoneme_ids: List[torch.Tensor] = [] phoneme_ids_lens: List[int] = [] semantic_ids: List[torch.Tensor] = [] semantic_ids_lens: List[int] = [] # return
for item in examples: sample_index.append(item["idx"]) phoneme_ids.append(np.array(item["phoneme_ids"], dtype=np.int64)) semantic_ids.append(np.array(item["semantic_ids"], dtype=np.int64)) phoneme_ids_lens.append(item["phoneme_ids_len"]) semantic_ids_lens.append(item["semantic_ids_len"])
# pad 0 phoneme_ids = batch_sequences(phoneme_ids) semantic_ids = batch_sequences(semantic_ids, pad_value=self.PAD)
# # convert each batch to torch.tensor phoneme_ids = torch.tensor(phoneme_ids) semantic_ids = torch.tensor(semantic_ids) phoneme_ids_lens = torch.tensor(phoneme_ids_lens) semantic_ids_lens = torch.tensor(semantic_ids_lens) bert_padded = torch.FloatTensor(len(examples), 1024, max(phoneme_ids_lens)) bert_padded.zero_()
for idx, item in enumerate(examples): bert = item["bert_feature"] if bert != None: bert_padded[idx, :, : bert.shape[-1]] = bert
return { # List[int] "ids": sample_index, # torch.Tensor (B, max_phoneme_length) "phoneme_ids": phoneme_ids, # torch.Tensor (B) "phoneme_ids_len": phoneme_ids_lens, # torch.Tensor (B, max_semantic_ids_length) "semantic_ids": semantic_ids, # torch.Tensor (B) "semantic_ids_len": semantic_ids_lens, # torch.Tensor (B, 1024, max_phoneme_length) "bert_feature": bert_padded, }
if __name__ == "__main__": root_dir = "/data/docker/liujing04/gpt-vits/prepare/dump_mix/" dataset = Text2SemanticDataset( phoneme_path=root_dir + "phoneme_train.npy", semantic_path=root_dir + "semantic_train.tsv", )
batch_size = 12 dataloader = DataLoader( dataset, batch_size=batch_size, collate_fn=dataset.collate, shuffle=False ) for i, batch in enumerate(dataloader): if i % 1000 == 0: print(i) # if i == 0: # print('batch["ids"]:', batch["ids"]) # print('batch["phoneme_ids"]:', batch["phoneme_ids"], # batch["phoneme_ids"].shape) # print('batch["phoneme_ids_len"]:', batch["phoneme_ids_len"], # batch["phoneme_ids_len"].shape) # print('batch["semantic_ids"]:', batch["semantic_ids"], # batch["semantic_ids"].shape) # print('batch["semantic_ids_len"]:', batch["semantic_ids_len"], # batch["semantic_ids_len"].shape)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/models/t2s_lightning_module.py# reference: https://github.com/lifeiteng/vall-eimport os, sys
now_dir = os.getcwd()sys.path.append(now_dir)from typing import Dict
import torchfrom pytorch_lightning import LightningModulefrom AR.models.t2s_model import Text2SemanticDecoderfrom AR.modules.lr_schedulers import WarmupCosineLRSchedulefrom AR.modules.optim import ScaledAdam
class Text2SemanticLightningModule(LightningModule): def __init__(self, config, output_dir, is_train=True): super().__init__() self.config = config self.top_k = 3 self.model = Text2SemanticDecoder(config=config, top_k=self.top_k) pretrained_s1 = config.get("pretrained_s1") if pretrained_s1 and is_train: # print(self.load_state_dict(torch.load(pretrained_s1,map_location="cpu")["state_dict"])) print( self.load_state_dict( torch.load(pretrained_s1, map_location="cpu")["weight"] ) ) if is_train: self.automatic_optimization = False self.save_hyperparameters() self.eval_dir = output_dir / "eval" self.eval_dir.mkdir(parents=True, exist_ok=True)
def training_step(self, batch: Dict, batch_idx: int): opt = self.optimizers() scheduler = self.lr_schedulers() forward=self.model.forward if self.config["train"].get("if_dpo",False)==True else self.model.forward_old loss, acc = forward( batch["phoneme_ids"], batch["phoneme_ids_len"], batch["semantic_ids"], batch["semantic_ids_len"], batch["bert_feature"], ) self.manual_backward(loss) if batch_idx > 0 and batch_idx % 4 == 0: opt.step() opt.zero_grad() scheduler.step()
self.log( "total_loss", loss, on_step=True, on_epoch=True, prog_bar=True, sync_dist=True, ) self.log( "lr", scheduler.get_last_lr()[0], on_epoch=True, prog_bar=True, sync_dist=True, ) self.log( f"top_{self.top_k}_acc", acc, on_step=True, on_epoch=True, prog_bar=True, sync_dist=True, )
def validation_step(self, batch: Dict, batch_idx: int): return
# # get loss # loss, acc = self.model.forward( # batch['phoneme_ids'], batch['phoneme_ids_len'], # batch['semantic_ids'], batch['semantic_ids_len'], # batch['bert_feature'] # ) # # self.log( # "val_total_loss", # loss, # on_step=True, # on_epoch=True, # prog_bar=True, # sync_dist=True) # self.log( # f"val_top_{self.top_k}_acc", # acc, # on_step=True, # on_epoch=True, # prog_bar=True, # sync_dist=True) # # # get infer output # semantic_len = batch['semantic_ids'].size(1) # prompt_len = min(int(semantic_len * 0.5), 150) # prompt = batch['semantic_ids'][:, :prompt_len] # pred_semantic = self.model.infer(batch['phoneme_ids'], # batch['phoneme_ids_len'], prompt, # batch['bert_feature'] # ) # save_name = f'semantic_toks_{batch_idx}.pt' # save_path = os.path.join(self.eval_dir, save_name) # torch.save(pred_semantic.detach().cpu(), save_path)
def configure_optimizers(self): model_parameters = self.model.parameters() parameters_names = [] parameters_names.append( [name_param_pair[0] for name_param_pair in self.model.named_parameters()] ) lm_opt = ScaledAdam( model_parameters, lr=0.01, betas=(0.9, 0.95), clipping_scale=2.0, parameters_names=parameters_names, show_dominant_parameters=False, clipping_update_period=1000, )
return { "optimizer": lm_opt, "lr_scheduler": { "scheduler": WarmupCosineLRSchedule( lm_opt, init_lr=self.config["optimizer"]["lr_init"], peak_lr=self.config["optimizer"]["lr"], end_lr=self.config["optimizer"]["lr_end"], warmup_steps=self.config["optimizer"]["warmup_steps"], total_steps=self.config["optimizer"]["decay_steps"], ) }, }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/models/t2s_lightning_module.py# reference: https://github.com/lifeiteng/vall-eimport os, sys
now_dir = os.getcwd()sys.path.append(now_dir)from typing import Dict
import torchfrom pytorch_lightning import LightningModulefrom AR.models.t2s_model_onnx import Text2SemanticDecoderfrom AR.modules.lr_schedulers import WarmupCosineLRSchedulefrom AR.modules.optim import ScaledAdam
class Text2SemanticLightningModule(LightningModule): def __init__(self, config, output_dir, is_train=True): super().__init__() self.config = config self.top_k = 3 self.model = Text2SemanticDecoder(config=config, top_k=self.top_k) pretrained_s1 = config.get("pretrained_s1") if pretrained_s1 and is_train: # print(self.load_state_dict(torch.load(pretrained_s1,map_location="cpu")["state_dict"])) print( self.load_state_dict( torch.load(pretrained_s1, map_location="cpu")["weight"] ) ) if is_train: self.automatic_optimization = False self.save_hyperparameters() self.eval_dir = output_dir / "eval" self.eval_dir.mkdir(parents=True, exist_ok=True)
def training_step(self, batch: Dict, batch_idx: int): opt = self.optimizers() scheduler = self.lr_schedulers() loss, acc = self.model.forward( batch["phoneme_ids"], batch["phoneme_ids_len"], batch["semantic_ids"], batch["semantic_ids_len"], batch["bert_feature"], ) self.manual_backward(loss) if batch_idx > 0 and batch_idx % 4 == 0: opt.step() opt.zero_grad() scheduler.step()
self.log( "total_loss", loss, on_step=True, on_epoch=True, prog_bar=True, sync_dist=True, ) self.log( "lr", scheduler.get_last_lr()[0], on_epoch=True, prog_bar=True, sync_dist=True, ) self.log( f"top_{self.top_k}_acc", acc, on_step=True, on_epoch=True, prog_bar=True, sync_dist=True, )
def validation_step(self, batch: Dict, batch_idx: int): return
def configure_optimizers(self): model_parameters = self.model.parameters() parameters_names = [] parameters_names.append( [name_param_pair[0] for name_param_pair in self.model.named_parameters()] ) lm_opt = ScaledAdam( model_parameters, lr=0.01, betas=(0.9, 0.95), clipping_scale=2.0, parameters_names=parameters_names, show_dominant_parameters=False, clipping_update_period=1000, )
return { "optimizer": lm_opt, "lr_scheduler": { "scheduler": WarmupCosineLRSchedule( lm_opt, init_lr=self.config["optimizer"]["lr_init"], peak_lr=self.config["optimizer"]["lr"], end_lr=self.config["optimizer"]["lr_end"], warmup_steps=self.config["optimizer"]["warmup_steps"], total_steps=self.config["optimizer"]["decay_steps"], ) }, }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/models/t2s_model.py# reference: https://github.com/lifeiteng/vall-eimport mathfrom typing import List, Optionalimport torchfrom tqdm import tqdm
from AR.models.utils import make_pad_maskfrom AR.models.utils import ( topk_sampling, sample, logits_to_probs, multinomial_sample_one_no_sync, dpo_loss, make_reject_y, get_batch_logps)from AR.modules.embedding import SinePositionalEmbeddingfrom AR.modules.embedding import TokenEmbeddingfrom AR.modules.transformer import LayerNormfrom AR.modules.transformer import TransformerEncoderfrom AR.modules.transformer import TransformerEncoderLayerfrom torch import nnfrom torch.nn import functional as Ffrom torchmetrics.classification import MulticlassAccuracy
default_config = { "embedding_dim": 512, "hidden_dim": 512, "num_head": 8, "num_layers": 12, "num_codebook": 8, "p_dropout": 0.0, "vocab_size": 1024 + 1, "phoneme_vocab_size": 512, "EOS": 1024,}
# @torch.jit.script ## 使用的话首次推理会非常慢,而且推理速度不稳定# Efficient implementation equivalent to the following:def scaled_dot_product_attention(query:torch.Tensor, key:torch.Tensor, value:torch.Tensor, attn_mask:Optional[torch.Tensor]=None, scale:Optional[torch.Tensor]=None) -> torch.Tensor: B, H, L, S =query.size(0), query.size(1), query.size(-2), key.size(-2) if scale is None: scale_factor = torch.tensor(1 / math.sqrt(query.size(-1))) else: scale_factor = scale attn_bias = torch.zeros(B, H, L, S, dtype=query.dtype, device=query.device)
if attn_mask is not None: if attn_mask.dtype == torch.bool: attn_bias.masked_fill_(attn_mask, float("-inf")) else: attn_bias += attn_mask attn_weight = query @ key.transpose(-2, -1) * scale_factor attn_weight += attn_bias attn_weight = torch.softmax(attn_weight, dim=-1)
if attn_mask is not None: if attn_mask.dtype == torch.bool: attn_weight.masked_fill_(attn_mask, 0) else: attn_mask[attn_mask!=float("-inf")] =0 attn_mask[attn_mask==float("-inf")] =1 attn_weight.masked_fill_(attn_mask, 0)
return attn_weight @ value
@torch.jit.scriptclass T2SMLP: def __init__(self, w1, b1, w2, b2): self.w1 = w1 self.b1 = b1 self.w2 = w2 self.b2 = b2
def forward(self, x): x = F.relu(F.linear(x, self.w1, self.b1)) x = F.linear(x, self.w2, self.b2) return x
@torch.jit.scriptclass T2SBlock: def __init__( self, num_heads, hidden_dim: int, mlp: T2SMLP, qkv_w, qkv_b, out_w, out_b, norm_w1, norm_b1, norm_eps1, norm_w2, norm_b2, norm_eps2, ): self.num_heads = num_heads self.mlp = mlp self.hidden_dim: int = hidden_dim self.qkv_w = qkv_w self.qkv_b = qkv_b self.out_w = out_w self.out_b = out_b self.norm_w1 = norm_w1 self.norm_b1 = norm_b1 self.norm_eps1 = norm_eps1 self.norm_w2 = norm_w2 self.norm_b2 = norm_b2 self.norm_eps2 = norm_eps2
self.false = torch.tensor(False, dtype=torch.bool)
@torch.jit.ignore def to_mask(self, x:torch.Tensor, padding_mask:Optional[torch.Tensor]): if padding_mask is None: return x if padding_mask.dtype == torch.bool: return x.masked_fill(padding_mask, 0) else: return x * padding_mask def process_prompt(self, x:torch.Tensor, attn_mask : torch.Tensor, padding_mask:Optional[torch.Tensor]=None, torch_sdpa:bool=True):
q, k, v = F.linear(self.to_mask(x, padding_mask), self.qkv_w, self.qkv_b).chunk(3, dim=-1)
batch_size = q.shape[0] q_len = q.shape[1] kv_len = k.shape[1] q = self.to_mask(q, padding_mask) k_cache = self.to_mask(k, padding_mask) v_cache = self.to_mask(v, padding_mask)
q = q.view(batch_size, q_len, self.num_heads, -1).transpose(1, 2) k = k_cache.view(batch_size, kv_len, self.num_heads, -1).transpose(1, 2) v = v_cache.view(batch_size, kv_len, self.num_heads, -1).transpose(1, 2)
if torch_sdpa: attn = F.scaled_dot_product_attention(q, k, v, ~attn_mask) else: attn = scaled_dot_product_attention(q, k, v, attn_mask)
attn = attn.permute(2, 0, 1, 3).reshape(batch_size*q_len, self.hidden_dim) attn = attn.view(q_len, batch_size, self.hidden_dim).transpose(1, 0) attn = F.linear(self.to_mask(attn, padding_mask), self.out_w, self.out_b)
if padding_mask is not None: for i in range(batch_size): # mask = padding_mask[i,:,0] if self.false.device!= padding_mask.device: self.false = self.false.to(padding_mask.device) idx = torch.where(padding_mask[i,:,0]==self.false)[0] x_item = x[i,idx,:].unsqueeze(0) attn_item = attn[i,idx,:].unsqueeze(0) x_item = x_item + attn_item x_item = F.layer_norm( x_item, [self.hidden_dim], self.norm_w1, self.norm_b1, self.norm_eps1 ) x_item = x_item + self.mlp.forward(x_item) x_item = F.layer_norm( x_item, [self.hidden_dim], self.norm_w2, self.norm_b2, self.norm_eps2, ) x[i,idx,:] = x_item.squeeze(0) x = self.to_mask(x, padding_mask) else: x = x + attn x = F.layer_norm( x, [self.hidden_dim], self.norm_w1, self.norm_b1, self.norm_eps1 ) x = x + self.mlp.forward(x) x = F.layer_norm( x, [self.hidden_dim], self.norm_w2, self.norm_b2, self.norm_eps2, ) return x, k_cache, v_cache def decode_next_token(self, x:torch.Tensor, k_cache:torch.Tensor, v_cache:torch.Tensor, attn_mask:Optional[torch.Tensor]=None, torch_sdpa:bool=True): q, k, v = F.linear(x, self.qkv_w, self.qkv_b).chunk(3, dim=-1)
k_cache = torch.cat([k_cache, k], dim=1) v_cache = torch.cat([v_cache, v], dim=1) batch_size = q.shape[0] q_len = q.shape[1] kv_len = k_cache.shape[1]
q = q.view(batch_size, q_len, self.num_heads, -1).transpose(1, 2) k = k_cache.view(batch_size, kv_len, self.num_heads, -1).transpose(1, 2) v = v_cache.view(batch_size, kv_len, self.num_heads, -1).transpose(1, 2)
if torch_sdpa: attn = F.scaled_dot_product_attention(q, k, v) else: attn = scaled_dot_product_attention(q, k, v, attn_mask)
attn = attn.permute(2, 0, 1, 3).reshape(batch_size*q_len, self.hidden_dim) attn = attn.view(q_len, batch_size, self.hidden_dim).transpose(1, 0) attn = F.linear(attn, self.out_w, self.out_b)
x = x + attn x = F.layer_norm( x, [self.hidden_dim], self.norm_w1, self.norm_b1, self.norm_eps1 ) x = x + self.mlp.forward(x) x = F.layer_norm( x, [self.hidden_dim], self.norm_w2, self.norm_b2, self.norm_eps2, ) return x, k_cache, v_cache
@torch.jit.scriptclass T2STransformer: def __init__(self, num_blocks : int, blocks: List[T2SBlock]): self.num_blocks : int = num_blocks self.blocks = blocks
def process_prompt( self, x:torch.Tensor, attn_mask : torch.Tensor, padding_mask : Optional[torch.Tensor]=None, torch_sdpa:bool=True ): k_cache : List[torch.Tensor] = [] v_cache : List[torch.Tensor] = [] for i in range(self.num_blocks): x, k_cache_, v_cache_ = self.blocks[i].process_prompt(x, attn_mask, padding_mask, torch_sdpa) k_cache.append(k_cache_) v_cache.append(v_cache_) return x, k_cache, v_cache
def decode_next_token( self, x:torch.Tensor, k_cache: List[torch.Tensor], v_cache: List[torch.Tensor], attn_mask : Optional[torch.Tensor]=None, torch_sdpa:bool=True ): for i in range(self.num_blocks): x, k_cache[i], v_cache[i] = self.blocks[i].decode_next_token(x, k_cache[i], v_cache[i], attn_mask, torch_sdpa) return x, k_cache, v_cache
class Text2SemanticDecoder(nn.Module): def __init__(self, config, norm_first=False, top_k=3): super(Text2SemanticDecoder, self).__init__() self.model_dim = config["model"]["hidden_dim"] self.embedding_dim = config["model"]["embedding_dim"] self.num_head = config["model"]["head"] self.num_layers = config["model"]["n_layer"] self.norm_first = norm_first self.vocab_size = config["model"]["vocab_size"] self.phoneme_vocab_size = config["model"]["phoneme_vocab_size"] self.p_dropout = config["model"]["dropout"] self.EOS = config["model"]["EOS"] self.norm_first = norm_first assert self.EOS == self.vocab_size - 1 # should be same as num of kmeans bin # assert self.EOS == 1024 self.bert_proj = nn.Linear(1024, self.embedding_dim) self.ar_text_embedding = TokenEmbedding( self.embedding_dim, self.phoneme_vocab_size, self.p_dropout ) self.ar_text_position = SinePositionalEmbedding( self.embedding_dim, dropout=0.1, scale=False, alpha=True ) self.ar_audio_embedding = TokenEmbedding( self.embedding_dim, self.vocab_size, self.p_dropout ) self.ar_audio_position = SinePositionalEmbedding( self.embedding_dim, dropout=0.1, scale=False, alpha=True )
self.h = TransformerEncoder( TransformerEncoderLayer( d_model=self.model_dim, nhead=self.num_head, dim_feedforward=self.model_dim * 4, dropout=0.1, batch_first=True, norm_first=norm_first, ), num_layers=self.num_layers, norm=LayerNorm(self.model_dim) if norm_first else None, )
self.ar_predict_layer = nn.Linear(self.model_dim, self.vocab_size, bias=False) self.loss_fct = nn.CrossEntropyLoss(reduction="sum")
self.ar_accuracy_metric = MulticlassAccuracy( self.vocab_size, top_k=top_k, average="micro", multidim_average="global", ignore_index=self.EOS, )
blocks = []
for i in range(self.num_layers): layer = self.h.layers[i] t2smlp = T2SMLP( layer.linear1.weight, layer.linear1.bias, layer.linear2.weight, layer.linear2.bias )
block = T2SBlock( self.num_head, self.model_dim, t2smlp, layer.self_attn.in_proj_weight, layer.self_attn.in_proj_bias, layer.self_attn.out_proj.weight, layer.self_attn.out_proj.bias, layer.norm1.weight, layer.norm1.bias, layer.norm1.eps, layer.norm2.weight, layer.norm2.bias, layer.norm2.eps )
blocks.append(block) self.t2s_transformer = T2STransformer(self.num_layers, blocks)
def make_input_data(self, x, x_lens, y, y_lens, bert_feature): x = self.ar_text_embedding(x) x = x + self.bert_proj(bert_feature.transpose(1, 2)) x = self.ar_text_position(x) x_mask = make_pad_mask(x_lens)
y_mask = make_pad_mask(y_lens) y_mask_int = y_mask.type(torch.int64) codes = y.type(torch.int64) * (1 - y_mask_int)
# Training # AR Decoder y, targets = self.pad_y_eos(codes, y_mask_int, eos_id=self.EOS) x_len = x_lens.max() y_len = y_lens.max() y_emb = self.ar_audio_embedding(y) y_pos = self.ar_audio_position(y_emb)
xy_padding_mask = torch.concat([x_mask, y_mask], dim=1)
ar_xy_padding_mask = xy_padding_mask
x_attn_mask = F.pad( torch.zeros((x_len, x_len), dtype=torch.bool, device=x.device), (0, y_len), value=True, ) # x_attn_mask[:, x_len]=False y_attn_mask = F.pad( torch.triu( torch.ones(y_len, y_len, dtype=torch.bool, device=x.device), diagonal=1, ), (x_len, 0), value=False, )
xy_attn_mask = torch.concat([x_attn_mask, y_attn_mask], dim=0) bsz, src_len = x.shape[0], x_len + y_len _xy_padding_mask = ( ar_xy_padding_mask.view(bsz, 1, 1, src_len) .expand(-1, self.num_head, -1, -1) .reshape(bsz * self.num_head, 1, src_len) ) xy_attn_mask = xy_attn_mask.logical_or(_xy_padding_mask) new_attn_mask = torch.zeros_like(xy_attn_mask, dtype=x.dtype) new_attn_mask.masked_fill_(xy_attn_mask, float("-inf")) xy_attn_mask = new_attn_mask # x 和完整的 y 一次性输入模型 xy_pos = torch.concat([x, y_pos], dim=1)
return xy_pos, xy_attn_mask, targets
def forward(self, x, x_lens, y, y_lens, bert_feature): """ x: phoneme_ids y: semantic_ids """
reject_y, reject_y_lens = make_reject_y(y, y_lens)
xy_pos, xy_attn_mask, targets = self.make_input_data(x, x_lens, y, y_lens, bert_feature)
xy_dec, _ = self.h( (xy_pos, None), mask=xy_attn_mask, ) x_len = x_lens.max() logits = self.ar_predict_layer(xy_dec[:, x_len:])
###### DPO ############# reject_xy_pos, reject_xy_attn_mask, reject_targets = self.make_input_data(x, x_lens, reject_y, reject_y_lens, bert_feature)
reject_xy_dec, _ = self.h( (reject_xy_pos, None), mask=reject_xy_attn_mask, ) x_len = x_lens.max() reject_logits = self.ar_predict_layer(reject_xy_dec[:, x_len:])
# loss # from feiteng: 每次 duration 越多, 梯度更新也应该更多, 所以用 sum
loss_1 = F.cross_entropy(logits.permute(0, 2, 1), targets, reduction="sum") acc = self.ar_accuracy_metric(logits.permute(0, 2, 1).detach(), targets).item()
A_logits, R_logits = get_batch_logps(logits, reject_logits, targets, reject_targets) loss_2, _, _ = dpo_loss(A_logits, R_logits, 0, 0, 0.2, reference_free=True) loss = loss_1 + loss_2
return loss, acc
def forward_old(self, x, x_lens, y, y_lens, bert_feature): """ x: phoneme_ids y: semantic_ids """ x = self.ar_text_embedding(x) x = x + self.bert_proj(bert_feature.transpose(1, 2)) x = self.ar_text_position(x) x_mask = make_pad_mask(x_lens)
y_mask = make_pad_mask(y_lens) y_mask_int = y_mask.type(torch.int64) codes = y.type(torch.int64) * (1 - y_mask_int)
# Training # AR Decoder y, targets = self.pad_y_eos(codes, y_mask_int, eos_id=self.EOS) x_len = x_lens.max() y_len = y_lens.max() y_emb = self.ar_audio_embedding(y) y_pos = self.ar_audio_position(y_emb)
xy_padding_mask = torch.concat([x_mask, y_mask], dim=1) ar_xy_padding_mask = xy_padding_mask
x_attn_mask = F.pad( torch.zeros((x_len, x_len), dtype=torch.bool, device=x.device), (0, y_len), value=True, ) y_attn_mask = F.pad( torch.triu( torch.ones(y_len, y_len, dtype=torch.bool, device=x.device), diagonal=1, ), (x_len, 0), value=False, ) xy_attn_mask = torch.concat([x_attn_mask, y_attn_mask], dim=0) bsz, src_len = x.shape[0], x_len + y_len _xy_padding_mask = ( ar_xy_padding_mask.view(bsz, 1, 1, src_len) .expand(-1, self.num_head, -1, -1) .reshape(bsz * self.num_head, 1, src_len) ) xy_attn_mask = xy_attn_mask.logical_or(_xy_padding_mask) new_attn_mask = torch.zeros_like(xy_attn_mask, dtype=x.dtype) new_attn_mask.masked_fill_(xy_attn_mask, float("-inf")) xy_attn_mask = new_attn_mask # x 和完整的 y 一次性输入模型 xy_pos = torch.concat([x, y_pos], dim=1) xy_dec, _ = self.h( (xy_pos, None), mask=xy_attn_mask, ) logits = self.ar_predict_layer(xy_dec[:, x_len:]).permute(0, 2, 1) # loss # from feiteng: 每次 duration 越多, 梯度更新也应该更多, 所以用 sum loss = F.cross_entropy(logits, targets, reduction="sum") acc = self.ar_accuracy_metric(logits.detach(), targets).item() return loss, acc
# 需要看下这个函数和 forward 的区别以及没有 semantic 的时候 prompts 输入什么 def infer( self, x, x_lens, prompts, bert_feature, top_k: int = -100, early_stop_num: int = -1, temperature: float = 1.0, ): x = self.ar_text_embedding(x) x = x + self.bert_proj(bert_feature.transpose(1, 2)) x = self.ar_text_position(x)
# AR Decoder y = prompts prefix_len = y.shape[1] x_len = x.shape[1] x_attn_mask = torch.zeros((x_len, x_len), dtype=torch.bool) stop = False for _ in tqdm(range(1500)): y_emb = self.ar_audio_embedding(y) y_pos = self.ar_audio_position(y_emb) # x 和逐渐增长的 y 一起输入给模型 xy_pos = torch.concat([x, y_pos], dim=1) y_len = y.shape[1] x_attn_mask_pad = F.pad( x_attn_mask, (0, y_len), value=True, ) y_attn_mask = F.pad( torch.triu(torch.ones(y_len, y_len, dtype=torch.bool), diagonal=1), (x_len, 0), value=False, ) xy_attn_mask = torch.concat([x_attn_mask_pad, y_attn_mask], dim=0).to( y.device )
xy_dec, _ = self.h( (xy_pos, None), mask=xy_attn_mask, ) logits = self.ar_predict_layer(xy_dec[:, -1]) samples = topk_sampling( logits, top_k=top_k, top_p=1.0, temperature=temperature )
if early_stop_num != -1 and (y.shape[1] - prefix_len) > early_stop_num: print("use early stop num:", early_stop_num) stop = True
if torch.argmax(logits, dim=-1)[0] == self.EOS or samples[0, 0] == self.EOS: # print(torch.argmax(logits, dim=-1)[0] == self.EOS, samples[0, 0] == self.EOS) stop = True if stop: if prompts.shape[1] == y.shape[1]: y = torch.concat([y, torch.zeros_like(samples)], dim=1) print("bad zero prediction") print(f"T2S Decoding EOS [{prefix_len} -> {y.shape[1]}]") break # 本次生成的 semantic_ids 和之前的 y 构成新的 y # print(samples.shape)#[1,1]#第一个1是bs # import os # os._exit(2333) y = torch.concat([y, samples], dim=1) return y
def pad_y_eos(self, y, y_mask_int, eos_id): targets = F.pad(y, (0, 1), value=0) + eos_id * F.pad( y_mask_int, (0, 1), value=1 ) # 错位 return targets[:, :-1], targets[:, 1:]
def infer_panel_batch_infer( self, x:List[torch.LongTensor], #####全部文本token x_lens:torch.LongTensor, prompts:torch.LongTensor, ####参考音频token bert_feature:List[torch.LongTensor], top_k: int = -100, top_p: int = 100, early_stop_num: int = -1, temperature: float = 1.0, repetition_penalty: float = 1.35, **kwargs, ): if prompts is None: print("Warning: Prompt free is not supported batch_infer! switch to naive_infer") return self.infer_panel_naive_batched(x, x_lens, prompts, bert_feature, top_k=top_k, top_p=top_p, early_stop_num=early_stop_num, temperature=temperature, **kwargs)
max_len = kwargs.get("max_len",x_lens.max()) x_list = [] for x_item, bert_item in zip(x, bert_feature): # max_len = max(max_len, x_item.shape[0], bert_item.shape[1]) x_item = self.ar_text_embedding(x_item.unsqueeze(0)) x_item = x_item + self.bert_proj(bert_item.transpose(0, 1).unsqueeze(0)) x_item = self.ar_text_position(x_item).squeeze(0) x_item = F.pad(x_item,(0,0,0,max_len-x_item.shape[0]),value=0) if x_item.shape[0]<max_len else x_item x_list.append(x_item) x = torch.stack(x_list, dim=0)
# AR Decoder y = prompts x_len = x.shape[1] x_attn_mask = torch.zeros((x_len, x_len), dtype=torch.bool) stop = False
k_cache = None v_cache = None ################### first step ########################## if y is not None: y_emb = self.ar_audio_embedding(y) y_len = y_emb.shape[1] prefix_len = y.shape[1] y_lens = torch.LongTensor([y_emb.shape[1]]*y_emb.shape[0]).to(x.device) y_pos = self.ar_audio_position(y_emb) xy_pos = torch.concat([x, y_pos], dim=1) ref_free = False else: y_emb = None y_len = 0 prefix_len = 0 y_lens = torch.LongTensor([y_len]*x.shape[0]).to(x.device) y_pos = None xy_pos = x y = torch.zeros(x.shape[0], 0, dtype=torch.int, device=x.device) ref_free = True
##### create mask ##### bsz = x.shape[0] src_len = x_len + y_len y_paddind_mask = make_pad_mask(y_lens, y_len) x_paddind_mask = make_pad_mask(x_lens, max_len) # (bsz, x_len + y_len) xy_padding_mask = torch.concat([x_paddind_mask, y_paddind_mask], dim=1)
x_mask = F.pad( x_attn_mask, (0, y_len), ###xx的纯0扩展到xx纯0+xy纯1,(x,x+y) value=True, ) y_mask = F.pad( ###yy的右上1扩展到左边xy的0,(y,x+y) torch.triu(torch.ones(y_len, y_len, dtype=torch.bool), diagonal=1), (x_len, 0), value=False, ) xy_mask = torch.concat([x_mask, y_mask], dim=0).view(1 , src_len, src_len).repeat(bsz, 1, 1).to(x.device) _xy_padding_mask = xy_padding_mask.view(bsz, 1, src_len).repeat(1, src_len, 1) for i in range(bsz): l = x_lens[i] _xy_padding_mask[i,l:max_len,:]=True xy_attn_mask = xy_mask.logical_or(_xy_padding_mask) xy_attn_mask = xy_attn_mask.unsqueeze(1).expand(-1, self.num_head, -1, -1) xy_attn_mask = xy_attn_mask.bool() xy_padding_mask = xy_padding_mask.view(bsz, src_len, 1).expand(-1, -1, self.model_dim)
###### decode ##### y_list = [None]*y.shape[0] batch_idx_map = list(range(y.shape[0])) idx_list = [None]*y.shape[0] for idx in tqdm(range(1500)): if idx == 0: xy_dec, k_cache, v_cache = self.t2s_transformer.process_prompt(xy_pos, xy_attn_mask, xy_padding_mask, False) else: xy_dec, k_cache, v_cache = self.t2s_transformer.decode_next_token(xy_pos, k_cache, v_cache, xy_attn_mask, False) logits = self.ar_predict_layer( xy_dec[:, -1] )
if idx == 0: xy_attn_mask = F.pad(xy_attn_mask[:,:,-1].unsqueeze(-2),(0,1),value=False) logits = logits[:, :-1] else: xy_attn_mask = F.pad(xy_attn_mask,(0,1),value=False)
samples = sample( logits, y, top_k=top_k, top_p=top_p, repetition_penalty=repetition_penalty, temperature=temperature )[0]
y = torch.concat([y, samples], dim=1) ####### 移除batch中已经生成完毕的序列,进一步优化计算量 tokens = torch.argmax(logits, dim=-1) reserved_idx_of_batch_for_y = None if (self.EOS in samples[:, 0]) or \ (self.EOS in tokens): ###如果生成到EOS,则停止 l1 = samples[:, 0]==self.EOS l2 = tokens==self.EOS l = l1.logical_or(l2) removed_idx_of_batch_for_y = torch.where(l==True)[0].tolist() reserved_idx_of_batch_for_y = torch.where(l==False)[0] # batch_indexs = torch.tensor(batch_idx_map, device=y.device)[removed_idx_of_batch_for_y] for i in removed_idx_of_batch_for_y: batch_index = batch_idx_map[i] idx_list[batch_index] = idx - 1 y_list[batch_index] = y[i, :-1] batch_idx_map = [batch_idx_map[i] for i in reserved_idx_of_batch_for_y.tolist()] # 只保留batch中未生成完毕的序列 if reserved_idx_of_batch_for_y is not None: # index = torch.LongTensor(batch_idx_map).to(y.device) y = torch.index_select(y, dim=0, index=reserved_idx_of_batch_for_y) xy_attn_mask = torch.index_select(xy_attn_mask, dim=0, index=reserved_idx_of_batch_for_y) if k_cache is not None : for i in range(len(k_cache)): k_cache[i] = torch.index_select(k_cache[i], dim=0, index=reserved_idx_of_batch_for_y) v_cache[i] = torch.index_select(v_cache[i], dim=0, index=reserved_idx_of_batch_for_y) if (early_stop_num != -1 and (y.shape[1] - prefix_len) > early_stop_num) or idx==1499: print("use early stop num:", early_stop_num) stop = True for i, batch_index in enumerate(batch_idx_map): batch_index = batch_idx_map[i] idx_list[batch_index] = idx y_list[batch_index] = y[i, :-1] if not (None in idx_list): stop = True if stop: if y.shape[1]==0: y = torch.concat([y, torch.zeros_like(samples)], dim=1) print("bad zero prediction") print(f"T2S Decoding EOS [{prefix_len} -> {y.shape[1]}]") break
####################### update next step ################################### y_emb = self.ar_audio_embedding(y[:, -1:]) xy_pos = y_emb * self.ar_audio_position.x_scale + self.ar_audio_position.alpha * self.ar_audio_position.pe[:, y_len + idx].to( dtype= y_emb.dtype,device=y_emb.device)
if (None in idx_list): for i in range(x.shape[0]): if idx_list[i] is None: idx_list[i] = 1500-1 ###如果没有生成到EOS,就用最大长度代替 if ref_free: return y_list, [0]*x.shape[0] # print(idx_list) return y_list, idx_list def infer_panel_naive_batched(self, x:List[torch.LongTensor], #####全部文本token x_lens:torch.LongTensor, prompts:torch.LongTensor, ####参考音频token bert_feature:List[torch.LongTensor], top_k: int = -100, top_p: int = 100, early_stop_num: int = -1, temperature: float = 1.0, repetition_penalty: float = 1.35, **kwargs ): y_list = [] idx_list = [] for i in range(len(x)): y, idx = self.infer_panel_naive(x[i].unsqueeze(0), x_lens[i], prompts[i].unsqueeze(0) if prompts is not None else None, bert_feature[i].unsqueeze(0), top_k, top_p, early_stop_num, temperature, repetition_penalty, **kwargs) y_list.append(y[0]) idx_list.append(idx) return y_list, idx_list def infer_panel_naive( self, x:torch.LongTensor, #####全部文本token x_lens:torch.LongTensor, prompts:torch.LongTensor, ####参考音频token bert_feature:torch.LongTensor, top_k: int = -100, top_p: int = 100, early_stop_num: int = -1, temperature: float = 1.0, repetition_penalty: float = 1.35, **kwargs ): x = self.ar_text_embedding(x) x = x + self.bert_proj(bert_feature.transpose(1, 2)) x = self.ar_text_position(x)
# AR Decoder y = prompts
x_len = x.shape[1] x_attn_mask = torch.zeros((x_len, x_len), dtype=torch.bool) stop = False # print(1111111,self.num_layers)
k_cache = None v_cache = None ################### first step ########################## if y is not None: y_emb = self.ar_audio_embedding(y) y_len = y_emb.shape[1] prefix_len = y.shape[1] y_pos = self.ar_audio_position(y_emb) xy_pos = torch.concat([x, y_pos], dim=1) ref_free = False else: y_emb = None y_len = 0 prefix_len = 0 y_pos = None xy_pos = x y = torch.zeros(x.shape[0], 0, dtype=torch.int, device=x.device) ref_free = True
bsz = x.shape[0] src_len = x_len + y_len x_attn_mask_pad = F.pad( x_attn_mask, (0, y_len), ###xx的纯0扩展到xx纯0+xy纯1,(x,x+y) value=True, ) y_attn_mask = F.pad( ###yy的右上1扩展到左边xy的0,(y,x+y) torch.triu(torch.ones(y_len, y_len, dtype=torch.bool), diagonal=1), (x_len, 0), value=False, ) xy_attn_mask = torch.concat([x_attn_mask_pad, y_attn_mask], dim=0)\ .unsqueeze(0)\ .expand(bsz*self.num_head, -1, -1)\ .view(bsz, self.num_head, src_len, src_len)\ .to(device=x.device, dtype=torch.bool)
for idx in tqdm(range(1500)): if xy_attn_mask is not None: xy_dec, k_cache, v_cache = self.t2s_transformer.process_prompt(xy_pos, xy_attn_mask, None) else: xy_dec, k_cache, v_cache = self.t2s_transformer.decode_next_token(xy_pos, k_cache, v_cache)
logits = self.ar_predict_layer( xy_dec[:, -1] )
if idx == 0: xy_attn_mask = None if(idx<11):###至少预测出10个token不然不给停止(0.4s) logits = logits[:, :-1]
samples = sample( logits, y, top_k=top_k, top_p=top_p, repetition_penalty=repetition_penalty, temperature=temperature )[0]
y = torch.concat([y, samples], dim=1)
if early_stop_num != -1 and (y.shape[1] - prefix_len) > early_stop_num: print("use early stop num:", early_stop_num) stop = True
if torch.argmax(logits, dim=-1)[0] == self.EOS or samples[0, 0] == self.EOS: stop = True if stop: if y.shape[1] == 0: y = torch.concat([y, torch.zeros_like(samples)], dim=1) print("bad zero prediction") print(f"T2S Decoding EOS [{prefix_len} -> {y.shape[1]}]") break
####################### update next step ################################### y_emb = self.ar_audio_embedding(y[:, -1:]) xy_pos = y_emb * self.ar_audio_position.x_scale + self.ar_audio_position.alpha * self.ar_audio_position.pe[:, y_len + idx].to(dtype=y_emb.dtype,device=y_emb.device)
if ref_free: return y[:, :-1], 0 return y[:, :-1], idx - 1 def infer_panel( self, x:torch.LongTensor, #####全部文本token x_lens:torch.LongTensor, prompts:torch.LongTensor, ####参考音频token bert_feature:torch.LongTensor, top_k: int = -100, top_p: int = 100, early_stop_num: int = -1, temperature: float = 1.0, repetition_penalty: float = 1.35, **kwargs ): return self.infer_panel_naive(x, x_lens, prompts, bert_feature, top_k, top_p, early_stop_num, temperature, repetition_penalty, **kwargs)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/models/t2s_model.py# reference: https://github.com/lifeiteng/vall-eimport torchfrom tqdm import tqdm
from AR.modules.embedding_onnx import SinePositionalEmbeddingfrom AR.modules.embedding_onnx import TokenEmbeddingfrom AR.modules.transformer_onnx import LayerNormfrom AR.modules.transformer_onnx import TransformerEncoderfrom AR.modules.transformer_onnx import TransformerEncoderLayerfrom torch import nnfrom torch.nn import functional as Ffrom torchmetrics.classification import MulticlassAccuracy
default_config = { "embedding_dim": 512, "hidden_dim": 512, "num_head": 8, "num_layers": 12, "num_codebook": 8, "p_dropout": 0.0, "vocab_size": 1024 + 1, "phoneme_vocab_size": 512, "EOS": 1024,}
inf_tensor_value = torch.FloatTensor([-float("Inf")]).float()
def logits_to_probs( logits, previous_tokens = None, temperature: float = 1.0, top_k = None, top_p = None, repetition_penalty: float = 1.0,): previous_tokens = previous_tokens.squeeze() if previous_tokens is not None and repetition_penalty != 1.0: previous_tokens = previous_tokens.long() score = torch.gather(logits, dim=0, index=previous_tokens) score = torch.where( score < 0, score * repetition_penalty, score / repetition_penalty ) logits.scatter_(dim=0, index=previous_tokens, src=score)
if top_p is not None and top_p < 1.0: sorted_logits, sorted_indices = torch.sort(logits, descending=True) cum_probs = torch.cumsum( torch.nn.functional.softmax(sorted_logits, dim=-1), dim=-1 ) sorted_indices_to_remove = cum_probs > top_p sorted_indices_to_remove[0] = False # keep at least one option indices_to_remove = sorted_indices_to_remove.scatter( dim=0, index=sorted_indices, src=sorted_indices_to_remove ) logits = logits.masked_fill(indices_to_remove, -float("Inf"))
logits = logits / max(temperature, 1e-5)
if top_k is not None: v, _ = torch.topk(logits, top_k) pivot = v.select(-1, -1).unsqueeze(-1) logits = torch.where(logits < pivot, inf_tensor_value, logits)
probs = torch.nn.functional.softmax(logits, dim=-1) return probs
def multinomial_sample_one_no_sync( probs_sort): # Does multinomial sampling without a cuda synchronization q = torch.randn_like(probs_sort) return torch.argmax(probs_sort / q, dim=-1, keepdim=True).to(dtype=torch.int)
def sample( logits, previous_tokens, **sampling_kwargs,): probs = logits_to_probs( logits=logits, previous_tokens=previous_tokens, **sampling_kwargs ) idx_next = multinomial_sample_one_no_sync(probs) return idx_next, probs
class OnnxEncoder(nn.Module): def __init__(self, ar_text_embedding, bert_proj, ar_text_position): super().__init__() self.ar_text_embedding = ar_text_embedding self.bert_proj = bert_proj self.ar_text_position = ar_text_position def forward(self, x, bert_feature): x = self.ar_text_embedding(x) x = x + self.bert_proj(bert_feature.transpose(1, 2)) return self.ar_text_position(x)
class T2SFirstStageDecoder(nn.Module): def __init__(self, ar_audio_embedding, ar_audio_position, h, ar_predict_layer, loss_fct, ar_accuracy_metric, top_k, early_stop_num, num_layers): super().__init__() self.ar_audio_embedding = ar_audio_embedding self.ar_audio_position = ar_audio_position self.h = h self.ar_predict_layer = ar_predict_layer self.loss_fct = loss_fct self.ar_accuracy_metric = ar_accuracy_metric self.top_k = top_k self.early_stop_num = early_stop_num self.num_layers = num_layers def forward(self, x, prompt): y = prompt x_example = x[:,:,0] * 0.0 #N, 1, 512 cache = { "all_stage": self.num_layers, "k": None, "v": None, "y_emb": None, "first_infer": 1, "stage": 0, }
y_emb = self.ar_audio_embedding(y)
cache["y_emb"] = y_emb y_pos = self.ar_audio_position(y_emb)
xy_pos = torch.concat([x, y_pos], dim=1)
y_example = y_pos[:,:,0] * 0.0 x_attn_mask = torch.matmul(x_example.transpose(0, 1) , x_example).bool() y_attn_mask = torch.ones_like(torch.matmul(y_example.transpose(0, 1), y_example), dtype=torch.int64) y_attn_mask = torch.cumsum(y_attn_mask, dim=1) - torch.cumsum( torch.ones_like(y_example.transpose(0, 1), dtype=torch.int64), dim=0 ) y_attn_mask = y_attn_mask > 0
x_y_pad = torch.matmul(x_example.transpose(0, 1), y_example).bool() y_x_pad = torch.matmul(y_example.transpose(0, 1), x_example).bool() x_attn_mask_pad = torch.cat([x_attn_mask, torch.ones_like(x_y_pad)], dim=1) y_attn_mask = torch.cat([y_x_pad, y_attn_mask], dim=1) xy_attn_mask = torch.concat([x_attn_mask_pad, y_attn_mask], dim=0) cache["k"] = torch.matmul(x_attn_mask_pad[0].float().unsqueeze(-1), torch.zeros((1, 512)))\ .unsqueeze(1).repeat(self.num_layers, 1, 1, 1) cache["v"] = torch.matmul(x_attn_mask_pad[0].float().unsqueeze(-1), torch.zeros((1, 512)))\ .unsqueeze(1).repeat(self.num_layers, 1, 1, 1)
xy_dec = self.h(xy_pos, mask=xy_attn_mask, cache=cache) logits = self.ar_predict_layer(xy_dec[:, -1]) samples = sample(logits[0], y, top_k=self.top_k, top_p=1.0, repetition_penalty=1.35)[0].unsqueeze(0)
y = torch.concat([y, samples], dim=1)
return y, cache["k"], cache["v"], cache["y_emb"], x_example
class T2SStageDecoder(nn.Module): def __init__(self, ar_audio_embedding, ar_audio_position, h, ar_predict_layer, loss_fct, ar_accuracy_metric, top_k, early_stop_num, num_layers): super().__init__() self.ar_audio_embedding = ar_audio_embedding self.ar_audio_position = ar_audio_position self.h = h self.ar_predict_layer = ar_predict_layer self.loss_fct = loss_fct self.ar_accuracy_metric = ar_accuracy_metric self.top_k = top_k self.early_stop_num = early_stop_num self.num_layers = num_layers
def forward(self, y, k, v, y_emb, x_example): cache = { "all_stage": self.num_layers, "k": torch.nn.functional.pad(k, (0, 0, 0, 0, 0, 1)), "v": torch.nn.functional.pad(v, (0, 0, 0, 0, 0, 1)), "y_emb": y_emb, "first_infer": 0, "stage": 0, }
y_emb = torch.cat( [cache["y_emb"], self.ar_audio_embedding(y[:, -1:])], 1 ) cache["y_emb"] = y_emb y_pos = self.ar_audio_position(y_emb)
xy_pos = y_pos[:, -1:] y_example = y_pos[:,:,0] * 0.0
xy_attn_mask = torch.cat([x_example, y_example], dim=1) xy_attn_mask = torch.zeros_like(xy_attn_mask, dtype=torch.bool)
xy_dec = self.h(xy_pos, mask=xy_attn_mask, cache=cache) logits = self.ar_predict_layer(xy_dec[:, -1]) samples = sample(logits[0], y, top_k=self.top_k, top_p=1.0, repetition_penalty=1.35)[0].unsqueeze(0)
y = torch.concat([y, samples], dim=1)
return y, cache["k"], cache["v"], cache["y_emb"], logits, samples
class Text2SemanticDecoder(nn.Module): def __init__(self, config, norm_first=False, top_k=3): super(Text2SemanticDecoder, self).__init__() self.model_dim = config["model"]["hidden_dim"] self.embedding_dim = config["model"]["embedding_dim"] self.num_head = config["model"]["head"] self.num_layers = config["model"]["n_layer"] self.norm_first = norm_first self.vocab_size = config["model"]["vocab_size"] self.phoneme_vocab_size = config["model"]["phoneme_vocab_size"] self.p_dropout = float(config["model"]["dropout"]) self.EOS = config["model"]["EOS"] self.norm_first = norm_first assert self.EOS == self.vocab_size - 1 self.bert_proj = nn.Linear(1024, self.embedding_dim) self.ar_text_embedding = TokenEmbedding(self.embedding_dim, self.phoneme_vocab_size, self.p_dropout) self.ar_text_position = SinePositionalEmbedding(self.embedding_dim, dropout=0.1, scale=False, alpha=True) self.ar_audio_embedding = TokenEmbedding(self.embedding_dim, self.vocab_size, self.p_dropout) self.ar_audio_position = SinePositionalEmbedding(self.embedding_dim, dropout=0.1, scale=False, alpha=True) self.h = TransformerEncoder( TransformerEncoderLayer( d_model=self.model_dim, nhead=self.num_head, dim_feedforward=self.model_dim * 4, dropout=0.1, batch_first=True, norm_first=norm_first, ), num_layers=self.num_layers, norm=LayerNorm(self.model_dim) if norm_first else None, ) self.ar_predict_layer = nn.Linear(self.model_dim, self.vocab_size, bias=False) self.loss_fct = nn.CrossEntropyLoss(reduction="sum") self.ar_accuracy_metric = MulticlassAccuracy( self.vocab_size, top_k=top_k, average="micro", multidim_average="global", ignore_index=self.EOS, ) self.top_k = torch.LongTensor([1]) self.early_stop_num = torch.LongTensor([-1])
def init_onnx(self): self.onnx_encoder = OnnxEncoder(self.ar_text_embedding, self.bert_proj, self.ar_text_position) self.first_stage_decoder = T2SFirstStageDecoder(self.ar_audio_embedding, self.ar_audio_position, self.h, self.ar_predict_layer, self.loss_fct, self.ar_accuracy_metric, self.top_k, self.early_stop_num, self.num_layers) self.stage_decoder = T2SStageDecoder(self.ar_audio_embedding, self.ar_audio_position, self.h, self.ar_predict_layer, self.loss_fct, self.ar_accuracy_metric, self.top_k, self.early_stop_num, self.num_layers)
def forward(self, x, prompts, bert_feature): early_stop_num = self.early_stop_num prefix_len = prompts.shape[1]
x = self.onnx_encoder(x, bert_feature) y, k, v, y_emb, stage, x_example = self.first_stage_decoder(x, prompts)
stop = False for idx in range(1, 1500): enco = self.stage_decoder(y, k, v, y_emb, stage, x_example) y, k, v, y_emb, stage, logits, samples = enco if early_stop_num != -1 and (y.shape[1] - prefix_len) > early_stop_num: stop = True if torch.argmax(logits, dim=-1)[0] == self.EOS or samples[0, 0] == self.EOS: stop = True if stop: break y[0, -1] = 0 return y, idx
def infer(self, x, prompts, bert_feature): top_k = self.top_k early_stop_num = self.early_stop_num
x = self.onnx_encoder(x, bert_feature)
y = prompts prefix_len = y.shape[1] x_len = x.shape[1] x_example = x[:,:,0] * 0.0 x_attn_mask = torch.matmul(x_example.transpose(0, 1), x_example) x_attn_mask = torch.zeros_like(x_attn_mask, dtype=torch.bool)
stop = False cache = { "all_stage": self.num_layers, "k": [None] * self.num_layers, "v": [None] * self.num_layers, "y_emb": None, "first_infer": 1, "stage": 0, } for idx in range(1500): if cache["first_infer"] == 1: y_emb = self.ar_audio_embedding(y) else: y_emb = torch.cat( [cache["y_emb"], self.ar_audio_embedding(y[:, -1:])], 1 ) cache["y_emb"] = y_emb y_pos = self.ar_audio_position(y_emb) if cache["first_infer"] == 1: xy_pos = torch.concat([x, y_pos], dim=1) else: xy_pos = y_pos[:, -1:] y_len = y_pos.shape[1] if cache["first_infer"] == 1: x_attn_mask_pad = F.pad(x_attn_mask, (0, y_len), value=True) y_attn_mask = F.pad( torch.triu(torch.ones(y_len, y_len, dtype=torch.bool), diagonal=1), (x_len, 0), value=False ) xy_attn_mask = torch.concat([x_attn_mask_pad, y_attn_mask], dim=0) else: xy_attn_mask = torch.zeros((1, x_len + y_len), dtype=torch.bool) xy_dec = self.h(xy_pos, mask=xy_attn_mask, cache=cache) logits = self.ar_predict_layer(xy_dec[:, -1]) samples = sample(logits[0], y, top_k=top_k, top_p=1.0, repetition_penalty=1.35)[0].unsqueeze(0) if early_stop_num != -1 and (y.shape[1] - prefix_len) > early_stop_num: stop = True if torch.argmax(logits, dim=-1)[0] == self.EOS or samples[0, 0] == self.EOS: stop = True if stop: if prompts.shape[1] == y.shape[1]: y = torch.concat([y, torch.zeros_like(samples)], dim=1) break y = torch.concat([y, samples], dim=1) cache["first_infer"] = 0 return y, idx123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/models/utils.py# reference: https://github.com/lifeiteng/vall-eimport torchimport torch.nn.functional as Ffrom typing import Tuple
def sequence_mask(length, max_length=None): if max_length is None: max_length = length.max() x = torch.arange(max_length, dtype=length.dtype, device=length.device) return x.unsqueeze(0) < length.unsqueeze(1)
def make_pad_mask(lengths: torch.Tensor, max_len: int = 0) -> torch.Tensor: """ Args: lengths: A 1-D tensor containing sentence lengths. max_len: The length of masks. Returns: Return a 2-D bool tensor, where masked positions are filled with `True` and non-masked positions are filled with `False`.
#>>> lengths = torch.tensor([1, 3, 2, 5]) #>>> make_pad_mask(lengths) tensor([[False, True, True, True, True], [False, False, False, True, True], [False, False, True, True, True], [False, False, False, False, False]]) """ assert lengths.ndim == 1, lengths.ndim max_len = max(max_len, lengths.max()) n = lengths.size(0) seq_range = torch.arange(0, max_len, device=lengths.device) expaned_lengths = seq_range.unsqueeze(0).expand(n, max_len)
return expaned_lengths >= lengths.unsqueeze(-1)
# https://github.com/microsoft/unilm/blob/master/xtune/src/transformers/modeling_utils.pydef top_k_top_p_filtering( logits, top_k=0, top_p=1.0, filter_value=-float("Inf"), min_tokens_to_keep=1): """Filter a distribution of logits using top-k and/or nucleus (top-p) filtering Args: logits: logits distribution shape (batch size, vocabulary size) if top_k > 0: keep only top k tokens with highest probability (top-k filtering). if top_p < 1.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering). Nucleus filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751) Make sure we keep at least min_tokens_to_keep per batch example in the output From: https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317 """ if top_k > 0: top_k = min(max(top_k, min_tokens_to_keep), logits.size(-1)) # Safety check # Remove all tokens with a probability less than the last token of the top-k indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None] logits[indices_to_remove] = filter_value
if top_p < 1.0: sorted_logits, sorted_indices = torch.sort(logits, descending=True) cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
# Remove tokens with cumulative probability above the threshold (token with 0 are kept) sorted_indices_to_remove = cumulative_probs > top_p if min_tokens_to_keep > 1: # Keep at least min_tokens_to_keep (set to min_tokens_to_keep-1 because we add the first one below) sorted_indices_to_remove[..., :min_tokens_to_keep] = 0 # Shift the indices to the right to keep also the first token above the threshold sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone() sorted_indices_to_remove[..., 0] = 0
# scatter sorted tensors to original indexing indices_to_remove = sorted_indices_to_remove.scatter( 1, sorted_indices, sorted_indices_to_remove ) logits[indices_to_remove] = filter_value return logits
def topk_sampling(logits, top_k=10, top_p=1.0, temperature=1.0): # temperature: (`optional`) float # The value used to module the next token probabilities. Must be strictly positive. Default to 1.0. # top_k: (`optional`) int # The number of highest probability vocabulary tokens to keep for top-k-filtering. Between 1 and infinity. Default to 50. # top_p: (`optional`) float # The cumulative probability of parameter highest probability vocabulary tokens to keep for nucleus sampling. Must be between 0 and 1. Default to 1.
# Temperature (higher temperature => more likely to sample low probability tokens) if temperature != 1.0: logits = logits / temperature # Top-p/top-k filtering logits = top_k_top_p_filtering(logits, top_k=top_k, top_p=top_p) # Sample token = torch.multinomial(F.softmax(logits, dim=-1), num_samples=1) return token
from typing import Optional, Tuple
def multinomial_sample_one_no_sync( probs_sort,): # Does multinomial sampling without a cuda synchronization q = torch.empty_like(probs_sort).exponential_(1) return torch.argmax(probs_sort / q, dim=-1, keepdim=True).to(dtype=torch.int)
def logits_to_probs( logits, previous_tokens: Optional[torch.Tensor] = None, temperature: float = 1.0, top_k: Optional[int] = None, top_p: Optional[int] = None, repetition_penalty: float = 1.0,): # if previous_tokens is not None: # previous_tokens = previous_tokens.squeeze() # print(logits.shape,previous_tokens.shape) # pdb.set_trace() if previous_tokens is not None and repetition_penalty != 1.0: previous_tokens = previous_tokens.long() score = torch.gather(logits, dim=1, index=previous_tokens) score = torch.where( score < 0, score * repetition_penalty, score / repetition_penalty ) logits.scatter_(dim=1, index=previous_tokens, src=score)
if top_p is not None and top_p < 1.0: sorted_logits, sorted_indices = torch.sort(logits, descending=True) cum_probs = torch.cumsum( torch.nn.functional.softmax(sorted_logits, dim=-1), dim=-1 ) sorted_indices_to_remove = cum_probs > top_p sorted_indices_to_remove[:, 0] = False # keep at least one option indices_to_remove = sorted_indices_to_remove.scatter( dim=1, index=sorted_indices, src=sorted_indices_to_remove ) logits = logits.masked_fill(indices_to_remove, -float("Inf"))
logits = logits / max(temperature, 1e-5)
if top_k is not None: v, _ = torch.topk(logits, min(top_k, logits.size(-1))) pivot = v[: , -1].unsqueeze(-1) logits = torch.where(logits < pivot, -float("Inf"), logits)
probs = torch.nn.functional.softmax(logits, dim=-1) return probs
def sample( logits, previous_tokens: Optional[torch.Tensor] = None, **sampling_kwargs,) -> Tuple[torch.Tensor, torch.Tensor]: probs = logits_to_probs( logits=logits, previous_tokens=previous_tokens, **sampling_kwargs ) idx_next = multinomial_sample_one_no_sync(probs) return idx_next, probs
def dpo_loss(policy_chosen_logps: torch.FloatTensor, policy_rejected_logps: torch.FloatTensor, reference_chosen_logps: torch.FloatTensor, reference_rejected_logps: torch.FloatTensor, beta: float, reference_free: bool = False) -> Tuple[torch.FloatTensor, torch.FloatTensor, torch.FloatTensor]: pi_logratios = policy_chosen_logps - policy_rejected_logps ref_logratios = reference_chosen_logps - reference_rejected_logps
if reference_free: ref_logratios = 0
logits = pi_logratios - ref_logratios
losses = -F.logsigmoid(beta * logits) chosen_rewards = beta * (policy_chosen_logps - reference_chosen_logps).detach() rejected_rewards = beta * (policy_rejected_logps - reference_rejected_logps).detach()
return losses.mean(), chosen_rewards, rejected_rewards
def get_batch_logps(logits_target: torch.FloatTensor, logits_reject: torch.FloatTensor, labels_target: torch.LongTensor, labels_reject: torch.LongTensor, average_log_prob: bool = False) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
# dummy token; we'll ignore the losses on these tokens later
per_token_logps_target = torch.gather(logits_target.log_softmax(-1), dim=2, index=labels_target.unsqueeze(2)).squeeze(2) per_token_logps_reject = torch.gather(logits_reject.log_softmax(-1), dim=2, index=labels_reject.unsqueeze(2)).squeeze(2)
return per_token_logps_target.sum(-1), per_token_logps_reject.sum(-1)
def make_reject_y(y_o, y_lens): def repeat_P(y): range_idx, _ = torch.randint(0, len(y), size=(2,)).sort() pre = y[:range_idx[0]] shf = y[range_idx[1]:] range_text = y[range_idx[0]:range_idx[1]] new_y = torch.cat([pre, range_text, range_text, shf]) return new_y def lost_P(y): range_idx, _ = torch.randint(0, len(y), size=(2,)).sort() pre = y[:range_idx[0]] shf = y[range_idx[1]:] range_text = y[range_idx[0]:range_idx[1]] new_y = torch.cat([pre, shf]) return new_y bs = len(y_lens) reject_y = [] reject_y_lens = [] for b in range(bs): process_item_idx = torch.randint(0, 1, size=(1, ))[0] if process_item_idx == 0: new_y = repeat_P(y_o[b]) reject_y.append(new_y) reject_y_lens.append(len(new_y)) elif process_item_idx==1: new_y = lost_P(y_o[b]) reject_y.append(new_y) reject_y_lens.append(len(new_y)) max_length = max(reject_y_lens) for b in range(bs): pad_length = max_length - reject_y_lens[b] reject_y[b] = torch.cat([reject_y[b], torch.zeros(pad_length, dtype=y_o.dtype, device=y_o.device)], dim=0)
reject_y = torch.stack(reject_y, dim = 0) reject_y_lens = torch.tensor(reject_y_lens, device=y_lens.device)
return reject_y, reject_y_lens123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/activation.pyfrom typing import Optionalfrom typing import Tupleimport torchfrom torch import Tensorfrom torch.nn import Linearfrom torch.nn import Modulefrom torch.nn.init import constant_from torch.nn.init import xavier_normal_from torch.nn.init import xavier_uniform_from torch.nn.modules.linear import NonDynamicallyQuantizableLinearfrom torch.nn.parameter import Parameter
from torch.nn import functional as Ffrom AR.modules.patched_mha_with_cache import multi_head_attention_forward_patched
F.multi_head_attention_forward = multi_head_attention_forward_patched
class MultiheadAttention(Module): r"""Allows the model to jointly attend to information from different representation subspaces as described in the paper: `Attention Is All You Need <https://arxiv.org/abs/1706.03762>`_.
Multi-Head Attention is defined as:
.. math:: \text{MultiHead}(Q, K, V) = \text{Concat}(head_1,\dots,head_h)W^O
where :math:`head_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)`.
``forward()`` will use a special optimized implementation if all of the following conditions are met:
- self attention is being computed (i.e., ``query``, ``key``, and ``value`` are the same tensor. This restriction will be loosened in the future.) - Either autograd is disabled (using ``torch.inference_mode`` or ``torch.no_grad``) or no tensor argument ``requires_grad`` - training is disabled (using ``.eval()``) - dropout is 0 - ``add_bias_kv`` is ``False`` - ``add_zero_attn`` is ``False`` - ``batch_first`` is ``True`` and the input is batched - ``kdim`` and ``vdim`` are equal to ``embed_dim`` - at most one of ``key_padding_mask`` or ``attn_mask`` is passed - if a `NestedTensor <https://pytorch.org/docs/stable/nested.html>`_ is passed, neither ``key_padding_mask`` nor ``attn_mask`` is passed
If the optimized implementation is in use, a `NestedTensor <https://pytorch.org/docs/stable/nested.html>`_ can be passed for ``query``/``key``/``value`` to represent padding more efficiently than using a padding mask. In this case, a `NestedTensor <https://pytorch.org/docs/stable/nested.html>`_ will be returned, and an additional speedup proportional to the fraction of the input that is padding can be expected.
Args: embed_dim: Total dimension of the model. num_heads: Number of parallel attention heads. Note that ``embed_dim`` will be split across ``num_heads`` (i.e. each head will have dimension ``embed_dim // num_heads``). dropout: Dropout probability on ``attn_output_weights``. Default: ``0.0`` (no dropout). bias: If specified, adds bias to input / output projection layers. Default: ``True``. add_bias_kv: If specified, adds bias to the key and value sequences at dim=0. Default: ``False``. add_zero_attn: If specified, adds a new batch of zeros to the key and value sequences at dim=1. Default: ``False``. kdim: Total number of features for keys. Default: ``None`` (uses ``kdim=embed_dim``). vdim: Total number of features for values. Default: ``None`` (uses ``vdim=embed_dim``). batch_first: If ``True``, then the input and output tensors are provided as (batch, seq, feature). Default: ``False`` (seq, batch, feature).
Examples::
>>> # xdoctest: +SKIP >>> multihead_attn = nn.MultiheadAttention(embed_dim, num_heads) >>> attn_output, attn_output_weights = multihead_attn(query, key, value)
""" __constants__ = ["batch_first"] bias_k: Optional[torch.Tensor] bias_v: Optional[torch.Tensor]
def __init__( self, embed_dim, num_heads, dropout=0.0, bias=True, add_bias_kv=False, add_zero_attn=False, kdim=None, vdim=None, batch_first=False, linear1_cls=Linear, linear2_cls=Linear, device=None, dtype=None, ) -> None: factory_kwargs = {"device": device, "dtype": dtype} super(MultiheadAttention, self).__init__() self.embed_dim = embed_dim self.kdim = kdim if kdim is not None else embed_dim self.vdim = vdim if vdim is not None else embed_dim self._qkv_same_embed_dim = self.kdim == embed_dim and self.vdim == embed_dim
self.num_heads = num_heads self.dropout = dropout self.batch_first = batch_first self.head_dim = embed_dim // num_heads assert ( self.head_dim * num_heads == self.embed_dim ), "embed_dim must be divisible by num_heads"
if add_bias_kv: self.bias_k = Parameter(torch.empty((1, 1, embed_dim), **factory_kwargs)) self.bias_v = Parameter(torch.empty((1, 1, embed_dim), **factory_kwargs)) else: self.bias_k = self.bias_v = None
if linear1_cls == Linear: if not self._qkv_same_embed_dim: self.q_proj_weight = Parameter( torch.empty((embed_dim, embed_dim), **factory_kwargs) ) self.k_proj_weight = Parameter( torch.empty((embed_dim, self.kdim), **factory_kwargs) ) self.v_proj_weight = Parameter( torch.empty((embed_dim, self.vdim), **factory_kwargs) ) self.register_parameter("in_proj_weight", None) else: self.in_proj_weight = Parameter( torch.empty((3 * embed_dim, embed_dim), **factory_kwargs) ) self.register_parameter("q_proj_weight", None) self.register_parameter("k_proj_weight", None) self.register_parameter("v_proj_weight", None)
if bias: self.in_proj_bias = Parameter( torch.empty(3 * embed_dim, **factory_kwargs) ) else: self.register_parameter("in_proj_bias", None) self.out_proj = NonDynamicallyQuantizableLinear( embed_dim, embed_dim, bias=bias, **factory_kwargs )
self._reset_parameters() else: if not self._qkv_same_embed_dim: raise NotImplementedError else: self.in_proj_linear = linear1_cls( embed_dim, 3 * embed_dim, bias=bias, **factory_kwargs ) self.in_proj_weight = self.in_proj_linear.weight
self.register_parameter("q_proj_weight", None) self.register_parameter("k_proj_weight", None) self.register_parameter("v_proj_weight", None)
if bias: self.in_proj_bias = self.in_proj_linear.bias else: self.register_parameter("in_proj_bias", None)
self.out_proj = linear2_cls( embed_dim, embed_dim, bias=bias, **factory_kwargs )
if self.bias_k is not None: xavier_normal_(self.bias_k) if self.bias_v is not None: xavier_normal_(self.bias_v)
self.add_zero_attn = add_zero_attn
def _reset_parameters(self): if self._qkv_same_embed_dim: xavier_uniform_(self.in_proj_weight) else: xavier_uniform_(self.q_proj_weight) xavier_uniform_(self.k_proj_weight) xavier_uniform_(self.v_proj_weight)
if self.in_proj_bias is not None: constant_(self.in_proj_bias, 0.0) constant_(self.out_proj.bias, 0.0)
if self.bias_k is not None: xavier_normal_(self.bias_k) if self.bias_v is not None: xavier_normal_(self.bias_v)
def __setstate__(self, state): # Support loading old MultiheadAttention checkpoints generated by v1.1.0 if "_qkv_same_embed_dim" not in state: state["_qkv_same_embed_dim"] = True
super(MultiheadAttention, self).__setstate__(state)
def forward( self, query: Tensor, key: Tensor, value: Tensor, key_padding_mask: Optional[Tensor] = None, need_weights: bool = True, attn_mask: Optional[Tensor] = None, average_attn_weights: bool = True, cache=None, ) -> Tuple[Tensor, Optional[Tensor]]: r""" Args: query: Query embeddings of shape :math:`(L, E_q)` for unbatched input, :math:`(L, N, E_q)` when ``batch_first=False`` or :math:`(N, L, E_q)` when ``batch_first=True``, where :math:`L` is the target sequence length, :math:`N` is the batch size, and :math:`E_q` is the query embedding dimension ``embed_dim``. Queries are compared against key-value pairs to produce the output. See "Attention Is All You Need" for more details. key: Key embeddings of shape :math:`(S, E_k)` for unbatched input, :math:`(S, N, E_k)` when ``batch_first=False`` or :math:`(N, S, E_k)` when ``batch_first=True``, where :math:`S` is the source sequence length, :math:`N` is the batch size, and :math:`E_k` is the key embedding dimension ``kdim``. See "Attention Is All You Need" for more details. value: Value embeddings of shape :math:`(S, E_v)` for unbatched input, :math:`(S, N, E_v)` when ``batch_first=False`` or :math:`(N, S, E_v)` when ``batch_first=True``, where :math:`S` is the source sequence length, :math:`N` is the batch size, and :math:`E_v` is the value embedding dimension ``vdim``. See "Attention Is All You Need" for more details. key_padding_mask: If specified, a mask of shape :math:`(N, S)` indicating which elements within ``key`` to ignore for the purpose of attention (i.e. treat as "padding"). For unbatched `query`, shape should be :math:`(S)`. Binary and byte masks are supported. For a binary mask, a ``True`` value indicates that the corresponding ``key`` value will be ignored for the purpose of attention. For a float mask, it will be directly added to the corresponding ``key`` value. need_weights: If specified, returns ``attn_output_weights`` in addition to ``attn_outputs``. Default: ``True``. attn_mask: If specified, a 2D or 3D mask preventing attention to certain positions. Must be of shape :math:`(L, S)` or :math:`(N\cdot\text{num\_heads}, L, S)`, where :math:`N` is the batch size, :math:`L` is the target sequence length, and :math:`S` is the source sequence length. A 2D mask will be broadcasted across the batch while a 3D mask allows for a different mask for each entry in the batch. Binary, byte, and float masks are supported. For a binary mask, a ``True`` value indicates that the corresponding position is not allowed to attend. For a byte mask, a non-zero value indicates that the corresponding position is not allowed to attend. For a float mask, the mask values will be added to the attention weight. average_attn_weights: If true, indicates that the returned ``attn_weights`` should be averaged across heads. Otherwise, ``attn_weights`` are provided separately per head. Note that this flag only has an effect when ``need_weights=True``. Default: ``True`` (i.e. average weights across heads)
Outputs: - **attn_output** - Attention outputs of shape :math:`(L, E)` when input is unbatched, :math:`(L, N, E)` when ``batch_first=False`` or :math:`(N, L, E)` when ``batch_first=True``, where :math:`L` is the target sequence length, :math:`N` is the batch size, and :math:`E` is the embedding dimension ``embed_dim``. - **attn_output_weights** - Only returned when ``need_weights=True``. If ``average_attn_weights=True``, returns attention weights averaged across heads of shape :math:`(L, S)` when input is unbatched or :math:`(N, L, S)`, where :math:`N` is the batch size, :math:`L` is the target sequence length, and :math:`S` is the source sequence length. If ``average_attn_weights=False``, returns attention weights per head of shape :math:`(\text{num\_heads}, L, S)` when input is unbatched or :math:`(N, \text{num\_heads}, L, S)`.
.. note:: `batch_first` argument is ignored for unbatched inputs. """ is_batched = query.dim() == 3 if key_padding_mask is not None: _kpm_dtype = key_padding_mask.dtype if _kpm_dtype != torch.bool and not torch.is_floating_point( key_padding_mask ): raise AssertionError( "only bool and floating types of key_padding_mask are supported" ) why_not_fast_path = "" if not is_batched: why_not_fast_path = ( f"input not batched; expected query.dim() of 3 but got {query.dim()}" ) elif query is not key or key is not value: # When lifting this restriction, don't forget to either # enforce that the dtypes all match or test cases where # they don't! why_not_fast_path = "non-self attention was used (query, key, and value are not the same Tensor)" elif self.in_proj_bias is not None and query.dtype != self.in_proj_bias.dtype: why_not_fast_path = f"dtypes of query ({query.dtype}) and self.in_proj_bias ({self.in_proj_bias.dtype}) don't match" elif ( self.in_proj_weight is not None and query.dtype != self.in_proj_weight.dtype ): # this case will fail anyway, but at least they'll get a useful error message. why_not_fast_path = f"dtypes of query ({query.dtype}) and self.in_proj_weight ({self.in_proj_weight.dtype}) don't match" elif self.training: why_not_fast_path = "training is enabled" elif not self.batch_first: why_not_fast_path = "batch_first was not True" elif self.bias_k is not None: why_not_fast_path = "self.bias_k was not None" elif self.bias_v is not None: why_not_fast_path = "self.bias_v was not None" elif self.dropout: why_not_fast_path = f"dropout was {self.dropout}, required zero" elif self.add_zero_attn: why_not_fast_path = "add_zero_attn was enabled" elif not self._qkv_same_embed_dim: why_not_fast_path = "_qkv_same_embed_dim was not True" elif attn_mask is not None: why_not_fast_path = "attn_mask was not None" elif query.is_nested and key_padding_mask is not None: why_not_fast_path = ( "key_padding_mask is not supported with NestedTensor input" ) elif self.num_heads % 2 == 1: why_not_fast_path = "num_heads is odd" elif torch.is_autocast_enabled(): why_not_fast_path = "autocast is enabled"
if not why_not_fast_path: tensor_args = ( query, key, value, self.in_proj_weight, self.in_proj_bias, self.out_proj.weight, self.out_proj.bias, ) # We have to use list comprehensions below because TorchScript does not support # generator expressions. if torch.overrides.has_torch_function(tensor_args): why_not_fast_path = "some Tensor argument has_torch_function" elif not all( [ (x is None or x.is_cuda or "cpu" in str(x.device)) for x in tensor_args ] ): why_not_fast_path = "some Tensor argument is neither CUDA nor CPU" elif torch.is_grad_enabled() and any( [x is not None and x.requires_grad for x in tensor_args] ): why_not_fast_path = ( "grad is enabled and at least one of query or the " "input/output projection weights or biases requires_grad" ) if not why_not_fast_path: return torch._native_multi_head_attention( query, key, value, self.embed_dim, self.num_heads, self.in_proj_weight, self.in_proj_bias, self.out_proj.weight, self.out_proj.bias, key_padding_mask if key_padding_mask is not None else attn_mask, need_weights, average_attn_weights, 1 if key_padding_mask is not None else 0 if attn_mask is not None else None, )
any_nested = query.is_nested or key.is_nested or value.is_nested assert not any_nested, ( "MultiheadAttention does not support NestedTensor outside of its fast path. " + f"The fast path was not hit because {why_not_fast_path}" )
if self.batch_first and is_batched: # make sure that the transpose op does not affect the "is" property if key is value: if query is key: query = key = value = query.transpose(1, 0) else: query, key = [x.transpose(1, 0) for x in (query, key)] value = key else: query, key, value = [x.transpose(1, 0) for x in (query, key, value)]
if not self._qkv_same_embed_dim: attn_output, attn_output_weights = F.multi_head_attention_forward( query, key, value, self.embed_dim, self.num_heads, self.in_proj_weight, self.in_proj_bias, self.bias_k, self.bias_v, self.add_zero_attn, self.dropout, self.out_proj.weight, self.out_proj.bias, training=self.training, key_padding_mask=key_padding_mask, need_weights=need_weights, attn_mask=attn_mask, use_separate_proj_weight=True, q_proj_weight=self.q_proj_weight, k_proj_weight=self.k_proj_weight, v_proj_weight=self.v_proj_weight, average_attn_weights=average_attn_weights, cache=cache, ) else: attn_output, attn_output_weights = F.multi_head_attention_forward( query, key, value, self.embed_dim, self.num_heads, self.in_proj_weight, self.in_proj_bias, self.bias_k, self.bias_v, self.add_zero_attn, self.dropout, self.out_proj.weight, self.out_proj.bias, training=self.training, key_padding_mask=key_padding_mask, need_weights=need_weights, attn_mask=attn_mask, average_attn_weights=average_attn_weights, cache=cache, ) if self.batch_first and is_batched: return attn_output.transpose(1, 0), attn_output_weights else: return attn_output, attn_output_weights123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/activation.pyfrom typing import Optionalfrom typing import Tupleimport torchfrom torch import Tensorfrom torch.nn import Linearfrom torch.nn import Modulefrom torch.nn.init import constant_from torch.nn.init import xavier_normal_from torch.nn.init import xavier_uniform_from torch.nn.modules.linear import NonDynamicallyQuantizableLinearfrom torch.nn.parameter import Parameter
from torch.nn import functional as Ffrom AR.modules.patched_mha_with_cache_onnx import multi_head_attention_forward_patched
class MultiheadAttention(Module): __constants__ = ["batch_first"] bias_k: Optional[torch.Tensor] bias_v: Optional[torch.Tensor]
def __init__( self, embed_dim, num_heads, dropout=0.0, bias=True, add_bias_kv=False, add_zero_attn=False, kdim=None, vdim=None, batch_first=False, linear1_cls=Linear, linear2_cls=Linear, device=None, dtype=None, ) -> None: factory_kwargs = {"device": device, "dtype": dtype} super(MultiheadAttention, self).__init__() self.embed_dim = embed_dim self.kdim = kdim if kdim is not None else embed_dim self.vdim = vdim if vdim is not None else embed_dim self._qkv_same_embed_dim = self.kdim == embed_dim and self.vdim == embed_dim
self.num_heads = num_heads self.dropout = dropout self.batch_first = batch_first self.head_dim = embed_dim // num_heads assert ( self.head_dim * num_heads == self.embed_dim ), "embed_dim must be divisible by num_heads"
if add_bias_kv: self.bias_k = Parameter(torch.empty((1, 1, embed_dim), **factory_kwargs)) self.bias_v = Parameter(torch.empty((1, 1, embed_dim), **factory_kwargs)) else: self.bias_k = self.bias_v = None
if linear1_cls == Linear: if not self._qkv_same_embed_dim: self.q_proj_weight = Parameter( torch.empty((embed_dim, embed_dim), **factory_kwargs) ) self.k_proj_weight = Parameter( torch.empty((embed_dim, self.kdim), **factory_kwargs) ) self.v_proj_weight = Parameter( torch.empty((embed_dim, self.vdim), **factory_kwargs) ) self.register_parameter("in_proj_weight", None) else: self.in_proj_weight = Parameter( torch.empty((3 * embed_dim, embed_dim), **factory_kwargs) ) self.register_parameter("q_proj_weight", None) self.register_parameter("k_proj_weight", None) self.register_parameter("v_proj_weight", None)
if bias: self.in_proj_bias = Parameter( torch.empty(3 * embed_dim, **factory_kwargs) ) else: self.register_parameter("in_proj_bias", None) self.out_proj = NonDynamicallyQuantizableLinear( embed_dim, embed_dim, bias=bias, **factory_kwargs )
self._reset_parameters() else: if not self._qkv_same_embed_dim: raise NotImplementedError else: self.in_proj_linear = linear1_cls( embed_dim, 3 * embed_dim, bias=bias, **factory_kwargs ) self.in_proj_weight = self.in_proj_linear.weight
self.register_parameter("q_proj_weight", None) self.register_parameter("k_proj_weight", None) self.register_parameter("v_proj_weight", None)
if bias: self.in_proj_bias = self.in_proj_linear.bias else: self.register_parameter("in_proj_bias", None)
self.out_proj = linear2_cls( embed_dim, embed_dim, bias=bias, **factory_kwargs )
if self.bias_k is not None: xavier_normal_(self.bias_k) if self.bias_v is not None: xavier_normal_(self.bias_v)
self.add_zero_attn = add_zero_attn
def _reset_parameters(self): if self._qkv_same_embed_dim: xavier_uniform_(self.in_proj_weight) else: xavier_uniform_(self.q_proj_weight) xavier_uniform_(self.k_proj_weight) xavier_uniform_(self.v_proj_weight)
if self.in_proj_bias is not None: constant_(self.in_proj_bias, 0.0) constant_(self.out_proj.bias, 0.0)
if self.bias_k is not None: xavier_normal_(self.bias_k) if self.bias_v is not None: xavier_normal_(self.bias_v)
def __setstate__(self, state): # Support loading old MultiheadAttention checkpoints generated by v1.1.0 if "_qkv_same_embed_dim" not in state: state["_qkv_same_embed_dim"] = True
super(MultiheadAttention, self).__setstate__(state)
def forward( self, query: Tensor, key: Tensor, value: Tensor, key_padding_mask: Optional[Tensor] = None, need_weights: bool = True, attn_mask: Optional[Tensor] = None, average_attn_weights: bool = True, cache=None, ) -> Tuple[Tensor, Optional[Tensor]]: any_nested = query.is_nested or key.is_nested or value.is_nested query = key = value = query.transpose(1, 0) attn_output = multi_head_attention_forward_patched( query, key, value, self.embed_dim, self.num_heads, self.in_proj_weight, self.in_proj_bias, self.bias_k, self.bias_v, self.add_zero_attn, self.dropout, self.out_proj.weight, self.out_proj.bias, training=self.training, key_padding_mask=key_padding_mask, need_weights=need_weights, attn_mask=attn_mask, average_attn_weights=average_attn_weights, cache=cache, ) return attn_output.transpose(1, 0)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/embedding.pyimport math
import torchfrom torch import nn
class TokenEmbedding(nn.Module): def __init__( self, embedding_dim: int, vocab_size: int, dropout: float = 0.0, ): super().__init__()
self.vocab_size = vocab_size self.embedding_dim = embedding_dim
self.dropout = torch.nn.Dropout(p=dropout) self.word_embeddings = nn.Embedding(self.vocab_size, self.embedding_dim)
@property def weight(self) -> torch.Tensor: return self.word_embeddings.weight
def embedding(self, index: int) -> torch.Tensor: return self.word_embeddings.weight[index : index + 1]
def forward(self, x: torch.Tensor): x = self.word_embeddings(x) x = self.dropout(x) return x
class SinePositionalEmbedding(nn.Module): def __init__( self, embedding_dim: int, dropout: float = 0.0, scale: bool = False, alpha: bool = False, ): super().__init__() self.embedding_dim = embedding_dim self.x_scale = math.sqrt(embedding_dim) if scale else 1.0 self.alpha = nn.Parameter(torch.ones(1), requires_grad=alpha) self.dropout = torch.nn.Dropout(p=dropout)
self.reverse = False self.pe = None self.extend_pe(torch.tensor(0.0).expand(1, 4000))
def extend_pe(self, x): """Reset the positional encodings.""" if self.pe is not None: if self.pe.size(1) >= x.size(1): if self.pe.dtype != x.dtype or self.pe.device != x.device: self.pe = self.pe.to(dtype=x.dtype, device=x.device) return pe = torch.zeros(x.size(1), self.embedding_dim) if self.reverse: position = torch.arange( x.size(1) - 1, -1, -1.0, dtype=torch.float32 ).unsqueeze(1) else: position = torch.arange(0, x.size(1), dtype=torch.float32).unsqueeze(1) div_term = torch.exp( torch.arange(0, self.embedding_dim, 2, dtype=torch.float32) * -(math.log(10000.0) / self.embedding_dim) ) pe[:, 0::2] = torch.sin(position * div_term) pe[:, 1::2] = torch.cos(position * div_term) pe = pe.unsqueeze(0) self.pe = pe.to(device=x.device, dtype=x.dtype).detach()
def forward(self, x: torch.Tensor) -> torch.Tensor: self.extend_pe(x) output = x.unsqueeze(-1) if x.ndim == 2 else x output = output * self.x_scale + self.alpha * self.pe[:, : x.size(1)] return self.dropout(output)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/embedding.pyimport math
import torchfrom torch import nn
class TokenEmbedding(nn.Module): def __init__( self, embedding_dim: int, vocab_size: int, dropout: float = 0.0, ): super().__init__()
self.vocab_size = vocab_size self.embedding_dim = embedding_dim
self.dropout = torch.nn.Dropout(p=dropout) self.word_embeddings = nn.Embedding(self.vocab_size, self.embedding_dim)
@property def weight(self) -> torch.Tensor: return self.word_embeddings.weight
def embedding(self, index: int) -> torch.Tensor: return self.word_embeddings.weight[index : index + 1]
def forward(self, x: torch.Tensor): x = self.word_embeddings(x) x = self.dropout(x) return x
class SinePositionalEmbedding(nn.Module): def __init__( self, embedding_dim: int, dropout: float = 0.0, scale: bool = False, alpha: bool = False, ): super().__init__() self.embedding_dim = embedding_dim self.x_scale = math.sqrt(embedding_dim) if scale else 1.0 self.alpha = nn.Parameter(torch.ones(1), requires_grad=alpha) self.dropout = torch.nn.Dropout(p=dropout) self.reverse = False self.div_term = torch.exp(torch.arange(0, self.embedding_dim, 2) * -(math.log(10000.0) / self.embedding_dim))
def extend_pe(self, x): position = torch.cumsum(torch.ones_like(x[:,:,0]), dim=1).transpose(0, 1) scpe = (position * self.div_term).unsqueeze(0) pe = torch.cat([torch.sin(scpe), torch.cos(scpe)]).permute(1, 2, 0) pe = pe.contiguous().view(1, -1, self.embedding_dim) return pe
def forward(self, x: torch.Tensor) -> torch.Tensor: pe = self.extend_pe(x) output = x.unsqueeze(-1) if x.ndim == 2 else x output = output * self.x_scale + self.alpha * pe return self.dropout(output)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/modules/lr_schedulers.py# reference: https://github.com/lifeiteng/vall-eimport math
import torchfrom matplotlib import pyplot as pltfrom torch import nnfrom torch.optim import Adam
class WarmupCosineLRSchedule(torch.optim.lr_scheduler._LRScheduler): """ Implements Warmup learning rate schedule until 'warmup_steps', going from 'init_lr' to 'peak_lr' for multiple optimizers. """
def __init__( self, optimizer, init_lr, peak_lr, end_lr, warmup_steps=10000, total_steps=400000, current_step=0, ): self.init_lr = init_lr self.peak_lr = peak_lr self.end_lr = end_lr self.optimizer = optimizer self._warmup_rate = (peak_lr - init_lr) / warmup_steps self._decay_rate = (end_lr - peak_lr) / (total_steps - warmup_steps) self._current_step = current_step self.lr = init_lr self.warmup_steps = warmup_steps self.total_steps = total_steps self._last_lr = [self.lr]
def set_lr(self, lr): self._last_lr = [g["lr"] for g in self.optimizer.param_groups] for g in self.optimizer.param_groups: # g['lr'] = lr g["lr"] = self.end_lr ###锁定用线性
def step(self): if self._current_step < self.warmup_steps: lr = self.init_lr + self._warmup_rate * self._current_step
elif self._current_step > self.total_steps: lr = self.end_lr
else: decay_ratio = (self._current_step - self.warmup_steps) / ( self.total_steps - self.warmup_steps ) if decay_ratio < 0.0 or decay_ratio > 1.0: raise RuntimeError( "Decay ratio must be in [0.0, 1.0]. Fix LR scheduler settings." ) coeff = 0.5 * (1.0 + math.cos(math.pi * decay_ratio)) lr = self.end_lr + coeff * (self.peak_lr - self.end_lr)
self.lr = lr = self.end_lr = 0.002 ###锁定用线性###不听话,直接锁定! self.set_lr(lr) self.lr = lr self._current_step += 1 return self.lr
if __name__ == "__main__": m = nn.Linear(10, 10) opt = Adam(m.parameters(), lr=1e-4) s = WarmupCosineLRSchedule( opt, 1e-6, 2e-4, 1e-6, warmup_steps=2000, total_steps=20000, current_step=0 ) lrs = [] for i in range(25000): s.step() lrs.append(s.lr) print(s.lr)
plt.plot(lrs) plt.plot(range(0, 25000), lrs) plt.show()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622# Copyright 2022 Xiaomi Corp. (authors: Daniel Povey)## See ../LICENSE for clarification regarding multiple authors## 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.import contextlibimport loggingfrom collections import defaultdictfrom typing import Listfrom typing import Tuple
import torchfrom torch import Tensorfrom torch.optim import Optimizer
class BatchedOptimizer(Optimizer): """ This class adds to class Optimizer the capability to optimize parameters in batches: it will stack the parameters and their grads for you so the optimizer can work on tensors with an extra leading dimension. This is intended for speed with GPUs, as it reduces the number of kernels launched in the optimizer.
Args: params: """
def __init__(self, params, defaults): super(BatchedOptimizer, self).__init__(params, defaults)
@contextlib.contextmanager def batched_params(self, param_group, group_params_names): """ This function returns (technically, yields) a list of of tuples (p, state), where p is a `fake` parameter that is stacked (over axis 0) from real parameters that share the same shape, and its gradient is also stacked; `state` is the state corresponding to this batch of parameters (it will be physically located in the "state" for one of the real parameters, the last one that has any particular shape and dtype).
This function is decorated as a context manager so that it can write parameters back to their "real" locations.
The idea is, instead of doing: <code> for p in group["params"]: state = self.state[p] ... </code> you can do: <code> with self.batched_params(group["params"]) as batches: for p, state, p_names in batches: ... </code>
Args: group: a parameter group, which is a list of parameters; should be one of self.param_groups. group_params_names: name for each parameter in group, which is List[str]. """ batches = defaultdict( list ) # `batches` maps from tuple (dtype_as_str,*shape) to list of nn.Parameter batches_names = defaultdict( list ) # `batches` maps from tuple (dtype_as_str,*shape) to list of str
assert len(param_group) == len(group_params_names) for p, named_p in zip(param_group, group_params_names): key = (str(p.dtype), *p.shape) batches[key].append(p) batches_names[key].append(named_p)
batches_names_keys = list(batches_names.keys()) sorted_idx = sorted( range(len(batches_names)), key=lambda i: batches_names_keys[i]) batches_names = [ batches_names[batches_names_keys[idx]] for idx in sorted_idx ] batches = [batches[batches_names_keys[idx]] for idx in sorted_idx]
stacked_params_dict = dict()
# turn batches into a list, in deterministic order. # tuples will contain tuples of (stacked_param, state, stacked_params_names), # one for each batch in `batches`. tuples = []
for batch, batch_names in zip(batches, batches_names): p = batch[0] # we arbitrarily store the state in the # state corresponding to the 1st parameter in the # group. class Optimizer will take care of saving/loading state. state = self.state[p] p_stacked = torch.stack(batch) grad = torch.stack([ torch.zeros_like(p) if p.grad is None else p.grad for p in batch ]) p_stacked.grad = grad stacked_params_dict[key] = p_stacked tuples.append((p_stacked, state, batch_names))
yield tuples # <-- calling code will do the actual optimization here!
for ((stacked_params, _state, _names), batch) in zip(tuples, batches): for i, p in enumerate(batch): # batch is list of Parameter p.copy_(stacked_params[i])
class ScaledAdam(BatchedOptimizer): """ Implements 'Scaled Adam', a variant of Adam where we scale each parameter's update proportional to the norm of that parameter; and also learn the scale of the parameter, in log space, subject to upper and lower limits (as if we had factored each parameter as param = underlying_param * log_scale.exp())
Args: params: The parameters or param_groups to optimize (like other Optimizer subclasses) lr: The learning rate. We will typically use a learning rate schedule that starts at 0.03 and decreases over time, i.e. much higher than other common optimizers. clipping_scale: (e.g. 2.0) A scale for gradient-clipping: if specified, the normalized gradients over the whole model will be clipped to have 2-norm equal to `clipping_scale` times the median 2-norm over the most recent period of `clipping_update_period` minibatches. By "normalized gradients", we mean after multiplying by the rms parameter value for this tensor [for non-scalars]; this is appropriate because our update is scaled by this quantity. betas: beta1,beta2 are momentum constants for regular momentum, and moving sum-sq grad. Must satisfy 0 < beta <= beta2 < 1. scalar_lr_scale: A scaling factor on the learning rate, that we use to update the scale of each parameter tensor and scalar parameters of the mode.. If each parameter were decomposed as p * p_scale.exp(), where (p**2).mean().sqrt() == 1.0, scalar_lr_scale would be a the scaling factor on the learning rate of p_scale. eps: A general-purpose epsilon to prevent division by zero param_min_rms: Minimum root-mean-square value of parameter tensor, for purposes of learning the scale on the parameters (we'll constrain the rms of each non-scalar parameter tensor to be >= this value) param_max_rms: Maximum root-mean-square value of parameter tensor, for purposes of learning the scale on the parameters (we'll constrain the rms of each non-scalar parameter tensor to be <= this value) scalar_max: Maximum absolute value for scalar parameters (applicable if your model has any parameters with numel() == 1). size_update_period: The periodicity, in steps, with which we update the size (scale) of the parameter tensor. This is provided to save a little time in the update. clipping_update_period: if clipping_scale is specified, this is the period """
def __init__( self, params, lr=3e-02, clipping_scale=None, betas=(0.9, 0.98), scalar_lr_scale=0.1, eps=1.0e-08, param_min_rms=1.0e-05, param_max_rms=3.0, scalar_max=10.0, size_update_period=4, clipping_update_period=100, parameters_names=None, show_dominant_parameters=True, ):
assert parameters_names is not None, ( "Please prepare parameters_names," "which is a List[List[str]]. Each List[str] is for a group" "and each str is for a parameter") defaults = dict( lr=lr, clipping_scale=clipping_scale, betas=betas, scalar_lr_scale=scalar_lr_scale, eps=eps, param_min_rms=param_min_rms, param_max_rms=param_max_rms, scalar_max=scalar_max, size_update_period=size_update_period, clipping_update_period=clipping_update_period, )
super(ScaledAdam, self).__init__(params, defaults) assert len(self.param_groups) == len(parameters_names) self.parameters_names = parameters_names self.show_dominant_parameters = show_dominant_parameters
def __setstate__(self, state): super(ScaledAdam, self).__setstate__(state)
@torch.no_grad() def step(self, closure=None): """Performs a single optimization step.
Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: with torch.enable_grad(): loss = closure()
batch = True
for group, group_params_names in zip(self.param_groups, self.parameters_names):
with self.batched_params(group["params"], group_params_names) as batches:
# batches is list of pairs (stacked_param, state). stacked_param is like # a regular parameter, and will have a .grad, but the 1st dim corresponds to # a stacking dim, it is not a real dim.
if (len(batches[0][1]) == 0): # if len(first state) == 0: not yet initialized clipping_scale = 1 else: clipping_scale = self._get_clipping_scale(group, batches)
for p, state, _ in batches: # Perform optimization step. # grad is not going to be None, we handled that when creating the batches. grad = p.grad if grad.is_sparse: raise RuntimeError( "ScaledAdam optimizer does not support sparse gradients" ) # State initialization if len(state) == 0: self._init_state(group, p, state)
self._step_one_batch(group, p, state, clipping_scale)
return loss
def _init_state(self, group: dict, p: Tensor, state: dict): """ Initializes state dict for parameter 'p'. Assumes that dim 0 of tensor p is actually the batch dimension, corresponding to batched-together parameters of a given shape.
Args: group: Dict to look up configuration values. p: The parameter that we are initializing the state for state: Dict from string to whatever state we are initializing """ size_update_period = group["size_update_period"]
state["step"] = 0
kwargs = {"device": p.device, "dtype": p.dtype}
# 'delta' implements conventional momentum. There are # several different kinds of update going on, so rather than # compute "exp_avg" like in Adam, we store and decay a # parameter-change "delta", which combines all forms of # update. this is equivalent to how it's done in Adam, # except for the first few steps. state["delta"] = torch.zeros_like( p, memory_format=torch.preserve_format)
batch_size = p.shape[0] numel = p.numel() // batch_size numel = p.numel()
if numel > 1: # "param_rms" just periodically records the scalar root-mean-square value of # the parameter tensor. # it has a shape like (batch_size, 1, 1, 1, 1) param_rms = ( (p**2).mean(dim=list(range(1, p.ndim)), keepdim=True).sqrt()) state["param_rms"] = param_rms
state["scale_exp_avg_sq"] = torch.zeros_like(param_rms) state["scale_grads"] = torch.zeros(size_update_period, *param_rms.shape, **kwargs)
# exp_avg_sq is the weighted sum of scaled gradients. as in Adam. state["exp_avg_sq"] = torch.zeros_like( p, memory_format=torch.preserve_format)
def _get_clipping_scale(self, group: dict, tuples: List[Tuple[Tensor, dict, List[str]]] ) -> float: """ Returns a scalar factor <= 1.0 that dictates gradient clipping, i.e. we will scale the gradients by this amount before applying the rest of the update.
Args: group: the parameter group, an item in self.param_groups tuples: a list of tuples of (param, state, param_names) where param is a batched set of parameters, with a .grad (1st dim is batch dim) and state is the state-dict where optimization parameters are kept. param_names is a List[str] while each str is name for a parameter in batched set of parameters "param". """ assert len(tuples) >= 1 clipping_scale = group["clipping_scale"] (first_p, first_state, _) = tuples[0] step = first_state["step"] if clipping_scale is None or step == 0: # no clipping. return early on step == 0 because the other # parameters' state won't have been initialized yet. return 1.0 clipping_update_period = group["clipping_update_period"]
tot_sumsq = torch.tensor(0.0, device=first_p.device) for (p, state, param_names) in tuples: grad = p.grad if grad.is_sparse: raise RuntimeError( "ScaledAdam optimizer does not support sparse gradients") if p.numel() == p.shape[0]: # a batch of scalars tot_sumsq += (grad**2).sum() # sum() to change shape [1] to [] else: tot_sumsq += ((grad * state["param_rms"])**2).sum()
tot_norm = tot_sumsq.sqrt() if "model_norms" not in first_state: first_state["model_norms"] = torch.zeros( clipping_update_period, device=p.device) first_state["model_norms"][step % clipping_update_period] = tot_norm
if step % clipping_update_period == 0: # Print some stats. # We don't reach here if step == 0 because we would have returned # above. sorted_norms = first_state["model_norms"].sort()[0].to("cpu") quartiles = [] for n in range(0, 5): index = min( clipping_update_period - 1, (clipping_update_period // 4) * n, ) quartiles.append(sorted_norms[index].item())
median = quartiles[2] threshold = clipping_scale * median first_state["model_norm_threshold"] = threshold percent_clipped = (first_state["num_clipped"] * 100.0 / clipping_update_period if "num_clipped" in first_state else 0.0) first_state["num_clipped"] = 0 quartiles = " ".join(["%.3e" % x for x in quartiles]) logging.info( f"Clipping_scale={clipping_scale}, grad-norm quartiles {quartiles}, " f"threshold={threshold:.3e}, percent-clipped={percent_clipped:.1f}" )
if step < clipping_update_period: return 1.0 # We have not yet estimated a norm to clip to. else: try: model_norm_threshold = first_state["model_norm_threshold"] except KeyError: logging.info( "Warning: model_norm_threshold not in state: possibly " "you changed config when restarting, adding clipping_scale option?" ) return 1.0 ans = min(1.0, (model_norm_threshold / (tot_norm + 1.0e-20)).item()) if ans < 1.0: first_state["num_clipped"] += 1 if ans < 0.1: logging.warn( f"Scaling gradients by {ans}, model_norm_threshold={model_norm_threshold}" ) if self.show_dominant_parameters: assert p.shape[0] == len(param_names) self._show_gradient_dominating_parameter(tuples, tot_sumsq) return ans
def _show_gradient_dominating_parameter( self, tuples: List[Tuple[Tensor, dict, List[str]]], tot_sumsq: Tensor): """ Show information of parameter wihch dominanting tot_sumsq.
Args: tuples: a list of tuples of (param, state, param_names) where param is a batched set of parameters, with a .grad (1st dim is batch dim) and state is the state-dict where optimization parameters are kept. param_names is a List[str] while each str is name for a parameter in batched set of parameters "param". tot_sumsq: sumsq of all parameters. Though it's could be calculated from tuples, we still pass it to save some time. """ all_sumsq_orig = {} for (p, state, batch_param_names) in tuples: # p is a stacked batch parameters. batch_grad = p.grad if p.numel() == p.shape[0]: # a batch of scalars batch_sumsq_orig = batch_grad**2 # Dummpy values used by following `zip` statement. batch_rms_orig = torch.ones(p.shape[0]) else: batch_rms_orig = state["param_rms"] batch_sumsq_orig = ((batch_grad * batch_rms_orig)**2).sum( dim=list(range(1, batch_grad.ndim)))
for name, sumsq_orig, rms, grad in zip(batch_param_names, batch_sumsq_orig, batch_rms_orig, batch_grad):
proportion_orig = sumsq_orig / tot_sumsq all_sumsq_orig[name] = (proportion_orig, sumsq_orig, rms, grad)
assert torch.isclose( sum([value[0] for value in all_sumsq_orig.values()]).cpu(), torch.tensor(1.0), ) sorted_by_proportion = { k: v for k, v in sorted( all_sumsq_orig.items(), key=lambda item: item[1][0], reverse=True, ) } dominant_param_name = next(iter(sorted_by_proportion)) (dominant_proportion, dominant_sumsq, dominant_rms, dominant_grad, ) = sorted_by_proportion[dominant_param_name] logging.info(f"Parameter Dominanting tot_sumsq {dominant_param_name}" f" with proportion {dominant_proportion:.2f}," f" where dominant_sumsq=(grad_sumsq*orig_rms_sq)" f"={dominant_sumsq:.3e}," f" grad_sumsq = {(dominant_grad**2).sum():.3e}," f" orig_rms_sq={(dominant_rms**2).item():.3e}")
def _step_one_batch(self, group: dict, p: Tensor, state: dict, clipping_scale: float): """ Do the step for one parameter, which is actually going to be a batch of `real` parameters, with dim 0 as the batch dim. Args: group: dict to look up configuration values p: parameter to update (actually multiple parameters stacked together as a batch) state: state-dict for p, to look up the optimizer state """ lr = group["lr"] size_update_period = group["size_update_period"] beta1 = group["betas"][0]
grad = p.grad if clipping_scale != 1.0: grad = grad * clipping_scale step = state["step"] delta = state["delta"]
delta.mul_(beta1) batch_size = p.shape[0] numel = p.numel() // batch_size if numel > 1: # Update the size/scale of p, and set param_rms scale_grads = state["scale_grads"] scale_grads[step % size_update_period] = (p * grad).sum( dim=list(range(1, p.ndim)), keepdim=True) if step % size_update_period == size_update_period - 1: param_rms = state["param_rms"] # shape: (batch_size, 1, 1, ..) param_rms.copy_((p**2) .mean(dim=list(range(1, p.ndim)), keepdim=True) .sqrt()) if step > 0: # self._size_update() learns the overall scale on the # parameter, by shrinking or expanding it. self._size_update(group, scale_grads, p, state)
if numel == 1: # For parameters with 1 element we just use regular Adam. # Updates delta. self._step_scalar(group, p, state) else: self._step(group, p, state)
state["step"] = step + 1
def _size_update(self, group: dict, scale_grads: Tensor, p: Tensor, state: dict) -> None: """ Called only where p.numel() > 1, this updates the scale of the parameter. If we imagine: p = underlying_param * scale.exp(), and we are doing gradient descent on underlying param and on scale, this function does the update on `scale`.
Args: group: dict to look up configuration values scale_grads: a tensor of shape (size_update_period, batch_size, 1, 1,...) containing grads w.r.t. the scales. p: The parameter to update state: The state-dict of p """
param_rms = state["param_rms"] beta1, beta2 = group["betas"] size_lr = group["lr"] * group["scalar_lr_scale"] param_min_rms = group["param_min_rms"] param_max_rms = group["param_max_rms"] eps = group["eps"] step = state["step"] batch_size = p.shape[0]
size_update_period = scale_grads.shape[0] # correct beta2 for the size update period: we will have # faster decay at this level. beta2_corr = beta2**size_update_period
scale_exp_avg_sq = state[ "scale_exp_avg_sq"] # shape: (batch_size, 1, 1, ..) scale_exp_avg_sq.mul_(beta2_corr).add_( (scale_grads**2).mean(dim=0), # mean over dim `size_update_period` alpha=1 - beta2_corr, ) # shape is (batch_size, 1, 1, ...)
# The 1st time we reach here is when size_step == 1. size_step = (step + 1) // size_update_period bias_correction2 = 1 - beta2_corr**size_step # we don't bother with bias_correction1; this will help prevent divergence # at the start of training.
denom = scale_exp_avg_sq.sqrt() + eps
scale_step = (-size_lr * (bias_correction2**0.5) * scale_grads.sum(dim=0) / denom)
is_too_small = param_rms < param_min_rms is_too_large = param_rms > param_max_rms
# when the param gets too small, just don't shrink it any further. scale_step.masked_fill_(is_too_small, 0.0) # when it gets too large, stop it from getting any larger. scale_step.masked_fill_(is_too_large, -size_lr * size_update_period) delta = state["delta"] # the factor of (1-beta1) relates to momentum. delta.add_(p * scale_step, alpha=(1 - beta1))
def _step(self, group: dict, p: Tensor, state: dict): """ This function does the core update of self.step(), in the case where the members of the batch have more than 1 element.
Args: group: A dict which will be used to look up configuration values p: The parameter to be updated grad: The grad of p state: The state-dict corresponding to parameter p
This function modifies p. """ grad = p.grad lr = group["lr"] beta1, beta2 = group["betas"] eps = group["eps"] param_min_rms = group["param_min_rms"] step = state["step"]
exp_avg_sq = state["exp_avg_sq"] exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=(1 - beta2))
this_step = state["step"] - (state["zero_step"] if "zero_step" in state else 0) bias_correction2 = 1 - beta2**(this_step + 1) if bias_correction2 < 0.99: # note: not in-place. exp_avg_sq = exp_avg_sq * (1.0 / bias_correction2)
denom = exp_avg_sq.sqrt() denom += eps grad = grad / denom
alpha = -lr * (1 - beta1) * state["param_rms"].clamp(min=param_min_rms)
delta = state["delta"] delta.add_(grad * alpha) p.add_(delta)
def _step_scalar(self, group: dict, p: Tensor, state: dict): """ A simplified form of the core update for scalar tensors, where we cannot get a good estimate of the parameter rms. """ beta1, beta2 = group["betas"] scalar_max = group["scalar_max"] eps = group["eps"] lr = group["lr"] * group["scalar_lr_scale"] grad = p.grad
exp_avg_sq = state["exp_avg_sq"] # shape: (batch_size,) exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
# bias_correction2 is like in Adam. Don't bother with bias_correction1; # slower update at the start will help stability anyway. bias_correction2 = 1 - beta2**(state["step"] + 1) denom = (exp_avg_sq / bias_correction2).sqrt() + eps
delta = state["delta"] delta.add_(grad / denom, alpha=-lr * (1 - beta1)) p.clamp_(min=-scalar_max, max=scalar_max) p.add_(delta)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465from torch.nn.functional import *
from torch.nn.functional import (
_mha_shape_check,
_canonical_mask,
_none_or_dtype,
_in_projection_packed,
)
from torch.nn import functional as F
import torch
# Tensor = torch.Tensor
# from typing import Callable, List, Optional, Tuple, Union
def multi_head_attention_forward_patched(
query: Tensor,
key: Tensor,
value: Tensor,
embed_dim_to_check: int,
num_heads: int,
in_proj_weight: Optional[Tensor],
in_proj_bias: Optional[Tensor],
bias_k: Optional[Tensor],
bias_v: Optional[Tensor],
add_zero_attn: bool,
dropout_p: float,
out_proj_weight: Tensor,
out_proj_bias: Optional[Tensor],
training: bool = True,
key_padding_mask: Optional[Tensor] = None,
need_weights: bool = True,
attn_mask: Optional[Tensor] = None,
use_separate_proj_weight: bool = False,
q_proj_weight: Optional[Tensor] = None,
k_proj_weight: Optional[Tensor] = None,
v_proj_weight: Optional[Tensor] = None,
static_k: Optional[Tensor] = None,
static_v: Optional[Tensor] = None,
average_attn_weights: bool = True,
is_causal: bool = False,
cache=None,
) -> Tuple[Tensor, Optional[Tensor]]:
r"""
Args:
query, key, value: map a query and a set of key-value pairs to an output.
See "Attention Is All You Need" for more details.
embed_dim_to_check: total dimension of the model.
num_heads: parallel attention heads.
in_proj_weight, in_proj_bias: input projection weight and bias.
bias_k, bias_v: bias of the key and value sequences to be added at dim=0.
add_zero_attn: add a new batch of zeros to the key and
value sequences at dim=1.
dropout_p: probability of an element to be zeroed.
out_proj_weight, out_proj_bias: the output projection weight and bias.
training: apply dropout if is ``True``.
key_padding_mask: if provided, specified padding elements in the key will
be ignored by the attention. This is an binary mask. When the value is True,
the corresponding value on the attention layer will be filled with -inf.
need_weights: output attn_output_weights.
Default: `True`
Note: `needs_weight` defaults to `True`, but should be set to `False`
For best performance when attention weights are not nedeeded.
*Setting needs_weights to `True`
leads to a significant performance degradation.*
attn_mask: 2D or 3D mask that prevents attention to certain positions. A 2D mask will be broadcasted for all
the batches while a 3D mask allows to specify a different mask for the entries of each batch.
is_causal: If specified, applies a causal mask as attention mask, and ignores
attn_mask for computing scaled dot product attention.
Default: ``False``.
.. warning::
is_causal is provides a hint that the attn_mask is the
causal mask.Providing incorrect hints can result in
incorrect execution, including forward and backward
compatibility.
use_separate_proj_weight: the function accept the proj. weights for query, key,
and value in different forms. If false, in_proj_weight will be used, which is
a combination of q_proj_weight, k_proj_weight, v_proj_weight.
q_proj_weight, k_proj_weight, v_proj_weight, in_proj_bias: input projection weight and bias.
static_k, static_v: static key and value used for attention operators.
average_attn_weights: If true, indicates that the returned ``attn_weights`` should be averaged across heads.
Otherwise, ``attn_weights`` are provided separately per head. Note that this flag only has an effect
when ``need_weights=True.``. Default: True
Shape:
Inputs:
- query: :math:`(L, E)` or :math:`(L, N, E)` where L is the target sequence length, N is the batch size, E is
the embedding dimension.
- key: :math:`(S, E)` or :math:`(S, N, E)`, where S is the source sequence length, N is the batch size, E is
the embedding dimension.
- value: :math:`(S, E)` or :math:`(S, N, E)` where S is the source sequence length, N is the batch size, E is
the embedding dimension.
- key_padding_mask: :math:`(S)` or :math:`(N, S)` where N is the batch size, S is the source sequence length.
If a FloatTensor is provided, it will be directly added to the value.
If a BoolTensor is provided, the positions with the
value of ``True`` will be ignored while the position with the value of ``False`` will be unchanged.
- attn_mask: 2D mask :math:`(L, S)` where L is the target sequence length, S is the source sequence length.
3D mask :math:`(N*num_heads, L, S)` where N is the batch size, L is the target sequence length,
S is the source sequence length. attn_mask ensures that position i is allowed to attend the unmasked
positions. If a BoolTensor is provided, positions with ``True``
are not allowed to attend while ``False`` values will be unchanged. If a FloatTensor
is provided, it will be added to the attention weight.
- static_k: :math:`(N*num_heads, S, E/num_heads)`, where S is the source sequence length,
N is the batch size, E is the embedding dimension. E/num_heads is the head dimension.
- static_v: :math:`(N*num_heads, S, E/num_heads)`, where S is the source sequence length,
N is the batch size, E is the embedding dimension. E/num_heads is the head dimension.
Outputs:
- attn_output: :math:`(L, E)` or :math:`(L, N, E)` where L is the target sequence length, N is the batch size,
E is the embedding dimension.
- attn_output_weights: Only returned when ``need_weights=True``. If ``average_attn_weights=True``, returns
attention weights averaged across heads of shape :math:`(L, S)` when input is unbatched or
:math:`(N, L, S)`, where :math:`N` is the batch size, :math:`L` is the target sequence length, and
:math:`S` is the source sequence length. If ``average_attn_weights=False``, returns attention weights per
head of shape :math:`(num_heads, L, S)` when input is unbatched or :math:`(N, num_heads, L, S)`.
"""
tens_ops = (
query,
key,
value,
in_proj_weight,
in_proj_bias,
bias_k,
bias_v,
out_proj_weight,
out_proj_bias,
)
if has_torch_function(tens_ops):
return handle_torch_function(
multi_head_attention_forward,
tens_ops,
query,
key,
value,
embed_dim_to_check,
num_heads,
in_proj_weight,
in_proj_bias,
bias_k,
bias_v,
add_zero_attn,
dropout_p,
out_proj_weight,
out_proj_bias,
training=training,
key_padding_mask=key_padding_mask,
need_weights=need_weights,
attn_mask=attn_mask,
is_causal=is_causal,
use_separate_proj_weight=use_separate_proj_weight,
q_proj_weight=q_proj_weight,
k_proj_weight=k_proj_weight,
v_proj_weight=v_proj_weight,
static_k=static_k,
static_v=static_v,
average_attn_weights=average_attn_weights,
cache=cache,
)
is_batched = _mha_shape_check(
query, key, value, key_padding_mask, attn_mask, num_heads
)
# For unbatched input, we unsqueeze at the expected batch-dim to pretend that the input
# is batched, run the computation and before returning squeeze the
# batch dimension so that the output doesn't carry this temporary batch dimension.
if not is_batched:
# unsqueeze if the input is unbatched
query = query.unsqueeze(1)
key = key.unsqueeze(1)
value = value.unsqueeze(1)
if key_padding_mask is not None:
key_padding_mask = key_padding_mask.unsqueeze(0)
# set up shape vars
tgt_len, bsz, embed_dim = query.shape
src_len, _, _ = key.shape
key_padding_mask = _canonical_mask(
mask=key_padding_mask,
mask_name="key_padding_mask",
other_type=_none_or_dtype(attn_mask),
other_name="attn_mask",
target_type=query.dtype,
)
if is_causal and attn_mask is None:
raise RuntimeError(
"Need attn_mask if specifying the is_causal hint. "
"You may use the Transformer module method "
"`generate_square_subsequent_mask` to create this mask."
)
if is_causal and key_padding_mask is None and not need_weights:
# when we have a kpm or need weights, we need attn_mask
# Otherwise, we use the is_causal hint go as is_causal
# indicator to SDPA.
attn_mask = None
else:
attn_mask = _canonical_mask(
mask=attn_mask,
mask_name="attn_mask",
other_type=None,
other_name="",
target_type=query.dtype,
check_other=False,
)
if key_padding_mask is not None:
# We have the attn_mask, and use that to merge kpm into it.
# Turn off use of is_causal hint, as the merged mask is no
# longer causal.
is_causal = False
assert (
embed_dim == embed_dim_to_check
), f"was expecting embedding dimension of {embed_dim_to_check}, but got {embed_dim}"
if isinstance(embed_dim, torch.Tensor):
# embed_dim can be a tensor when JIT tracing
head_dim = embed_dim.div(num_heads, rounding_mode="trunc")
else:
head_dim = embed_dim // num_heads
assert (
head_dim * num_heads == embed_dim
), f"embed_dim {embed_dim} not divisible by num_heads {num_heads}"
if use_separate_proj_weight:
# allow MHA to have different embedding dimensions when separate projection weights are used
assert (
key.shape[:2] == value.shape[:2]
), f"key's sequence and batch dims {key.shape[:2]} do not match value's {value.shape[:2]}"
else:
assert (
key.shape == value.shape
), f"key shape {key.shape} does not match value shape {value.shape}"
#
# compute in-projection
#
if not use_separate_proj_weight:
assert (
in_proj_weight is not None
), "use_separate_proj_weight is False but in_proj_weight is None"
q, k, v = _in_projection_packed(query, key, value, in_proj_weight, in_proj_bias)
else:
assert (
q_proj_weight is not None
), "use_separate_proj_weight is True but q_proj_weight is None"
assert (
k_proj_weight is not None
), "use_separate_proj_weight is True but k_proj_weight is None"
assert (
v_proj_weight is not None
), "use_separate_proj_weight is True but v_proj_weight is None"
if in_proj_bias is None:
b_q = b_k = b_v = None
else:
b_q, b_k, b_v = in_proj_bias.chunk(3)
q, k, v = _in_projection(
query,
key,
value,
q_proj_weight,
k_proj_weight,
v_proj_weight,
b_q,
b_k,
b_v,
)
if cache != None:
if cache["first_infer"] == 1:
cache["k"][cache["stage"]] = k
# print(0,cache["k"].shape)
cache["v"][cache["stage"]] = v
else: ###12个layer每个都要留自己的cache_kv
# print(1,cache["k"].shape)
cache["k"][cache["stage"]] = torch.cat(
[cache["k"][cache["stage"]], k], 0
) ##本来时序是1,但是proj的时候可能transpose了所以时序到0维了
cache["v"][cache["stage"]] = torch.cat([cache["v"][cache["stage"]], v], 0)
# print(2, cache["k"].shape)
src_len = cache["k"][cache["stage"]].shape[0]
k = cache["k"][cache["stage"]]
v = cache["v"][cache["stage"]]
# if attn_mask is not None:
# attn_mask=attn_mask[-1:,]
# print(attn_mask.shape,attn_mask)
cache["stage"] = (cache["stage"] + 1) % cache["all_stage"]
# print(2333,cache)
# prep attention mask
attn_mask = _canonical_mask(
mask=attn_mask,
mask_name="attn_mask",
other_type=None,
other_name="",
target_type=q.dtype,
check_other=False,
)
if attn_mask is not None:
# ensure attn_mask's dim is 3
if attn_mask.dim() == 2:
correct_2d_size = (tgt_len, src_len)
if attn_mask.shape != correct_2d_size:
raise RuntimeError(
f"The shape of the 2D attn_mask is {attn_mask.shape}, but should be {correct_2d_size}."
)
attn_mask = attn_mask.unsqueeze(0)
elif attn_mask.dim() == 3:
correct_3d_size = (bsz * num_heads, tgt_len, src_len)
if attn_mask.shape != correct_3d_size:
raise RuntimeError(
f"The shape of the 3D attn_mask is {attn_mask.shape}, but should be {correct_3d_size}."
)
else:
raise RuntimeError(
f"attn_mask's dimension {attn_mask.dim()} is not supported"
)
# add bias along batch dimension (currently second)
if bias_k is not None and bias_v is not None:
assert static_k is None, "bias cannot be added to static key."
assert static_v is None, "bias cannot be added to static value."
k = torch.cat([k, bias_k.repeat(1, bsz, 1)])
v = torch.cat([v, bias_v.repeat(1, bsz, 1)])
if attn_mask is not None:
attn_mask = pad(attn_mask, (0, 1))
if key_padding_mask is not None:
key_padding_mask = pad(key_padding_mask, (0, 1))
else:
assert bias_k is None
assert bias_v is None
#
# reshape q, k, v for multihead attention and make em batch first
#
q = q.view(tgt_len, bsz * num_heads, head_dim).transpose(0, 1)
if static_k is None:
k = k.view(k.shape[0], bsz * num_heads, head_dim).transpose(0, 1)
else:
# TODO finish disentangling control flow so we don't do in-projections when statics are passed
assert (
static_k.size(0) == bsz * num_heads
), f"expecting static_k.size(0) of {bsz * num_heads}, but got {static_k.size(0)}"
assert (
static_k.size(2) == head_dim
), f"expecting static_k.size(2) of {head_dim}, but got {static_k.size(2)}"
k = static_k
if static_v is None:
v = v.view(v.shape[0], bsz * num_heads, head_dim).transpose(0, 1)
else:
# TODO finish disentangling control flow so we don't do in-projections when statics are passed
assert (
static_v.size(0) == bsz * num_heads
), f"expecting static_v.size(0) of {bsz * num_heads}, but got {static_v.size(0)}"
assert (
static_v.size(2) == head_dim
), f"expecting static_v.size(2) of {head_dim}, but got {static_v.size(2)}"
v = static_v
# add zero attention along batch dimension (now first)
if add_zero_attn:
zero_attn_shape = (bsz * num_heads, 1, head_dim)
k = torch.cat(
[k, torch.zeros(zero_attn_shape, dtype=k.dtype, device=k.device)], dim=1
)
v = torch.cat(
[v, torch.zeros(zero_attn_shape, dtype=v.dtype, device=v.device)], dim=1
)
if attn_mask is not None:
attn_mask = pad(attn_mask, (0, 1))
if key_padding_mask is not None:
key_padding_mask = pad(key_padding_mask, (0, 1))
# update source sequence length after adjustments
src_len = k.size(1)
# merge key padding and attention masks
if key_padding_mask is not None:
assert key_padding_mask.shape == (
bsz,
src_len,
), f"expecting key_padding_mask shape of {(bsz, src_len)}, but got {key_padding_mask.shape}"
key_padding_mask = (
key_padding_mask.view(bsz, 1, 1, src_len)
.expand(-1, num_heads, -1, -1)
.reshape(bsz * num_heads, 1, src_len)
)
if attn_mask is None:
attn_mask = key_padding_mask
else:
attn_mask = attn_mask + key_padding_mask
# adjust dropout probability
if not training:
dropout_p = 0.0
#
# (deep breath) calculate attention and out projection
#
if need_weights:
B, Nt, E = q.shape
q_scaled = q / math.sqrt(E)
assert not (
is_causal and attn_mask is None
), "FIXME: is_causal not implemented for need_weights"
if attn_mask is not None:
attn_output_weights = torch.baddbmm(
attn_mask, q_scaled, k.transpose(-2, -1)
)
else:
attn_output_weights = torch.bmm(q_scaled, k.transpose(-2, -1))
attn_output_weights = softmax(attn_output_weights, dim=-1)
if dropout_p > 0.0:
attn_output_weights = dropout(attn_output_weights, p=dropout_p)
attn_output = torch.bmm(attn_output_weights, v)
attn_output = (
attn_output.transpose(0, 1).contiguous().view(tgt_len * bsz, embed_dim)
)
attn_output = linear(attn_output, out_proj_weight, out_proj_bias)
attn_output = attn_output.view(tgt_len, bsz, attn_output.size(1))
# optionally average attention weights over heads
attn_output_weights = attn_output_weights.view(bsz, num_heads, tgt_len, src_len)
if average_attn_weights:
attn_output_weights = attn_output_weights.mean(dim=1)
if not is_batched:
# squeeze the output if input was unbatched
attn_output = attn_output.squeeze(1)
attn_output_weights = attn_output_weights.squeeze(0)
return attn_output, attn_output_weights
else:
# attn_mask can be either (L,S) or (N*num_heads, L, S)
# if attn_mask's shape is (1, L, S) we need to unsqueeze to (1, 1, L, S)
# in order to match the input for SDPA of (N, num_heads, L, S)
if attn_mask is not None:
if attn_mask.size(0) == 1 and attn_mask.dim() == 3:
attn_mask = attn_mask.unsqueeze(0)
else:
attn_mask = attn_mask.view(bsz, num_heads, -1, src_len)
q = q.view(bsz, num_heads, tgt_len, head_dim)
k = k.view(bsz, num_heads, src_len, head_dim)
v = v.view(bsz, num_heads, src_len, head_dim)
# with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=True, enable_mem_efficient=True):
attn_output = scaled_dot_product_attention(
q, k, v, attn_mask, dropout_p, is_causal
)
attn_output = (
attn_output.permute(2, 0, 1, 3).contiguous().view(bsz * tgt_len, embed_dim)
)
attn_output = linear(attn_output, out_proj_weight, out_proj_bias)
attn_output = attn_output.view(tgt_len, bsz, attn_output.size(1))
if not is_batched:
# squeeze the output if input was unbatched
attn_output = attn_output.squeeze(1)
return attn_output, None1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192from torch.nn.functional import *
from torch.nn.functional import (
_mha_shape_check,
_canonical_mask,
_none_or_dtype,
_in_projection_packed,
)
def multi_head_attention_forward_patched(
query,
key,
value,
embed_dim_to_check: int,
num_heads: int,
in_proj_weight,
in_proj_bias: Optional[Tensor],
bias_k: Optional[Tensor],
bias_v: Optional[Tensor],
add_zero_attn: bool,
dropout_p: float,
out_proj_weight: Tensor,
out_proj_bias: Optional[Tensor],
training: bool = True,
key_padding_mask: Optional[Tensor] = None,
need_weights: bool = True,
attn_mask: Optional[Tensor] = None,
use_separate_proj_weight: bool = False,
q_proj_weight: Optional[Tensor] = None,
k_proj_weight: Optional[Tensor] = None,
v_proj_weight: Optional[Tensor] = None,
static_k: Optional[Tensor] = None,
static_v: Optional[Tensor] = None,
average_attn_weights: bool = True,
is_causal: bool = False,
cache=None,
) -> Tuple[Tensor, Optional[Tensor]]:
# set up shape vars
_, _, embed_dim = query.shape
attn_mask = _canonical_mask(
mask=attn_mask,
mask_name="attn_mask",
other_type=None,
other_name="",
target_type=query.dtype,
check_other=False,
)
head_dim = embed_dim // num_heads
proj_qkv = linear(query, in_proj_weight, in_proj_bias)
proj_qkv = proj_qkv.unflatten(-1, (3, query.size(-1))).unsqueeze(0).transpose(0, -2).squeeze(-2).contiguous()
q, k, v = proj_qkv[0], proj_qkv[1], proj_qkv[2]
if cache["first_infer"] == 1:
cache["k"][cache["stage"]] = k
cache["v"][cache["stage"]] = v
else:
cache["k"][cache["stage"]] = torch.cat([cache["k"][cache["stage"]][:-1], k], 0)
cache["v"][cache["stage"]] = torch.cat([cache["v"][cache["stage"]][:-1], v], 0)
k = cache["k"][cache["stage"]]
v = cache["v"][cache["stage"]]
cache["stage"] = (cache["stage"] + 1) % cache["all_stage"]
attn_mask = _canonical_mask(
mask=attn_mask,
mask_name="attn_mask",
other_type=None,
other_name="",
target_type=q.dtype,
check_other=False,
)
attn_mask = attn_mask.unsqueeze(0)
q = q.view(-1, num_heads, head_dim).transpose(0, 1)
k = k.view(-1, num_heads, head_dim).transpose(0, 1)
v = v.view(-1, num_heads, head_dim).transpose(0, 1)
dropout_p = 0.0
attn_mask = attn_mask.unsqueeze(0)
q = q.view(num_heads, -1, head_dim).unsqueeze(0)
k = k.view(num_heads, -1, head_dim).unsqueeze(0)
v = v.view(num_heads, -1, head_dim).unsqueeze(0)
attn_output = scaled_dot_product_attention(
q, k, v, attn_mask, dropout_p, is_causal
)
attn_output = (
attn_output.permute(2, 0, 1, 3).contiguous().view(-1, embed_dim)
)
attn_output = linear(attn_output, out_proj_weight, out_proj_bias)
attn_output = attn_output.view(-1, 1, attn_output.size(1))
return attn_output123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335# Copyright 2022 Xiaomi Corp. (authors: Daniel Povey)## See ../../../../LICENSE for clarification regarding multiple authors## 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.import loggingimport mathimport randomfrom typing import Optionalfrom typing import Tuplefrom typing import Union
import torchimport torch.nn as nnfrom torch import Tensor
class DoubleSwishFunction(torch.autograd.Function): """ double_swish(x) = x * torch.sigmoid(x-1) This is a definition, originally motivated by its close numerical similarity to swish(swish(x)), where swish(x) = x * sigmoid(x).
Memory-efficient derivative computation: double_swish(x) = x * s, where s(x) = torch.sigmoid(x-1) double_swish'(x) = d/dx double_swish(x) = x * s'(x) + x' * s(x) = x * s'(x) + s(x). Now, s'(x) = s(x) * (1-s(x)). double_swish'(x) = x * s'(x) + s(x). = x * s(x) * (1-s(x)) + s(x). = double_swish(x) * (1-s(x)) + s(x) ... so we just need to remember s(x) but not x itself. """
@staticmethod def forward(ctx, x: Tensor) -> Tensor: requires_grad = x.requires_grad x_dtype = x.dtype if x.dtype == torch.float16: x = x.to(torch.float32)
s = torch.sigmoid(x - 1.0) y = x * s
if requires_grad: deriv = y * (1 - s) + s # notes on derivative of x * sigmoid(x - 1): # https://www.wolframalpha.com/input?i=d%2Fdx+%28x+*+sigmoid%28x-1%29%29 # min \simeq -0.043638. Take floor as -0.043637 so it's a lower bund # max \simeq 1.1990. Take ceil to be 1.2 so it's an upper bound. # the combination of "+ torch.rand_like(deriv)" and casting to torch.uint8 (which # floors), should be expectation-preserving. floor = -0.043637 ceil = 1.2 d_scaled = (deriv - floor) * (255.0 / (ceil - floor)) + torch.rand_like( deriv ) if __name__ == "__main__": # for self-testing only. assert d_scaled.min() >= 0.0 assert d_scaled.max() < 256.0 d_int = d_scaled.to(torch.uint8) ctx.save_for_backward(d_int) if x.dtype == torch.float16 or torch.is_autocast_enabled(): y = y.to(torch.float16) return y
@staticmethod def backward(ctx, y_grad: Tensor) -> Tensor: (d,) = ctx.saved_tensors # the same constants as used in forward pass. floor = -0.043637 ceil = 1.2 d = d * ((ceil - floor) / 255.0) + floor return y_grad * d
class DoubleSwish(torch.nn.Module): def forward(self, x: Tensor) -> Tensor: """Return double-swish activation function which is an approximation to Swish(Swish(x)), that we approximate closely with x * sigmoid(x-1). """ if torch.jit.is_scripting() or torch.jit.is_tracing(): return x * torch.sigmoid(x - 1.0) return DoubleSwishFunction.apply(x)
class ActivationBalancerFunction(torch.autograd.Function): @staticmethod def forward( ctx, x: Tensor, scale_factor: Tensor, sign_factor: Optional[Tensor], channel_dim: int, ) -> Tensor: if channel_dim < 0: channel_dim += x.ndim ctx.channel_dim = channel_dim xgt0 = x > 0 if sign_factor is None: ctx.save_for_backward(xgt0, scale_factor) else: ctx.save_for_backward(xgt0, scale_factor, sign_factor) return x
@staticmethod def backward(ctx, x_grad: Tensor) -> Tuple[Tensor, None, None, None]: if len(ctx.saved_tensors) == 3: xgt0, scale_factor, sign_factor = ctx.saved_tensors for _ in range(ctx.channel_dim, x_grad.ndim - 1): scale_factor = scale_factor.unsqueeze(-1) sign_factor = sign_factor.unsqueeze(-1) factor = sign_factor + scale_factor * (xgt0.to(x_grad.dtype) - 0.5) else: xgt0, scale_factor = ctx.saved_tensors for _ in range(ctx.channel_dim, x_grad.ndim - 1): scale_factor = scale_factor.unsqueeze(-1) factor = scale_factor * (xgt0.to(x_grad.dtype) - 0.5) neg_delta_grad = x_grad.abs() * factor return ( x_grad - neg_delta_grad, None, None, None, )
def _compute_scale_factor( x: Tensor, channel_dim: int, min_abs: float, max_abs: float, gain_factor: float, max_factor: float,) -> Tensor: if channel_dim < 0: channel_dim += x.ndim sum_dims = [d for d in range(x.ndim) if d != channel_dim] x_abs_mean = torch.mean(x.abs(), dim=sum_dims).to(torch.float32)
if min_abs == 0.0: below_threshold = 0.0 else: # below_threshold is 0 if x_abs_mean > min_abs, can be at most max_factor if # x_abs)_mean , min_abs. below_threshold = ((min_abs - x_abs_mean) * (gain_factor / min_abs)).clamp( min=0, max=max_factor )
above_threshold = ((x_abs_mean - max_abs) * (gain_factor / max_abs)).clamp( min=0, max=max_factor )
return below_threshold - above_threshold
def _compute_sign_factor( x: Tensor, channel_dim: int, min_positive: float, max_positive: float, gain_factor: float, max_factor: float,) -> Tensor: if channel_dim < 0: channel_dim += x.ndim sum_dims = [d for d in range(x.ndim) if d != channel_dim] proportion_positive = torch.mean((x > 0).to(torch.float32), dim=sum_dims) if min_positive == 0.0: factor1 = 0.0 else: # 0 if proportion_positive >= min_positive, else can be # as large as max_factor. factor1 = ( (min_positive - proportion_positive) * (gain_factor / min_positive) ).clamp_(min=0, max=max_factor)
if max_positive == 1.0: factor2 = 0.0 else: # 0 if self.proportion_positive <= max_positive, else can be # as large as -max_factor. factor2 = ( (proportion_positive - max_positive) * (gain_factor / (1.0 - max_positive)) ).clamp_(min=0, max=max_factor) sign_factor = factor1 - factor2 # require min_positive != 0 or max_positive != 1: assert not isinstance(sign_factor, float) return sign_factor
class ActivationBalancer(torch.nn.Module): """ Modifies the backpropped derivatives of a function to try to encourage, for each channel, that it is positive at least a proportion `threshold` of the time. It does this by multiplying negative derivative values by up to (1+max_factor), and positive derivative values by up to (1-max_factor), interpolated from 1 at the threshold to those extremal values when none of the inputs are positive.
Args: num_channels: the number of channels channel_dim: the dimension/axis corresponding to the channel, e.g. -1, 0, 1, 2; will be interpreted as an offset from x.ndim if negative. min_positive: the minimum, per channel, of the proportion of the time that (x > 0), below which we start to modify the derivatives. max_positive: the maximum, per channel, of the proportion of the time that (x > 0), above which we start to modify the derivatives. max_factor: the maximum factor by which we modify the derivatives for either the sign constraint or the magnitude constraint; e.g. with max_factor=0.02, the the derivatives would be multiplied by values in the range [0.98..1.02]. sign_gain_factor: determines the 'gain' with which we increase the change in gradient once the constraints on min_positive and max_positive are violated. scale_gain_factor: determines the 'gain' with which we increase the change in gradient once the constraints on min_abs and max_abs are violated. min_abs: the minimum average-absolute-value difference from the mean value per channel, which we allow, before we start to modify the derivatives to prevent this. max_abs: the maximum average-absolute-value difference from the mean value per channel, which we allow, before we start to modify the derivatives to prevent this. min_prob: determines the minimum probability with which we modify the gradients for the {min,max}_positive and {min,max}_abs constraints, on each forward(). This is done randomly to prevent all layers from doing it at the same time. Early in training we may use higher probabilities than this; it will decay to this value. """
def __init__( self, num_channels: int, channel_dim: int, min_positive: float = 0.05, max_positive: float = 0.95, max_factor: float = 0.04, sign_gain_factor: float = 0.01, scale_gain_factor: float = 0.02, min_abs: float = 0.2, max_abs: float = 100.0, min_prob: float = 0.1, ): super(ActivationBalancer, self).__init__() self.num_channels = num_channels self.channel_dim = channel_dim self.min_positive = min_positive self.max_positive = max_positive self.max_factor = max_factor self.min_abs = min_abs self.max_abs = max_abs self.min_prob = min_prob self.sign_gain_factor = sign_gain_factor self.scale_gain_factor = scale_gain_factor
# count measures how many times the forward() function has been called. # We occasionally sync this to a tensor called `count`, that exists to # make sure it is synced to disk when we load and save the model. self.cpu_count = 0 self.register_buffer("count", torch.tensor(0, dtype=torch.int64))
def forward(self, x: Tensor) -> Tensor: if torch.jit.is_scripting() or not x.requires_grad or torch.jit.is_tracing(): return _no_op(x)
count = self.cpu_count self.cpu_count += 1
if random.random() < 0.01: # Occasionally sync self.cpu_count with self.count. # count affects the decay of 'prob'. don't do this on every iter, # because syncing with the GPU is slow. self.cpu_count = max(self.cpu_count, self.count.item()) self.count.fill_(self.cpu_count)
# the prob of doing some work exponentially decreases from 0.5 till it hits # a floor at min_prob (==0.1, by default) prob = max(self.min_prob, 0.5 ** (1 + (count / 4000.0)))
if random.random() < prob: sign_gain_factor = 0.5 if self.min_positive != 0.0 or self.max_positive != 1.0: sign_factor = _compute_sign_factor( x, self.channel_dim, self.min_positive, self.max_positive, gain_factor=self.sign_gain_factor / prob, max_factor=self.max_factor, ) else: sign_factor = None
scale_factor = _compute_scale_factor( x.detach(), self.channel_dim, min_abs=self.min_abs, max_abs=self.max_abs, gain_factor=self.scale_gain_factor / prob, max_factor=self.max_factor, ) return ActivationBalancerFunction.apply( x, scale_factor, sign_factor, self.channel_dim, ) else: return _no_op(x)
def BalancedDoubleSwish( d_model, channel_dim=-1, max_abs=10.0, min_prob=0.25) -> nn.Sequential: """ ActivationBalancer -> DoubleSwish """ balancer = ActivationBalancer( d_model, channel_dim=channel_dim, max_abs=max_abs, min_prob=min_prob ) return nn.Sequential( balancer, DoubleSwish(), )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/transformer.pyimport copyimport numbersfrom functools import partialfrom typing import Anyfrom typing import Callablefrom typing import Listfrom typing import Optionalfrom typing import Tuplefrom typing import Union
import torchfrom AR.modules.activation import MultiheadAttentionfrom AR.modules.scaling import BalancedDoubleSwishfrom torch import nnfrom torch import Tensorfrom torch.nn import functional as F
_shape_t = Union[int, List[int], torch.Size]
class LayerNorm(nn.Module): __constants__ = ["normalized_shape", "eps", "elementwise_affine"] normalized_shape: Tuple[int, ...] eps: float elementwise_affine: bool
def __init__( self, normalized_shape: _shape_t, eps: float = 1e-5, elementwise_affine: bool = True, device=None, dtype=None, ) -> None: factory_kwargs = {"device": device, "dtype": dtype} super(LayerNorm, self).__init__() if isinstance(normalized_shape, numbers.Integral): # mypy error: incompatible types in assignment normalized_shape = (normalized_shape,) # type: ignore[assignment] self.normalized_shape = tuple(normalized_shape) # type: ignore[arg-type] self.eps = eps self.elementwise_affine = elementwise_affine if self.elementwise_affine: self.weight = nn.Parameter( torch.empty(self.normalized_shape, **factory_kwargs) ) self.bias = nn.Parameter( torch.empty(self.normalized_shape, **factory_kwargs) ) else: self.register_parameter("weight", None) self.register_parameter("bias", None)
self.reset_parameters()
def reset_parameters(self) -> None: if self.elementwise_affine: nn.init.ones_(self.weight) nn.init.zeros_(self.bias)
def forward(self, input: Tensor, embedding: Any = None) -> Tensor: if isinstance(input, tuple): input, embedding = input return ( F.layer_norm( input, self.normalized_shape, self.weight, self.bias, self.eps, ), embedding, )
assert embedding is None return F.layer_norm( input, self.normalized_shape, self.weight, self.bias, self.eps )
def extra_repr(self) -> str: return ( "{normalized_shape}, eps={eps}, " "elementwise_affine={elementwise_affine}".format(**self.__dict__) )
class IdentityNorm(nn.Module): def __init__( self, d_model: int, eps: float = 1e-5, device=None, dtype=None, ) -> None: super(IdentityNorm, self).__init__()
def forward(self, input: Tensor, embedding: Any = None) -> Tensor: if isinstance(input, tuple): return input
assert embedding is None return input
class TransformerEncoder(nn.Module): r"""TransformerEncoder is a stack of N encoder layers. Users can build the BERT(https://arxiv.org/abs/1810.04805) model with corresponding parameters.
Args: encoder_layer: an instance of the TransformerEncoderLayer() class (required). num_layers: the number of sub-encoder-layers in the encoder (required). norm: the layer normalization component (optional). enable_nested_tensor: if True, input will automatically convert to nested tensor (and convert back on output). This will improve the overall performance of TransformerEncoder when padding rate is high. Default: ``True`` (enabled).
Examples:: >>> encoder_layer = TransformerEncoderLayer(d_model=512, nhead=8) >>> transformer_encoder = TransformerEncoder(encoder_layer, num_layers=6) >>> src = torch.rand(10, 32, 512) >>> out = transformer_encoder(src) """ __constants__ = ["norm"]
def __init__(self, encoder_layer, num_layers, norm=None): super(TransformerEncoder, self).__init__() self.layers = _get_clones(encoder_layer, num_layers) self.num_layers = num_layers self.norm = norm
def forward( self, src: Tensor, mask: Optional[Tensor] = None, src_key_padding_mask: Optional[Tensor] = None, return_layer_states: bool = False, cache=None, ) -> Tensor: r"""Pass the input through the encoder layers in turn.
Args: src: the sequence to the encoder (required). mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional). return_layer_states: return layers' state (optional).
Shape: see the docs in Transformer class. """ if return_layer_states: layer_states = [] # layers' output output = src for mod in self.layers: output = mod( output, src_mask=mask, src_key_padding_mask=src_key_padding_mask, cache=cache, ) layer_states.append(output[0])
if self.norm is not None: output = self.norm(output)
return layer_states, output
output = src for mod in self.layers: output = mod( output, src_mask=mask, src_key_padding_mask=src_key_padding_mask, cache=cache, )
if self.norm is not None: output = self.norm(output)
return output
class TransformerEncoderLayer(nn.Module): __constants__ = ["batch_first", "norm_first"]
def __init__( self, d_model: int, nhead: int, dim_feedforward: int = 2048, dropout: float = 0.1, activation: Union[str, Callable[[Tensor], Tensor]] = F.relu, batch_first: bool = False, norm_first: bool = False, device=None, dtype=None, linear1_self_attention_cls: nn.Module = nn.Linear, linear2_self_attention_cls: nn.Module = nn.Linear, linear1_feedforward_cls: nn.Module = nn.Linear, linear2_feedforward_cls: nn.Module = nn.Linear, layer_norm_cls: nn.Module = LayerNorm, layer_norm_eps: float = 1e-5, adaptive_layer_norm=False, ) -> None: factory_kwargs = {"device": device, "dtype": dtype} super(TransformerEncoderLayer, self).__init__() # print(233333333333,d_model,nhead) # import os # os._exit(2333333) self.self_attn = MultiheadAttention( d_model, # 512 16 nhead, dropout=dropout, batch_first=batch_first, linear1_cls=linear1_self_attention_cls, linear2_cls=linear2_self_attention_cls, **factory_kwargs, )
# Implementation of Feedforward model self.linear1 = linear1_feedforward_cls( d_model, dim_feedforward, **factory_kwargs ) self.dropout = nn.Dropout(dropout) self.linear2 = linear2_feedforward_cls( dim_feedforward, d_model, **factory_kwargs )
self.norm_first = norm_first self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout)
# Legacy string support for activation function. if isinstance(activation, str): activation = _get_activation_fn(activation) elif isinstance(activation, partial): activation = activation(d_model) elif activation == BalancedDoubleSwish: activation = BalancedDoubleSwish(d_model)
# # We can't test self.activation in forward() in TorchScript, # # so stash some information about it instead. # if activation is F.relu or isinstance(activation, torch.nn.ReLU): # self.activation_relu_or_gelu = 1 # elif activation is F.gelu or isinstance(activation, torch.nn.GELU): # self.activation_relu_or_gelu = 2 # else: # self.activation_relu_or_gelu = 0 self.activation = activation
norm1 = layer_norm_cls(d_model, eps=layer_norm_eps, **factory_kwargs) if layer_norm_cls == IdentityNorm: norm2 = BalancedBasicNorm(d_model, eps=layer_norm_eps, **factory_kwargs) else: norm2 = layer_norm_cls(d_model, eps=layer_norm_eps, **factory_kwargs)
if adaptive_layer_norm: self.norm1 = AdaptiveLayerNorm(d_model, norm1) self.norm2 = AdaptiveLayerNorm(d_model, norm2) else: self.norm1 = norm1 self.norm2 = norm2
def __setstate__(self, state): super(TransformerEncoderLayer, self).__setstate__(state) if not hasattr(self, "activation"): self.activation = F.relu
def forward( self, src: Tensor, src_mask: Optional[Tensor] = None, src_key_padding_mask: Optional[Tensor] = None, cache=None, ) -> Tensor: r"""Pass the input through the encoder layer.
Args: src: the sequence to the encoder layer (required). src_mask: the mask for the src sequence (optional). src_key_padding_mask: the mask for the src keys per batch (optional).
Shape: see the docs in Transformer class. """ x, stage_embedding = src, None is_src_tuple = False if isinstance(src, tuple): x, stage_embedding = src is_src_tuple = True
if src_key_padding_mask is not None: _skpm_dtype = src_key_padding_mask.dtype if _skpm_dtype != torch.bool and not torch.is_floating_point( src_key_padding_mask ): raise AssertionError( "only bool and floating types of key_padding_mask are supported" )
if self.norm_first: x = x + self._sa_block( self.norm1(x, stage_embedding), src_mask, src_key_padding_mask, cache=cache, ) x = x + self._ff_block(self.norm2(x, stage_embedding)) else: x = self.norm1( x + self._sa_block(x, src_mask, src_key_padding_mask, cache=cache), stage_embedding, ) x = self.norm2(x + self._ff_block(x), stage_embedding)
if is_src_tuple: return (x, stage_embedding) return x
# self-attention block def _sa_block( self, x: Tensor, attn_mask: Optional[Tensor], key_padding_mask: Optional[Tensor], cache=None, ) -> Tensor: # print(x.shape,attn_mask.shape,key_padding_mask) # torch.Size([1, 188, 512]) torch.Size([188, 188]) None # import os # os._exit(23333) x = self.self_attn( x, x, x, attn_mask=attn_mask, key_padding_mask=key_padding_mask, need_weights=False, cache=cache, )[0] return self.dropout1(x)
# feed forward block def _ff_block(self, x: Tensor) -> Tensor: x = self.linear2(self.dropout(self.activation(self.linear1(x)))) return self.dropout2(x)
class AdaptiveLayerNorm(nn.Module): r"""Adaptive Layer Normalization"""
def __init__(self, d_model, norm) -> None: super(AdaptiveLayerNorm, self).__init__() self.project_layer = nn.Linear(d_model, 2 * d_model) self.norm = norm self.d_model = d_model self.eps = self.norm.eps
def forward(self, input: Tensor, embedding: Tensor = None) -> Tensor: if isinstance(input, tuple): input, embedding = input weight, bias = torch.split( self.project_layer(embedding), split_size_or_sections=self.d_model, dim=-1, ) return (weight * self.norm(input) + bias, embedding)
weight, bias = torch.split( self.project_layer(embedding), split_size_or_sections=self.d_model, dim=-1, ) return weight * self.norm(input) + bias
def _get_clones(module, N): return nn.ModuleList([copy.deepcopy(module) for i in range(N)])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/transformer.pyimport copyimport numbersfrom functools import partialfrom typing import Anyfrom typing import Callablefrom typing import Listfrom typing import Optionalfrom typing import Tuplefrom typing import Union
import torchfrom AR.modules.activation_onnx import MultiheadAttentionfrom AR.modules.scaling import BalancedDoubleSwishfrom torch import nnfrom torch import Tensorfrom torch.nn import functional as F
_shape_t = Union[int, List[int], torch.Size]
class LayerNorm(nn.Module): __constants__ = ["normalized_shape", "eps", "elementwise_affine"] normalized_shape: Tuple[int, ...] eps: float elementwise_affine: bool
def __init__( self, normalized_shape: _shape_t, eps: float = 1e-5, elementwise_affine: bool = True, device=None, dtype=None, ) -> None: factory_kwargs = {"device": device, "dtype": dtype} super(LayerNorm, self).__init__() if isinstance(normalized_shape, numbers.Integral): # mypy error: incompatible types in assignment normalized_shape = (normalized_shape,) # type: ignore[assignment] self.normalized_shape = tuple(normalized_shape) # type: ignore[arg-type] self.eps = eps self.elementwise_affine = elementwise_affine if self.elementwise_affine: self.weight = nn.Parameter( torch.empty(self.normalized_shape, **factory_kwargs) ) self.bias = nn.Parameter( torch.empty(self.normalized_shape, **factory_kwargs) ) else: self.register_parameter("weight", None) self.register_parameter("bias", None)
self.reset_parameters()
def reset_parameters(self) -> None: if self.elementwise_affine: nn.init.ones_(self.weight) nn.init.zeros_(self.bias)
def forward(self, input: Tensor, embedding: Any = None) -> Tensor: if isinstance(input, tuple): input, embedding = input return ( F.layer_norm( input, self.normalized_shape, self.weight, self.bias, self.eps, ), embedding, )
assert embedding is None return F.layer_norm( input, self.normalized_shape, self.weight, self.bias, self.eps )
def extra_repr(self) -> str: return ( "{normalized_shape}, eps={eps}, " "elementwise_affine={elementwise_affine}".format(**self.__dict__) )
class IdentityNorm(nn.Module): def __init__( self, d_model: int, eps: float = 1e-5, device=None, dtype=None, ) -> None: super(IdentityNorm, self).__init__()
def forward(self, input: Tensor, embedding: Any = None) -> Tensor: if isinstance(input, tuple): return input
assert embedding is None return input
class TransformerEncoder(nn.Module): r"""TransformerEncoder is a stack of N encoder layers. Users can build the BERT(https://arxiv.org/abs/1810.04805) model with corresponding parameters.
Args: encoder_layer: an instance of the TransformerEncoderLayer() class (required). num_layers: the number of sub-encoder-layers in the encoder (required). norm: the layer normalization component (optional). enable_nested_tensor: if True, input will automatically convert to nested tensor (and convert back on output). This will improve the overall performance of TransformerEncoder when padding rate is high. Default: ``True`` (enabled).
Examples:: >>> encoder_layer = TransformerEncoderLayer(d_model=512, nhead=8) >>> transformer_encoder = TransformerEncoder(encoder_layer, num_layers=6) >>> src = torch.rand(10, 32, 512) >>> out = transformer_encoder(src) """ __constants__ = ["norm"]
def __init__(self, encoder_layer, num_layers, norm=None): super(TransformerEncoder, self).__init__() self.layers = _get_clones(encoder_layer, num_layers) self.num_layers = num_layers self.norm = norm
def forward( self, src: Tensor, mask: Optional[Tensor] = None, src_key_padding_mask: Optional[Tensor] = None, return_layer_states: bool = False, cache=None, ) -> Tensor: output = src for mod in self.layers: output = mod( output, src_mask=mask, src_key_padding_mask=src_key_padding_mask, cache=cache, )
if self.norm is not None: output = self.norm(output)
return output
class TransformerEncoderLayer(nn.Module): __constants__ = ["batch_first", "norm_first"] def __init__( self, d_model: int, nhead: int, dim_feedforward: int = 2048, dropout: float = 0.1, activation: Union[str, Callable[[Tensor], Tensor]] = F.relu, batch_first: bool = False, norm_first: bool = False, device=None, dtype=None, linear1_self_attention_cls: nn.Module = nn.Linear, linear2_self_attention_cls: nn.Module = nn.Linear, linear1_feedforward_cls: nn.Module = nn.Linear, linear2_feedforward_cls: nn.Module = nn.Linear, layer_norm_cls: nn.Module = LayerNorm, layer_norm_eps: float = 1e-5, adaptive_layer_norm=False, ) -> None: factory_kwargs = {"device": device, "dtype": dtype} super(TransformerEncoderLayer, self).__init__() self.self_attn = MultiheadAttention( d_model, # 512 16 nhead, dropout=dropout, batch_first=batch_first, linear1_cls=linear1_self_attention_cls, linear2_cls=linear2_self_attention_cls, **factory_kwargs, ) self.linear1 = linear1_feedforward_cls( d_model, dim_feedforward, **factory_kwargs ) self.dropout = nn.Dropout(dropout) self.linear2 = linear2_feedforward_cls( dim_feedforward, d_model, **factory_kwargs ) self.norm_first = norm_first self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout) if isinstance(activation, str): activation = _get_activation_fn(activation) elif isinstance(activation, partial): activation = activation(d_model) elif activation == BalancedDoubleSwish: activation = BalancedDoubleSwish(d_model) self.activation = activation
norm1 = layer_norm_cls(d_model, eps=layer_norm_eps, **factory_kwargs) if layer_norm_cls == IdentityNorm: norm2 = BalancedBasicNorm(d_model, eps=layer_norm_eps, **factory_kwargs) else: norm2 = layer_norm_cls(d_model, eps=layer_norm_eps, **factory_kwargs)
if adaptive_layer_norm: self.norm1 = AdaptiveLayerNorm(d_model, norm1) self.norm2 = AdaptiveLayerNorm(d_model, norm2) else: self.norm1 = norm1 self.norm2 = norm2
def __setstate__(self, state): super(TransformerEncoderLayer, self).__setstate__(state) if not hasattr(self, "activation"): self.activation = F.relu
def forward( self, src: Tensor, src_mask: Optional[Tensor] = None, src_key_padding_mask: Optional[Tensor] = None, cache=None, ) -> Tensor: x = src stage_embedding = None x = self.norm1( x + self._sa_block(x, src_mask, src_key_padding_mask, cache=cache), stage_embedding, ) x = self.norm2(x + self._ff_block(x), stage_embedding)
return x
def _sa_block( self, x: Tensor, attn_mask: Optional[Tensor], key_padding_mask: Optional[Tensor], cache=None, ) -> Tensor: x = self.self_attn( x, x, x, attn_mask=attn_mask, key_padding_mask=key_padding_mask, need_weights=False, cache=cache, ) return self.dropout1(x)
def _ff_block(self, x: Tensor) -> Tensor: x = self.linear2(self.dropout(self.activation(self.linear1(x)))) return self.dropout2(x)
class AdaptiveLayerNorm(nn.Module): r"""Adaptive Layer Normalization"""
def __init__(self, d_model, norm) -> None: super(AdaptiveLayerNorm, self).__init__() self.project_layer = nn.Linear(d_model, 2 * d_model) self.norm = norm self.d_model = d_model self.eps = self.norm.eps
def forward(self, input: Tensor, embedding: Tensor = None) -> Tensor: if isinstance(input, tuple): input, embedding = input weight, bias = torch.split( self.project_layer(embedding), split_size_or_sections=self.d_model, dim=-1, ) return (weight * self.norm(input) + bias, embedding)
weight, bias = torch.split( self.project_layer(embedding), split_size_or_sections=self.d_model, dim=-1, ) return weight * self.norm(input) + bias
def _get_clones(module, N): return nn.ModuleList([copy.deepcopy(module) for i in range(N)])12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/text_processing/phonemizer.py# reference: https://github.com/lifeiteng/vall-eimport itertoolsimport refrom typing import Dictfrom typing import List
import regexfrom gruut import sentencesfrom gruut.const import Sentencefrom gruut.const import Wordfrom AR.text_processing.symbols import SYMBOL_TO_ID
class GruutPhonemizer: def __init__(self, language: str): self._phonemizer = sentences self.lang = language self.symbol_to_id = SYMBOL_TO_ID self._special_cases_dict: Dict[str] = { r"\.\.\.": "... ", ";": "; ", ":": ": ", ",": ", ", r"\.": ". ", "!": "! ", r"\?": "? ", "—": "—", "…": "… ", "«": "«", "»": "»", } self._punctuation_regexp: str = ( rf"([{''.join(self._special_cases_dict.keys())}])" )
def _normalize_punctuation(self, text: str) -> str: text = regex.sub(rf"\pZ+{self._punctuation_regexp}", r"\1", text) text = regex.sub(rf"{self._punctuation_regexp}(\pL)", r"\1 \2", text) text = regex.sub(r"\pZ+", r" ", text) return text.strip()
def _convert_punctuation(self, word: Word) -> str: if not word.phonemes: return "" if word.phonemes[0] in ["‖", "|"]: return word.text.strip()
phonemes = "".join(word.phonemes) # remove modifier characters ˈˌː with regex phonemes = re.sub(r"[ˈˌː͡]", "", phonemes) return phonemes.strip()
def phonemize(self, text: str, espeak: bool = False) -> str: text_to_phonemize: str = self._normalize_punctuation(text) sents: List[Sentence] = [ sent for sent in self._phonemizer(text_to_phonemize, lang="en-us", espeak=espeak) ] words: List[str] = [ self._convert_punctuation(word) for word in itertools.chain(*sents) ] return " ".join(words)
def transform(self, phonemes): # convert phonemes to ids # dictionary is in symbols.py return [self.symbol_to_id[p] for p in phonemes if p in self.symbol_to_id.keys()]
if __name__ == "__main__": phonemizer = GruutPhonemizer("en-us") # text -> IPA phonemes = phonemizer.phonemize("Hello, wor-ld ?") print("phonemes:", phonemes) print("len(phonemes):", len(phonemes)) phoneme_ids = phonemizer.transform(phonemes) print("phoneme_ids:", phoneme_ids) print("len(phoneme_ids):", len(phoneme_ids))12345678910# modified from https://github.com/yangdongchao/SoundStorm/blob/master/soundstorm/s1/AR/text_processing/symbols.py# reference: https://github.com/lifeiteng/vall-ePAD = "_"PUNCTUATION = ';:,.!?¡¿—…"«»“” 'LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"IPA_LETTERS = "ɑɐɒæɓʙβɔɕçɗɖðʤəɘɚɛɜɝɞɟʄɡɠɢʛɦɧħɥʜɨɪʝɭɬɫɮʟɱɯɰŋɳɲɴøɵɸθœɶʘɹɺɾɻʀʁɽʂʃʈʧʉʊʋⱱʌɣɤʍχʎʏʑʐʒʔʡʕʢǀǁǂǃˈˌːˑʼʴʰʱʲʷˠˤ˞↓↑→↗↘'̩'ᵻ"SYMBOLS = [PAD] + list(PUNCTUATION) + list(LETTERS) + list(IPA_LETTERS)SPACE_ID = SYMBOLS.index(" ")SYMBOL_TO_ID = {s: i for i, s in enumerate(SYMBOLS)}ID_TO_SYMBOL = {i: s for i, s in enumerate(SYMBOLS)}12345678910111213141516171819202122232425262728293031323334353637import re
def str2bool(str): return True if str.lower() == 'true' else False
def get_newest_ckpt(string_list): # 定义一个正则表达式模式,用于匹配字符串中的数字 pattern = r'epoch=(\d+)-step=(\d+)\.ckpt'
# 使用正则表达式提取每个字符串中的数字信息,并创建一个包含元组的列表 extracted_info = [] for string in string_list: match = re.match(pattern, string) if match: epoch = int(match.group(1)) step = int(match.group(2)) extracted_info.append((epoch, step, string)) # 按照 epoch 后面的数字和 step 后面的数字进行排序 sorted_info = sorted( extracted_info, key=lambda x: (x[0], x[1]), reverse=True) # 获取最新的 ckpt 文件名 newest_ckpt = sorted_info[0][2] return newest_ckpt
# 文本存在且不为空时 return Truedef check_txt_file(file_path): try: with open(file_path, 'r') as file: text = file.readline().strip() assert text.strip() != '' return text except Exception: return False return False1234567891011121314151617181920212223242526272829303132333435363738#!/usr/bin/env python3"""Initialize modules for espnet2 neural networks."""import torchfrom typeguard import check_argument_types
def initialize(model: torch.nn.Module, init: str): """Initialize weights of a neural network module.
Parameters are initialized using the given method or distribution.
Custom initialization routines can be implemented into submodules as function `espnet_initialization_fn` within the custom module.
Args: model: Target. init: Method of initialization. """ assert check_argument_types() print("init with", init)
# weight init for p in model.parameters(): if p.dim() > 1: if init == "xavier_uniform": torch.nn.init.xavier_uniform_(p.data) elif init == "xavier_normal": torch.nn.init.xavier_normal_(p.data) elif init == "kaiming_uniform": torch.nn.init.kaiming_uniform_(p.data, nonlinearity="relu") elif init == "kaiming_normal": torch.nn.init.kaiming_normal_(p.data, nonlinearity="relu") else: raise ValueError("Unknown initialization: " + init) # bias init for name, p in model.named_parameters(): if ".bias" in name and p.dim() == 1: p.data.zero_()12345678910111213141516171819202122232425262728293031323334import sys
import torchimport yaml
def load_yaml_config(path): with open(path) as f: config = yaml.full_load(f) return config
def save_config_to_yaml(config, path): assert path.endswith(".yaml") with open(path, "w") as f: f.write(yaml.dump(config)) f.close()
def write_args(args, path): args_dict = dict( (name, getattr(args, name)) for name in dir(args) if not name.startswith("_") ) with open(path, "a") as args_file: args_file.write("==> torch version: {}\n".format(torch.__version__)) args_file.write( "==> cudnn version: {}\n".format(torch.backends.cudnn.version()) ) args_file.write("==> Cmd:\n") args_file.write(str(sys.argv)) args_file.write("\n==> args:\n") for k, v in sorted(args_dict.items()): args_file.write(" %s: %s\n" % (str(k), str(v))) args_file.close()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034from copy import deepcopyimport mathimport os, sys, gcimport randomimport traceback
from tqdm import tqdmnow_dir = os.getcwd()sys.path.append(now_dir)import ffmpegimport osfrom typing import Generator, List, Tuple, Unionimport numpy as npimport torchimport torch.nn.functional as Fimport yamlfrom transformers import AutoModelForMaskedLM, AutoTokenizer
from AR.models.t2s_lightning_module import Text2SemanticLightningModulefrom feature_extractor.cnhubert import CNHubertfrom module.models import SynthesizerTrnimport librosafrom time import time as ttimefrom tools.i18n.i18n import I18nAuto, scan_language_listfrom tools.my_utils import load_audiofrom module.mel_processing import spectrogram_torchfrom TTS_infer_pack.text_segmentation_method import splitsfrom TTS_infer_pack.TextPreprocessor import TextPreprocessorlanguage=os.environ.get("language","Auto")language=sys.argv[-1] if sys.argv[-1] in scan_language_list() else languagei18n = I18nAuto(language=language)
# configs/tts_infer.yaml"""custom: bert_base_path: GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large cnhuhbert_base_path: GPT_SoVITS/pretrained_models/chinese-hubert-base device: cpu is_half: false t2s_weights_path: GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s1bert25hz-5kh-longer-epoch=12-step=369668.ckpt vits_weights_path: GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s2G2333k.pth version: v2default: bert_base_path: GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large cnhuhbert_base_path: GPT_SoVITS/pretrained_models/chinese-hubert-base device: cpu is_half: false t2s_weights_path: GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt vits_weights_path: GPT_SoVITS/pretrained_models/s2G488k.pth version: v1default_v2: bert_base_path: GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large cnhuhbert_base_path: GPT_SoVITS/pretrained_models/chinese-hubert-base device: cpu is_half: false t2s_weights_path: GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s1bert25hz-5kh-longer-epoch=12-step=369668.ckpt vits_weights_path: GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s2G2333k.pth version: v2"""
def set_seed(seed:int): seed = int(seed) seed = seed if seed != -1 else random.randrange(1 << 32) print(f"Set seed to {seed}") os.environ['PYTHONHASHSEED'] = str(seed) random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) try: if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) # torch.backends.cudnn.deterministic = True # torch.backends.cudnn.benchmark = False # torch.backends.cudnn.enabled = True # 开启后会影响精度 torch.backends.cuda.matmul.allow_tf32 = False torch.backends.cudnn.allow_tf32 = False except: pass return seed class TTS_Config: default_configs={ "default":{ "device": "cpu", "is_half": False, "version": "v1", "t2s_weights_path": "GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt", "vits_weights_path": "GPT_SoVITS/pretrained_models/s2G488k.pth", "cnhuhbert_base_path": "GPT_SoVITS/pretrained_models/chinese-hubert-base", "bert_base_path": "GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large", }, "default_v2":{ "device": "cpu", "is_half": False, "version": "v2", "t2s_weights_path": "GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s1bert25hz-5kh-longer-epoch=12-step=369668.ckpt", "vits_weights_path": "GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s2G2333k.pth", "cnhuhbert_base_path": "GPT_SoVITS/pretrained_models/chinese-hubert-base", "bert_base_path": "GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large", }, } configs:dict = None v1_languages:list = ["auto", "en", "zh", "ja", "all_zh", "all_ja"] v2_languages:list = ["auto", "auto_yue", "en", "zh", "ja", "yue", "ko", "all_zh", "all_ja", "all_yue", "all_ko"] languages:list = v2_languages # "all_zh",#全部按中文识别 # "en",#全部按英文识别#######不变 # "all_ja",#全部按日文识别 # "all_yue",#全部按中文识别 # "all_ko",#全部按韩文识别 # "zh",#按中英混合识别####不变 # "ja",#按日英混合识别####不变 # "yue",#按粤英混合识别####不变 # "ko",#按韩英混合识别####不变 # "auto",#多语种启动切分识别语种 # "auto_yue",#多语种启动切分识别语种
def __init__(self, configs: Union[dict, str]=None): # 设置默认配置文件路径 configs_base_path:str = "GPT_SoVITS/configs/" os.makedirs(configs_base_path, exist_ok=True) self.configs_path:str = os.path.join(configs_base_path, "tts_infer.yaml") if configs in ["", None]: if not os.path.exists(self.configs_path): self.save_configs() print(f"Create default config file at {self.configs_path}") configs:dict = deepcopy(self.default_configs) if isinstance(configs, str): self.configs_path = configs configs:dict = self._load_configs(self.configs_path)
assert isinstance(configs, dict) version = configs.get("version", "v2").lower() assert version in ["v1", "v2"] self.default_configs["default"] = configs.get("default", self.default_configs["default"]) self.default_configs["default_v2"] = configs.get("default_v2", self.default_configs["default_v2"])
default_config_key = "default"if version=="v1" else "default_v2" self.configs:dict = configs.get("custom", deepcopy(self.default_configs[default_config_key])) self.device = self.configs.get("device", torch.device("cpu")) self.is_half = self.configs.get("is_half", False) self.version = version self.t2s_weights_path = self.configs.get("t2s_weights_path", None) self.vits_weights_path = self.configs.get("vits_weights_path", None) self.bert_base_path = self.configs.get("bert_base_path", None) self.cnhuhbert_base_path = self.configs.get("cnhuhbert_base_path", None) self.languages = self.v2_languages if self.version=="v2" else self.v1_languages
if (self.t2s_weights_path in [None, ""]) or (not os.path.exists(self.t2s_weights_path)): self.t2s_weights_path = self.default_configs[default_config_key]['t2s_weights_path'] print(f"fall back to default t2s_weights_path: {self.t2s_weights_path}") if (self.vits_weights_path in [None, ""]) or (not os.path.exists(self.vits_weights_path)): self.vits_weights_path = self.default_configs[default_config_key]['vits_weights_path'] print(f"fall back to default vits_weights_path: {self.vits_weights_path}") if (self.bert_base_path in [None, ""]) or (not os.path.exists(self.bert_base_path)): self.bert_base_path = self.default_configs[default_config_key]['bert_base_path'] print(f"fall back to default bert_base_path: {self.bert_base_path}") if (self.cnhuhbert_base_path in [None, ""]) or (not os.path.exists(self.cnhuhbert_base_path)): self.cnhuhbert_base_path = self.default_configs[default_config_key]['cnhuhbert_base_path'] print(f"fall back to default cnhuhbert_base_path: {self.cnhuhbert_base_path}") self.update_configs() self.max_sec = None self.hz:int = 50 self.semantic_frame_rate:str = "25hz" self.segment_size:int = 20480 self.filter_length:int = 2048 self.sampling_rate:int = 32000 self.hop_length:int = 640 self.win_length:int = 2048 self.n_speakers:int = 300
def _load_configs(self, configs_path: str)->dict: if os.path.exists(configs_path): ... else: print(i18n("路径不存在,使用默认配置")) self.save_configs(configs_path) with open(configs_path, 'r') as f: configs = yaml.load(f, Loader=yaml.FullLoader) return configs
def save_configs(self, configs_path:str=None)->None: configs=deepcopy(self.default_configs) if self.configs is not None: configs["custom"] = self.update_configs() if configs_path is None: configs_path = self.configs_path with open(configs_path, 'w') as f: yaml.dump(configs, f)
def update_configs(self): self.config = { "device" : str(self.device), "is_half" : self.is_half, "version" : self.version, "t2s_weights_path" : self.t2s_weights_path, "vits_weights_path" : self.vits_weights_path, "bert_base_path" : self.bert_base_path, "cnhuhbert_base_path": self.cnhuhbert_base_path, } return self.config
def update_version(self, version:str)->None: self.version = version self.languages = self.v2_languages if self.version=="v2" else self.v1_languages def __str__(self): self.configs = self.update_configs() string = "TTS Config".center(100, '-') + '\n' for k, v in self.configs.items(): string += f"{str(k).ljust(20)}: {str(v)}\n" string += "-" * 100 + '\n' return string def __repr__(self): return self.__str__()
def __hash__(self): return hash(self.configs_path)
def __eq__(self, other): return isinstance(other, TTS_Config) and self.configs_path == other.configs_path
class TTS: def __init__(self, configs: Union[dict, str, TTS_Config]): if isinstance(configs, TTS_Config): self.configs = configs else: self.configs:TTS_Config = TTS_Config(configs) self.t2s_model:Text2SemanticLightningModule = None self.vits_model:SynthesizerTrn = None self.bert_tokenizer:AutoTokenizer = None self.bert_model:AutoModelForMaskedLM = None self.cnhuhbert_model:CNHubert = None self._init_models() self.text_preprocessor:TextPreprocessor = \ TextPreprocessor(self.bert_model, self.bert_tokenizer, self.configs.device) self.prompt_cache:dict = { "ref_audio_path" : None, "prompt_semantic": None, "refer_spec" : [], "prompt_text" : None, "prompt_lang" : None, "phones" : None, "bert_features" : None, "norm_text" : None, "aux_ref_audio_paths": [], } self.stop_flag:bool = False self.precision:torch.dtype = torch.float16 if self.configs.is_half else torch.float32
def _init_models(self,): self.init_t2s_weights(self.configs.t2s_weights_path) self.init_vits_weights(self.configs.vits_weights_path) self.init_bert_weights(self.configs.bert_base_path) self.init_cnhuhbert_weights(self.configs.cnhuhbert_base_path) # self.enable_half_precision(self.configs.is_half) def init_cnhuhbert_weights(self, base_path: str): print(f"Loading CNHuBERT weights from {base_path}") self.cnhuhbert_model = CNHubert(base_path) self.cnhuhbert_model=self.cnhuhbert_model.eval() self.cnhuhbert_model = self.cnhuhbert_model.to(self.configs.device) if self.configs.is_half and str(self.configs.device)!="cpu": self.cnhuhbert_model = self.cnhuhbert_model.half() def init_bert_weights(self, base_path: str): print(f"Loading BERT weights from {base_path}") self.bert_tokenizer = AutoTokenizer.from_pretrained(base_path) self.bert_model = AutoModelForMaskedLM.from_pretrained(base_path) self.bert_model=self.bert_model.eval() self.bert_model = self.bert_model.to(self.configs.device) if self.configs.is_half and str(self.configs.device)!="cpu": self.bert_model = self.bert_model.half() def init_vits_weights(self, weights_path: str): print(f"Loading VITS weights from {weights_path}") self.configs.vits_weights_path = weights_path dict_s2 = torch.load(weights_path, map_location=self.configs.device) hps = dict_s2["config"] if dict_s2['weight']['enc_p.text_embedding.weight'].shape[0] == 322: self.configs.update_version("v1") else: self.configs.update_version("v2") self.configs.save_configs() hps["model"]["version"] = self.configs.version self.configs.filter_length = hps["data"]["filter_length"] self.configs.segment_size = hps["train"]["segment_size"] self.configs.sampling_rate = hps["data"]["sampling_rate"] self.configs.hop_length = hps["data"]["hop_length"] self.configs.win_length = hps["data"]["win_length"] self.configs.n_speakers = hps["data"]["n_speakers"] self.configs.semantic_frame_rate = "25hz" kwargs = hps["model"] vits_model = SynthesizerTrn( self.configs.filter_length // 2 + 1, self.configs.segment_size // self.configs.hop_length, n_speakers=self.configs.n_speakers, **kwargs )
if hasattr(vits_model, "enc_q"): del vits_model.enc_q vits_model = vits_model.to(self.configs.device) vits_model = vits_model.eval() vits_model.load_state_dict(dict_s2["weight"], strict=False) self.vits_model = vits_model if self.configs.is_half and str(self.configs.device)!="cpu": self.vits_model = self.vits_model.half()
def init_t2s_weights(self, weights_path: str): print(f"Loading Text2Semantic weights from {weights_path}") self.configs.t2s_weights_path = weights_path self.configs.save_configs() self.configs.hz = 50 dict_s1 = torch.load(weights_path, map_location=self.configs.device) config = dict_s1["config"] self.configs.max_sec = config["data"]["max_sec"] t2s_model = Text2SemanticLightningModule(config, "****", is_train=False) t2s_model.load_state_dict(dict_s1["weight"]) t2s_model = t2s_model.to(self.configs.device) t2s_model = t2s_model.eval() self.t2s_model = t2s_model if self.configs.is_half and str(self.configs.device)!="cpu": self.t2s_model = self.t2s_model.half() def enable_half_precision(self, enable: bool = True, save: bool = True): ''' To enable half precision for the TTS model. Args: enable: bool, whether to enable half precision. ''' if str(self.configs.device) == "cpu" and enable: print("Half precision is not supported on CPU.") return self.configs.is_half = enable self.precision = torch.float16 if enable else torch.float32 if save: self.configs.save_configs() if enable: if self.t2s_model is not None: self.t2s_model =self.t2s_model.half() if self.vits_model is not None: self.vits_model = self.vits_model.half() if self.bert_model is not None: self.bert_model =self.bert_model.half() if self.cnhuhbert_model is not None: self.cnhuhbert_model = self.cnhuhbert_model.half() else: if self.t2s_model is not None: self.t2s_model = self.t2s_model.float() if self.vits_model is not None: self.vits_model = self.vits_model.float() if self.bert_model is not None: self.bert_model = self.bert_model.float() if self.cnhuhbert_model is not None: self.cnhuhbert_model = self.cnhuhbert_model.float() def set_device(self, device: torch.device, save: bool = True): ''' To set the device for all models. Args: device: torch.device, the device to use for all models. ''' self.configs.device = device if save: self.configs.save_configs() if self.t2s_model is not None: self.t2s_model = self.t2s_model.to(device) if self.vits_model is not None: self.vits_model = self.vits_model.to(device) if self.bert_model is not None: self.bert_model = self.bert_model.to(device) if self.cnhuhbert_model is not None: self.cnhuhbert_model = self.cnhuhbert_model.to(device) def set_ref_audio(self, ref_audio_path:str): ''' To set the reference audio for the TTS model, including the prompt_semantic and refer_spepc. Args: ref_audio_path: str, the path of the reference audio. ''' self._set_prompt_semantic(ref_audio_path) self._set_ref_spec(ref_audio_path) self._set_ref_audio_path(ref_audio_path) def _set_ref_audio_path(self, ref_audio_path): self.prompt_cache["ref_audio_path"] = ref_audio_path
def _set_ref_spec(self, ref_audio_path): spec = self._get_ref_spec(ref_audio_path) if self.prompt_cache["refer_spec"] in [[],None]: self.prompt_cache["refer_spec"]=[spec] else: self.prompt_cache["refer_spec"][0] = spec
def _get_ref_spec(self, ref_audio_path): audio = load_audio(ref_audio_path, int(self.configs.sampling_rate)) audio = torch.FloatTensor(audio) maxx=audio.abs().max() if(maxx>1):audio/=min(2,maxx) audio_norm = audio audio_norm = audio_norm.unsqueeze(0) spec = spectrogram_torch( audio_norm, self.configs.filter_length, self.configs.sampling_rate, self.configs.hop_length, self.configs.win_length, center=False, ) spec = spec.to(self.configs.device) if self.configs.is_half: spec = spec.half() return spec
def _set_prompt_semantic(self, ref_wav_path:str): zero_wav = np.zeros( int(self.configs.sampling_rate * 0.3), dtype=np.float16 if self.configs.is_half else np.float32, ) with torch.no_grad(): wav16k, sr = librosa.load(ref_wav_path, sr=16000) if (wav16k.shape[0] > 160000 or wav16k.shape[0] < 48000): raise OSError(i18n("参考音频在3~10秒范围外,请更换!")) wav16k = torch.from_numpy(wav16k) zero_wav_torch = torch.from_numpy(zero_wav) wav16k = wav16k.to(self.configs.device) zero_wav_torch = zero_wav_torch.to(self.configs.device) if self.configs.is_half: wav16k = wav16k.half() zero_wav_torch = zero_wav_torch.half()
wav16k = torch.cat([wav16k, zero_wav_torch]) hubert_feature = self.cnhuhbert_model.model(wav16k.unsqueeze(0))[ "last_hidden_state" ].transpose( 1, 2 ) # .float() codes = self.vits_model.extract_latent(hubert_feature) prompt_semantic = codes[0, 0].to(self.configs.device) self.prompt_cache["prompt_semantic"] = prompt_semantic def batch_sequences(self, sequences: List[torch.Tensor], axis: int = 0, pad_value: int = 0, max_length:int=None): seq = sequences[0] ndim = seq.dim() if axis < 0: axis += ndim dtype:torch.dtype = seq.dtype pad_value = torch.tensor(pad_value, dtype=dtype) seq_lengths = [seq.shape[axis] for seq in sequences] if max_length is None: max_length = max(seq_lengths) else: max_length = max(seq_lengths) if max_length < max(seq_lengths) else max_length
padded_sequences = [] for seq, length in zip(sequences, seq_lengths): padding = [0] * axis + [0, max_length - length] + [0] * (ndim - axis - 1) padded_seq = torch.nn.functional.pad(seq, padding, value=pad_value) padded_sequences.append(padded_seq) batch = torch.stack(padded_sequences) return batch def to_batch(self, data:list, prompt_data:dict=None, batch_size:int=5, threshold:float=0.75, split_bucket:bool=True, device:torch.device=torch.device("cpu"), precision:torch.dtype=torch.float32, ): _data:list = [] index_and_len_list = [] for idx, item in enumerate(data): norm_text_len = len(item["norm_text"]) index_and_len_list.append([idx, norm_text_len])
batch_index_list = [] if split_bucket: index_and_len_list.sort(key=lambda x: x[1]) index_and_len_list = np.array(index_and_len_list, dtype=np.int64) batch_index_list_len = 0 pos = 0 while pos <index_and_len_list.shape[0]: # batch_index_list.append(index_and_len_list[pos:min(pos+batch_size,len(index_and_len_list))]) pos_end = min(pos+batch_size,index_and_len_list.shape[0]) while pos < pos_end: batch=index_and_len_list[pos:pos_end, 1].astype(np.float32) score=batch[(pos_end-pos)//2]/(batch.mean()+1e-8) if (score>=threshold) or (pos_end-pos==1): batch_index=index_and_len_list[pos:pos_end, 0].tolist() batch_index_list_len += len(batch_index) batch_index_list.append(batch_index) pos = pos_end break pos_end=pos_end-1 assert batch_index_list_len == len(data) else: for i in range(len(data)): if i%batch_size == 0: batch_index_list.append([]) batch_index_list[-1].append(i)
for batch_idx, index_list in enumerate(batch_index_list): item_list = [data[idx] for idx in index_list] phones_list = [] phones_len_list = [] # bert_features_list = [] all_phones_list = [] all_phones_len_list = [] all_bert_features_list = [] norm_text_batch = [] all_bert_max_len = 0 all_phones_max_len = 0 for item in item_list: if prompt_data is not None: all_bert_features = torch.cat([prompt_data["bert_features"], item["bert_features"]], 1)\ .to(dtype=precision, device=device) all_phones = torch.LongTensor(prompt_data["phones"]+item["phones"]).to(device) phones = torch.LongTensor(item["phones"]).to(device) # norm_text = prompt_data["norm_text"]+item["norm_text"] else: all_bert_features = item["bert_features"]\ .to(dtype=precision, device=device) phones = torch.LongTensor(item["phones"]).to(device) all_phones = phones # norm_text = item["norm_text"]
all_bert_max_len = max(all_bert_max_len, all_bert_features.shape[-1]) all_phones_max_len = max(all_phones_max_len, all_phones.shape[-1]) phones_list.append(phones) phones_len_list.append(phones.shape[-1]) all_phones_list.append(all_phones) all_phones_len_list.append(all_phones.shape[-1]) all_bert_features_list.append(all_bert_features) norm_text_batch.append(item["norm_text"]) phones_batch = phones_list all_phones_batch = all_phones_list all_bert_features_batch = all_bert_features_list max_len = max(all_bert_max_len, all_phones_max_len) # phones_batch = self.batch_sequences(phones_list, axis=0, pad_value=0, max_length=max_len) #### 直接对phones和bert_features进行pad。(padding策略会影响T2S模型生成的结果,但不直接影响复读概率。影响复读概率的主要因素是mask的策略) # all_phones_batch = self.batch_sequences(all_phones_list, axis=0, pad_value=0, max_length=max_len) # all_bert_features_batch = all_bert_features_list # all_bert_features_batch = torch.zeros((len(all_bert_features_list), 1024, max_len), dtype=precision, device=device) # for idx, item in enumerate(all_bert_features_list): # all_bert_features_batch[idx, :, : item.shape[-1]] = item # #### 先对phones进行embedding、对bert_features进行project,再pad到相同长度,(padding策略会影响T2S模型生成的结果,但不直接影响复读概率。影响复读概率的主要因素是mask的策略) # all_phones_list = [self.t2s_model.model.ar_text_embedding(item.to(self.t2s_model.device)) for item in all_phones_list] # all_phones_list = [F.pad(item,(0,0,0,max_len-item.shape[0]),value=0) for item in all_phones_list] # all_phones_batch = torch.stack(all_phones_list, dim=0) # all_bert_features_list = [self.t2s_model.model.bert_proj(item.to(self.t2s_model.device).transpose(0, 1)) for item in all_bert_features_list] # all_bert_features_list = [F.pad(item,(0,0,0,max_len-item.shape[0]), value=0) for item in all_bert_features_list] # all_bert_features_batch = torch.stack(all_bert_features_list, dim=0) batch = { "phones": phones_batch, "phones_len": torch.LongTensor(phones_len_list).to(device), "all_phones": all_phones_batch, "all_phones_len": torch.LongTensor(all_phones_len_list).to(device), "all_bert_features": all_bert_features_batch, "norm_text": norm_text_batch, "max_len": max_len, } _data.append(batch) return _data, batch_index_list def recovery_order(self, data:list, batch_index_list:list)->list: ''' Recovery the order of the audio according to the batch_index_list. Args: data (List[list(np.ndarray)]): the out of order audio . batch_index_list (List[list[int]]): the batch index list. Returns: list (List[np.ndarray]): the data in the original order. ''' length = len(sum(batch_index_list, [])) _data = [None]*length for i, index_list in enumerate(batch_index_list): for j, index in enumerate(index_list): _data[index] = data[i][j] return _data
def stop(self,): ''' Stop the inference process. ''' self.stop_flag = True @torch.no_grad() def run(self, inputs:dict): """ Text to speech inference. Args: inputs (dict): { "text": "", # str.(required) text to be synthesized "text_lang: "", # str.(required) language of the text to be synthesized "ref_audio_path": "", # str.(required) reference audio path "aux_ref_audio_paths": [], # list.(optional) auxiliary reference audio paths for multi-speaker tone fusion "prompt_text": "", # str.(optional) prompt text for the reference audio "prompt_lang": "", # str.(required) language of the prompt text for the reference audio "top_k": 5, # int. top k sampling "top_p": 1, # float. top p sampling "temperature": 1, # float. temperature for sampling "text_split_method": "cut0", # str. text split method, see text_segmentation_method.py for details. "batch_size": 1, # int. batch size for inference "batch_threshold": 0.75, # float. threshold for batch splitting. "split_bucket: True, # bool. whether to split the batch into multiple buckets. "return_fragment": False, # bool. step by step return the audio fragment. "speed_factor":1.0, # float. control the speed of the synthesized audio. "fragment_interval":0.3, # float. to control the interval of the audio fragment. "seed": -1, # int. random seed for reproducibility. "parallel_infer": True, # bool. whether to use parallel inference. "repetition_penalty": 1.35 # float. repetition penalty for T2S model. } returns: Tuple[int, np.ndarray]: sampling rate and audio data. """ ########## variables initialization ########### self.stop_flag:bool = False text:str = inputs.get("text", "") text_lang:str = inputs.get("text_lang", "") ref_audio_path:str = inputs.get("ref_audio_path", "") aux_ref_audio_paths:list = inputs.get("aux_ref_audio_paths", []) prompt_text:str = inputs.get("prompt_text", "") prompt_lang:str = inputs.get("prompt_lang", "") top_k:int = inputs.get("top_k", 5) top_p:float = inputs.get("top_p", 1) temperature:float = inputs.get("temperature", 1) text_split_method:str = inputs.get("text_split_method", "cut0") batch_size = inputs.get("batch_size", 1) batch_threshold = inputs.get("batch_threshold", 0.75) speed_factor = inputs.get("speed_factor", 1.0) split_bucket = inputs.get("split_bucket", True) return_fragment = inputs.get("return_fragment", False) fragment_interval = inputs.get("fragment_interval", 0.3) seed = inputs.get("seed", -1) seed = -1 if seed in ["", None] else seed actual_seed = set_seed(seed) parallel_infer = inputs.get("parallel_infer", True) repetition_penalty = inputs.get("repetition_penalty", 1.35)
if parallel_infer: print(i18n("并行推理模式已开启")) self.t2s_model.model.infer_panel = self.t2s_model.model.infer_panel_batch_infer else: print(i18n("并行推理模式已关闭")) self.t2s_model.model.infer_panel = self.t2s_model.model.infer_panel_naive_batched
if return_fragment: print(i18n("分段返回模式已开启")) if split_bucket: split_bucket = False print(i18n("分段返回模式不支持分桶处理,已自动关闭分桶处理"))
if split_bucket and speed_factor==1.0: print(i18n("分桶处理模式已开启")) elif speed_factor!=1.0: print(i18n("语速调节不支持分桶处理,已自动关闭分桶处理")) split_bucket = False else: print(i18n("分桶处理模式已关闭"))
if fragment_interval<0.01: fragment_interval = 0.01 print(i18n("分段间隔过小,已自动设置为0.01"))
no_prompt_text = False if prompt_text in [None, ""]: no_prompt_text = True
assert text_lang in self.configs.languages if not no_prompt_text: assert prompt_lang in self.configs.languages
if ref_audio_path in [None, ""] and \ ((self.prompt_cache["prompt_semantic"] is None) or (self.prompt_cache["refer_spec"] in [None, []])): raise ValueError("ref_audio_path cannot be empty, when the reference audio is not set using set_ref_audio()")
###### setting reference audio and prompt text preprocessing ######## t0 = ttime() if (ref_audio_path is not None) and (ref_audio_path != self.prompt_cache["ref_audio_path"]): if not os.path.exists(ref_audio_path): raise ValueError(f"{ref_audio_path} not exists") self.set_ref_audio(ref_audio_path) aux_ref_audio_paths = aux_ref_audio_paths if aux_ref_audio_paths is not None else [] paths = set(aux_ref_audio_paths)&set(self.prompt_cache["aux_ref_audio_paths"]) if not (len(list(paths)) == len(aux_ref_audio_paths) == len(self.prompt_cache["aux_ref_audio_paths"])): self.prompt_cache["aux_ref_audio_paths"] = aux_ref_audio_paths self.prompt_cache["refer_spec"] = [self.prompt_cache["refer_spec"][0]] for path in aux_ref_audio_paths: if path in [None, ""]: continue if not os.path.exists(path): print(i18n("音频文件不存在,跳过:{}").format(path)) continue self.prompt_cache["refer_spec"].append(self._get_ref_spec(path)) if not no_prompt_text: prompt_text = prompt_text.strip("\n") if (prompt_text[-1] not in splits): prompt_text += "。" if prompt_lang != "en" else "." print(i18n("实际输入的参考文本:"), prompt_text) if self.prompt_cache["prompt_text"] != prompt_text: self.prompt_cache["prompt_text"] = prompt_text self.prompt_cache["prompt_lang"] = prompt_lang phones, bert_features, norm_text = \ self.text_preprocessor.segment_and_extract_feature_for_text( prompt_text, prompt_lang, self.configs.version) self.prompt_cache["phones"] = phones self.prompt_cache["bert_features"] = bert_features self.prompt_cache["norm_text"] = norm_text
###### text preprocessing ######## t1 = ttime() data:list = None if not return_fragment: data = self.text_preprocessor.preprocess(text, text_lang, text_split_method, self.configs.version) if len(data) == 0: yield self.configs.sampling_rate, np.zeros(int(self.configs.sampling_rate), dtype=np.int16) return
batch_index_list:list = None data, batch_index_list = self.to_batch(data, prompt_data=self.prompt_cache if not no_prompt_text else None, batch_size=batch_size, threshold=batch_threshold, split_bucket=split_bucket, device=self.configs.device, precision=self.precision ) else: print(i18n("############ 切分文本 ############")) texts = self.text_preprocessor.pre_seg_text(text, text_lang, text_split_method) data = [] for i in range(len(texts)): if i%batch_size == 0: data.append([]) data[-1].append(texts[i]) def make_batch(batch_texts): batch_data = [] print(i18n("############ 提取文本Bert特征 ############")) for text in tqdm(batch_texts): phones, bert_features, norm_text = self.text_preprocessor.segment_and_extract_feature_for_text(text, text_lang, self.configs.version) if phones is None: continue res={ "phones": phones, "bert_features": bert_features, "norm_text": norm_text, } batch_data.append(res) if len(batch_data) == 0: return None batch, _ = self.to_batch(batch_data, prompt_data=self.prompt_cache if not no_prompt_text else None, batch_size=batch_size, threshold=batch_threshold, split_bucket=False, device=self.configs.device, precision=self.precision ) return batch[0]
t2 = ttime() try: print("############ 推理 ############") ###### inference ###### t_34 = 0.0 t_45 = 0.0 audio = [] for item in data: t3 = ttime() if return_fragment: item = make_batch(item) if item is None: continue
batch_phones:List[torch.LongTensor] = item["phones"] # batch_phones:torch.LongTensor = item["phones"] batch_phones_len:torch.LongTensor = item["phones_len"] all_phoneme_ids:torch.LongTensor = item["all_phones"] all_phoneme_lens:torch.LongTensor = item["all_phones_len"] all_bert_features:torch.LongTensor = item["all_bert_features"] norm_text:str = item["norm_text"] max_len = item["max_len"]
print(i18n("前端处理后的文本(每句):"), norm_text) if no_prompt_text : prompt = None else: prompt = self.prompt_cache["prompt_semantic"].expand(len(all_phoneme_ids), -1).to(self.configs.device)
pred_semantic_list, idx_list = self.t2s_model.model.infer_panel( all_phoneme_ids, all_phoneme_lens, prompt, all_bert_features, # prompt_phone_len=ph_offset, top_k=top_k, top_p=top_p, temperature=temperature, early_stop_num=self.configs.hz * self.configs.max_sec, max_len=max_len, repetition_penalty=repetition_penalty, ) t4 = ttime() t_34 += t4 - t3
refer_audio_spec:torch.Tensor = [item.to(dtype=self.precision, device=self.configs.device) for item in self.prompt_cache["refer_spec"]]
batch_audio_fragment = [] # ## vits并行推理 method 1 # pred_semantic_list = [item[-idx:] for item, idx in zip(pred_semantic_list, idx_list)] # pred_semantic_len = torch.LongTensor([item.shape[0] for item in pred_semantic_list]).to(self.configs.device) # pred_semantic = self.batch_sequences(pred_semantic_list, axis=0, pad_value=0).unsqueeze(0) # max_len = 0 # for i in range(0, len(batch_phones)): # max_len = max(max_len, batch_phones[i].shape[-1]) # batch_phones = self.batch_sequences(batch_phones, axis=0, pad_value=0, max_length=max_len) # batch_phones = batch_phones.to(self.configs.device) # batch_audio_fragment = (self.vits_model.batched_decode( # pred_semantic, pred_semantic_len, batch_phones, batch_phones_len,refer_audio_spec # ))
if speed_factor == 1.0: # ## vits并行推理 method 2 pred_semantic_list = [item[-idx:] for item, idx in zip(pred_semantic_list, idx_list)] upsample_rate = math.prod(self.vits_model.upsample_rates) audio_frag_idx = [pred_semantic_list[i].shape[0]*2*upsample_rate for i in range(0, len(pred_semantic_list))] audio_frag_end_idx = [ sum(audio_frag_idx[:i+1]) for i in range(0, len(audio_frag_idx))] all_pred_semantic = torch.cat(pred_semantic_list).unsqueeze(0).unsqueeze(0).to(self.configs.device) _batch_phones = torch.cat(batch_phones).unsqueeze(0).to(self.configs.device) _batch_audio_fragment = (self.vits_model.decode( all_pred_semantic, _batch_phones, refer_audio_spec, speed=speed_factor ).detach()[0, 0, :]) audio_frag_end_idx.insert(0, 0) batch_audio_fragment= [_batch_audio_fragment[audio_frag_end_idx[i-1]:audio_frag_end_idx[i]] for i in range(1, len(audio_frag_end_idx))] else: # ## vits串行推理 for i, idx in enumerate(idx_list): phones = batch_phones[i].unsqueeze(0).to(self.configs.device) _pred_semantic = (pred_semantic_list[i][-idx:].unsqueeze(0).unsqueeze(0)) # .unsqueeze(0)#mq要多unsqueeze一次 audio_fragment =(self.vits_model.decode( _pred_semantic, phones, refer_audio_spec, speed=speed_factor ).detach()[0, 0, :]) batch_audio_fragment.append( audio_fragment ) ###试试重建不带上prompt部分
t5 = ttime() t_45 += t5 - t4 if return_fragment: print("%.3f\t%.3f\t%.3f\t%.3f" % (t1 - t0, t2 - t1, t4 - t3, t5 - t4)) yield self.audio_postprocess([batch_audio_fragment], self.configs.sampling_rate, None, speed_factor, False, fragment_interval ) else: audio.append(batch_audio_fragment)
if self.stop_flag: yield self.configs.sampling_rate, np.zeros(int(self.configs.sampling_rate), dtype=np.int16) return
if not return_fragment: print("%.3f\t%.3f\t%.3f\t%.3f" % (t1 - t0, t2 - t1, t_34, t_45)) if len(audio) == 0: yield self.configs.sampling_rate, np.zeros(int(self.configs.sampling_rate), dtype=np.int16) return yield self.audio_postprocess(audio, self.configs.sampling_rate, batch_index_list, speed_factor, split_bucket, fragment_interval )
except Exception as e: traceback.print_exc() # 必须返回一个空音频, 否则会导致显存不释放。 yield self.configs.sampling_rate, np.zeros(int(self.configs.sampling_rate), dtype=np.int16) # 重置模型, 否则会导致显存释放不完全。 del self.t2s_model del self.vits_model self.t2s_model = None self.vits_model = None self.init_t2s_weights(self.configs.t2s_weights_path) self.init_vits_weights(self.configs.vits_weights_path) raise e finally: self.empty_cache() def empty_cache(self): try: gc.collect() # 触发gc的垃圾回收。避免内存一直增长。 if "cuda" in str(self.configs.device): torch.cuda.empty_cache() elif str(self.configs.device) == "mps": torch.mps.empty_cache() except: pass def audio_postprocess(self, audio:List[torch.Tensor], sr:int, batch_index_list:list=None, speed_factor:float=1.0, split_bucket:bool=True, fragment_interval:float=0.3 )->Tuple[int, np.ndarray]: zero_wav = torch.zeros( int(self.configs.sampling_rate * fragment_interval), dtype=self.precision, device=self.configs.device ) for i, batch in enumerate(audio): for j, audio_fragment in enumerate(batch): max_audio=torch.abs(audio_fragment).max()#简单防止16bit爆音 if max_audio>1: audio_fragment/=max_audio audio_fragment:torch.Tensor = torch.cat([audio_fragment, zero_wav], dim=0) audio[i][j] = audio_fragment.cpu().numpy() if split_bucket: audio = self.recovery_order(audio, batch_index_list) else: # audio = [item for batch in audio for item in batch] audio = sum(audio, []) audio = np.concatenate(audio, 0) audio = (audio * 32768).astype(np.int16) # try: # if speed_factor != 1.0: # audio = speed_change(audio, speed=speed_factor, sr=int(sr)) # except Exception as e: # print(f"Failed to change speed of audio: \n{e}") return sr, audio def speed_change(input_audio:np.ndarray, speed:float, sr:int): # 将 NumPy 数组转换为原始 PCM 流 raw_audio = input_audio.astype(np.int16).tobytes()
# 设置 ffmpeg 输入流 input_stream = ffmpeg.input('pipe:', format='s16le', acodec='pcm_s16le', ar=str(sr), ac=1)
# 变速处理 output_stream = input_stream.filter('atempo', speed)
# 输出流到管道 out, _ = ( output_stream.output('pipe:', format='s16le', acodec='pcm_s16le') .run(input=raw_audio, capture_stdout=True, capture_stderr=True) )
# 将管道输出解码为 NumPy 数组 processed_audio = np.frombuffer(out, np.int16)
return processed_audio123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
import os, sys
from tqdm import tqdmnow_dir = os.getcwd()sys.path.append(now_dir)
import reimport torchimport LangSegmentfrom text import chinesefrom typing import Dict, List, Tuplefrom text.cleaner import clean_textfrom text import cleaned_text_to_sequencefrom transformers import AutoModelForMaskedLM, AutoTokenizerfrom TTS_infer_pack.text_segmentation_method import split_big_text, splits, get_method as get_seg_method
from tools.i18n.i18n import I18nAuto, scan_language_list
language=os.environ.get("language","Auto")language=sys.argv[-1] if sys.argv[-1] in scan_language_list() else languagei18n = I18nAuto(language=language)punctuation = set(['!', '?', '…', ',', '.', '-'," "])
def get_first(text:str) -> str: pattern = "[" + "".join(re.escape(sep) for sep in splits) + "]" text = re.split(pattern, text)[0].strip() return text
def merge_short_text_in_array(texts:str, threshold:int) -> list: if (len(texts)) < 2: return texts result = [] text = "" for ele in texts: text += ele if len(text) >= threshold: result.append(text) text = "" if (len(text) > 0): if len(result) == 0: result.append(text) else: result[len(result) - 1] += text return result
class TextPreprocessor: def __init__(self, bert_model:AutoModelForMaskedLM, tokenizer:AutoTokenizer, device:torch.device): self.bert_model = bert_model self.tokenizer = tokenizer self.device = device def preprocess(self, text:str, lang:str, text_split_method:str, version:str="v2")->List[Dict]: print(i18n("############ 切分文本 ############")) text = self.replace_consecutive_punctuation(text) texts = self.pre_seg_text(text, lang, text_split_method) result = [] print(i18n("############ 提取文本Bert特征 ############")) for text in tqdm(texts): phones, bert_features, norm_text = self.segment_and_extract_feature_for_text(text, lang, version) if phones is None or norm_text=="": continue res={ "phones": phones, "bert_features": bert_features, "norm_text": norm_text, } result.append(res) return result
def pre_seg_text(self, text:str, lang:str, text_split_method:str): text = text.strip("\n") if len(text) == 0: return [] if (text[0] not in splits and len(get_first(text)) < 4): text = "。" + text if lang != "en" else "." + text print(i18n("实际输入的目标文本:")) print(text) seg_method = get_seg_method(text_split_method) text = seg_method(text) while "\n\n" in text: text = text.replace("\n\n", "\n")
_texts = text.split("\n") _texts = self.filter_text(_texts) _texts = merge_short_text_in_array(_texts, 5) texts = []
for text in _texts: # 解决输入目标文本的空行导致报错的问题 if (len(text.strip()) == 0): continue if not re.sub("\W+", "", text): # 检测一下,如果是纯符号,就跳过。 continue if (text[-1] not in splits): text += "。" if lang != "en" else "." # 解决句子过长导致Bert报错的问题 if (len(text) > 510): texts.extend(split_big_text(text)) else: texts.append(text) print(i18n("实际输入的目标文本(切句后):")) print(texts) return texts def segment_and_extract_feature_for_text(self, text:str, language:str, version:str="v1")->Tuple[list, torch.Tensor, str]: return self.get_phones_and_bert(text, language, version) def get_phones_and_bert(self, text:str, language:str, version:str, final:bool=False): if language in {"en", "all_zh", "all_ja", "all_ko", "all_yue"}: language = language.replace("all_","") if language == "en": LangSegment.setfilters(["en"]) formattext = " ".join(tmp["text"] for tmp in LangSegment.getTexts(text)) else: # 因无法区别中日韩文汉字,以用户输入为准 formattext = text while " " in formattext: formattext = formattext.replace(" ", " ") if language == "zh": if re.search(r'[A-Za-z]', formattext): formattext = re.sub(r'[a-z]', lambda x: x.group(0).upper(), formattext) formattext = chinese.mix_text_normalize(formattext) return self.get_phones_and_bert(formattext,"zh",version) else: phones, word2ph, norm_text = self.clean_text_inf(formattext, language, version) bert = self.get_bert_feature(norm_text, word2ph).to(self.device) elif language == "yue" and re.search(r'[A-Za-z]', formattext): formattext = re.sub(r'[a-z]', lambda x: x.group(0).upper(), formattext) formattext = chinese.mix_text_normalize(formattext) return self.get_phones_and_bert(formattext,"yue",version) else: phones, word2ph, norm_text = self.clean_text_inf(formattext, language, version) bert = torch.zeros( (1024, len(phones)), dtype=torch.float32, ).to(self.device) elif language in {"zh", "ja", "ko", "yue", "auto", "auto_yue"}: textlist=[] langlist=[] LangSegment.setfilters(["zh","ja","en","ko"]) if language == "auto": for tmp in LangSegment.getTexts(text): langlist.append(tmp["lang"]) textlist.append(tmp["text"]) elif language == "auto_yue": for tmp in LangSegment.getTexts(text): if tmp["lang"] == "zh": tmp["lang"] = "yue" langlist.append(tmp["lang"]) textlist.append(tmp["text"]) else: for tmp in LangSegment.getTexts(text): if tmp["lang"] == "en": langlist.append(tmp["lang"]) else: # 因无法区别中日韩文汉字,以用户输入为准 langlist.append(language) textlist.append(tmp["text"]) # print(textlist) # print(langlist) phones_list = [] bert_list = [] norm_text_list = [] for i in range(len(textlist)): lang = langlist[i] phones, word2ph, norm_text = self.clean_text_inf(textlist[i], lang, version) bert = self.get_bert_inf(phones, word2ph, norm_text, lang) phones_list.append(phones) norm_text_list.append(norm_text) bert_list.append(bert) bert = torch.cat(bert_list, dim=1) phones = sum(phones_list, []) norm_text = ''.join(norm_text_list)
if not final and len(phones) < 6: return self.get_phones_and_bert("." + text,language,version,final=True)
return phones, bert, norm_text
def get_bert_feature(self, text:str, word2ph:list)->torch.Tensor: with torch.no_grad(): inputs = self.tokenizer(text, return_tensors="pt") for i in inputs: inputs[i] = inputs[i].to(self.device) res = self.bert_model(**inputs, output_hidden_states=True) res = torch.cat(res["hidden_states"][-3:-2], -1)[0].cpu()[1:-1] assert len(word2ph) == len(text) phone_level_feature = [] for i in range(len(word2ph)): repeat_feature = res[i].repeat(word2ph[i], 1) phone_level_feature.append(repeat_feature) phone_level_feature = torch.cat(phone_level_feature, dim=0) return phone_level_feature.T def clean_text_inf(self, text:str, language:str, version:str="v2"): phones, word2ph, norm_text = clean_text(text, language, version) phones = cleaned_text_to_sequence(phones, version) return phones, word2ph, norm_text
def get_bert_inf(self, phones:list, word2ph:list, norm_text:str, language:str): language=language.replace("all_","") if language == "zh": feature = self.get_bert_feature(norm_text, word2ph).to(self.device) else: feature = torch.zeros( (1024, len(phones)), dtype=torch.float32, ).to(self.device)
return feature
def filter_text(self,texts): _text=[] if all(text in [None, " ", "\n",""] for text in texts): raise ValueError(i18n("请输入有效文本")) for text in texts: if text in [None, " ", ""]: pass else: _text.append(text) return _text
def replace_consecutive_punctuation(self,text): punctuations = ''.join(re.escape(p) for p in punctuation) pattern = f'([{punctuations}])([{punctuations}])+' result = re.sub(pattern, r'\1', text) return result
1from . import TTS, text_segmentation_method123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
import refrom typing import Callable
punctuation = set(['!', '?', '…', ',', '.', '-'," "])METHODS = dict()
def get_method(name:str)->Callable: method = METHODS.get(name, None) if method is None: raise ValueError(f"Method {name} not found") return method
def get_method_names()->list: return list(METHODS.keys())
def register_method(name): def decorator(func): METHODS[name] = func return func return decorator
splits = {",", "。", "?", "!", ",", ".", "?", "!", "~", ":", ":", "—", "…", }
def split_big_text(text, max_len=510): # 定义全角和半角标点符号 punctuation = "".join(splits)
# 切割文本 segments = re.split('([' + punctuation + '])', text) # 初始化结果列表和当前片段 result = [] current_segment = '' for segment in segments: # 如果当前片段加上新的片段长度超过max_len,就将当前片段加入结果列表,并重置当前片段 if len(current_segment + segment) > max_len: result.append(current_segment) current_segment = segment else: current_segment += segment # 将最后一个片段加入结果列表 if current_segment: result.append(current_segment) return result
def split(todo_text): todo_text = todo_text.replace("……", "。").replace("——", ",") if todo_text[-1] not in splits: todo_text += "。" i_split_head = i_split_tail = 0 len_text = len(todo_text) todo_texts = [] while 1: if i_split_head >= len_text: break # 结尾一定有标点,所以直接跳出即可,最后一段在上次已加入 if todo_text[i_split_head] in splits: i_split_head += 1 todo_texts.append(todo_text[i_split_tail:i_split_head]) i_split_tail = i_split_head else: i_split_head += 1 return todo_texts
# 不切@register_method("cut0")def cut0(inp): if not set(inp).issubset(punctuation): return inp else: return "/n"
# 凑四句一切@register_method("cut1")def cut1(inp): inp = inp.strip("\n") inps = split(inp) split_idx = list(range(0, len(inps), 4)) split_idx[-1] = None if len(split_idx) > 1: opts = [] for idx in range(len(split_idx) - 1): opts.append("".join(inps[split_idx[idx]: split_idx[idx + 1]])) else: opts = [inp] opts = [item for item in opts if not set(item).issubset(punctuation)] return "\n".join(opts)
# 凑50字一切@register_method("cut2")def cut2(inp): inp = inp.strip("\n") inps = split(inp) if len(inps) < 2: return inp opts = [] summ = 0 tmp_str = "" for i in range(len(inps)): summ += len(inps[i]) tmp_str += inps[i] if summ > 50: summ = 0 opts.append(tmp_str) tmp_str = "" if tmp_str != "": opts.append(tmp_str) # print(opts) if len(opts) > 1 and len(opts[-1]) < 50: ##如果最后一个太短了,和前一个合一起 opts[-2] = opts[-2] + opts[-1] opts = opts[:-1] opts = [item for item in opts if not set(item).issubset(punctuation)] return "\n".join(opts)
# 按中文句号。切@register_method("cut3")def cut3(inp): inp = inp.strip("\n") opts = ["%s" % item for item in inp.strip("。").split("。")] opts = [item for item in opts if not set(item).issubset(punctuation)] return "\n".join(opts)
#按英文句号.切@register_method("cut4")def cut4(inp): inp = inp.strip("\n") opts = ["%s" % item for item in inp.strip(".").split(".")] opts = [item for item in opts if not set(item).issubset(punctuation)] return "\n".join(opts)
# 按标点符号切# contributed by https://github.com/AI-Hobbyist/GPT-SoVITS/blob/main/GPT_SoVITS/inference_webui.py@register_method("cut5")def cut5(inp): inp = inp.strip("\n") punds = {',', '.', ';', '?', '!', '、', ',', '。', '?', '!', ';', ':', '…'} mergeitems = [] items = []
for i, char in enumerate(inp): if char in punds: if char == '.' and i > 0 and i < len(inp) - 1 and inp[i - 1].isdigit() and inp[i + 1].isdigit(): items.append(char) else: items.append(char) mergeitems.append("".join(items)) items = [] else: items.append(char)
if items: mergeitems.append("".join(items))
opt = [item for item in mergeitems if not set(item).issubset(punds)] return "\n".join(opt)
if __name__ == '__main__': method = get_method("cut5") print(method("你好,我是小明。你好,我是小红。你好,我是小刚。你好,我是小张。")) 1*.yaml12345678910111213141516171819202122232425262728293031train: seed: 1234 epochs: 300 batch_size: 8 gradient_accumulation: 4 save_every_n_epoch: 1 precision: 16 gradient_clip: 1.0optimizer: lr: 0.01 lr_init: 0.00001 lr_end: 0.0001 warmup_steps: 2000 decay_steps: 40000data: max_eval_sample: 8 max_sec: 54 num_workers: 1 pad_val: 1024 # same with EOS in modelmodel: vocab_size: 1025 phoneme_vocab_size: 512 embedding_dim: 512 hidden_dim: 512 head: 16 linear_units: 2048 n_layer: 12 dropout: 0 EOS: 1024inference: top_k: 512345678910111213141516171819202122232425262728293031train: seed: 1234 epochs: 300 batch_size: 8 gradient_accumulation: 4 save_every_n_epoch: 1 precision: 16-mixed gradient_clip: 1.0optimizer: lr: 0.01 lr_init: 0.00001 lr_end: 0.0001 warmup_steps: 2000 decay_steps: 40000data: max_eval_sample: 8 max_sec: 54 num_workers: 1 pad_val: 1024 # same with EOS in modelmodel: vocab_size: 1025 phoneme_vocab_size: 512 embedding_dim: 1024 hidden_dim: 1024 head: 16 linear_units: 2048 n_layer: 16 dropout: 0 EOS: 1024inference: top_k: 512345678910111213141516171819202122232425262728293031train: seed: 1234 epochs: 300 batch_size: 12 gradient_accumulation: 4 save_every_n_epoch: 1 precision: 16-mixed gradient_clip: 1.0optimizer: lr: 0.01 lr_init: 0.00001 lr_end: 0.0001 warmup_steps: 2000 decay_steps: 40000data: max_eval_sample: 8 max_sec: 54 num_workers: 1 pad_val: 1024 # same with EOS in modelmodel: vocab_size: 1025 phoneme_vocab_size: 512 embedding_dim: 1024 hidden_dim: 1024 head: 16 linear_units: 2048 n_layer: 6 dropout: 0 EOS: 1024inference: top_k: 512345678910111213141516171819202122232425262728293031train: seed: 1234 epochs: 20 batch_size: 8 save_every_n_epoch: 1 precision: 16-mixed gradient_clip: 1.0optimizer: lr: 0.01 lr_init: 0.00001 lr_end: 0.0001 warmup_steps: 2000 decay_steps: 40000data: max_eval_sample: 8 max_sec: 54 num_workers: 4 pad_val: 1024 # same with EOS in modelmodel: vocab_size: 1025 phoneme_vocab_size: 732 embedding_dim: 512 hidden_dim: 512 head: 16 linear_units: 2048 n_layer: 24 dropout: 0 EOS: 1024 random_bert: 0inference: top_k: 1512345678910111213141516171819202122232425262728293031train: seed: 1234 epochs: 20 batch_size: 8 save_every_n_epoch: 1 precision: 16-mixed gradient_clip: 1.0optimizer: lr: 0.01 lr_init: 0.00001 lr_end: 0.0001 warmup_steps: 2000 decay_steps: 40000data: max_eval_sample: 8 max_sec: 54 num_workers: 4 pad_val: 1024 # same with EOS in modelmodel: vocab_size: 1025 phoneme_vocab_size: 512 embedding_dim: 512 hidden_dim: 512 head: 16 linear_units: 2048 n_layer: 24 dropout: 0 EOS: 1024 random_bert: 0inference: top_k: 51234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677train: seed: 1234 epochs: 100 batch_size: 6 gradient_accumulation: 4 save_every_n_epoch: 1 precision: 32 gradient_clip: 1.0optimizer: lr: 0.01 lr_init: 0.00001 lr_end: 0.0001 warmup_steps: 2000 decay_steps: 40000data: max_eval_sample: 8 max_sec: 40 num_workers: 1 pad_val: 1024 # same with EOS in modelmodel: saving_path: "ckpt/" resume_checkpoint: null vocoder_config_path: "quantizer/new_ckpt/config.json" vocoder_ckpt_path: "quantizer/new_ckpt/g_00600000" datadir: "/home/liweiche/GigaSpeech/wavs" metapath: "/home/liweiche/GigaSpeech/train2.json" val_metapath: "/home/liweiche/GigaSpeech/dev2.json" sampledir: "logs/" pretrained_path: null lr: 0.0001 batch_size: 200.0 train_bucket_size: 8192 training_step: 800000 optim_flat_percent: 0.0 warmup_step: 50 adam_beta1: 0.9 adam_beta2: 0.98 ffd_size: 3072 hidden_size: 768 enc_nlayers: 6 dec_nlayers: 6 nheads: 12 ar_layer: 4 ar_ffd_size: 1024 ar_hidden_size: 256 ar_nheads: 4 aligner_softmax_temp: 1.0 layer_norm_eps: 0.00001 speaker_embed_dropout: 0.05 label_smoothing: 0.0 val_check_interval: 5000 check_val_every_n_epoch: 1 precision: "fp16" nworkers: 16 distributed: true accelerator: "ddp" version: null accumulate_grad_batches: 1 use_repetition_token: true use_repetition_gating: false repetition_penalty: 1.0 sampling_temperature: 1.0 top_k: -1 min_top_k: 3 top_p: 0.8 sample_num: 4 length_penalty_max_length: 15000 length_penalty_max_prob: 0.95 max_input_length: 2048 max_output_length: 2000 sample_rate: 16000 n_codes: 1024 n_cluster_groups: 1 phone_context_window: 4 phoneset_size: 1000inference: top_k: 51234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798{ "train": { "log_interval": 100, "eval_interval": 500, "seed": 1234, "epochs": 100, "learning_rate": 0.0001, "betas": [ 0.8, 0.99 ], "eps": 1e-09, "batch_size": 32, "fp16_run": true, "lr_decay": 0.999875, "segment_size": 20480, "init_lr_ratio": 1, "warmup_epochs": 0, "c_mel": 45, "c_kl": 1.0, "text_low_lr_rate": 0.4, "gpu_numbers": "0", "pretrained_s2G": "", "pretrained_s2D": "", "save_every_epoch": 5, "if_save_latest": false, "if_save_every_weights": true, "name": "sovits_training" }, "data": { "max_wav_value": 32768.0, "sampling_rate": 32000, "filter_length": 2048, "hop_length": 640, "win_length": 2048, "n_mel_channels": 128, "mel_fmin": 0.0, "mel_fmax": null, "add_blank": true, "n_speakers": 300, "cleaned_text": true, "exp_dir": "/workspace/GPT-SoVITS/logs/s2" }, "model": { "inter_channels": 192, "hidden_channels": 192, "filter_channels": 768, "n_heads": 2, "n_layers": 6, "kernel_size": 3, "p_dropout": 0.1, "resblock": "1", "resblock_kernel_sizes": [ 3, 7, 11 ], "resblock_dilation_sizes": [ [ 1, 3, 5 ], [ 1, 3, 5 ], [ 1, 3, 5 ] ], "upsample_rates": [ 10, 8, 2, 2, 2 ], "upsample_initial_channel": 512, "upsample_kernel_sizes": [ 16, 16, 8, 2, 2 ], "n_layers_q": 3, "use_spectral_norm": false, "gin_channels": 512, "semantic_frame_rate": "25hz", "freeze_quantizer": true }, "s2_ckpt_dir": "/workspace/GPT-SoVITS/logs/s2/sovits_training", "content_module": "cnhubert"}1234567891011121314151617181920212223242526272829303132gpu: n_card: 1 n_process_per_card: 2io: text_path: D:\RVC1006\GPT-SoVITS\GPT_SoVITS save_every_n_epoch: 1 precision: 16-mixed gradient_clip: 1.0optimizer: lr: 0.01 lr_init: 0.00001 lr_end: 0.0001 warmup_steps: 2000 decay_steps: 40000data: max_eval_sample: 8 max_sec: 54 num_workers: 1 pad_val: 1024 # same with EOS in modelmodel: vocab_size: 1025 phoneme_vocab_size: 512 embedding_dim: 512 hidden_dim: 512 head: 16 linear_units: 2048 n_layer: 24 dropout: 0 EOS: 1024 random_bert: 0inference: top_k: 5123456789101112131415161718192021222324custom: bert_base_path: GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large cnhuhbert_base_path: GPT_SoVITS/pretrained_models/chinese-hubert-base device: cuda is_half: true t2s_weights_path: GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s1bert25hz-5kh-longer-epoch=12-step=369668.ckpt version: v2 vits_weights_path: GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s2G2333k.pthdefault: bert_base_path: GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large cnhuhbert_base_path: GPT_SoVITS/pretrained_models/chinese-hubert-base device: cpu is_half: false t2s_weights_path: GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt version: v1 vits_weights_path: GPT_SoVITS/pretrained_models/s2G488k.pthdefault_v2: bert_base_path: GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large cnhuhbert_base_path: GPT_SoVITS/pretrained_models/chinese-hubert-base device: cpu is_half: false t2s_weights_path: GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s1bert25hz-5kh-longer-epoch=12-step=369668.ckpt version: v2 vits_weights_path: GPT_SoVITS/pretrained_models/gsv-v2final-pretrained/s2G2333k.pth123456from . import cnhubert, whisper_enc
content_module_map = { 'cnhubert': cnhubert, 'whisper': whisper_enc}.gitattributes+54
Docker/damo.sha256Added+3
Docker/download.pyAdded+5
Docker/download.shAdded+11
Docker/links.sha256Added+12
Docker/links.txtAdded+34
GPT_SoVITS/AR/__init__.pyAdded
GPT_SoVITS/AR/data/__init__.pyAdded
GPT_SoVITS/AR/data/bucket_sampler.pyAdded+163
GPT_SoVITS/AR/data/data_module.pyAdded+76
GPT_SoVITS/AR/data/dataset.pyAdded+321
GPT_SoVITS/AR/models/__init__.pyAdded
GPT_SoVITS/AR/models/t2s_lightning_module.pyAdded+141
GPT_SoVITS/AR/models/t2s_lightning_module_onnx.pyAdded+107
GPT_SoVITS/AR/models/t2s_model.pyAdded+901
GPT_SoVITS/AR/models/t2s_model_onnx.pyAdded+338
GPT_SoVITS/AR/models/utils.pyAdded+229
GPT_SoVITS/AR/modules/__init__.pyAdded
GPT_SoVITS/AR/modules/activation.pyAdded+428
GPT_SoVITS/AR/modules/activation_onnx.pyAdded+178
GPT_SoVITS/AR/modules/embedding.pyAdded+81
GPT_SoVITS/AR/modules/embedding_onnx.pyAdded+63
GPT_SoVITS/AR/modules/lr_schedulers.pyAdded+83
GPT_SoVITS/AR/modules/optim.pyAdded+622
GPT_SoVITS/AR/modules/patched_mha_with_cache.pyAdded+465
GPT_SoVITS/AR/modules/patched_mha_with_cache_onnx.pyAdded+92
GPT_SoVITS/AR/modules/scaling.pyAdded+335
GPT_SoVITS/AR/modules/transformer.pyAdded+378
GPT_SoVITS/AR/modules/transformer_onnx.pyAdded+292
GPT_SoVITS/AR/text_processing/__init__.pyAdded
GPT_SoVITS/AR/text_processing/phonemizer.pyAdded+79
GPT_SoVITS/AR/text_processing/symbols.pyAdded+10
GPT_SoVITS/AR/utils/__init__.pyAdded+37
GPT_SoVITS/AR/utils/initialize.pyAdded+38
GPT_SoVITS/AR/utils/io.pyAdded+34
GPT_SoVITS/TTS_infer_pack/TTS.pyAdded+1034
GPT_SoVITS/TTS_infer_pack/TextPreprocessor.pyAdded+244
GPT_SoVITS/TTS_infer_pack/__init__.pyAdded+1
GPT_SoVITS/TTS_infer_pack/text_segmentation_method.pyAdded+173
GPT_SoVITS/configs/.gitignoreAdded+1
GPT_SoVITS/configs/s1.yamlAdded+31
GPT_SoVITS/configs/s1big.yamlAdded+31
GPT_SoVITS/configs/s1big2.yamlAdded+31
GPT_SoVITS/configs/s1longer-v2.yamlAdded+31
GPT_SoVITS/configs/s1longer.yamlAdded+31
GPT_SoVITS/configs/s1mq.yamlAdded+77
GPT_SoVITS/configs/s2.jsonAdded+98
GPT_SoVITS/configs/train.yamlAdded+32
GPT_SoVITS/configs/tts_infer.yamlAdded+24
GPT_SoVITS/feature_extractor/__init__.pyAdded+6