# samadon1/llm-from-scratch architecture

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

- Interactive diagram: https://gitdiagram.com/samadon1/llm-from-scratch
- Repository: https://github.com/samadon1/llm-from-scratch

## Overview

1. **Repository purpose**
   - This is a research pipeline for building a medical-domain language model.
   - The project moves through three training phases: **pretraining**, **instruction fine-tuning**, and **DPO-based preference optimization**.

2. **Top-level structure**
   - The repo is organized as three stage-specific folders:
     - `Pretraining/`
     - `Finetuning/`
     - `RLHF_DPO/`
   - The codebase is notebook-driven, so the main “orchestration” happens inside Jupyter notebooks rather than a service layer or application backend.

3. **Pretraining subsystem**
   - `Pretraining/custom_llm_model.py` contains the custom model definition used for the medical pretraining stage.
   - `Pretraining/medicaltext.txt` is the local text corpus feeding that stage.
   - `Pretraining/pretraining_notebook.ipynb` likely wires the dataset, tokenizer/training loop, and model instantiation together.
   - This stage establishes the base language model on domain text before any instruction alignment.

4. **Model boundary**
   - The custom model in `custom_llm_model.py` is the central architectural artifact in the repo.
   - It is the only standalone Python source file, which suggests the rest of the system is experimental/training logic embedded in notebooks.
   - The pretrained weights are not shown in the tree, so model artifacts are probably generated during notebook execution and stored outside the repo or in notebook outputs.

5. **Instruction fine-tuning subsystem**
   - `Finetuning/instruction_finetuning.ipynb` performs supervised instruction tuning.
   - The README says this stage uses **LitGPT** with **LoRA**, indicating parameter-efficient fine-tuning rather than full model retraining.
   - The instruction dataset comes from Hugging Face: `axiong/pmc_llama_instructions`.
   - This stage shifts the model from generic medical text completion to instruction-following behavior.

6. **DPO / preference optimization subsystem**
   - `RLHF_DPO/dpo.ipynb` is the final alignment stage.
   - The workflow described in the README is:
     - generate multiple outputs using the fine-tuned model,
     - create preference pairs,
     - rank/compare them using **Levenshtein distance**,
     - optimize the model with **Direct Preference Optimization**.
   - This stage introduces a preference-learning boundary on top of the instruction-tuned model.

7. **Data flow across stages**
   - The pipeline is sequential:
     - raw medical text → base pretrained model,
     - pretrained model + instruction dataset → instruction-tuned model,
     - instruction-tuned model + generated preference pairs → DPO-aligned model.
   - Each stage depends on the output of the previous one, so the architecture is a training chain rather than parallel independent experiments.

8. **External data and tooling**
   - External datasets materially shape the architecture:
     - Kaggle medical text dataset for pretraining,
     - Hugging Face PMC LLaMA instructions for fine-tuning.
   - Tooling called out in the README includes **LitGPT** and **LoRA**, which suggests the fine-tuning stage is built around an existing LLM training framework rather than custom training infrastructure.

9. **System boundaries**
   - There is no evidence of deployment, inference API, database, or UI layers in this repository.
   - The main boundary is between:
     - **data preparation / training notebooks**
     - and **model definition / optimization logic**
   - This makes the repo primarily an experimentation and training workspace.

10. **How to diagram it**
   - A clear architecture diagram should show:
     - the medical text corpus feeding the custom pretraining model,
     - the instruction dataset feeding the LoRA/LitGPT fine-tuning stage,
     - the fine-tuned model generating candidates for DPO preference creation,
     - the final DPO notebook consuming preference pairs to produce the aligned medical model.
   - The notebook files are the execution entry points for each stage, while `custom_llm_model.py` is the only reusable code module that anchors the training stack.

