### SpeechSpeechPretrainer Setup and Training Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Initialize and run the SpeechSpeechPretrainer for the first pretraining stage. Supports distributed training, learning rate scheduling, and checkpointing. ```python import torch from torch.utils.data import Dataset from spear_tts_pytorch import TextToSemantic, SpeechSpeechPretrainer class AudioDataset(Dataset): def __init__(self, num_samples=1000): self.num_samples = num_samples def __len__(self): return self.num_samples def __getitem__(self, idx): return (torch.randn(16000),) # 1 second of audio at 16kHz model = TextToSemantic( dim=512, num_text_token_ids=256, num_semantic_token_ids=500, heads=8, source_depth=4, target_depth=4 ) trainer = SpeechSpeechPretrainer( model=model, wav2vec=None, # Or pass HubertWithKmeans instance num_train_steps=10000, num_warmup_steps=500, batch_size=16, dataset=AudioDataset(1000), deletion_prob=0.6, reconstruct_seq=True, lr=3e-4, initial_lr=1e-5, grad_accum_every=2, max_grad_norm=0.5, valid_frac=0.05, log_every=10, save_results_every=100, save_model_every=1000, results_folder='./results_pretrain', force_clear_prev_results=True ) # Run training trainer.train() # Save checkpoint trainer.save('./checkpoints/pretrain_10000.pt') # Load checkpoint trainer.load('./checkpoints/pretrain_10000.pt') ``` -------------------------------- ### SemanticToTextTrainer Setup and Training Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Initialize and run the SemanticToTextTrainer for the backtranslation stage. Encoder and speech embeddings are frozen during this stage. ```python import torch from torch.utils.data import Dataset from spear_tts_pytorch import TextToSemantic, SemanticToTextTrainer class SemanticTextDataset(Dataset): def __init__(self, num_samples=1000): self.num_samples = num_samples def __len__(self): return self.num_samples def __getitem__(self, idx): semantic_tokens = torch.randint(0, 500, (128,)) text_tokens = torch.randint(0, 256, (64,)) return semantic_tokens, text_tokens model = TextToSemantic( dim=512, num_text_token_ids=256, num_semantic_token_ids=500, heads=8, source_depth=4, target_depth=4 ) trainer = SemanticToTextTrainer( model=model, num_train_steps=5000, num_warmup_steps=250, batch_size=16, dataset=SemanticTextDataset(1000), lr=3e-4, initial_lr=1e-5, grad_accum_every=2, max_grad_norm=0.5, valid_frac=0.05, log_every=10, save_results_every=100, save_model_every=1000, results_folder='./results_s2t', force_clear_prev_results=True ) # Run backtranslation training trainer.train() # Save and load checkpoints trainer.save('./checkpoints/s2t_5000.pt') trainer.load('./checkpoints/s2t_5000.pt', restore_optimizer=True) ``` -------------------------------- ### Install spear-tts-pytorch Source: https://github.com/lucidrains/spear-tts-pytorch/blob/main/README.md Install the spear-tts-pytorch package using pip. This is the first step to using the library. ```bash pip install spear-tts-pytorch ``` -------------------------------- ### TextToSemanticTrainer Setup and Training Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Initialize and run the TextToSemanticTrainer for the final stage. Supports combining real and pseudo-labeled data, and freezing encoder layers. ```python import torch from torch.utils.data import Dataset from spear_tts_pytorch import TextToSemantic, TextToSemanticTrainer class TextSemanticDataset(Dataset): def __init__(self, num_samples=500): self.num_samples = num_samples def __len__(self): return self.num_samples def __getitem__(self, idx): text_tokens = torch.randint(0, 256, (64,)) semantic_tokens = torch.randint(0, 500, (128,)) return text_tokens, semantic_tokens model = TextToSemantic( dim=512, num_text_token_ids=256, num_semantic_token_ids=500, heads=8, source_depth=4, target_depth=4, target_early_exit_layer=2 ) trainer = TextToSemanticTrainer( model=model, num_train_steps=10000, num_warmup_steps=500, batch_size=16, dataset=TextSemanticDataset(500), # Small labeled dataset generated_audio_text_dataset_folder='./generated_audio_text_pairs', # Pseudo-labeled data dataset_delimiter_id=-1, lr=3e-4, initial_lr=1e-5, grad_accum_every=2, max_grad_norm=0.5, valid_frac=0.05, freeze_encoder_layers_below=2, # Freeze lower encoder layers should_train_early_exit_layer_if_available=True, # Train speculative decoding log_every=10, save_results_every=100, save_model_every=1000, results_folder='./results_t2s', force_clear_prev_results=True ) # Run final training trainer.train() # Checkpointing trainer.save('./checkpoints/t2s_final.pt') ``` -------------------------------- ### Semantic To Text Wrapper for Backtranslation Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Utilize this wrapper for backtranslation training (speech-to-text). It trains the model to convert semantic audio tokens back to text, enabling pseudo-label dataset generation. The model must be initialized prior to instantiation. ```python import torch from spear_tts_pytorch import TextToSemantic, SemanticToTextWrapper model = TextToSemantic( dim=512, num_text_token_ids=256, num_semantic_token_ids=500, heads=8, source_depth=4, target_depth=4 ) # Create wrapper for speech-to-text training s2t_wrapper = SemanticToTextWrapper(model=model) # Training data semantic_token_ids = torch.randint(0, 500, (4, 128)) # Audio semantic tokens grapheme_token_ids = torch.randint(0, 256, (4, 64)) # Text grapheme tokens loss, logits = s2t_wrapper( semantic_token_ids=semantic_token_ids, grapheme_token_ids=grapheme_token_ids ) print(f"Backtranslation loss: {loss.item():.4f}") print(f"Output logits shape: {logits.shape}") ``` -------------------------------- ### MockDataset Usage Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Demonstrates the usage of MockDataset for testing purposes, which generates random audio tensors. ```python import torch from spear_tts_pytorch import MockDataset, GeneratedAudioTextDataset # MockDataset for testing - returns random audio tensors mock_ds = MockDataset(length=100) sample = mock_ds[0] print(f"Mock sample shape: {sample.shape}") # Output: Mock sample shape: torch.Size([1024]) ``` -------------------------------- ### Initialize TextToSemantic Model Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Initialize the core encoder-decoder transformer model for text-to-semantic conversion. Requires a pre-trained wav2vec model for audio tokenization. Configure model dimensions, attention heads, and transformer depths. ```python import torch from audiolm_pytorch import HubertWithKmeans from spear_tts_pytorch import TextToSemantic # Initialize wav2vec model for audio tokenization wav2vec = HubertWithKmeans( checkpoint_path='./hubert_base_ls960.pt', kmeans_path='./hubert_base_ls960_L9_km500.bin' ) # Create the TextToSemantic model model = TextToSemantic( wav2vec=wav2vec, dim=512, num_text_token_ids=256, heads=8, target_kv_heads=2, # Grouped query attention for efficient decoding source_depth=6, # Encoder transformer depth target_depth=6, # Decoder transformer depth attn_dropout=0.1, ff_dropout=0.1, attn_flash=True, # Enable flash attention cond_drop_prob=0.2, # Classifier-free guidance dropout target_early_exit_layer=3 # For speculative decoding ) # Forward pass for training (text-to-speech) text_tokens = torch.randint(0, 256, (2, 64)) # Batch of text tokens semantic_tokens = torch.randint(0, 500, (2, 128)) # Batch of semantic tokens loss = model( source=text_tokens, target=semantic_tokens, source_type='text', target_type='speech', return_loss=True ) print(f"Training loss: {loss.item():.4f}") # Output: Training loss: 6.2145 ``` -------------------------------- ### Speech Speech Pretraining Wrapper Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Use this wrapper for the initial self-supervised pretraining phase. It learns speech representations by reconstructing tokens with a specified deletion probability. Ensure the model is initialized before creating the wrapper. ```python import torch from spear_tts_pytorch import TextToSemantic, SpeechSpeechPretrainWrapper model = TextToSemantic( dim=512, num_text_token_ids=256, num_semantic_token_ids=500, heads=8, source_depth=4, target_depth=4 ) # Create pretraining wrapper with 60% token deletion pretrain_wrapper = SpeechSpeechPretrainWrapper( model=model, deletion_prob=0.6, reconstruct_seq=True, # Reconstruct full sequence mask_id=499 # Token ID used for masking ) # Training on raw audio or semantic tokens semantic_tokens = torch.randint(0, 500, (4, 256)) # Batch of semantic tokens loss, logits = pretrain_wrapper(semantic_tokens) print(f"Pretraining loss: {loss.item():.4f}") print(f"Output logits shape: {logits.shape}") ``` -------------------------------- ### Load Generated Audio-Text Dataset Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Loads pseudo-labeled pairs from a directory and integrates with PyTorch DataLoader for batch processing. ```python generated_ds = GeneratedAudioTextDataset( folder='./generated_audio_text_pairs', delimiter_id=-1 ) # Returns tuple of (semantic_tokens, text_tokens) semantic_tokens, text_tokens = generated_ds[0] print(f"Loaded pair - Semantic: {semantic_tokens.shape}, Text: {text_tokens.shape}") # Output: Loaded pair - Semantic: torch.Size([128]), Text: torch.Size([64]) # Use with DataLoader from torch.utils.data import DataLoader dataloader = DataLoader(generated_ds, batch_size=4, shuffle=True) for semantic_batch, text_batch in dataloader: print(f"Batch shapes - Semantic: {semantic_batch.shape}, Text: {text_batch.shape}") break # Output: Batch shapes - Semantic: torch.Size([4, 128]), Text: torch.Size([4, 64]) ``` -------------------------------- ### Execute Complete Training Pipeline Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Performs end-to-end training including speech-speech pretraining, semantic-to-text backtranslation, pseudo-label generation, and final text-to-semantic training. ```python import torch from audiolm_pytorch import HubertWithKmeans from spear_tts_pytorch import ( TextToSemantic, SpeechSpeechPretrainer, SemanticToTextTrainer, TextToSemanticTrainer, SemanticToTextDatasetGenerator, GeneratedAudioTextDataset, MockDataset ) # Initialize wav2vec for audio tokenization wav2vec = HubertWithKmeans( checkpoint_path='./hubert_base_ls960.pt', kmeans_path='./hubert_base_ls960_L9_km500.bin' ) # Create the TextToSemantic model model = TextToSemantic( wav2vec=wav2vec, dim=512, num_text_token_ids=256, heads=8, target_kv_heads=2, source_depth=6, target_depth=6, attn_flash=True, cond_drop_prob=0.2, target_early_exit_layer=3 ) # Stage 1: Speech-Speech Pretraining pretrain_dataset = MockDataset(10000) pretrainer = SpeechSpeechPretrainer( model=model, wav2vec=wav2vec, dataset=pretrain_dataset, num_train_steps=50000, num_warmup_steps=1000, batch_size=32, deletion_prob=0.6, reconstruct_seq=True, results_folder='./results/stage1_pretrain' ) pretrainer.train() # Stage 2: Semantic-to-Text Backtranslation # (requires paired semantic-text dataset) s2t_trainer = SemanticToTextTrainer( model=model, dataset=your_paired_dataset, # Your semantic-text pairs num_train_steps=20000, num_warmup_steps=500, batch_size=32, results_folder='./results/stage2_backtranslation' ) s2t_trainer.train() # Generate pseudo-labeled dataset unlabeled_audio = MockDataset(100000) generator = SemanticToTextDatasetGenerator( model=model, dataset=unlabeled_audio, folder='./pseudo_labeled_data', batch_size=16 ) generator(max_length=2048, beam_search_decode=True) # Stage 3: Final Text-to-Semantic Training t2s_trainer = TextToSemanticTrainer( model=model, dataset=your_small_labeled_dataset, # Small labeled dataset generated_audio_text_dataset_folder='./pseudo_labeled_data', num_train_steps=100000, num_warmup_steps=2000, batch_size=32, freeze_encoder_layers_below=3, should_train_early_exit_layer_if_available=True, results_folder='./results/stage3_final' ) t2s_trainer.train() # Inference with trained model text_input = torch.randint(0, 256, (1, 64)) semantic_output = model.generate( source=text_input, source_type='text', target_type='speech', max_length=1024, spec_decode=True, # Fast speculative decoding cond_scale=2.0 # Classifier-free guidance ) ``` -------------------------------- ### Semantic To Text Dataset Generator Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Use this generator to create pseudo-labeled audio-text datasets from unlabeled audio data. The generated dataset is suitable for the final text-to-semantic training stage. Configure folder paths, batch size, and padding IDs. ```python import torch from torch.utils.data import Dataset from spear_tts_pytorch import ( TextToSemantic, SemanticToTextDatasetGenerator, GeneratedAudioTextDataset, MockDataset ) model = TextToSemantic( dim=512, num_text_token_ids=256, num_semantic_token_ids=500, heads=8, source_depth=4, target_depth=4 ) # Create dataset of audio samples (or use your own) audio_dataset = MockDataset(100) # Initialize dataset generator dataset_generator = SemanticToTextDatasetGenerator( model=model, dataset=audio_dataset, folder='./generated_audio_text_pairs', batch_size=4, delimiter_id=-1, audio_pad_id=None, text_pad_id=0 ) # Generate pseudo-labeled dataset dataset_generator( max_length=1024, beam_search_decode=True ) # Load the generated dataset for training generated_dataset = GeneratedAudioTextDataset( folder='./generated_audio_text_pairs', delimiter_id=-1 ) # Access a sample (returns semantic_tokens, text_tokens tuple) semantic_tokens, text_tokens = generated_dataset[0] print(f"Semantic tokens: {semantic_tokens.shape}, Text tokens: {text_tokens.shape}") ``` -------------------------------- ### Text To Semantic Wrapper for TTS Training Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Employ this wrapper for the final text-to-speech training stage. It trains the model to convert text tokens to semantic audio tokens. Set `target_early_exit_layer` in the model to enable early exit loss for speculative decoding. ```python import torch from spear_tts_pytorch import TextToSemantic, TextToSemanticWrapper model = TextToSemantic( dim=512, num_text_token_ids=256, num_semantic_token_ids=500, heads=8, source_depth=4, target_depth=4, target_early_exit_layer=2 # Enable early exit for speculative decoding ) # Create wrapper for text-to-speech training t2s_wrapper = TextToSemanticWrapper(model=model) # Training data grapheme_token_ids = torch.randint(0, 256, (4, 64)) # Text input semantic_token_ids = torch.randint(0, 500, (4, 128)) # Audio target loss, logits = t2s_wrapper( grapheme_token_ids=grapheme_token_ids, semantic_token_ids=semantic_token_ids, return_early_exit_loss=True # Train early exit layer for speculative decoding ) print(f"Text-to-semantic loss: {loss.item():.4f}") print(f"Output logits shape: {logits.shape}") ``` -------------------------------- ### Spear-TTS Text-to-Semantic Model Usage Source: https://github.com/lucidrains/spear-tts-pytorch/blob/main/README.md Demonstrates how to initialize and use the TextToSemantic model. Requires a pre-trained wav2vec model and dataset generator. The model is used for converting text into semantic representations. ```python import torch from audiolm_pytorch import HubertWithKmeans from spear_tts_pytorch import ( TextToSemantic, SemanticToTextDatasetGenerator, GeneratedAudioTextDataset, MockDataset ) wav2vec = HubertWithKmeans( checkpoint_path = './hubert_base_ls960.pt', kmeans_path = './hubert_base_ls960_L9_km500.bin' ) model = TextToSemantic( wav2vec = wav2vec, dim = 512, num_text_token_ids = 256, heads = 8, target_kv_heads = 2, # grouped query attention, for memory efficient decoding source_depth = 1, target_depth = 1 ) ds = MockDataset(10) dataset_generator = SemanticToTextDatasetGenerator( model = model, dataset = ds, folder = './output_folder' ) dataset_generator(max_length = 2) generated_dataset = GeneratedAudioTextDataset( folder = './output_folder' ) assert len(generated_dataset) == 10 ``` -------------------------------- ### Generate Sequences with TextToSemantic Source: https://context7.com/lucidrains/spear-tts-pytorch/llms.txt Generate target sequences from source sequences using autoregressive decoding. Supports standard sampling, beam search, and speculative decoding. Classifier-free guidance can be applied for improved quality. Use `return_target_mask=True` for speech-to-text tasks. ```python import torch from spear_tts_pytorch import TextToSemantic model = TextToSemantic( dim=512, num_text_token_ids=256, num_semantic_token_ids=500, heads=8, target_kv_heads=2, source_depth=4, target_depth=4, cond_drop_prob=0.2, target_early_exit_layer=2 ) # Text-to-speech generation with standard sampling text_tokens = torch.randint(0, 256, (1, 32)) semantic_output = model.generate( source=text_tokens, source_type='text', target_type='speech', max_length=512, temperature=0.9, filter_logits_fn=top_k, filter_fn_kwargs={'thres': 0.9} ) print(f"Generated semantic tokens shape: {semantic_output.shape}") # Output: Generated semantic tokens shape: torch.Size([1, 512]) # Text-to-speech with beam search decoding semantic_output_beam = model.generate( source=text_tokens, source_type='text', target_type='speech', max_length=512, beam_search_decode=True, beam_size=4, temperature=0.7 ) # Text-to-speech with speculative decoding (faster inference) semantic_output_spec = model.generate( source=text_tokens, source_type='text', target_type='speech', max_length=512, spec_decode=True, spec_decode_gamma=5, spec_decode_lenience=1.0 ) # Speech-to-text backtranslation with classifier-free guidance semantic_input = torch.randint(0, 500, (1, 128)) text_output = model.generate( source=semantic_input, source_type='speech', target_type='text', max_length=256, cond_scale=2.0, # Classifier-free guidance scale return_target_mask=True ) text_tokens, text_mask = text_output print(f"Generated text tokens: {text_tokens.shape}, mask: {text_mask.shape}") # Output: Generated text tokens: torch.Size([1, 256]), mask: torch.Size([1, 256]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.