# dipsankadariya/build-llms-from-scratch architecture

> Architecture diagram of the GitHub repository dipsankadariya/build-llms-from-scratch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dipsankadariya/build-llms-from-scratch
- Repository: https://github.com/dipsankadariya/build-llms-from-scratch

## Overview

This is a PyTorch learning implementation of a GPT-2-style language model, progressing from text and attention foundations through model construction, pretraining, and fine-tuning. Its principal workflow prepares token sequences, trains a decoder model on unlabeled text, then adapts it for downstream tasks and instruction-style responses. The README also describes pretrained-weight loading, sampling, evaluation, and LoRA. Several capabilities are documented but their notebook implementations were not sampled, so those links rely on README evidence rather than inspected code.

Main entry points: [`04- Pretraining/components.py`](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/components.py) (GPT Model), [`04- Pretraining/Pretraining on Unlabeled Data.ipynb`](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/Pretraining%20on%20Unlabeled%20Data.ipynb) (Pretraining Loop), [`05-Finetuning`](https://github.com/dipsankadariya/build-llms-from-scratch/tree/main/05-Finetuning) (Instruction Tuning), [`04- Pretraining/5.5 Loading pretrained weights from OpenAI.ipynb`](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/5.5%20Loading%20pretrained%20weights%20from%20OpenAI.ipynb) (Weight Loading)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_text["Text Preparation"]
  node_textdata["Source Text<br/>[the-verdict.txt]"]
  node_tokenization["Tokenization"]
  node_windowdata["Windowed Batches<br/>[components.py]"]
end

subgraph group_model["GPT Model"]
  node_attentionstudy["Attention Mechanisms<br/>[Attention.ipynb]"]
  node_architecture["GPT Architecture<br/>[llm-arch.ipynb]"]
  node_modelcomponents["Model Components<br/>[components.py]"]
  node_gptmodel["GPT Model<br/>[components.py]"]
  node_generation["Text Generation<br/>[components.py]"]
end

subgraph group_pretraining["Pretraining"]
  node_trainbatches["Training Data<br/>[components.py]"]
  node_training["Pretraining Loop"]
  node_evaluation["Model Evaluation"]
  node_weightloading["Weight Loading"]
  node_gptdownload["GPT-2 Checkpoint Loader<br/>[gpt_download.py]"]
  node_textconversion["Token Conversion<br/>[utils.py]"]
end

subgraph group_adaptation["Fine-Tuning"]
  node_classification["Classification Tuning"]
  node_instruction["Instruction Tuning"]
  node_assistant["Chat Assistant"]
end

node_learner(("Learner"))
node_pretrainedservice[("GPT-2 Weights")]

node_learner -->|"provides text"| node_textdata
node_textdata -->|"tokenizes"| node_tokenization
node_tokenization -->|"prepares tokens"| node_windowdata
node_windowdata -->|"batches sequences"| node_trainbatches
node_learner -->|"studies attention"| node_attentionstudy
node_attentionstudy -->|"informs model"| node_architecture
node_architecture -->|"builds from"| node_modelcomponents
node_modelcomponents -->|"implements model"| node_gptmodel
node_gptmodel -->|"generates tokens"| node_generation
node_trainbatches -->|"supplies batches"| node_training
node_training -->|"trains"| node_gptmodel
node_training -->|"evaluates"| node_evaluation
node_weightloading -->|"loads parameters"| node_gptdownload
node_gptdownload -->|"downloads weights"| node_pretrainedservice
node_weightloading -->|"transfers weights"| node_gptmodel
node_textconversion -->|"converts tokens"| node_generation
node_gptmodel -->|"fine-tunes for labels"| node_classification
node_gptmodel -->|"fine-tunes on instructions"| node_instruction
node_instruction -->|"enables chat"| node_assistant
node_gptmodel -.->|"adapts with LoRA"| node_instruction

click node_textdata "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/the-verdict.txt"
click node_tokenization "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/01-Tokenization/WorkingWithTextualData.ipynb"
click node_windowdata "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/03-GPT%20Architecture/components.py"
click node_attentionstudy "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/02-Attention%20Mechanism/Attention.ipynb"
click node_architecture "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/03-GPT%20Architecture/llm-arch.ipynb"
click node_modelcomponents "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/03-GPT%20Architecture/components.py"
click node_gptmodel "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/components.py"
click node_generation "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/components.py"
click node_trainbatches "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/components.py"
click node_training "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/Pretraining%20on%20Unlabeled%20Data.ipynb"
click node_evaluation "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/Pretraining%20on%20Unlabeled%20Data.ipynb"
click node_weightloading "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/5.5%20Loading%20pretrained%20weights%20from%20OpenAI.ipynb"
click node_gptdownload "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/gpt_download.py"
click node_textconversion "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/utils.py"
click node_classification "https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/05-Finetuning/FinetuningForClassification.ipynb"
click node_instruction "https://github.com/dipsankadariya/build-llms-from-scratch/tree/main/05-Finetuning"
click node_assistant "https://github.com/dipsankadariya/build-llms-from-scratch/tree/main/05-Finetuning"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_textdata,node_tokenization,node_windowdata toneBlue
class node_attentionstudy,node_architecture,node_modelcomponents,node_gptmodel,node_generation,node_pretrainedservice toneAmber
class node_trainbatches,node_training,node_evaluation,node_weightloading,node_gptdownload,node_textconversion toneMint
class node_classification,node_instruction,node_assistant toneRose
class node_learner toneIndigo
```

## Components

### Text Preparation

- [Source Text](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/the-verdict.txt): `the-verdict.txt`
- [Tokenization](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/01-Tokenization/WorkingWithTextualData.ipynb): `01-Tokenization/WorkingWithTextualData.ipynb`
- [Windowed Batches](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/03-GPT%20Architecture/components.py): `03-GPT Architecture/components.py`

### GPT Model

- [Attention Mechanisms](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/02-Attention%20Mechanism/Attention.ipynb): `02-Attention Mechanism/Attention.ipynb`
- [GPT Architecture](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/03-GPT%20Architecture/llm-arch.ipynb): `03-GPT Architecture/llm-arch.ipynb`
- [Model Components](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/03-GPT%20Architecture/components.py): `03-GPT Architecture/components.py`
- [GPT Model](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/components.py): `04- Pretraining/components.py`
- [Text Generation](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/components.py): `04- Pretraining/components.py`

### Pretraining

- [Training Data](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/components.py): `04- Pretraining/components.py`
- [Pretraining Loop](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/Pretraining%20on%20Unlabeled%20Data.ipynb): `04- Pretraining/Pretraining on Unlabeled Data.ipynb`
- [Model Evaluation](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/Pretraining%20on%20Unlabeled%20Data.ipynb): `04- Pretraining/Pretraining on Unlabeled Data.ipynb`
- [Weight Loading](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/5.5%20Loading%20pretrained%20weights%20from%20OpenAI.ipynb): `04- Pretraining/5.5 Loading pretrained weights from OpenAI.ipynb`
- [GPT-2 Checkpoint Loader](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/gpt_download.py): `04- Pretraining/gpt_download.py`
- [Token Conversion](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/04-%20Pretraining/utils.py): `04- Pretraining/utils.py`

### Fine-Tuning

- [Classification Tuning](https://github.com/dipsankadariya/build-llms-from-scratch/blob/main/05-Finetuning/FinetuningForClassification.ipynb): `05-Finetuning/FinetuningForClassification.ipynb`
- [Instruction Tuning](https://github.com/dipsankadariya/build-llms-from-scratch/tree/main/05-Finetuning): `05-Finetuning`
- [Chat Assistant](https://github.com/dipsankadariya/build-llms-from-scratch/tree/main/05-Finetuning): `05-Finetuning`

### Other components

- **Learner**
- **GPT-2 Weights**

## Connections

- Learner → Source Text: provides text (evidence: `README.md`)
- Source Text → Tokenization: tokenizes (evidence: `README.md`)
- Tokenization → Windowed Batches: prepares tokens (evidence: `README.md`)
- Windowed Batches → Training Data: batches sequences (evidence: `04- Pretraining/components.py`)
- Learner → Attention Mechanisms: studies attention (evidence: `README.md`)
- Attention Mechanisms → GPT Architecture: informs model (evidence: `README.md`)
- GPT Architecture → Model Components: builds from (evidence: `03-GPT Architecture/components.py`)
- Model Components → GPT Model: implements model (evidence: `README.md`)
- GPT Model → Text Generation: generates tokens (evidence: `04- Pretraining/components.py`)
- Training Data → Pretraining Loop: supplies batches (evidence: `README.md`)
- Pretraining Loop → GPT Model: trains (evidence: `README.md`)
- Pretraining Loop → Model Evaluation: evaluates (evidence: `README.md`)
- Weight Loading → GPT-2 Checkpoint Loader: loads parameters (evidence: `04- Pretraining/gpt_download.py`)
- GPT-2 Checkpoint Loader → GPT-2 Weights: downloads weights (evidence: `04- Pretraining/gpt_download.py`)
- Weight Loading → GPT Model: transfers weights (evidence: `README.md`)
- Token Conversion → Text Generation: converts tokens (evidence: `README.md`)
- GPT Model → Classification Tuning: fine-tunes for labels (evidence: `README.md`)
- GPT Model → Instruction Tuning: fine-tunes on instructions (evidence: `README.md`)
- Instruction Tuning → Chat Assistant: enables chat (evidence: `README.md`)
- GPT Model → Instruction Tuning: adapts with LoRA (evidence: `README.md`)
