train-llm-from-scratch
A step-by-step tutorial for training your own transformer language model with PyTorch, from raw text to text generation and alignment. Aimed at learners who want to understand how LLMs are built.
Pick a small issue you can reproduce, then fix it with your AI coding agent.
Share on XLicense: MIT
Overview
This is a step-by-step tutorial and set of scripts for training your own transformer language model in PyTorch, based on the Attention Is All You Need paper. It walks from raw text and tokens to a base model, then supervised fine-tuning, a reward model, PPO, DPO and GRPO, ending with evaluation and chat. The algorithms are written by hand in plain PyTorch without trl, peft or transformers. The author says a million or billion parameter model can be trained on a single GPU.
Key features
- Transformer implemented from scratch in plain PyTorch
- Path from raw text to a pretrained base model
- Covers SFT, reward model, PPO, DPO and GRPO
- No trl, peft or transformers dependency
- Ends with evaluation and chat
Best for
Learners who want to understand how language models are built and aligned by reading and running every step themselves. It is a teaching project, and the sample output shown is from a small 13 million parameter model.
- Upstream
- FareedKhan-dev/train-llm-from-scratch
- Fork on GitHub
- Guo-astro/train-llm-from-scratch
- Upstream stars
- 13k
- Category
- AI agents and LLM tools
- Language
- Python
- License
- MIT
- Forked
- 2026-09-25
- Sync status
- In syncLast synced 2026-11-08
More in AI agents and LLM tools
/last30days is an AI-agent skill for researching recent discussions across social platforms, video, news, markets, and the web. It brings scattered signals together in a synthesized brief for trend research and preparation.
Forked 2026-10-22Last synced 2026-11-08License: MITAI agents and LLM toolsGitHub
A portable C99 inference engine for running Kimi K3 on a CPU without a GPU or machine-learning framework. It streams the large checkpoint from disk to support inference with limited RAM, for batch text generation or text chat.
Forked 2026-10-21Last synced 2026-11-08License: Apache-2.0AI agents and LLM toolsGitHub
LayerFS gives AI agents writable workspaces over shared history, so they can work in parallel without copying the base. Use it to checkpoint, branch, rewind, and merge agent changes.
Forked 2026-10-21Last synced 2026-11-08License: MITAI agents and LLM toolsGitHub
Magnitude runs open models locally and tunes kernels for your hardware. Use it to connect local models to supported agents or an OpenAI-compatible client.
Forked 2026-10-19Last synced 2026-11-08License: Apache-2.0AI agents and LLM toolsGitHub