### Initialize and Use Mirasol Model Source: https://github.com/lucidrains/mirasol-pytorch/blob/main/README.md Instantiate the Mirasol model with specified dimensions and parameters. This example demonstrates how to pass audio, video, and text data to the model for training and how to generate text after training. ```python import torch from mirasol_pytorch import Mirasol model = Mirasol( dim = 512, num_text_tokens = 256, video_image_size = 128, video_frames_per_timechunk = 2, audio_freq_dim = 64, audio_time_dim_per_timechunk = 32, audio_patch_size = (32, 16), video_patch_size = (64, 2), audio_encoder = dict( dim = 512, depth = 2 ), video_encoder = dict( dim = 512, depth = 2 ) ) audio = torch.randn(1, 64, 1024) video = torch.randn(1, 3, 12, 128, 128) text = torch.randint(0, 256, (1, 1024)) loss = model( audio = audio, video = video, text = text ) loss.backward() # after much training sampled_text = model.generate( audio = audio, video = video, seq_len = 512 ) ``` -------------------------------- ### Install Mirasol-Pytorch Source: https://github.com/lucidrains/mirasol-pytorch/blob/main/README.md Install the library using pip. This command is used to add the Mirasol-Pytorch package to your project. ```bash pip install mirasol-pytorch ``` -------------------------------- ### Distributed Training Setup and Model Initialization Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Configure distributed training using `torch.distributed` and initialize the Mirasol model on the appropriate device. The model can be wrapped with `DistributedDataParallel` if running in a distributed environment. Similarity regularization loss is enabled via `sim_reg_loss_weight`. ```python import torch import torch.distributed as dist from mirasol_pytorch import Mirasol from mirasol_pytorch.distributed import get_is_distributed, all_gather # Initialize distributed training (example with torchrun) # torchrun --nproc_per_node=4 train.py def setup_distributed(): dist.init_process_group(backend='nccl') local_rank = dist.get_rank() torch.cuda.set_device(local_rank) return local_rank # Check if running in distributed mode if get_is_distributed(): local_rank = setup_distributed() device = torch.device(f'cuda:{local_rank}') else: device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = Mirasol( dim=512, num_text_tokens=256, video_image_size=128, video_frames_per_timechunk=2, audio_freq_dim=64, audio_time_dim_per_timechunk=32, audio_patch_size=(32, 16), video_patch_size=(64, 2), audio_encoder=dict(dim=512, depth=2), video_encoder=dict(dim=512, depth=2), # Enable similarity regularization (uses all_gather in distributed) sim_reg_loss_weight=0.1 ).to(device) if get_is_distributed(): model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[local_rank]) # Training loop with distributed data audio = torch.randn(2, 64, 1024, device=device) video = torch.randn(2, 3, 12, 128, 128, device=device) text = torch.randint(0, 256, (2, 256), device=device) loss = model(audio=audio, video=video, text=text) loss.backward() print(f"[Rank {dist.get_rank() if get_is_distributed() else 0}] Loss: {loss.item():.4f}") ``` -------------------------------- ### Initialize and Train Mirasol Model Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Create a Mirasol model instance and perform a forward pass with audio, video, and text inputs. ```python import torch from mirasol_pytorch import Mirasol # Initialize the Mirasol model with configuration model = Mirasol( dim=512, # Model dimension num_text_tokens=256, # Size of text vocabulary video_image_size=128, # Video frame spatial size (height/width) video_frames_per_timechunk=2, # Frames per time chunk audio_freq_dim=64, # Audio frequency dimension audio_time_dim_per_timechunk=32, # Audio time steps per chunk audio_patch_size=(32, 16), # Audio patch (freq, time) video_patch_size=(64, 2), # Video patch (spatial, time) audio_encoder=dict( dim=512, depth=2 ), video_encoder=dict( dim=512, depth=2 ), encoder_depth=6, # A/V encoder transformer depth decoder_depth=6, # Text decoder depth combiner_depth=2, # Combiner transformer depth combiner_output_num_tokens=3, # Output tokens per time chunk num_audio_video_register_tokens=8, # Register tokens for attention audio_video_mask_prob=0.15, # Masking probability for A/V tokens text_max_seq_len=2048, # Maximum text sequence length text_forgetful_causal_mask_prob=0.1, # Forgetful causal masking prob flash_attn=True, # Use Flash Attention av_autoregressive_loss_weight=1., # Weight for A/V autoregressive loss av_reconstruction_loss_weight=1., # Weight for reconstruction loss sim_reg_loss_weight=0. # Similarity regularization weight ) # Prepare input data # Audio: (batch, frequency, time) audio = torch.randn(1, 64, 1024) # Video: (batch, channels, frames, height, width) video = torch.randn(1, 3, 12, 128, 128) # Text tokens: (batch, sequence_length) text = torch.randint(0, 256, (1, 1024)) # Forward pass returns combined loss loss = model( audio=audio, video=video, text=text ) # Backpropagation loss.backward() print(f"Training loss: {loss.item()}") ``` -------------------------------- ### Defining Custom Encoders Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Demonstrates how to instantiate custom encoder modules for use with the Mirasol architecture. ```python import torch from torch import nn from mirasol_pytorch import Mirasol from x_transformers import Encoder # Create custom video encoder with specific configuration custom_video_encoder = Encoder( dim=512, depth=4, heads=8, dim_head=64, ff_mult=4, flash_attn=True ) # Create custom audio encoder custom_audio_encoder = Encoder( dim=512, depth=3, heads=8, dim_head=64, ff_mult=4, flash_attn=True ) ``` -------------------------------- ### Create Custom Combiner and Model Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Instantiate a custom combiner and then the Mirasol model, passing the custom audio, video, and combiner encoders. This allows for flexible model architecture customization. ```python from mirasol_pytorch import Mirasol, Encoder custom_combiner = Encoder( dim=512, depth=4, heads=8, dim_head=64, flash_attn=True ) model = Mirasol( dim=512, num_text_tokens=256, video_image_size=128, video_frames_per_timechunk=2, audio_freq_dim=64, audio_time_dim_per_timechunk=32, audio_patch_size=(32, 16), video_patch_size=(64, 2), # Pass custom modules directly audio_encoder=custom_audio_encoder, video_encoder=custom_video_encoder, combiner=custom_combiner, combiner_output_num_tokens=4 ) audio = torch.randn(1, 64, 1024) video = torch.randn(1, 3, 12, 128, 128) text = torch.randint(0, 256, (1, 256)) loss = model(audio=audio, video=video, text=text) print(f"Loss with custom encoders: {loss.item():.4f}") ``` -------------------------------- ### Retrieve Detailed Loss Breakdown Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Enable return_loss_breakdown to monitor individual loss components during training. ```python import torch from mirasol_pytorch import Mirasol model = Mirasol( dim=512, num_text_tokens=256, video_image_size=128, video_frames_per_timechunk=2, audio_freq_dim=64, audio_time_dim_per_timechunk=32, audio_patch_size=(32, 16), video_patch_size=(64, 2), audio_encoder=dict(dim=512, depth=2), video_encoder=dict(dim=512, depth=2), av_autoregressive_loss_weight=1., av_reconstruction_loss_weight=1., sim_reg_loss_weight=0.1 # Enable similarity regularization ) audio = torch.randn(2, 64, 1024) video = torch.randn(2, 3, 12, 128, 128) text = torch.randint(0, 256, (2, 512)) # Get detailed loss breakdown total_loss, loss_breakdown = model( audio=audio, video=video, text=text, return_loss_breakdown=True ) # Access individual loss components print(f"Total Loss: {total_loss.item():.4f}") print(f"Text Autoregressive Loss: {loss_breakdown.text_autoregressive:.4f}") print(f"A/V Autoregressive Loss: {loss_breakdown.av_autoregressive:.4f}") print(f"A/V Reconstruction Loss: {loss_breakdown.av_recon:.4f}") print(f"Text-A/V Similarity Reg Loss: {loss_breakdown.text_av_sim_reg:.4f}") total_loss.backward() ``` -------------------------------- ### Video Reconstruction with Reduced Resolution Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Configures the model to use a reconstruction loss with a smaller video resolution for improved training efficiency. ```python import torch from mirasol_pytorch import Mirasol # Configure model with video reconstruction at reduced resolution model = Mirasol( dim=512, num_text_tokens=256, video_image_size=128, video_frames_per_timechunk=2, audio_freq_dim=64, audio_time_dim_per_timechunk=32, audio_patch_size=(32, 16), video_patch_size=(64, 2), # Smaller patch size for reconstruction (must be <= video_patch_size) video_recon_patch_size=(32, 1), video_recon_interpolate_mode='bilinear', # Interpolation mode audio_encoder=dict(dim=512, depth=2), video_encoder=dict(dim=512, depth=2), av_reconstruction_loss_weight=1.0 ) audio = torch.randn(1, 64, 1024) video = torch.randn(1, 3, 12, 128, 128) text = torch.randint(0, 256, (1, 256)) # Training with reconstruction loss at reduced resolution total_loss, losses = model( audio=audio, video=video, text=text, return_loss_breakdown=True ) print(f"Reconstruction loss (reduced res): {losses.av_recon:.4f}") ``` -------------------------------- ### Inference with Pre-encoded Audio and Video Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Passes pre-computed features directly to the model to skip the encoding step during inference. ```python import torch from mirasol_pytorch import Mirasol model = Mirasol( dim=512, num_text_tokens=256, video_image_size=128, video_frames_per_timechunk=2, audio_freq_dim=64, audio_time_dim_per_timechunk=32, audio_patch_size=(32, 16), video_patch_size=(64, 2), audio_encoder=dict(dim=512, depth=2), video_encoder=dict(dim=512, depth=2) ) # Get expected shapes from model audio_tokens_per_chunk = model.audio_timechunk_tokens video_tokens_per_chunk = model.video_timechunk_tokens dim = 512 print(f"Audio tokens per chunk: {audio_tokens_per_chunk}") print(f"Video tokens per chunk: {video_tokens_per_chunk}") # Pre-encode using external encoder or cached features num_time_chunks = 32 batch_size = 2 # Pre-encoded audio: (batch, time_chunks, tokens_per_chunk, dim) encoded_audio = torch.randn(batch_size, num_time_chunks, audio_tokens_per_chunk, dim) # Pre-encoded video: (batch, time_chunks, tokens_per_chunk, dim) encoded_video = torch.randn(batch_size, num_time_chunks, video_tokens_per_chunk, dim) text = torch.randint(0, 256, (batch_size, 256)) # Forward pass with pre-encoded features (skips encoding step) loss = model( encoded_audio=encoded_audio, encoded_video=encoded_video, text=text ) print(f"Loss with pre-encoded inputs: {loss.item():.4f}") ``` -------------------------------- ### Inference Mode (No Loss Calculation) Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Set `return_loss=False` during model instantiation for inference to directly obtain decoder outputs (logits) without computing losses. Ensure the model is in evaluation mode (`model.eval()`) and use `torch.no_grad()`. ```python import torch from mirasol_pytorch import Mirasol model = Mirasol( dim=512, num_text_tokens=256, video_image_size=128, video_frames_per_timechunk=2, audio_freq_dim=64, audio_time_dim_per_timechunk=32, audio_patch_size=(32, 16), video_patch_size=(64, 2), audio_encoder=dict(dim=512, depth=2), video_encoder=dict(dim=512, depth=2) ) model.eval() audio = torch.randn(1, 64, 1024) video = torch.randn(1, 3, 12, 128, 128) text = torch.randint(0, 256, (1, 128)) with torch.no_grad(): # Get decoder logits without loss computation logits = model( audio=audio, video=video, text=text, return_loss=False ) print(f"Output logits shape: {logits.shape}") # Output: torch.Size([1, 129, 257]) - (batch, seq_len+1, vocab_size+1) # Get predicted tokens predicted_tokens = logits.argmax(dim=-1) print(f"Predicted tokens shape: {predicted_tokens.shape}") ``` -------------------------------- ### Autoregressive Text Generation with Mirasol Source: https://context7.com/lucidrains/mirasol-pytorch/llms.txt Performs text generation conditioned on audio and video inputs. The model automatically switches to evaluation mode during generation. ```python import torch from mirasol_pytorch import Mirasol model = Mirasol( dim=512, num_text_tokens=256, video_image_size=128, video_frames_per_timechunk=2, audio_freq_dim=64, audio_time_dim_per_timechunk=32, audio_patch_size=(32, 16), video_patch_size=(64, 2), audio_encoder=dict(dim=512, depth=2), video_encoder=dict(dim=512, depth=2), text_max_seq_len=2048 ) # Load trained weights (after training) # model.load_state_dict(torch.load('mirasol_trained.pt')) # Prepare audio and video inputs audio = torch.randn(1, 64, 1024) video = torch.randn(1, 3, 12, 128, 128) # Generate text from audio/video context # Model automatically switches to eval mode generated_tokens = model.generate( audio=audio, video=video, seq_len=512 # Maximum tokens to generate ) print(f"Generated token shape: {generated_tokens.shape}") # Output: Generated token shape: torch.Size([1, 512]) # Generate with a text prompt for continuation prompt = torch.randint(0, 256, (1, 32)) # Starting prompt tokens continued_tokens = model.generate( audio=audio, video=video, prompt=prompt, seq_len=256 ) print(f"Continued token shape: {continued_tokens.shape}") # Output: Continued token shape: torch.Size([1, 289]) (prompt + generated) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.