hemantvirmani commited on
Commit
ef92c33
·
verified ·
1 Parent(s): fa1001f

updating README.md for adding context for model saved in Hf format

Browse files
Files changed (1) hide show
  1. README.md +74 -3
README.md CHANGED
@@ -40,8 +40,8 @@ Built this project to develop hands-on intuition for LLMs - inspired by Andrej K
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) |
@@ -58,6 +58,23 @@ scheduler). The file is ~670MB.
58
 
59
  To load, you need the `TinyGPT` model class (included below).
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  ---
62
 
63
  ## Usage
@@ -105,6 +122,60 @@ for prompt in prompts:
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)
@@ -136,4 +207,4 @@ for prompt in prompts:
136
  ## Thanks to
137
 
138
  - Andrej Karpathy's nanoGPT - Video and Code
139
- - Dataset: HuggingFace [FineWeb-Edu](https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu)
 
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 x 32 gradient accumulation steps) |
45
  | Weight decay | 0.1 |
46
  | Gradient clipping | 1.0 |
47
  | Precision | bfloat16 (bf16) |
 
58
 
59
  To load, you need the `TinyGPT` model class (included below).
60
 
61
+ The model is also available in **Hugging Face Transformers format** in this
62
+ repository. The HF-format files include:
63
+
64
+ - `model.safetensors`
65
+ - `config.json`
66
+ - `generation_config.json`
67
+ - `tokenizer.json`
68
+ - `tokenizer_config.json`
69
+
70
+ The HF-format model can be loaded with `transformers` and is useful for standard
71
+ Hugging Face workflows. Note that TinyGPT was trained with a separate,
72
+ non-weight-tied LM head that includes a trained bias. Standard
73
+ `GPT2LMHeadModel.from_pretrained()` loads the main model weights but treats
74
+ `lm_head.bias` as an unexpected key because the default GPT-2 head is biasless.
75
+ For exact TinyGPT inference, restore the LM-head bias as shown below or use
76
+ `infer_hf.py` from the GitHub repo.
77
+
78
  ---
79
 
80
  ## Usage
 
122
  print(model.generate_text(start_text=prompt, max_tokens=500, temperature=0.7))
123
  ```
124
 
125
+ ### 4. Load the Hugging Face format model
126
+
127
+ ```bash
128
+ pip install torch transformers safetensors huggingface_hub
129
+ ```
130
+
131
+ ```python
132
+ import torch
133
+ from huggingface_hub import hf_hub_download
134
+ from safetensors.torch import load_file
135
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
136
+
137
+ model_id = "hemantvirmani/tinyGPT"
138
+
139
+ tokenizer = GPT2Tokenizer.from_pretrained(model_id)
140
+ model = GPT2LMHeadModel.from_pretrained(model_id)
141
+
142
+ # Restore TinyGPT's trained LM-head bias for exact inference.
143
+ weights_path = hf_hub_download(repo_id=model_id, filename="model.safetensors")
144
+ state_dict = load_file(weights_path, device="cpu")
145
+ if "lm_head.bias" in state_dict:
146
+ lm_head = torch.nn.Linear(model.config.n_embd, model.config.vocab_size, bias=True)
147
+ lm_head.weight = torch.nn.Parameter(state_dict["lm_head.weight"])
148
+ lm_head.bias = torch.nn.Parameter(state_dict["lm_head.bias"])
149
+ model.lm_head = lm_head
150
+
151
+ device = "cuda" if torch.cuda.is_available() else "cpu"
152
+ model = model.to(device)
153
+ model.eval()
154
+
155
+ prompt = "Photosynthesis is the process by which plants"
156
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
157
+
158
+ with torch.no_grad():
159
+ output_ids = model.generate(
160
+ **inputs,
161
+ max_new_tokens=500,
162
+ do_sample=True,
163
+ temperature=0.7,
164
+ top_k=0,
165
+ top_p=1.0,
166
+ repetition_penalty=1.3,
167
+ pad_token_id=tokenizer.eos_token_id,
168
+ )
169
+
170
+ print(tokenizer.decode(output_ids[0], skip_special_tokens=True))
171
+ ```
172
+
173
+ You can also run the helper script from the GitHub repo:
174
+
175
+ ```bash
176
+ python infer_hf.py --model_dir hemantvirmani/tinyGPT --prompt "Photosynthesis is the process by which plants"
177
+ ```
178
+
179
  ---
180
 
181
  ## Sample Outputs (temperature=0.7, 500 tokens)
 
207
  ## Thanks to
208
 
209
  - Andrej Karpathy's nanoGPT - Video and Code
210
+ - Dataset: HuggingFace [FineWeb-Edu](https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu)