test_torchao.py 8.4 KB
Newer Older
Driss Guessous's avatar
Driss Guessous committed
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
Driss Guessous's avatar
Driss Guessous committed
3
4
5
6
import importlib.metadata
import importlib.util

import pytest
7
import torch
Driss Guessous's avatar
Driss Guessous committed
8
9
10
11
12
13
14
15

DTYPE = ["bfloat16"]

TORCHAO_AVAILABLE = importlib.util.find_spec("torchao") is not None


@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
def test_pre_quantized_model(vllm_runner):
16
17
18
19
20
21
    with vllm_runner(
        "drisspg/fp8-opt-125m",
        quantization="torchao",
        dtype="bfloat16",
        enforce_eager=True,
    ) as llm:
22
        output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
Driss Guessous's avatar
Driss Guessous committed
23
24
25
    assert output


26
27
28
29
30
31
@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
@pytest.mark.parametrize(
    "pt_load_map_location",
    [
        "cuda:0",
        # {"": "cuda"},
32
33
34
    ],
)
def test_opt_125m_int8wo_model_loading_with_params(vllm_runner, pt_load_map_location):
35
    torch._dynamo.reset()
36
    model_name = "jerryzh168/opt-125m-int8wo-partial-quant"
37
38
39
40
41
    with vllm_runner(
        model_name=model_name,
        quantization="torchao",
        dtype="bfloat16",
        pt_load_map_location=pt_load_map_location,
42
        enforce_eager=True,
43
    ) as llm:
44
        output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
45
46
47
48

        assert output


49
50
51
52
@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
def test_opt_125m_int4wo_model_per_module_quant(vllm_runner):
    torch._dynamo.reset()
    model_name = "jerryzh168/opt-125m-int4wo-per-module"
53
54
55
56
57
    with vllm_runner(
        model_name=model_name,
        quantization="torchao",
        dtype="bfloat16",
        pt_load_map_location="cuda:0",
58
        enforce_eager=True,
59
    ) as llm:
60
        output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
61
62
63
64

        assert output


65
66
67
68
@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
def test_qwenvl_int8wo_model_loading_with_params(vllm_runner):
    torch._dynamo.reset()
    model_name = "mobicham/Qwen2.5-VL-3B-Instruct_int8wo_ao"
69
70
71
72
73
    with vllm_runner(
        model_name=model_name,
        quantization="torchao",
        dtype="bfloat16",
        pt_load_map_location="cuda:0",
74
        enforce_eager=True,
75
    ) as llm:
76
        output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
77
78
79
80

        assert output


81
82
83
84
@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
@pytest.mark.skip(
    reason="since torchao nightly is only compatible with torch nightly"
    "currently https://github.com/pytorch/ao/issues/2919, we'll have to skip "
85
86
    "torchao tests that requires newer versions (0.14.0.dev+) for now"
)
87
88
def test_opt_125m_awq_int4wo_model_loading_with_params(vllm_runner):
    torch._dynamo.reset()
89
90
91
92
93
94
95
    model_name = "torchao-testing/opt-125m-AWQConfig-Int4WeightOnlyConfig-v2-0.14.0.dev"
    with vllm_runner(
        model_name=model_name,
        quantization="torchao",
        dtype="bfloat16",
        pt_load_map_location="cuda:0",
    ) as llm:
96
        output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
97
98

        assert output
99
100
101
102
103
104
105
106
107
108
109
110
111


@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
def test_on_the_fly_quant_config_dict_json(vllm_runner):
    """Testing on the fly quantization, load_weights integration point,
    with config dict serialized to json string
    """
    torch._dynamo.reset()
    model_name = "facebook/opt-125m"

    import json

    from torchao.core.config import config_to_dict
112
    from torchao.quantization import Float8DynamicActivationFloat8WeightConfig, PerRow
113
114

    torchao_quant_config = Float8DynamicActivationFloat8WeightConfig(
115
116
        granularity=PerRow()
    )
117
    hf_overrides = {
118
119
120
        "quantization_config_dict_json": json.dumps(
            config_to_dict(torchao_quant_config)
        )
121
    }
122
123
124
125
126
127
    with vllm_runner(
        model_name=model_name,
        dtype="bfloat16",
        pt_load_map_location="cuda:0",
        quantization="torchao",
        hf_overrides=hf_overrides,
128
        enforce_eager=True,
129
    ) as llm:
130
        output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

        assert output


