How samadon1/llm-from-scratch works
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: instruction_finetuning.ipynb Instruction notebook · dpo.ipynb DPO notebook · README.md README · pretraining_notebook.ipynb Pretraining notebook
Components 15
Pretraining
Base medical-domain model training on local corpus
- Pretraining notebook · Orchestrates tokenizer, data, and training for domain pretraining
Pretraining/pretraining_notebook.ipynb - Custom model · Standalone Python model definition used in pretraining
Pretraining/custom_llm_model.py - Medical text · Local text corpus for base language modeling
Pretraining/medicaltext.txt - Base model · Medical-domain base model produced by pretraining
Finetuning
Instruction tuning with parameter-efficient adaptation
- Instruction notebook · Supervised instruction-tuning workflow
Finetuning/instruction_finetuning.ipynb - LitGPT · External training stack used for fine-tuning
- LoRA · Parameter-efficient adaptation used during fine-tuning
- Instr. dataset · Hugging Face instruction data used for supervised tuning
- Instr. model · Model after supervised instruction fine-tuning
RLHF / DPO
Preference optimization and final alignment
- DPO notebook · Preference optimization workflow using generated comparisons
RLHF_DPO/dpo.ipynb - Candidates · Multiple responses generated from the instruction-tuned model
- Distance ranker · Ranks or compares outputs using Levenshtein distance
- Pref pairs · Chosen/rejected pairs prepared for DPO
- Aligned model · Final model after direct preference optimization
Other components
- README · Project description and training-stage summary
README.md
Connections 19
- 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