-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathexample_dbf_vllm_inference.py
More file actions
124 lines (97 loc) · 3.5 KB
/
Copy pathexample_dbf_vllm_inference.py
File metadata and controls
124 lines (97 loc) · 3.5 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
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Example: Quantize a model with DBF and run inference with vLLM
Performs the following steps:
1. Quantize with DBF
2. Save the quantized model
3. Load the quantized model with vLLM's offline LLM interface
4. Generate text
Requirements:
pip install vllm
Notes:
vLLM loads DBF models through OneComp's DBF plugin. This example uses the
naive DBF linear path, which is the supported DBF vLLM inference path.
The setting is applied before importing vLLM below.
vLLM runs a DeepGEMM (FP8) kernel warmup at engine startup even for
non-FP8 quantization such as DBF. 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 os
import torch
os.environ.setdefault("ONECOMP_DBF_NAIVE_LINEAR", "1")
from vllm import LLM, SamplingParams
from onecomp import DBF, CalibrationConfig, ModelConfig, Runner, setup_logger
def main():
setup_logger()
# Step 1: Quantize with DBF
save_dir = "./TinyLlama-1.1B-Chat-dbf"
model_config = ModelConfig(
model_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
)
quantizer = DBF(
target_bits=1.5,
# Keep the example compact. Increase these values for quality-focused runs.
iters=10,
balance_iters=5,
)
calibration_config = CalibrationConfig(
num_calibration_samples=32,
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()