-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
[Model][2/N] Improve all pooling task | Support multi-vector retrieval #25370
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
+786
−399
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
0e89f16
Support multi-vector retrieval
noooop dff015e
Merge branch 'main' into multi_vector_retrieval
noooop bb68b24
fix tests
noooop 3690760
fix gpt2
noooop 314ff69
conflicts
noooop b6162d2
conflicts
noooop e476596
conflicts
noooop d746956
Merge branch 'main' into multi_vector_retrieval
noooop 831107e
add back
noooop a6c7991
fix
noooop 6bb154a
Merge branch 'main' into multi_vector_retrieval
noooop dd143a9
fix
noooop a70524b
fix
noooop 4a217eb
fix
noooop 924d040
Merge branch 'main' into multi_vector_retrieval
noooop 87cfb18
fix
noooop 33cb313
Merge branch 'main' into multi_vector_retrieval
noooop f17f566
gemini
noooop f38ef5d
fix IOProcessor
noooop b3ddb8a
fix IOProcessor
noooop 0478277
Merge branch 'main' into multi_vector_retrieval
noooop 4d56660
fix IOProcessor
noooop 60db627
fix test_prithvi_mae.py
noooop 9c9150b
Merge branch 'main' into multi_vector_retrieval
noooop 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
56 changes: 56 additions & 0 deletions
56
examples/offline_inference/pooling/multi_vector_retrieval.py
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| from argparse import Namespace | ||
|
|
||
| from vllm import LLM, EngineArgs | ||
| from vllm.utils import FlexibleArgumentParser | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = FlexibleArgumentParser() | ||
| parser = EngineArgs.add_cli_args(parser) | ||
| # Set example specific arguments | ||
| parser.set_defaults( | ||
| model="BAAI/bge-m3", | ||
| runner="pooling", | ||
| enforce_eager=True, | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main(args: Namespace): | ||
| # Sample prompts. | ||
| prompts = [ | ||
| "Hello, my name is", | ||
| "The president of the United States is", | ||
| "The capital of France is", | ||
| "The future of AI is", | ||
| ] | ||
|
|
||
| # Create an LLM. | ||
| # You should pass runner="pooling" for embedding models | ||
| llm = LLM(**vars(args)) | ||
|
|
||
| # Generate embedding. The output is a list of EmbeddingRequestOutputs. | ||
| outputs = llm.embed(prompts) | ||
|
|
||
| # Print the outputs. | ||
| print("\nGenerated Outputs:\n" + "-" * 60) | ||
| for prompt, output in zip(prompts, outputs): | ||
| embeds = output.outputs.embedding | ||
| print(len(embeds)) | ||
|
|
||
| # Generate embedding for each token. The output is a list of PoolingRequestOutput. | ||
| outputs = llm.encode(prompts, pooling_task="token_embed") | ||
|
|
||
| # Print the outputs. | ||
| print("\nGenerated Outputs:\n" + "-" * 60) | ||
| for prompt, output in zip(prompts, outputs): | ||
| multi_vector = output.outputs.data | ||
| print(multi_vector.shape) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = parse_args() | ||
| main(args) |
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
54 changes: 54 additions & 0 deletions
54
examples/online_serving/pooling/multi_vector_retrieval_client.py
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| """ | ||
| Example online usage of Pooling API for multi vector retrieval. | ||
|
|
||
| Run `vllm serve <model> --runner pooling` | ||
| to start up the server in vLLM. e.g. | ||
|
|
||
| vllm serve BAAI/bge-m3 | ||
| """ | ||
|
|
||
| import argparse | ||
|
|
||
| import requests | ||
| import torch | ||
|
|
||
|
|
||
| def post_http_request(prompt: dict, api_url: str) -> requests.Response: | ||
| headers = {"User-Agent": "Test Client"} | ||
| response = requests.post(api_url, headers=headers, json=prompt) | ||
| return response | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--host", type=str, default="localhost") | ||
| parser.add_argument("--port", type=int, default=8000) | ||
| parser.add_argument("--model", type=str, default="BAAI/bge-m3") | ||
|
|
||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main(args): | ||
| api_url = f"http://{args.host}:{args.port}/pooling" | ||
| model_name = args.model | ||
|
|
||
| prompts = [ | ||
| "Hello, my name is", | ||
| "The president of the United States is", | ||
| "The capital of France is", | ||
| "The future of AI is", | ||
| ] | ||
| prompt = {"model": model_name, "input": prompts} | ||
|
|
||
| pooling_response = post_http_request(prompt=prompt, api_url=api_url) | ||
| for output in pooling_response.json()["data"]: | ||
| multi_vector = torch.tensor(output["data"]) | ||
| print(multi_vector.shape) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = parse_args() | ||
| main(args) |
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
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
45 changes: 45 additions & 0 deletions
45
tests/models/language/pooling/test_multi_vector_retrieval.py
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| import pytest | ||
| import torch | ||
| from transformers import AutoModel | ||
|
|
||
| from tests.models.utils import check_embeddings_close | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "model", | ||
| ["BAAI/bge-m3"], | ||
| ) | ||
| @pytest.mark.parametrize("dtype", ["half"]) | ||
| @torch.inference_mode | ||
| def test_embed_models(hf_runner, vllm_runner, example_prompts, model: str, dtype: str): | ||
| with vllm_runner( | ||
| model, | ||
| runner="pooling", | ||
| max_model_len=None, | ||
| ) as vllm_model: | ||
| vllm_outputs = vllm_model.token_embed(example_prompts) | ||
|
|
||
| with hf_runner( | ||
| model, | ||
| auto_cls=AutoModel, | ||
| ) as hf_model: | ||
| tokenizer = hf_model.tokenizer | ||
| hf_outputs = [] | ||
| for prompt in example_prompts: | ||
| inputs = tokenizer([prompt], return_tensors="pt") | ||
| inputs = hf_model.wrap_device(inputs) | ||
| output = hf_model.model(**inputs) | ||
| embedding = output.last_hidden_state[0].float() | ||
| # normal | ||
| hf_outputs.append(embedding.cpu()) | ||
|
|
||
| for hf_output, vllm_output in zip(hf_outputs, vllm_outputs): | ||
| check_embeddings_close( | ||
| embeddings_0_lst=hf_output, | ||
| embeddings_1_lst=vllm_output, | ||
| name_0="hf", | ||
| name_1="vllm", | ||
| tol=1e-2, | ||
| ) |
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.