test_beam_search.py 2.08 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
3
"""Compare the outputs of HF and vLLM when using beam search.

4
Run `pytest tests/samplers/test_beam_search.py`.
5
"""
6

7
8
import pytest

9
10
11
12
13
14
15

@pytest.fixture(autouse=True)
def v1(run_with_both_engines):
    """We can run both engines for this test."""
    pass


16
17
18
19
# FIXME(zhuohan): The test can not pass if we:
#   1. Increase max_tokens to 256.
#   2. Increase beam_width to 8.
#   3. Use the model "huggyllama/llama-7b".
20
MAX_TOKENS = [64]
21
BEAM_WIDTHS = [4]
22
MODELS = ["TinyLlama/TinyLlama-1.1B-Chat-v1.0"]
23
24


25
@pytest.mark.skip_v1  # FIXME: This fails on V1 right now.
26
27
28
29
30
31
32
33
34
35
36
37
38
@pytest.mark.parametrize("model", MODELS)
@pytest.mark.parametrize("dtype", ["half"])
@pytest.mark.parametrize("max_tokens", MAX_TOKENS)
@pytest.mark.parametrize("beam_width", BEAM_WIDTHS)
def test_beam_search_single_input(
    hf_runner,
    vllm_runner,
    example_prompts,
    model: str,
    dtype: str,
    max_tokens: int,
    beam_width: int,
) -> None:
39
    example_prompts = example_prompts[:1]
40
41
42
    with hf_runner(model, dtype=dtype) as hf_model:
        hf_outputs = hf_model.generate_beam_search(example_prompts, beam_width,
                                                   max_tokens)
43

44
    with vllm_runner(model, dtype=dtype) as vllm_model:
45
46
        vllm_outputs = vllm_model.generate_beam_search(example_prompts,
                                                       beam_width, max_tokens)
47
48

    for i in range(len(example_prompts)):
49
50
51
52
53
54
55
56
57
        hf_output_ids, hf_output_texts = hf_outputs[i]
        vllm_output_ids, vllm_output_texts = vllm_outputs[i]
        for i, (hf_text,
                vllm_text) in enumerate(zip(hf_output_texts,
                                            vllm_output_texts)):
            print(f">>>{i}-th hf output:")
            print(hf_text)
            print(f">>>{i}-th vllm output:")
            print(vllm_text)
58
59
60
61
62
        assert len(hf_output_ids) == len(vllm_output_ids)
        for j in range(len(hf_output_ids)):
            assert hf_output_ids[j] == vllm_output_ids[j], (
                f"Test{i} output{j}:\nHF: {hf_output_ids}\n"
                f"vLLM: {vllm_output_ids}")