Phi-3
Microsoft's small language model
Microsoft's small language model
Phi-3 is a series of small language models (SLMs) developed by Microsoft. Aiming for "small but powerful" models, it achieves performance that competes with much larger models using 3.8B to 14B parameters. It can run on local devices and in edge environments.
The Phi series began in 2023 with Phi-1 (code-specialized) and evolved in 2024 into Phi-2 (2.7B) and Phi-3 (3.8B/7B/14B). Microsoft Research's "Textbooks Are All You Need" study demonstrated that even small models can achieve strong performance with high-quality synthetic data.
The core innovation is the quality of the training data. A significant portion of the 4.9 trillion-token dataset consists of "textbook-level" synthetic data. As a result, it shows performance that surpasses larger models in mathematics, coding, and commonsense reasoning.
In practice, Phi-3 is well suited to cost-efficient AI deployment. It can run on smartphones and, while reducing cloud costs, provides sufficient performance on many tasks. It is also free for commercial use under the MIT License.
# Phi-3 λͺ¨λΈ μ¬μ© μμ
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import torch
# 1. Phi-3 Mini (3.8B) λͺ¨λΈ λ‘λ
model_name = "microsoft/Phi-3-mini-4k-instruct" # λλ 128k λ²μ
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16, # λ©λͺ¨λ¦¬ μ μ½
device_map="auto",
trust_remote_code=True
)
# 2. νμ΄νλΌμΈμΌλ‘ κ°λ¨νκ² μ¬μ©
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
# 3. Phi-3 λν νμ (Instruct λͺ¨λΈ)
messages = [
{"role": "user", "content": "νΌλ³΄λμΉ μμ΄μ PythonμΌλ‘ ꡬνν΄μ€"}
]
# μ±ν
ν
νλ¦Ώ μ μ©
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
# 4. μμ±
outputs = pipe(
prompt,
max_new_tokens=256,
do_sample=True,
temperature=0.7,
top_p=0.9,
)
print(outputs[0]["generated_text"])
# 5. ONNXλ‘ λ³ννμ¬ λ λΉ λ₯Έ μΆλ‘ (μ ν)
# from optimum.onnxruntime import ORTModelForCausalLM
# ort_model = ORTModelForCausalLM.from_pretrained(model_name, export=True)
As of January 2025 - Phi-3 model specifications and benchmarks
| Model | Parameter | Context | MMLU | Features |
|---|---|---|---|---|
| Phi-3-mini | 3.8B | 4K / 128K | 69% | Runs on smartphones |
| Phi-3-small | 7B | 8K / 128K | 75% | Outperforms GPT-3.5T |
| Phi-3-medium | 14B | 4K / 128K | 78% | Outperforms Gemini 1.0 Pro |
| Phi-3.5-mini | 3.8B | 128K | 69% | Enhanced multilingual capability |
| Phi-3.5-MoE | 6.6B active/42B total | 128K | 78% | MoE architecture |
| Phi-3.5-Vision | 4.2B | 128K | - | Multimodal |
Cost: MIT-licensed open source; usage-based billing when hosted on Azure AI
Hardware requirements: Phi-3-mini can run on 8GB VRAM (FP16) or 4GB VRAM (INT4 quantization)
"If you quantize Phi-3-mini to INT4, it runs on 4GB VRAM. It delivers GPT-3.5-level performance on general Q&A tasks while allowing you to eliminate API costs entirely."
"The core innovation of Phi-3 is high-quality synthetic data. Microsoft Research's 'Textbooks Are All You Need' paper demonstrated that data quality can matter more than model size."
"Phi-3 is weak on fact-based knowledge (such as TriviaQA), but on reasoning/coding tasks it competes with models 10 times larger. It is an optimal choice for edge device deployment."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
Phi-3 has few parameters, so there is a limit to how much factual knowledge it can store. For fact-based tasks such as TriviaQA, consider a larger model.
The Base model is for next-token prediction, and the Instruct model is for conversation. Use "Phi-3-mini-instruct".
Use Phi-3 for reasoning, coding, and math tasks, and combine it with RAG or a larger model for complex knowledge tasks. Optimize memory with quantization.