### Install Llama QRLHF Source: https://context7.com/lucidrains/llama-qrlhf/llms.txt Provides instructions for installing the library via pip or from source. ```bash pip install llama-qrlhf # Or install from source git clone https://github.com/lucidrains/llama-qrlhf cd llama-qrlhf pip install -e . # Dependencies # - torch>=2.4 # - accelerate # - ema-pytorch # - einx>=0.3.0 # - einops>=0.8.0 # - jaxtyping ``` -------------------------------- ### Initialize Dueling Q-Head Source: https://context7.com/lucidrains/llama-qrlhf/llms.txt Implements the Dueling Network Architecture to separately estimate state values and action advantages. ```python import torch from llama_qrlhf.llama import DuelingHead # Initialize dueling head q_head = DuelingHead( dim=512, # Input dimension from transformer num_tokens=32000, # Number of possible actions (vocab size) expansion_factor=2 # Hidden layer expansion ) # Input embeddings from transformer embeddings = torch.randn(2, 128, 512) # [batch, seq_len, dim] # Get Q-values via dueling architecture q_values = q_head(embeddings) print(f"Q-values shape: {q_values.shape}") # [2, 128, 32000] # Q = V(s) + A(s,a) - mean(A(s,a)) # Values and advantages are computed separately then combined ``` -------------------------------- ### Configure QRLHF Trainer with EMA Source: https://context7.com/lucidrains/llama-qrlhf/llms.txt Wraps a language model with an EMA target network to ensure stable Q-value estimation during training. ```python import torch from torch.utils.data import Dataset, DataLoader from llama_qrlhf import QRLHF from llama_qrlhf.llama import Llama # Define a simple dataset class TextDataset(Dataset): def __init__(self, num_samples=1000, seq_len=64, vocab_size=32000): self.data = torch.randint(0, vocab_size, (num_samples, seq_len)) def __len__(self): return len(self.data) def __getitem__(self, idx): return self.data[idx] # Initialize model model = Llama( num_tokens=32000, dim=256, depth=4, dueling_q_head=True ) dataset = TextDataset() # Create QRLHF trainer with EMA target network qrlhf = QRLHF( model=model, dataset=dataset, accelerate_kwargs={}, ema_kwargs={'beta': 0.99} # EMA decay rate for target model ) # Access the model and EMA target print(f"Main model: {qrlhf.lm}") print(f"EMA target model: {qrlhf.lm_target}") ``` -------------------------------- ### Initialize Llama Model with Q-learning Head Source: https://context7.com/lucidrains/llama-qrlhf/llms.txt Configures a Llama transformer model with optional dueling Q-value output heads for reinforcement learning tasks. ```python import torch from llama_qrlhf.llama import Llama # Initialize a Llama model with Q-learning head model = Llama( num_tokens=32000, # 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 ff_mult=4, # Feedforward expansion multiplier dueling_q_head=True, # Enable dueling Q-network head dueling_q_head_expansion_factor=2 # Q-head hidden expansion ) # Forward pass with input tokens input_ids = torch.randint(0, 32000, (2, 128)) # batch=2, seq_len=128 # Get logits only (standard language modeling) logits = model(input_ids) print(f"Logits shape: {logits.shape}") # [2, 128, 32000] # Get both logits and Q-values for RL training logits, q_values = model(input_ids, return_q_values=True) print(f"Q-values shape: {q_values.shape}") # [2, 128, 32000] ``` -------------------------------- ### Apply Conservative Regularization Loss Source: https://context7.com/lucidrains/llama-qrlhf/llms.txt Calculates CQL-style regularization to penalize out-of-distribution actions and prevent Q-value overestimation. ```python import torch from llama_qrlhf.llama_qrlhf import conservative_regularization_loss batch_size, seq_len, num_actions = 4, 32, 1000 # Q-values from model forward pass q_values = torch.randn(batch_size, seq_len, num_actions) ``` -------------------------------- ### Compute Autoregressive Q-Learning Loss Source: https://context7.com/lucidrains/llama-qrlhf/llms.txt Calculates the Q-learning loss using the Q-Transformer formulation, requiring a model, EMA target, and reward signals. ```python import torch from llama_qrlhf.llama import Llama from llama_qrlhf.llama_qrlhf import autoregressive_q_learn from ema_pytorch import EMA # Setup model and EMA target model = Llama( num_tokens=1000, dim=128, depth=2, dueling_q_head=True ) ema_model = EMA(model, beta=0.99) batch_size, seq_len = 4, 32 # Prepare training batch states = torch.randint(0, 1000, (batch_size, seq_len)) # Full sequences prompt_len = torch.randint(5, 15, (batch_size, seq_len)) # Prompt lengths per position next_states = torch.randint(0, 1000, (batch_size,)) # Next token actions rewards = torch.zeros(batch_size, seq_len) # Sparse rewards rewards[:, -1] = 1.0 # Reward at end of sequence # Compute Q-learning loss loss = autoregressive_q_learn( model=model, ema_model=ema_model, states=states, prompt_len=prompt_len, next_states=next_states, rewards=rewards, eos_id=2, # EOS token ID for terminal detection discount_gamma=0.998 # Reward discount factor ) print(f"Q-learning loss: {loss.item()}") ``` -------------------------------- ### Compute Conservative Regularization Loss Source: https://context7.com/lucidrains/llama-qrlhf/llms.txt Calculates the CQL loss to prevent overestimation of Q-values for out-of-distribution actions. ```python # Actual actions taken in dataset states_and_actions = torch.randint(0, num_actions, (batch_size, seq_len)) # Mask indicating which positions are actions (vs prompts) action_mask = torch.ones(batch_size, seq_len, dtype=torch.bool) action_mask[:, :8] = False # First 8 tokens are prompt # Compute conservative regularization cql_loss = conservative_regularization_loss( q_values=q_values, states_and_actions=states_and_actions, action_mask=action_mask, reward_min=0.0 # Minimum reward to regularize towards ) print(f"CQL regularization loss: {cql_loss.item()}") ``` -------------------------------- ### Implement Attention with Rotary Embeddings Source: https://context7.com/lucidrains/llama-qrlhf/llms.txt Configures multi-head self-attention with RoPE and causal masking for autoregressive modeling. ```python import torch from llama_qrlhf.llama import Attention, RotaryEmbedding # Initialize attention layer attention = Attention( dim=512, dim_head=64, heads=8 ) # Initialize rotary embeddings rotary = RotaryEmbedding(dim=64, theta=10000) # Input sequence x = torch.randn(2, 128, 512) # [batch, seq_len, dim] # Generate rotary embeddings for sequence length rotary_emb = rotary(seq_len=128, device=x.device) # Forward pass with rotary embeddings output = attention(x, rotary_emb=rotary_emb) print(f"Attention output shape: {output.shape}") # [2, 128, 512] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.