How gridlogic-dk/train-llm-from-scratch works
This repository is a from-scratch PyTorch LLM training and alignment suite, with a Streamlit control panel. The principal workflow prepares text or instruction data, pretrains a Transformer, then supports SFT, reward-model training, preference optimization and RL stages, followed by GSM8K evaluation or chat inference. The README documents the major training stages; the source index and excerpts support selected implementation links. Some data-preparation and training-script internals were not sampled, so those connections are shown only where documented or left out.
Main entry points: pretrain_base.py Pretraining · train_sft.py Supervised Tuning · app.py Control Panel · stage_page.py Stage Renderer
Components 27
Data Preparation
- Data Prep Scripts
scripts - Token Batches
data_loader/data_loader.py - Prompt Dataset
data_loader/prompt_dataset.py
Model Core
- Pretraining
scripts/pretrain_base.py - Transformer
src/models/transformer.py - Attention and MLP
src/models/transformer_block.py
Post-Training
- Supervised Tuning
scripts/train_sft.py - Chat Formatting
src/post_training/chat_template.py - Reward Training
scripts/train_reward.py - DPO Training
scripts/train_dpo.py - PPO Training
scripts/train_ppo.py - GRPO Training
scripts/train_grpo.py - RL Rollouts
src/post_training/rollout.py - Reward Verifiers
src/post_training/rewards/verifiers.py - Stage Config
config/post_training_config.py
Evaluation and Inference
- GSM8K Evaluation
src/post_training/evaluation.py - Chat Inference
src/post_training/inference.py - Evaluation Script
scripts/eval_post_training.py - Chat Script
scripts/chat.py
Control Panel
- Control Panel
ui/app.py - Stage Pages
ui/pages - Stage Renderer
ui/stage_page.py - Job Manager
ui/jobs.py - Stage Registry
ui/stages.py - UI Theme
ui/theme.py
Other components
- Engineer
- Operator
Connections 34
- Engineer → Data Prep Scripts: prepares data
README.md - Engineer → Pretraining: starts training
README.md - Pretraining → Supervised Tuning: initializes
README.md - Supervised Tuning → Reward Training: continues to
README.md - Supervised Tuning → DPO Training: initializes
README.md - Reward Training → PPO Training: supports
README.md - DPO Training → GRPO Training: continues to
README.md - Pretraining → Token Batches: reads batches
scripts/pretrain_base.py - Token Batches → Transformer: supplies tokens
- Transformer → Attention and MLP: uses blocks
src/models/transformer.py - Pretraining → Transformer: trains model
scripts/pretrain_base.py - Pretraining → Stage Config: loads config
scripts/pretrain_base.py - Supervised Tuning → Chat Formatting: formats conversations
- Prompt Dataset → Chat Formatting: uses formatting
- PPO Training → RL Rollouts: generates rollouts
scripts/train_ppo.py - GRPO Training → RL Rollouts: generates rollouts
scripts/train_grpo.py - PPO Training → Prompt Dataset: loads prompts
scripts/train_ppo.py - GRPO Training → Prompt Dataset: loads prompts
scripts/train_grpo.py - GSM8K Evaluation → RL Rollouts: generates tokens
src/post_training/evaluation.py - GSM8K Evaluation → Reward Verifiers: checks answers
src/post_training/evaluation.py - Chat Inference → Transformer: loads checkpoint
src/post_training/inference.py - Chat Inference → GSM8K Evaluation: generates replies
src/post_training/inference.py - Evaluation Script → GSM8K Evaluation: evaluates model
- Chat Script → Chat Inference: generates chat
- Operator → Control Panel: opens panel
ui/app.py - Control Panel → Job Manager: shows job status
ui/app.py - Control Panel → Stage Registry: shows pipeline
ui/app.py - Stage Pages → Stage Renderer: renders stages
ui/pages/2_Pretrain.py - Stage Renderer → Job Manager: launches and monitors
ui/stage_page.py - Stage Renderer → Stage Registry: reads stage metadata
ui/stage_page.py - Control Panel → UI Theme: styles panel
ui/app.py - Stage Renderer → UI Theme: styles pages
ui/stage_page.py - Job Manager → Pretraining: launches configured script
- Job Manager → Supervised Tuning: launches configured script