Update README.md
Browse files
README.md
CHANGED
|
@@ -1,36 +1,103 @@
|
|
| 1 |
---
|
| 2 |
language: en
|
| 3 |
-
license:
|
|
|
|
| 4 |
tags:
|
| 5 |
-
- gpt2
|
| 6 |
-
- instruction-tuning
|
| 7 |
-
- text-generation
|
| 8 |
-
-
|
| 9 |
-
- pytorch
|
| 10 |
-
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
# GPT-2 Medium Instruct
|
| 15 |
|
| 16 |
-
**
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
## Model Details
|
| 21 |
|
| 22 |
-
|
| 23 |
-
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
##
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
```python
|
| 36 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
|
@@ -39,101 +106,155 @@ import torch
|
|
| 39 |
model_id = "snehangshu511/gpt2-medium-instruct"
|
| 40 |
|
| 41 |
tokenizer = GPT2Tokenizer.from_pretrained(model_id)
|
| 42 |
-
model
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 56 |
-
|
| 57 |
-
outputs = model.generate(
|
| 58 |
-
**inputs,
|
| 59 |
-
max_new_tokens=150,
|
| 60 |
-
temperature=0.7,
|
| 61 |
-
top_p=0.9,
|
| 62 |
-
top_k=50,
|
| 63 |
-
repetition_penalty=1.2,
|
| 64 |
-
do_sample=True,
|
| 65 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 66 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 67 |
)
|
| 68 |
-
|
| 69 |
-
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 70 |
```
|
| 71 |
|
| 72 |
-
##
|
| 73 |
|
| 74 |
-
|
| 75 |
-
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
- Val Loss (eval): 1.413
|
| 82 |
-
- Val Loss (step): 1.561
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
- Instruction following tasks
|
| 88 |
-
- Question answering
|
| 89 |
-
- Creative writing assistance
|
| 90 |
-
- Educational demonstrations
|
| 91 |
-
- Research on instruction tuning of smaller language models
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
- May generate factually incorrect or biased content
|
| 99 |
-
- Primarily trained on English data
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
|
| 105 |
-
##
|
| 106 |
|
| 107 |
-
**Input:**
|
| 108 |
```
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
```
|
| 113 |
|
| 114 |
-
|
| 115 |
|
| 116 |
## Citation
|
| 117 |
|
|
|
|
|
|
|
| 118 |
```bibtex
|
| 119 |
-
@
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
year
|
| 123 |
-
publisher = {
|
| 124 |
-
howpublished = {\url{https://huggingface.co/snehangshu511/gpt2-medium-instruct}}
|
| 125 |
}
|
| 126 |
-
```
|
| 127 |
-
|
| 128 |
-
## Acknowledgements
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
---
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
Feel free to use this model for research, learning, and experimentation!
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
language: en
|
| 3 |
+
license: mit
|
| 4 |
+
base_model: openai-community/gpt2-medium
|
| 5 |
tags:
|
| 6 |
+
- gpt2
|
| 7 |
+
- instruction-tuning
|
| 8 |
+
- text-generation
|
| 9 |
+
- alpaca
|
| 10 |
+
- pytorch-lightning
|
| 11 |
+
- causal-lm
|
| 12 |
+
datasets:
|
| 13 |
+
- yahma/alpaca-cleaned
|
| 14 |
+
pipeline_tag: text-generation
|
| 15 |
+
model_name: gpt2-medium-instruct
|
| 16 |
---
|
| 17 |
|
| 18 |
# GPT-2 Medium Instruct
|
| 19 |
|
| 20 |
+
A **355M parameter GPT-2 Medium** model fine-tuned from scratch on the `yahma/alpaca-cleaned` instruction dataset. Built as a learning project following Sebastian Raschka's *"Build a Large Language Model from Scratch"* book, with a full custom training pipeline in PyTorch Lightning.
|
| 21 |
|
| 22 |
+
---
|
| 23 |
|
| 24 |
## Model Details
|
| 25 |
|
| 26 |
+
| Property | Value |
|
| 27 |
+
|---|---|
|
| 28 |
+
| **Base model** | `openai-community/gpt2-medium` |
|
| 29 |
+
| **Parameters** | ~355M |
|
| 30 |
+
| **Architecture** | GPT-2 (decoder-only transformer) |
|
| 31 |
+
| **Fine-tuning dataset** | `yahma/alpaca-cleaned` (10,000 training samples) |
|
| 32 |
+
| **Context length** | 1,024 tokens |
|
| 33 |
+
| **Vocabulary size** | 50,257 tokens |
|
| 34 |
+
| **Embedding dim** | 1,024 |
|
| 35 |
+
| **Transformer layers** | 24 |
|
| 36 |
+
| **Attention heads** | 16 |
|
| 37 |
+
| **Tokenizer** | GPT-2 BPE (via `tiktoken` / HF `GPT2Tokenizer`) |
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## Training Details
|
| 42 |
+
|
| 43 |
+
### Dataset
|
| 44 |
+
|
| 45 |
+
The model was fine-tuned on the [`yahma/alpaca-cleaned`](https://huggingface.co/datasets/yahma/alpaca-cleaned) dataset β a cleaned version of Stanford Alpaca's 52K instruction-following data generated from `text-davinci-003`.
|
| 46 |
+
|
| 47 |
+
| Split | Samples |
|
| 48 |
+
|---|---|
|
| 49 |
+
| Train | 10,000 |
|
| 50 |
+
| Validation | 1,000 |
|
| 51 |
+
| Test | 1,000 |
|
| 52 |
+
|
| 53 |
+
### Prompt Format
|
| 54 |
+
|
| 55 |
+
The model uses the standard **Alpaca prompt template**:
|
| 56 |
+
|
| 57 |
+
```
|
| 58 |
+
Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
| 59 |
+
|
| 60 |
+
### Instruction:
|
| 61 |
+
{instruction}
|
| 62 |
+
|
| 63 |
+
### Input:
|
| 64 |
+
{input} β omitted if empty
|
| 65 |
+
|
| 66 |
+
### Response:
|
| 67 |
+
{output}
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
During training, the **instruction + input portion is masked** with `-100` in the targets so the loss is only computed on the response tokens. This is the standard technique to make the model learn *how to respond* rather than memorize the prompt structure.
|
| 71 |
+
|
| 72 |
+
### Optimizer
|
| 73 |
+
|
| 74 |
+
| Hyperparameter | Value |
|
| 75 |
+
|---|---|
|
| 76 |
+
| Optimizer | AdamW |
|
| 77 |
+
| Learning rate | `3e-5` |
|
| 78 |
+
| Weight decay | `0.1` |
|
| 79 |
+
| Beta1 / Beta2 | `0.9` / `0.95` |
|
| 80 |
+
| Gradient clip | `1.0` |
|
| 81 |
|
| 82 |
+
### Training Config
|
| 83 |
|
| 84 |
+
| Setting | Value |
|
| 85 |
+
|---|---|
|
| 86 |
+
| Framework | PyTorch Lightning |
|
| 87 |
+
| Epochs | 2 (+ 1 continuation epoch) |
|
| 88 |
+
| Batch size (per device) | 2 |
|
| 89 |
+
| Gradient accumulation steps | 4 |
|
| 90 |
+
| Effective batch size | 8 |
|
| 91 |
+
| Precision | `16-mixed` (FP16 + FP32) |
|
| 92 |
+
| Hardware | Single GPU (Colab) |
|
| 93 |
+
| Early stopping patience | 3 validation checks |
|
| 94 |
+
| Checkpoint metric | `val_loss_eval` (minimize) |
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
## Usage
|
| 99 |
+
|
| 100 |
+
### Basic Inference
|
| 101 |
|
| 102 |
```python
|
| 103 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
|
|
|
| 106 |
model_id = "snehangshu511/gpt2-medium-instruct"
|
| 107 |
|
| 108 |
tokenizer = GPT2Tokenizer.from_pretrained(model_id)
|
| 109 |
+
model = GPT2LMHeadModel.from_pretrained(model_id)
|
| 110 |
+
model.eval()
|
| 111 |
+
|
| 112 |
+
def build_prompt(instruction, input_text=""):
|
| 113 |
+
base = (
|
| 114 |
+
"Below is an instruction that describes a task. "
|
| 115 |
+
"Write a response that appropriately completes the request.\n\n"
|
| 116 |
+
)
|
| 117 |
+
if input_text.strip():
|
| 118 |
+
return f"{base}### Instruction:\n{instruction}\n\n### Input:\n{input_text}\n\n### Response:\n"
|
| 119 |
+
return f"{base}### Instruction:\n{instruction}\n\n### Response:\n"
|
| 120 |
+
|
| 121 |
+
prompt = build_prompt("Explain what machine learning is in simple terms.")
|
| 122 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 123 |
+
|
| 124 |
+
with torch.no_grad():
|
| 125 |
+
output_ids = model.generate(
|
| 126 |
+
**inputs,
|
| 127 |
+
max_new_tokens=200,
|
| 128 |
+
do_sample=True,
|
| 129 |
+
temperature=0.7,
|
| 130 |
+
top_p=0.9,
|
| 131 |
+
top_k=50,
|
| 132 |
+
repetition_penalty=1.2,
|
| 133 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 134 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Decode only the newly generated tokens (strip the prompt)
|
| 138 |
+
input_len = inputs["input_ids"].shape[1]
|
| 139 |
+
response = tokenizer.decode(output_ids[0][input_len:], skip_special_tokens=True)
|
| 140 |
+
print(response)
|
| 141 |
+
```
|
| 142 |
|
| 143 |
+
### With Optional Input Context
|
| 144 |
|
| 145 |
+
```python
|
| 146 |
+
prompt = build_prompt(
|
| 147 |
+
instruction="Summarize the following text.",
|
| 148 |
+
input_text="The Industrial Revolution began in Britain in the 18th century..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
)
|
|
|
|
|
|
|
| 150 |
```
|
| 151 |
|
| 152 |
+
### Recommended Generation Settings
|
| 153 |
|
| 154 |
+
| Setting | Recommended range | Effect |
|
| 155 |
+
|---|---|---|
|
| 156 |
+
| `temperature` | 0.6 β 0.9 | Higher = more creative, lower = more deterministic |
|
| 157 |
+
| `top_p` | 0.85 β 0.95 | Nucleus sampling β limits token pool to top P% probability mass |
|
| 158 |
+
| `top_k` | 40 β 60 | Hard limits candidate tokens to top K at each step |
|
| 159 |
+
| `repetition_penalty` | 1.1 β 1.3 | Higher = less repetition in output |
|
| 160 |
+
| `max_new_tokens` | 100 β 300 | Keep under 800 to stay within the 1024 context window |
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
---
|
| 163 |
|
| 164 |
+
## Architecture Notes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
+
This model was built **from scratch** using a custom `GPTModel` class (no `AutoModel` during training). The weights were converted from the custom format to HF-compatible `GPT2LMHeadModel` format for this Hub upload.
|
| 167 |
+
|
| 168 |
+
Key architectural decisions:
|
| 169 |
+
|
| 170 |
+
- **Weight tying disabled** (`tie_word_embeddings=False`): In standard GPT-2, the output head shares weights with the embedding layer. During conversion, `lm_head.weight` was explicitly cloned to avoid shared-memory issues with `safetensors`. The config reflects this.
|
| 171 |
+
|
| 172 |
+
- **QKV separation**: The custom training model stores Q, K, V as separate linear layers. During HF conversion, they are re-fused into the standard `c_attn` format that `GPT2LMHeadModel` expects.
|
| 173 |
+
|
| 174 |
+
- **Drop rate = 0.0**: Dropout is disabled during fine-tuning, which is standard practice when working with pretrained models on relatively small datasets.
|
| 175 |
+
|
| 176 |
+
---
|
| 177 |
+
|
| 178 |
+
## Files in This Repository
|
| 179 |
+
|
| 180 |
+
| File | Description |
|
| 181 |
+
|---|---|
|
| 182 |
+
| `model.safetensors` | Model weights in safetensors format (recommended) |
|
| 183 |
+
| `pytorch_model.bin` | Model weights in legacy `.bin` format |
|
| 184 |
+
| `config.json` | GPT2Config β model architecture definition |
|
| 185 |
+
| `generation_config.json` | Default generation settings |
|
| 186 |
+
| `tokenizer.json` | Fast tokenizer file |
|
| 187 |
+
| `tokenizer_config.json` | Tokenizer configuration |
|
| 188 |
+
| `vocab.json` | BPE vocabulary |
|
| 189 |
+
| `merges.txt` | BPE merge rules |
|
| 190 |
+
| `checkpoints/model.ckpt` | Original PyTorch Lightning training checkpoint |
|
| 191 |
|
| 192 |
+
---
|
| 193 |
+
|
| 194 |
+
## Limitations
|
|
|
|
|
|
|
| 195 |
|
| 196 |
+
- **Small training subset**: Only 10,000 of the available ~52,000 Alpaca samples were used. A full dataset run would likely yield noticeably better results.
|
| 197 |
+
- **GPT-2 base**: GPT-2 Medium, while a solid model, is much smaller than modern instruction-tuned LLMs. Responses can be inconsistent or drift from the prompt on complex tasks.
|
| 198 |
+
- **No RLHF**: The model is instruction-tuned via supervised fine-tuning only β no reinforcement learning from human feedback. It may produce responses that are grammatically correct but factually wrong.
|
| 199 |
+
- **Context length**: Hard-limited to 1,024 tokens. Long prompts can get truncated.
|
| 200 |
+
- **No safety alignment**: There is no safety filtering or RLHF alignment. Do not deploy in production without additional safety measures.
|
| 201 |
|
| 202 |
+
---
|
| 203 |
|
| 204 |
+
## Training Pipeline Summary
|
| 205 |
|
|
|
|
| 206 |
```
|
| 207 |
+
yahma/alpaca-cleaned (52K rows)
|
| 208 |
+
β load 10K rows
|
| 209 |
+
Alpaca prompt formatting
|
| 210 |
+
β
|
| 211 |
+
tiktoken BPE tokenization
|
| 212 |
+
β -100 masking on prompt tokens
|
| 213 |
+
Custom PyTorch Dataset + DataLoader (dynamic padding)
|
| 214 |
+
β
|
| 215 |
+
GPT-2 Medium pretrained weights loaded from openai-community/gpt2-medium
|
| 216 |
+
β
|
| 217 |
+
PyTorch Lightning fine-tuning
|
| 218 |
+
- AdamW, lr=3e-5, 2 epochs
|
| 219 |
+
- FP16 mixed precision
|
| 220 |
+
- Gradient accumulation (eff. batch = 8)
|
| 221 |
+
- Checkpoint on best val_loss
|
| 222 |
+
β
|
| 223 |
+
Lightning prefix stripped β raw GPTModel state dict
|
| 224 |
+
β
|
| 225 |
+
Custom β HF format conversion (QKV fusing, key renaming)
|
| 226 |
+
β
|
| 227 |
+
Saved as model.safetensors + pytorch_model.bin
|
| 228 |
+
β
|
| 229 |
+
Pushed to snehangshu511/gpt2-medium-instruct
|
| 230 |
```
|
| 231 |
|
| 232 |
+
---
|
| 233 |
|
| 234 |
## Citation
|
| 235 |
|
| 236 |
+
If you use this model, please also cite the resources it was built from:
|
| 237 |
+
|
| 238 |
```bibtex
|
| 239 |
+
@book{raschka2024llms,
|
| 240 |
+
title = {Build a Large Language Model (From Scratch)},
|
| 241 |
+
author = {Sebastian Raschka},
|
| 242 |
+
year = {2024},
|
| 243 |
+
publisher = {Manning Publications}
|
|
|
|
| 244 |
}
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
+
@misc{alpaca,
|
| 247 |
+
title = {Stanford Alpaca: An Instruction-following LLaMA model},
|
| 248 |
+
author = {Taori et al.},
|
| 249 |
+
year = {2023},
|
| 250 |
+
url = {https://github.com/tatsu-lab/stanford_alpaca}
|
| 251 |
+
}
|
| 252 |
+
```
|
| 253 |
|
| 254 |
---
|
| 255 |
|
| 256 |
+
## Author
|
|
|
|
|
|
|
| 257 |
|
| 258 |
+
**Snehangshu Bhuin** β Data Scientist
|
| 259 |
+
GitHub: [snehangshu2002](https://github.com/snehangshu2002)
|
| 260 |
+
Built as part of ongoing LLM learning and portfolio development.
|