ID: 215
Category: AI/ML Research
Language: Python
Framework: PyTorch + PEFT (LoRA/QLoRA) + Transformers
Based on Parameter-Efficient Fine-Tuning (PEFT) technology, trains high-performance medical domain large language models on consumer-grade GPUs or single A100. Supports advanced fine-tuning methods such as LoRA, QLoRA, optimized for medical text understanding and generation tasks.
bash
# Core dependencies
pip install torch transformers datasets accelerate peft bitsandbytes
# Optional for training optimization
pip install flash-attn --no-build-isolation
pip install wandb tensorboard
# Medical NLP utilities
pip install scispacy scikit-learn
python
from skills.low_resource_ai_researcher.scripts.main import MedicalPEFTTrainer
# Initialize trainer
trainer = MedicalPEFTTrainer(
model_name="meta-llama/Llama-2-7b-hf",
task="medical_qa"
)
# Train with LoRA
trainer.train(
output_dir="./medical_lora_model",
num_epochs=3,
batch_size=4,
use_qlora=True # 4-bit quantization
)
ProfileGPU MemoryQuantizationMax Model SizeBatch Sizeconsumer-24g24GB (RTX 3090/4090)QLoRA 4-bit70B1-2a100-40g40GB (A100)LoRA 8-bit70B4-8a100-80g80GB (A100)LoRA 16-bit70B8-16multi-gpu2x A100LoRA 16-bit70B+16+
yaml
lora:
r: 64 # LoRA rank
lora_alpha: 128 # Scaling factor
target_modules: # Modules to apply LoRA
- q_proj
- v_proj
- k_proj
- o_proj
- gate_proj
- up_proj
- down_proj
lora_dropout: 0.05
bias: "none"
task_type: "CAUSAL_LM"
bash
# Basic training
python scripts/main.py \
--model_name_or_path meta-llama/Llama-2-7b-hf \
--dataset medical_qa \
--output_dir ./output \
--use_qlora \
--per_device_train_batch_size 4
# With custom config
python scripts/main.py --config configs/medical_qlora.yaml
# Resume training
python scripts/main.py --resume_from_checkpoint ./output/checkpoint-1000
python
trainer = MedicalPEFTTrainer(
model_name: str, # Base model name/path
task: str, # Task type: medical_qa, diagnosis, clinical_note
lora_r: int = 64, # LoRA rank
lora_alpha: int = 128, # LoRA alpha
use_qlora: bool = False, # Use 4-bit quantization
target_modules: List[str] = None,
device_map: str = "auto",
trust_remote_code: bool = True
)
MethodDescriptiontrain()Start fine-tuning with configured parametersevaluate()Evaluate on medical benchmark datasetsmerge_and_save()Merge LoRA weights and save full modelload_model()Load a trained model for inferencegenerate()Generate medical text/responses
DatasetDescriptionSizePubMedQABiomedical QA1k QA pairsMedQAUSMLE-style questions61kMedMCQAMedical entrance exam QA194kMIMIC-IIIClinical notesDe-identifiedCMeEEChinese medical NER15kHuatuo-26MChinese medical corpus26M samples
ModelMethodGPUTraining TimeMedQA AccLLaMA-2-7BLoRAA100-40G2h58.2%LLaMA-2-7BQLoRARTX 40903h57.8%LLaMA-2-13BQLoRAA100-40G4h62.5%Mistral-7BLoRAA100-40G2.5h61.3%
python
# Enable gradient checkpointing
trainer.train(gradient_checkpointing=True)
# Reduce sequence length
trainer.train(max_seq_length=1024)
# Use DeepSpeed ZeRO-3 for large models
python
# Enable Flash Attention
trainer.train(use_flash_attention=True)
# Use bf16 on Ampere GPUs
trainer.train(bf16=True)
This skill follows the license of the underlying models used. Medical applications require compliance with HIPAA/GDPR regulations.
Risk IndicatorAssessmentLevelCode ExecutionPython/R scripts executed locallyMediumNetwork AccessNo external API callsLowFile System AccessRead input files, write output filesMediumInstruction TamperingStandard prompt guidelinesLowData ExposureOutput files saved to workspaceLow
bash
# Python dependencies
pip install -r requirements.txt
Current Stage: Draft
Next Review Date: 2026-03-06
Known Issues: None
Planned Improvements: