YuvrajSingh9886 commited on
Commit
1172ec2
·
verified ·
1 Parent(s): 8e50a94

Upload 7 files

Browse files
Files changed (8) hide show
  1. .gitattributes +4 -0
  2. README.md +285 -3
  3. loss_curves.jpg +0 -0
  4. loss_curves_dpo.jpg +3 -0
  5. loss_curves_sft.jpg +0 -0
  6. sample.jpg +3 -0
  7. sample_1.jpg +3 -0
  8. sample_2.jpg +3 -0
.gitattributes CHANGED
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ loss_curves_dpo.jpg filter=lfs diff=lfs merge=lfs -text
37
+ sample_1.jpg filter=lfs diff=lfs merge=lfs -text
38
+ sample_2.jpg filter=lfs diff=lfs merge=lfs -text
39
+ sample.jpg filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,3 +1,285 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Introducing SmolLlama - A Smaller Language Model
3
+
4
+ - So, I trained a Llama a 130M architecture I coded from ground up to build a small instruct model, going through the below-mentioned stages from scratch.
5
+ - Trained on FineWeb dataset form HuggingFace consisting of 15 M texts (10BT snapshot) for a total of full 3 epochs
6
+
7
+ The three main stages are:
8
+
9
+ 1) Pretraining
10
+ 2) SFT (Instruction Tuning)
11
+ 3) Reward Tuning for human-like responses (DPO)
12
+
13
+ - Test it out here - [SmolLlama](https://huggingface.co/spaces/YuvrajSingh9886/SmolLlama)
14
+
15
+ - Models have been uploaded on HF!
16
+
17
+ ### 1) Pretraining
18
+
19
+ #### Dataset
20
+
21
+ - I used the [FineWeb](https://huggingface.co/datasets/HuggingFaceFW/fineweb?row=0) dataset from HuggingFace (10BT checkpoint) consisting of roughly 15M texts.
22
+
23
+ 1) Train dataset - 12 M texts
24
+ 2) Val dataset - 3M texts
25
+
26
+
27
+
28
+ ### 2) SFT
29
+
30
+ #### Dataset
31
+
32
+ - I used the [alpaca](https://huggingface.co/datasets/yahma/alpaca-cleaned) dataset from HuggingFace consisting of roughly 52k texts.
33
+
34
+ 1) Train dataset - 45k texts
35
+ 2) Val dataset - 5k texts
36
+
37
+
38
+
39
+ ### 3) Reference Alignment
40
+
41
+ #### Dataset
42
+
43
+ - I used the [Ultrafeedback](https://huggingface.co/datasets/trl-lib/ultrafeedback_binarized) dataset from HuggingFace consisting of roughly 62.1K texts.
44
+
45
+ 1) Train dataset - 56K texts
46
+ 2) Val dataset - 6K texts
47
+
48
+
49
+
50
+ ---
51
+
52
+ #### ModelArgs (Hyperparameters)
53
+
54
+
55
+ | Parameter | Description | Default Value | Type |
56
+ |--------------------------------|-----------------------------------------------------------------------------|-----------------------------------|-----------|
57
+ | `epochs` | Number of training epochs | `4` | `int` |
58
+ | `block_size` | Size of each block (context length) | `512` | `int` |
59
+ | `batch_size` | Batch size for training | `64` | `int` |
60
+ | `inference` | Inference mode (not specified) | `None` | `None` |
61
+ | `embeddings_dims` | Dimensionality of embeddings | `512` | `int` |
62
+ | `attn_dropout` | Dropout rate for attention layers | `0.1` | `float` |
63
+ | `no_of_heads` | Number of attention heads | `8` | `int` |
64
+ | `dropout` | Dropout rate for the model | `0.1` | `float` |
65
+ | `val_epochs` | Number of validation epochs | `2` | `int` |
66
+ | `max_lr` | Maximum learning rate | `6e-4` | `float` |
67
+ | `no_of_decoder_layers` | Number of decoder layers | `8` | `int` |
68
+ | `weight_decay_optim` | Weight decay for the optimizer | `0.1` | `float` |
69
+ | `beta_1` | Beta 1 for Adam optimizer | `0.9` | `float` |
70
+ | `beta_2` | Beta 2 for Adam optimizer | `0.95` | `float` |
71
+ | `clip` | Gradient clipping value | `1.0` | `float` |
72
+ | `device` | Device to run the model (`cuda` or `cpu`) | `'cuda'` | `str` |
73
+ | `no_kv_heads` | Number of key-value heads | `2` | `int` |
74
+ | `vocab_size` | Size of the vocabulary | `50304` | `int` |
75
+ | `eps` | Epsilon value for numerical stability | `1e-5` | `float` |
76
+ | `dtype` | Data type for tensors (`bfloat16` if supported, else `float16`) | `'bfloat16'` or `'float16'` | `str` |
77
+ | `save_checkpoint_dir` | Directory to save model checkpoints | `"checkpoints"` | `str` |
78
+ | `prompt` | Default prompt for inference | `"Once upon a time"` | `str` |
79
+ | `save_checkpoint_iter` | Save checkpoint every N iterations | `50` | `int` |
80
+ | `total_iters` | Total number of training iterations | `10000` | `int` |
81
+ | `eval_iters` | Evaluate model every N iterations | `50` | `int` |
82
+ | `eval_check` | Check evaluation metrics every N iterations | `100` | `int` |
83
+ | `warmup_iters` | Number of warmup iterations for learning rate scheduling | `700` | `int` |
84
+ | `min_lr` | Minimum learning rate (10% of `max_lr`) | `0.1 * max_lr` | `float` |
85
+ | `lr_decay_iters` | Number of iterations for learning rate decay | `10000` | `int` |
86
+ | `total_batch_size` | Total batch size across all devices | `524288` | `int` |
87
+ | `micro_batch_size` | Micro batch size per device | `batch_size` | `int` |
88
+ | `gradient_accumulation_steps` | Gradient accumulation steps | `total_batch_size // (micro_batch_size * (block_size * torch.cuda.device_count()))` | `int` |
89
+ | `no_kv_heads` | Number of key-value heads | `2` | `int` |
90
+ ---
91
+ ### Hardware Setup
92
+
93
+ - Used DPP using Pytorch torchrun consisting of 2x H100s SXM (80GB VRAM each) rented on runpod.io
94
+ - The model is a 1.1GB in size but needs around 1.1 GB of VRAM when loaded in fp32 precision
95
+ ---
96
+
97
+ #### Frameworks:
98
+ **Pytorch**
99
+
100
+ ---
101
+
102
+ #### Epochs/Steps
103
+ - Iterations (train) = 6K (gradient accumulation of 0.5M tokens)
104
+
105
+ - Val iterations = every 50 steps
106
+ ---
107
+
108
+ #### Losses
109
+
110
+ - Result - Pretraining
111
+
112
+ Train loss: 3.77 (stagnated)
113
+ Val Loss: 3.80 (stagnated)
114
+
115
+
116
+ - Result - SFT
117
+
118
+ Train Loss: 1.01
119
+ Val Loss: 1.39
120
+
121
+ - Result - DPO
122
+
123
+ Train Loss: 0.96
124
+ Val Loss: 1.05
125
+
126
+ ---
127
+
128
+ #### Screenshots of the loss curves
129
+
130
+ - Pretrain
131
+
132
+ ![Train Loss Curves](loss_curves.jpg)
133
+
134
+ - SFT
135
+
136
+ ![Train Loss Curves (SFT)](loss_curves_sft.jpg)
137
+
138
+ - DPO
139
+
140
+ ![Train Loss Curves (DPO)](loss_curves_dpo.jpg)
141
+
142
+
143
+
144
+ ---
145
+ #### Output
146
+
147
+ - Sample Ouput
148
+
149
+ ![Sample Ouput 1](sample_1.jpg)
150
+
151
+ ![Sample Ouput 2](sample_2.jpg)
152
+ ---
153
+
154
+ ### Local setup
155
+
156
+
157
+ ### Requirements
158
+
159
+
160
+
161
+ ```python
162
+ git [clone the repo](https://github.com/YuvrajSingh-mist/SmolLlama.git)
163
+ cd SmolLlama
164
+ bash ./install.sh
165
+
166
+ ```
167
+ - A wandb.ai account for plotting graphs for your loss curves
168
+
169
+ - On your terminal run
170
+ ```python
171
+ wandb login
172
+ ```
173
+
174
+ - Enter the api key and follow the instructions and once you are succesfully logged in follow the given steps
175
+
176
+
177
+ - Download the model
178
+
179
+ Can use 'P or F or D' to download the model, stands for pretarined, fine tuned and preference alignment models resp.
180
+ ```python
181
+ python donwload_model_weight.py -D
182
+ ```
183
+
184
+
185
+ ---
186
+
187
+ ### Running
188
+
189
+
190
+ #### Training a model
191
+
192
+ - Kindly hange 'device' to any of your available cuda gpus.
193
+
194
+ To run:
195
+
196
+ ```python
197
+ cd SmolLlama
198
+ ```
199
+
200
+ Prepare the dataset. You can set either "tinystories" or "fw" or "dpo" to True to downalod the corresponding datasets.
201
+
202
+ ```python
203
+
204
+ python data.py --tinystories
205
+
206
+
207
+ ```
208
+
209
+ ```python
210
+ bash ./install.sh
211
+ ```
212
+
213
+ Download the weights of the model
214
+
215
+ ```python
216
+
217
+ python download_model_weight.py -sft/dpo/pretrained
218
+
219
+ ```
220
+
221
+ Train the model
222
+
223
+ Now run the following command (Use 'P' for pretraining/SFT and 'D' for DPO)
224
+
225
+ ```python
226
+ torchrun --standalone --nproc_per_node=gpu trainer.py \
227
+ --train P \
228
+ --epochs 4 \
229
+ --beta 0.1 \
230
+ --block_size 256 \
231
+ --batch_size 128 \
232
+ --embeddings_dims 512 \
233
+ --attn_dropout 0.1 \
234
+ --no_of_heads 8 \
235
+ --dropout 0.1 \
236
+ --val_epochs 2 \
237
+ --max_lr 6e-4 \
238
+ --no_of_decoder_layers 16 \
239
+ --weight_decay_optim 0.1 \
240
+ --beta_1 0.9 \
241
+ --beta_2 0.95 \
242
+ --clip 1.0 \
243
+ --device cuda \
244
+ --no_kv_heads 2 \
245
+ --vocab_size 50304 \
246
+ --eps 1e-5 \
247
+ --dtype "bfloat16" \
248
+ --save_checkpoint_dir "checkpoints" \
249
+ --prompt "Once upon a time" \
250
+ --save_checkpoint_iter 50 \
251
+ --total_iters 20000 \
252
+ --eval_iters 50 \
253
+ --eval_check 100 \
254
+ --warmup_iters 700 \
255
+ --min_lr 6e-5 \
256
+ --lr_decay_iters 20000 \
257
+ --total_batch_size 524288 \
258
+ --micro_batch_size 128 \
259
+ --gradient_accumulation_steps 4096
260
+ --max_length 100 \
261
+ --temperature 0.8
262
+ ```
263
+
264
+
265
+ #### Inference on a model
266
+
267
+ - First method:
268
+
269
+ ```python
270
+ python inference_sft.py --prompt "Follow the given instruction carefully. What was the Civil Rights Movement?" --max_length 256 --temperature 0.8
271
+ ```
272
+
273
+ - Second method (through Huggingface):
274
+
275
+ <!-- python download_model_weight.py -sft/dpo/pretrained -->
276
+ ```python
277
+
278
+ cd gradio/
279
+
280
+ pip install gradio
281
+
282
+ python app.py
283
+
284
+ ```
285
+
loss_curves.jpg ADDED
loss_curves_dpo.jpg ADDED

Git LFS Details

  • SHA256: 0fc830a28278761a58a7409362153037a8db944a5614459f39aae2b83fd6c1a6
  • Pointer size: 131 Bytes
  • Size of remote file: 101 kB
loss_curves_sft.jpg ADDED
sample.jpg ADDED

Git LFS Details

  • SHA256: a0e6d965d844c82711ab8b6a1ca8b0d951c6959e1156fc9d0ac8e02598f7f1f2
  • Pointer size: 131 Bytes
  • Size of remote file: 129 kB
sample_1.jpg ADDED

Git LFS Details

  • SHA256: 534554e782ad54029e075764dbbb214d5d86fd3baa2fb9483d765637b06d221d
  • Pointer size: 131 Bytes
  • Size of remote file: 154 kB
sample_2.jpg ADDED

Git LFS Details

  • SHA256: ca725acff073e77c43acbf29511ae9c5edb40f2638221c48ec2c59566d768fc6
  • Pointer size: 131 Bytes
  • Size of remote file: 170 kB