Text Generation
Transformers
PyTorch
ExecuTorch
multilingual
phi3
torchao
phi
phi4
nlp
code
math
chat
conversational
custom_code
text-generation-inference
Instructions to use pytorch/Phi-4-mini-instruct-INT8-INT4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use pytorch/Phi-4-mini-instruct-INT8-INT4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="pytorch/Phi-4-mini-instruct-INT8-INT4", trust_remote_code=True) 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("pytorch/Phi-4-mini-instruct-INT8-INT4", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("pytorch/Phi-4-mini-instruct-INT8-INT4", trust_remote_code=True, 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]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use pytorch/Phi-4-mini-instruct-INT8-INT4 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "pytorch/Phi-4-mini-instruct-INT8-INT4" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "pytorch/Phi-4-mini-instruct-INT8-INT4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/pytorch/Phi-4-mini-instruct-INT8-INT4
- SGLang
How to use pytorch/Phi-4-mini-instruct-INT8-INT4 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 "pytorch/Phi-4-mini-instruct-INT8-INT4" \ --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": "pytorch/Phi-4-mini-instruct-INT8-INT4", "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 "pytorch/Phi-4-mini-instruct-INT8-INT4" \ --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": "pytorch/Phi-4-mini-instruct-INT8-INT4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use pytorch/Phi-4-mini-instruct-INT8-INT4 with Docker Model Runner:
docker model run hf.co/pytorch/Phi-4-mini-instruct-INT8-INT4
Update README.md
Browse files
README.md
CHANGED
|
@@ -20,8 +20,8 @@ pipeline_tag: text-generation
|
|
| 20 |
[Phi4-mini](https://huggingface.co/microsoft/Phi-4-mini-instruct) is quantized by the PyTorch team using [torchao](https://huggingface.co/docs/transformers/main/en/quantization/torchao) with 8-bit embeddings and 8-bit dynamic activations with 4-bit weight linears (INT8-INT4).
|
| 21 |
The model is suitable for mobile deployment with [ExecuTorch](https://github.com/pytorch/executorch).
|
| 22 |
|
| 23 |
-
We provide the [quantized pte](https://huggingface.co/pytorch/Phi-4-mini-instruct-INT8-INT4/blob/main/
|
| 24 |
-
(The provided pte file is exported with
|
| 25 |
|
| 26 |
# Running in a mobile app
|
| 27 |
The [pte file](https://huggingface.co/pytorch/Phi-4-mini-instruct-INT8-INT4/blob/main/phi4-mini-INT8-INT4.pte) can be run with ExecuTorch on a mobile phone. See the [instructions](https://pytorch.org/executorch/main/llm/llama-demo-ios.html) for doing this in iOS.
|
|
@@ -213,27 +213,29 @@ We can run the quantized model on a mobile phone using [ExecuTorch](https://gith
|
|
| 213 |
Once ExecuTorch is [set-up](https://pytorch.org/executorch/main/getting-started.html), exporting and running the model on device is a breeze.
|
| 214 |
|
| 215 |
We first convert the [quantized checkpoint](https://huggingface.co/pytorch/Phi-4-mini-instruct-INT8-INT4/blob/main/pytorch_model.bin) to one ExecuTorch's LLM export script expects by renaming some of the checkpoint keys.
|
| 216 |
-
The following script does this for you.
|
| 217 |
```Shell
|
| 218 |
-
|
|
|
|
| 219 |
```
|
| 220 |
|
| 221 |
Once the checkpoint is converted, we can export to ExecuTorch's pte format with the XNNPACK delegate.
|
| 222 |
-
The below command exports with a max_seq_length/max_context_length of
|
| 223 |
|
| 224 |
```Shell
|
| 225 |
-
|
| 226 |
-
python -m executorch.examples.models.llama.export_llama \
|
| 227 |
--model "phi_4_mini" \
|
| 228 |
-
--checkpoint
|
| 229 |
-
--params
|
|
|
|
| 230 |
-kv \
|
| 231 |
--use_sdpa_with_kv_cache \
|
| 232 |
-X \
|
| 233 |
-
--
|
| 234 |
-
--
|
| 235 |
-
--
|
| 236 |
-
--
|
|
|
|
| 237 |
```
|
| 238 |
|
| 239 |
After that you can run the model in a mobile app (see [Running in a mobile app](#running-in-a-mobile-app)).
|
|
|
|
| 20 |
[Phi4-mini](https://huggingface.co/microsoft/Phi-4-mini-instruct) is quantized by the PyTorch team using [torchao](https://huggingface.co/docs/transformers/main/en/quantization/torchao) with 8-bit embeddings and 8-bit dynamic activations with 4-bit weight linears (INT8-INT4).
|
| 21 |
The model is suitable for mobile deployment with [ExecuTorch](https://github.com/pytorch/executorch).
|
| 22 |
|
| 23 |
+
We provide the [quantized pte](https://huggingface.co/pytorch/Phi-4-mini-instruct-INT8-INT4/blob/main/model.pte) for direct use in ExecuTorch.
|
| 24 |
+
(The provided pte file is exported with at max_seq_length/max_context_length of 1024; if you wish to change this, re-export the quantized model following the instructions in [Exporting to ExecuTorch](#exporting-to-executorch).)
|
| 25 |
|
| 26 |
# Running in a mobile app
|
| 27 |
The [pte file](https://huggingface.co/pytorch/Phi-4-mini-instruct-INT8-INT4/blob/main/phi4-mini-INT8-INT4.pte) can be run with ExecuTorch on a mobile phone. See the [instructions](https://pytorch.org/executorch/main/llm/llama-demo-ios.html) for doing this in iOS.
|
|
|
|
| 213 |
Once ExecuTorch is [set-up](https://pytorch.org/executorch/main/getting-started.html), exporting and running the model on device is a breeze.
|
| 214 |
|
| 215 |
We first convert the [quantized checkpoint](https://huggingface.co/pytorch/Phi-4-mini-instruct-INT8-INT4/blob/main/pytorch_model.bin) to one ExecuTorch's LLM export script expects by renaming some of the checkpoint keys.
|
| 216 |
+
The following script does this for you.
|
| 217 |
```Shell
|
| 218 |
+
HF_MODEL_DIR=$(hf download pytorch/Phi-4-mini-instruct-INT8-INT4)
|
| 219 |
+
python -m executorch.examples.models.phi_4_mini.convert_weights $HF_MODEL_DIR pytorch_model_converted.bin
|
| 220 |
```
|
| 221 |
|
| 222 |
Once the checkpoint is converted, we can export to ExecuTorch's pte format with the XNNPACK delegate.
|
| 223 |
+
The below command exports with a max_seq_length/max_context_length of 1024, but it can be changed as desired.
|
| 224 |
|
| 225 |
```Shell
|
| 226 |
+
python-m executorch.examples.models.llama.export_llama \
|
|
|
|
| 227 |
--model "phi_4_mini" \
|
| 228 |
+
--checkpoint pytorch_model_converted.bin \
|
| 229 |
+
--params examples/models/phi_4_mini/config/config.json \
|
| 230 |
+
--output_name model.pte \
|
| 231 |
-kv \
|
| 232 |
--use_sdpa_with_kv_cache \
|
| 233 |
-X \
|
| 234 |
+
--xnnpack-extended-ops \
|
| 235 |
+
--max_context_length 1024 \
|
| 236 |
+
--max_seq_length 1024 \
|
| 237 |
+
--dtype fp32 \
|
| 238 |
+
--metadata '{"get_bos_id":199999, "get_eos_ids":[200020,199999]}'
|
| 239 |
```
|
| 240 |
|
| 241 |
After that you can run the model in a mobile app (see [Running in a mobile app](#running-in-a-mobile-app)).
|