Upload README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# LMCODE: Language Model with Memory CODE
|
| 2 |
+
|
| 3 |
+
A memory-augmented language model with **dual memory systems**: long-term and short-term memory, inspired by recent research in memory-augmented neural networks.
|
| 4 |
+
|
| 5 |
+
## Overview
|
| 6 |
+
|
| 7 |
+
LMCODE (Language Model with Memory CODE) extends traditional transformer-based language models with sophisticated memory mechanisms that enable:
|
| 8 |
+
|
| 9 |
+
- **Long-term memory**: Persistent storage of knowledge and experiences (10,000+ memory slots)
|
| 10 |
+
- **Short-term memory**: Working memory for immediate context (similar to Transformer KV cache)
|
| 11 |
+
- **Memory retrieval**: Efficient similarity-based retrieval from long-term memory
|
| 12 |
+
- **Memory consolidation**: Automatic merging of similar memories to prevent redundancy
|
| 13 |
+
- **Experience replay**: Training with mixed current data and retrieved memories
|
| 14 |
+
|
| 15 |
+
## Architecture
|
| 16 |
+
|
| 17 |
+
### Components
|
| 18 |
+
|
| 19 |
+
1. **ShortTermMemory**: Recurrent memory module for immediate context
|
| 20 |
+
- Update gates for controlled memory modification
|
| 21 |
+
- Read/write projections for memory access
|
| 22 |
+
- Soft updates to prevent catastrophic forgetting
|
| 23 |
+
|
| 24 |
+
2. **LongTermMemory**: Persistent key-value store for long-term knowledge
|
| 25 |
+
- 10,000+ memory slots per layer
|
| 26 |
+
- Importance-weighted retrieval
|
| 27 |
+
- Consolidation mechanism for similar memories
|
| 28 |
+
- FIFO storage with intelligent replacement
|
| 29 |
+
|
| 30 |
+
3. **MemoryAugmentedLayer**: Transformer layer with integrated memory
|
| 31 |
+
- Self-attention mechanism
|
| 32 |
+
- Short-term memory integration
|
| 33 |
+
- Long-term memory retrieval with gating
|
| 34 |
+
- Feed-forward network
|
| 35 |
+
|
| 36 |
+
4. **LMCODE**: Complete language model
|
| 37 |
+
- Multiple memory-augmented layers
|
| 38 |
+
- Token and position embeddings
|
| 39 |
+
- Language model head
|
| 40 |
+
- Autoregressive generation support
|
| 41 |
+
|
| 42 |
+
## Quick Start
|
| 43 |
+
|
| 44 |
+
### Installation
|
| 45 |
+
|
| 46 |
+
```bash
|
| 47 |
+
pip install torch numpy matplotlib
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
### Basic Usage
|
| 51 |
+
|
| 52 |
+
```python
|
| 53 |
+
from model_architecture import LMCODE, LMCODEConfig
|
| 54 |
+
|
| 55 |
+
# Create configuration
|
| 56 |
+
config = LMCODEConfig(
|
| 57 |
+
vocab_size=50257,
|
| 58 |
+
hidden_size=512,
|
| 59 |
+
num_layers=6,
|
| 60 |
+
num_heads=8,
|
| 61 |
+
short_term_memory_size=512,
|
| 62 |
+
long_term_memory_slots=10000
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Initialize model
|
| 66 |
+
model = LMCODE(config)
|
| 67 |
+
|
| 68 |
+
# Forward pass
|
| 69 |
+
input_ids = torch.randint(0, config.vocab_size, (1, 10))
|
| 70 |
+
outputs = model(input_ids, use_long_term_memory=True)
|
| 71 |
+
|
| 72 |
+
# Generate text
|
| 73 |
+
generated = model.generate(input_ids, max_length=100)
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
### Training
|
| 77 |
+
|
| 78 |
+
```python
|
| 79 |
+
from training import MemoryAwareTrainer, MemoryDataset
|
| 80 |
+
|
| 81 |
+
# Create dataset
|
| 82 |
+
train_data = create_synthetic_dataset(num_samples=1000, seq_len=50)
|
| 83 |
+
train_dataset = MemoryDataset(train_data, memory_sample_ratio=0.2)
|
| 84 |
+
|
| 85 |
+
# Create trainer
|
| 86 |
+
trainer_config = {
|
| 87 |
+
'learning_rate': 1e-4,
|
| 88 |
+
'weight_decay': 0.01,
|
| 89 |
+
'gradient_clip': 1.0,
|
| 90 |
+
'memory_consolidation_interval': 1000,
|
| 91 |
+
'warmup_steps': 1000
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
trainer = MemoryAwareTrainer(model, trainer_config)
|
| 95 |
+
|
| 96 |
+
# Train
|
| 97 |
+
history = trainer.train(train_dataset, num_epochs=10, batch_size=32)
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
## Files
|
| 101 |
+
|
| 102 |
+
- `model_architecture.py`: Core LMCODE model with memory modules
|
| 103 |
+
- `training.py`: Memory-aware trainer with experience replay
|
| 104 |
+
- `utils.py`: Memory analysis, visualization, and monitoring tools
|
| 105 |
+
- `run_demo.py`: Complete demonstration of all features
|
| 106 |
+
- `README.md`: Full documentation
|
| 107 |
+
|
| 108 |
+
## Research Background
|
| 109 |
+
|
| 110 |
+
Inspired by key papers:
|
| 111 |
+
- **LongMem (2023)**: Augmenting LLMs with Long-Term Memory
|
| 112 |
+
- **MemoRAG (2024)**: Dual-system RAG with Global and Local Memory
|
| 113 |
+
- **CAMELoT (2024)**: Training-free Consolidated Associative Memory
|
| 114 |
+
|
| 115 |
+
## License
|
| 116 |
+
|
| 117 |
+
MIT License
|