### Install Package Source: https://github.com/lucidrains/ring-attention-pytorch/blob/main/README.md Install the library using pip. ```bash $ pip install ring-attention-pytorch ``` -------------------------------- ### Install Requirements Source: https://github.com/lucidrains/ring-attention-pytorch/blob/main/README.md Install necessary dependencies for testing. ```bash $ pip install -r requirements.txt ``` -------------------------------- ### Configure Attention Variants Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Examples for applying padding masks, grouped query attention, and numerical stability via softclamping. ```python mask = torch.ones(batch, seq_len).bool() mask[0, 256:] = False out = default_attention(q, k, v, mask=mask, causal=False) ``` ```python kv_heads = 2 k_gqa = torch.randn(batch, seq_len, kv_heads, dim_head) v_gqa = torch.randn(batch, seq_len, kv_heads, dim_head) out = default_attention(q, k_gqa, v_gqa, causal=True) ``` ```python out = default_attention( q, k, v, causal=True, softclamp_qk_sim=True, softclamp_value=50.0 ) ``` -------------------------------- ### Implement Distributed Ring Attention Training Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Full setup for distributed training using PyTorch DDP and the RingTransformer module. ```python import os import torch import torch.multiprocessing as mp import torch.distributed as dist from torch.nn.parallel import DistributedDataParallel as DDP from ring_attention_pytorch import RingTransformer def setup(rank, world_size, use_cuda): os.environ['MASTER_ADDR'] = 'localhost' os.environ['MASTER_PORT'] = '12355' backend = "nccl" if use_cuda else "gloo" dist.init_process_group(backend, rank=rank, world_size=world_size) if use_cuda: torch.cuda.set_device(rank) def cleanup(): dist.destroy_process_group() def train_step(rank, world_size, use_cuda=True): setup(rank, world_size, use_cuda) # Create model with ring attention model = RingTransformer( num_tokens=256, dim=128, depth=2, heads=8, causal=True, ring_attn=True, striped_ring_attn=True, ring_seq_size=512, bucket_size=256 ) if use_cuda: model = model.cuda(rank) # Wrap with DDP model = DDP(model) # Training loop optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) # Long sequence training seq = torch.randint(0, 256, (2, 4096)) if use_cuda: seq = seq.cuda(rank) # Forward and backward with ring attention loss = model(seq, return_loss=True) loss.backward() optimizer.step() if rank == 0: print(f"Loss: {loss.item():.4f}") cleanup() # Launch distributed training if __name__ == '__main__': world_size = torch.cuda.device_count() # Use all available GPUs mp.spawn(train_step, args=(world_size, True), nprocs=world_size, join=True) ``` -------------------------------- ### Execute low-level ring_flash_attn Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Implements the core ring reduce algorithm using pre-computed query, key, and value tensors. ```python import torch from ring_attention_pytorch import ring_flash_attn # Prepare Q, K, V tensors # Shape: (batch, seq_len, heads, dim_head) batch, seq_len, heads, dim_head = 2, 1024, 8, 64 q = torch.randn(batch, seq_len, heads, dim_head) k = torch.randn(batch, seq_len, heads, dim_head) v = torch.randn(batch, seq_len, heads, dim_head) ``` -------------------------------- ### Run Tests Source: https://github.com/lucidrains/ring-attention-pytorch/blob/main/README.md Execute test scripts for specific attention implementations. ```bash $ python assert.py --use-cuda --causal --striped-ring-attn ``` ```bash $ python assert_tree_attn.py --use-cuda --seq-len 8192 ``` -------------------------------- ### Initialize RingAttention Source: https://github.com/lucidrains/ring-attention-pytorch/blob/main/README.md Configure and use the RingAttention module for sequence processing. ```python import torch from ring_attention_pytorch import RingAttention attn = RingAttention( dim = 512, dim_head = 64, heads = 8, causal = True, auto_shard_seq = True, ring_attn = True, ring_seq_size = 512 ) tokens = torch.randn(1, 1024, 512) attended = attn(tokens) assert attended.shape == tokens.shape ``` -------------------------------- ### Initialize and use RingAttention module Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Configures the core attention module with sequence sharding and causal masking. Supports both standard and grouped query attention. ```python import torch from ring_attention_pytorch import RingAttention # Create ring attention module with automatic sequence sharding attn = RingAttention( dim=512, # Model dimension dim_head=64, # Dimension per attention head heads=8, # Number of attention heads num_grouped_query_heads=2, # For grouped query attention (kv_heads = heads // this) causal=True, # Enable causal masking for autoregressive models auto_shard_seq=True, # Automatically shard sequence across machines ring_attn=True, # Enable ring attention ring_seq_size=512, # Sequence size per machine bucket_size=256, # Flash attention bucket size striped_ring_attn=False, # Enable striped attention for better load balancing max_lookback_seq_len=None, # Optional: limit attention lookback rotary_embed=True, # Enable rotary positional embeddings use_cuda_kernel=True # Use optimized CUDA kernels when available ) # Forward pass with token embeddings tokens = torch.randn(1, 1024, 512) # (batch, seq_len, dim) attended = attn(tokens) # With optional mask mask = torch.ones(1, 1024).bool() attended = attn(tokens, mask=mask) assert attended.shape == tokens.shape ``` -------------------------------- ### Initialize and train RingTransformer model Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Sets up a full transformer model for long-context training and inference. Handles token embeddings, layers, and output projection. ```python import torch from ring_attention_pytorch import RingTransformer # Initialize ring attention transformer model = RingTransformer( num_tokens=50000, # Vocabulary size dim=512, # Model dimension depth=6, # Number of transformer layers causal=True, # Autoregressive model dim_head=64, # Dimension per head heads=8, # Number of attention heads ff_mult=4, # Feed-forward multiplier num_grouped_query_heads=2, # Grouped query attention groups bucket_size=512, # Flash attention bucket size ring_attn=True, # Enable ring attention striped_ring_attn=True, # Enable striped attention for autoregressive ring_seq_size=512, # Sequence size per machine auto_shard_seq=True, # Auto-shard across machines max_lookback_seq_len=2048, # Optional: limit lookback to save compute rotary_embed_theta=10000, # Rotary embedding base frequency use_cuda_kernel=True # Use CUDA kernels ) # Training: compute loss directly token_ids = torch.randint(0, 50000, (2, 8192)) # (batch, seq_len) loss = model(token_ids, return_loss=True) loss.backward() # Inference: get logits with torch.no_grad(): logits = model(token_ids) # (batch, seq_len, num_tokens) # With explicit labels labels = token_ids[:, 1:] input_ids = token_ids[:, :-1] loss = model(input_ids, labels=labels, return_loss=True) ``` -------------------------------- ### ring_flash_attn_cuda Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt CUDA-optimized ring flash attention using Triton kernels. ```APIDOC ## ring_flash_attn_cuda ### Description CUDA-optimized ring flash attention using Triton kernels for forward pass and Tri Dao's flash attention for backward pass. ### Parameters - **q, k, v** (Tensor) - Input tensors on CUDA device - **mask** (Tensor, optional) - Padding mask - **causal** (bool) - Causal masking - **bucket_size** (int) - Flash attention bucket size - **ring_reduce_col** (bool) - Enable ring reduce - **striped_ring_attn** (bool) - Striped attention pattern - **max_lookback_seq_len** (int, optional) - Limit context window - **ring_size** (int, optional) - Ring size ``` -------------------------------- ### Use Tree Attention Decoding Source: https://github.com/lucidrains/ring-attention-pytorch/blob/main/README.md Import and execute tree attention decoding across distributed machines. ```python from ring_attention_pytorch import tree_attn_decode out = tree_attn_decode(q, k, v) # where q, k, v exists across all machines ``` -------------------------------- ### default_attention Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Standard attention implementation for reference or fallback. ```APIDOC ## default_attention ### Description Standard attention implementation with support for grouped query attention and optional softclamping. ### Parameters - **q, k, v** (Tensor) - Input tensors - **causal** (bool) - Causal masking ``` -------------------------------- ### Default Attention Implementation Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Standard multi-head attention with optional causal masking. Serves as a reference or fallback when specialized attention is not required. ```python import torch from ring_attention_pytorch import default_attention # Standard multi-head attention batch, seq_len, heads, dim_head = 2, 512, 8, 64 q = torch.randn(batch, seq_len, heads, dim_head) k = torch.randn(batch, seq_len, heads, dim_head) v = torch.randn(batch, seq_len, heads, dim_head) # Basic attention out = default_attention(q, k, v) # Causal attention for autoregressive models out = default_attention(q, k, v, causal=True) ``` -------------------------------- ### ring_flash_attn Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Standard ring flash attention implementation for distributed sequence processing. ```APIDOC ## ring_flash_attn ### Description Performs ring flash attention across distributed machines. ### Parameters - **q** (Tensor) - Query tensor - **k** (Tensor) - Key tensor - **v** (Tensor) - Value tensor - **mask** (Tensor, optional) - Padding mask - **causal** (bool) - Causal masking - **bucket_size** (int) - Flash attention bucket size - **ring_reduce_col** (bool) - Enable ring reduce across machines - **striped_ring_attn** (bool) - Striped attention pattern - **max_lookback_seq_len** (int, optional) - Limit attention span - **ring_size** (int, optional) - Ring size (defaults to world_size) ``` -------------------------------- ### Standard Ring Flash Attention Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Use for standard attention with optional causal masking and distributed ring reduction. Configure bucket size and ring reduction parameters as needed. ```python out = ring_flash_attn( q, k, v, mask=None, # Optional padding mask causal=True, # Causal masking bucket_size=512, # Flash attention bucket size ring_reduce_col=True, # Enable ring reduce across machines striped_ring_attn=False, # Striped attention pattern max_lookback_seq_len=None, # Limit attention span ring_size=None # Ring size (defaults to world_size) ) assert out.shape == q.shape # With key padding mask for variable length sequences mask = torch.ones(batch, seq_len).bool() mask[0, 512:] = False # Mask out padding out = ring_flash_attn(q, k, v, mask=mask, causal=False) ``` -------------------------------- ### CUDA Optimized Ring Flash Attention Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Leverages CUDA and Triton kernels for significant speedups on GPU clusters. Requires CUDA tensors and supports mixed precision. ```python import torch from ring_attention_pytorch import ring_flash_attn_cuda # Requires CUDA tensors device = torch.device('cuda') batch, seq_len, heads, dim_head = 2, 2048, 8, 64 q = torch.randn(batch, seq_len, heads, dim_head, device=device) k = torch.randn(batch, seq_len, heads, dim_head, device=device) v = torch.randn(batch, seq_len, heads, dim_head, device=device) # CUDA ring flash attention with striped pattern out = ring_flash_attn_cuda( q, k, v, mask=None, causal=True, bucket_size=1024, ring_reduce_col=True, striped_ring_attn=True, # Better load balancing for causal max_lookback_seq_len=4096, # Limit context window ring_size=None ) # Supports mixed precision q_half = q.half() k_half = k.half() v_half = v.half() out_half = ring_flash_attn_cuda(q_half, k_half, v_half, causal=True) ``` -------------------------------- ### tree_attn_decode Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Tree Attention Decoding for efficient distributed inference. ```APIDOC ## tree_attn_decode ### Description Tree Attention Decoding implementation for efficient distributed inference during autoregressive generation. ### Parameters - **q, k, v** (Tensor) - Query and KV cache tensors - **eps** (float) - Numerical stability - **shard_kv_seq** (bool) - Auto-shard KV across world - **use_triton** (bool, optional) - Auto-detect CUDA for Triton ``` -------------------------------- ### Tree Attention Decoding Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Efficient distributed inference for autoregressive generation using a tree-reduction pattern. Supports automatic KV cache sharding or pre-sharded KV caches. ```python import torch from ring_attention_pytorch import tree_attn_decode # During inference on a distributed system # Each machine has a shard of the KV cache batch, heads, kv_seq_len, dim = 1, 8, 8192, 64 # Query for the current decoding step (single position) q = torch.randn(batch, heads, 1, dim) # Full KV cache (will be automatically sharded across machines) k = torch.randn(batch, heads, kv_seq_len, dim) v = torch.randn(batch, heads, kv_seq_len, dim) # Tree attention decoding with automatic KV sharding out = tree_attn_decode( q, k, v, eps=1e-8, # Numerical stability shard_kv_seq=True, # Auto-shard KV across world use_triton=None # Auto-detect CUDA for Triton ) assert out.shape == (batch, heads, 1, dim) # When KV is pre-sharded (shard_kv_seq=False) # Each machine only passes its local KV shard # Useful when KV cache is already distributed local_k = k[:, :, :1024, :] # Rank's portion of KV local_v = v[:, :, :1024, :] out = tree_attn_decode(q, local_k, local_v, shard_kv_seq=False) ``` -------------------------------- ### Ring Rotary Embedding Module Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Rotary positional embeddings adapted for ring attention, supporting striped patterns and automatic position offsets based on rank. Generates frequencies for a given sequence length or custom positions. ```python import torch from ring_attention_pytorch import RingRotaryEmbedding, apply_rotary_pos_emb # Create rotary embeddings for ring attention rotary_emb = RingRotaryEmbedding( dim=64, # Head dimension (must match dim_head) ring=True, # Enable ring-aware position calculation striped=True, # Support striped attention pattern buckets=2, # Number of buckets per machine theta=10000 # Base frequency ) # Generate rotary embeddings for sequence length seq_len = 512 freqs = rotary_emb(seq_len) # Shape: (seq_len, dim) # Apply to query and key tensors # Shape: (batch, seq_len, heads, dim_head) q = torch.randn(2, seq_len, 8, 64) k = torch.randn(2, seq_len, 8, 64) q_rotated = apply_rotary_pos_emb(freqs, q) k_rotated = apply_rotary_pos_emb(freqs, k) # Custom positions (for variable-length or special patterns) custom_positions = torch.arange(seq_len) * 2 # Every other position freqs_custom = rotary_emb(custom_positions) ``` -------------------------------- ### RingRotaryEmbedding Source: https://context7.com/lucidrains/ring-attention-pytorch/llms.txt Rotary positional embeddings adapted for ring attention. ```APIDOC ## RingRotaryEmbedding ### Description Rotary positional embeddings adapted for ring attention with support for striped attention patterns. ### Parameters - **dim** (int) - Head dimension - **ring** (bool) - Enable ring-aware position calculation - **striped** (bool) - Support striped attention pattern - **buckets** (int) - Number of buckets per machine - **theta** (int) - Base frequency ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.