← things I build
AIPersonal · public repoCODE ON GITHUB

Phi-3 QLoRA.

A pipeline that fine-tunes Phi-3-mini with QLoRA on Databricks Dolly 15k, tracks runs in MLflow, exports to ONNX and serves generation over FastAPI.

TECH USED
PyTorchHugging FaceMLflowONNXFastAPIDocker
Code excerpt from scripts/train_sft.py with the BitsAndBytesConfig for 4-bit NF4 and the LoraConfig
01Training config4-bit NF4 and LoRA setup from scripts/train_sft.py
Excerpt of configs/phi3_qlora.yaml with the LoRA rank, target modules and training hyperparameters
02HyperparametersLoRA and training settings from configs/phi3_qlora.yaml
the full path

From problem to what's next.

In the order it actually happened. Use the route to jump around.

01THE PROBLEM

Fine-tuning a small LLM is only half the job: the run has to fit limited GPU memory, stay tracked and reproducible, and end up served behind an API.

02THE SOLUTION

A training script loads Phi-3-mini in 4-bit NF4 and trains LoRA adapters on 8,000 Dolly samples with TRL, logging to MLflow. The adapter is merged, optionally exported to ONNX with Optimum, and served by FastAPI on PyTorch or ONNX Runtime.

WHAT I BUILT
  1. 01Wrote the QLoRA SFT script: 4-bit NF4 base, LoRA on attention and MLP projections, eval-loss checkpointing and MLflow logging.
  2. 02Added merge-to-disk and an Optimum ONNX export script.
  3. 03Built a FastAPI service with /generate and /compare (base vs fine-tuned), switching between PyTorch and ONNX Runtime backends.
  4. 04Added Docker Compose for the API and MLflow, and a notebook that plots MLflow metrics.
03HOW IT'S BUILT

The system, end to end.

TRAINING
  1. STEP 01Dolly 15k8,000 samples · 5% eval split
  2. STEP 02TRL SFTTrainerQLoRA · 4-bit NF4 · LoRA r=16
  3. STEP 03MLflowparams, metrics, adapter artifacts
  4. STEP 04Adapter and mergedsaved to outputs/
SERVING
  1. STEP 01Merged weightsfrom the training run
  2. STEP 02Optimum exportONNX, optional
  3. STEP 03Inference backendPyTorch or ONNX Runtime, by env var
  4. STEP 04FastAPI/generate · /compare · /health
04WHAT BROKE,
AND THE FIX

FIX →The service keeps a PyTorch backend that loads the merged weights or the adapter directly.

FIX →The service loads weights from the merged path and the architecture from MODEL_ID.
05WHERE IT IS NOW
  1. IDEA
  2. BUILDING
  3. LIVE

Training, export and serving code are in the repo with a documented quick start.