### Complete Training Example Setup Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Sets up a complete training pipeline for a FLASH Transformer on the enwik8 dataset. Includes model instantiation, AutoregressiveWrapper, optimizer, and dataset/dataloader configuration for character-level language modeling. ```python import torch import torch.optim as optim from torch.utils.data import DataLoader, Dataset from flash_pytorch import FLASHTransformer from flash_pytorch.autoregressive_wrapper import AutoregressiveWrapper import gzip import numpy as np # Configuration SEQ_LEN = 1024 BATCH_SIZE = 4 GRADIENT_ACCUMULATE_EVERY = 4 LEARNING_RATE = 2e-4 # Create model model = FLASHTransformer( num_tokens=256, dim=512, depth=8, causal=True, group_size=256, shift_tokens=True, laplace_attn_fn=True ) model = AutoregressiveWrapper(model) model.cuda() ``` -------------------------------- ### Install FLASH-pytorch Source: https://github.com/lucidrains/flash-pytorch/blob/main/README.md Install the package via pip. ```bash $ pip install FLASH-pytorch ``` -------------------------------- ### AutoregressiveWrapper Text Generation Example Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Generate new text sequences using the AutoregressiveWrapper. Provide start tokens (prompt) and specify generation length, temperature for sampling, and optionally an end-of-sequence token. Top-k filtering can be applied. ```python start_tokens = torch.randint(0, 256, (1, 64)).cuda() # Prompt tokens generated = model.generate( start_tokens, seq_len=512, # Generate 512 new tokens temperature=0.8, # Sampling temperature filter_thres=0.9, # Top-k filtering threshold eos_token=None # Optional early stopping token ) print(f"Generated shape: {generated.shape}") ``` -------------------------------- ### AutoregressiveWrapper Training Example Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Compute cross-entropy loss for training using the AutoregressiveWrapper. The wrapper automatically handles the next-token prediction shift. Ensure input tensors are on the correct device (e.g., CUDA). ```python x = torch.randint(0, 256, (4, 1025)).cuda() # (batch, seq_len+1) loss = model(x) # Automatically shifts for next-token prediction loss.backward() print(f"Training loss: {loss.item()}") ``` -------------------------------- ### Decode Generated Tokens Source: https://context7.com/lucidrains/flash-pytorch/llms.txt A helper function to decode generated token IDs back into human-readable text. This example assumes character-level encoding where token IDs map to ASCII characters. ```python def decode_tokens(tokens): return ''.join([chr(max(32, t)) for t in tokens]) output_text = decode_tokens(generated[0].cpu().tolist()) print(f"Generated text: {output_text[:100]}...") ``` -------------------------------- ### Initialize FLASHTransformer Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Import the FLASHTransformer class for building complete transformer architectures. ```python import torch from flash_pytorch import FLASHTransformer ``` -------------------------------- ### Initialize and use FLASH attention layer Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Configure the FLASH module for hybrid quadratic-linear attention and process sequences with padding masks. ```python import torch from flash_pytorch import FLASH # Create FLASH attention layer flash = FLASH( dim=512, # Model dimension group_size=256, # Size of attention groups causal=True, # Autoregressive mode query_key_dim=128, # Query/key dimension expansion_factor=2., # Hidden dimension multiplier laplace_attn_fn=True, # Use Laplacian attention function shift_tokens=True, # Enable token shifting for better convergence reduce_group_non_causal_attn=True # Reduce groups in non-causal mode ) # Process sequences - auto-pads to nearest group size x = torch.randn(1, 1111, 512) # Arbitrary sequence length output = flash(x) # (1, 1111, 512) - maintains original length # With padding mask for variable-length sequences batch_size, seq_len = 4, 1000 x_batch = torch.randn(batch_size, seq_len, 512) lengths = [800, 950, 1000, 750] # Actual lengths per sequence mask = torch.zeros(batch_size, seq_len, dtype=torch.bool) for i, length in enumerate(lengths): mask[i, :length] = True output_batch = flash(x_batch, mask=mask) print(f"Batch output shape: {output_batch.shape}") # Batch output shape: torch.Size([4, 1000, 512]) ``` -------------------------------- ### Initialize FLASH Source: https://github.com/lucidrains/flash-pytorch/blob/main/README.md Combine GAU with grouped linear attention to overcome issues with autoregressive linear attention. ```python import torch from flash_pytorch import FLASH flash = FLASH( dim = 512, group_size = 256, # group size causal = True, # autoregressive or not query_key_dim = 128, # query / key dimension expansion_factor = 2., # hidden dimension = dim * expansion_factor laplace_attn_fn = True # new Mega paper claims this is more stable than relu squared as attention function ) x = torch.randn(1, 1111, 512) # sequence will be auto-padded to nearest group size out = flash(x) # (1, 1111, 512) ``` -------------------------------- ### Load and Prepare enwik8 Dataset Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Loads the enwik8 dataset from a gzipped file, splits it into training and validation sets, and creates PyTorch DataLoaders for training. ```python # Load and prepare enwik8 data with gzip.open('./data/enwik8.gz') as file: X = np.frombuffer(file.read(int(95e6)), dtype=np.uint8) trX, vaX = np.split(X, [int(90e6)]) data_train, data_val = torch.from_numpy(trX), torch.from_numpy(vaX) train_dataset = TextSamplerDataset(data_train, SEQ_LEN) train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True) ``` -------------------------------- ### Initialize FLASHTransformer Source: https://github.com/lucidrains/flash-pytorch/blob/main/README.md Use the full FLASH transformer architecture with support for positional embeddings and RoPE. ```python import torch from flash_pytorch import FLASHTransformer model = FLASHTransformer( num_tokens = 20000, # number of tokens dim = 512, # model dimension depth = 12, # depth causal = True, # autoregressive or not group_size = 256, # size of the groups query_key_dim = 128, # dimension of queries / keys expansion_factor = 2., # hidden dimension = dim * expansion_factor norm_type = 'scalenorm', # in the paper, they claimed scalenorm led to faster training at no performance hit. the other option is 'layernorm' (also default) shift_tokens = True # discovered by an independent researcher in Shenzhen @BlinkDL, this simply shifts half of the feature space forward one step along the sequence dimension - greatly improved convergence even more in my local experiments ) x = torch.randint(0, 20000, (1, 1024)) logits = model(x) # (1, 1024, 20000) ``` -------------------------------- ### Instantiate FLASH Transformer for Language Modeling Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Instantiate a FLASHTransformer for causal language modeling. Configure vocabulary size, model dimensions, depth, attention parameters, and normalization types. Ensure 'causal=True' for autoregressive tasks. ```python model = FLASHTransformer( num_tokens=20000, # Vocabulary size dim=512, # Model dimension depth=12, # Number of FLASH layers causal=True, # Autoregressive mode group_size=256, # Attention group size query_key_dim=128, # Query/key dimension expansion_factor=2., # Hidden dimension multiplier norm_type='scalenorm', # 'scalenorm' (faster) or 'layernorm' shift_tokens=True, # Token shifting for improved convergence laplace_attn_fn=True, # Laplacian attention function attn_dropout=0.1 # Attention dropout ) ``` -------------------------------- ### Initialize and use Gated Attention Unit (GAU) Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Instantiate a GAU for autoregressive modeling and process sequences with optional masking. ```python import torch from flash_pytorch import GAU # Create a Gated Attention Unit for autoregressive modeling gau = GAU( dim=512, # Model dimension query_key_dim=128, # Query/key projection dimension causal=True, # Enable causal masking for autoregressive expansion_factor=2, # Hidden dim = dim * expansion_factor laplace_attn_fn=True, # Use Laplacian attention (more stable than ReLU squared) dropout=0.1, # Dropout rate add_residual=True # Add residual connection ) # Process a batch of sequences x = torch.randn(1, 1024, 512) # (batch, seq_len, dim) output = gau(x) # (1, 1024, 512) # With optional attention mask mask = torch.ones(1, 1024, dtype=torch.bool) mask[:, 512:] = False # Mask out second half output_masked = gau(x, mask=mask) print(f"Input shape: {x.shape}") print(f"Output shape: {output.shape}") # Input shape: torch.Size([1, 1024, 512]) # Output shape: torch.Size([1, 1024, 512]) ``` -------------------------------- ### Run Autoregressive Enwik8 Test Source: https://github.com/lucidrains/flash-pytorch/blob/main/README.md Execute the training script for the Enwik8 benchmark. ```bash $ python train.py ``` -------------------------------- ### Initialize Gated Attention Unit (GAU) Source: https://github.com/lucidrains/flash-pytorch/blob/main/README.md Use GAU to replace multi-headed attention with a single head using ReLU squared activation. ```python import torch from flash_pytorch import GAU gau = GAU( dim = 512, query_key_dim = 128, # query / key dimension causal = True, # autoregressive or not expansion_factor = 2, # hidden dimension = dim * expansion_factor laplace_attn_fn = True # new Mega paper claims this is more stable than relu squared as attention function ) x = torch.randn(1, 1024, 512) out = gau(x) # (1, 1024, 512) ``` -------------------------------- ### Implement Gradient Accumulation Training Loop Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Uses standard PyTorch optimization patterns with gradient accumulation to scale training across multiple batches. ```python optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE) for batch_idx, batch in enumerate(train_loader): model.train() loss = model(batch) loss = loss / GRADIENT_ACCUMULATE_EVERY loss.backward() if (batch_idx + 1) % GRADIENT_ACCUMULATE_EVERY == 0: torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5) optimizer.step() optimizer.zero_grad() print(f"Step {batch_idx + 1}, Loss: {loss.item() * GRADIENT_ACCUMULATE_EVERY:.4f}") if batch_idx >= 100: # Demo: stop after 100 batches break ``` -------------------------------- ### Wrap FLASH Transformer with AutoregressiveWrapper Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Wrap a FLASHTransformer with AutoregressiveWrapper for convenient training and generation. The wrapper handles padding and shifting for autoregressive tasks. Move the model to CUDA if available. ```python base_model = FLASHTransformer( num_tokens=256, # Character-level vocabulary dim=512, depth=8, causal=True, group_size=256, shift_tokens=True, laplace_attn_fn=True ) model = AutoregressiveWrapper(base_model, pad_value=0) model.cuda() ``` -------------------------------- ### FLASH Transformer Forward Pass for Training Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Perform a forward pass with the FLASHTransformer for training. Input should be token IDs. The output logits can be used for calculating cross-entropy loss. ```python x = torch.randint(0, 20000, (1, 1024)) # (batch, seq_len) logits = model(x) # (1, 1024, 20000) print(f"Input shape: {x.shape}") print(f"Output logits shape: {logits.shape}") ``` -------------------------------- ### Citations Source: https://github.com/lucidrains/flash-pytorch/blob/main/README.md BibTeX entries for the relevant research papers and software. ```bibtex @article{Hua2022TransformerQI, title = {Transformer Quality in Linear Time}, author = {Weizhe Hua and Zihang Dai and Hanxiao Liu and Quoc V. Le}, journal = {ArXiv}, year = {2022}, volume = {abs/2202.10447} } ``` ```bibtex @software{peng_bo_2021_5196578, author = {PENG Bo}, title = {BlinkDL/RWKV-LM: 0.01}, month = {aug}, year = {2021}, publisher = {Zenodo}, version = {0.01}, doi = {10.5281/zenodo.5196578}, url = {https://doi.org/10.5281/zenodo.5196578} } ``` ```bibtex @inproceedings{Ma2022MegaMA, title = {Mega: Moving Average Equipped Gated Attention}, author = {Xuezhe Ma and Chunting Zhou and Xiang Kong and Junxian He and Liangke Gui and Graham Neubig and Jonathan May and Luke Zettlemoyer}, year = {2022} } ``` -------------------------------- ### FLASH Transformer Forward Pass with Attention Mask Source: https://context7.com/lucidrains/flash-pytorch/llms.txt Perform a forward pass with attention masking for padded batches. The mask should be a boolean tensor where 'False' indicates masked positions. ```python batch_tokens = torch.randint(0, 20000, (4, 512)) mask = torch.ones(4, 512, dtype=torch.bool) mask[0, 400:] = False # First sequence is shorter logits_masked = model(batch_tokens, mask=mask) ``` -------------------------------- ### TextSamplerDataset for Character-Level Data Source: https://context7.com/lucidrains/flash-pytorch/llms.txt A PyTorch Dataset class for loading and sampling character-level text data. It reads sequences of a specified length from a numpy array and prepares them for training. ```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 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.