@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
def test_on_the_fly_quant_config_file(vllm_runner):
    """Testing on the fly quantization, load_weights integration point,
    with config file
    """
    torch._dynamo.reset()
    model_name = "facebook/opt-125m"
    import json
    from tempfile import NamedTemporaryFile

    from torchao.core.config import config_to_dict
146
    from torchao.quantization import Float8DynamicActivationFloat8WeightConfig, PerRow
147
148
149
150
151
152
153
154
155
156

    config = Float8DynamicActivationFloat8WeightConfig(granularity=PerRow())

    with NamedTemporaryFile(mode="w", delete=False) as f:
        f.write(json.dumps(config_to_dict(config)))
        # close the file to save it
        f.close()
        config_file_name = str(f.name)

        hf_overrides = {"quantization_config_file": config_file_name}
157
158
159
160
161
162
        with vllm_runner(
            model_name=model_name,
            dtype="bfloat16",
            pt_load_map_location="cuda:0",
            quantization="torchao",
            hf_overrides=hf_overrides,
163
            enforce_eager=True,
164
        ) as llm:
165
            output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
166
167
168
169
170
171
172
173
174

            assert output


@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
def test_reload_weights():
    import json

    from torchao.core.config import config_to_dict
175
    from torchao.quantization import Float8DynamicActivationFloat8WeightConfig, PerRow
176
177
178
179

    from vllm import LLM, SamplingParams

    torchao_quant_config = Float8DynamicActivationFloat8WeightConfig(
180
181
        granularity=PerRow()
    )
182
183

    hf_overrides = {
184
185
186
        "quantization_config_dict_json": json.dumps(
            config_to_dict(torchao_quant_config)
        )
187
188
189
190
191
192
193
194
195
196
197
    }

    llm = LLM(
        model="Qwen/Qwen3-0.6B",
        dtype="bfloat16",
        load_format="dummy",
        enforce_eager=True,
        quantization="torchao",
        hf_overrides=hf_overrides,
    )
    # Update load format from `dummy` to `auto`
198
199
200
    llm.collective_rpc(
        "update_config", args=({"load_config": {"load_format": "auto"}},)
    )
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    # Now reload real weights inplace
    llm.collective_rpc("reload_weights")
    prompts = [
        "Hello, my name is",
        "The president of the United States is",
        "The capital of France is",
        "The future of AI is",
    ]
    # Create a sampling params object.
    sampling_params = SamplingParams(temperature=0, top_p=0.95)
    outputs = llm.generate(prompts, sampling_params)
    # make sure it runs
    for output in outputs:
        generated_text = output.outputs[0].text
        assert generated_text
        # can also uncomment locally to make sure the generated
        # output makes sense
        # prompt = output.prompt
        # print(f"Prompt:    {prompt!r}")
        # print(f"Output:    {generated_text!r}")
        # print("-" * 60)
222
223


224
225
226
227
228
229
230
231
232
233
234
235
@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
@pytest.mark.skip(
    reason="since torchao nightly is only compatible with torch nightly"
    "currently https://github.com/pytorch/ao/issues/2919, we'll have to skip "
    "torchao tests that requires newer versions (0.14.0.dev+) for now"
)
def test_opt_125m_float8_weight_only_safetensors_model_loading_with_params(vllm_runner):
    torch._dynamo.reset()
    model_name = (
        "torchao-testing/opt-125m-Float8WeightOnlyConfig-v2-0.14.0.dev-safetensors"
    )
    with vllm_runner(model_name=model_name, dtype="bfloat16") as llm:
236
        output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
237
238
239
240

        assert output


241
242
243
244
245
246
247
248
249
250
251
252
@pytest.mark.skipif(not TORCHAO_AVAILABLE, reason="torchao is not available")
@pytest.mark.skip(
    reason="since torchao nightly is only compatible with torch nightly"
    "currently https://github.com/pytorch/ao/issues/2919, we'll have to skip "
    "torchao tests that requires newer versions (0.14.0.dev+) for now"
)
def test_opt_125m_module_fqn_to_config_regex_model(vllm_runner):
    torch._dynamo.reset()
    model_name = "torchao-testing/opt-125m-ModuleFqnToConfig-v1-regex-0.14.0.dev"
    with vllm_runner(
        model_name=model_name, dtype="bfloat16", pt_load_map_location="cuda:0"
    ) as llm:
253
        output = llm.generate_greedy(["The capital of France is"], max_tokens=4)
254
255
256
257

        assert output


Driss Guessous's avatar
Driss Guessous committed
258
259
if __name__ == "__main__":
    pytest.main([__file__])