test_lm_head.py 1.67 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
7
8
9
10
11
"""Tests whether gptq models with quantized lm_head can be loaded.

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

import pytest
import torch

from vllm.model_executor.layers.quantization.gptq import GPTQLinearMethod
12
from vllm.model_executor.layers.quantization.gptq_marlin import GPTQMarlinLinearMethod
13
from vllm.model_executor.layers.vocab_parallel_embedding import (
14
15
    UnquantizedEmbeddingMethod,
)
16
17
18

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

19
20
21
22
MODELS_QUANT = [
    ("ModelCloud/Qwen1.5-1.8B-Chat-GPTQ-4bits-dynamic-cfg-with-lm_head", True),
    ("TheBloke/TinyLlama-1.1B-Chat-v1.0-GPTQ", False),
]
23
24


25
@pytest.mark.parametrize("model_id, lm_head_quantized", MODELS_QUANT)
26
27
def test_lm_head(
    vllm_runner,
28
29
    model_id: str,
    lm_head_quantized: bool,
30
    monkeypatch,
31
) -> None:
32
33
    # `LLM.apply_model` requires pickling a function.
    monkeypatch.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1")
34
35
36
    with vllm_runner(
        model_id, dtype=torch.float16, max_model_len=2048, enforce_eager=True
    ) as vllm_model:
37
38
39
40

        def check_model(model):
            lm_head_layer = model.lm_head
            if lm_head_quantized:
41
42
43
44
                assert isinstance(
                    lm_head_layer.quant_method,
                    (GPTQLinearMethod, GPTQMarlinLinearMethod),
                )
45
            else:
46
47
48
                assert isinstance(
                    lm_head_layer.quant_method, UnquantizedEmbeddingMethod
                )
49
50
51

        vllm_model.apply_model(check_model)

52
        print(vllm_model.generate_greedy(["Hello my name is"], max_tokens=10)[0][1])