A step-by-step guide to implement a GPT-like Large Language Model from scratch using PyTorch. The project breaks down everything from tokens and dataset preparation, up to training causal self-attention mechanisms and generating synthetic text.
- Text Data Processing — Implemented Byte Pair Encoding (BPE), sliding window context, and tokenization from scratch.
- Attention Mechanisms — Built unweighted, weighted, causal, and multi-head self-attention techniques.
- GPT Architecture — Designed Transformer Blocks, shortcut connections, feed-forward networks (GeLU), and layered normalization.
- Pretraining & Generation — Implemented decoding strategy scripts, validation steps, model pretraining loops, and text evaluation logic.
| Layer | Technology |
|---|---|
| Framework | PyTorch >= 2.3.0 |
| Language | Python 3.10+ |
| Environment | JupyterLab >= 4.0 |
| Tokenization | Tiktoken >= 0.5.1 |
| Data Sci | NumPy, Pandas, Matplotlib, TensorFlow |
| Dependencies | uv / pyproject.toml |
Ensure you have Python 3.10+ installed.
-
Clone the Repository
git clone https://github.com/itxmjr/LLM-From-Scratch.git cd LLM-From-Scratch -
Install Dependencies You can use standard pip or
uvto install the requirements frompyproject.toml.# Using uv (recommended) uv sync # Or using standard pip pip install -e .
-
Run the Jupyter Environment
jupyter lab
Launch JupyterLab and proceed chronologically through the topic directories, which contain incremental notebooks outlining the architectural growth of a GPT model:
- Chapter 2:
Ch02_Working_With_Text_Data/— Focuses on data preparation, BPE tokenization, and vector embeddings. - Chapter 3:
Ch03_Coding_Attention_Mechanism/— Implements the mathematical backbone of transformer layers (self and causal attention). - Chapter 4:
Ch04_Implementing_A_GPT_Model/— Wraps the self-attention into full structural Transformer capabilities. - Chapter 5:
Ch05_Pretraining_On_Unlabeled_Data/— Shows how to train the assembled models and evaluate generated output.
llm-from-scratch/
├── Ch02_Working_With_Text_Data/
├── Ch03_Coding_Attention_Mechanism/
├── Ch04_Implementing_A_GPT_Model/
├── Ch05_Pretraining_On_Unlabeled_Data/
├── assets/
├── pyproject.toml
├── uv.lock
└── README.md
Contributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/AmazingFeature). - Commit your changes (
git commit -m 'Add some AmazingFeature'). - Push to the branch (
git push origin feature/AmazingFeature). - Open a Pull Request.
This project is licensed under the MIT License — see the LICENSE file for details.