This is a comprehensive LLM learning and training project inspired by:
It implements the entire LLM training pipeline from scratch, including tokenizer training, data processing, model pre-training, supervised fine-tuning (SFT), reward modeling (RM), reinforcement learning from human feedback (RLHF), quantization, and deployment.
# Clone the repository
git clone https://github.com/TimS-ml/LLM-from-Scratch.git
cd LLM-from-Scratch
# Install dependencies (using uv)
pip install uv
uv sync
# Or using pip
pip install -e .
# Step 1: Train tokenizer (3 minutes on Mac laptop)
uv run python -m scripts.train_tokenizer
# Step 2: Tokenize text data (6 minutes)
uv run python -m scripts.tokenize
# Step 3: Pre-train model (35 minutes)
uv run python -m scripts.pretrain
# Step 4: Evaluate model
uv run python -m scripts.eval_pretrain
All timings based on Mac laptop, using TinyStories-train dataset
# Configure: configs/pretrain_*.yaml
uv run python -m scripts.pretrain
# With DeepSpeed (multi-GPU)
deepspeed scripts/pretrain.py --config configs/pretrain_qwen2_5.yaml
# Configure: configs/sft_gsm8k.yaml
uv run python -m scripts.train_sft
# With LoRA adapters
uv run python -m scripts.train_sft --use_lora --lora_rank 8
# Configure: configs/rm_training.yaml
uv run python -m scripts.train_rm
# With DeepSpeed
deepspeed scripts/train_rm.py --config configs/rm_training.yaml
# Configure: configs/dpo_training.yaml
uv run python -m scripts.train_dpo
# With DeepSpeed
deepspeed scripts/train_dpo.py --config configs/dpo_training.yaml
# Configure: configs/quantization.yaml
uv run python -m scripts.quantize_model
# Example: Quantize to 4-bit
uv run python -m scripts.quantize_model \
--model_path ./checkpoints/my_model \
--bits 4 \
--output_dir ./quantized_models
# Launch interactive Streamlit web interface
uv run python -m scripts.launch_demo
# Or directly
streamlit run scratch_cs336/serve/web_ui.py
from scratch_cs336.core.generation.utils import make_context, generate_text
# Multi-turn conversation
messages = [
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi! How can I help you?"},
{"role": "user", "content": "Tell me a joke."}
]
context = make_context(tokenizer, messages)
# Generate with custom parameters
output = generate_text(
model,
context,
max_length=100,
temperature=0.8,
top_p=0.9,
repetition_penalty=1.2
)
LLM-from-Scratch/
├── scratch_cs336/ # Main package
│ ├── core/ # Reusable primitives
│ │ ├── models/ # Model architectures (CS336 LM, Qwen2.5)
│ │ ├── tokenizer/ # Tokenizer training & utilities
│ │ ├── generation/ # Generation utilities
│ │ └── quantize/ # GPTQ quantization
│ ├── training/ # Training pipeline (pretrain, SFT, RM, DPO)
│ ├── data/ # Data processing code
│ ├── eval/ # Evaluation
│ ├── serve/ # Web UI and CLI chat
│ └── utils.py # Common utilities (incl. load_config)
├── scripts/ # Thin entry points (plain YAML config, no Hydra)
├── configs/ # YAML configuration files (+ deepspeed/)
├── data/ # Data only (downloads, committed SFT samples)
├── tests/ # Tests
├── examples/ # Example workflows
└── docs/ # Documentation
Python
98.1%
Shell
1.9%
This is a comprehensive LLM learning and training project inspired by:
It implements the entire LLM training pipeline from scratch, including tokenizer training, data processing, model pre-training, supervised fine-tuning (SFT), reward modeling (RM), reinforcement learning from human feedback (RLHF), quantization, and deployment.
# Clone the repository
git clone https://github.com/TimS-ml/LLM-from-Scratch.git
cd LLM-from-Scratch
# Install dependencies (using uv)
pip install uv
uv sync
# Or using pip
pip install -e .
# Step 1: Train tokenizer (3 minutes on Mac laptop)
uv run python -m scripts.train_tokenizer
# Step 2: Tokenize text data (6 minutes)
uv run python -m scripts.tokenize
# Step 3: Pre-train model (35 minutes)
uv run python -m scripts.pretrain
# Step 4: Evaluate model
uv run python -m scripts.eval_pretrain
All timings based on Mac laptop, using TinyStories-train dataset
# Configure: configs/pretrain_*.yaml
uv run python -m scripts.pretrain
# With DeepSpeed (multi-GPU)
deepspeed scripts/pretrain.py --config configs/pretrain_qwen2_5.yaml
# Configure: configs/sft_gsm8k.yaml
uv run python -m scripts.train_sft
# With LoRA adapters
uv run python -m scripts.train_sft --use_lora --lora_rank 8
# Configure: configs/rm_training.yaml
uv run python -m scripts.train_rm
# With DeepSpeed
deepspeed scripts/train_rm.py --config configs/rm_training.yaml
# Configure: configs/dpo_training.yaml
uv run python -m scripts.train_dpo
# With DeepSpeed
deepspeed scripts/train_dpo.py --config configs/dpo_training.yaml
# Configure: configs/quantization.yaml
uv run python -m scripts.quantize_model
# Example: Quantize to 4-bit
uv run python -m scripts.quantize_model \
--model_path ./checkpoints/my_model \
--bits 4 \
--output_dir ./quantized_models
# Launch interactive Streamlit web interface
uv run python -m scripts.launch_demo
# Or directly
streamlit run scratch_cs336/serve/web_ui.py
from scratch_cs336.core.generation.utils import make_context, generate_text
# Multi-turn conversation
messages = [
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi! How can I help you?"},
{"role": "user", "content": "Tell me a joke."}
]
context = make_context(tokenizer, messages)
# Generate with custom parameters
output = generate_text(
model,
context,
max_length=100,
temperature=0.8,
top_p=0.9,
repetition_penalty=1.2
)
LLM-from-Scratch/
├── scratch_cs336/ # Main package
│ ├── core/ # Reusable primitives
│ │ ├── models/ # Model architectures (CS336 LM, Qwen2.5)
│ │ ├── tokenizer/ # Tokenizer training & utilities
│ │ ├── generation/ # Generation utilities
│ │ └── quantize/ # GPTQ quantization
│ ├── training/ # Training pipeline (pretrain, SFT, RM, DPO)
│ ├── data/ # Data processing code
│ ├── eval/ # Evaluation
│ ├── serve/ # Web UI and CLI chat
│ └── utils.py # Common utilities (incl. load_config)
├── scripts/ # Thin entry points (plain YAML config, no Hydra)
├── configs/ # YAML configuration files (+ deepspeed/)
├── data/ # Data only (downloads, committed SFT samples)
├── tests/ # Tests
├── examples/ # Example workflows
└── docs/ # Documentation
Python
98.1%
Shell
1.9%