This repository is a first-principles developer guide for understanding language-model components and building a small decoder-only Transformer end to end. It starts with βwhat is AI?β and progresses through tokenization, attention, training, inference, alignment, multimodality, serving, continual learning, and experimental AI systems.
Scope: This is an educational systems guide with one runnable mini-transformer. It is not a production LLM framework, a paper-reproduction suite, or evidence that the advanced roadmap has been implemented.
- Python-first teaching code β Most examples use Python/NumPy/PyTorch, with a small number of C++/kernel sketches for systems context
- First-principles explanations β Derivations, shape traces, diagrams, and code are labelled by implementation status
- Broad systems view β Tokenization, training, inference, alignment, multimodality, serving, safety, and continual-learning concepts
- Practical dataset guides β Load pinned external datasets from Hugging Face, Kaggle, HTTPS files, and Common Crawl, or build a versioned text dataset from authorized raw sources
- Connected to Aarambh AI β The systems topics are informed by the aarambh-ai Rust project, while this repository uses Python for accessible experiments
- Separates implementation from proposals β Chapter 37 is explicitly an experimental Aarambh AI research roadmap, not a report of completed results
Every chapter now states what kind of material it contains:
| Label | Meaning |
|---|---|
| Core teaching chapter | Small executable examples that demonstrate the real operation on CPU |
| Advanced educational chapter | Research-grounded explanation plus simplified code or pseudocode; production details may be omitted |
| Systems simulation | Connects components and prints illustrative metrics but is not model training |
| Research proposal | An unvalidated architecture idea that requires experiments and ablations |
The cohesive runnable implementation is code/minillm.py. It includes a tokenizer, causal Transformer, real backward pass, AdamW updates, validation, checkpointing, loading, and autoregressive generation. Tests verify that optimizer steps change parameters and checkpoints round-trip.
make check # run manuscript validation and unit tests
make demo # run a small CPU-friendly training demonstration
make train # run a longer educational CPU training job
git clone https://github.com/AarambhDevHub/llm-from-scratch.git
cd llm-from-scratch
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
make install
make check
make demomake demo uses a deliberately small CPU configuration and one PyTorch CPU thread to avoid thread-pool overhead on tiny tensors. Use make train for a longer educational run. Generated checkpoints are written under checkpoints/ and ignored by Git.
| You | Start Here |
|---|---|
| Complete beginner to AI/ML | Chapter 1 |
| Developer who can code but never built a model | Chapter 1 |
| ML engineer who uses frameworks (PyTorch, etc.) but wants to understand the internals | Chapter 5 (or skim first 4) |
| Rust/Python/C++ developer interested in LLM systems | Start anywhere, each chapter is self-contained |
| Researcher wanting a full-system view | All chapters |
Prerequisites: Basic programming knowledge (any language). High school math (algebra).
PART 1: FOUNDATIONS
βββ 01-what-is-ai.md β AI vs ML vs DL, history, the big picture
βββ 02-how-computers-learn.md β Training, inference, data, models, parameters
βββ 03-math-for-ml.md β Linear algebra, calculus, probability you actually need
βββ 04-neural-networks-101.md β Perceptrons, forward pass, activation functions
PART 2: THE TRANSFORMER REVOLUTION
βββ 05-text-to-numbers.md β Tokenization: BPE, WordPiece, SentencePiece
βββ 06-embeddings.md β From tokens to meaning: word vectors, positional encoding
βββ 07-attention.md β The secret sauce: QKV, multi-head, causal masking
βββ 08-transformer.md β The full architecture: blocks, residuals, norms
βββ 09-feedforward-moe.md β FFN, SwiGLU, Mixture of Experts, routing
PART 3: TRAINING
βββ 10-training-loop.md β Forward pass, loss, backpropagation
βββ 11-optimizers.md β SGD, Adam, AdamW, learning rate schedules
βββ 12-data-pipeline.md β Datasets, loaders, preprocessing at scale
βββ DATASET_GUIDE_EXTERNAL_SOURCES.md β Hugging Face, Kaggle, HTTP files, Common Crawl
βββ DATASET_GUIDE_BUILD_FROM_SCRATCH.md β Collection, cleaning, dedup, splits, manifests
βββ 13-distributed-training.md β Data parallel, model parallel, multi-node
PART 4: INFERENCE & DEPLOYMENT
βββ 14-inference-engine.md β Prefill, decode, KV cache, continuous batching
βββ 15-sampling.md β Temperature, top-k, top-p, speculative decoding
βββ 16-quantization.md β INT8, GPTQ, AWQ, GGUF, QAT
βββ 17-evaluation.md β Perplexity, MMLU, GSM8K, HumanEval, evals
PART 5: ADVANCED ARCHITECTURES
βββ 18-advanced-attention.md β GQA, Gated DeltaNet, DeepSeek Sparse Attention, MLA
βββ 19-multi-token-prediction.md β Predicting N tokens at once
βββ 20-mixture-of-experts.md β Fine-grained MoE, shared experts, sparse dispatch
βββ 21-thinking-engine.md β Chain-of-thought, token budgets, reasoning modes
βββ 22-kernels.md β Custom CUDA, SIMD, FlashAttention in C++
PART 6: FINE-TUNING & ALIGNMENT
βββ 23-fine-tuning.md β SFT, LoRA, QLoRA, DoRA, QDoRA
βββ 24-grpo-dpo-rlhf.md β GRPO, DPO, RLAIF, reward models
βββ 25-distillation.md β On-policy and offline distillation
PART 7: MULTIMODAL
βββ 26-vision.md β ViT, CLIP, projectors, LLaVA-style fusion
βββ 27-audio.md β Mel-spectrograms, frozen encoders, audio understanding
βββ 28-video-documents.md β Temporal fusion, layout awareness, page rasterization
PART 8: PRODUCTION SYSTEMS
βββ 29-safety.md β Prompt injection, jailbreak, PII, toxicity, red-teaming
βββ 30-serving.md β HTTP server, OpenAI API, prefix caching, multi-tenant
βββ 31-rag.md β Retrieval-Augmented Generation, embedding, ANN index
βββ 32-tool-use-agents.md β Tool calling, sandboxed execution, multi-agent orchestration
βββ 33-model-merging.md β SLERP, task vectors, weight averaging
PART 9: CONTINUOUS LEARNING
βββ 34-self-learning.md β Online GRPO, replay buffer, self-critique
βββ 35-forgetting.md β Catastrophic forgetting diagnostics, Manas integration
PART 10: THE COMPLETE PICTURE
βββ 36-end-to-end.md β Full pipeline: idea β data β train β deploy β iterate
βββ 37-v4-vision.md β Experimental roadmap and required ablations
RUNNABLE REFERENCE AND VALIDATION
βββ code/minillm.py β Small real Transformer training + generation
βββ tests/test_minillm.py β Unit tests for updates and checkpoints
βββ tools/validate_markdown.py β Syntax, fence, and local-link validation
βββ REFERENCES.md β Primary-source index by topic
βββ VALIDATION_REPORT.md β Automated and runtime check summary
How to read the learning map: Move from top to bottom when learning from scratch. Parts 1β4 form the core implementation path; Parts 5β9 extend into research and production concepts; Part 10 combines the system and clearly separates a runnable mini-model from speculative v4 work. File indentation indicates grouping, not software dependencies.
Every chapter follows the same structure:
- Definition β One-sentence technical definition
- Beginner Explanation β Plain language, no jargon
- Why We Need It β The problem this solves
- Under the Hood β Mathematical derivation, step-by-step computation
- Python Code β Explicitly labelled as executable, educational, simulated, or experimental
- C++/CUDA Code β Where performance matters
- Walk-Through Example β Concrete numbers, traced through the entire computation
- Diagram β Visual representation followed by a written reading guide and limitations
- Common Beginner Questions β FAQ with detailed answers
- Key Vocabulary β Terms defined in context
- What's Next β Preview of the next chapter
# From the cloned repository root
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
# Validate the manuscript and run the real mini-model tests
make check
# Run the small CPU model and generate text
make demo
# Optional: run a longer educational training job
make trainPython blocks are syntax-checked, but some advanced blocks are educational sketches and are not independently production-ready. The supported end-to-end CPU example is code/minillm.py; chapter status notes explain the scope of every other block.
Use these after Chapter 12 or whenever you are ready to replace toy text with a documented corpus:
- Using External Datasets from Hugging Face and Other Sources β inspect licenses and schemas, pin revisions, stream large datasets, load local/remote files, use Kaggle and Common Crawl responsibly, normalize records, and create manifests.
- Building Your Own Dataset from Scratch β define a task and schema, collect authorized files, preserve provenance, clean text, remove exact duplicates, create deterministic leakage-safe splits, export JSONL, and document the release.
Both guides include explained diagrams, executable Python examples, reproducibility rules, privacy and licensing warnings, troubleshooting, and release checklists.
Start with β Chapter 1: What is Artificial Intelligence?
The chapters are designed to be read in order, but each is self-contained enough to jump into if you have prior knowledge.
Technical corrections, primary-source references, diagram improvements, and reproducibility fixes are welcome. Read CONTRIBUTING.md before opening a pull request. Use the issue forms for reproducible bugs and content corrections.
This repository is educational and is not hardened for untrusted production workloads. Report security vulnerabilities privately according to SECURITY.md.
Citation metadata is available in CITATION.cff. GitHub can generate BibTeX and other citation formats from the repository's Cite this repository control.
Copyright 2026 Darshan Vichhi and contributors.
Licensed under the Apache License 2.0.