### Complete Training Loop Example Source: https://context7.com/lucidrains/pause-transformer/llms.txt Demonstrates a full training setup for Pause Transformer on character-level data. Includes model configuration, optimizer setup, training loop with loss computation and gradient updates, and evaluation with entropy-based adaptive pausing. The final inference uses the last pause step's logits. ```python import torch import torch.optim as optim from pause_transformer import PauseTransformer # Model configuration model = PauseTransformer( num_tokens=256, # Byte-level vocabulary dim=512, depth=6, max_pause_length=2, dim_head=64, heads=8, ff_mult=4 ) # Setup optimizer optimizer = optim.AdamW(model.parameters(), lr=1e-4, weight_decay=0.01) # Training loop model.train() for step in range(1000): # Generate random training data (replace with real data) x = torch.randint(0, 256, (8, 512)) # Forward pass with loss loss = model(x, return_loss=True) # Backward pass optimizer.zero_grad() loss.backward() optimizer.step() if step % 100 == 0: print(f"Step {step}, Loss: {loss.item():.4f}") # Evaluation with entropy-based adaptive pausing model.eval() with torch.no_grad(): test_x = torch.randint(0, 256, (1, 256)) # Get entropy to guide pause allocation entropy = model(test_x, arrest_pausing=True, return_logit_entropy=True) # Allocate more pause steps to high-entropy (uncertain) tokens pause_lengths = torch.clamp((entropy * 2).long(), 0, 2) # Final inference with adaptive computation logits = model(test_x, pause_lengths=pause_lengths) predictions = logits[:, :, -1, :].argmax(dim=-1) # Use last pause step print(f"Prediction shape: {predictions.shape}") ``` -------------------------------- ### Initialize and Use PauseTransformer Model Source: https://context7.com/lucidrains/pause-transformer/llms.txt Initializes the PauseTransformer model with specified parameters and performs a forward pass. Use this for basic model setup and inference. ```python import torch from pause_transformer import PauseTransformer # Initialize the Pause Transformer model model = PauseTransformer( num_tokens=256, # Vocabulary size (e.g., 256 for byte-level) dim=512, # Model embedding dimension depth=6, # Number of transformer layers max_pause_length=2, # Maximum thinking steps per token dim_head=64, # Dimension per attention head heads=8, # Number of attention heads ff_mult=4 # Feedforward expansion multiplier ) # Create sample input sequence x = torch.randint(0, 256, (1, 1024)) # (batch_size, sequence_length) # Forward pass - returns logits with pause dimension logits = model(x) # Output shape: (batch, seq_len, max_pause_length + 1, num_tokens) print(f"Logits shape: {logits.shape}") # torch.Size([1, 1024, 3, 256]) ``` -------------------------------- ### Logit Entropy for Adaptive Computation Source: https://context7.com/lucidrains/pause-transformer/llms.txt Returns the entropy of the output logits instead of the logits themselves using `return_logit_entropy=True`. This can be used for adaptive computation, where tokens with high entropy (uncertainty) are allocated more pause steps. Ensure `arrest_pausing=True` is set for the initial pass to get entropy without pausing. ```python import torch from pause_transformer import PauseTransformer model = PauseTransformer( num_tokens=256, dim=512, depth=6, max_pause_length=2 ) x = torch.randint(0, 256, (1, 128)) # First pass: get entropy without pausing to identify uncertain tokens entropy = model(x, arrest_pausing=True, return_logit_entropy=True) print(f"Entropy shape: {entropy.shape}") # torch.Size([1, 128]) print(f"Mean entropy: {entropy.mean().item():.4f}") # Use entropy to determine which tokens need more thinking # Higher entropy = more uncertain = needs more pause steps threshold = entropy.median() pause_lengths = (entropy > threshold).long() * 2 # 0 or 2 pause steps print(f"Adaptive pause lengths: {pause_lengths}") # Second pass: run with adaptive pause lengths logits = model(x, pause_lengths=pause_lengths) ``` -------------------------------- ### Independent Pause Training (no_prev_pause_integration) Source: https://context7.com/lucidrains/pause-transformer/llms.txt Disables the integration of previous token's pause information during training via `no_prev_pause_integration=True`. This forces each token to develop its thinking capability independently, useful for specific training curricula. Compare results with `no_prev_pause_integration=False`. ```python import torch from pause_transformer import PauseTransformer model = PauseTransformer( num_tokens=256, dim=512, depth=6, max_pause_length=2 ) x = torch.randint(0, 256, (4, 256)) # Training without previous pause integration # Each token's thinking is independent of neighbors loss = model(x, return_loss=True, no_prev_pause_integration=True) print(f"Loss (independent pause): {loss.item():.4f}") # Compare with integrated pause training loss_integrated = model(x, return_loss=True, no_prev_pause_integration=False) print(f"Loss (integrated pause): {loss_integrated.item():.4f}") ``` -------------------------------- ### Random Uniform Pausing for Training Regularization Source: https://context7.com/lucidrains/pause-transformer/llms.txt Enables random pause lengths sampled uniformly from [0, max_pause_length] during training. This acts as a regularization technique, encouraging the model to learn variable computation depths. Each forward pass with `rand_uniform_pausing=True` will use different random pause configurations. ```python import torch from pause_transformer import PauseTransformer model = PauseTransformer( num_tokens=256, dim=512, depth=6, max_pause_length=3 ) x = torch.randint(0, 256, (4, 256)) # Training with random uniform pause lengths per token loss = model(x, return_loss=True, rand_uniform_pausing=True) print(f"Loss (random pause): {loss.item():.4f}") # Each forward pass uses different random pause configurations for i in range(3): loss = model(x, return_loss=True, rand_uniform_pausing=True) print(f"Iteration {i+1} loss: {loss.item():.4f}") ``` -------------------------------- ### Train PauseTransformer with Automatic Loss Source: https://context7.com/lucidrains/pause-transformer/llms.txt Trains the PauseTransformer model using automatic cross-entropy loss computation. The model handles label shifting internally, simplifying the training loop. ```python import torch from pause_transformer import PauseTransformer model = PauseTransformer( num_tokens=256, dim=512, depth=6, max_pause_length=2 ) # Training mode - model automatically creates labels from shifted input x = torch.randint(0, 256, (4, 512)) # (batch_size, sequence_length) # Compute loss directly loss = model(x, return_loss=True) print(f"Training loss: {loss.item():.4f}") # Backpropagation loss.backward() ``` -------------------------------- ### Fast Inference with arrest_pausing Source: https://context7.com/lucidrains/pause-transformer/llms.txt Performs fast inference or training by disabling pause token processing using `arrest_pausing=True`. The model behaves like a standard causal transformer, outputting logits without the pause dimension. ```python import torch from pause_transformer import PauseTransformer model = PauseTransformer( num_tokens=256, dim=512, depth=6, max_pause_length=2 ) x = torch.randint(0, 256, (1, 1024)) # Fast inference without pause processing logits = model(x, arrest_pausing=True) # Output shape: (batch, seq_len, num_tokens) - no pause dimension print(f"Logits shape (no pause): {logits.shape}") # torch.Size([1, 1024, 256]) # Training without pause computation loss = model(x, return_loss=True, arrest_pausing=True) print(f"Loss (no pause): {loss.item():.4f}") ``` -------------------------------- ### Custom Pause Lengths Per Token Source: https://context7.com/lucidrains/pause-transformer/llms.txt Utilizes the `pause_lengths` parameter to specify variable thinking steps for each token, enabling fine-grained adaptive computation. This is useful for controlling computational allocation per token. ```python import torch from pause_transformer import PauseTransformer model = PauseTransformer( num_tokens=256, dim=512, depth=6, max_pause_length=4 # Allow up to 4 thinking steps ) batch_size, seq_len = 2, 128 x = torch.randint(0, 256, (batch_size, seq_len)) # Define custom pause lengths per token # Each token can think for 0 to max_pause_length steps pause_lengths = torch.randint(0, 5, (batch_size, seq_len)) # Forward with custom pause distribution logits = model(x, pause_lengths=pause_lengths) # Training with custom pause lengths loss = model(x, return_loss=True, pause_lengths=pause_lengths) print(f"Loss with custom pause: {loss.item():.4f}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.