### Multi-GPU Training Setup with Accelerate Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Demonstrates how to configure and launch multi-GPU training for AudioLM using the 🤗 Accelerate library. First, run 'accelerate config' to set up the environment, then use 'accelerate launch train.py' to start the training script. ```bash $ accelerate config ``` ```bash $ accelerate launch train.py ``` -------------------------------- ### Install AudioLM-Pytorch Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Install the package via pip. ```bash $ pip install audiolm-pytorch ``` -------------------------------- ### Install audiolm-pytorch with Pip Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Installs the audiolm-pytorch library and its dependencies using pip. This command should be run in an environment with Python and pip available. ```python !pip install audiolm-pytorch ``` -------------------------------- ### Load SoundStream from Checkpoint Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Initialize and load a SoundStream model directly from a saved checkpoint file using a class method. This simplifies setup by automatically applying saved configurations. ```python from audiolm_pytorch import SoundStream soundstream = SoundStream.init_and_load_from('./path/to/checkpoint.pt') ``` -------------------------------- ### Initialize Fine Transformer Trainer Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Configures the FineTransformer and its trainer. This setup requires a trained SoundStream codec. The trainer is initialized with the transformer, codec, dataset path, batch size, and training steps. ```python soundstream = SoundStream.init_and_load_from('/path/to/trained/soundstream.pt') fine_transformer = FineTransformer( num_coarse_quantizers = 3, num_fine_quantizers = 5, codebook_size = 1024, dim = 512, depth = 6, flash_attn = True ) trainer = FineTransformerTrainer( transformer = fine_transformer, codec = soundstream, folder = '/path/to/audio/files', batch_size = 1, data_max_length = 320 * 32, num_train_steps = 1_000_000 ) trainer.train() ``` -------------------------------- ### Configure and Launch Distributed Training Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Use HuggingFace Accelerate to manage multi-GPU training environments. ```bash # Configure accelerate (run once) accelerate config # Launch distributed training accelerate launch train.py ``` -------------------------------- ### Initialize SoundStream Trainer Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Sets up the SoundStream trainer for AudioLM. This requires the `SoundStream` and `SoundStreamTrainer` classes. The `dataset_folder` must be defined. Note the effective batch size calculation and the reduced `num_train_steps` for demonstration. ```python soundstream = SoundStream( codebook_size = 1024, rq_num_quantizers = 8, ) trainer = SoundStreamTrainer( soundstream, folder = dataset_folder, batch_size = 4, grad_accum_every = 8, # effective batch size of 32 data_max_length = 320 * 32, save_results_every = 2, save_model_every = 4, num_train_steps = 9 ).cuda() # NOTE: I changed num_train_steps to 9 (aka 8 + 1) from 10000 to make things go faster for demo purposes ``` -------------------------------- ### Initialize AudioLM and MusicLM SoundStream Presets Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Use these pre-configured classes to instantiate SoundStream models with hyperparameters optimized for speech or music tasks. ```python from audiolm_pytorch import AudioLMSoundStream, MusicLMSoundStream # AudioLM preset: 16kHz, 12 quantizers, optimized for speech audiolm_soundstream = AudioLMSoundStream( codebook_size = 1024, # Default: strides = (2, 4, 5, 8), target_sample_hz = 16000, rq_num_quantizers = 12 ) # MusicLM preset: 24kHz, 12 quantizers, optimized for music musiclm_soundstream = MusicLMSoundStream( codebook_size = 1024, # Default: strides = (3, 4, 5, 8), target_sample_hz = 24000, rq_num_quantizers = 12 ) ``` -------------------------------- ### Initialize and Train SemanticTransformer Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Downloads required HuBERT checkpoints and configures the SemanticTransformer trainer. ```python # hubert checkpoints can be downloaded at # https://github.com/facebookresearch/fairseq/tree/main/examples/hubert if not os.path.isdir("hubert"): os.makedirs("hubert") if not os.path.isfile(hubert_ckpt): hubert_ckpt_download = f"https://dl.fbaipublicfiles.com/{hubert_ckpt}" urllib.request.urlretrieve(hubert_ckpt_download, f"./{hubert_ckpt}") if not os.path.isfile(hubert_quantizer): hubert_quantizer_download = f"https://dl.fbaipublicfiles.com/{hubert_quantizer}" urllib.request.urlretrieve(hubert_quantizer_download, f"./{hubert_quantizer}") wav2vec = HubertWithKmeans( checkpoint_path = f'./{hubert_ckpt}', kmeans_path = f'./{hubert_quantizer}' ) semantic_transformer = SemanticTransformer( num_semantic_tokens = wav2vec.codebook_size, dim = 1024, depth = 6 ).cuda() trainer = SemanticTransformerTrainer( transformer = semantic_transformer, wav2vec = wav2vec, folder = dataset_folder, batch_size = 1, data_max_length = 320 * 32, num_train_steps = 1 ) trainer.train() ``` -------------------------------- ### Initialize Coarse Transformer Trainer Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Sets up the CoarseTransformer and its trainer. This requires a pre-trained wav2vec checkpoint and a trained SoundStream codec. The trainer is configured with the dataset path, batch size, and number of training steps. ```python wav2vec = HubertWithKmeans( checkpoint_path = './hubert/hubert_base_ls960.pt', kmeans_path = './hubert/hubert_base_ls960_L9_km500.bin' ) soundstream = SoundStream.init_and_load_from('/path/to/trained/soundstream.pt') coarse_transformer = CoarseTransformer( num_semantic_tokens = wav2vec.codebook_size, codebook_size = 1024, num_coarse_quantizers = 3, dim = 512, depth = 6, flash_attn = True ) trainer = CoarseTransformerTrainer( transformer = coarse_transformer, codec = soundstream, wav2vec = wav2vec, folder = '/path/to/audio/files', batch_size = 1, data_max_length = 320 * 32, num_train_steps = 1_000_000 ) trainer.train() ``` -------------------------------- ### Initialize Text-Conditioned Semantic Transformer Trainer Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Sets up the SemanticTransformer for text-conditioned audio generation. Requires a wav2vec model and a custom dataset that returns both audio and its text description. The trainer is configured with dataset, batch size, gradient accumulation, and training steps. ```python import torch from audiolm_pytorch import HubertWithKmeans, SemanticTransformer, SemanticTransformerTrainer from torch.utils.data import Dataset wav2vec = HubertWithKmeans( checkpoint_path = './hubert/hubert_base_ls960.pt', kmeans_path = './hubert/hubert_base_ls960_L9_km500.bin' ) semantic_transformer = SemanticTransformer( num_semantic_tokens = 500, dim = 1024, depth = 6, has_condition = True, # this will have to be set to True cond_as_self_attn_prefix = True # whether to condition as prefix to self attention, instead of cross attention, as was done in 'VALL-E' paper ).cuda() # mock text audio dataset (as an example) # you will have to extend your own from `Dataset`, and return an audio tensor as well as a string (the audio description) in any order (the framework will autodetect and route it into the transformer) class MockTextAudioDataset(Dataset): def __init__(self, length = 100, audio_length = 320 * 32): super().__init__() self.audio_length = audio_length self.len = length def __len__(self): return self.len def __getitem__(self, idx): mock_audio = torch.randn(self.audio_length) mock_caption = 'audio caption' return mock_caption, mock_audio dataset = MockTextAudioDataset() # instantiate semantic transformer trainer and train trainer = SemanticTransformerTrainer( transformer = semantic_transformer, wav2vec = wav2vec, dataset = dataset, batch_size = 4, grad_accum_every = 8, data_max_length = 320 * 32, num_train_steps = 1_000_000 ) trainer.train() # after much training above sample = trainer.generate(text = ['sound of rain drops on the rooftops'], batch_size = 1, max_length = 2) # (1, < 128) - may terminate early if it detects [eos] ``` -------------------------------- ### Configure CoarseTransformer Trainer Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Sets up the CoarseTransformer with a pre-trained SoundStream codec and HuBERT quantizer. ```python wav2vec = HubertWithKmeans( checkpoint_path = f'./{hubert_ckpt}', kmeans_path = f'./{hubert_quantizer}' ) soundstream = SoundStream( codebook_size = 1024, rq_num_quantizers = 8, ) soundstream.load(f"./{soundstream_ckpt}") coarse_transformer = CoarseTransformer( num_semantic_tokens = wav2vec.codebook_size, codebook_size = 1024, num_coarse_quantizers = 3, dim = 512, depth = 6 ) trainer = CoarseTransformerTrainer( transformer = coarse_transformer, codec = soundstream, wav2vec = wav2vec, folder = dataset_folder, batch_size = 1, data_max_length = 320 * 32, save_results_every = 2, save_model_every = 4, num_train_steps = 9 ) # NOTE: I changed num_train_steps to 9 (aka 8 + 1) from 10000 to make things go faster for demo purposes ``` -------------------------------- ### Create Placeholder Dataset Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Generates a directory structure and populates it with placeholder WAV files. This function uses `get_sinewave` and `save_wav`. It will not create the dataset if the directory already exists. ```python import os def make_placeholder_dataset(): # Make a placeholder dataset with a few .wav files that you can "train" on, just to verify things work e2e if os.path.isdir(dataset_folder): return os.makedirs(dataset_folder) save_wav(f"{dataset_folder}/example.wav", get_sinewave()) save_wav(f"{dataset_folder}/example2.wav", get_sinewave(duration_ms=500)) os.makedirs(f"{dataset_folder}/subdirectory") save_wav(f"{dataset_folder}/subdirectory/example.wav", get_sinewave(freq=330.0)) make_placeholder_dataset() ``` -------------------------------- ### Integrate SoundStreamTrainer with Weights & Biases Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Enable Weights & Biases (wandb) tracking for SoundStream training. Set `use_wandb_tracking` to True and use the `wandb_tracker` context manager to specify project and run names. ```python trainer = SoundStreamTrainer( soundstream, ..., use_wandb_tracking = True ) # wrap .train() with contextmanager, specifying project and run name with trainer.wandb_tracker(project = 'soundstream', run = 'baseline'): trainer.train() ``` -------------------------------- ### Initialize and Use AudioLM Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Combines all trained components (wav2vec, codec, semantic, coarse, and fine transformers) into a single AudioLM model for audio generation. Supports generation from scratch, with priming audio, or with text conditioning. ```python from audiolm_pytorch import AudioLM # Assuming wav2vec, soundstream, semantic_transformer, coarse_transformer, fine_transformer are already initialized and trained # Initialize AudioLM with trained components # audiolm = AudioLM( # wav2vec = wav2vec, # codec = soundstream, # semantic_transformer = semantic_transformer, # coarse_transformer = coarse_transformer, # fine_transformer = fine_transformer # ) # generated_wav = audiolm(batch_size = 1) # or with priming # generated_wav_with_prime = audiolm(prime_wave = torch.randn(1, 320 * 8)) # or with text condition, if given # generated_wav_with_text_condition = audiolm(text = ['chirping of birds and the distant echos of bells']) ``` -------------------------------- ### Define dataset and checkpoint paths Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Sets up essential variables for dataset locations and pre-trained model checkpoints. Ensure these paths are updated to reflect your project structure and downloaded model locations. ```python # define all dataset paths, checkpoints, etc dataset_folder = "placeholder_dataset" soundstream_ckpt = "results/soundstream.8.pt" # this can change depending on number of steps hubert_ckpt = 'hubert/hubert_base_ls960.pt' hubert_quantizer = f'hubert/hubert_base_ls960_L9_km500.bin' # listed in row "HuBERT Base (~95M params)", column Quantizer ``` -------------------------------- ### Assemble and Run AudioLM Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Combines pre-trained components into the AudioLM pipeline to generate audio. ```python # Everything together audiolm = AudioLM( wav2vec = wav2vec, codec = soundstream, semantic_transformer = semantic_transformer, coarse_transformer = coarse_transformer, fine_transformer = fine_transformer ) generated_wav = audiolm(batch_size = 1) ``` -------------------------------- ### Initialize EncodecWrapper Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Create an instance of the EncodecWrapper to use the pretrained 24kHz Encodec model. ```python from audiolm_pytorch import EncodecWrapper encodec = EncodecWrapper() ``` -------------------------------- ### Initialize and Train Fine Transformer Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Initializes the FineTransformer for generating fine acoustic tokens from coarse tokens. Requires SoundStream for acoustic tokenization and a trainer for the training process. ```python from audiolm_pytorch import SoundStream, FineTransformer, FineTransformerTrainer soundstream = SoundStream.init_and_load_from('/path/to/trained/soundstream.pt') fine_transformer = FineTransformer( num_coarse_quantizers = 3, num_fine_quantizers = 5, codebook_size = 1024, dim = 512, depth = 6, flash_attn = True ) trainer = FineTransformerTrainer( transformer = fine_transformer, codec = soundstream, folder = '/path/to/audio/files', batch_size = 1, data_max_length = 320 * 32, num_train_steps = 1_000_000 ) trainer.train() ``` -------------------------------- ### Initialize and Train SoundStream Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Configures the SoundStream neural codec with residual vector quantization and trains it using the SoundStreamTrainer. ```python from audiolm_pytorch import SoundStream, SoundStreamTrainer # Initialize SoundStream with residual vector quantization soundstream = SoundStream( codebook_size = 4096, rq_num_quantizers = 8, rq_groups = 2, # multi-headed residual VQ use_lookup_free_quantizer = True, # use lookup-free quantization use_finite_scalar_quantizer = False, attn_window_size = 128, # local attention at bottleneck attn_depth = 2 # transformer blocks in bottleneck ) # Train SoundStream on audio files trainer = SoundStreamTrainer( soundstream, folder = '/path/to/audio/files', batch_size = 4, grad_accum_every = 8, # effective batch size of 32 data_max_length_seconds = 2, # train on 2 second audio num_train_steps = 1_000_000 ).cuda() trainer.train() # After training, use for encoding/decoding soundstream.eval() audio = torch.randn(10080).cuda() recons = soundstream(audio, return_recons_only = True) # (1, 10080) ``` -------------------------------- ### Initialize and Use AudioLM for End-to-End Generation Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Combines SemanticTransformer, CoarseTransformer, and FineTransformer for end-to-end audio generation. Supports unconditional generation, audio priming, and text conditioning. ```python from audiolm_pytorch import AudioLM, HubertWithKmeans, SoundStream from audiolm_pytorch import SemanticTransformer, CoarseTransformer, FineTransformer # Initialize components (assuming all are trained) wav2vec = HubertWithKmeans( checkpoint_path = './hubert/hubert_base_ls960.pt', kmeans_path = './hubert/hubert_base_ls960_L9_km500.bin' ) soundstream = SoundStream.init_and_load_from('./soundstream.pt') semantic_transformer = SemanticTransformer( num_semantic_tokens = wav2vec.codebook_size, dim = 1024, depth = 6 ) coarse_transformer = CoarseTransformer( num_semantic_tokens = wav2vec.codebook_size, codebook_size = 1024, num_coarse_quantizers = 3, dim = 512, depth = 6 ) fine_transformer = FineTransformer( num_coarse_quantizers = 3, num_fine_quantizers = 5, codebook_size = 1024, dim = 512, depth = 6 ) # Create AudioLM with all components audiolm = AudioLM( wav2vec = wav2vec, codec = soundstream, semantic_transformer = semantic_transformer, coarse_transformer = coarse_transformer, fine_transformer = fine_transformer ) ``` ```python # Generate audio unconditionally generated_wav = audiolm(batch_size = 1) ``` ```python # Generate with audio priming generated_wav_with_prime = audiolm(prime_wave = torch.randn(1, 320 * 8)) ``` ```python # Generate with text conditioning (if transformers have has_condition=True) generated_wav_with_text = audiolm( text = ['chirping of birds and the distant echos of bells'] ) ``` -------------------------------- ### Initialize and Train SoundStream Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Instantiate and train the SoundStream model. Configure parameters like codebook size, quantizer groups, and attention settings. Training requires specifying an audio file folder, batch size, and training steps. ```python from audiolm_pytorch import SoundStream, SoundStreamTrainer soundstream = SoundStream( codebook_size = 4096, rq_num_quantizers = 8, rq_groups = 2, # this paper proposes using multi-headed residual vector quantization - https://arxiv.org/abs/2305.02765 use_lookup_free_quantizer = True, # whether to use residual lookup free quantization - there are now reports of successful usage of this unpublished technique use_finite_scalar_quantizer = False, # whether to use residual finite scalar quantization attn_window_size = 128, # local attention receptive field at bottleneck attn_depth = 2 # 2 local attention transformer blocks - the soundstream folks were not experts with attention, so i took the liberty to add some. encodec went with lstms, but attention should be better ) trainer = SoundStreamTrainer( soundstream, folder = '/path/to/audio/files', batch_size = 4, grad_accum_every = 8, # effective batch size of 32 data_max_length_seconds = 2, # train on 2 second audio num_train_steps = 1_000_000 ).cuda() trainer.train() ``` -------------------------------- ### Train FineTransformer Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Initializes the SoundStream codec and FineTransformer, then executes the training loop. ```python soundstream = SoundStream( codebook_size = 1024, rq_num_quantizers = 8, ) soundstream.load(f"./{soundstream_ckpt}") fine_transformer = FineTransformer( num_coarse_quantizers = 3, num_fine_quantizers = 5, codebook_size = 1024, dim = 512, depth = 6 ) trainer = FineTransformerTrainer( transformer = fine_transformer, codec = soundstream, folder = dataset_folder, batch_size = 1, data_max_length = 320 * 32, num_train_steps = 9 ) # NOTE: I changed num_train_steps to 9 (aka 8 + 1) from 10000 to make things go faster for demo purposes # adjusting save_*_every variables for the same reason trainer.train() ``` -------------------------------- ### Implement Distributed Training Script Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt The trainer automatically handles distributed execution when launched via Accelerate. ```python # train.py from audiolm_pytorch import SoundStream, SoundStreamTrainer soundstream = SoundStream(codebook_size = 4096, rq_num_quantizers = 8) trainer = SoundStreamTrainer( soundstream, folder = '/path/to/audio/files', batch_size = 4, grad_accum_every = 8, data_max_length_seconds = 2, num_train_steps = 1_000_000 ) # Accelerate handles multi-GPU automatically trainer.train() ``` -------------------------------- ### Enable Weights & Biases Tracking for Trainers Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Enable experiment tracking by setting use_wandb_tracking to True and wrapping the training loop in the wandb_tracker context manager. ```python from audiolm_pytorch import SoundStream, SoundStreamTrainer soundstream = SoundStream( codebook_size = 4096, rq_num_quantizers = 8 ) trainer = SoundStreamTrainer( soundstream, folder = '/path/to/audio/files', batch_size = 4, grad_accum_every = 8, data_max_length_seconds = 2, num_train_steps = 1_000_000, use_wandb_tracking = True # Enable W&B tracking ) # Wrap training with W&B context manager with trainer.wandb_tracker(project = 'soundstream', run = 'baseline'): trainer.train() ``` -------------------------------- ### Initialize Semantic Transformer Trainer Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Initializes the SemanticTransformer and its trainer. Requires a pre-trained wav2vec checkpoint and kmeans model. The trainer is configured with dataset path, batch size, and training steps. ```python wav2vec = HubertWithKmeans( checkpoint_path = './hubert/hubert_base_ls960.pt', kmeans_path = './hubert/hubert_base_ls960_L9_km500.bin' ) semantic_transformer = SemanticTransformer( num_semantic_tokens = wav2vec.codebook_size, dim = 1024, depth = 6, flash_attn = True ).cuda() trainer = SemanticTransformerTrainer( transformer = semantic_transformer, wav2vec = wav2vec, folder ='/path/to/audio/files', batch_size = 1, data_max_length = 320 * 32, num_train_steps = 1 ) trainer.train() ``` -------------------------------- ### Train SoundStream Model Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Initiates the training process for a SoundStream model. ```python trainer.train() ``` -------------------------------- ### Initialize and Train Coarse Transformer Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Initializes the CoarseTransformer for generating coarse acoustic tokens conditioned on semantic tokens. Requires HubertWithKmeans for semantic tokenization and SoundStream for acoustic tokenization, along with a trainer. ```python from audiolm_pytorch import HubertWithKmeans, SoundStream, CoarseTransformer, CoarseTransformerTrainer wav2vec = HubertWithKmeans( checkpoint_path = './hubert/hubert_base_ls960.pt', kmeans_path = './hubert/hubert_base_ls960_L9_km500.bin' ) soundstream = SoundStream.init_and_load_from('/path/to/trained/soundstream.pt') coarse_transformer = CoarseTransformer( num_semantic_tokens = wav2vec.codebook_size, codebook_size = 1024, num_coarse_quantizers = 3, dim = 512, depth = 6, flash_attn = True ) trainer = CoarseTransformerTrainer( transformer = coarse_transformer, codec = soundstream, wav2vec = wav2vec, folder = '/path/to/audio/files', batch_size = 1, data_max_length = 320 * 32, num_train_steps = 1_000_000 ) trainer.train() ``` -------------------------------- ### Import necessary libraries for audiolm-pytorch Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Imports all required modules from the audiolm-pytorch library, along with standard Python libraries for file operations, data handling, and deep learning. ```python import math import wave import struct import os import urllib.request import tarfile from audiolm_pytorch import SoundStream, SoundStreamTrainer, HubertWithKmeans, SemanticTransformer, SemanticTransformerTrainer, HubertWithKmeans, CoarseTransformer, CoarseTransformerWrapper, CoarseTransformerTrainer, FineTransformer, FineTransformerWrapper, FineTransformerTrainer, AudioLM from torch import nn import torch import torchaudio ``` -------------------------------- ### Check GPU Availability with NVIDIA-SMI Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb This command displays information about the NVIDIA GPU(s) available on the system. Ensure a GPU is detected and functioning correctly before proceeding. ```bash Mon Jan 30 20:47:47 2023 +-----------------------------------------------------------------------------+ | NVIDIA-SMI 510.47.03 Driver Version: 510.47.03 CUDA Version: 11.6 | |-------------------------------+----------------------+----------------------| | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |===============================+======================+======================| | 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 | | N/A 73C P0 32W / 70W | 10692MiB / 15360MiB | 0% Default | | | | N/A | +-------------------------------+----------------------+----------------------+ +-----------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=============================================================================| | 0 N/A N/A 5896 C 10689MiB | +-----------------------------------------------------------------------------+ ``` -------------------------------- ### Import Specialized SoundStream Variants Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Import and use SoundStream variants specifically designed for AudioLM and MusicLM. ```python from audiolm_pytorch import AudioLMSoundStream, MusicLMSoundStream soundstream = AudioLMSoundStream(...) # say you want the hyperparameters as in Audio LM paper # rest is the same as above ``` -------------------------------- ### Generate Placeholder Sine Wave Audio Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Creates a sine wave audio signal. Useful for generating synthetic data for testing or demonstration purposes. Requires `math` module. ```python import math def get_sinewave(freq=440.0, duration_ms=200, volume=1.0, sample_rate=44100.0): # code adapted from https://stackoverflow.com/a/33913403 audio = [] num_samples = duration_ms * (sample_rate / 1000.0) for x in range(int(num_samples)): audio.append(volume * math.sin(2 * math.pi * freq * (x / sample_rate))) return audio ``` -------------------------------- ### Initialize and Train Semantic Transformer Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Initializes the SemanticTransformer for predicting semantic tokens autoregressively, optionally conditioned on text embeddings. Requires HubertWithKmeans for tokenization and a trainer for the training process. ```python from audiolm_pytorch import HubertWithKmeans, SemanticTransformer, SemanticTransformerTrainer wav2vec = HubertWithKmeans( checkpoint_path = './hubert/hubert_base_ls960.pt', kmeans_path = './hubert/hubert_base_ls960_L9_km500.bin' ) semantic_transformer = SemanticTransformer( num_semantic_tokens = wav2vec.codebook_size, dim = 1024, depth = 6, flash_attn = True, has_condition = True, # enable text conditioning cond_as_self_attn_prefix = True # VALL-E style conditioning ).cuda() trainer = SemanticTransformerTrainer( transformer = semantic_transformer, wav2vec = wav2vec, folder = '/path/to/audio/files', batch_size = 1, data_max_length = 320 * 32, num_train_steps = 1_000_000 ) trainer.train() ``` ```python # Generate semantic tokens from text sample = trainer.generate( text = ['sound of rain drops on the rooftops'], batch_size = 1, max_length = 2048 ) ``` -------------------------------- ### Extract Semantic Tokens with HubertWithKmeans Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Extracts high-level semantic tokens from audio using a pre-trained HuBERT model and k-means clustering. ```python from audiolm_pytorch import HubertWithKmeans import torch # Load HuBERT with k-means clustering # Checkpoints available at: https://github.com/facebookresearch/fairseq/tree/main/examples/hubert wav2vec = HubertWithKmeans( checkpoint_path = './hubert/hubert_base_ls960.pt', kmeans_path = './hubert/hubert_base_ls960_L9_km500.bin', target_sample_hz = 16000, output_layer = 9 # which transformer layer to extract features from ) # Extract semantic tokens from audio audio = torch.randn(2, 16000) # batch of 2, 1 second each semantic_tokens = wav2vec(audio, flatten = False) # [batch, seq_len] ``` -------------------------------- ### Use EncodecWrapper for Audio Encoding Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Provides a drop-in replacement for SoundStream using Meta AI's pre-trained Encodec model. ```python from audiolm_pytorch import EncodecWrapper import torch # Use pretrained 24kHz Encodec (no training required) encodec = EncodecWrapper( target_sample_hz = 24000, bandwidth = 6.0 # 6kbps uses 8 codebooks ) # Encode audio to get embeddings and codebook indices audio = torch.randn(1, 24000) # 1 second of audio at 24kHz emb, codes, _ = encodec(audio, return_encoded = True) # emb: embeddings, codes: [batch, timesteps, num_quantizers] # Decode back to audio from codebook indices reconstructed = encodec.decode_from_codebook_indices(codes) # reconstructed: [batch, 1, num_samples] ``` -------------------------------- ### Access Codebook Properties Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Prints the codebook size and downsample factor of the wav2vec model. Useful for understanding model configuration. ```python print(f"Codebook size: {wav2vec.codebook_size}") # e.g., 500 print(f"Downsample factor: {wav2vec.downsample_factor}") # 320 ``` -------------------------------- ### Save Audio Data to WAV File Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Saves a list of audio samples to a WAV file. This function requires `wave` and `struct` modules. Ensure the audio data is within the range of a 16-bit signed integer (-32767 to 32767). ```python import wave import struct def save_wav(file_name, audio, sample_rate=44100.0): # Open up a wav file wav_file=wave.open(file_name,"w") # wav params nchannels = 1 sampwidth = 2 # 44100 is the industry standard sample rate - CD quality. If you need to # save on file size you can adjust it downwards. The stanard for low quality # is 8000 or 8kHz. nframes = len(audio) comptype = "NONE" compname = "not compressed" wav_file.setparams((nchannels, sampwidth, sample_rate, nframes, comptype, compname)) # WAV files here are using short, 16 bit, signed integers for the # sample size. So we multiply the floating point data we have by 32767, the # maximum value for a short integer. NOTE: It is theortically possible to # use the floating point -1.0 to 1.0 data directly in a WAV file but not # obvious how to do that using the wave module in python. for sample in audio: wav_file.writeframes(struct.pack('h', int( sample * 32767.0 ))) wav_file.close() return ``` -------------------------------- ### Tokenize Audio with SoundStream Source: https://context7.com/lucidrains/audiolm-pytorch/llms.txt Converts raw audio waveforms into discrete codebook indices using a pre-trained SoundStream model. ```python from audiolm_pytorch import SoundStream import torch # Load trained SoundStream from checkpoint soundstream = SoundStream.init_and_load_from('./path/to/checkpoint.pt') soundstream.eval() # Tokenize audio into codebook indices audio = torch.randn(1, 512 * 320) # batch of 1, ~10 seconds at 16kHz codes = soundstream.tokenize(audio) # returns codebook indices # Decode back from codebook indices to audio recon_audio = soundstream.decode_from_codebook_indices(codes) # Verify reconstruction matches forward pass assert torch.allclose( recon_audio, soundstream(audio, return_recons_only = True) ) ``` -------------------------------- ### Download and Extract Dataset Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Downloads a specified dataset archive (e.g., from OpenSLR) and extracts its contents. This code requires `urllib.request` and `tarfile` modules. It checks if the files/directories already exist to avoid re-downloading or re-extracting. ```python # Get actual dataset. Uncomment this if you want to try training on real data # full dataset: https://www.openslr.org/12 # We'll use https://us.openslr.org/resources/12/dev-clean.tar.gz development set, "clean" speech. # We *should* train on, well, training, but this is just to demo running things end-to-end at all so I just picked a small clean set. # url = "https://us.openslr.org/resources/12/dev-clean.tar.gz" # filename = "dev-clean" # filename_targz = filename + ".tar.gz" # if not os.path.isfile(filename_targz): # urllib.request.urlretrieve(url, filename_targz) # if not os.path.isdir(filename): # # open file # with tarfile.open(filename_targz) as t: # t.extractall(filename) ``` -------------------------------- ### Save Generated Audio Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/audiolm_pytorch_demo.ipynb Saves the generated tensor to a WAV file using torchaudio. ```python output_path = "out.wav" sample_rate = 44100 torchaudio.save(output_path, generated_wav.cpu(), sample_rate) ``` -------------------------------- ### Test SoundStream Autoencoding Source: https://github.com/lucidrains/audiolm-pytorch/blob/main/README.md Evaluate the autoencoding capabilities of a trained SoundStream model. Ensure the model is in evaluation mode to disable residual dropout. ```python # after a lot of training, you can test the autoencoding as so soundstream.eval() # your soundstream must be in eval mode, to avoid having the residual dropout of the residual VQ necessary for training audio = torch.randn(10080).cuda() recons = soundstream(audio, return_recons_only = True) # (1, 10080) - 1 channel ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.