test_gptq_dynamic.py 3.42 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
7
"""Tests whether gptq models with dynamic quantized can be loaded.

Run `pytest tests/quantization/test_gptq_dynamic.py --forked`.
"""

8
import os
9
10
11
12
13
import pytest
import torch

from vllm.model_executor.layers.linear import UnquantizedLinearMethod
from vllm.model_executor.layers.quantization.gptq import GPTQLinearMethod
14
from vllm.model_executor.layers.quantization.gptq_marlin import GPTQMarlinLinearMethod
15
from vllm.model_executor.layers.quantization.utils.gptq_utils import (
16
17
    get_dynamic_override,
)
18
from ..utils import models_path_prefix
19
20
21
22
23
24
25

PROMPT = "On the surface of Mars, we found"

# The first layer is quantized using bits=4, group_size=128
# The second layer is quantized using bits=8, group_size=32
# All other layers (layer index >= 2) are not quantized
MODEL_QUANT = [
26
    (os.path.join(models_path_prefix, "ModelCloud/Qwen1.5-1.8B-Chat-GPTQ-4bits-dynamic-cfg-with-lm_head-symTrue"), True),
27
    (
28
        os.path.join(models_path_prefix, "ModelCloud/Qwen1.5-1.8B-Chat-GPTQ-4bits-dynamic-cfg-with-lm_head-symFalse"),
29
30
        False,
    ),
31
32
33
34
]


@pytest.mark.parametrize("model_id, use_marlin_kernel", MODEL_QUANT)
35
36
37
def test_gptq_with_dynamic(
    vllm_runner, model_id: str, use_marlin_kernel: bool, monkeypatch
):
38
39
    # `LLM.apply_model` requires pickling a function.
    monkeypatch.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1")
40

41
42
43
    linear_method_cls = (
        GPTQMarlinLinearMethod if use_marlin_kernel else (GPTQLinearMethod)
    )
44

45
46
47
    with vllm_runner(
        model_id, dtype=torch.float16, max_model_len=2048, enforce_eager=True
    ) as llm:
48
49
50
51

        def check_model(model):
            for name, submodule in model.named_modules():
                if name == "lm_head":
52
53
                    assert isinstance(submodule.quant_method, linear_method_cls)
                elif name == "model.layers.0.self_attn.qkv_proj":
54
55
                    # The first layer is quantized using bits=4, group_size=128
                    # desc_act=True
56
                    assert isinstance(submodule.quant_method, linear_method_cls)
57
58
59
60
                    config = submodule.quant_method.quant_config
                    assert config.weight_bits == 4
                    assert config.group_size == 128
                    assert config.desc_act
61
                elif name == "model.layers.1.self_attn.qkv_proj":
62
63
                    # The second layer is quantized using bits=8, group_size=32
                    # desc_act=False
64
                    assert isinstance(submodule.quant_method, linear_method_cls)
65
                    config = submodule.quant_method.quant_config
66
67
68
69
70
71
72
                    assert (
                        get_dynamic_override(config, layer_name=name, key="bits") == 8
                    )
                    assert (
                        get_dynamic_override(config, layer_name=name, key="group_size")
                        == 32
                    )
73
                    assert not get_dynamic_override(
74
75
76
77
78
79
                        config, layer_name=name, key="desc_act"
                    )
                elif (
                    name == "model.layers.2.self_attn.qkv_proj"
                    or name == "model.layers.2.mlp.gate_up_proj"
                ):
80
                    # All other layers (layer index >= 2) are not quantized
81
                    assert isinstance(submodule.quant_method, UnquantizedLinearMethod)
82

83
        llm.apply_model(check_model)