I trained a 1B-parameter LLM from scratch on 20B tokens for about $200
Mirrored from r/LocalLLaMA for archival readability. Support the source by reading on the original site.
| A few months ago, I had the idea of making a LLM from scratch as a personal project (for learning and partly for improving my resume). Since I learned a lot from other posts on here over the past year, I wanted to share the results. TLDR: I trained a 1.1B param model on 20B tokens from fineweb-edu, then finetuned it on openhermes with LoRA to get a chat model. Total cost was about $200 (in February/March though, so it would probably be more expensive now).
The architecture is based on Gemma3 since it was my most used model when I started. There are a few differences: - I have a smaller context length (4096) and because of that I didn't use sliding window attention. - I have a smaller vocabulary (32k, trained the tokenizer with sentencepiece) - I also tweaked some hyperparameters to reach my target parameter count. For the data, I used fineweb-edu for training the tokenizer and pretraining the model. Then LoRA finetuned the model on openhermes. I purposely tried to find data from 2023 and earlier because I saw this post back then and thought it would be cool to test the model by asking it questions about the "future" (like I did in the gallery images). As far as the training goes: PretrainingFor pretraining, I first did training runs on 2B tokens to test the architecture at 3 sizes: 185M, 500M and 1.1B. Then I did a final run of the 1.1B model on 20B training tokens. I did it on vast.ai and here's the summary:
Also I logged in wandb generations from a few fixed prompts every 30M training tokens or so (was probably the most fun part of the project to check the new samples every couple hours to see the improvements) Here are a few examples for the final 1B model.
At 30M tokens seen At 20B tokens seen
At 30M tokens seen At 20B tokens seen Lora finetuningTo get a chat model, I ran some Lora finetuning on the best 1B model, using Openhermes as a dataset. I also did it on vast.ai, but on a 3060 and over 52 hours. Reached a final validation perplexity of 2.71 (not that it means anything since it is not on the same dataset as the previous values) Again I did have some regular logging of sample prompts.
At 3M tokens seen
At 3M tokens seen
```text A frog's heart beating In the dark and damp wood A frog's voice, so soft No one can hear. It's a call, a croak, a chorus Of frogs in the night's air The land, the air, the water A place where frogs thrive. ```
At 3M tokens seen Overall, over training that the model became more and more concise, especially compared to the base that was very yappy. Still, the quality is not very good for the total price. (when comparing to nanochat for example). When I have some more time, I will probably experiment with some full sft instead of LoRA, and maybe some extended datasets. Side-questsThe post is already pretty long so I will just list quickly some of the other things I tried out: - Because my version had some differences with the original Gemma3 and also because I wanted to understand a bit better how it works, I added the architecture in a fork of llama.cpp. - To test it out, I vibecoded a WearOS app I used to run a Q2_K GGUF version of the 1B model (runs at about 2tok/s on my watch) - I ran a few benchmarks with lm-eval, nothing really interesting to note, it is weaker than Gemma3 1B across the board. - I deployed a demo website on GCP (deploying the model on CPU with the GGUFs) to analyze logprobs of the base model (and compare it with a few other small models) and chat with the instruction-tuned model. I don't know much about frontend so the React was completely vibecoded. ConclusionEven if the model is not that good, I learned a lot while doing it and I can only recommend to anyone who wants to better understand LLMs. It has also helped me in my job search process over the past 4 months (whether for getting more interviews or for doing better in ML technical interviews) Let me know if you have any feedback testing the model or any question! [link] [comments] |
Discussion (0)
Sign in to join the discussion. Free account, 30 seconds — email code or GitHub.
Sign in →No comments yet. Sign in and be the first to say something.