-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexample_gptq_vllm_inference.py
More file actions
111 lines (87 loc) · 3.14 KB
/
Copy pathexample_gptq_vllm_inference.py
File metadata and controls
111 lines (87 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
Example: Quantize a model with OneComp and run inference with vLLM
Performs the following steps:
1. Quantize with GPTQ (4-bit, groupsize=128)
2. Save the quantized model
3. Load the quantized model with vLLM's offline LLM interface
4. Generate text
Requirements:
pip install vllm
Note:
vLLM runs a DeepGEMM (FP8) kernel warmup at engine startup even for
non-FP8 quantization such as GPTQ. If ``deep_gemm`` is not installed this
fails with ``RuntimeError: DeepGEMM backend is not available or outdated``.
OneComp-quantized models do not need DeepGEMM, so disable the FP8 path
before running this script::
export VLLM_USE_DEEP_GEMM=0
export VLLM_DEEP_GEMM_WARMUP=skip
See docs/user-guide/vllm-inference.md (Troubleshooting) for details.
Copyright 2025-2026 Fujitsu Ltd.
Author: Keiji Kimura
"""
import gc
import torch
from vllm import LLM, SamplingParams
from onecomp import GPTQ, CalibrationConfig, ModelConfig, Runner, setup_logger
def main():
setup_logger()
# Step 1: Quantize with GPTQ
save_dir = "./TinyLlama-1.1B-Chat-gptq-4bit"
model_config = ModelConfig(
model_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
)
quantizer = GPTQ(wbits=4, groupsize=128)
calibration_config = CalibrationConfig(
num_calibration_samples=128,
max_length=512,
)
runner = Runner(
model_config=model_config,
quantizer=quantizer,
calibration_config=calibration_config,
qep=False,
)
# NOTE: The calibration settings above are kept compact so the demo
# runs fast and may be insufficient for real quantisation. For
# higher quality, prefer the CalibrationConfig() defaults
# (max_length=2048, num_calibration_samples=512).
# For qep=False runs with large calibration data, also pass
# ``batch_size`` as a CalibrationConfig argument, e.g.
# CalibrationConfig(
# max_length=2048,
# num_calibration_samples=512,
# batch_size=128,
# )
# so that Runner.quantize_with_calibration_chunked runs instead of
# a single all-at-once forward pass.
runner.run()
# Step 2: Save the quantized model
runner.save_quantized_model(save_dir)
# Free GPU memory used by quantization before loading vLLM
del runner
gc.collect()
torch.cuda.empty_cache()
# Step 3: Load the quantized model with vLLM.
# gpu_memory_utilization=0.78 leaves headroom for the residual
# quantizer process (~16 GiB) on a UMA 121.7 GiB device (e.g. DGX
# Spark / GB200). The vLLM default 0.92 cgroup-OOMs on shared-memory
# GPUs.
llm = LLM(
model=save_dir,
max_model_len=512,
dtype="float16",
enforce_eager=True,
gpu_memory_utilization=0.78,
)
# Step 4: Generate text
prompts = [
"Explain what post-training quantization is in one sentence:",
"The capital of France is",
]
outputs = llm.generate(prompts, SamplingParams(max_tokens=64, temperature=0.0))
for output in outputs:
print(f"Prompt: {output.prompt}")
print(f"Response: {output.outputs[0].text}")
print()
if __name__ == "__main__":
main()