This repository hosts the Phi4-mini-instruct model quantized with torchao using int4 weight-only quantization and the hqq algorithm. This work is brought to you by the PyTorch team. This model can be used directly or served using vLLM for 67% VRAM reduction (2.98 GB needed) and speedup on A100 GPUs.
Inference with vLLM
Install vllm nightly and torchao nightly to get some recent changes:
1from vllm import LLM, SamplingParams
23# Sample prompts.
4prompts = [
5 "Hello, my name is",
6 "The president of the United States is",
7 "The capital of France is",
8 "The future of AI is",
9]
10# Create a sampling params object.
11sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
121314if __name__ == '__main__':
15 # Create an LLM.
16 llm = LLM(model="pytorch/Phi-4-mini-instruct-INT4")
17 # Generate texts from the prompts.
18 # The output is a list of RequestOutput objects
19 # that contain the prompt, generated text, and other information.
20 outputs = llm.generate(prompts, sampling_params)
21 # Print the outputs.
22 print("
23Generated Outputs:
24" + "-" * 60)
25 for output in outputs:
26 prompt = output.prompt
27 generated_text = output.outputs[0].text
28 print(f"Prompt: {prompt!r}")
29 print(f"Output: {generated_text!r}")
30 print("-" * 60)
Note: please use VLLM_DISABLE_COMPILE_CACHE=1 to disable compile cache when running this code, e.g. VLLM_DISABLE_COMPILE_CACHE=1 python example.py, since there are some issues with the composability of compile in vLLM and torchao,
this is expected be resolved in pytorch 2.8.
Our INT4 model 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 where the decode tokens per second will matters more than the time to first token.
Results (A100 machine)
Benchmark (Latency)
Phi-4 mini-Ins
phi4-mini-INT4
latency (batch_size=1)
2.46s
2.2s (1.12x speedup)
Results (H100 machine)
Benchmark (Latency)
Phi-4 mini-Ins
phi4-mini-INT4
latency (batch_size=1)
1.61s
1.08s (1.49x speedup)
Note the result of latency (benchmark_latency) is in seconds, and serving (benchmark_serving) is in number of requests per second.
Int4 weight only is optimized for batch size 1 and short input and output token length, please stay tuned for models optimized for larger batch sizes or longer token length.
Abstract: We present TorchAO, a PyTorch-native model optimization framework leveraging quantization and sparsity to provide an end-to-end, training-to-serving workflow for AI models. TorchAO supports a variety of popular model optimization techniques, including FP8 quantized training, quantization-aware training (QAT), post-training quantization (PTQ), and 2:4 sparsity, and leverages a novel tensor subclass abstraction to represent a variety of widely-used, backend agnostic low precision data types, including INT4, INT8, FP8, MXFP4, MXFP6, and MXFP8. TorchAO integrates closely with the broader ecosystem at each step of the model optimization pipeline, from pre-training (TorchTitan) to fine-tuning (TorchTune, Axolotl) to serving (HuggingFace, vLLM, SGLang, ExecuTorch), connecting an otherwise fragmented space in a single, unified workflow. TorchAO has enabled recent launches of the quantized Llama 3.2 1B/3B and LlamaGuard3-8B models and is open-source at this https URL .
PyTorch has not performed safety evaluations or red teamed the quantized models. Performance characteristics, outputs, and behaviors may differ from the original models. Users are solely responsible for selecting appropriate use cases, evaluating and mitigating for accuracy, safety, and fairness, ensuring security, and complying with all applicable laws and regulations.
Nothing contained in this Model Card should be interpreted as or deemed a restriction or modification to the licenses the models are released under, including any limitations of liability or disclaimers of warranties provided therein.