πŸ€– AI/ML

Phi-3

Microsoft's small language model

πŸ“– Detailed Explanation

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.

πŸ’» Code Example

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

πŸ“Š Performance & Cost

As of January 2025 - Phi-3 model specifications and benchmarks

ModelParameterContextMMLUFeatures
Phi-3-mini3.8B4K / 128K69%Runs on smartphones
Phi-3-small7B8K / 128K75%Outperforms GPT-3.5T
Phi-3-medium14B4K / 128K78%Outperforms Gemini 1.0 Pro
Phi-3.5-mini3.8B128K69%Enhanced multilingual capability
Phi-3.5-MoE6.6B active/42B total128K78%MoE architecture
Phi-3.5-Vision4.2B128K-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)

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"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."
πŸ’¬ In an Interview
"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."
πŸ’¬ In a Technical Discussion
"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

⚠️ Common Mistakes & Cautions

❌
Using Phi-3 for factual knowledge tasks

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.

❌
Using the Base model directly for conversation

The Base model is for next-token prediction, and the Instruct model is for conversation. Use "Phi-3-mini-instruct".

βœ…
The right way

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.

πŸ”— Related Terms

πŸ“š Learn More