Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Phi-3 QLoRA fine-tuning and ONNX inference

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).

Architecture

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
Loading

Quick start

1. Environment

python -m venv .venv
.venv\Scripts\activate   # Windows
pip install -e ".[dev]"  # or: pip install -r requirements.txt

On 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.

2. Train (QLoRA + MLflow)

set MLFLOW_TRACKING_URI=file:./mlruns
set MLFLOW_EXPERIMENT_NAME=lora-phi3-sft
python scripts/train_sft.py --config configs/phi3_qlora.yaml

Outputs (by default):

  • outputs/phi3-lora/adapter — PEFT adapter (also logged under MLflow adapter/).
  • outputs/phi3-lora/merged — merged weights for serving/export when save_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).

3. MLflow UI

mlflow ui --backend-store-uri file:./mlruns

Or use Docker Compose and open http://localhost:5000.

4. Export ONNX

From the merged checkpoint:

python scripts/export_onnx.py --model outputs/phi3-lora/merged --output outputs/phi3-lora/onnx --provider CPUExecutionProvider

Use 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.

5. Inference API

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 8000

PyTorch (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 8000

ONNX:

set INFERENCE_BACKEND=onnx
set ONNX_MODEL_PATH=outputs/phi3-lora/onnx
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

Example 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.

6. Docker Compose

Requires an exported ONNX model at outputs/phi3-lora/onnx.

docker compose up --build
  • API: http://localhost:8000
  • MLflow: http://localhost:5000 (mounts ./mlruns from the host)

Notebook

notebooks/compare.ipynb — plot MLflow metrics and optional base vs fine-tuned samples (PyTorch).

Operations notes

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

Layout

  • configs/phi3_qlora.yaml — training hyperparameters
  • scripts/train_sft.py — QLoRA SFT + MLflow
  • scripts/export_onnx.py — ONNX export
  • app/ — FastAPI application
  • docker/ — container images
  • docker-compose.yml — API + MLflow

License

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages