-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Model] CLIP Embedding Support #26010
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2ab3c9e
[Model] Support CLIP Embeddings
DarkLight1337 5f99748
Add examples
DarkLight1337 d93e867
Remove dead code
DarkLight1337 729f874
Remove outdated docstring
DarkLight1337 d58ef3b
Use `all_special_ids`
DarkLight1337 8fa5dc3
Use Attention instead of MultiHeadAttention for text encoder
DarkLight1337 e123c1f
Fix the test
DarkLight1337 8415fa7
Rename to avoid confusion
DarkLight1337 93c4490
Fix the table
DarkLight1337 ee205e1
Merge branch 'main' into support-clip-embed
DarkLight1337 bc7f8de
Fix and improve registry error
DarkLight1337 9ba3364
Merge branch 'main' into support-clip-embed
DarkLight1337 11f76eb
Fix dummy data
DarkLight1337 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 |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
||
import pytest | ||
from transformers import CLIPModel | ||
|
||
from ....conftest import IMAGE_ASSETS, HfRunner, PromptImageInput, VllmRunner | ||
from ...utils import check_embeddings_close | ||
|
||
HF_TEXT_PROMPTS = [ | ||
"a photo of a stop sign", | ||
"a photo of a cherry blossom", | ||
] | ||
|
||
HF_IMAGE_PROMPTS = IMAGE_ASSETS.prompts({ | ||
"stop_sign": "", | ||
"cherry_blossom": "", | ||
}) | ||
|
||
MODELS = ["openai/clip-vit-base-patch32"] | ||
|
||
|
||
def _run_test( | ||
hf_runner: type[HfRunner], | ||
vllm_runner: type[VllmRunner], | ||
input_texts: list[str], | ||
input_images: PromptImageInput, | ||
model: str, | ||
*, | ||
dtype: str, | ||
) -> None: | ||
# NOTE: take care of the order. run vLLM first, and then run HF. | ||
# vLLM needs a fresh new process without cuda initialization. | ||
# if we run HF first, the cuda initialization will be done and it | ||
# will hurt multiprocessing backend with fork method (the default method). | ||
with vllm_runner(model, | ||
runner="pooling", | ||
dtype=dtype, | ||
enforce_eager=True, | ||
max_model_len=77) as vllm_model: | ||
vllm_outputs = vllm_model.embed(input_texts, images=input_images) | ||
|
||
with hf_runner(model, dtype=dtype, auto_cls=CLIPModel) as hf_model: | ||
all_inputs = hf_model.get_inputs(input_texts, images=input_images) | ||
|
||
all_outputs = [] | ||
for inputs in all_inputs: | ||
if "pixel_values" in inputs: | ||
inputs.pop("input_ids") | ||
pooled_output = hf_model.model.get_image_features( | ||
**hf_model.wrap_device(inputs)).squeeze(0) | ||
else: | ||
pooled_output = hf_model.model.get_text_features( | ||
**hf_model.wrap_device(inputs)).squeeze(0) | ||
|
||
all_outputs.append(pooled_output.tolist()) | ||
|
||
hf_outputs = all_outputs | ||
|
||
check_embeddings_close( | ||
embeddings_0_lst=hf_outputs, | ||
embeddings_1_lst=vllm_outputs, | ||
name_0="hf", | ||
name_1="vllm", | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("model", MODELS) | ||
@pytest.mark.parametrize("dtype", ["float"]) | ||
def test_models_text( | ||
hf_runner, | ||
vllm_runner, | ||
image_assets, | ||
model: str, | ||
dtype: str, | ||
) -> None: | ||
input_texts_images = [(text, None) for text in HF_TEXT_PROMPTS] | ||
input_texts = [text for text, _ in input_texts_images] | ||
input_images = [image for _, image in input_texts_images] | ||
|
||
_run_test( | ||
hf_runner, | ||
vllm_runner, | ||
input_texts, | ||
input_images, # type: ignore | ||
model, | ||
dtype=dtype, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("model", MODELS) | ||
@pytest.mark.parametrize("dtype", ["float"]) | ||
def test_models_image( | ||
hf_runner, | ||
vllm_runner, | ||
image_assets, | ||
model: str, | ||
dtype: str, | ||
) -> None: | ||
input_texts_images = [ | ||
(text, asset.pil_image) | ||
for text, asset in zip(HF_IMAGE_PROMPTS, image_assets) | ||
] | ||
input_texts = [text for text, _ in input_texts_images] | ||
input_images = [image for _, image in input_texts_images] | ||
|
||
_run_test( | ||
hf_runner, | ||
vllm_runner, | ||
input_texts, | ||
input_images, | ||
model, | ||
dtype=dtype, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("model", MODELS) | ||
@pytest.mark.parametrize("dtype", ["float"]) | ||
def test_models_text_image_no_crash( | ||
vllm_runner, | ||
image_assets, | ||
model: str, | ||
dtype: str, | ||
) -> None: | ||
texts = [HF_TEXT_PROMPTS[0]] | ||
images = [image_assets[0].pil_image] | ||
|
||
with vllm_runner(model, | ||
runner="pooling", | ||
dtype=dtype, | ||
enforce_eager=True, | ||
max_model_len=77) as vllm_model: | ||
with pytest.raises(ValueError, match="not both"): | ||
vllm_model.embed(texts, images=images) | ||
|
||
# Should still be able to run subsequent requests | ||
vllm_model.embed(texts) | ||
vllm_model.embed([""], images=images) |
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
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.