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
|
@@ -33,18 +33,11 @@ from transformers import (
|
|
| 33 |
AutoModelForCausalLM,
|
| 34 |
AutoProcessor,
|
| 35 |
AutoTokenizer,
|
| 36 |
-
TorchAoConfig,
|
| 37 |
-
)
|
| 38 |
-
from torchao.quantization.quant_api import (
|
| 39 |
-
IntxWeightOnlyConfig,
|
| 40 |
-
Int8DynamicActivationIntxWeightConfig,
|
| 41 |
-
AOPerModuleConfig
|
| 42 |
)
|
| 43 |
-
from torchao.quantization.granularity import PerGroup, PerAxis
|
| 44 |
import torch
|
| 45 |
|
| 46 |
model_id = "microsoft/Phi-4-mini-instruct"
|
| 47 |
-
untied_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=
|
| 48 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 49 |
|
| 50 |
print(untied_model)
|
|
@@ -54,7 +47,7 @@ if getattr(untied_model.config.get_text_config(decoder=True), "tie_word_embeddin
|
|
| 54 |
setattr(untied_model.config.get_text_config(decoder=True), "tie_word_embeddings", False)
|
| 55 |
|
| 56 |
untied_model._tied_weights_keys = []
|
| 57 |
-
untied_model.lm_head.weight = torch.nn.Parameter(
|
| 58 |
|
| 59 |
print("tied weights:", find_tied_parameters(untied_model))
|
| 60 |
|
|
@@ -91,7 +84,6 @@ USER_ID = "YOUR_USER_ID"
|
|
| 91 |
MODEL_NAME = model_id.split("/")[-1]
|
| 92 |
untied_model_id = f"{USER_ID}/{MODEL_NAME}-untied-weights"
|
| 93 |
|
| 94 |
-
|
| 95 |
embedding_config = IntxWeightOnlyConfig(
|
| 96 |
weight_dtype=torch.int8,
|
| 97 |
granularity=PerAxis(0),
|
|
@@ -101,7 +93,11 @@ linear_config = Int8DynamicActivationIntxWeightConfig(
|
|
| 101 |
weight_granularity=PerGroup(32),
|
| 102 |
weight_scale_dtype=torch.bfloat16,
|
| 103 |
)
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 106 |
|
| 107 |
# TODO: use AOPerModuleConfig once fix for tied weights is landed
|
|
|
|
| 33 |
AutoModelForCausalLM,
|
| 34 |
AutoProcessor,
|
| 35 |
AutoTokenizer,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
|
|
|
| 37 |
import torch
|
| 38 |
|
| 39 |
model_id = "microsoft/Phi-4-mini-instruct"
|
| 40 |
+
untied_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
|
| 41 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 42 |
|
| 43 |
print(untied_model)
|
|
|
|
| 47 |
setattr(untied_model.config.get_text_config(decoder=True), "tie_word_embeddings", False)
|
| 48 |
|
| 49 |
untied_model._tied_weights_keys = []
|
| 50 |
+
untied_model.lm_head.weight = torch.nn.Parameter(untied_model.lm_head.weight.clone())
|
| 51 |
|
| 52 |
print("tied weights:", find_tied_parameters(untied_model))
|
| 53 |
|
|
|
|
| 84 |
MODEL_NAME = model_id.split("/")[-1]
|
| 85 |
untied_model_id = f"{USER_ID}/{MODEL_NAME}-untied-weights"
|
| 86 |
|
|
|
|
| 87 |
embedding_config = IntxWeightOnlyConfig(
|
| 88 |
weight_dtype=torch.int8,
|
| 89 |
granularity=PerAxis(0),
|
|
|
|
| 93 |
weight_granularity=PerGroup(32),
|
| 94 |
weight_scale_dtype=torch.bfloat16,
|
| 95 |
)
|
| 96 |
+
|
| 97 |
+
quant_config = AOPerModuleConfig({"_default": linear_config, "model.embed_tokens": embedding_config})
|
| 98 |
+
quantization_config = TorchAoConfig(quant_type=quant_config, include_embedding=True, untie_embedding_weights=True, modules_to_not_convert=[])
|
| 99 |
+
|
| 100 |
+
quantized_model = AutoModelForCausalLM.from_pretrained(untied_model_id, torch_dtype=torch.float32, device_map="auto", quantization_config=quantization_config)
|
| 101 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 102 |
|
| 103 |
# TODO: use AOPerModuleConfig once fix for tied weights is landed
|