train-llm-from-scratch: Build and train your own small language model from scratch
Added · Updated
Made by Fareed Khan
View profile on GitHub (opens in a new tab)This is a hands-on tutorial that walks through building a language model entirely from scratch in PyTorch, with no shortcuts through libraries like transformers, trl or peft that normally do the heavy lifting.
It starts from the Transformer architecture in the original "Attention is All You Need" paper and goes all the way through modern post-training steps — supervised fine-tuning, a reward model, and reinforcement learning methods like DPO, PPO and GRPO — so the reader ends with a small model that can hold a conversation, trainable on a single GPU. Every code block is preceded by a plain explanation and followed by the output you should expect.
What it does
The tutorial is organized as one continuous path: prepare training text, build a Transformer from small hand-written pieces (attention heads, the multi-layer perceptron, the full block), pretrain it to predict the next token, generate text from it, then run it through the same post-training pipeline real assistant models go through — supervised fine-tuning, a Bradley-Terry reward model, and DPO/ORPO/KTO or PPO/GRPO reinforcement learning — all implemented from scratch on real public datasets, plus a small web control panel for talking to the finished model.
You could use it to…
- Build and train a small language model from scratch
- Watch pretraining, fine-tuning and RL happen in code you wrote
- Follow every step from raw text to a chatting model
Can I use this?
You'll need Python, PyTorch and one GPU · Setup needed
Worth knowing The models this produces on a single GPU are small (the author shows a 13-million-parameter example) and meant for learning the mechanics, not for matching production chatbots; the material assumes comfort reading and writing Python. MIT licence.
Why it's here
Not trending this week
Last seen on the GitHub trending list on 28 September 2026. It stays on BuildTube so you can still find it.
- 13,168 people have starred it on GitHub.
Numbers checked on 8 November 2026; not refreshed since.
Behind it
View sourceBuildTube has not run or verified this project. Everything above is written from what the creator published.
Made this?
Quick questions
- What is train-llm-from-scratch?
- The tutorial is organized as one continuous path: prepare training text, build a Transformer from small hand-written pieces (attention heads, the multi-layer perceptron, the full block), pretrain it to predict the next token, generate text from it, then run it through the same post-training pipeline real assistant models go through — supervised fine-tuning, a Bradley-Terry reward model, and DPO/ORPO/KTO or PPO/GRPO reinforcement learning — all implemented from scratch on real public datasets, plus a small web control panel for talking to the finished model.
- What can I use train-llm-from-scratch for?
- Build and train a small language model from scratch; Watch pretraining, fine-tuning and RL happen in code you wrote; Follow every step from raw text to a chatting model
- Can I run train-llm-from-scratch on my computer?
- It runs from a terminal on your own computer. The models this produces on a single GPU are small (the author shows a 13-million-parameter example) and meant for learning the mechanics, not for matching production chatbots; the material assumes comfort reading and writing Python. Python, PyTorch and one GPU.
- What do I need to use train-llm-from-scratch?
- Python, PyTorch, and access to at least one GPU; the author notes the whole path can be followed and run end to end.
- What license does train-llm-from-scratch use?
- MIT licence.
More from this week
- kimi-k3-in-c Run a 2.78-trillion-parameter model on 8 GB of RAM
Get text answers from the huge Kimi K3 AI model using one CPU and no graphics card.
- agent-desktop Let AI agents read and click desktop apps
For developers building computer-use agents: agent-desktop lets an AI agent read and operate desktop apps on Mac, Windows or Linux from a command line.
- Auto-Ontology Give AI agents checked business meanings for your database
Help AI agents answer questions about company databases using reviewed definitions and SQL, with each answer showing its steps.
Explore by topic
Post this
Download share imageFor makers
Made this? Show it’s on BuildTube — add the badge to your README or embed the card on your site.
README badge (Markdown)
[](https://buildtube.app/p/gh-fareedkhan-dev-train-llm-from-scratch/)Embed card (HTML)
<iframe src="https://buildtube.app/embed/gh-fareedkhan-dev-train-llm-from-scratch/" width="560" height="240" style="border:0;max-width:100%" loading="lazy" title="train-llm-from-scratch on BuildTube"></iframe>