building-an-llm-from-scratch
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (9.3%) to scientific vocabulary
Repository
Basic Info
- Host: GitHub
- Owner: Sangwan70
- License: other
- Language: Jupyter Notebook
- Default Branch: main
- Homepage: https://www.theskillpedia.com
- Size: 12 MB
Statistics
- Stars: 9
- Watchers: 1
- Forks: 31
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
Building a Large Language Model From Scratch
This repository contains the code for developing, pretraining, and finetuning a GPT-like LLM.
You'll learn and understand how large language models (LLMs) work from the inside out by coding them from the ground up, step by step. I'll guide you through creating your own LLM, explaining each stage with clear text, diagrams, and examples.
!Environment Setup for Building Large Language Models
The method described for training and developing your own small-but-functional model for educational purposes mirrors the approach used in creating large-scale foundational models such as those behind ChatGPT. In addition, this repository includes code for loading the weights of larger pretrained models for finetuning.
- Link to the official Source Code
bash
git clone --depth 1 https://github.com/Sangwan70/Building-an-LLM-From-Scratch.git
Table of Contents
[!TIP] If you're seeking guidance on installing Python and Python packages and setting up your code environment, I suggest reading the README.md file located in the setup directory.
| part Title | Main Code (for Quick Access) | All Code + Supplementary |
|------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|-------------------------------|
| Setup recommendations | - | - |
| Part 1: Working with Text Data | - Part 1: Working with Text
- dataloader.ipynb (summary)
- additional_examples.ipynb | ./part_1 |
| Part 2: Coding Attention Mechanisms | - Part 2: Coding Attention Mechanisms
- multihead-attention.ipynb (summary)
- additional_examples.ipynb| ./part_2 |
| Part 3: Implementing a GPT Model from Scratch | - Part 3: Implementing a GPT model from Scratch
- gpt.py (summary)
- additional_examples.ipynb | ./part_3 |
| Part 4: Pretraining on Unlabeled Data | - Part 4: Pretraining on Unlabeled Data
- gpt_train.py (summary)
- gpt_generate.py (summary)
- additional_examples.ipynb | ./part_4 |
| Part 5: Finetuning for Text Classification | - Part 5: Finetuning for Text Classification
- gptclassfinetune.py
- additional_examples.ipynb | ./part_5 |
| Part 6: Finetuning to Follow Instructions | - Part 6: Finetuning To Follow Instructions
- gptinstructionfinetuning.py (summary)
- ollama_evaluate.py (summary)
- example-solutions.ipynb | ./part_6 |
| Appendix A: Introduction to PyTorch | - code-part1.ipynb
- code-part2.ipynb
- DDP-script.py
- additional_examples.ipynb | ./appendix-A |
| Appendix B: Adding Bells and Whistles to the Training Loop | - appendix-B.ipynb | ./appendix-B |
| Appendix C: Parameter-efficient Finetuning with LoRA | - appendix-C.ipynb | ./appendix-C |
Hardware Requirements
The code in the main parts of this course is designed to run on conventional laptops within a reasonable timeframe and does not require specialized hardware. Additionally, the code automatically utilizes GPUs if they are available. (Please see the setup doc for additional recommendations.)
Additional Material
Several folders contain additional materials for interested readers:
- Setup
- Part 1: Working with text data
- Part 2: Coding attention mechanisms
- Part 3: Implementing a GPT model from scratch
- Part 4: Pretraining on unlabeled data:
- Alternative Weight Loading from Hugging Face Model Hub using Transformers
- Pretraining GPT on the Project Gutenberg Dataset
- Adding Bells and Whistles to the Training Loop
- Optimizing Hyperparameters for Pretraining
- Building a User Interface to Interact With the Pretrained LLM
- Converting GPT to Llama
- Llama 3.2 From Scratch
- Memory-efficient Model Weight Loading
- Part 5: Finetuning for classification
- Part 6: Finetuning to follow instructions
- Dataset Utilities for Finding Near Duplicates and Creating Passive Voice Entries
- Evaluating Instruction Responses Using the OpenAI API and Ollama
- Generating a Dataset for Instruction Finetuning
- Improving a Dataset for Instruction Finetuning
- Generating a Preference Dataset with Llama 3.2 3B and Ollama
- Direct Preference Optimization (DPO) for LLM Alignment
- Building a User Interface to Interact With the Instruction Finetuned GPT Model
Questions, Feedback, and Contributing to This Repository
I welcome all sorts of feedback via GitHub Discussions. Likewise, if you have any questions or just want to bounce ideas off others, please don't hesitate to post these in the forum as well.
Citation
BibTeX entry:
author = {Ram N Sangwan},
title = {Building An LLM From Scratch}
github = {https://github.com/Sangwan70/Building-an-LLM-From-Scratch}
}
Owner
- Name: Ram Niwas Sangwan
- Login: Sangwan70
- Kind: user
- Company: Alliance Softech Pvt Ltd
- Website: https://www.rnsangwan.com
- Repositories: 57
- Profile: https://github.com/Sangwan70
GitHub Events
Total
- Watch event: 11
- Push event: 81
- Fork event: 33
- Create event: 2
Last Year
- Watch event: 11
- Push event: 81
- Fork event: 33
- Create event: 2
Dependencies
- pytorch/pytorch 2.5.0-cuda12.4-cudnn9-runtime build
- requests *
- tqdm *
- transformers >=4.33.2
- thop *
- chainlit >=1.2.0
- blobfile >=3.0.0
- huggingface_hub >=0.24.7
- ipywidgets >=8.1.2
- safetensors >=0.4.4
- sentencepiece >=0.1.99
- pytest >=8.1.1 test
- transformers >=4.44.2 test
- scikit-learn >=1.3.0
- transformers >=4.33.2
- chainlit >=1.2.0
- openai >=1.30.3
- scikit-learn >=1.3.1
- tqdm >=4.65.0
- openai >=1.30.3
- tqdm >=4.65.0
- openai >=1.30.3
- tqdm >=4.65.0
- chainlit >=1.2.0
- jupyterlab >=4.0
- matplotlib >=3.7.1
- numpy >=1.25,<2.0
- pandas >=2.2.1
- psutil >=5.9.5
- tensorflow >=2.15.0
- tiktoken >=0.5.1
- torch >=2.0.1
- tqdm >=4.66.1