Text Generation
Transformers
Safetensors
English
phi3
phi
nlp
math
code
chat
conversational
Eval Results
text-generation-inference
Run this model with HF Jobs
Run microsoft/phi-4 on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/microsoft/phi-4. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/microsoft/phi-4 on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/microsoft/phi-4. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/microsoft/phi-4. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use microsoft/phi-4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/phi-4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="microsoft/phi-4") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-4") model = AutoModelForCausalLM.from_pretrained("microsoft/phi-4", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/phi-4 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/phi-4" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/microsoft/phi-4
- SGLang
How to use microsoft/phi-4 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "microsoft/phi-4" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "microsoft/phi-4" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/phi-4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use microsoft/phi-4 with Docker Model Runner:
docker model run hf.co/microsoft/phi-4
Suggested tokenizer changes by Unsloth.ai (#21)
Browse files- fix(root): Adds proposed changes by unsloth.ai. (b15fb484864f7247bfec0860d31e105173e809c7)
- chore(root): Changes pad_token to <|dummy_85|>. (492fc706206120688c0cc9d538e6be3ad6558f7f)
Files changed (4)
7 unmodified lines8910111213144 unmodified lines192021222324257 unmodified lines"auto_map": {},"bos_token_id": 100257,"embd_pdrop": 0.0,"eos_token_id": 100257,"hidden_act": "silu","hidden_size": 5120,"initializer_range": 0.02,4 unmodified lines"num_hidden_layers": 40,"num_key_value_heads": 10,"original_max_position_embeddings": 16384,"pad_token_id": 100257,"resid_pdrop": 0.0,"rms_norm_eps": 1e-05,"rope_scaling": null,7 unmodified lines8910111213144 unmodified lines192021222324257 unmodified lines"auto_map": {},"bos_token_id": 100257,"embd_pdrop": 0.0,"eos_token_id": 100265,"hidden_act": "silu","hidden_size": 5120,"initializer_range": 0.02,4 unmodified lines"num_hidden_layers": 40,"num_key_value_heads": 10,"original_max_position_embeddings": 16384,"pad_token_id": 100349,"resid_pdrop": 0.0,"rms_norm_eps": 1e-05,"rope_scaling": null,
123456789{"_from_model_config": true,"bos_token_id": 100257,"eos_token_id": [100257,100265],"transformers_version": "4.47.0"}1234567{"_from_model_config": true,"bos_token_id": 100257,"eos_token_id": 100265,"pad_token_id": 100349,"transformers_version": "4.47.0"}
12345{"bos_token": "<|endoftext|>","eos_token": "<|endoftext|>","pad_token": "<|endoftext|>"}12345{"bos_token": "<|endoftext|>","eos_token": "<|im_end|>","pad_token": "<|dummy_85|>"}
770 unmodified lines771772773774775776777778779780770 unmodified lines}},"bos_token": "<|endoftext|>","chat_template": "{% for message in messages %}{% if (message['role'] == 'system') %}{{'<|im_start|>system<|im_sep|>' + message['content'] + '<|im_end|>'}}{% elif (message['role'] == 'user') %}{{'<|im_start|>user<|im_sep|>' + message['content'] + '<|im_end|><|im_start|>assistant<|im_sep|>'}}{% elif (message['role'] == 'assistant') %}{{message['content'] + '<|im_end|>'}}{% endif %}{% endfor %}","clean_up_tokenization_spaces": false,"eos_token": "<|endoftext|>","model_max_length": 16384,"pad_token": "<|endoftext|>","tokenizer_class": "GPT2Tokenizer"}770 unmodified lines771772773774775776777778779780770 unmodified lines}},"bos_token": "<|endoftext|>","chat_template": "{% for message in messages %}{% if (message['role'] == 'system') %}{{'<|im_start|>system<|im_sep|>' + message['content'] + '<|im_end|>'}}{% elif (message['role'] == 'user') %}{{'<|im_start|>user<|im_sep|>' + message['content'] + '<|im_end|>'}}{% elif (message['role'] == 'assistant') %}{{'<|im_start|>assistant<|im_sep|>' + message['content'] + '<|im_end|>'}}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant<|im_sep|>' }}{% endif %}","clean_up_tokenization_spaces": false,"eos_token": "<|im_end|>","model_max_length": 16384,"pad_token": "<|dummy_85|>","tokenizer_class": "GPT2Tokenizer"}
config.json+2-2
generation_config.json+2-4
special_tokens_map.json+2-2
tokenizer_config.json+3-3