### Install ponder-transformer Source: https://github.com/lucidrains/ponder-transformer/blob/main/README.md Install the ponder-transformer package using pip. ```bash pip install ponder-transformer ``` -------------------------------- ### Complete training loop for Ponder Transformer Source: https://context7.com/lucidrains/ponder-transformer/llms.txt A full example showing model configuration, training loop with loss optimization, and inference mode usage. ```python import torch from torch.optim import AdamW from ponder_transformer import PonderTransformer # Model configuration model = PonderTransformer( num_tokens=10000, dim=256, max_seq_len=128, causal=True, heads=4, dim_head=64, ponder_kl_div_loss_weight=0.01, ponder_lambda_p=0.2 ) optimizer = AdamW(model.parameters(), lr=1e-4) # Training loop model.train() for step in range(100): # Simulate batch data x = torch.randint(0, 10000, (16, 128)) labels = torch.randint(0, 10000, (16, 128)) mask = torch.ones(16, 128).bool() optimizer.zero_grad() loss = model(x, labels=labels, mask=mask) loss.backward() optimizer.step() if step % 10 == 0: print(f"Step {step}, Loss: {loss.item():.4f}") # Inference with adaptive computation model.eval() with torch.no_grad(): test_input = torch.randint(0, 10000, (4, 128)) test_mask = torch.ones(4, 128).bool() logits, halt_layers = model(test_input, mask=test_mask) print(f"Inference complete - halt layers: {halt_layers.tolist()}") # Example: [2, 4, 3, 5] - different samples used different computation depths ``` -------------------------------- ### Initialize and run a single Ponder Transformer block Source: https://context7.com/lucidrains/ponder-transformer/llms.txt Demonstrates the instantiation of a single Block and performing a forward pass to obtain transformed states and halt logits. ```python block = Block( dim=512, # Model dimension dim_head=64, # Dimension per attention head heads=8, # Number of attention heads causal=True, # Causal masking for autoregressive ff_mult=4 # Feedforward hidden dimension multiplier ) # Input hidden states x = torch.randn(2, 128, 512) # (batch, seq_len, dim) mask = torch.ones(2, 128).bool() # Forward pass returns transformed states and halt logits output, halt_logits = block(x, mask=mask) print(f"Output shape: {output.shape}") # (2, 128, 512) print(f"Halt logits shape: {halt_logits.shape}") # (2, 128) for causal, (2,) for non-causal ``` -------------------------------- ### Initialize and Train PonderTransformer Source: https://github.com/lucidrains/ponder-transformer/blob/main/README.md Initialize the PonderTransformer model and compute a loss for training. Ensure labels are provided for loss calculation. ```python import torch from ponder_transformer import PonderTransformer model = PonderTransformer( num_tokens = 20000, dim = 512, max_seq_len = 512 ) mask = torch.ones(1, 512).bool() x = torch.randint(0, 20000, (1, 512)) y = torch.randint(0, 20000, (1, 512)) loss = model(x, labels = y, mask = mask) loss.backward() ``` -------------------------------- ### Initialize and Train PonderTransformer Source: https://context7.com/lucidrains/ponder-transformer/llms.txt Initialize the PonderTransformer model and use it in training mode. Returns a combined loss including KL divergence for regularization. Ensure input token IDs, target token IDs, and attention mask are correctly formatted. ```python import torch from ponder_transformer import PonderTransformer # Initialize the model with vocabulary and architecture settings model = PonderTransformer( num_tokens=20000, # Vocabulary size dim=512, # Model dimension max_seq_len=512, # Maximum sequence length causal=True, # Enable causal masking for autoregressive tasks dim_head=64, # Dimension per attention head heads=8, # Number of attention heads ponder_kl_div_loss_weight=0.01, # Weight for KL divergence regularization ponder_lambda_p=0.2, # Geometric prior probability parameter ponder_epsilon=0.05 # Threshold for computing max training steps ) # Training mode - returns combined loss (weighted cross-entropy + KL divergence) model.train() x = torch.randint(0, 20000, (4, 512)) # Input token IDs: (batch, seq_len) labels = torch.randint(0, 20000, (4, 512)) # Target token IDs: (batch, seq_len) mask = torch.ones(4, 512).bool() # Attention mask: (batch, seq_len) loss = model(x, labels=labels, mask=mask) loss.backward() # Output: scalar tensor with combined pondering loss ``` -------------------------------- ### Initialize and Evaluate Causal PonderTransformer Source: https://github.com/lucidrains/ponder-transformer/blob/main/README.md Initialize a causal PonderTransformer and set it to evaluation mode. In eval mode, the model returns logits and halting indices, enabling early termination. ```python import torch from ponder_transformer import PonderTransformer model = PonderTransformer( num_tokens = 20000, dim = 512, max_seq_len = 512, causal = True ) x = torch.randint(0, 20000, (2, 512)) mask = torch.ones(2, 512).bool() model.eval() # setting to eval makes it return the logits as well as the halting indices logits, layer_indices = model(x, mask = mask) # (2, 512, 20000), (2) ``` -------------------------------- ### Initialize Non-Causal PonderTransformer for Bidirectional Tasks Source: https://context7.com/lucidrains/ponder-transformer/llms.txt Initialize the PonderTransformer for non-autoregressive tasks by setting causal=False to enable bidirectional attention. This mode is suitable for classification or encoding tasks. ```python import torch from ponder_transformer import PonderTransformer # Non-causal model for bidirectional tasks model = PonderTransformer( num_tokens=20000, dim=512, max_seq_len=256, causal=False, # Bidirectional attention heads=8, dim_head=64 ) # Training with bidirectional attention model.train() x = torch.randint(0, 20000, (8, 256)) labels = torch.randint(0, 20000, (8, 256)) mask = torch.ones(8, 256).bool() loss = model(x, labels=labels, mask=mask) loss.backward() # Evaluation returns logits and per-batch halt indices model.eval() logits, halt_indices = model(x, mask=mask) print(f"Bidirectional logits: {logits.shape}") # (8, 256, 20000) ``` -------------------------------- ### Evaluate PonderTransformer with Early Stopping Source: https://context7.com/lucidrains/ponder-transformer/llms.txt Use the PonderTransformer in evaluation mode for inference. The model automatically halts computation when all batch samples have emitted halting signals, returning logits and the layer indices where each sample halted. ```python import torch from ponder_transformer import PonderTransformer model = PonderTransformer( num_tokens=20000, dim=512, max_seq_len=512, causal=True ) # Switch to evaluation mode for inference with early halting model.eval() x = torch.randint(0, 20000, (2, 512)) # Batch of 2 sequences mask = torch.ones(2, 512).bool() # Returns logits and layer indices where each batch element halted logits, halt_layer_indices = model(x, mask=mask) print(f"Logits shape: {logits.shape}") # (2, 512, 20000) - predictions print(f"Halt indices: {halt_layer_indices}") # (2,) - layer where each sample exited # Example output: # Logits shape: torch.Size([2, 512, 20000]) # Halt indices: tensor([3, 5]) # First sample halted at layer 3, second at layer 5 ``` -------------------------------- ### PonderTransformer Block Class Source: https://context7.com/lucidrains/ponder-transformer/llms.txt The internal Block class represents a single transformer layer with attention, feedforward, and halting probability computation. It outputs transformed hidden states and halt logits for adaptive computation. ```python import torch from ponder_transformer.ponder_transformer import Block ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.