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
|
@@ -31,7 +31,7 @@ On iPhone 15 Pro, the model runs at 17.3 tokens/sec and uses 3206 Mb of memory.
|
|
| 31 |
# Quantization Recipe
|
| 32 |
|
| 33 |
First need to install the required packages:
|
| 34 |
-
```
|
| 35 |
pip install git+https://github.com/huggingface/transformers@main
|
| 36 |
pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu126
|
| 37 |
```
|
|
@@ -39,7 +39,7 @@ pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/c
|
|
| 39 |
## Untie Embedding Weights
|
| 40 |
Before quantization, since we need quantize input embedding and unembedding (lm_head) layer which are tied, but we want to quantize them separately, we first need to untie the model:
|
| 41 |
|
| 42 |
-
```
|
| 43 |
from transformers import (
|
| 44 |
AutoModelForCausalLM,
|
| 45 |
AutoProcessor,
|
|
@@ -73,7 +73,7 @@ tokenizer.push_to_hub(save_to)
|
|
| 73 |
|
| 74 |
We used following code to get the quantized model:
|
| 75 |
|
| 76 |
-
```
|
| 77 |
from transformers import (
|
| 78 |
AutoModelForCausalLM,
|
| 79 |
AutoProcessor,
|
|
@@ -156,12 +156,12 @@ We rely on [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-h
|
|
| 156 |
Need to install lm-eval from source: https://github.com/EleutherAI/lm-evaluation-harness#install
|
| 157 |
|
| 158 |
## baseline
|
| 159 |
-
```
|
| 160 |
lm_eval --model hf --model_args pretrained=microsoft/Phi-4-mini-instruct --tasks hellaswag --device cuda:0 --batch_size 64
|
| 161 |
```
|
| 162 |
|
| 163 |
## int8 dynamic activation and int4 weight quantization (8da4w)
|
| 164 |
-
```
|
| 165 |
lm_eval --model hf --model_args pretrained=pytorch/Phi-4-mini-instruct-8da4w --tasks hellaswag --device cuda:0 --batch_size 64
|
| 166 |
```
|
| 167 |
|
|
@@ -195,13 +195,13 @@ Once ExecuTorch is [set-up](https://pytorch.org/executorch/main/getting-started.
|
|
| 195 |
|
| 196 |
We first convert the [quantized checkpoint](https://huggingface.co/pytorch/Phi-4-mini-instruct-8da4w/blob/main/pytorch_model.bin) to one ExecuTorch's LLM export script expects by renaming some of the checkpoint keys.
|
| 197 |
The following script does this for you. We have uploaded the converted checkpoint [phi4-mini-8da4w-converted.bin](https://huggingface.co/pytorch/Phi-4-mini-instruct-8da4w/blob/main/phi4-mini-8da4w-converted.bin) for convenience.
|
| 198 |
-
```
|
| 199 |
python -m executorch.examples.models.phi_4_mini.convert_weights pytorch_model.bin phi4-mini-8da4w-converted.bin
|
| 200 |
```
|
| 201 |
|
| 202 |
Once the checkpoint is converted, we can export to ExecuTorch's PTE format with the XNNPACK delegate.
|
| 203 |
|
| 204 |
-
```
|
| 205 |
PARAMS="executorch/examples/models/phi_4_mini/config.json"
|
| 206 |
python -m executorch.examples.models.llama.export_llama \
|
| 207 |
--model "phi_4_mini" \
|
|
|
|
| 31 |
# Quantization Recipe
|
| 32 |
|
| 33 |
First need to install the required packages:
|
| 34 |
+
```Shell
|
| 35 |
pip install git+https://github.com/huggingface/transformers@main
|
| 36 |
pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu126
|
| 37 |
```
|
|
|
|
| 39 |
## Untie Embedding Weights
|
| 40 |
Before quantization, since we need quantize input embedding and unembedding (lm_head) layer which are tied, but we want to quantize them separately, we first need to untie the model:
|
| 41 |
|
| 42 |
+
```Py
|
| 43 |
from transformers import (
|
| 44 |
AutoModelForCausalLM,
|
| 45 |
AutoProcessor,
|
|
|
|
| 73 |
|
| 74 |
We used following code to get the quantized model:
|
| 75 |
|
| 76 |
+
```Py
|
| 77 |
from transformers import (
|
| 78 |
AutoModelForCausalLM,
|
| 79 |
AutoProcessor,
|
|
|
|
| 156 |
Need to install lm-eval from source: https://github.com/EleutherAI/lm-evaluation-harness#install
|
| 157 |
|
| 158 |
## baseline
|
| 159 |
+
```Shell
|
| 160 |
lm_eval --model hf --model_args pretrained=microsoft/Phi-4-mini-instruct --tasks hellaswag --device cuda:0 --batch_size 64
|
| 161 |
```
|
| 162 |
|
| 163 |
## int8 dynamic activation and int4 weight quantization (8da4w)
|
| 164 |
+
```Shell
|
| 165 |
lm_eval --model hf --model_args pretrained=pytorch/Phi-4-mini-instruct-8da4w --tasks hellaswag --device cuda:0 --batch_size 64
|
| 166 |
```
|
| 167 |
|
|
|
|
| 195 |
|
| 196 |
We first convert the [quantized checkpoint](https://huggingface.co/pytorch/Phi-4-mini-instruct-8da4w/blob/main/pytorch_model.bin) to one ExecuTorch's LLM export script expects by renaming some of the checkpoint keys.
|
| 197 |
The following script does this for you. We have uploaded the converted checkpoint [phi4-mini-8da4w-converted.bin](https://huggingface.co/pytorch/Phi-4-mini-instruct-8da4w/blob/main/phi4-mini-8da4w-converted.bin) for convenience.
|
| 198 |
+
```Shell
|
| 199 |
python -m executorch.examples.models.phi_4_mini.convert_weights pytorch_model.bin phi4-mini-8da4w-converted.bin
|
| 200 |
```
|
| 201 |
|
| 202 |
Once the checkpoint is converted, we can export to ExecuTorch's PTE format with the XNNPACK delegate.
|
| 203 |
|
| 204 |
+
```Shell
|
| 205 |
PARAMS="executorch/examples/models/phi_4_mini/config.json"
|
| 206 |
python -m executorch.examples.models.llama.export_llama \
|
| 207 |
--model "phi_4_mini" \
|