-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[FEATURE] Enables /score endpoint for embedding models #12846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 34 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
9b0ce65
[FEATURE] Enables offline /score for embedding models
gmarinho2 74cd2dd
Merge branch 'upstream_main'
gmarinho2 590ab4d
Changes variable name and uses tuple instead of list.
gmarinho2 f219024
Separates scoring logic and makes small ajustments
gmarinho2 47211e5
Separates scoring logic and makes small ajustments
gmarinho2 1a89033
Moves scoring functions declaration to llm class
gmarinho2 db7919b
Completes embedding_score function signature
gmarinho2 41b11be
Passes new parameters to self.encode() in embeddind_score
gmarinho2 b57e01a
Adds type annotations for the parameters
gmarinho2 4956eae
Minor adjustments
gmarinho2 f8b8d8c
Minor adjustments in embedding_score
gmarinho2 cd835de
trigger ci
gmarinho2 0e95ead
trigger ci
gmarinho2 bba2ea6
Merge branch 'vllm-project:main' into main
gmarinho2 635b8e8
Merge branch 'vllm-project:main' into main
gmarinho2 7851b44
first implementation of embedding scores via api
gmarinho2 12383b2
second version of api scoring
gmarinho2 12ef932
makes separate functions for cross-encoder score and embedding score
gmarinho2 945280f
fixes pre-commit errors
gmarinho2 053bdbb
adapts union sintax to python 3.9
gmarinho2 cfbc4b9
fixes alternating response bug
gmarinho2 a9b3a0d
fixes pre-commit errors
gmarinho2 4bcc9d5
Refactorings
maxdebayser 366ab62
fixing type errors
gmarinho2 bcf20df
fix typing errors
maxdebayser 9eaf4fc
remove assert
gmarinho2 f68bfaf
Merge branch 'main' into scoring-openai
gmarinho2 700603b
fix error type
maxdebayser 409ad05
Add unit tests for the scoring API with embedding models
maxdebayser 7e9478a
adds documentation
gmarinho2 056f1be
Refactor /rerank to reuse code from /score
maxdebayser 6a6e45b
Merge branch 'max-scoring-openai' into scoring-openai
gmarinho2 5c0495d
fixes union syntax
gmarinho2 7b2891b
adds documentation
gmarinho2 6218cc3
fixing mypy errors and refactoring
gmarinho2 5b54495
Puts embedding score code in score_utils to avoid duplicated code
gmarinho2 17a2960
changes variable name for clarity
gmarinho2 2a082f3
refactoring serving_score
gmarinho2 077cbae
factor out common code
maxdebayser 1fa73a8
remove extra code lines
maxdebayser c9a7240
Merge branch 'vllm-project:main' into scoring-openai
gmarinho2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,123 +1,185 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import math | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| import requests | ||
| import torch.nn.functional as F | ||
| from torch import tensor | ||
|
|
||
| from vllm.entrypoints.openai.protocol import ScoreResponse | ||
|
|
||
| from ...utils import RemoteOpenAIServer | ||
|
|
||
| MODEL_NAME = "BAAI/bge-reranker-v2-m3" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def server(): | ||
| args = ["--enforce-eager", "--max-model-len", "100"] | ||
|
|
||
| with RemoteOpenAIServer(MODEL_NAME, args) as remote_server: | ||
| MODELS = [ | ||
| { | ||
| "name": "BAAI/bge-reranker-v2-m3", | ||
| "is_cross_encoder": True | ||
| }, | ||
| { | ||
| "name": "BAAI/bge-base-en-v1.5", | ||
| "is_cross_encoder": False | ||
| }, | ||
| ] | ||
| DTYPE = "half" | ||
|
|
||
|
|
||
| def run_transformers(hf_model, model, text_pairs): | ||
| if model["is_cross_encoder"]: | ||
| return hf_model.predict(text_pairs).tolist() | ||
| else: | ||
| hf_embeddings = [ | ||
| hf_model.encode(text_pair) for text_pair in text_pairs | ||
| ] | ||
| return [ | ||
| F.cosine_similarity(tensor(pair[0]), tensor(pair[1]), dim=0) | ||
| for pair in hf_embeddings | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="class", params=MODELS) | ||
| def model(request): | ||
| yield request.param | ||
|
|
||
|
|
||
| @pytest.fixture(scope="class") | ||
| def server(model: dict[str, Any]): | ||
| args = ["--enforce-eager", "--max-model-len", "100", "--dtype", DTYPE] | ||
|
|
||
| with RemoteOpenAIServer(model["name"], args) as remote_server: | ||
| yield remote_server | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_text_1_str_text_2_list(server: RemoteOpenAIServer, model_name: str): | ||
| text_1 = "What is the capital of France?" | ||
| text_2 = [ | ||
| "The capital of Brazil is Brasilia.", "The capital of France is Paris." | ||
| ] | ||
|
|
||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model_name, | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| }) | ||
| score_response.raise_for_status() | ||
| score = ScoreResponse.model_validate(score_response.json()) | ||
|
|
||
| assert score.id is not None | ||
| assert score.data is not None | ||
| assert len(score.data) == 2 | ||
| assert score.data[0].score <= 0.01 | ||
| assert score.data[1].score >= 0.9 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_text_1_list_text_2_list(server: RemoteOpenAIServer, model_name: str): | ||
| text_1 = [ | ||
| "What is the capital of the United States?", | ||
| "What is the capital of France?" | ||
| ] | ||
| text_2 = [ | ||
| "The capital of Brazil is Brasilia.", "The capital of France is Paris." | ||
| ] | ||
|
|
||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model_name, | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| }) | ||
| score_response.raise_for_status() | ||
| score = ScoreResponse.model_validate(score_response.json()) | ||
|
|
||
| assert score.id is not None | ||
| assert score.data is not None | ||
| assert len(score.data) == 2 | ||
| assert score.data[0].score <= 0.01 | ||
| assert score.data[1].score >= 0.9 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_text_1_str_text_2_str(server: RemoteOpenAIServer, model_name: str): | ||
| text_1 = "What is the capital of France?" | ||
| text_2 = "The capital of France is Paris." | ||
|
|
||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model_name, | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| }) | ||
| score_response.raise_for_status() | ||
| score = ScoreResponse.model_validate(score_response.json()) | ||
|
|
||
| assert score.id is not None | ||
| assert score.data is not None | ||
| assert len(score.data) == 1 | ||
| assert score.data[0].score >= 0.9 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_score_max_model_len(server: RemoteOpenAIServer, model_name: str): | ||
|
|
||
| text_1 = "What is the capital of France?" * 20 | ||
| text_2 = [ | ||
| "The capital of Brazil is Brasilia.", "The capital of France is Paris." | ||
| ] | ||
|
|
||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model_name, | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| }) | ||
| assert score_response.status_code == 400 | ||
| # Assert just a small fragments of the response | ||
| assert "Please reduce the length of the input." in \ | ||
| score_response.text | ||
|
|
||
| # Test truncation | ||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model_name, | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| "truncate_prompt_tokens": 101 | ||
| }) | ||
| assert score_response.status_code == 400 | ||
| assert "Please, select a smaller truncation size." in \ | ||
| score_response.text | ||
| @pytest.fixture(scope="class") | ||
| def runner(model: dict[str, Any], hf_runner): | ||
| kwargs = { | ||
| "dtype": DTYPE, | ||
| "is_cross_encoder" if model["is_cross_encoder"]\ | ||
| else "is_sentence_transformer": True | ||
| } | ||
|
|
||
| with hf_runner(model["name"], **kwargs) as hf_model: | ||
| yield hf_model | ||
|
|
||
|
|
||
| class TestModel: | ||
|
|
||
| def test_text_1_str_text_2_list(self, server: RemoteOpenAIServer, | ||
| model: dict[str, Any], runner): | ||
| text_1 = "What is the capital of France?" | ||
| text_2 = [ | ||
| "The capital of Brazil is Brasilia.", | ||
| "The capital of France is Paris." | ||
| ] | ||
|
|
||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model["name"], | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| }) | ||
| score_response.raise_for_status() | ||
| score = ScoreResponse.model_validate(score_response.json()) | ||
|
|
||
| assert score.id is not None | ||
| assert score.data is not None | ||
| assert len(score.data) == 2 | ||
|
|
||
| vllm_outputs = [d.score for d in score.data] | ||
|
|
||
| text_pairs = [[text_1, text_2[0]], [text_1, text_2[1]]] | ||
| hf_outputs = run_transformers(runner, model, text_pairs) | ||
|
|
||
| for i in range(len(vllm_outputs)): | ||
| assert math.isclose(hf_outputs[i], vllm_outputs[i], rel_tol=0.01) | ||
|
|
||
| def test_text_1_list_text_2_list(self, server: RemoteOpenAIServer, | ||
| model: dict[str, Any], runner): | ||
| text_1 = [ | ||
| "What is the capital of the United States?", | ||
| "What is the capital of France?" | ||
| ] | ||
| text_2 = [ | ||
| "The capital of Brazil is Brasilia.", | ||
| "The capital of France is Paris." | ||
| ] | ||
|
|
||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model["name"], | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| }) | ||
| score_response.raise_for_status() | ||
| score = ScoreResponse.model_validate(score_response.json()) | ||
|
|
||
| assert score.id is not None | ||
| assert score.data is not None | ||
| assert len(score.data) == 2 | ||
|
|
||
| vllm_outputs = [d.score for d in score.data] | ||
|
|
||
| text_pairs = [[text_1[0], text_2[0]], [text_1[1], text_2[1]]] | ||
| hf_outputs = run_transformers(runner, model, text_pairs) | ||
|
|
||
| for i in range(len(vllm_outputs)): | ||
| assert math.isclose(hf_outputs[i], vllm_outputs[i], rel_tol=0.01) | ||
|
|
||
| def test_text_1_str_text_2_str(self, server: RemoteOpenAIServer, | ||
| model: dict[str, Any], runner): | ||
| text_1 = "What is the capital of France?" | ||
| text_2 = "The capital of France is Paris." | ||
|
|
||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model["name"], | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| }) | ||
| score_response.raise_for_status() | ||
| score = ScoreResponse.model_validate(score_response.json()) | ||
|
|
||
| assert score.id is not None | ||
| assert score.data is not None | ||
| assert len(score.data) == 1 | ||
|
|
||
| vllm_outputs = [d.score for d in score.data] | ||
|
|
||
| text_pairs = [[text_1, text_2]] | ||
| hf_outputs = run_transformers(runner, model, text_pairs) | ||
|
|
||
| for i in range(len(vllm_outputs)): | ||
| assert math.isclose(hf_outputs[i], vllm_outputs[i], rel_tol=0.01) | ||
|
|
||
| def test_score_max_model_len(self, server: RemoteOpenAIServer, | ||
| model: dict[str, Any]): | ||
|
|
||
| text_1 = "What is the capital of France?" * 20 | ||
| text_2 = [ | ||
| "The capital of Brazil is Brasilia.", | ||
| "The capital of France is Paris." | ||
| ] | ||
|
|
||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model["name"], | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| }) | ||
| assert score_response.status_code == 400 | ||
| # Assert just a small fragments of the response | ||
| assert "Please reduce the length of the input." in \ | ||
| score_response.text | ||
|
|
||
| # Test truncation | ||
| score_response = requests.post(server.url_for("score"), | ||
| json={ | ||
| "model": model["name"], | ||
| "text_1": text_1, | ||
| "text_2": text_2, | ||
| "truncate_prompt_tokens": 101 | ||
| }) | ||
| assert score_response.status_code == 400 | ||
| assert "Please, select a smaller truncation size." in \ | ||
| score_response.text |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.