2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-02-02 14:58:18 -05:00
|
|
|
|
2024-06-30 08:53:00 -07:00
|
|
|
import base64
|
2025-10-22 18:38:57 +08:00
|
|
|
import json
|
2024-06-30 08:53:00 -07:00
|
|
|
|
|
|
|
|
import numpy as np
|
2024-06-14 02:21:53 +08:00
|
|
|
import openai
|
|
|
|
|
import pytest
|
2024-08-26 21:33:17 -07:00
|
|
|
import pytest_asyncio
|
2024-11-01 16:13:35 +08:00
|
|
|
import requests
|
2025-08-05 15:37:00 +08:00
|
|
|
import torch
|
|
|
|
|
import torch.nn.functional as F
|
2024-11-01 16:13:35 +08:00
|
|
|
|
2025-09-11 16:53:09 +08:00
|
|
|
from tests.models.language.pooling.embed_utils import run_embedding_correctness_test
|
|
|
|
|
from tests.models.utils import check_embeddings_close
|
|
|
|
|
from tests.utils import RemoteOpenAIServer
|
2025-12-01 15:30:43 +08:00
|
|
|
from vllm.entrypoints.pooling.embed.protocol import EmbeddingResponse
|
|
|
|
|
from vllm.entrypoints.pooling.pooling.protocol import PoolingResponse
|
2025-11-21 20:13:18 -06:00
|
|
|
from vllm.platforms import current_platform
|
2025-12-02 13:33:37 +08:00
|
|
|
from vllm.tokenizers import get_tokenizer
|
2025-10-22 18:38:57 +08:00
|
|
|
from vllm.utils.serial_utils import (
|
|
|
|
|
EMBED_DTYPE_TO_TORCH_DTYPE,
|
|
|
|
|
ENDIANNESS,
|
|
|
|
|
MetadataItem,
|
|
|
|
|
binary2tensor,
|
2025-12-08 20:01:21 +08:00
|
|
|
build_metadata_items,
|
2025-10-22 18:38:57 +08:00
|
|
|
decode_pooling_output,
|
|
|
|
|
)
|
2024-06-14 02:21:53 +08:00
|
|
|
|
2025-02-28 08:50:43 +00:00
|
|
|
MODEL_NAME = "intfloat/multilingual-e5-small"
|
2024-11-01 16:13:35 +08:00
|
|
|
DUMMY_CHAT_TEMPLATE = """{% for message in messages %}{{message['role'] + ': ' + message['content'] + '\\n'}}{% endfor %}""" # noqa: E501
|
2025-04-24 22:06:28 +08:00
|
|
|
DTYPE = "bfloat16"
|
2026-01-16 14:17:04 +08:00
|
|
|
input_text = "The best thing about vLLM is that it supports many different models"
|
|
|
|
|
input_tokens = [
|
|
|
|
|
0,
|
|
|
|
|
581,
|
|
|
|
|
2965,
|
|
|
|
|
13580,
|
|
|
|
|
1672,
|
|
|
|
|
81,
|
|
|
|
|
23708,
|
|
|
|
|
594,
|
|
|
|
|
83,
|
|
|
|
|
450,
|
|
|
|
|
442,
|
|
|
|
|
8060,
|
|
|
|
|
7,
|
|
|
|
|
5941,
|
|
|
|
|
12921,
|
|
|
|
|
115774,
|
|
|
|
|
2,
|
|
|
|
|
]
|
2024-06-14 02:21:53 +08:00
|
|
|
|
2025-12-24 00:42:30 -06:00
|
|
|
if current_platform.is_rocm():
|
|
|
|
|
# Disable Flash/MemEfficient SDP on ROCm to avoid HF Transformers
|
|
|
|
|
# accuracy issues: https://github.com/vllm-project/vllm/issues/30167
|
|
|
|
|
# TODO: Remove once ROCm SDP accuracy issues are resolved on HuggingFace
|
|
|
|
|
torch.backends.cuda.enable_flash_sdp(False)
|
|
|
|
|
torch.backends.cuda.enable_mem_efficient_sdp(False)
|
|
|
|
|
torch.backends.cuda.enable_math_sdp(True)
|
|
|
|
|
|
|
|
|
|
|
2024-06-14 02:21:53 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2024-11-01 16:13:35 +08:00
|
|
|
def server():
|
2024-07-17 15:43:21 +08:00
|
|
|
args = [
|
2025-07-28 10:42:40 +08:00
|
|
|
"--runner",
|
|
|
|
|
"pooling",
|
2024-07-17 15:43:21 +08:00
|
|
|
# use half precision for speed and memory savings in CI environment
|
|
|
|
|
"--dtype",
|
2025-04-24 22:06:28 +08:00
|
|
|
DTYPE,
|
2024-07-17 15:43:21 +08:00
|
|
|
"--enforce-eager",
|
|
|
|
|
"--max-model-len",
|
2025-02-28 16:56:44 +00:00
|
|
|
"512",
|
2024-11-01 16:13:35 +08:00
|
|
|
"--chat-template",
|
|
|
|
|
DUMMY_CHAT_TEMPLATE,
|
2024-07-17 15:43:21 +08:00
|
|
|
]
|
|
|
|
|
|
2025-12-24 00:42:30 -06:00
|
|
|
# ROCm: Use Flex Attention to support encoder-only self-attention.
|
|
|
|
|
if current_platform.is_rocm():
|
|
|
|
|
args.extend(["--attention-backend", "FLEX_ATTENTION"])
|
|
|
|
|
|
2024-11-01 16:13:35 +08:00
|
|
|
with RemoteOpenAIServer(MODEL_NAME, args) as remote_server:
|
2024-07-12 21:51:48 -07:00
|
|
|
yield remote_server
|
2024-06-14 02:21:53 +08:00
|
|
|
|
|
|
|
|
|
2024-08-26 21:33:17 -07:00
|
|
|
@pytest_asyncio.fixture
|
2024-11-01 16:13:35 +08:00
|
|
|
async def client(server):
|
|
|
|
|
async with server.get_async_client() as async_client:
|
2024-08-26 21:33:17 -07:00
|
|
|
yield async_client
|
2024-06-14 02:21:53 +08:00
|
|
|
|
|
|
|
|
|
2025-04-24 22:06:28 +08:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
|
def hf_model(hf_runner):
|
|
|
|
|
with hf_runner(MODEL_NAME, dtype=DTYPE, is_sentence_transformer=True) as hf_model:
|
|
|
|
|
yield hf_model
|
|
|
|
|
|
|
|
|
|
|
2024-06-14 02:21:53 +08:00
|
|
|
@pytest.mark.asyncio
|
2024-11-01 16:13:35 +08:00
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
2026-01-16 14:17:04 +08:00
|
|
|
async def test_basic(
|
|
|
|
|
server: RemoteOpenAIServer, client: openai.AsyncOpenAI, model_name: str
|
|
|
|
|
):
|
|
|
|
|
# test /v1/models
|
|
|
|
|
response = requests.get(server.url_for("/v1/models"))
|
|
|
|
|
model = response.json()["data"][0]["id"]
|
|
|
|
|
assert model == MODEL_NAME
|
2024-06-14 02:21:53 +08:00
|
|
|
|
2026-01-16 14:17:04 +08:00
|
|
|
models = await client.models.list()
|
|
|
|
|
models = models.data
|
|
|
|
|
served_model = models[0]
|
|
|
|
|
assert served_model.id == MODEL_NAME
|
|
|
|
|
|
|
|
|
|
# test /tokenize
|
|
|
|
|
response = requests.post(
|
|
|
|
|
server.url_for("/tokenize"),
|
|
|
|
|
json={"model": model_name, "prompt": input_text},
|
|
|
|
|
)
|
|
|
|
|
assert response.json()["tokens"] == input_tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
|
|
|
|
async def test_completion_request(
|
|
|
|
|
client: openai.AsyncOpenAI, model_name: str, hf_model
|
|
|
|
|
):
|
|
|
|
|
# test input: str
|
2024-12-24 17:54:30 +08:00
|
|
|
embedding_response = await client.embeddings.create(
|
2024-06-14 02:21:53 +08:00
|
|
|
model=model_name,
|
2026-01-16 14:17:04 +08:00
|
|
|
input=input_text,
|
2024-06-14 02:21:53 +08:00
|
|
|
encoding_format="float",
|
|
|
|
|
)
|
2024-12-24 17:54:30 +08:00
|
|
|
embeddings = EmbeddingResponse.model_validate(
|
|
|
|
|
embedding_response.model_dump(mode="json")
|
2025-10-05 15:06:22 +01:00
|
|
|
)
|
2024-12-24 17:54:30 +08:00
|
|
|
|
2024-06-14 02:21:53 +08:00
|
|
|
assert embeddings.id is not None
|
|
|
|
|
assert len(embeddings.data) == 1
|
2025-02-28 16:56:44 +00:00
|
|
|
assert len(embeddings.data[0].embedding) == 384
|
2024-06-14 02:21:53 +08:00
|
|
|
assert embeddings.usage.completion_tokens == 0
|
2026-01-16 14:17:04 +08:00
|
|
|
assert embeddings.usage.prompt_tokens == len(input_tokens)
|
|
|
|
|
assert embeddings.usage.total_tokens == len(input_tokens)
|
2024-06-14 02:21:53 +08:00
|
|
|
|
2025-04-24 22:06:28 +08:00
|
|
|
vllm_outputs = [d.embedding for d in embeddings.data]
|
2026-01-16 14:17:04 +08:00
|
|
|
run_embedding_correctness_test(hf_model, [input_text], vllm_outputs)
|
2025-04-24 22:06:28 +08:00
|
|
|
|
2026-01-16 14:17:04 +08:00
|
|
|
# test input: list[int]
|
2024-12-24 17:54:30 +08:00
|
|
|
embedding_response = await client.embeddings.create(
|
2024-06-14 02:21:53 +08:00
|
|
|
model=model_name,
|
|
|
|
|
input=input_tokens,
|
|
|
|
|
encoding_format="float",
|
|
|
|
|
)
|
2024-12-24 17:54:30 +08:00
|
|
|
embeddings = EmbeddingResponse.model_validate(
|
|
|
|
|
embedding_response.model_dump(mode="json")
|
2025-10-05 15:06:22 +01:00
|
|
|
)
|
2024-12-24 17:54:30 +08:00
|
|
|
|
2024-06-14 02:21:53 +08:00
|
|
|
assert embeddings.id is not None
|
|
|
|
|
assert len(embeddings.data) == 1
|
2025-02-28 16:56:44 +00:00
|
|
|
assert len(embeddings.data[0].embedding) == 384
|
2024-06-14 02:21:53 +08:00
|
|
|
assert embeddings.usage.completion_tokens == 0
|
2026-01-16 14:17:04 +08:00
|
|
|
assert embeddings.usage.prompt_tokens == len(input_tokens)
|
|
|
|
|
assert embeddings.usage.total_tokens == len(input_tokens)
|
|
|
|
|
|
|
|
|
|
vllm_outputs = [d.embedding for d in embeddings.data]
|
|
|
|
|
run_embedding_correctness_test(hf_model, [input_text], vllm_outputs)
|
2024-06-14 02:21:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2024-11-01 16:13:35 +08:00
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
2026-01-16 14:17:04 +08:00
|
|
|
async def test_completion_request_batched(
|
|
|
|
|
client: openai.AsyncOpenAI, model_name: str, hf_model
|
|
|
|
|
):
|
|
|
|
|
N = 10
|
|
|
|
|
input_texts = [input_text] * N
|
|
|
|
|
|
|
|
|
|
# test input: list[str]
|
2024-12-24 17:54:30 +08:00
|
|
|
embedding_response = await client.embeddings.create(
|
2024-06-14 02:21:53 +08:00
|
|
|
model=model_name,
|
|
|
|
|
input=input_texts,
|
|
|
|
|
encoding_format="float",
|
|
|
|
|
)
|
2024-12-24 17:54:30 +08:00
|
|
|
embeddings = EmbeddingResponse.model_validate(
|
|
|
|
|
embedding_response.model_dump(mode="json")
|
2025-10-05 15:06:22 +01:00
|
|
|
)
|
2024-12-24 17:54:30 +08:00
|
|
|
|
2024-06-14 02:21:53 +08:00
|
|
|
assert embeddings.id is not None
|
2026-01-16 14:17:04 +08:00
|
|
|
assert len(embeddings.data) == N
|
2025-02-28 16:56:44 +00:00
|
|
|
assert len(embeddings.data[0].embedding) == 384
|
2024-11-01 16:13:35 +08:00
|
|
|
assert embeddings.usage.completion_tokens == 0
|
2026-01-16 14:17:04 +08:00
|
|
|
assert embeddings.usage.prompt_tokens == len(input_tokens) * N
|
|
|
|
|
assert embeddings.usage.total_tokens == len(input_tokens) * N
|
2024-06-14 02:21:53 +08:00
|
|
|
|
2025-04-24 22:06:28 +08:00
|
|
|
vllm_outputs = [d.embedding for d in embeddings.data]
|
2025-05-01 14:03:08 +08:00
|
|
|
run_embedding_correctness_test(hf_model, input_texts, vllm_outputs)
|
2025-04-24 22:06:28 +08:00
|
|
|
|
2025-03-03 01:34:51 +00:00
|
|
|
# test list[list[int]]
|
2024-12-24 17:54:30 +08:00
|
|
|
embedding_response = await client.embeddings.create(
|
2024-06-14 02:21:53 +08:00
|
|
|
model=model_name,
|
2026-01-16 14:17:04 +08:00
|
|
|
input=[input_tokens] * N,
|
2024-06-14 02:21:53 +08:00
|
|
|
encoding_format="float",
|
|
|
|
|
)
|
2024-12-24 17:54:30 +08:00
|
|
|
embeddings = EmbeddingResponse.model_validate(
|
|
|
|
|
embedding_response.model_dump(mode="json")
|
2025-10-05 15:06:22 +01:00
|
|
|
)
|
2024-12-24 17:54:30 +08:00
|
|
|
|
2024-06-14 02:21:53 +08:00
|
|
|
assert embeddings.id is not None
|
2026-01-16 14:17:04 +08:00
|
|
|
assert len(embeddings.data) == N
|
2025-02-28 16:56:44 +00:00
|
|
|
assert len(embeddings.data[0].embedding) == 384
|
2024-06-14 02:21:53 +08:00
|
|
|
assert embeddings.usage.completion_tokens == 0
|
2026-01-16 14:17:04 +08:00
|
|
|
assert embeddings.usage.prompt_tokens == len(input_tokens) * N
|
|
|
|
|
assert embeddings.usage.total_tokens == len(input_tokens) * N
|
|
|
|
|
|
|
|
|
|
vllm_outputs = [d.embedding for d in embeddings.data]
|
|
|
|
|
run_embedding_correctness_test(hf_model, input_texts, vllm_outputs)
|
2024-06-30 08:53:00 -07:00
|
|
|
|
|
|
|
|
|
2024-11-01 16:13:35 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
2026-01-16 14:17:04 +08:00
|
|
|
async def test_truncate_prompt_tokens(client: openai.AsyncOpenAI, model_name: str):
|
|
|
|
|
input_texts = [
|
|
|
|
|
"Como o Brasil pode fomentar o desenvolvimento de modelos de IA?",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# test single embedding
|
|
|
|
|
embedding_response = await client.embeddings.create(
|
|
|
|
|
model=model_name, input=input_texts, extra_body={"truncate_prompt_tokens": 10}
|
|
|
|
|
)
|
|
|
|
|
embeddings = EmbeddingResponse.model_validate(
|
|
|
|
|
embedding_response.model_dump(mode="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert embeddings.id is not None
|
|
|
|
|
assert len(embeddings.data) == 1
|
|
|
|
|
assert len(embeddings.data[0].embedding) == 384
|
|
|
|
|
assert embeddings.usage.completion_tokens == 0
|
|
|
|
|
assert embeddings.usage.prompt_tokens == 10
|
|
|
|
|
assert embeddings.usage.total_tokens == 10
|
|
|
|
|
|
|
|
|
|
input_tokens = [
|
|
|
|
|
1,
|
|
|
|
|
24428,
|
|
|
|
|
289,
|
|
|
|
|
18341,
|
|
|
|
|
26165,
|
|
|
|
|
285,
|
|
|
|
|
19323,
|
|
|
|
|
283,
|
|
|
|
|
289,
|
|
|
|
|
26789,
|
|
|
|
|
3871,
|
|
|
|
|
28728,
|
|
|
|
|
9901,
|
|
|
|
|
340,
|
|
|
|
|
2229,
|
|
|
|
|
385,
|
|
|
|
|
340,
|
|
|
|
|
315,
|
|
|
|
|
28741,
|
|
|
|
|
28804,
|
|
|
|
|
2,
|
|
|
|
|
]
|
|
|
|
|
embedding_response = await client.embeddings.create(
|
|
|
|
|
model=model_name, input=input_tokens, extra_body={"truncate_prompt_tokens": 10}
|
|
|
|
|
)
|
|
|
|
|
embeddings = EmbeddingResponse.model_validate(
|
|
|
|
|
embedding_response.model_dump(mode="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert embeddings.id is not None
|
|
|
|
|
assert len(embeddings.data) == 1
|
|
|
|
|
assert len(embeddings.data[0].embedding) == 384
|
|
|
|
|
assert embeddings.usage.completion_tokens == 0
|
|
|
|
|
assert embeddings.usage.prompt_tokens == 10
|
|
|
|
|
assert embeddings.usage.total_tokens == 10
|
|
|
|
|
|
|
|
|
|
# invalid_truncate_prompt_tokens
|
|
|
|
|
input_texts = [
|
|
|
|
|
"Como o Brasil pode fomentar o desenvolvimento de modelos de IA?",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
with pytest.raises(openai.BadRequestError):
|
|
|
|
|
response = await client.embeddings.create(
|
|
|
|
|
model=model_name,
|
|
|
|
|
input=input_texts,
|
|
|
|
|
extra_body={"truncate_prompt_tokens": 8193},
|
|
|
|
|
)
|
|
|
|
|
assert "error" in response.object
|
|
|
|
|
assert (
|
|
|
|
|
"truncate_prompt_tokens value is greater than max_model_len. "
|
|
|
|
|
"Please, select a smaller truncation size." in response.message
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-01-22 18:32:44 +08:00
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
|
|
|
|
async def test_chat_request(
|
|
|
|
|
server: RemoteOpenAIServer, client: openai.AsyncOpenAI, model_name: str
|
|
|
|
|
):
|
|
|
|
|
messages = [
|
|
|
|
|
{
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": "The cat sat on the mat.",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "A feline was resting on a rug.",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": "Stars twinkle brightly in the night sky.",
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# test chat request basic usage
|
|
|
|
|
chat_response = requests.post(
|
|
|
|
|
server.url_for("v1/embeddings"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"messages": messages,
|
|
|
|
|
"encoding_format": "float",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
chat_response.raise_for_status()
|
|
|
|
|
chat_embeddings = EmbeddingResponse.model_validate(chat_response.json())
|
|
|
|
|
|
|
|
|
|
tokenizer = get_tokenizer(tokenizer_name=model_name)
|
|
|
|
|
prompt = tokenizer.apply_chat_template(
|
|
|
|
|
messages,
|
|
|
|
|
chat_template=DUMMY_CHAT_TEMPLATE,
|
|
|
|
|
add_generation_prompt=True,
|
|
|
|
|
continue_final_message=False,
|
|
|
|
|
tokenize=False,
|
|
|
|
|
)
|
|
|
|
|
completion_response = await client.embeddings.create(
|
|
|
|
|
model=model_name,
|
|
|
|
|
input=prompt,
|
|
|
|
|
encoding_format="float",
|
|
|
|
|
# To be consistent with chat
|
|
|
|
|
extra_body={"add_special_tokens": False},
|
|
|
|
|
)
|
|
|
|
|
completion_embeddings = EmbeddingResponse.model_validate(
|
|
|
|
|
completion_response.model_dump(mode="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert chat_embeddings.id is not None
|
|
|
|
|
assert completion_embeddings.id is not None
|
|
|
|
|
assert chat_embeddings.created <= completion_embeddings.created
|
|
|
|
|
assert chat_embeddings.model_dump(exclude={"id", "created"}) == (
|
|
|
|
|
completion_embeddings.model_dump(exclude={"id", "created"})
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# test add_generation_prompt
|
|
|
|
|
response = requests.post(
|
|
|
|
|
server.url_for("v1/embeddings"),
|
|
|
|
|
json={"model": model_name, "messages": messages, "add_generation_prompt": True},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
output = EmbeddingResponse.model_validate(response.json())
|
|
|
|
|
|
|
|
|
|
assert output.object == "list"
|
|
|
|
|
assert len(output.data) == 1
|
|
|
|
|
assert output.model == MODEL_NAME
|
|
|
|
|
assert output.usage.prompt_tokens == 34
|
|
|
|
|
|
|
|
|
|
# test continue_final_message
|
|
|
|
|
response = requests.post(
|
|
|
|
|
server.url_for("v1/embeddings"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"messages": messages,
|
|
|
|
|
"continue_final_message": True,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
output = EmbeddingResponse.model_validate(response.json())
|
|
|
|
|
|
|
|
|
|
assert output.object == "list"
|
|
|
|
|
assert len(output.data) == 1
|
|
|
|
|
assert output.model == MODEL_NAME
|
|
|
|
|
assert output.usage.prompt_tokens == 33
|
|
|
|
|
|
|
|
|
|
# test add_special_tokens
|
|
|
|
|
response = requests.post(
|
|
|
|
|
server.url_for("v1/embeddings"),
|
|
|
|
|
json={"model": model_name, "messages": messages, "add_special_tokens": True},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
output = EmbeddingResponse.model_validate(response.json())
|
|
|
|
|
|
|
|
|
|
assert output.object == "list"
|
|
|
|
|
assert len(output.data) == 1
|
|
|
|
|
assert output.model == MODEL_NAME
|
|
|
|
|
assert output.usage.prompt_tokens == 36
|
|
|
|
|
|
|
|
|
|
# test continue_final_message with add_generation_prompt
|
|
|
|
|
response = requests.post(
|
|
|
|
|
server.url_for("v1/embeddings"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"messages": messages,
|
|
|
|
|
"continue_final_message": True,
|
|
|
|
|
"add_generation_prompt": True,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
"Cannot set both `continue_final_message` and `add_generation_prompt` to True."
|
|
|
|
|
in response.json()["error"]["message"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_invocations_completion_request(
|
|
|
|
|
server: RemoteOpenAIServer, client: openai.AsyncOpenAI
|
|
|
|
|
):
|
2026-01-16 14:17:04 +08:00
|
|
|
request_args = {
|
|
|
|
|
"model": MODEL_NAME,
|
|
|
|
|
"input": input_text,
|
|
|
|
|
"encoding_format": "float",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
completion_response = await client.embeddings.create(**request_args)
|
|
|
|
|
|
|
|
|
|
invocation_response = requests.post(
|
|
|
|
|
server.url_for("invocations"), json=request_args
|
|
|
|
|
)
|
|
|
|
|
invocation_response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
completion_output = completion_response.model_dump()
|
|
|
|
|
invocation_output = invocation_response.json()
|
|
|
|
|
|
|
|
|
|
assert completion_output.keys() == invocation_output.keys()
|
|
|
|
|
for completion_data, invocation_data in zip(
|
|
|
|
|
completion_output["data"], invocation_output["data"]
|
|
|
|
|
):
|
|
|
|
|
assert completion_data.keys() == invocation_data.keys()
|
|
|
|
|
check_embeddings_close(
|
|
|
|
|
embeddings_0_lst=[completion_data["embedding"]],
|
|
|
|
|
embeddings_1_lst=[invocation_data["embedding"]],
|
|
|
|
|
name_0="completion",
|
|
|
|
|
name_1="invocation",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-01-22 18:32:44 +08:00
|
|
|
async def test_invocations_chat_request(server: RemoteOpenAIServer):
|
2026-01-16 14:17:04 +08:00
|
|
|
messages = [
|
|
|
|
|
{
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": "The cat sat on the mat.",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "A feline was resting on a rug.",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": "Stars twinkle brightly in the night sky.",
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
request_args = {
|
|
|
|
|
"model": MODEL_NAME,
|
|
|
|
|
"messages": messages,
|
|
|
|
|
"encoding_format": "float",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chat_response = requests.post(server.url_for("v1/embeddings"), json=request_args)
|
|
|
|
|
chat_response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
invocation_response = requests.post(
|
|
|
|
|
server.url_for("invocations"), json=request_args
|
|
|
|
|
)
|
|
|
|
|
invocation_response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
chat_output = chat_response.json()
|
|
|
|
|
invocation_output = invocation_response.json()
|
|
|
|
|
|
|
|
|
|
assert chat_output.keys() == invocation_output.keys()
|
|
|
|
|
for chat_data, invocation_data in zip(
|
|
|
|
|
chat_output["data"], invocation_output["data"]
|
|
|
|
|
):
|
|
|
|
|
assert chat_data.keys() == invocation_data.keys()
|
|
|
|
|
check_embeddings_close(
|
|
|
|
|
embeddings_0_lst=[chat_data["embedding"]],
|
|
|
|
|
embeddings_1_lst=[invocation_data["embedding"]],
|
|
|
|
|
name_0="chat",
|
|
|
|
|
name_1="invocation",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
|
|
|
|
async def test_base64_embedding(hf_model, client: openai.AsyncOpenAI, model_name: str):
|
2024-06-30 08:53:00 -07:00
|
|
|
input_texts = [
|
|
|
|
|
"Hello my name is",
|
|
|
|
|
"The best thing about vLLM is that it supports many different models",
|
|
|
|
|
]
|
|
|
|
|
|
2024-11-01 16:13:35 +08:00
|
|
|
responses_float = await client.embeddings.create(
|
|
|
|
|
input=input_texts, model=model_name, encoding_format="float"
|
|
|
|
|
)
|
2025-04-08 22:14:46 -06:00
|
|
|
float_data = [d.embedding for d in responses_float.data]
|
2025-05-01 14:03:08 +08:00
|
|
|
run_embedding_correctness_test(hf_model, input_texts, float_data)
|
2024-06-30 08:53:00 -07:00
|
|
|
|
2024-11-01 16:13:35 +08:00
|
|
|
responses_base64 = await client.embeddings.create(
|
|
|
|
|
input=input_texts, model=model_name, encoding_format="base64"
|
|
|
|
|
)
|
2025-04-08 22:14:46 -06:00
|
|
|
base64_data = []
|
2024-06-30 08:53:00 -07:00
|
|
|
for data in responses_base64.data:
|
2025-04-08 22:14:46 -06:00
|
|
|
base64_data.append(
|
2024-06-30 08:53:00 -07:00
|
|
|
np.frombuffer(base64.b64decode(data.embedding), dtype="float32").tolist()
|
2024-08-26 06:16:38 +03:00
|
|
|
)
|
2024-06-30 08:53:00 -07:00
|
|
|
|
2025-05-01 14:03:08 +08:00
|
|
|
run_embedding_correctness_test(hf_model, input_texts, base64_data)
|
2024-08-26 06:16:38 +03:00
|
|
|
|
|
|
|
|
# Default response is float32 decoded from base64 by OpenAI Client
|
2024-11-01 16:13:35 +08:00
|
|
|
responses_default = await client.embeddings.create(
|
|
|
|
|
input=input_texts, model=model_name
|
|
|
|
|
)
|
2025-04-08 22:14:46 -06:00
|
|
|
default_data = [d.embedding for d in responses_default.data]
|
2025-05-01 14:03:08 +08:00
|
|
|
run_embedding_correctness_test(hf_model, input_texts, default_data)
|
2024-10-04 15:31:40 -03:00
|
|
|
|
|
|
|
|
|
2025-10-14 03:06:43 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
2025-10-22 18:38:57 +08:00
|
|
|
async def test_base64_embed_dtype_and_endianness(
|
|
|
|
|
server: RemoteOpenAIServer, client: openai.AsyncOpenAI, model_name: str
|
2025-10-14 03:06:43 +08:00
|
|
|
):
|
2026-01-16 14:17:04 +08:00
|
|
|
input_texts = [input_text] * 3
|
2025-10-14 03:06:43 +08:00
|
|
|
responses_float = await client.embeddings.create(
|
|
|
|
|
input=input_texts, model=model_name, encoding_format="float"
|
|
|
|
|
)
|
|
|
|
|
float_data = [d.embedding for d in responses_float.data]
|
|
|
|
|
|
2025-10-22 18:38:57 +08:00
|
|
|
for embed_dtype in EMBED_DTYPE_TO_TORCH_DTYPE:
|
|
|
|
|
for endianness in ENDIANNESS:
|
|
|
|
|
responses_base64 = requests.post(
|
|
|
|
|
server.url_for("/v1/embeddings"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"input": input_texts,
|
|
|
|
|
"encoding_format": "base64",
|
|
|
|
|
"embed_dtype": embed_dtype,
|
|
|
|
|
"endianness": endianness,
|
|
|
|
|
},
|
2025-10-14 03:06:43 +08:00
|
|
|
)
|
|
|
|
|
|
2025-10-22 18:38:57 +08:00
|
|
|
base64_data = []
|
|
|
|
|
for data in responses_base64.json()["data"]:
|
|
|
|
|
binary = base64.b64decode(data["embedding"])
|
|
|
|
|
tensor = binary2tensor(binary, (-1,), embed_dtype, endianness)
|
|
|
|
|
base64_data.append(tensor.to(torch.float32).tolist())
|
|
|
|
|
|
|
|
|
|
check_embeddings_close(
|
|
|
|
|
embeddings_0_lst=float_data,
|
|
|
|
|
embeddings_1_lst=base64_data,
|
|
|
|
|
name_0="float_data",
|
|
|
|
|
name_1="base64_data",
|
|
|
|
|
tol=1e-2,
|
|
|
|
|
)
|
2025-10-14 03:06:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
2025-10-22 18:38:57 +08:00
|
|
|
async def test_bytes_embed_dtype_and_endianness(
|
|
|
|
|
server: RemoteOpenAIServer, client: openai.AsyncOpenAI, model_name: str
|
2025-10-14 03:06:43 +08:00
|
|
|
):
|
2026-01-16 14:17:04 +08:00
|
|
|
input_texts = [input_text] * 3
|
2025-10-22 18:38:57 +08:00
|
|
|
responses_float = await client.embeddings.create(
|
|
|
|
|
input=input_texts, model=model_name, encoding_format="float"
|
|
|
|
|
)
|
|
|
|
|
float_data = [d.embedding for d in responses_float.data]
|
|
|
|
|
|
|
|
|
|
for embed_dtype in list(EMBED_DTYPE_TO_TORCH_DTYPE.keys()):
|
|
|
|
|
for endianness in ENDIANNESS:
|
|
|
|
|
responses_bytes = requests.post(
|
|
|
|
|
server.url_for("/v1/embeddings"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"input": input_texts,
|
|
|
|
|
"encoding_format": "bytes",
|
|
|
|
|
"embed_dtype": embed_dtype,
|
|
|
|
|
"endianness": endianness,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
metadata = json.loads(responses_bytes.headers["metadata"])
|
|
|
|
|
body = responses_bytes.content
|
|
|
|
|
items = [MetadataItem(**x) for x in metadata["data"]]
|
|
|
|
|
|
|
|
|
|
bytes_data = decode_pooling_output(items=items, body=body)
|
|
|
|
|
bytes_data = [x.to(torch.float32).tolist() for x in bytes_data]
|
|
|
|
|
|
|
|
|
|
check_embeddings_close(
|
|
|
|
|
embeddings_0_lst=float_data,
|
|
|
|
|
embeddings_1_lst=bytes_data,
|
|
|
|
|
name_0="float_data",
|
|
|
|
|
name_1="bytes_data",
|
|
|
|
|
tol=1e-2,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-12-08 20:01:21 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
|
|
|
|
async def test_bytes_only_embed_dtype_and_endianness(
|
|
|
|
|
server: RemoteOpenAIServer, client: openai.AsyncOpenAI, model_name: str
|
|
|
|
|
):
|
|
|
|
|
input_texts = [
|
|
|
|
|
"The best thing about vLLM is that it supports many different models",
|
|
|
|
|
] * 2
|
|
|
|
|
|
|
|
|
|
responses_float = await client.embeddings.create(
|
|
|
|
|
input=input_texts, model=model_name, encoding_format="float"
|
|
|
|
|
)
|
|
|
|
|
float_data = [d.embedding for d in responses_float.data]
|
|
|
|
|
embedding_size = len(float_data[0])
|
|
|
|
|
|
|
|
|
|
for embed_dtype in list(EMBED_DTYPE_TO_TORCH_DTYPE.keys()):
|
|
|
|
|
for endianness in ENDIANNESS:
|
|
|
|
|
responses_bytes = requests.post(
|
|
|
|
|
server.url_for("/v1/embeddings"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"input": input_texts,
|
|
|
|
|
"encoding_format": "bytes_only",
|
|
|
|
|
"embed_dtype": embed_dtype,
|
|
|
|
|
"endianness": endianness,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert "metadata" not in responses_bytes.headers
|
|
|
|
|
body = responses_bytes.content
|
|
|
|
|
items = build_metadata_items(
|
|
|
|
|
embed_dtype=embed_dtype,
|
|
|
|
|
endianness=endianness,
|
|
|
|
|
shape=(embedding_size,),
|
|
|
|
|
n_request=len(input_texts),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
bytes_data = decode_pooling_output(items=items, body=body)
|
|
|
|
|
bytes_data = [x.to(torch.float32).tolist() for x in bytes_data]
|
|
|
|
|
|
|
|
|
|
check_embeddings_close(
|
|
|
|
|
embeddings_0_lst=float_data,
|
|
|
|
|
embeddings_1_lst=bytes_data,
|
|
|
|
|
name_0="float_data",
|
|
|
|
|
name_1="bytes_data",
|
|
|
|
|
tol=1e-2,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-22 18:38:57 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
|
|
|
|
@pytest.mark.parametrize("param_name", ["encoding_format", "embed_dtype", "endianness"])
|
|
|
|
|
async def test_params_not_supported(
|
|
|
|
|
server: RemoteOpenAIServer, model_name: str, param_name: str
|
|
|
|
|
):
|
2025-10-14 03:06:43 +08:00
|
|
|
responses_base64 = requests.post(
|
|
|
|
|
server.url_for("/v1/embeddings"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
2026-01-16 14:17:04 +08:00
|
|
|
"input": input_text,
|
2025-10-14 03:06:43 +08:00
|
|
|
"encoding_format": "base64",
|
2025-10-22 18:38:57 +08:00
|
|
|
param_name: f"bad_{param_name}",
|
2025-10-14 03:06:43 +08:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert responses_base64.status_code == 400
|
2025-10-22 18:38:57 +08:00
|
|
|
assert "literal_error" in responses_base64.json()["error"]["message"]
|
|
|
|
|
assert f"bad_{param_name}" in responses_base64.json()["error"]["message"]
|
2025-10-14 03:06:43 +08:00
|
|
|
|
|
|
|
|
|
2025-08-05 15:37:00 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
|
|
|
|
async def test_normalize(server: RemoteOpenAIServer, model_name: str):
|
|
|
|
|
async def get_outputs(normalize):
|
|
|
|
|
request_args = {
|
|
|
|
|
"model": MODEL_NAME,
|
|
|
|
|
"input": input_text,
|
|
|
|
|
"encoding_format": "float",
|
|
|
|
|
"normalize": normalize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response = requests.post(server.url_for("v1/embeddings"), json=request_args)
|
|
|
|
|
outputs = response.json()
|
|
|
|
|
|
|
|
|
|
return torch.tensor([x["embedding"] for x in outputs["data"]])
|
|
|
|
|
|
|
|
|
|
default = await get_outputs(normalize=None)
|
|
|
|
|
w_normal = await get_outputs(normalize=True)
|
|
|
|
|
wo_normal = await get_outputs(normalize=False)
|
|
|
|
|
|
|
|
|
|
assert torch.allclose(default, w_normal, atol=1e-2), "Default should use normal."
|
|
|
|
|
assert not torch.allclose(w_normal, wo_normal, atol=1e-2), (
|
|
|
|
|
"wo_normal should not use normal."
|
2025-10-05 15:06:22 +01:00
|
|
|
)
|
2025-08-05 15:37:00 +08:00
|
|
|
assert torch.allclose(w_normal, F.normalize(wo_normal, p=2, dim=-1), atol=1e-2), (
|
|
|
|
|
"w_normal should be close to normal(wo_normal)."
|
2025-10-05 15:06:22 +01:00
|
|
|
)
|
2025-10-15 19:14:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
2025-10-30 20:13:05 +08:00
|
|
|
async def test_pooling_embed(server: RemoteOpenAIServer, model_name: str):
|
|
|
|
|
task = "embed"
|
2025-10-15 19:14:41 +08:00
|
|
|
response = requests.post(
|
|
|
|
|
server.url_for("pooling"),
|
2025-10-30 20:13:05 +08:00
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"input": input_text,
|
|
|
|
|
"encoding_format": "float",
|
|
|
|
|
"task": task,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
poolings = PoolingResponse.model_validate(response.json())
|
|
|
|
|
|
|
|
|
|
assert len(poolings.data) == 1
|
|
|
|
|
assert len(poolings.data[0].data) == 384
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
|
|
|
|
async def test_pooling_token_embed(server: RemoteOpenAIServer, model_name: str):
|
|
|
|
|
task = "token_embed"
|
|
|
|
|
response = requests.post(
|
|
|
|
|
server.url_for("pooling"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"input": input_text,
|
|
|
|
|
"encoding_format": "float",
|
|
|
|
|
"task": task,
|
|
|
|
|
},
|
2025-10-15 19:14:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
poolings = PoolingResponse.model_validate(response.json())
|
|
|
|
|
|
|
|
|
|
assert len(poolings.data) == 1
|
2026-01-16 14:17:04 +08:00
|
|
|
assert len(poolings.data[0].data) == len(input_tokens)
|
2025-10-15 19:14:41 +08:00
|
|
|
assert len(poolings.data[0].data[0]) == 384
|
2025-10-30 20:13:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize("model_name", [MODEL_NAME])
|
|
|
|
|
@pytest.mark.parametrize("task", ["classify", "token_classify", "plugin"])
|
|
|
|
|
async def test_pooling_not_supported(
|
|
|
|
|
server: RemoteOpenAIServer, model_name: str, task: str
|
|
|
|
|
):
|
|
|
|
|
response = requests.post(
|
|
|
|
|
server.url_for("pooling"),
|
|
|
|
|
json={
|
|
|
|
|
"model": model_name,
|
|
|
|
|
"input": "test",
|
|
|
|
|
"encoding_format": "float",
|
|
|
|
|
"task": task,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
assert response.json()["error"]["type"] == "BadRequestError"
|
|
|
|
|
assert response.json()["error"]["message"].startswith(
|
|
|
|
|
f"Task {task} is not supported"
|
|
|
|
|
)
|