### Run Enwik8 Training Script Source: https://github.com/lucidrains/block-recurrent-transformer-pytorch/blob/main/README.md Execute the training script after installing the required dependencies. ```bash $ python train.py ``` -------------------------------- ### Install Block Recurrent Transformer Source: https://github.com/lucidrains/block-recurrent-transformer-pytorch/blob/main/README.md Install the package via pip. ```bash $ pip install block-recurrent-transformer-pytorch ``` -------------------------------- ### Full training pipeline with Accelerate Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt A complete training script using the RecurrentTrainerWrapper and Hugging Face Accelerate for distributed training on the enwik8 dataset. ```python import gzip import random import numpy as np import torch from torch.optim import Adam from torch.utils.data import DataLoader, Dataset from accelerate import Accelerator from block_recurrent_transformer_pytorch import BlockRecurrentTransformer, RecurrentTrainerWrapper # Hyperparameters NUM_BATCHES = 100000 BATCH_SIZE = 4 GRADIENT_ACCUMULATE_EVERY = 4 LEARNING_RATE = 1e-4 SEQ_LEN = 2048 # Setup accelerator for distributed training accelerator = Accelerator() device = accelerator.device # Initialize model model = BlockRecurrentTransformer( num_tokens=256, # byte-level for enwik8 dim=512, depth=6, dim_head=64, heads=8, max_seq_len=1024, block_width=512, num_state_vectors=512, recurrent_layers=(4,), use_flash_attn=True ) train_wrapper = RecurrentTrainerWrapper( model, xl_memories_dropout=0.1, state_dropout=0.1 ) # Dataset class for text data 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 def __len__(self): return self.data.size(0) // self.seq_len # Load enwik8 data 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) # Optimizer optimizer = Adam(model.parameters(), lr=LEARNING_RATE) # Prepare for distributed training model, optimizer, train_loader = accelerator.prepare(model, optimizer, train_loader) # Training loop model.train() for batch_idx, batch in enumerate(train_loader): if batch_idx >= NUM_BATCHES: break loss = train_wrapper(batch.to(device)) accelerator.backward(loss / GRADIENT_ACCUMULATE_EVERY) if (batch_idx + 1) % GRADIENT_ACCUMULATE_EVERY == 0: accelerator.clip_grad_norm_(model.parameters(), 0.5) optimizer.step() optimizer.zero_grad() if batch_idx % 100 == 0: accelerator.print(f"Step {batch_idx}, Loss: {loss.item():.4f}") ``` -------------------------------- ### Configure multiple recurrent layers Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Set up multiple recurrent layers at different depths to facilitate complex state propagation. ```python import torch from block_recurrent_transformer_pytorch import BlockRecurrentTransformer ``` -------------------------------- ### Configure compressed memories Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Enable compressed memories to extend effective context length using learned convolutions. ```python import torch from block_recurrent_transformer_pytorch import BlockRecurrentTransformer # Model with compressed memories enabled model = BlockRecurrentTransformer( num_tokens=20000, dim=512, depth=6, dim_head=64, heads=8, max_seq_len=1024, block_width=512, num_state_vectors=512, recurrent_layers=(4,), use_compressed_mem=True, # Enable compressed memories compressed_mem_factor=4, # Compress by 4x (512 tokens -> 128 compressed) use_flash_attn=True ) # With compression enabled, effective memory extends further back: # - Recent: 512 tokens (uncompressed) # - Older: 512 tokens compressed to 128 (representing 512 original tokens) # Total context: ~1024 tokens per memory layer seq = torch.randint(0, 20000, (1, 1024)) logits, memories, states = model(seq) # Process multiple segments for _ in range(5): logits, memories, states = model(seq, xl_memories=memories, states=states) print(f"Processing with {len(memories)} memory layers") ``` -------------------------------- ### Initialize and run inference with BlockRecurrentTransformer Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Configure the transformer with recurrent layers and pass state tensors between forward calls to maintain context. ```python model = BlockRecurrentTransformer( num_tokens=20000, dim=512, depth=8, # 8 layers total dim_head=64, heads=8, max_seq_len=1024, block_width=512, num_state_vectors=256, # State vectors per recurrent layer recurrent_layers=(3, 6), # Recurrence at layers 3 and 6 (1-indexed) read_recurrent_layers=(3, 6), # Where to read states (must be <= write layer) use_flash_attn=True ) seq = torch.randint(0, 20000, (1, 1024)) logits, memories, states = model(seq) # Each recurrent layer maintains its own state print(f"Number of state tensors: {len(states)}") # 2 (one per recurrent layer) # Continue processing with accumulated states logits, memories, states = model(seq, xl_memories=memories, states=states) ``` -------------------------------- ### Initialize and Use Block Recurrent Transformer Source: https://github.com/lucidrains/block-recurrent-transformer-pytorch/blob/main/README.md Configure the model architecture and perform forward passes with recurrent memory states. ```python import torch from block_recurrent_transformer_pytorch import BlockRecurrentTransformer model = BlockRecurrentTransformer( num_tokens = 20000, # vocab size dim = 512, # model dimensions depth = 6, # depth dim_head = 64, # attention head dimensions heads = 8, # number of attention heads max_seq_len = 1024, # the total receptive field of the transformer, in the paper this was 2 * block size block_width = 512, # block size - total receptive field is max_seq_len, 2 * block size in paper. the block furthest forwards becomes the new cached xl memories, which is a block size of 1 (please open an issue if i am wrong) num_state_vectors = 512, # number of state vectors, i believe this was a single block size in the paper, but can be any amount recurrent_layers = (4,), # where to place the recurrent layer(s) for states with fixed simple gating use_compressed_mem = False, # whether to use compressed memories of a single block width, from https://arxiv.org/abs/1911.05507 compressed_mem_factor = 4, # compression factor of compressed memories use_flash_attn = True # use flash attention, if on pytorch 2.0 ) seq = torch.randint(0, 2000, (1, 1024)) out, mems1, states1 = model(seq) out, mems2, states2 = model(seq, xl_memories = mems1, states = states1) out, mems3, states3 = model(seq, xl_memories = mems2, states = states2) ``` -------------------------------- ### Initialize BlockRecurrentTransformer Model Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Initialize the main transformer model class with a full configuration. This model processes sequences in blocks while maintaining recurrent states and transformer-XL memories for long-range context. ```python import torch from block_recurrent_transformer_pytorch import BlockRecurrentTransformer # Initialize the model with full configuration model = BlockRecurrentTransformer( num_tokens=20000, # Vocabulary size dim=512, # Model dimension depth=6, # Number of transformer layers dim_head=64, # Dimension per attention head heads=8, # Number of attention heads max_seq_len=1024, # Total receptive field (typically 2 * block_width) block_width=512, # Block size for processing num_state_vectors=512, # Number of recurrent state vectors recurrent_layers=(4,), # Layers with recurrence (1-indexed) use_compressed_mem=False, # Enable compressed memories compressed_mem_factor=4, # Compression ratio for memories use_flash_attn=True # Use Flash Attention (PyTorch 2.0+) ) # Forward pass with sequence seq = torch.randint(0, 20000, (1, 1024)) # First segment - no prior memories or states logits, memories1, states1 = model(seq) # logits: (batch, seq_len, num_tokens) # memories1: list of tensors for XL memories # states1: list of tensors for recurrent states # Subsequent segments - pass prior memories and states for continuity logits, memories2, states2 = model(seq, xl_memories=memories1, states=states1) logits, memories3, states3 = model(seq, xl_memories=memories2, states=states2) print(f"Output shape: {logits.shape}") # torch.Size([1, 1024, 20000]) ``` -------------------------------- ### Train with RecurrentTrainerWrapper Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Use the training wrapper to handle segmented training over long sequences with automatic state and memory propagation. ```python import torch from torch.optim import Adam from block_recurrent_transformer_pytorch import BlockRecurrentTransformer, RecurrentTrainerWrapper # Create the base model model = BlockRecurrentTransformer( num_tokens=256, dim=512, depth=6, dim_head=64, heads=8, max_seq_len=1024, block_width=512, num_state_vectors=512, recurrent_layers=(4,), use_flash_attn=True ) # Wrap with training wrapper train_wrapper = RecurrentTrainerWrapper( model, xl_memories_dropout=0.1, # Probability of dropping XL memories during training state_dropout=0.1 # Probability of dropping states during training ) optimizer = Adam(model.parameters(), lr=1e-4) # Training loop with long sequence # Sequence length must be (n * max_seq_len) + 1 for proper segmentation seq_len = 2048 # 2 segments of 1024 each batch = torch.randint(0, 256, (4, seq_len + 1)) # batch=4, seq_len=2049 # Forward pass computes loss over all segments model.train() loss = train_wrapper(batch) print(f"Training loss: {loss.item():.4f}") # Backward pass and optimization loss.backward() optimizer.step() optimizer.zero_grad() ``` -------------------------------- ### Text Generation with Autoregressive Model Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Generate sequences autoregressively using the model's `generate` method. Supports top-k sampling and temperature control for diverse text generation. Ensure the model is in evaluation mode (`model.eval()`) before generation. ```python import torch from block_recurrent_transformer_pytorch import BlockRecurrentTransformer model = BlockRecurrentTransformer( num_tokens=256, dim=512, depth=6, dim_head=64, heads=8, max_seq_len=1024, block_width=512, num_state_vectors=512, recurrent_layers=(4,), use_flash_attn=True ) model.eval() # Prime sequence to start generation prime = torch.randint(0, 256, (1, 128)) # batch=1, prime_length=128 ``` -------------------------------- ### Generate tokens with BlockRecurrentTransformer Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Perform token generation using the base model, optionally providing prior memories and states. ```python with torch.no_grad(): generated = model.generate( prime, length=512, # Total length to generate (must be <= max_seq_len+1) temperature=0.9, # Sampling temperature (lower = more deterministic) filter_thres=0.9, # Top-k filtering threshold xl_memories=[], # Optional prior memories states=[], # Optional prior states return_memories_and_states=False # Return memories/states for continued generation ) print(f"Generated shape: {generated.shape}") # torch.Size([1, 384]) ``` -------------------------------- ### Generate long sequences with RecurrentTrainerWrapper Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Generate sequences exceeding the model's max_seq_len by leveraging the wrapper's automatic segmentation. ```python import torch from block_recurrent_transformer_pytorch import BlockRecurrentTransformer, RecurrentTrainerWrapper model = BlockRecurrentTransformer( num_tokens=256, dim=512, depth=6, dim_head=64, heads=8, max_seq_len=1024, block_width=512, num_state_vectors=512, recurrent_layers=(4,), use_flash_attn=True ) train_wrapper = RecurrentTrainerWrapper(model) model.eval() # Start with a prime sequence prime = torch.randint(0, 256, (1, 64)) # Generate much longer than max_seq_len (handles multiple segments automatically) with torch.no_grad(): generated = train_wrapper.generate( prime, length=4096, # Can exceed max_seq_len - wrapper handles segmentation temperature=0.9, filter_thres=0.9 ) print(f"Generated {generated.shape[-1]} tokens") # 4096 tokens ``` -------------------------------- ### Forward Pass with Loss Computation Source: https://context7.com/lucidrains/block-recurrent-transformer-pytorch/llms.txt Compute cross-entropy loss directly during the forward pass by setting `return_loss=True`. The model automatically handles target offset for next-token prediction and allows specifying an `ignore_index` for loss calculation. ```python import torch from block_recurrent_transformer_pytorch import BlockRecurrentTransformer model = BlockRecurrentTransformer( num_tokens=256, dim=512, depth=6, dim_head=64, heads=8, max_seq_len=1024, block_width=512, num_state_vectors=512, recurrent_layers=(4,), ignore_index=-100, # Token index to ignore in loss computation use_flash_attn=True ) # Input sequence (includes target token at the end) seq = torch.randint(0, 256, (2, 1025)) # batch=2, seq_len+1 for loss # Forward pass with loss computation loss, memories, states = model( seq, return_loss=True, # Compute cross-entropy loss xl_memories=[], # No prior memories states=[] # No prior states ) print(f"Loss: {loss.item():.4f}") # Backpropagation loss.backward() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.