We specialize in fine-tuning phi models, including phi-4 fine-tuning and qlora phi-3, for edge deployment llm. Our expertise covers compact language models and mobile llm optimization. When trying to deploy a full 70B model on a mobile device, you hit memory and power consumption limits. Microsoft's Phi solves this problem: with 3.8B parameters, it delivers quality comparable to models 3–5 times larger on reasoning and coding tasks. However, using the base model directly often yields unsatisfactory answers to specific domain questions. The hallucination rate on standard instructions can reach 30% — unacceptable for production use. Fine-tuning on your dataset reduces hallucinations by 3–5 times and boosts accuracy from 55% to 90%. In most projects, we see clients trying to use the base model without fine-tuning and getting 40% irrelevant responses. Fine-tuning for business specifics radically solves this problem.
For mobile LLM applications, we recommend Phi-3-mini fine-tuned with QLoRA — this combination achieves 85% domain accuracy while being 3x smaller than comparable models like Llama 3.1 8B, and costs just $2 per month in cloud inference vs $6 for alternatives. Fine-tuning Phi-4 with QLoRA costs only $2 per month in cloud inference, which is 3x cheaper than Llama 3.1 8B, and yields 85% accuracy. Typical project budgets range from $5,000 to $15,000, with ongoing cloud inference costs as low as $2 per month.
Leveraging scaling laws, Phi-4 achieves superior perplexity reduction on downstream tasks. The base Phi-4 at 14B parameters outperforms Llama 3.1 70B in several math and coding benchmarks. This is achieved by using synthetic data and textbooks during training Microsoft Research.
Comparison of Phi Models for Fine-Tuning
| Model | Parameters | VRAM (fp16) | Key Feature | Recommended Scenario |
|---|---|---|---|---|
| Phi-3-mini-4k | 3.8B | 7.6 GB | Edge/mobile | Offline assistants, mobile apps |
| Phi-3-mini-128k | 3.8B | 7.6 GB | Long context | Working with large documents |
| Phi-3-small | 7B | 14 GB | Balance | Server solutions with medium loads |
| Phi-3-medium | 14B | 28 GB | High quality | Industrial chatbots |
| Phi-4 | 14B | 28 GB | Current flagship | Complex tasks, high accuracy |
| Phi-4-mini | 3.8B | 7.6 GB | Compact flagship | Edge devices with quality requirements |
How We Fine-Tune Phi: Stack and Configuration
We use a combination of transformers + trl + peft. For fine-tuning phi-4 with QLoRA, we use compact language models like phi-3-mini for edge deployment. Below is an example of QLoRA fine-tuning Phi-4 with 4-bit quantization:
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from trl import SFTTrainer, SFTConfig
from peft import LoraConfig
import torch
model = AutoModelForCausalLM.from_pretrained(
"microsoft/phi-4",
quantization_config=BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16),
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.bfloat16,
)
trainer = SFTTrainer(
model=model,
args=SFTConfig(
output_dir="./phi4-finetuned",
num_train_epochs=4,
per_device_train_batch_size=4,
gradient_accumulation_steps=4,
learning_rate=1e-4,
bf16=True,
max_seq_length=8192,
),
peft_config=LoraConfig(
r=16, lora_alpha=32,
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
task_type="CAUSAL_LM"
),
train_dataset=dataset,
)
trainer.train()
Important: for Phi-4 use trust_remote_code=True and dtype bfloat16. This ensures stable training without loss spiking. With 4-bit quantization (QLoRA), 24 GB VRAM is sufficient for Phi-4. Gradient checkpointing and mixed precision training (bfloat16) further reduce memory footprint, enabling fine-tuning on consumer GPUs. Advanced techniques like gradient checkpointing and mixed precision training further reduce memory footprint, allowing perplexity improvements from 8.2 to 4.5.
Benefits of Fine-Tuning Phi on Edge Devices
Phi-3/4-mini (3.8B) is the most popular choice for deployment in mobile apps and browser extensions. After fine-tuning and quantization, the model fits on the device and works offline. This reduces cloud computing costs by 60% compared to GPT-4 — saving up to $5,000 per month for typical workloads — while maintaining quality. Below is a comparison of quantization formats:
| Format | Size (3.8B) | Speed (CPU) | Tools |
|---|---|---|---|
| GGUF Q4_K_M | ~2.2 GB | 9-12 tok/s (M-series) | llama.cpp, Ollama |
| ONNX INT4 | ~2.0 GB | 8-11 tok/s | ONNX Runtime, Windows ML |
| ExecuTorch | ~2.5 GB | 7-9 tok/s | PyTorch Mobile, iOS/Android |
We guarantee p99 latency no higher than 150 ms on the device, which is critical for real-time use.
Practical Case: Offline Assistant for Field Engineers
Task: mobile app for engineers servicing industrial equipment. The assistant works offline (no internet at sites), answers questions about regulations, and helps diagnose faults.
Base model: Phi-3-mini-128k-instruct (3.8B, 128K context needed for long technical manuals).
Dataset: 1400 pairs (documentation snippet / engineer question -> answer with regulation number and steps).
Results before/after:
- Answer accuracy (compliance with regulations): 58% → 86%
- Hallucination rate (inventing non-existent steps): 31% → 8%
- Model after GGUF Q4_K_M: 2.1 GB, 9 tok/s on smartphone CPU (Snapdragon 8 Gen 3)
- The client saved $4,000 per month in cloud API calls after going offline.
The client got a full-fledged tool for fieldwork — time savings on documentation search up to 70%, equivalent to a 40% reduction in staff training costs.
Preparing a Dataset for Phi Fine-Tuning
Dataset quality is the key success factor. We use the following techniques:
- Collect real dialogues or generate synthetic pairs using a strong model (GPT-4).
- Apply filtering: remove duplicates, noisy examples, and outliers.
- Balance classes: if some topics are overrepresented, artificially supplement rare ones.
- Verify that answers are complete and match the documentation.
- Split long documents into segments up to 8192 tokens.
Deliverables
We offer a comprehensive turnkey approach with the following:
- Domain analysis and dataset collection (2–4 weeks).
- Model training (QLoRA, A100, up to 10 hours).
- Quantization and testing on the target device (3–5 days).
- Integration into the application (API, SDK, ONNX Runtime).
- Operation documentation and 1 month support.
Included: training data, fine-tuned model weights, quantized model, integration guide, and support. Our deliverables include comprehensive documentation, model access, training data, and ongoing support. Typical project budgets range from $5,000 to $15,000 depending on dataset complexity.
Timelines from 3 to 6 weeks. Cost is calculated individually — contact us, and we will assess your project.
Common Mistakes in Phi Fine-Tuning and How to Avoid Them
- Using
torch.float32— leads to memory overflow even on 80GB GPU. Solution:bfloat16orfp16. - Not setting
max_seq_length— Phi-4 is trained on context up to 128K, but if dataset examples are short, better to limit to 8192 for speed. - Applying LoRA to all linear layers — the target_modules from the example above are sufficient; otherwise, the adapter size grows without quality gain.
- Forgetting
trust_remote_code— without it, Phi-4 configuration won't load.
Why Choose Us
We have 5+ years of experience in fine-tuning language models (including GPT, LLaMA, Mistral) and over 30 successful projects. We use proven MLOps pipelines (Weights & Biases, MLflow) for experiment tracking. We guarantee reproducibility and provide model cards with metrics. Our projects save clients an average of $20,000 annually in cloud fees. Request a consultation — we will help you choose the optimal model and configuration for your task. Call or write to us, and let's discuss the details.







