File size: 5,369 Bytes
a4d5b05 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# Training Guide
## Prerequisites
- Python 3.10+
- PyTorch 2.0+ with CUDA
- 6GB+ GPU memory (for batch_size=4)
- ~200MB disk space (enwik8 dataset)
## Quick Start
### 1. Clone & Install
```bash
git clone https://github.com/inkbytefo/agi-former.git
cd agi-former
pip install -r requirements.txt
```
### 2. Run Training
```bash
python train.py
```
**Expected Output:**
```
Step 10: Loss = 2.8451 | BPC = 4.1056 | LR = 3.00e-05
Step 20: Loss = 2.5123 | BPC = 3.6246 | LR = 6.00e-05
...
Step 5000: Loss = 1.3988 | BPC = 2.0181 | LR = 3.00e-04
-- VALIDATION: Loss = 1.5650 | BPC = 2.2578 --
Saved best_model.pth
```
**Training Time:** ~15 minutes (T4 GPU, 5000 steps)
---
## Configuration
Edit hyperparameters in `train.py`:
```python
# Model
D_MODEL = 512
N_LAYERS = 6
NUM_HEADS = 8
PATCH_SIZE = 4
WINDOW_SIZE = 128
THINKING_STEPS = 3 # System 2 iterations
# Training
BATCH_SIZE = 4
MAX_STEPS = 5000
BASE_LR = 3e-4
WARMUP_STEPS = 100
GRAD_CLIP = 0.5
```
### Hyperparameter Guide
#### Model Size
- **Small:** `d_model=256, n_layers=4` → Fast, lower quality
- **Medium:** `d_model=512, n_layers=6` → **Default** (balanced)
- **Large:** `d_model=768, n_layers=8` → Better BPC, slower
#### System 2
- `thinking_steps=0` → Disable (baseline)
- `thinking_steps=3` → **Default** (active reasoning)
- `thinking_steps=5+` → More refinement, higher compute
---
## Dataset
### Enwik8
**Source:** First 100MB of English Wikipedia XML
**Size:** 100,000,000 bytes
**Split:**
- Train: 90MB
- Validation: 5MB
- Test: 5MB
**Auto-download:** Dataset downloads automatically on first run to `./data/enwik8`.
### Custom Data
To train on your own data:
```python
# In train.py, replace:
from src.data.real_data import get_enwik8_dataloader
# With your custom loader:
def get_custom_dataloader(batch_size, seq_len):
# Your implementation
# Must return: (batch, seq_len) tensors of bytes (0-255)
pass
```
---
## Training Process
### 1. Initialization
```
[*] Creating AGIFORMER model...
- d_model=512, n_layers=6, thinking_steps=3
[*] Parameters: ~50M
[*] Downloading enwik8... (if first run)
```
### 2. Warmup Phase (Steps 0-100)
```
Step 10: Loss = 2.8451 | BPC = 4.1056 | LR = 3.00e-05
```
- Linear LR ramp: `0 → 3e-4`
- High loss expected (model random)
### 3. Learning Phase (Steps 100-5000)
```
Step 1000: Loss = 1.9234 | BPC = 2.7745 | LR = 3.00e-04
Step 2000: Loss = 1.7123 | BPC = 2.4701 | LR = 3.00e-04
Step 3000: Loss = 1.6234 | BPC = 2.3418 | LR = 3.00e-04
```
- Loss decreases steadily
- Validation every 200 steps
### 4. Checkpointing
```
-- VALIDATION: Loss = 1.5650 | BPC = 2.2578 --
Saved best_model.pth
```
- `best_model.pth` → Lowest validation loss
- `last_model.pth` → Final checkpoint
---
## Monitoring
### Metrics
**Loss:** Cross-entropy (lower is better)
```
Loss = -log P(next_byte | context)
```
**BPC (Bits Per Character):**
```
BPC = Loss / ln(2)
```
- Random baseline: 8.0 BPC
- Character-level models: 1.2-1.5 BPC
- AGIFORMER (5k steps): 2.26 BPC
### Expected Progress
| Steps | BPC | Status |
|-------|-----|--------|
| 0-100 | 4.0-3.5 | Warmup |
| 500 | 3.0-2.8 | Learning syntax |
| 1000 | 2.8-2.6 | Basic patterns |
| 3000 | 2.5-2.3 | Word structure |
| 5000 | 2.3-2.2 | ✅ Proof of concept |
| 20k+ | <2.0 | Production quality |
---
## Troubleshooting
### NaN Loss
**Symptoms:**
```
Step 150: Loss = nan | BPC = nan
```
**Causes:**
1. Learning rate too high
2. Gradient explosion
3. Numerical instability in attention
**Solutions:**
- ✅ Already fixed in code (stability patches)
- If persists: Lower `BASE_LR` to `1e-4`
- Increase `GRAD_CLIP` to `1.0`
### Out of Memory
**Error:**
```
CUDA out of memory
```
**Solutions:**
- Reduce `BATCH_SIZE` (4 → 2 → 1)
- Reduce `d_model` (512 → 256)
- Reduce `n_layers` (6 → 4)
### Slow Training
**<100 steps/min:**
**Solutions:**
- Use GPU (not CPU): `DEVICE = 'cuda'`
- Enable mixed precision: `torch.cuda.amp.autocast()`
- Reduce `thinking_steps` (3 → 1)
---
## Advanced: Multi-GPU
For distributed training:
```python
# In train.py
import torch.distributed as dist
# Wrap model
model = torch.nn.parallel.DistributedDataParallel(model)
# Launch
torchrun --nproc_per_node=4 train.py
```
**Expected Speedup:** ~3.5× on 4 GPUs
---
## Resuming Training
To continue from checkpoint:
```python
# In train.py, after model creation:
if os.path.exists("last_model.pth"):
model.load_state_dict(torch.load("last_model.pth"))
print("Resumed from checkpoint")
```
---
## Hyperparameter Tuning
### Learning Rate
- **Too High (>5e-4):** Loss spikes, NaN
- **Too Low (<1e-5):** Slow convergence
- **Sweet Spot:** `3e-4` with warmup
### Gradient Clipping
- **Too Aggressive (<0.1):** Slow learning
- **Too Loose (>2.0):** Instability
- **Default:** `0.5`
### System 2 Steps
- `0`: Baseline (no thinking)
- `1-3`: **Recommended** (active reasoning)
- `5+`: Diminishing returns (expensive)
---
## Export to Hugging Face
```bash
python upload_to_hf.py --repo YOUR_USERNAME/agiformer --token YOUR_HF_TOKEN
```
Uploads:
- `best_model.pth`
- Source code (`src/`)
- Documentation
---
## Next Steps
After training:
1. **Test Generation:** `python generate.py`
2. **Inspect System 2:** `python inspect_reasoning.py`
3. **Extend Training:** Increase `MAX_STEPS` to 20k+
4. **Fine-tune:** Change dataset to your domain
|