Upload folder using huggingface_hub
Browse files- README.md +99 -0
- config.json +10 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
license: mit
|
| 4 |
+
tags:
|
| 5 |
+
- gpt
|
| 6 |
+
- pytorch
|
| 7 |
+
- from-scratch
|
| 8 |
+
- course-project
|
| 9 |
+
- decoder-only
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Mini-GPT (mini-gpt2_decoder)
|
| 13 |
+
|
| 14 |
+
A small GPT-style decoder-only transformer, trained from scratch.
|
| 15 |
+
|
| 16 |
+
## Model Details
|
| 17 |
+
|
| 18 |
+
| | |
|
| 19 |
+
|---|---|
|
| 20 |
+
| Architecture | Decoder-only transformer (GPT-2 style, pre-norm) |
|
| 21 |
+
| Parameters | ~30.0M |
|
| 22 |
+
| Layers | 6 |
|
| 23 |
+
| Attention heads | 6 |
|
| 24 |
+
| Embedding dimension | 384 |
|
| 25 |
+
| Context length | 256 |
|
| 26 |
+
| Vocabulary | 50257 tokens (GPT-2 BPE, via `tiktoken`) |
|
| 27 |
+
|
| 28 |
+
## Training Data
|
| 29 |
+
|
| 30 |
+
A ~100MB slice of **OpenWebText**, tokenized with GPT-2's BPE tokenizer.
|
| 31 |
+
|
| 32 |
+
## Project Context
|
| 33 |
+
|
| 34 |
+
This model was trained and optimized across six phases:
|
| 35 |
+
1. FP32 baseline training
|
| 36 |
+
2. Mixed precision (AMP)
|
| 37 |
+
3. Memory engineering (gradient checkpointing + gradient accumulation)
|
| 38 |
+
4. A custom fused attention kernel written in Triton
|
| 39 |
+
5. KV-cached autoregressive generation
|
| 40 |
+
6. INT8 post-training quantization (evaluated separately, CPU-only -- **this
|
| 41 |
+
upload contains the FP32 weights**, see the scope note in `upload_to_hf.py`
|
| 42 |
+
in the project repo for why)
|
| 43 |
+
|
| 44 |
+
## Optimization Results
|
| 45 |
+
|
| 46 |
+
# Mini-GPT Optimization Project -- Final Summary
|
| 47 |
+
|
| 48 |
+
## Phases 1-3: Training Optimizations
|
| 49 |
+
- **baseline**: 1170.6s, peak memory 8208MB, final val loss 5.5229
|
| 50 |
+
- **mixed_precision**: 484.0s, peak memory 8208MB, final val loss 5.5225
|
| 51 |
+
- **mixed_precision**: 483.7s, peak memory 8208MB, final val loss 5.5224
|
| 52 |
+
- **memory_engineering**: 1763.6s, peak memory 8208MB, final val loss 5.0966
|
| 53 |
+
- From `baseline` to `memory_engineering`: 0.66x time change, 1.00x memory change, validation loss moved from 5.5229 to 5.0966 (should stay close -- these are meant to be near-lossless optimizations, not accuracy tradeoffs).
|
| 54 |
+
|
| 55 |
+
## Phase 4: Custom Fused Attention Kernel
|
| 56 |
+
- Average forward-pass speedup across tested sequence lengths: **0.87x** (vanilla SDPA vs. our Triton kernel).
|
| 57 |
+
- Best result: 0.89x at seq_len=64 (18.006ms -> 20.132ms).
|
| 58 |
+
- Scope: forward pass only -- see model/attention.py's docstring for the autograd/backward-pass caveat.
|
| 59 |
+
|
| 60 |
+
## Phase 5: KV-Cache Generation
|
| 61 |
+
- Speedup grew from **1.10x** at 20 tokens to **1.23x** at 200 tokens -- confirming the expected O(n) vs O(n^2) behavior (naive generation's wasted recomputation grows with length; cached generation's doesn't).
|
| 62 |
+
- Verified mathematically exact: token-for-token identical output under greedy decoding, not an approximation.
|
| 63 |
+
|
| 64 |
+
## Phase 6: INT8 Quantization
|
| 65 |
+
- Model size: 114.6MB -> 102.7MB (**1.12x smaller**).
|
| 66 |
+
- CPU latency: 180.14ms -> 165.93ms (**1.09x faster**).
|
| 67 |
+
- Perplexity cost: 163.00 -> 156.69 (**+-6.31**) -- the one deliberate accuracy-for-efficiency trade in the whole project.
|
| 68 |
+
|
| 69 |
+
## Overall Story
|
| 70 |
+
Four of the five speed/memory optimizations (mixed precision, gradient checkpointing, the fused kernel, KV-cache) are effectively free -- same output, faster or leaner. Only quantization (Phase 6) trades a small, explicitly measured amount of accuracy for a large gain in deployability on cheap hardware. That asymmetry -- most optimizations are free, one isn't, and we can say exactly how much it costs -- is the actual finding of this project.
|
| 71 |
+
|
| 72 |
+
## Usage
|
| 73 |
+
|
| 74 |
+
```python
|
| 75 |
+
import json
|
| 76 |
+
import torch
|
| 77 |
+
from safetensors.torch import load_model
|
| 78 |
+
|
| 79 |
+
from model.gpt import GPTConfig, MiniGPT # from this project's repo
|
| 80 |
+
|
| 81 |
+
with open("config.json") as f:
|
| 82 |
+
config_dict = json.load(f)
|
| 83 |
+
config = GPTConfig(**config_dict)
|
| 84 |
+
|
| 85 |
+
model = MiniGPT(config)
|
| 86 |
+
load_model(model, "model.safetensors")
|
| 87 |
+
model.eval()
|
| 88 |
+
|
| 89 |
+
# Naive generation (see generate.py in the project repo for the KV-cached,
|
| 90 |
+
# much faster version used during actual benchmarking)
|
| 91 |
+
idx = torch.zeros((1, 1), dtype=torch.long) # or your own prompt token ids
|
| 92 |
+
out = model.generate(idx, max_new_tokens=50)
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
## Limitations
|
| 96 |
+
|
| 97 |
+
Trained on a relatively small (~100MB) data slice as a course project focused
|
| 98 |
+
on systems optimization, not on maximizing language modeling quality -- expect
|
| 99 |
+
grammatically plausible but not highly coherent output over long spans.
|
config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"vocab_size": 50257,
|
| 3 |
+
"context_length": 256,
|
| 4 |
+
"n_layers": 6,
|
| 5 |
+
"n_heads": 6,
|
| 6 |
+
"n_embd": 384,
|
| 7 |
+
"dropout": 0.1,
|
| 8 |
+
"bias": true,
|
| 9 |
+
"use_fused_kernel": false
|
| 10 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:217c0e6086303c3dd9f0cd9693522a4e39f054795ac824ac67bfa9c7b905a5f4
|
| 3 |
+
size 120185272
|