hemantvirmani commited on
Commit
5750fbb
·
verified ·
1 Parent(s): 9dcc9ad

Update README.md

Browse files

README updated with the description

Files changed (1) hide show
  1. README.md +136 -0
README.md CHANGED
@@ -1,3 +1,139 @@
1
  ---
2
  license: mit
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
  ---
4
+ # TinyGPT — GPT-2 Style LM (~163M) trained on FineWeb-Edu
5
+
6
+ A GPT-2 style decoder-only transformer pretrained from scratch on ~43B tokens
7
+ of the FineWeb-Edu dataset, achieving a validation loss of **2.84**.
8
+
9
+ Built this project to develop hands-on intuition for LLMs - inspired by Andrej Karpathy's nanoGPT
10
+
11
+ ---
12
+
13
+ ## Model Details
14
+
15
+ | Parameter | Value |
16
+ |-----------|-------|
17
+ | Architecture | Decoder-only Transformer (GPT-2 style) |
18
+ | Parameters | ~163M |
19
+ | Layers | 12 |
20
+ | Attention heads | 12 |
21
+ | Embedding dim | 768 |
22
+ | Context length | 1024 tokens |
23
+ | Vocab size | 50,257 |
24
+ | Tokenizer | GPT-2 BPE via `tiktoken` |
25
+ | Attention | Causal self-attention (Flash Attention via `F.scaled_dot_product_attention`) |
26
+ | LM head | Separate linear layer (not weight-tied) |
27
+
28
+ > **Why ~163M and not 124M?** Standard GPT-2 124M ties the LM head weights
29
+ > with the token embedding table, saving ~38M parameters. TinyGPT uses a
30
+ > separate `nn.Linear` head, resulting in ~163M total parameters.
31
+
32
+ ---
33
+
34
+ ## Training Details
35
+
36
+ | Detail | Value |
37
+ |--------|-------|
38
+ | Dataset | [FineWeb-Edu](https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu) (`sample-100BT` subset) |
39
+ | Tokens trained | ~43B |
40
+ | Validation loss | 2.84 |
41
+ | Optimizer | AdamW (betas=(0.9, 0.95), eps=1e-8) |
42
+ | Learning rate | 6e-4 |
43
+ | LR schedule | Linear warmup (4000 steps) → Cosine decay to 6e-5 |
44
+ | Effective batch size | 512 (16 × 32 gradient accumulation steps) |
45
+ | Weight decay | 0.1 |
46
+ | Gradient clipping | 1.0 |
47
+ | Precision | bfloat16 (bf16) |
48
+ | Max iterations | 600,000 |
49
+ | Dropout | 0.0 |
50
+
51
+ ---
52
+
53
+ ## Format
54
+
55
+ Weights are saved in **PyTorch native format** — a plain state dict saved with
56
+ `torch.save()`, containing only model weights (no optimizer state, no
57
+ scheduler). The file is ~670MB.
58
+
59
+ To load, you need the `TinyGPT` model class (included below).
60
+
61
+ ---
62
+
63
+ ## Usage
64
+
65
+ ### 1. Install dependencies
66
+
67
+ Clone the repo and install requirements:
68
+
69
+ ```bash
70
+ git clone https://github.com/hemantvirmani/tinygpt
71
+ cd tinygpt
72
+ pip install -r requirements.txt
73
+ ```
74
+
75
+ ### 2. Get the model class
76
+
77
+ The `TinyGPT` model class is available at:
78
+ **[https://github.com/hemantvirmani/tinygpt](https://github.com/hemantvirmani/tinygpt)**
79
+
80
+ Clone or download `tinygpt.py` and place it in your working directory.
81
+
82
+ ### 3. Load weights and run inference
83
+
84
+ ```python
85
+ import tinygpt
86
+
87
+ model = tinygpt.load_model_for_inference()
88
+
89
+ prompts = [
90
+ "Hello, I'm a language model,",
91
+ "The human brain contains approximately",
92
+ "Photosynthesis is the process by which plants",
93
+ "The theory of relativity states that ",
94
+ "The Roman Empire fell due to several factors including",
95
+ "During the Industrial Revolution, workers ",
96
+ "To solve a quadratic equation, you must first",
97
+ "The key differences between mitosis and meiosis are ",
98
+ "Once upon a time in ancient India, there lived a king who ",
99
+ ]
100
+
101
+ for prompt in prompts:
102
+ print(f"\n{'='*60}")
103
+ print(f"PROMPT: {prompt}")
104
+ print(f"{'='*60}")
105
+ print(model.generate_text(start_text=prompt, max_tokens=500, temperature=0.7))
106
+ ```
107
+
108
+ ---
109
+
110
+ ## Sample Outputs (temperature=0.7, 500 tokens)
111
+
112
+ **Prompt:** `Photosynthesis is the process by which plants`
113
+ > Photosynthesis is the process by which plants take in sunlight, water,
114
+ > carbon dioxide and nutrients to produce energy for their cells. Humans
115
+ > depend on photosynthesis to provide their own energy, but many plants
116
+ > also use the energy of other organisms to produce food. The five types of...
117
+
118
+ **Prompt:** `The Roman Empire fell due to several factors including`
119
+ > The Roman Empire fell due to several factors including the decline of the
120
+ > Roman army, the rise of the Papacy, and the threat of the Islamic invasion.
121
+ > The fall of the Roman Empire was the result of a series of civil wars in
122
+ > the late fourth century, and was led by the first emperor of the Roman
123
+ > Empire, Constantine the Great.
124
+
125
+ ---
126
+
127
+ ## Limitations
128
+
129
+ - This is a **base language model** — it completes text, it does not follow
130
+ instructions or answer questions.
131
+ - Prone to repetition loops, especially at low temperature.
132
+ - Fine-tuning required for instruction-following or domain-specific tasks.
133
+
134
+ ---
135
+
136
+ ## Thanks to
137
+
138
+ - Andrej Karpathy's nanoGPT - Video and Code
139
+ - Dataset: HuggingFace [FineWeb-Edu](https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu)