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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MEMGPT
|
| 2 |
+
|
| 3 |
+
A GPT-2-style large language model (LLM) repository.This implementation includes full support for distributed training, sharded datasets, benchmark evaluation, and efficient text generation.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## ๐ง Features
|
| 8 |
+
|
| 9 |
+
- Transformer architecture based on GPT-2.
|
| 10 |
+
- Configurable training and model hyperparameters via JSON.
|
| 11 |
+
- Sharded dataset loading from `.npy` files.
|
| 12 |
+
- Mixed-precision training with `torch.autocast`.
|
| 13 |
+
- DDP (DistributedDataParallel) support.
|
| 14 |
+
- Evaluation support with HellaSwag.
|
| 15 |
+
- Modular codebase for easy extensibility.
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## ๐ Project Structure
|
| 20 |
+
|
| 21 |
+
```bash
|
| 22 |
+
MEMGPT/
|
| 23 |
+
โโโ configs/
|
| 24 |
+
โ โโโ config.json # Model and training configuration
|
| 25 |
+
โ
|
| 26 |
+
โโโ data/
|
| 27 |
+
โ โโโ edu_fineweb/ # Sharded training data
|
| 28 |
+
โ โ โโโ train_000001.npy
|
| 29 |
+
โ โ โโโ train_000002.npy
|
| 30 |
+
โ โ โโโ test_000001.npy
|
| 31 |
+
โ โโโ hellaswag/
|
| 32 |
+
โ โ โโโ hellaswag_val.jsonl
|
| 33 |
+
โ โโโ fineweb.py # Dataset sharding/processing logic
|
| 34 |
+
โ
|
| 35 |
+
โโโ model_core/
|
| 36 |
+
โ โโโ __init__.py
|
| 37 |
+
โ โโโ attention.py # Self-attention module
|
| 38 |
+
โ โโโ model.py # GPT2 model architecture
|
| 39 |
+
โ โโโ dataloader.py # DataLoader_1 class
|
| 40 |
+
โ โโโ training.py # train_nanogpt function
|
| 41 |
+
โ
|
| 42 |
+
โโโ scripts/
|
| 43 |
+
โ โโโ train.py # Entry point to start training
|
| 44 |
+
โ โโโ evaluate.py # Run evaluation
|
| 45 |
+
โ โโโ generate.py # Generate text from trained model
|
| 46 |
+
โ
|
| 47 |
+
โโโ evaluation/
|
| 48 |
+
โ โโโ __init__.py
|
| 49 |
+
โ โโโ hellaswag.py # HellaSwag dataset preparation
|
| 50 |
+
โ โโโ val_hellaswag.py # HellaSwag scoring function
|
| 51 |
+
โ
|
| 52 |
+
โโโ logs/
|
| 53 |
+
โ โโโ log.txt # Training log file
|
| 54 |
+
โ โโโ model_xxxxx.pt # Checkpoint files
|
| 55 |
+
โ
|
| 56 |
+
โโโ .gitignore
|
| 57 |
+
โโโ README.md
|
| 58 |
+
โโโ requirements.txt
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
---
|
| 62 |
+
|
| 63 |
+
## โ๏ธ Configuration
|
| 64 |
+
|
| 65 |
+
Edit `configs/config.json` to configure your model and training setup.
|
| 66 |
+
|
| 67 |
+
Example:
|
| 68 |
+
```json
|
| 69 |
+
{
|
| 70 |
+
"model": {
|
| 71 |
+
"block_size": 1024,
|
| 72 |
+
"vocab_size": 50304,
|
| 73 |
+
"n_layer": 12,
|
| 74 |
+
"n_head": 12,
|
| 75 |
+
"n_embd": 768
|
| 76 |
+
},
|
| 77 |
+
"training": {
|
| 78 |
+
"max_steps": 19073,
|
| 79 |
+
"log_dir": "log",
|
| 80 |
+
"total_batch_size": 524288,
|
| 81 |
+
"B": 64,
|
| 82 |
+
"T": 1024,
|
| 83 |
+
"max_lr": 0.0006,
|
| 84 |
+
"min_lr": 0.00006,
|
| 85 |
+
"warmup_steps": 715,
|
| 86 |
+
"weight_decay": 0.1,
|
| 87 |
+
"learning_rate": 0.0006
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
---
|
| 93 |
+
|
| 94 |
+
## ๐ Training
|
| 95 |
+
|
| 96 |
+
To start training the model:
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
python scripts/train.py
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
This script internally loads `train_nanogpt()` from `model_core/training.py` using the config in `configs/config.json`.
|
| 103 |
+
|
| 104 |
+
### Optional: Distributed Training
|
| 105 |
+
|
| 106 |
+
To run training across multiple GPUs using PyTorch DDP:
|
| 107 |
+
|
| 108 |
+
```bash
|
| 109 |
+
torchrun --nproc_per_node=NUM_GPUS scripts/train.py
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
Replace `NUM_GPUS` with the number of GPUs you want to use.
|
| 113 |
+
|
| 114 |
+
---
|
| 115 |
+
|
| 116 |
+
## ๐ Evaluation
|
| 117 |
+
|
| 118 |
+
To evaluate on HellaSwag:
|
| 119 |
+
|
| 120 |
+
```bash
|
| 121 |
+
python scripts/evaluate.py
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
Make sure the `hellaswag_val.jsonl` file is available under `data/hellaswag/`.
|
| 125 |
+
|
| 126 |
+
---
|
| 127 |
+
|
| 128 |
+
## โ๏ธ Text Generation
|
| 129 |
+
|
| 130 |
+
To generate text from a trained model:
|
| 131 |
+
|
| 132 |
+
```bash
|
| 133 |
+
python scripts/generate.py
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
Make sure to adjust the generation script to point to the correct checkpoint under the `logs/` directory.
|
| 137 |
+
|
| 138 |
+
---
|
| 139 |
+
|
| 140 |
+
## ๐งฉ Requirements
|
| 141 |
+
|
| 142 |
+
Install required packages:
|
| 143 |
+
|
| 144 |
+
```bash
|
| 145 |
+
pip install -r requirements.txt
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
---
|
| 149 |
+
|
| 150 |
+
## ๐ Notes
|
| 151 |
+
|
| 152 |
+
- Ensure your `.npy` sharded data is placed under `data/edu_fineweb/`.
|
| 153 |
+
- The log directory and checkpoints will be saved in `logs/`.
|
| 154 |
+
- The `DataLoader_1` handles distributed data loading.
|
| 155 |
+
- Supports `bfloat16` autocasting for better training efficiency.
|
| 156 |
+
|
| 157 |
+
---
|
| 158 |
+
|
| 159 |
+
## ๐ฎ License
|
| 160 |
+
|
| 161 |
+
MIT License. Feel free to modify and build upon this for research or commercial use.
|
| 162 |
+
|
| 163 |
+
---
|
| 164 |
+
|
| 165 |
+
## ๐ Acknowledgements
|
| 166 |
+
|
| 167 |
+
Inspired by Andrej Karpathy's nanoGPT. Special thanks to the Andrej Karpathy Youtube tutorials and open-source AI community.
|
| 168 |
+
|