roneneldan/TinyStories
Viewer • Updated • 2.14M • 94.6k • 1.11k
Mô hình GPT-2 architecture (163M parameters) được train từ đầu (from scratch) bằng PyTorch thuần, trên dataset TinyStories. Đây là sản phẩm của dự án học tập LEARN-LLM.
| Tham số | Giá trị |
|---|---|
| Architecture | GPT-2 (Decoder-only Transformer) |
| Vocab size | 50,257 (GPT-2 tiktoken BPE) |
| Context length | 1,024 tokens |
| Embedding dim | 768 |
| Attention heads | 12 |
| Transformer layers | 12 |
| Feed-forward dim | 3,072 (4×) |
| Total parameters | ~163M |
Note: Không dùng weight-tying giữa
tok_embvàout_headnên 163M thay vì 124M của GPT-2 gốc.
| Chi tiết | Giá trị |
|---|---|
| Dataset | roneneldan/TinyStories |
| Tokenizer | GPT-2 (tiktoken) |
| Optimizer | AdamW |
| Learning rate | 6e-4 (cosine decay + warmup) |
| Warmup steps | 2,000 |
| Effective batch size | 64 (microbatch=6, accum=16) |
| Context length | 1,024 |
| Hardware | Apple M5 Pro (MPS) |
| Checkpoint step | 49,000 |
| File | Mô tả |
|---|---|
best_checkpoint.pth |
Checkpoint với val loss thấp nhất |
last_checkpoint.pth |
Checkpoint cuối cùng (dùng để resume) |
import torch
import tiktoken
import sys
# Clone repo để có model code
# git clone https://github.com/Tung003/LEARN-LLM.git
sys.path.insert(0, "LEARN-LLM/notebooks")
from chapter_3_models.artifacts.gpt_model import GPTModel
from chapter_3_models.artifacts.generate import generate_text_simple
# Download checkpoint từ HF
from huggingface_hub import hf_hub_download
ckpt_path = hf_hub_download(
repo_id="TungChu/gpt2",
filename="best_checkpoint.pth"
)
# Load model
checkpoint = torch.load(ckpt_path, map_location="cpu")
model = GPTModel(checkpoint["model_config"])
model.load_state_dict(checkpoint["model_state_dict"])
model.eval()
# Generate text
tokenizer = tiktoken.get_encoding("gpt2")
prompt = "Once upon a time"
tokens = tokenizer.encode(prompt)
idx = torch.tensor([tokens])
with torch.no_grad():
out = generate_text_simple(model, idx, max_new_tokens=100, context_size=1024)
print(tokenizer.decode(out[0].tolist()))
| Steps | Val Loss | Ghi chú |
|---|---|---|
| 6,000 | 2.35 | Early checkpoint |
| 49,000 | — | Current checkpoint |
MIT License — Tự do sử dụng cho mục đích học tập và nghiên cứu.