This repository demonstrates fine-tuning a lightweight LLM (TinyLlama-1.1B) on bioinformatics instruction-response prompts using Low-Rank Adaptation (LoRA). The goal is to adapt a general instruction-tuned model to answer domain-specific questions and showcase the improvements before and after fine-tuning.
- Fine-tune TinyLlama for bioinformatics instructions
- Run locally on Apple Silicon (M4 Max, MPS backend)
- Compare outputs before and after LoRA fine-tuning
bioinfo-lora-finetuning-demo/
├── data/
│ └── bioinfo_train.jsonl # Instruction-response dataset
├── src/
│ ├── lora_train.py # LoRA fine-tuning script
│ ├── lora_infer_before.py # Baseline inference
│ ├── lora_infer_after.py # Inference using LoRA adapter
│ └── merge_lora.py # Merge adapter into base model
├── results/
│ ├── sample_outputs_before.txt
│ ├── sample_outputs_after.txt
│ └── merged-model/
├── requirements.txt
└── README.md
- Clone the repository and create a virtual environment:
git clone <your-repo-url>
cd bioinfo-lora-finetuning-demo
python -m venv venv
source venv/bin/activate- Install dependencies:
pip install --upgrade pip setuptools wheel
pip install -r requirements.txtNote: On macOS M4, if you encounter sentencepiece build errors, use:
pip install sentencepiece --prefer-binarydata/bioinfo_train.jsonlcontains bioinformatics instruction-response pairs in JSON Lines format:
{"instruction": "Explain what a FASTQ file is.", "output": "A FASTQ file stores sequencing reads with quality scores..."}
{"instruction": "What is SNP annotation?", "output": "SNP annotation links single-nucleotide polymorphisms to genes and predicts functional impacts."}- You can expand this dataset to hundreds of examples for improved results.
python src/lora_train.py \
--dataset_path data/bioinfo_train.jsonl \
--epochs 3 \
--batch_size 2 \
--gradient_accumulation 4 \
--lr 2e-4 \
--output_dir results/lora-adapter- Saves LoRA adapter to
./results/lora-adapter - Uses MPS GPU if available
- Training logs show decreasing loss over steps
python src/lora_infer_before.pypython src/lora_infer_after.pyExample comparison:
| Prompt | Baseline | LoRA Fine-Tuned |
|---|---|---|
| Explain FASTQ | “A text file containing sequences.” | “A FASTQ file stores sequencing reads with quality scores, used in genome sequencing and bioinformatics pipelines.” |
| SNP annotation | “A process in genomics.” | “SNP annotation links single-nucleotide polymorphisms to genes and predicts functional impact.” |
To create a standalone fine-tuned model:
python src/merge_lora.py- Output:
./results/merged-model/ - Load without PEFT adapters:
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("./results/merged-model").to("mps")
tokenizer = AutoTokenizer.from_pretrained("./results/merged-model")- Train time: ~1 min per epoch on M4 Max
- Loss decrease: ~10 → 0.3
- Output improvement: Domain-specific answers with bioinformatics terms