Home/Inference & Serving/train-llm-from-scratch
train-llm-from-scratch logo

train-llm-from-scratch

FareedKhan-dev/train-llm-from-scratch

A straightforward method for training your LLM from raw text to aligned model generation

GraphCanon updated 3d · GitHub synced 3d · 28 views this month

9.1k stars1.3k forksLast push 3d Python MIT

Decision brief

train-llm-from-scratch offers a comprehensive approach for training your own Large Language Model (LLM) using PyTorch, solely powered by a single GPU.

Good fit when

  • You're interested in building an LLM from the ground up without relying on prebuilt packages like transformers or peft.
  • You are a student who wants to understand every step of creating and training an LLM through clear explanations and hand-written code.

Avoid when

  • Your goal is to rapidly prototype and fine-tune an existing pre-trained LLM with minimal coding effort.
  • You prefer using established transformer libraries or frameworks like Hugging Face's transformers, which offer quicker setup but less control over the underlying code.
Pricing:
freemium - This repository is available under the MIT license, allowing free use for both personal and commercial purposes. The model training requires resources on your end with no additional licensing costs.
Requirements:
A single GPU environment is necessary.; Basic understanding of PyTorch is recommended to leverage the full potential of this tool.; Familiarity with NLP and transformer-based models can be helpful but not mandatory.

Observed Jul 9, 2026 · Source: enrich:decision_facts

Verify the decision

Maintenance and security

Full trust report
Maintenance
Very active (0d since push)
As of 3d
Provenance
Not a fork · Personal account
As of 3d
Security (OSV)
No criticals
As of 1mo

Public GitHub metadata and optional OSV scans. Signals, not a guarantee. Trust methodology.

Install

pip install train-llm-from-scratch
PyPI

How it fits your stack(9)

Typed graph edges - alternatives, integrations, successors, and dependencies. Ranked by relationship type, not raw GitHub stars.

Alternative

Relationship graph

Optional deeper exploration of typed edges and category neighbours.

Similar tools

Same-category neighbours not already linked as typed edges.

Evidence and technical details

Sourced facts, taxonomy, compatibility claims, README excerpt, and machine-readable endpoints.

Overview

Provides scripts to train large language models (LLM) with various parameters using PyTorch, starting from basic data processing and ending at an aligned reasoning style model.

Capability facts

Languages
python

Source: github.language+pyproject.toml · Aug 17, 2026

Categories

Tags

README

Train LLM From Scratch

I am Looking for a PhD position in AI. GitHub

I implemented a transformer model from scratch using PyTorch, based on the paper Attention is All You Need. You can use my scripts to train your own billion or million parameter LLM using a single GPU.

This started as a pretraining tutorial. It now goes all the way from raw text to an aligned, reasoning style model, with every algorithm hand written in plain PyTorch (no trl, no peft, no transformers). The whole journey is one idea repeated: turn text into numbers, predict the next token, then keep changing the data and the loss until the model does what we want.

Here is the path we will walk, end to end:

raw text  ->  tokens  ->  a Transformer  ->  next-token loss  ->  a base model
base model  ->  SFT  ->  Reward Model  ->  {PPO, DPO}  ->  GRPO  ->  evaluation and chat

Below is the output of a trained 13 million parameter LLM, just so you can see where the small end of this starts:

In ***1978, The park was returned to the factory-plate that
the public share to the lower of the electronic fence that
follow from the Station's cities. The Canal of ancient Western
nations were confined to the city spot. The villages were directly
linked to cities in China that revolt that the US budget and in
Odambinais is uncertain and fortune established in rural areas.

Table of Contents

  • Who this is for
  • Prerequisites and Training Time
  • Setup
  • Code Structure
  • Step 1: Preparing the Data
  • Step 2: The Model, Built From Small Pieces
    • Multi Layer Perceptron (MLP)
    • Single Head Attention
    • Multi Head Attention
    • The Transformer Block
    • The Full Transformer
  • Step 3: Pretraining the Base Model
  • Step 4: Generating Text
  • Step 5: Post-Training, Turning a Base Model Into an Assistant
    • SFT (Supervised Fine-Tuning)
    • The Reward Model
    • DPO, ORPO and KTO
    • PPO
    • GRPO / RLVR
  • Step 6: Evaluation
  • Step 7: Talking to the Model
  • The Streamlit Control Panel
  • The Documentation Site
  • Run the Whole Thing
  • What's Next

Who this is for

I tried to write this so one page works for very different readers:

  • If you are a student, read top to bottom. Every block of code comes after a plain explanation of what it does and why, and most blocks are followed by the output you should expect.
  • If you are a developer, the commands and file paths are all here. You can copy, run, and read the referenced source files directly.
  • If you are a researcher, the post-training half is the interesting part: SFT, a Bradley-Terry reward model, PPO with GAE, DPO/ORPO/KTO, and GRPO, all from scratch on the same small Transformer, trained on real public datasets.

Every diagram in this README is colored the same way, so the colors mean something:

  • green is raw data
  • teal is stored, tokenized data on disk
  • blue is a plain processing step
  • yellow is the model or a training step
  • orange is the reinforcement learning and reward parts
  • red is a loss
  • grey is a saved checkpoint
  • purple is the final output or evaluation

Prerequisites and Training Time

You need a basic understanding of object oriented programming, neural networks, and PyTorch. Below are so

For agents

This page has a .md twin and JSON over the API.

Was this helpful?

Anonymous feedback helps us improve pages and translations.