Add README
#2
by Compactbot - opened
README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Repetition Diagnostic
|
| 2 |
+
|
| 3 |
+
A builder tool for tiny language model developers. Sweeps `repetition_penalty` values and measures how they affect generation quality.
|
| 4 |
+
|
| 5 |
+
## Why
|
| 6 |
+
|
| 7 |
+
Tiny models (<50M params) are extremely prone to degenerate repetition loops. The default `repetition_penalty=1.0` means **no penalty** — the model can output the same token forever. Most training scripts don't test generation quality, so you discover this at inference time with no idea what to do.
|
| 8 |
+
|
| 9 |
+
This tool gives you a quick diagnostic: sweep the penalty, see where quality peaks, and know what to set in your inference config.
|
| 10 |
+
|
| 11 |
+
## What it measures
|
| 12 |
+
|
| 13 |
+
| Metric | Description |
|
| 14 |
+
|--------|-------------|
|
| 15 |
+
| **Repetition rate** | Fraction of output tokens that already appeared earlier in the sequence |
|
| 16 |
+
| **Unique token ratio** | Unique tokens / total tokens (diversity) |
|
| 17 |
+
| **Max loop length** | Longest repeated n-gram (how deep the loop goes) |
|
| 18 |
+
| **Repeated trigrams** | Count of 3-token sequences appearing 2+ times |
|
| 19 |
+
|
| 20 |
+
## Usage
|
| 21 |
+
|
| 22 |
+
```bash
|
| 23 |
+
# Basic: sweep penalties on a single prompt
|
| 24 |
+
python3 repetition_diagnostic.py --model gpt2 --prompt "Once upon a time"
|
| 25 |
+
|
| 26 |
+
# Multiple prompts
|
| 27 |
+
python3 repetition_diagnostic.py --model gpt2 --prompts "Once upon a time|The cat sat|I want to"
|
| 28 |
+
|
| 29 |
+
# Custom penalty range
|
| 30 |
+
python3 repetition_diagnostic.py --model gpt2 --penalties "1.0,1.05,1.1,1.2,1.5"
|
| 31 |
+
|
| 32 |
+
# Save results as JSON
|
| 33 |
+
python3 repetition_diagnostic.py --model gpt2 --output results.json
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
## Example output
|
| 37 |
+
|
| 38 |
+
```
|
| 39 |
+
Model: gpt2
|
| 40 |
+
Prompts: ['Once upon a time']
|
| 41 |
+
Penalties: [1.0, 1.1, 1.2, 1.5, 2.0]
|
| 42 |
+
|
| 43 |
+
Prompt RP Tokens Repet Unique MaxLoop RepTri
|
| 44 |
+
------------------------------------------------------------------------
|
| 45 |
+
Once upon a time 1.0 48 25.5% 72.9% 3 1
|
| 46 |
+
Once upon a time 1.1 48 0.0% 100.0% 0 0
|
| 47 |
+
Once upon a time 1.2 48 0.0% 100.0% 0 0
|
| 48 |
+
Once upon a time 1.5 48 0.0% 100.0% 0 0
|
| 49 |
+
Once upon a time 2.0 48 0.0% 100.0% 0 0
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
## Interpretation
|
| 53 |
+
|
| 54 |
+
- **rp=1.0 with high repetition rate**: Your model has a repetition problem. Try rp=1.1 or 1.2.
|
| 55 |
+
- **rp=1.1 already fixes it**: Good — your model is mostly fine, just needs a small nudge.
|
| 56 |
+
- **Even rp=2.0 still shows loops**: The model is severely undertrained or has a degenerate mode. No penalty will save it.
|
| 57 |
+
- **rp=1.0 shows no repetition**: Your model is robust. You can use the default.
|
| 58 |
+
|
| 59 |
+
## Requirements
|
| 60 |
+
|
| 61 |
+
- Python 3.8+
|
| 62 |
+
- `torch`
|
| 63 |
+
- `transformers`
|
| 64 |
+
|
| 65 |
+
```bash
|
| 66 |
+
pip install torch transformers
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
## Notes
|
| 70 |
+
|
| 71 |
+
- Uses the same seed for all penalty values so the comparison is fair (same random draws, different penalty).
|
| 72 |
+
- Generation is done manually (not `model.generate()`) so the penalty is applied exactly as specified, without HuggingFace's internal logic.
|
| 73 |
+
- Works with any model that `AutoModelForCausalLM` can load.
|