### Install mixture-of-attention Source: https://github.com/lucidrains/mixture-of-attention/blob/main/README.md Install the package using pip. This is the primary method for obtaining the library. ```bash pip install mixture-of-attention ``` -------------------------------- ### Initialize Transformer and AutoregressiveWrapper for Training Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Set up a Transformer model and wrap it with AutoregressiveWrapper for training on datasets like enwik8. Configure model dimensions, vocabulary size, and training parameters. ```python import gzip import torch import numpy as np from torch.optim import Adam from torch.utils.data import DataLoader, Dataset from mixture_of_attention.transformer import Transformer from mixture_of_attention.autoregressive_wrapper import AutoregressiveWrapper # Configuration SEQ_LEN = 512 BATCH_SIZE = 4 LEARNING_RATE = 1e-4 GRADIENT_ACCUMULATE_EVERY = 4 # Initialize model model = Transformer( num_tokens=256, dim=512, depth=8, num_experts=2, seq_len=SEQ_LEN, local_attn_window_size=64, num_routed_queries=32, num_routed_key_values=64, cosine_sim_routing=True, use_triton=True ) model = AutoregressiveWrapper(model).cuda() ``` -------------------------------- ### Initialize and Use Attend Module Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Initialize the Attend module for efficient attention computation. It supports Flash Attention, causal masking, and key padding masks. Automatic GPU optimization selects the best attention implementation. ```python import torch from mixture_of_attention.attend import Attend # Initialize attend module attend = Attend( dropout=0.1, # Attention dropout causal=False, # Causal masking flash=True # Use Flash Attention (requires PyTorch 2.0+) ) # Query, Key, Value tensors: (batch, heads, seq_len, dim_head) q = torch.randn(2, 8, 128, 64) k = torch.randn(2, 8, 128, 64) v = torch.randn(2, 8, 128, 64) # Attention computation output = attend(q, k, v) # Returns: (2, 8, 128, 64) # With key padding mask: (batch, key_seq_len) mask = torch.ones((2, 128)).bool() mask[:, 100:] = False # Mask out positions 100+ output = attend(q, k, v, mask=mask) # Causal attention for autoregressive models causal_attend = Attend( dropout=0.0, causal=True, # Apply causal mask flash=True ) # Automatic GPU detection for optimal Flash Attention config # - A100 GPUs: Uses flash attention # - Other GPUs: Uses math or memory-efficient attention output = causal_attend(q, k, v) ``` -------------------------------- ### Initialize MixtureOfAttention Source: https://github.com/lucidrains/mixture-of-attention/blob/main/README.md Instantiate the MixtureOfAttention module. Ensure dimensions and expert configurations match your model requirements. This is for non-autoregressive use cases. ```python import torch from mixture_of_attention import MixtureOfAttention mixture_of_attn = MixtureOfAttention( dim = 512, dim_context = 256, num_routed_queries = 16, num_routed_key_values = 16, num_experts = 2, dim_head = 64, heads = 8 ) x = torch.randn(1, 1024, 512) mask = torch.ones((1, 1024)).bool() context = torch.randn(1, 512, 256) context_mask = torch.ones((1, 512)).bool() mixture_of_attn(x, context = context, mask = mask) # (1, 1024, 512) ``` -------------------------------- ### Execute training loop Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Performs model training with gradient accumulation and clipping. ```python optimizer = Adam(model.parameters(), lr=LEARNING_RATE) for batch in train_loader: model.train() # Gradient accumulation for _ in range(GRADIENT_ACCUMULATE_EVERY): loss = model(batch) loss.backward(loss / GRADIENT_ACCUMULATE_EVERY) # Gradient clipping and update torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5) optimizer.step() optimizer.zero_grad() print(f"Training loss: {loss.item():.4f}") ``` -------------------------------- ### Coordinate Descent Algorithms Source: https://github.com/lucidrains/mixture-of-attention/blob/main/README.md BibTeX entry for the paper on coordinate descent algorithms, relevant for optimization techniques. ```bibtex @article{Wright2015CoordinateDA, title = {Coordinate descent algorithms}, author = {Stephen J. Wright}, journal = {Mathematical Programming}, year = {2015}, volume = {151}, pages = {3-34} } ``` -------------------------------- ### Initialize MixtureOfAutoregressiveAttention Source: https://github.com/lucidrains/mixture-of-attention/blob/main/README.md Instantiate the MixtureOfAutoregressiveAttention module. Configure local and routed attention window sizes according to your needs. This variant is designed for autoregressive tasks. ```python import torch from mixture_of_attention import MixtureOfAutoregressiveAttention mixture_of_attn = MixtureOfAutoregressiveAttention( dim = 512, local_attn_window_size = 64, # local attention window size routed_window_size = None, # will be set to the same as local_attn_window_size if None. ideally less than or equal to local attention window size for full receptive field num_routed_queries = 12, num_routed_key_values = 12, num_experts = 2, dim_head = 64, heads = 8 ) x = torch.randn(1, 1023, 512) out = mixture_of_attn(x) # (1, 1023, 512) ``` -------------------------------- ### Initialize Mixture of Autoregressive Attention Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Initialize the MixtureOfAutoregressiveAttention module with specified dimensions, attention window sizes, number of experts, and other configuration parameters. Use Flash Attention for performance if available. ```python import torch from mixture_of_attention import MixtureOfAutoregressiveAttention mixture_of_attn = MixtureOfAutoregressiveAttention( dim=512, # Input dimension local_attn_window_size=64, # Local attention window size (required) routed_window_size=None, # Routed window size (defaults to local_attn_window_size) num_routed_queries=12, # Number of queries to route per expert num_routed_key_values=12, # Number of key-values to route per expert num_experts=2, # Number of attention experts dim_head=64, # Dimension per attention head heads=8, # Number of attention heads dropout=0.0, # Attention dropout flash_attn=True, # Use Flash Attention prenorm=True, # Apply RMS normalization use_triton=False, # Use Triton kernels (disable if not available) average_routed=False # Whether to average expert outputs ) ``` -------------------------------- ### Initialize MixtureOfAutoregressiveAttention Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Import the MixtureOfAutoregressiveAttention class for causal attention implementations. ```python import torch from mixture_of_attention import MixtureOfAutoregressiveAttention ``` -------------------------------- ### FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness Source: https://github.com/lucidrains/mixture-of-attention/blob/main/README.md BibTeX entry for the FlashAttention paper, detailing a fast and memory-efficient exact attention mechanism with IO-awareness. ```bibtex @inproceedings{dao2022flashattention, title = {Flash{A}ttention: Fast and Memory-Efficient Exact Attention with {IO}-Awareness}, author = {Dao, Tri and Fu, Daniel Y. and Ermon, Stefano and Rudra, Atri and R{\'e}, Christopher}, booktitle = {Advances in Neural Information Processing Systems}, year = {2022} } ``` -------------------------------- ### Einops: Clear and Reliable Tensor Manipulations with Einstein-like Notation Source: https://github.com/lucidrains/mixture-of-attention/blob/main/README.md BibTeX entry for the einops library, which simplifies tensor manipulations using Einstein-like notation. ```bibtex @inproceedings{rogozhnikov2022einops, title = {Einops: Clear and Reliable Tensor Manipulations with Einstein-like Notation}, author = {Alex Rogozhnikov}, booktitle = {International Conference on Learning Representations}, year = {2022}, url = {https://openreview.net/forum?id=oapKSVM2bcj} } ``` -------------------------------- ### Initialize and Use Transformer Model Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Initialize a Transformer model with specified parameters and perform a forward pass. The model can be wrapped with AutoregressiveWrapper for training and generation. ```python import torch # Initialize transformer model model = Transformer( num_tokens=256, # Vocabulary size (e.g., 256 for byte-level) dim=512, # Model dimension depth=8, # Number of transformer layers seq_len=512, # Maximum sequence length local_attn_window_size=64, # Local attention window num_routed_queries=32, # Queries to route per expert num_routed_key_values=64, # Key-values to route per expert num_experts=2, # Number of attention experts dim_head=64, # Dimension per attention head heads=8, # Number of attention heads ff_mult=4, # Feed-forward expansion multiplier cosine_sim_routing=True, # Use cosine similarity for routing routed_window_size=None, # Override routed window size use_triton=True, # Use Triton kernels routed_rotary_emb=True # Use rotary embeddings for routed attention ) # Input: token indices (batch, sequence_length) x = torch.randint(0, 256, (2, 512)) # Forward pass returns logits logits = model(x) # Returns: (2, 512, 256) # Wrap for autoregressive training and generation model = AutoregressiveWrapper( model, pad_value=0 # Padding token value ) # Training: input includes target (shifted by 1) x = torch.randint(0, 256, (2, 513)) # seq_len + 1 loss = model(x) # Returns cross-entropy loss # Generation prompt = torch.randint(0, 256, (1, 128)) # Prompt tokens generated = model.generate( prompt, seq_len=256, # Number of tokens to generate temperature=1.0, # Sampling temperature filter_thres=0.9 # Top-k filtering threshold ) # Returns: (1, 256) generated tokens ``` -------------------------------- ### Initialize and Use MixtureOfAttention Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Configure and execute cross-attention and self-attention using the MixtureOfAttention class. ```python import torch from mixture_of_attention import MixtureOfAttention # Initialize mixture of attention with 2 experts mixture_of_attn = MixtureOfAttention( dim=512, # Input dimension dim_context=256, # Context dimension (for cross-attention) num_routed_queries=16, # Number of queries to route per expert num_routed_key_values=16, # Number of key-values to route per expert num_experts=2, # Number of attention experts dim_head=64, # Dimension per attention head heads=8, # Number of attention heads local_attn=False, # Whether to use local attention in parallel local_attn_window_size=None, # Window size for local attention flash_attn=True, # Use Flash Attention (requires PyTorch 2.0+) prenorm=True, # Apply RMS normalization before attention use_triton=True, # Use Triton kernels for routing average_routed=False # Whether to average routed outputs ) # Input tensor: (batch, sequence_length, dim) x = torch.randn(1, 1024, 512) mask = torch.ones((1, 1024)).bool() # Cross-attention context: (batch, context_length, dim_context) context = torch.randn(1, 512, 256) context_mask = torch.ones((1, 512)).bool() # Forward pass - cross-attention output = mixture_of_attn( x, context=context, mask=mask, context_mask=context_mask ) # Returns: (1, 1024, 512) # Self-attention (no context provided) self_attn = MixtureOfAttention( dim=512, num_routed_queries=16, num_routed_key_values=16, num_experts=2, dim_head=64, heads=8, local_attn=True, # Enable local attention for self-attention local_attn_window_size=64 # Local attention window ) x = torch.randn(1, 1024, 512) output = self_attn(x, mask=mask) # Returns: (1, 1024, 512) ``` -------------------------------- ### Load enwik8 data Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Reads and prepares the enwik8 dataset for training and validation. ```python with gzip.open("./data/enwik8.gz") as file: data = np.frombuffer(file.read(int(95e6)), dtype=np.uint8).copy() np_train, np_valid = np.split(data, [int(90e6)]) data_train = torch.from_numpy(np_train) data_val = torch.from_numpy(np_valid) train_dataset = TextSamplerDataset(data_train, SEQ_LEN) train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True) ``` -------------------------------- ### Initialize and Apply Rotary Positional Embeddings Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Initialize RotaryEmbedding and apply it to query and key tensors. Ensure the dimension matches `dim_head`. ```python import torch from mixture_of_attention.rotary_emb import RotaryEmbedding, apply_rotary_pos_emb # Initialize rotary embedding rotary_emb = RotaryEmbedding( dim=64, # Dimension (should match dim_head) theta=10000 # Base frequency ) # Generate position embeddings for sequence seq_len = 1024 freqs = rotary_emb(seq_len) # Returns: (seq_len, dim) # Apply to query/key tensors # q, k shape: (batch, heads, seq_len, dim_head) q = torch.randn(2, 8, 1024, 64) k = torch.randn(2, 8, 1024, 64) # Apply rotary embeddings q_rotated = apply_rotary_pos_emb(freqs, q) k_rotated = apply_rotary_pos_emb(freqs, k) ``` -------------------------------- ### Stabilized Sparse Scaling Algorithms for Entropy Regularized Transport Problems Source: https://github.com/lucidrains/mixture-of-attention/blob/main/README.md BibTeX entry for a paper on stabilized sparse scaling algorithms for entropy-regularized transport problems. ```bibtex @article{Schmitzer2016StabilizedSS, title = {Stabilized Sparse Scaling Algorithms for Entropy Regularized Transport Problems}, author = {Bernhard Schmitzer}, journal = {ArXiv}, year = {2016}, volume = {abs/1610.06519} } ``` -------------------------------- ### Multi-Expert Attention with Scaling Factors Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Apply scaling factors to queries, keys, values, and output in multi-expert attention. Ensure scaling tensor dimensions are compatible. ```python # With scaling factors for routed attention output = multi_expert_attn( x, mask=mask, queries_scale=torch.ones(2, 4, 128, 1), # Scale queries keys_scale=torch.ones(2, 4, 1, 128, 1), # Scale keys values_scale=torch.ones(2, 4, 1, 128, 1), # Scale values output_scale=torch.ones(2, 4, 128, 1) # Scale output ) ``` -------------------------------- ### CoLT5: Faster Long-Range Transformers with Conditional Computation Source: https://github.com/lucidrains/mixture-of-attention/blob/main/README.md BibTeX entry for the CoLT5 paper, which explores conditional computation for efficient long-range transformers. ```bibtex @inproceedings{Ainslie2023CoLT5FL, title = {CoLT5: Faster Long-Range Transformers with Conditional Computation}, author = {Joshua Ainslie and Tao Lei and Michiel de Jong and Santiago Ontan'on and Siddhartha Brahma and Yury Zemlyanskiy and David Uthus and Mandy Guo and James Lee-Thorp and Yi Tay and Yun-Hsuan Sung and Sumit Sanghai}, year = {2023} } ``` -------------------------------- ### Multi-Expert Grouped Attention Initialization Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Initialize Attention with multiple expert groups for parallel processing. Set causal=True for autoregressive tasks and prenorm=True for pre-layer normalization. ```python # Multi-expert grouped attention multi_expert_attn = Attention( dim=512, dim_head=64, heads=8, groups=4, # 4 expert groups processed in parallel causal=True, # Causal for autoregressive flash=True, prenorm=True ) ``` -------------------------------- ### Generate text sequences Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Uses the trained model to generate text from a random prompt. ```python model.eval() prompt = torch.randint(0, 256, (1, 128)).cuda() generated = model.generate(prompt, seq_len=512, temperature=0.8) print("Generated:", "".join(chr(max(32, t)) for t in generated[0])) ``` -------------------------------- ### Transformer Model Initialization Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Initialize the Transformer class, which utilizes mixture of autoregressive attention layers. Suitable for language modeling and sequence generation. ```python import torch from mixture_of_attention.transformer import Transformer from mixture_of_attention.autoregressive_wrapper import AutoregressiveWrapper ``` -------------------------------- ### Initialize Base Attention Module Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Initialize the base Attention module for multi-head attention. Supports grouped experts, rotary embeddings, and Flash Attention. Set causal=True for autoregressive tasks. ```python import torch from mixture_of_attention import Attention # Basic single-expert attention attn = Attention( dim=512, # Input dimension dim_head=64, # Dimension per head dim_context=None, # Context dimension (defaults to dim) heads=8, # Number of attention heads causal=False, # Causal masking for autoregressive groups=1, # Number of expert groups dropout=0.0, # Attention dropout flash=True, # Use Flash Attention prenorm=False # Apply RMS normalization ) ``` -------------------------------- ### Define TextSamplerDataset class Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt A PyTorch Dataset implementation for sampling sequences from a large tensor. ```python class TextSamplerDataset(Dataset): def __init__(self, data, seq_len): self.data = data self.seq_len = seq_len def __getitem__(self, index): rand_start = torch.randint(0, self.data.size(0) - self.seq_len, (1,)) full_seq = self.data[rand_start:rand_start + self.seq_len + 1].long() return full_seq.cuda() def __len__(self): return self.data.size(0) // self.seq_len ``` -------------------------------- ### Self-Attention Forward Pass Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Perform a self-attention forward pass using the Attention module. Input shape is (batch, sequence, dim). A boolean mask can be provided. ```python # Input: (batch, sequence, dim) x = torch.randn(2, 128, 512) mask = torch.ones((2, 128)).bool() # Self-attention output = attn(x, mask=mask) # Returns: (2, 128, 512) ``` -------------------------------- ### Forward Pass with Mixture of Autoregressive Attention Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Perform a forward pass with the MixtureOfAutoregressiveAttention module. Input tensor shape is (batch, sequence_length, dim). Optionally override routing parameters. ```python # Input tensor: (batch, sequence_length, dim) x = torch.randn(1, 1023, 512) # Forward pass - autoregressive attention output = mixture_of_attn(x) # Returns: (1, 1023, 512) ``` ```python # With dynamic routing parameters output = mixture_of_attn( x, num_routed_queries=8, # Override default routed queries num_routed_key_values=8 # Override default routed key-values ) ``` -------------------------------- ### Cross-Attention Forward Pass Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Perform a cross-attention forward pass by providing a separate context tensor. The context tensor shape is (batch, context_sequence, dim). ```python # Cross-attention with context context = torch.randn(2, 64, 512) output = attn(x, context=context, mask=mask) ``` -------------------------------- ### Multi-Expert Grouped Attention Forward Pass Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Perform a forward pass with multi-expert grouped attention. Input tensor shape is (batch, groups, sequence, dim). ```python # Input with expert dimension: (batch, groups, sequence, dim) x = torch.randn(2, 4, 128, 512) mask = torch.ones((2, 4, 128)).bool() output = multi_expert_attn(x, mask=mask) # Returns: (2, 4, 128, 512) ``` -------------------------------- ### Mixture of Autoregressive Attention with Rotary Embeddings Source: https://context7.com/lucidrains/mixture-of-attention/llms.txt Apply rotary embeddings for positional encoding with MixtureOfAutoregressiveAttention. Ensure rotary embedding dimension matches dim_head. ```python from mixture_of_attention.rotary_emb import RotaryEmbedding rotary_emb = RotaryEmbedding(dim=64) # Same as dim_head pos_emb = rotary_emb(seq_len=1023) # Generate position embeddings output = mixture_of_attn(x, rotary_emb=pos_emb) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.