"components/backends/sglang/docs/sgl-hicache-example.md" did not exist on "fa4a7f1e71479cbf2bb735551296862c4399c418"
test_prompt_validation.py 3.43 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
import io

6
# imports for structured outputs tests
7
import openai
8
import pybase64
9
import pytest
10
import regex as re
11
12
import torch

13
from vllm.entrypoints.renderer import BaseRenderer
14
15
16
17

from ...utils import RemoteOpenAIServer


18
19
@pytest.fixture(scope="function", autouse=True)
def use_v1_only(monkeypatch):
20
    monkeypatch.setenv("VLLM_USE_V1", "1")
21
22


23
24
25
26
27
28
29
@pytest.mark.asyncio
async def test_empty_prompt():
    model_name = "gpt2"
    server_args = ["--enforce-eager"]
    with RemoteOpenAIServer(model_name, server_args) as remote_server:
        client = remote_server.get_async_client()

30
        with pytest.raises(
31
32
            openai.BadRequestError,
            match="Either prompt or prompt_embeds must be provided and non-empty.",
33
        ):
34
35
36
37
38
39
40
            await client.completions.create(
                model=model_name,
                prompt="",
                max_tokens=5,
                temperature=0.0,
                extra_body={"prompt_embeds": []},
            )
41
42
43
44
45
46
47
48
49


@pytest.mark.asyncio
async def test_out_of_vocab_token_ids():
    model_name = "gpt2"
    server_args = ["--enforce-eager"]
    with RemoteOpenAIServer(model_name, server_args) as remote_server:
        client = remote_server.get_async_client()

50
51
52
53
54
55
        with pytest.raises(
            openai.BadRequestError, match=re.compile(".*out of vocabulary.*").pattern
        ):
            await client.completions.create(
                model=model_name, prompt=[999999], max_tokens=5, temperature=0.0
            )
56
57


58
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16, torch.float16])
59
@pytest.mark.parametrize(
60
61
    "layout", [torch.strided, torch.sparse_coo, torch.sparse_csc, torch.sparse_csr]
)
62
63
@pytest.mark.parametrize("seq_len", [2, 10])
@pytest.mark.parametrize("hidden_size", [2, 10])
64
65
66
def test_load_prompt_embeds(
    dtype: torch.dtype, layout: torch.layout, seq_len: int, hidden_size: int
):
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    # construct arbitrary tensors of various dtypes, layouts, and sizes.
    # We need to check against different layouts to make sure that if a user
    # uses sparse tensors to reduce the transmission size of prompt embeddings,
    # we must cast them to dense/strided before passing them into the engine.
    # We don't use non-CPU tensors in this test to avoid preemptively
    # initializing cuda and break other tests in the suite that fork processes.
    # We also need to make sure that we only use devices that are actually
    # available in the environment the test is running on. For simplicity,
    # we just test against CPU.
    tensor = torch.randn((seq_len, hidden_size), dtype=dtype)
    if layout == torch.strided:
        tensor = tensor.contiguous()
    elif layout == torch.sparse_coo:
        tensor = tensor.to_sparse_coo()
    elif layout == torch.sparse_csc:
        tensor = tensor.to_sparse_csc()
    elif layout == torch.sparse_csr:
        tensor = tensor.to_sparse_csr()

    buffer = io.BytesIO()
    torch.save(tensor, buffer)
    buffer.seek(0)
    encoded_tensor = pybase64.b64encode(buffer.getvalue())

91
    loaded_prompt_embeds = BaseRenderer.load_prompt_embeds(encoded_tensor)
92
93
94
95
    assert len(loaded_prompt_embeds) == 1
    loaded_tensor = loaded_prompt_embeds[0]["prompt_embeds"]
    assert loaded_tensor.device.type == "cpu"
    assert loaded_tensor.layout == torch.strided
96
97
98
    torch.testing.assert_close(
        loaded_tensor, tensor.to("cpu").to_dense(), equal_nan=True
    )