Text Generation
Transformers
PyTorch
multilingual
phi3
torchao
phi
phi4
nlp
code
math
chat
conversational
custom_code
text-generation-inference
Instructions to use pytorch/Phi-4-mini-instruct-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-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-INT4", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("pytorch/Phi-4-mini-instruct-INT4", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("pytorch/Phi-4-mini-instruct-INT4", trust_remote_code=True) 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=40) 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-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-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-INT4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/pytorch/Phi-4-mini-instruct-INT4
- SGLang
How to use pytorch/Phi-4-mini-instruct-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-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-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-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-INT4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use pytorch/Phi-4-mini-instruct-INT4 with Docker Model Runner:
docker model run hf.co/pytorch/Phi-4-mini-instruct-INT4
Update README.md
#2
by supriyar - opened
README.md
CHANGED
|
@@ -17,11 +17,11 @@ base_model:
|
|
| 17 |
pipeline_tag: text-generation
|
| 18 |
---
|
| 19 |
|
| 20 |
-
[Phi4-mini](https://huggingface.co/microsoft/Phi-4-mini-instruct)
|
| 21 |
|
| 22 |
# Quantization Recipe
|
| 23 |
|
| 24 |
-
|
| 25 |
```
|
| 26 |
pip install git+https://github.com/huggingface/transformers@main
|
| 27 |
pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu126
|
|
@@ -29,7 +29,7 @@ pip install torch
|
|
| 29 |
pip install accelerate
|
| 30 |
```
|
| 31 |
|
| 32 |
-
|
| 33 |
```
|
| 34 |
import torch
|
| 35 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
|
|
@@ -144,7 +144,6 @@ lm_eval --model hf --model_args pretrained=pytorch/Phi-4-mini-instruct-int4wo-hq
|
|
| 144 |
|
| 145 |
# Peak Memory Usage
|
| 146 |
|
| 147 |
-
We can use the following code to get a sense of peak memory usage during inference:
|
| 148 |
|
| 149 |
## Results
|
| 150 |
|
|
@@ -155,6 +154,7 @@ We can use the following code to get a sense of peak memory usage during inferen
|
|
| 155 |
|
| 156 |
|
| 157 |
## Benchmark Peak Memory
|
|
|
|
| 158 |
|
| 159 |
```
|
| 160 |
import torch
|
|
@@ -198,8 +198,8 @@ print(f"Peak Memory Usage: {mem:.02f} GB")
|
|
| 198 |
|
| 199 |
# Model Performance
|
| 200 |
|
| 201 |
-
Our int4wo is only optimized for batch size 1, so
|
| 202 |
-
|
| 203 |
|
| 204 |
|
| 205 |
## Results (A100 machine)
|
|
|
|
| 17 |
pipeline_tag: text-generation
|
| 18 |
---
|
| 19 |
|
| 20 |
+
[Phi4-mini](https://huggingface.co/microsoft/Phi-4-mini-instruct) quantized with [torchao](https://huggingface.co/docs/transformers/main/en/quantization/torchao) int4 weight only quantization, by PyTorch team. Use it directly or serve using [vLLM](https://docs.vllm.ai/en/latest/). Get 67% VRAM reduction and 12-20% speedup on A100 GPUs.
|
| 21 |
|
| 22 |
# Quantization Recipe
|
| 23 |
|
| 24 |
+
Install the required packages:
|
| 25 |
```
|
| 26 |
pip install git+https://github.com/huggingface/transformers@main
|
| 27 |
pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu126
|
|
|
|
| 29 |
pip install accelerate
|
| 30 |
```
|
| 31 |
|
| 32 |
+
Use the following code to get the quantized model:
|
| 33 |
```
|
| 34 |
import torch
|
| 35 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
|
|
|
|
| 144 |
|
| 145 |
# Peak Memory Usage
|
| 146 |
|
|
|
|
| 147 |
|
| 148 |
## Results
|
| 149 |
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
## Benchmark Peak Memory
|
| 157 |
+
We can use the following code to get a sense of peak memory usage during inference:
|
| 158 |
|
| 159 |
```
|
| 160 |
import torch
|
|
|
|
| 198 |
|
| 199 |
# Model Performance
|
| 200 |
|
| 201 |
+
Our int4wo is only optimized for batch size 1, so expect some slowdown with larger batch sizes, we expect this to be used in local server deployment for single or a few users
|
| 202 |
+
where the decode tokens per second will matters more than the time to first token.
|
| 203 |
|
| 204 |
|
| 205 |
## Results (A100 machine)
|