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.
1import torch
2import tiktoken
3import sys
4
5# Clone repo để có model code
6# git clone https://github.com/Tung003/LEARN-LLM.git
7
8sys.path.insert(0, "LEARN-LLM/notebooks")
9from chapter_3_models.artifacts.gpt_model import GPTModel
10from chapter_3_models.artifacts.generate import generate_text_simple
11
12# Download checkpoint từ HF
13from huggingface_hub import hf_hub_download
14
15ckpt_path = hf_hub_download(
16 repo_id="TungChu/gpt2",
17 filename="best_checkpoint.pth"
18)
19
20# Load model
21checkpoint = torch.load(ckpt_path, map_location="cpu")
22model = GPTModel(checkpoint["model_config"])
23model.load_state_dict(checkpoint["model_state_dict"])
24model.eval()
25
26# Generate text
27tokenizer = tiktoken.get_encoding("gpt2")
28prompt = "Once upon a time"
29tokens = tokenizer.encode(prompt)
30idx = torch.tensor([tokens])
31
32with torch.no_grad():
33 out = generate_text_simple(model, idx, max_new_tokens=100, context_size=1024)
34
35print(tokenizer.decode(out[0].tolist()))
MIT License — Tự do sử dụng cho mục đích học tập và nghiên cứu.