test_lm_head.py 2.11 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
"""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
10
import os
11
12
13
14
15

from vllm.model_executor.layers.quantization.gptq import GPTQLinearMethod
from vllm.model_executor.layers.quantization.gptq_marlin import (
    GPTQMarlinLinearMethod)
from vllm.model_executor.layers.quantization.marlin import MarlinLinearMethod
16
17
from vllm.model_executor.layers.vocab_parallel_embedding import (
    UnquantizedEmbeddingMethod)
18
from ..utils import models_path_prefix
19
20
21

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

22
MODELS_QUANT = [
zhuwenwen's avatar
zhuwenwen committed
23
24
    (os.path.join(models_path_prefix, "ModelCloud/Qwen1.5-1.8B-Chat-GPTQ-4bits-dynamic-cfg-with-lm_head"), True),
    (os.path.join(models_path_prefix, "ModelCloud/TinyLlama-1.1B-Chat-v1.0-GPTQ-4bit-10-25-2024"), False),
25
    # (os.path.join(models_path_prefix, "TheBloke/TinyLlama-1.1B-Chat-v1.0-GPTQ"), False),
zhuwenwen's avatar
zhuwenwen committed
26
    # (os.path.join(models_path_prefix, "neuralmagic/Meta-Llama-3-8B-Instruct-FP8"), False)
27
]
28
29


30
@pytest.mark.parametrize("model_id, lm_head_quantized", MODELS_QUANT)
31
32
def test_lm_head(
    vllm_runner,
33
34
    model_id: str,
    lm_head_quantized: bool,
35
    monkeypatch,
36
) -> None:
37
38
    # vllm_runner.apply_model() relies on V0 internals.
    monkeypatch.setenv("VLLM_USE_V1", "0")
39
    with vllm_runner(model_id, dtype=torch.float16,
40
41
42
43
44
                     max_model_len=2048) as vllm_model:

        def check_model(model):
            lm_head_layer = model.lm_head
            if lm_head_quantized:
45
                assert isinstance(lm_head_layer.quant_method,
46
47
48
                                  (GPTQLinearMethod, GPTQMarlinLinearMethod,
                                   MarlinLinearMethod))
            else:
49
                assert isinstance(lm_head_layer.quant_method,
50
51
52
53
54
55
                                  UnquantizedEmbeddingMethod)

        vllm_model.apply_model(check_model)

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