2023-09-22 17:48:04 -07:00
|
|
|
import random
|
|
|
|
from typing import Tuple
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2023-10-16 12:57:26 -07:00
|
|
|
import pytest
|
2023-09-22 17:48:04 -07:00
|
|
|
import torch
|
2024-01-13 05:51:03 +08:00
|
|
|
from transformers import GenerationConfig, GenerationMixin
|
2023-09-22 17:48:04 -07:00
|
|
|
|
|
|
|
from vllm.model_executor.layers.sampler import Sampler
|
|
|
|
from vllm.model_executor.utils import set_random_seed
|
|
|
|
from vllm.sequence import SamplingParams, SequenceData, SequenceGroupMetadata
|
2023-12-02 16:06:17 -08:00
|
|
|
from vllm.worker.model_runner import ModelRunner
|
2023-09-22 17:48:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
class MockLogitsSampler(Sampler):
|
|
|
|
|
|
|
|
def __init__(self, vocab_size: int, fake_logits: torch.Tensor):
|
|
|
|
super().__init__(vocab_size=vocab_size)
|
|
|
|
self.fake_logits = fake_logits
|
|
|
|
|
|
|
|
def forward(self, *args, **kwargs):
|
|
|
|
with patch("vllm.model_executor.layers.sampler._prune_hidden_states",
|
2023-11-20 11:58:01 -08:00
|
|
|
lambda x, y: x), patch(
|
|
|
|
"vllm.model_executor.layers.sampler._get_logits",
|
2023-09-22 17:48:04 -07:00
|
|
|
lambda *args, **kwargs: self.fake_logits):
|
2023-11-20 11:58:01 -08:00
|
|
|
return super().forward(*args, **kwargs)
|
2023-09-22 17:48:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
def _prepare_test(
|
|
|
|
batch_size: int
|
2023-12-02 16:06:17 -08:00
|
|
|
) -> Tuple[torch.Tensor, torch.Tensor, MockLogitsSampler, ModelRunner]:
|
2023-09-22 17:48:04 -07:00
|
|
|
vocab_size = 32000
|
|
|
|
input_tensor = torch.rand((batch_size, 1024),
|
|
|
|
device="cuda",
|
|
|
|
dtype=torch.float16)
|
|
|
|
fake_logits = torch.full((batch_size, vocab_size),
|
|
|
|
1e-2,
|
|
|
|
device=input_tensor.device,
|
|
|
|
dtype=input_tensor.dtype)
|
|
|
|
sampler = MockLogitsSampler(32000, fake_logits)
|
2023-12-02 16:06:17 -08:00
|
|
|
model_runner = ModelRunner(None, None, None)
|
|
|
|
return input_tensor, fake_logits, sampler, model_runner
|
2023-09-22 17:48:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
RANDOM_SEEDS = list(range(128))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("seed", RANDOM_SEEDS)
|
|
|
|
def test_sampler_all_greedy(seed: int):
|
|
|
|
set_random_seed(seed)
|
|
|
|
batch_size = random.randint(1, 256)
|
2023-12-02 16:06:17 -08:00
|
|
|
input_tensor, fake_logits, sampler, model_runner = _prepare_test(
|
|
|
|
batch_size)
|
2023-09-22 17:48:04 -07:00
|
|
|
|
|
|
|
seq_group_metadata_list = []
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens = []
|
2023-09-22 17:48:04 -07:00
|
|
|
for i in range(batch_size):
|
|
|
|
seq_group_metadata_list.append(
|
|
|
|
SequenceGroupMetadata(
|
|
|
|
request_id=f"test_{i}",
|
|
|
|
is_prompt=True,
|
|
|
|
seq_data={0: SequenceData([1, 2, 3])},
|
|
|
|
sampling_params=SamplingParams(temperature=0, ),
|
|
|
|
block_tables={0: [1]},
|
|
|
|
))
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens.append(seq_group_metadata_list[-1].seq_data[0].get_len())
|
2023-09-22 17:48:04 -07:00
|
|
|
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata = model_runner._prepare_sample(seq_group_metadata_list,
|
|
|
|
prompt_lens)
|
2023-09-22 17:48:04 -07:00
|
|
|
sampler_output = sampler(embedding=None,
|
|
|
|
hidden_states=input_tensor,
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata=sampling_metadata)
|
2023-09-22 17:48:04 -07:00
|
|
|
expected = torch.argmax(fake_logits, dim=-1)
|
|
|
|
for i, sequence_output in enumerate(sampler_output):
|
2023-10-16 12:57:26 -07:00
|
|
|
for nth_output in sequence_output.samples:
|
2023-09-22 17:48:04 -07:00
|
|
|
assert nth_output.output_token == expected[i].item()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("seed", RANDOM_SEEDS)
|
|
|
|
def test_sampler_all_random(seed: int):
|
|
|
|
set_random_seed(seed)
|
|
|
|
batch_size = random.randint(1, 256)
|
2023-12-02 16:06:17 -08:00
|
|
|
input_tensor, fake_logits, sampler, model_runner = _prepare_test(
|
|
|
|
batch_size)
|
2023-09-22 17:48:04 -07:00
|
|
|
|
|
|
|
for i in range(batch_size):
|
|
|
|
fake_logits[i, i] = 1e2
|
|
|
|
|
|
|
|
seq_group_metadata_list = []
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens = []
|
2023-09-22 17:48:04 -07:00
|
|
|
for i in range(batch_size):
|
|
|
|
seq_group_metadata_list.append(
|
|
|
|
SequenceGroupMetadata(
|
|
|
|
request_id=f"test_{i}",
|
|
|
|
is_prompt=True,
|
|
|
|
seq_data={0: SequenceData([1, 2, 3])},
|
|
|
|
sampling_params=SamplingParams(
|
|
|
|
temperature=1.0,
|
|
|
|
n=random.randint(1, 10),
|
|
|
|
),
|
|
|
|
block_tables={0: [1]},
|
|
|
|
))
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens.append(seq_group_metadata_list[-1].seq_data[0].get_len())
|
2023-09-22 17:48:04 -07:00
|
|
|
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata = model_runner._prepare_sample(seq_group_metadata_list,
|
|
|
|
prompt_lens)
|
2023-09-22 17:48:04 -07:00
|
|
|
sampler_output = sampler(embedding=None,
|
|
|
|
hidden_states=input_tensor,
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata=sampling_metadata)
|
2023-09-22 17:48:04 -07:00
|
|
|
for i, sequence_output in enumerate(sampler_output):
|
2023-10-16 12:57:26 -07:00
|
|
|
for nth_output in sequence_output.samples:
|
2023-09-22 17:48:04 -07:00
|
|
|
assert nth_output.output_token == i
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("seed", RANDOM_SEEDS)
|
|
|
|
def test_sampler_all_beam(seed: int):
|
|
|
|
set_random_seed(seed)
|
|
|
|
batch_size = random.randint(1, 256)
|
2023-12-02 16:06:17 -08:00
|
|
|
input_tensor, _, sampler, model_runner = _prepare_test(batch_size)
|
2023-09-22 17:48:04 -07:00
|
|
|
|
|
|
|
seq_group_metadata_list = []
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens = []
|
2023-09-22 17:48:04 -07:00
|
|
|
for i in range(batch_size):
|
|
|
|
seq_group_metadata_list.append(
|
|
|
|
SequenceGroupMetadata(
|
|
|
|
request_id=f"test_{i}",
|
|
|
|
is_prompt=True,
|
|
|
|
seq_data={0: SequenceData([1, 2, 3])},
|
|
|
|
sampling_params=SamplingParams(
|
|
|
|
temperature=0,
|
|
|
|
best_of=2,
|
|
|
|
use_beam_search=True,
|
|
|
|
),
|
|
|
|
block_tables={0: [1]},
|
|
|
|
))
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens.append(seq_group_metadata_list[-1].seq_data[0].get_len())
|
2023-09-22 17:48:04 -07:00
|
|
|
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata = model_runner._prepare_sample(seq_group_metadata_list,
|
|
|
|
prompt_lens)
|
2023-09-22 17:48:04 -07:00
|
|
|
sampler(embedding=None,
|
|
|
|
hidden_states=input_tensor,
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata=sampling_metadata)
|
2023-09-22 17:48:04 -07:00
|
|
|
# no assertion here as I am not sure how to determine whether
|
|
|
|
# the outputs are expected - in other words, this just tests
|
|
|
|
# whether there are no exceptions in the sampler
|
|
|
|
# when handling an all-beam search case.
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("seed", RANDOM_SEEDS)
|
|
|
|
def test_sampler_mixed(seed: int):
|
|
|
|
set_random_seed(seed)
|
|
|
|
batch_size = random.randint(1, 256)
|
2023-12-02 16:06:17 -08:00
|
|
|
input_tensor, fake_logits, sampler, model_runner = _prepare_test(
|
|
|
|
batch_size)
|
2023-09-22 17:48:04 -07:00
|
|
|
|
|
|
|
seq_group_metadata_list = []
|
|
|
|
expected_tokens = []
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens = []
|
2023-09-22 17:48:04 -07:00
|
|
|
for i in range(batch_size):
|
|
|
|
n = 1
|
|
|
|
sampling_type = random.randint(0, 2)
|
|
|
|
if sampling_type == 0:
|
|
|
|
sampling_params = SamplingParams(temperature=0)
|
|
|
|
elif sampling_type == 1:
|
|
|
|
n = random.randint(1, 10)
|
|
|
|
sampling_params = SamplingParams(
|
|
|
|
temperature=random.random() + 0.1,
|
|
|
|
top_p=min(random.random() + 0.1, 1),
|
|
|
|
top_k=random.randint(0, 10) or -1,
|
|
|
|
n=n,
|
|
|
|
presence_penalty=random.randint(0, 1),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
sampling_params = SamplingParams(temperature=0,
|
|
|
|
use_beam_search=True,
|
|
|
|
best_of=2)
|
|
|
|
for idx in range(n):
|
|
|
|
fake_logits[i, i + idx] = 1e2
|
|
|
|
expected_tokens.append(i + idx)
|
|
|
|
seq_group_metadata_list.append(
|
|
|
|
SequenceGroupMetadata(
|
|
|
|
request_id=f"test_{i}",
|
|
|
|
is_prompt=True,
|
|
|
|
seq_data={0: SequenceData([1, 2, 3])},
|
|
|
|
sampling_params=sampling_params,
|
|
|
|
block_tables={0: [1]},
|
|
|
|
))
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens.append(seq_group_metadata_list[-1].seq_data[0].get_len())
|
2023-09-22 17:48:04 -07:00
|
|
|
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata = model_runner._prepare_sample(seq_group_metadata_list,
|
|
|
|
prompt_lens)
|
2023-09-22 17:48:04 -07:00
|
|
|
sampler_output = sampler(embedding=None,
|
|
|
|
hidden_states=input_tensor,
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata=sampling_metadata)
|
2023-09-22 17:48:04 -07:00
|
|
|
for i, sequence_output in enumerate(sampler_output):
|
|
|
|
if seq_group_metadata_list[i].sampling_params.use_beam_search:
|
|
|
|
continue
|
2023-10-16 12:57:26 -07:00
|
|
|
for nth_output in sequence_output.samples:
|
2023-09-22 17:48:04 -07:00
|
|
|
assert nth_output.output_token in expected_tokens
|
2023-11-03 23:12:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("seed", RANDOM_SEEDS)
|
|
|
|
def test_sampler_logits_processors(seed: int):
|
|
|
|
set_random_seed(seed)
|
|
|
|
batch_size = random.randint(1, 256)
|
2023-12-02 16:06:17 -08:00
|
|
|
input_tensor, _, sampler, model_runner = _prepare_test(batch_size)
|
2023-11-03 23:12:15 +02:00
|
|
|
|
|
|
|
# This sample logits processor gives infinite score to the i-th token,
|
|
|
|
# where i is the length of the input sequence.
|
|
|
|
# We therefore expect the output token sequence to be [0, 1, 2, ...]
|
|
|
|
def pick_ith(token_ids, logits):
|
|
|
|
logits[len(token_ids)] = float("inf")
|
|
|
|
return logits
|
|
|
|
|
|
|
|
seq_group_metadata_list = []
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens = []
|
2023-11-03 23:12:15 +02:00
|
|
|
for i in range(batch_size):
|
|
|
|
seq_group_metadata_list.append(
|
|
|
|
SequenceGroupMetadata(
|
|
|
|
request_id=f"test_{i}",
|
|
|
|
is_prompt=True,
|
|
|
|
seq_data={0: SequenceData([1, 2, 3])},
|
|
|
|
sampling_params=SamplingParams(temperature=0,
|
|
|
|
logits_processors=[pick_ith]),
|
|
|
|
block_tables={0: [1]},
|
|
|
|
))
|
2023-12-02 16:06:17 -08:00
|
|
|
prompt_lens.append(seq_group_metadata_list[-1].seq_data[0].get_len())
|
2023-11-03 23:12:15 +02:00
|
|
|
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata = model_runner._prepare_sample(seq_group_metadata_list,
|
|
|
|
prompt_lens)
|
2023-11-03 23:12:15 +02:00
|
|
|
sampler_output = sampler(embedding=None,
|
|
|
|
hidden_states=input_tensor,
|
2023-12-02 16:06:17 -08:00
|
|
|
sampling_metadata=sampling_metadata)
|
2023-11-20 11:58:01 -08:00
|
|
|
for _, sequence_output in enumerate(sampler_output):
|
2023-11-03 23:12:15 +02:00
|
|
|
for idx, nth_output in enumerate(sequence_output.samples):
|
|
|
|
assert nth_output.output_token == idx
|
2024-01-13 05:51:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("seed", RANDOM_SEEDS)
|
|
|
|
def test_sampler_top_k_top_p(seed: int):
|
|
|
|
set_random_seed(seed)
|
|
|
|
batch_size = random.randint(1, 256)
|
|
|
|
top_k = random.randint(100, 500)
|
|
|
|
top_p = random.random() * 0.1
|
|
|
|
vocab_size = 32000
|
|
|
|
input_tensor = torch.rand((batch_size, 1024),
|
|
|
|
device="cuda",
|
|
|
|
dtype=torch.float16)
|
|
|
|
fake_logits = torch.normal(0,
|
|
|
|
5,
|
|
|
|
size=(batch_size, vocab_size),
|
|
|
|
device=input_tensor.device,
|
|
|
|
dtype=input_tensor.dtype)
|
|
|
|
sampler = MockLogitsSampler(32000, fake_logits)
|
|
|
|
model_runner = ModelRunner(None, None, None)
|
|
|
|
|
|
|
|
generation_model = GenerationMixin()
|
|
|
|
generation_config = GenerationConfig(top_k=top_k,
|
|
|
|
top_p=top_p,
|
|
|
|
do_sample=True)
|
|
|
|
warpers = generation_model._get_logits_warper(generation_config)
|
|
|
|
assert len(warpers) == 2 # top_p and top_k
|
|
|
|
|
|
|
|
seq_group_metadata_list = []
|
|
|
|
prompt_lens = []
|
|
|
|
for i in range(batch_size):
|
|
|
|
seq_group_metadata_list.append(
|
|
|
|
SequenceGroupMetadata(
|
|
|
|
request_id=f"test_{i}",
|
|
|
|
is_prompt=True,
|
|
|
|
seq_data={0: SequenceData([1, 2, 3])},
|
|
|
|
sampling_params=SamplingParams(
|
|
|
|
temperature=1,
|
|
|
|
top_k=top_k,
|
|
|
|
top_p=top_p,
|
|
|
|
),
|
|
|
|
block_tables={0: [1]},
|
|
|
|
))
|
|
|
|
prompt_lens.append(seq_group_metadata_list[-1].seq_data[0].get_len())
|
|
|
|
|
|
|
|
sampling_metadata = model_runner._prepare_sample(seq_group_metadata_list,
|
|
|
|
prompt_lens)
|
|
|
|
|
|
|
|
sample_probs = None
|
|
|
|
|
|
|
|
def mock_sample(probs, logprobs, sampling_metadata):
|
|
|
|
nonlocal sample_probs
|
|
|
|
sample_probs = probs
|
|
|
|
return [[prob.topk(1, dim=-1).indices.tolist(), [0]] for prob in probs]
|
|
|
|
|
|
|
|
with patch("vllm.model_executor.layers.sampler._sample", mock_sample):
|
|
|
|
sampler(embedding=None,
|
|
|
|
hidden_states=input_tensor,
|
|
|
|
sampling_metadata=sampling_metadata)
|
|
|
|
hf_probs = warpers(torch.zeros_like(fake_logits), fake_logits.clone())
|
|
|
|
hf_probs = torch.softmax(hf_probs, dim=-1, dtype=torch.float)
|
|
|
|
assert torch.allclose(hf_probs, sample_probs, atol=1e-5)
|
|
|
|
assert torch.equal(hf_probs.eq(0), sample_probs.eq(0))
|