|
1 | 1 | # SPDX-License-Identifier: Apache-2.0
|
2 | 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
3 | 3 |
|
| 4 | +import io |
| 5 | + |
4 | 6 | # imports for guided decoding tests
|
5 | 7 | import openai
|
| 8 | +import pybase64 |
6 | 9 | import pytest
|
7 | 10 | import regex as re
|
| 11 | +import torch |
| 12 | + |
| 13 | +from vllm.entrypoints.openai.serving_engine import OpenAIServing |
8 | 14 |
|
9 | 15 | from ...utils import RemoteOpenAIServer
|
10 | 16 |
|
@@ -42,3 +48,46 @@ async def test_out_of_vocab_token_ids():
|
42 | 48 | prompt=[999999],
|
43 | 49 | max_tokens=5,
|
44 | 50 | temperature=0.0)
|
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("dtype", |
| 54 | + [torch.float32, torch.bfloat16, torch.float16]) |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "layout", |
| 57 | + [torch.strided, torch.sparse_coo, torch.sparse_csc, torch.sparse_csr]) |
| 58 | +@pytest.mark.parametrize("seq_len", [2, 10]) |
| 59 | +@pytest.mark.parametrize("hidden_size", [2, 10]) |
| 60 | +def test_load_prompt_embeds(dtype: torch.dtype, layout: torch.layout, |
| 61 | + seq_len: int, hidden_size: int): |
| 62 | + # construct arbitrary tensors of various dtypes, layouts, and sizes. |
| 63 | + # We need to check against different layouts to make sure that if a user |
| 64 | + # uses sparse tensors to reduce the transmission size of prompt embeddings, |
| 65 | + # we must cast them to dense/strided before passing them into the engine. |
| 66 | + # We don't use non-CPU tensors in this test to avoid preemptively |
| 67 | + # initializing cuda and break other tests in the suite that fork processes. |
| 68 | + # We also need to make sure that we only use devices that are actually |
| 69 | + # available in the environment the test is running on. For simplicity, |
| 70 | + # we just test against CPU. |
| 71 | + tensor = torch.randn((seq_len, hidden_size), dtype=dtype) |
| 72 | + if layout == torch.strided: |
| 73 | + tensor = tensor.contiguous() |
| 74 | + elif layout == torch.sparse_coo: |
| 75 | + tensor = tensor.to_sparse_coo() |
| 76 | + elif layout == torch.sparse_csc: |
| 77 | + tensor = tensor.to_sparse_csc() |
| 78 | + elif layout == torch.sparse_csr: |
| 79 | + tensor = tensor.to_sparse_csr() |
| 80 | + |
| 81 | + buffer = io.BytesIO() |
| 82 | + torch.save(tensor, buffer) |
| 83 | + buffer.seek(0) |
| 84 | + encoded_tensor = pybase64.b64encode(buffer.getvalue()) |
| 85 | + |
| 86 | + loaded_prompt_embeds = OpenAIServing._load_prompt_embeds(encoded_tensor) |
| 87 | + assert len(loaded_prompt_embeds) == 1 |
| 88 | + loaded_tensor = loaded_prompt_embeds[0]["prompt_embeds"] |
| 89 | + assert loaded_tensor.device.type == "cpu" |
| 90 | + assert loaded_tensor.layout == torch.strided |
| 91 | + torch.testing.assert_close(loaded_tensor, |
| 92 | + tensor.to("cpu").to_dense(), |
| 93 | + equal_nan=True) |
0 commit comments