test_mistral.py 4.52 KB
Newer Older
1
2
"""Compare the outputs of HF and vLLM for Mistral models using greedy sampling.

3
Run `pytest tests/models/test_mistral.py`.
4
5
6
"""
import pytest

7
8
from vllm import SamplingParams

9
from ...utils import check_logprobs_close
10

11
12
MODELS = [
    "mistralai/Mistral-7B-Instruct-v0.1",
13
    "mistralai/Mistral-7B-Instruct-v0.3",
14
15
    # Mistral-Nemo is to big for CI, but passes locally
    # "mistralai/Mistral-Nemo-Instruct-2407"
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
SAMPLING_PARAMS = SamplingParams(max_tokens=512, temperature=0.0, logprobs=5)

# for function calling
TOOLS = [{
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type":
                    "string",
                    "description":
                    "The city to find the weather for, e.g. 'San Francisco'"
                },
                "state": {
                    "type":
                    "string",
                    "description":
                    "the two-letter abbreviation for the state that the city is"
                    " in, e.g. 'CA' which would mean 'California'"
                },
                "unit": {
                    "type": "string",
                    "description": "The unit to fetch the temperature in",
                    "enum": ["celsius", "fahrenheit"]
                }
            },
            "required": ["city", "state", "unit"]
        }
    }
}]
MSGS = [{
    "role":
    "user",
    "content": ("Can you tell me what the temperate"
                " will be in Dallas, in fahrenheit?")
}]
EXPECTED_FUNC_CALL = (
    '[{"name": "get_current_weather", "arguments": '
    '{"city": "Dallas", "state": "TX", "unit": "fahrenheit"}}]')

62
63
64

@pytest.mark.parametrize("model", MODELS)
@pytest.mark.parametrize("dtype", ["bfloat16"])
65
66
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
67
68
69
def test_models(
    hf_runner,
    vllm_runner,
70
    example_prompts,
71
72
73
    model: str,
    dtype: str,
    max_tokens: int,
74
    num_logprobs: int,
75
) -> None:
76
    # TODO(sang): Sliding window should be tested separately.
77
78
79
    with hf_runner(model, dtype=dtype) as hf_model:
        hf_outputs = hf_model.generate_greedy_logprobs_limit(
            example_prompts, max_tokens, num_logprobs)
80

81
82
    with vllm_runner(model, dtype=dtype,
                     tokenizer_mode="mistral") as vllm_model:
83
84
        vllm_outputs = vllm_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)
85

86
87
88
89
90
91
    check_logprobs_close(
        outputs_0_lst=hf_outputs,
        outputs_1_lst=vllm_outputs,
        name_0="hf",
        name_1="vllm",
    )
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
125
126
127
128
129
130
131


@pytest.mark.parametrize("model", MODELS[1:])
@pytest.mark.parametrize("dtype", ["bfloat16"])
@pytest.mark.parametrize("max_tokens", [64])
@pytest.mark.parametrize("num_logprobs", [5])
def test_mistral_format(
    vllm_runner,
    example_prompts,
    model: str,
    dtype: str,
    max_tokens: int,
    num_logprobs: int,
) -> None:
    with vllm_runner(
            model,
            dtype=dtype,
            tokenizer_mode="auto",
            load_format="safetensors",
            config_format="hf",
    ) as hf_format_model:
        hf_format_outputs = hf_format_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)

    with vllm_runner(
            model,
            dtype=dtype,
            tokenizer_mode="mistral",
            load_format="mistral",
            config_format="mistral",
    ) as mistral_format_model:
        mistral_format_outputs = mistral_format_model.generate_greedy_logprobs(
            example_prompts, max_tokens, num_logprobs)

    check_logprobs_close(
        outputs_0_lst=hf_format_outputs,
        outputs_1_lst=mistral_format_outputs,
        name_0="hf",
        name_1="mistral",
    )
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150


@pytest.mark.parametrize("dtype", ["bfloat16"])
@pytest.mark.parametrize("model", MODELS[1:])  # v1 can't do func calling
def test_mistral_function_calling(
    vllm_runner,
    model: str,
    dtype: str,
) -> None:
    with vllm_runner(model,
                     dtype=dtype,
                     tokenizer_mode="mistral",
                     config_format="mistral",
                     load_format="mistral") as vllm_model:
        outputs = vllm_model.model.chat(MSGS,
                                        tools=TOOLS,
                                        sampling_params=SAMPLING_PARAMS)

        assert outputs[0].outputs[0].text.strip() == EXPECTED_FUNC_CALL