Main entry points: [`Finetuning/instruction_finetuning.ipynb`](https://github.com/samadon1/llm-from-scratch/blob/main/Finetuning/instruction_finetuning.ipynb) (Instruction notebook), [`RLHF_DPO/dpo.ipynb`](https://github.com/samadon1/llm-from-scratch/blob/main/RLHF_DPO/dpo.ipynb) (DPO notebook), [`README.md`](https://github.com/samadon1/llm-from-scratch/blob/main/README.md) (README), [`Pretraining/pretraining_notebook.ipynb`](https://github.com/samadon1/llm-from-scratch/blob/main/Pretraining/pretraining_notebook.ipynb) (Pretraining notebook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_pretraining["Pretraining"]
  node_n_pre_nb["Pretraining notebook"]
  node_n_custom_model["Custom model<br/>model code"]
  node_n_med_text["Medical text<br/>corpus<br/>[medicaltext.txt]"]
  node_n_pre_model(("Base model<br/>pretrained LM"))
end

subgraph group_g_finetuning["Finetuning"]
  node_n_ft_nb["Instruction notebook"]
  node_n_litgpt["LitGPT<br/>training framework"]
  node_n_lora{{"LoRA<br/>adapter method"}}
  node_n_instr_data["Instr. dataset<br/>hf dataset"]
  node_n_it_model(("Instr. model<br/>instruction-tuned LM"))
end

subgraph group_g_dpo["RLHF / DPO"]
  node_n_dpo_nb["DPO notebook<br/>[dpo.ipynb]"]
  node_n_candidates["Candidates<br/>generated outputs"]
  node_n_levenshtein{{"Distance ranker<br/>scoring heuristic"}}
  node_n_pref_pairs["Pref pairs<br/>preference data"]
  node_n_dpo_model(("Aligned model<br/>DPO-aligned LM"))
end

node_n_readme["README<br/>overview<br/>[README.md]"]

node_n_readme -.->|"describes"| node_n_pre_nb
node_n_readme -.->|"describes"| node_n_ft_nb
node_n_readme -.->|"describes"| node_n_dpo_nb
node_n_med_text -->|"feeds"| node_n_pre_nb
node_n_pre_nb -->|"instantiates"| node_n_custom_model
node_n_custom_model -->|"produces"| node_n_pre_model
node_n_pre_model -->|"initializes"| node_n_ft_nb
node_n_instr_data -->|"feeds"| node_n_ft_nb
node_n_ft_nb -.->|"runs on"| node_n_litgpt
node_n_ft_nb -.->|"applies"| node_n_lora
node_n_litgpt -->|"trains"| node_n_it_model
node_n_lora -->|"adapts"| node_n_it_model
node_n_it_model -->|"starts from"| node_n_dpo_nb
node_n_dpo_nb -->|"generates"| node_n_candidates
node_n_candidates -->|"scores"| node_n_levenshtein
node_n_levenshtein -->|"selects"| node_n_pref_pairs
node_n_pref_pairs -->|"consumed by"| node_n_dpo_nb
node_n_dpo_nb -->|"optimizes"| node_n_dpo_model
node_n_it_model -.->|"basis for"| node_n_dpo_model

click node_n_readme "https://github.com/samadon1/llm-from-scratch/blob/main/README.md"
click node_n_pre_nb "https://github.com/samadon1/llm-from-scratch/blob/main/Pretraining/pretraining_notebook.ipynb"
click node_n_custom_model "https://github.com/samadon1/llm-from-scratch/blob/main/Pretraining/custom_llm_model.py"
click node_n_med_text "https://github.com/samadon1/llm-from-scratch/blob/main/Pretraining/medicaltext.txt"
click node_n_ft_nb "https://github.com/samadon1/llm-from-scratch/blob/main/Finetuning/instruction_finetuning.ipynb"
click node_n_dpo_nb "https://github.com/samadon1/llm-from-scratch/blob/main/RLHF_DPO/dpo.ipynb"

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_n_pre_nb,node_n_custom_model,node_n_med_text,node_n_pre_model toneBlue
class node_n_ft_nb,node_n_litgpt,node_n_lora,node_n_instr_data,node_n_it_model toneAmber
class node_n_dpo_nb,node_n_candidates,node_n_levenshtein,node_n_pref_pairs,node_n_dpo_model toneMint
class node_n_readme toneNeutral
```

## Components

### Pretraining

Base medical-domain model training on local corpus

- [Pretraining notebook](https://github.com/samadon1/llm-from-scratch/blob/main/Pretraining/pretraining_notebook.ipynb): notebook · Orchestrates tokenizer, data, and training for domain pretraining · `Pretraining/pretraining_notebook.ipynb`
- [Custom model](https://github.com/samadon1/llm-from-scratch/blob/main/Pretraining/custom_llm_model.py): model code · Standalone Python model definition used in pretraining · `Pretraining/custom_llm_model.py`
- [Medical text](https://github.com/samadon1/llm-from-scratch/blob/main/Pretraining/medicaltext.txt): corpus · Local text corpus for base language modeling · `Pretraining/medicaltext.txt`
- **Base model**: pretrained LM · Medical-domain base model produced by pretraining

### Finetuning

Instruction tuning with parameter-efficient adaptation

- [Instruction notebook](https://github.com/samadon1/llm-from-scratch/blob/main/Finetuning/instruction_finetuning.ipynb): notebook · Supervised instruction-tuning workflow · `Finetuning/instruction_finetuning.ipynb`
- **LitGPT**: training framework · External training stack used for fine-tuning
- **LoRA**: adapter method · Parameter-efficient adaptation used during fine-tuning
- **Instr. dataset**: hf dataset · Hugging Face instruction data used for supervised tuning
- **Instr. model**: instruction-tuned LM · Model after supervised instruction fine-tuning

### RLHF / DPO

Preference optimization and final alignment

- [DPO notebook](https://github.com/samadon1/llm-from-scratch/blob/main/RLHF_DPO/dpo.ipynb): notebook · Preference optimization workflow using generated comparisons · `RLHF_DPO/dpo.ipynb`
- **Candidates**: generated outputs · Multiple responses generated from the instruction-tuned model
- **Distance ranker**: scoring heuristic · Ranks or compares outputs using Levenshtein distance
- **Pref pairs**: preference data · Chosen/rejected pairs prepared for DPO
- **Aligned model**: DPO-aligned LM · Final model after direct preference optimization

### Other components

- [README](https://github.com/samadon1/llm-from-scratch/blob/main/README.md): overview · Project description and training-stage summary · `README.md`

## Connections

- README → Pretraining notebook: describes · README documents the pretraining workflow
- README → Instruction notebook: describes · README documents the fine-tuning workflow
- README → DPO notebook: describes · README documents the DPO workflow
- Medical text → Pretraining notebook: feeds · Local medical corpus is consumed by the pretraining notebook
- Pretraining notebook → Custom model: instantiates · Notebook wires in the custom model definition
- Custom model → Base model: produces · Pretraining yields a base medical-domain model
- Base model → Instruction notebook: initializes · Pretrained weights seed instruction fine-tuning
- Instr. dataset → Instruction notebook: feeds · Instruction dataset drives supervised tuning
- Instruction notebook → LitGPT: runs on · Fine-tuning is built around LitGPT
- Instruction notebook → LoRA: applies · LoRA enables parameter-efficient adaptation
- LitGPT → Instr. model: trains · Framework produces the instruction-tuned model
- LoRA → Instr. model: adapts · Adapter tuning shapes the fine-tuned checkpoint
- Instr. model → DPO notebook: starts from · Instruction-tuned model is the input to DPO
- DPO notebook → Candidates: generates · Notebook samples multiple candidate responses
- Candidates → Distance ranker: scores · Candidates are compared with a distance heuristic
- Distance ranker → Pref pairs: selects · Ranking yields preference pairs
- Pref pairs → DPO notebook: consumed by · Preference pairs are consumed by the DPO notebook
- DPO notebook → Aligned model: optimizes · Direct Preference Optimization produces the final aligned model
- Instr. model → Aligned model: basis for · Final alignment builds on the instruction-tuned checkpoint
