2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2023-09-04 17:29:42 -07:00
|
|
|
"""Compare the outputs of HF and vLLM when using beam search.
|
|
|
|
|
2024-03-29 13:06:40 +09:00
|
|
|
Run `pytest tests/samplers/test_beam_search.py`.
|
2023-09-04 17:29:42 -07:00
|
|
|
"""
|
2024-03-24 21:39:33 -07:00
|
|
|
|
2023-09-04 17:29:42 -07:00
|
|
|
import pytest
|
|
|
|
|
2025-03-15 01:02:20 -04:00
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def v1(run_with_both_engines):
|
|
|
|
"""We can run both engines for this test."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-09-04 17:29:42 -07:00
|
|
|
# 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".
|
2024-09-23 22:08:12 -07:00
|
|
|
MAX_TOKENS = [64]
|
2023-09-04 17:29:42 -07:00
|
|
|
BEAM_WIDTHS = [4]
|
2024-09-20 19:55:33 -07:00
|
|
|
MODELS = ["TinyLlama/TinyLlama-1.1B-Chat-v1.0"]
|
2023-09-04 17:29:42 -07:00
|
|
|
|
|
|
|
|
2025-03-15 01:02:20 -04:00
|
|
|
@pytest.mark.skip_v1 # FIXME: This fails on V1 right now.
|
2023-09-04 17:29:42 -07:00
|
|
|
@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:
|
2024-02-20 14:37:39 -08:00
|
|
|
example_prompts = example_prompts[:1]
|
2024-06-07 22:31:32 -07:00
|
|
|
with hf_runner(model, dtype=dtype) as hf_model:
|
|
|
|
hf_outputs = hf_model.generate_beam_search(example_prompts, beam_width,
|
|
|
|
max_tokens)
|
2023-09-04 17:29:42 -07:00
|
|
|
|
2024-06-08 01:59:20 -07:00
|
|
|
with vllm_runner(model, dtype=dtype) as vllm_model:
|
2024-10-06 22:47:04 -07:00
|
|
|
vllm_outputs = vllm_model.generate_beam_search(example_prompts,
|
|
|
|
beam_width, max_tokens)
|
2023-09-04 17:29:42 -07:00
|
|
|
|
|
|
|
for i in range(len(example_prompts)):
|
2024-09-20 19:55:33 -07:00
|
|
|
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)
|
2023-09-04 17:29:42 -07:00
|
|
|
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}")
|