QLoRA supervised fine-tuning (Phi-3-mini + Databricks Dolly 15k) with MLflow tracking, optional ONNX export via Hugging Face Optimum, and a FastAPI service (PyTorch or ONNX Runtime).
flowchart LR
subgraph train [Training]
DS[Dataset]
HF[HF model + PEFT]
TRL[TRL SFTTrainer]
MLF[MLflow]
DS --> TRL
HF --> TRL
TRL --> MLF
TRL --> Artifacts[Adapter + merged]
end
subgraph serve [Serving]
Artifacts --> Export[Optimum ONNX export]
Export --> ORT[ONNX Runtime]
ORT --> API[FastAPI]
Artifacts --> API
end
python -m venv .venv
.venv\Scripts\activate # Windows
pip install -e ".[dev]" # or: pip install -r requirements.txtOn Windows / Linux with NVIDIA GPUs, install a CUDA build of PyTorch from pytorch.org. bitsandbytes is required for QLoRA on non-macOS platforms with a compatible GPU stack. On macOS, requirements.txt omits bitsandbytes; use Linux/WSL/CUDA for full QLoRA training.
set MLFLOW_TRACKING_URI=file:./mlruns
set MLFLOW_EXPERIMENT_NAME=lora-phi3-sft
python scripts/train_sft.py --config configs/phi3_qlora.yamlOutputs (by default):
outputs/phi3-lora/adapter— PEFT adapter (also logged under MLflowadapter/).outputs/phi3-lora/merged— merged weights for serving/export whensave_merged_model: true.
Adjust configs/phi3_qlora.yaml if you hit OOM (e.g. lower max_seq_length, per_device_train_batch_size, or max_samples).
mlflow ui --backend-store-uri file:./mlrunsOr use Docker Compose and open http://localhost:5000.
From the merged checkpoint:
python scripts/export_onnx.py --model outputs/phi3-lora/merged --output outputs/phi3-lora/onnx --provider CPUExecutionProviderUse CUDAExecutionProvider when exporting on GPU if your ORT build supports it. Decoder ONNX export can be brittle; if it fails, use the PyTorch backend first.
Copy .env.example to .env and set paths.
PyTorch (merged weights):
Merged directories often contain weights only (no modeling_phi3.py). The service loads weights from MERGED_MODEL_PATH and the architecture from MODEL_ID (Hugging Face Hub / local cache). Set MODEL_ID to the same base model used in training.
set INFERENCE_BACKEND=pytorch
set MODEL_ID=microsoft/Phi-3-mini-4k-instruct
set MERGED_MODEL_PATH=outputs/phi3-lora/merged
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000PyTorch (PEFT adapter):
set INFERENCE_BACKEND=pytorch
set ADAPTER_PATH=outputs/phi3-lora/adapter
set MODEL_ID=microsoft/Phi-3-mini-4k-instruct
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000ONNX:
set INFERENCE_BACKEND=onnx
set ONNX_MODEL_PATH=outputs/phi3-lora/onnx
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Example POST /generate body: examples/generate_request.json.
curl.exe -s -X POST http://127.0.0.1:8000/generate -H "Content-Type: application/json" --data-binary "@examples/generate_request.json"| Method | Path | Description |
|---|---|---|
| GET | /health |
Status and backend label |
| POST | /generate |
prompt, max_new_tokens, temperature, top_p |
| POST | /compare |
Base vs fine-tuned (PyTorch only; needs adapter or merged path). Disabled for INFERENCE_BACKEND=onnx. |
Requires an exported ONNX model at outputs/phi3-lora/onnx.
docker compose up --build- API:
http://localhost:8000 - MLflow:
http://localhost:5000(mounts./mlrunsfrom the host)
notebooks/compare.ipynb — plot MLflow metrics and optional base vs fine-tuned samples (PyTorch).
| Area | Current setup | Common upgrades |
|---|---|---|
| MLflow | file:./mlruns |
Database backend + object storage for artifacts |
| Inference image | CPU ONNX | GPU ORT, dedicated model servers |
| ONNX | Optimum export | Model-specific ORT GenAI or serving stacks |
| Eval | Holdout eval_loss |
Task metrics, human review |
| Secrets | .env |
Secret managers in production |
configs/phi3_qlora.yaml— training hyperparametersscripts/train_sft.py— QLoRA SFT + MLflowscripts/export_onnx.py— ONNX exportapp/— FastAPI applicationdocker/— container imagesdocker-compose.yml— API + MLflow
Source code in this repository is released under the MIT License.
Pre-trained and fine-tuned model weights (e.g. Microsoft Phi-3, adapter checkpoints) remain under their original licenses; see the respective Hugging Face model cards.
Update the copyright line in LICENSE when publishing under your name or organization.
Before pushing: do not commit .env, local body.json, outputs/, mlruns/, or *.egg-info/ (they are listed in .gitignore). Run pip install -e . again after the package rename so editable metadata matches pyproject.toml.