Upload folder using huggingface_hub
Browse files23 unmodified lines24252627282930313255 unmodified lines88899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914023 unmodified linesMODEL_NAME = "distilgpt2"HIDDEN_DIM = 768 # distilgpt2 hidden dimensionMEMORY_DIM = 256 # Memory space dimensionLEARNING_RATE = 1e-3 # Base learning rate for test-time updatesMAX_NEW_TOKENS = 50 # Max tokens to generateMEMORY_ALPHA = 0.1 # Memory influence strength on generation# ========== Initialize Components ==========print("🧠 Initializing Titans + MIRAS brain...")55 unmodified linesif seq_len > 1:# We have context - train on predicting each next tokenwith torch.enable_grad():total_lm_loss = 0.0# For each position (except last), predict next tokenfor pos in range(seq_len - 1):h_pos = all_hidden[:, pos, :] # Hidden at position pos# Project to memory spacek = key_proj(h_pos)# Query memory and augment hidden statememory_out = memory(k)h_augmented = h_pos + MEMORY_ALPHA * output_proj(memory_out)# Compute logits for next tokenlogits = model.lm_head(h_augmented) # (1, vocab_size)# Target is the actual next tokentarget = inputs['input_ids'][:, pos + 1]# Cross-entropy losslm_loss = nn.functional.cross_entropy(logits, target)total_lm_loss = total_lm_loss + lm_loss# Average loss over positionsmemory_loss = total_lm_loss / (seq_len - 1)# Get retention factorretention = memory.retention_gate(memory_loss)effective_lr = LEARNING_RATE * retention# Backprop and updatememory_loss.backward()with torch.no_grad():# Update memoryif memory.W.grad is not None:memory.W -= effective_lr * memory.W.gradmemory.W.grad.zero_()# Update output projectionif output_proj.projection.weight.grad is not None:output_proj.projection.weight -= effective_lr * output_proj.projection.weight.gradoutput_proj.projection.weight.grad.zero_()# Update statsmemory.update_stats(memory_loss)else:# Single token - just compute MSE for statswith torch.no_grad():23 unmodified lines2425262728293031323355 unmodified lines899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314423 unmodified linesMODEL_NAME = "distilgpt2"HIDDEN_DIM = 768 # distilgpt2 hidden dimensionMEMORY_DIM = 256 # Memory space dimensionLEARNING_RATE = 0.01 # Increased learning rate for faster adaptationMAX_NEW_TOKENS = 50 # Max tokens to generateMEMORY_ALPHA = 1.0 # Increased from 0.1 - stronger memory influenceNUM_TRAIN_STEPS = 5 # Multiple gradient steps per input for better learning# ========== Initialize Components ==========print("🧠 Initializing Titans + MIRAS brain...")55 unmodified linesif seq_len > 1:# We have context - train on predicting each next token# Run multiple training steps for faster learningfor train_step in range(NUM_TRAIN_STEPS):with torch.enable_grad():total_lm_loss = 0.0# For each position (except last), predict next tokenfor pos in range(seq_len - 1):h_pos = all_hidden[:, pos, :] # Hidden at position pos# Project to memory spacek = key_proj(h_pos)# Query memory and augment hidden statememory_out = memory(k)h_augmented = h_pos + MEMORY_ALPHA * output_proj(memory_out)# Compute logits for next tokenlogits = model.lm_head(h_augmented) # (1, vocab_size)# Target is the actual next tokentarget = inputs['input_ids'][:, pos + 1]# Cross-entropy losslm_loss = nn.functional.cross_entropy(logits, target)total_lm_loss = total_lm_loss + lm_loss# Average loss over positionsmemory_loss = total_lm_loss / (seq_len - 1)# Get retention factorretention = memory.retention_gate(memory_loss)effective_lr = LEARNING_RATE * retention# Backprop and updatememory_loss.backward()with torch.no_grad():# Update memoryif memory.W.grad is not None:memory.W -= effective_lr * memory.W.gradmemory.W.grad.zero_()# Update output projectionif output_proj.projection.weight.grad is not None:output_proj.projection.weight -= effective_lr * output_proj.projection.weight.gradoutput_proj.projection.weight.grad.zero_()# Update stats after all training steps (use final loss)with torch.no_grad():memory.update_stats(memory_loss)else:# Single token - just compute MSE for statswith torch.no_grad():
app.py+48-44