Comparative Analysis of Knowledge Distillation Methods for Medical Question Answering
A comprehensive framework for distilling large medical language models (70B parameters) into efficient smaller models (1.5B parameters) using multiple state-of-the-art distillation techniques.
This project implements and compares 10 different knowledge distillation methods for medical language models, focusing on transferring knowledge from a large teacher model (Meditron-70B) to a compact student model (Qwen2-1.5B).
uv (Recommended)# Install uv package manager
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone repository
git clone https://github.com/shreyanmitra/Medistillation.git
cd Medistillation
# Sync dependencies
uv sync
pip# Clone repository
git clone https://github.com/shreyanmitra/Medistillation.git
cd Medistillation
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
python -c "import torch; print(f'PyTorch: {torch.__version__}'); print(f'CUDA Available: {torch.cuda.is_available()}')"
# Download and prepare all datasets (Med-DistillMix, benchmarks, etc.)
python src/DataLoader.py --prepare_all
This creates:
data/processed/train.jsonl (~414k examples)data/processed/validation.jsonldata/processed/test.jsonldata/benchmarks/ (MedQA, MedMCQA, PubMedQA, PubHealth)data/medppl_10k.jsonl (perplexity corpus)data/fidelitybench_med.jsonl (faithfulness evaluation)# Supervised Fine-Tuning (SFT) - Baseline
python src/Trainer.py \
--method sft \
--teacher_model epfl-llm/meditron-70b \
--student_model Qwen/Qwen2-1.5B \
--num_epochs 3 \
--batch_size 8 \
--output_dir outputs/sft_run1
# Training curves and evaluation plots
ls outputs/sft_run1/results/*.png
# Evaluation metrics (JSON)
cat outputs/sft_run1/results/comprehensive_evaluation.json
Generated Plots:
training_curves.png - Loss and learning rate over epochsbenchmark_comparison.png - Student vs teacher accuracyfidelity_metrics.png - KL/BLEU/ROUGE scoresfidelitybench_radar.png - Evidence faithfulness radar chartMedistillation/
├── data/ # Datasets (gitignored)
│ ├── raw/ # Original downloaded data
│ ├── processed/ # Train/val/test splits
│ ├── benchmarks/ # Evaluation benchmarks
│ ├── medppl_10k.jsonl # Perplexity corpus
│ └── fidelitybench_med.jsonl # Faithfulness eval
│
├── src/ # Source code
│ ├── DataLoader.py # Dataset preparation and loading
│ ├── DistillationMethods.py # Distillation algorithm implementations
│ └── Trainer.py # Training loop and evaluation
│
├── docs/ # Documentation
│ ├── guides/ # User guides
│ │ ├── EXPERIMENT_PROCEDURE.md
│ │ ├── FIDELITYBENCH_GUIDE.md
│ │ └── VISUALIZATION_GUIDE.md
│ ├── implementation/ # Technical details
│ └── research/ # Research materials
│
├── scripts/ # Utility scripts
│ └── sample_medmcqa.py # Data sampling utility
│
├── outputs/ # Training results (gitignored)
│ └── {method}_{run_name}/
│ ├── checkpoints/
│ ├── results/
│ └── final_model/
│
├── tests/ # Unit tests
├── requirements.txt # Python dependencies
└── README.md # This file
python src/Trainer.py --method sft --num_epochs 3
python src/Trainer.py --method logit_kd --alpha 0.5 --temperature 3.0
python src/Trainer.py --method adakd --base_temperature 3.0 --min_temperature 1.0 --max_temperature 5.0
python src/Trainer.py --method cot --num_rationales 3 --sampling_temperature 0.7
python src/Trainer.py --method fitnets --layer_mapping '{"6":12,"12":24}' --use_projections
python src/Trainer.py --method attention --layer_mapping '{"6":12,"12":24}' --match_all_heads
python src/Trainer.py --method ppo --epsilon 0.2 --gamma 0.99
python src/Trainer.py --method bond --num_samples 16
python src/Trainer.py --method spin --beta 0.1
Comprehensive guides available in docs/:
docs/guides/)docs/implementation/)docs/research/)# Test different temperatures for Logit-KD
python src/Trainer.py \
--run_ablation \
--ablation_type temperature \
--ablation_values "2.0,3.0,4.0,5.0" \
--method logit_kd
# View sensitivity analysis plot
start outputs/ablation_temperature/ablation_plot.png # Windows
# open outputs/ablation_temperature/ablation_plot.png # Mac/Linux
# Train different methods
python src/Trainer.py --method sft --output_dir outputs/sft
python src/Trainer.py --method logit_kd --alpha 0.5 --temperature 3.0 --output_dir outputs/logit_kd
python src/Trainer.py --method adakd --output_dir outputs/adakd
# Compare results
python scripts/compare_results.py outputs/sft outputs/logit_kd outputs/adakd
If you use this code in your research, please cite:
@misc{medistillation2025,
title={Comparative Analysis of Knowledge Distillation Methods for Medical Question Answering},
author={CSE 493S Team},
year={2025},
publisher={University of Washington},
url={https://github.com/shreyanmitra/Medistillation}
}
Happy Distilling! 🧪✨
Jupyter Notebook
50.9%
Python
48.5%
Comparative Analysis of Knowledge Distillation Methods for Medical Question Answering
A comprehensive framework for distilling large medical language models (70B parameters) into efficient smaller models (1.5B parameters) using multiple state-of-the-art distillation techniques.
This project implements and compares 10 different knowledge distillation methods for medical language models, focusing on transferring knowledge from a large teacher model (Meditron-70B) to a compact student model (Qwen2-1.5B).
uv (Recommended)# Install uv package manager
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone repository
git clone https://github.com/shreyanmitra/Medistillation.git
cd Medistillation
# Sync dependencies
uv sync
pip# Clone repository
git clone https://github.com/shreyanmitra/Medistillation.git
cd Medistillation
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
python -c "import torch; print(f'PyTorch: {torch.__version__}'); print(f'CUDA Available: {torch.cuda.is_available()}')"
# Download and prepare all datasets (Med-DistillMix, benchmarks, etc.)
python src/DataLoader.py --prepare_all
This creates:
data/processed/train.jsonl (~414k examples)data/processed/validation.jsonldata/processed/test.jsonldata/benchmarks/ (MedQA, MedMCQA, PubMedQA, PubHealth)data/medppl_10k.jsonl (perplexity corpus)data/fidelitybench_med.jsonl (faithfulness evaluation)# Supervised Fine-Tuning (SFT) - Baseline
python src/Trainer.py \
--method sft \
--teacher_model epfl-llm/meditron-70b \
--student_model Qwen/Qwen2-1.5B \
--num_epochs 3 \
--batch_size 8 \
--output_dir outputs/sft_run1
# Training curves and evaluation plots
ls outputs/sft_run1/results/*.png
# Evaluation metrics (JSON)
cat outputs/sft_run1/results/comprehensive_evaluation.json
Generated Plots:
training_curves.png - Loss and learning rate over epochsbenchmark_comparison.png - Student vs teacher accuracyfidelity_metrics.png - KL/BLEU/ROUGE scoresfidelitybench_radar.png - Evidence faithfulness radar chartMedistillation/
├── data/ # Datasets (gitignored)
│ ├── raw/ # Original downloaded data
│ ├── processed/ # Train/val/test splits
│ ├── benchmarks/ # Evaluation benchmarks
│ ├── medppl_10k.jsonl # Perplexity corpus
│ └── fidelitybench_med.jsonl # Faithfulness eval
│
├── src/ # Source code
│ ├── DataLoader.py # Dataset preparation and loading
│ ├── DistillationMethods.py # Distillation algorithm implementations
│ └── Trainer.py # Training loop and evaluation
│
├── docs/ # Documentation
│ ├── guides/ # User guides
│ │ ├── EXPERIMENT_PROCEDURE.md
│ │ ├── FIDELITYBENCH_GUIDE.md
│ │ └── VISUALIZATION_GUIDE.md
│ ├── implementation/ # Technical details
│ └── research/ # Research materials
│
├── scripts/ # Utility scripts
│ └── sample_medmcqa.py # Data sampling utility
│
├── outputs/ # Training results (gitignored)
│ └── {method}_{run_name}/
│ ├── checkpoints/
│ ├── results/
│ └── final_model/
│
├── tests/ # Unit tests
├── requirements.txt # Python dependencies
└── README.md # This file
python src/Trainer.py --method sft --num_epochs 3
python src/Trainer.py --method logit_kd --alpha 0.5 --temperature 3.0
python src/Trainer.py --method adakd --base_temperature 3.0 --min_temperature 1.0 --max_temperature 5.0
python src/Trainer.py --method cot --num_rationales 3 --sampling_temperature 0.7
python src/Trainer.py --method fitnets --layer_mapping '{"6":12,"12":24}' --use_projections
python src/Trainer.py --method attention --layer_mapping '{"6":12,"12":24}' --match_all_heads
python src/Trainer.py --method ppo --epsilon 0.2 --gamma 0.99
python src/Trainer.py --method bond --num_samples 16
python src/Trainer.py --method spin --beta 0.1
Comprehensive guides available in docs/:
docs/guides/)docs/implementation/)docs/research/)# Test different temperatures for Logit-KD
python src/Trainer.py \
--run_ablation \
--ablation_type temperature \
--ablation_values "2.0,3.0,4.0,5.0" \
--method logit_kd
# View sensitivity analysis plot
start outputs/ablation_temperature/ablation_plot.png # Windows
# open outputs/ablation_temperature/ablation_plot.png # Mac/Linux
# Train different methods
python src/Trainer.py --method sft --output_dir outputs/sft
python src/Trainer.py --method logit_kd --alpha 0.5 --temperature 3.0 --output_dir outputs/logit_kd
python src/Trainer.py --method adakd --output_dir outputs/adakd
# Compare results
python scripts/compare_results.py outputs/sft outputs/logit_kd outputs/adakd
If you use this code in your research, please cite:
@misc{medistillation2025,
title={Comparative Analysis of Knowledge Distillation Methods for Medical Question Answering},
author={CSE 493S Team},
year={2025},
publisher={University of Washington},
url={https://github.com/shreyanmitra/Medistillation}
}
Happy Distilling! 🧪✨
Jupyter Notebook
50.9%
Python
48.5%