-
Notifications
You must be signed in to change notification settings - Fork 94
[Bugfix][Core] Fix multimodal prompt building for rows without media #404
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
Open
natedemoss
wants to merge
4
commits into
vllm-project:main
Choose a base branch
from
natedemoss:fix/multimodal-prompt-building
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−12
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| """CPU unit tests for ``vime.utils.data._build_messages``. | ||
|
|
||
| ``--multimodal-keys`` maps a media type name to the dataset column holding that | ||
| media, and the prompt marks insertion points with the type's placeholder | ||
| (``<image>``, ``<video>``, ``<audio>``). ``_build_messages`` rewrites a plain | ||
| string prompt into the list-of-content-dicts form the VLM chat templates and | ||
| ``process_vision_info`` expect. | ||
|
|
||
| The cases below pin down what happens at the edges of that rewrite: a row of a | ||
| multimodal dataset that carries no media at all, a row that is already in | ||
| list-of-dicts form, and a ``--multimodal-keys`` mapping that names a type vime | ||
| does not support. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from vime.utils.data import _build_messages | ||
|
|
||
|
|
||
| NUM_GPUS = 0 | ||
|
|
||
| IMAGE_KEYS = {"image": "images"} | ||
|
|
||
|
|
||
| def _row(prompt, **columns): | ||
| return {"text": prompt, **columns} | ||
|
|
||
|
|
||
| def _content(messages): | ||
| assert len(messages) == 1, f"expected a single message, got {len(messages)}" | ||
| return messages[0]["content"] | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_media_placeholders_are_replaced(): | ||
| messages = _build_messages(_row("What is in <image>?", images=["a.png"]), "text", True, IMAGE_KEYS) | ||
|
|
||
| assert _content(messages) == [ | ||
| {"type": "text", "text": "What is in "}, | ||
| {"type": "image", "image": "a.png"}, | ||
| {"type": "text", "text": "?"}, | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_media_entries_may_be_rich_dicts(): | ||
| item = {"type": "image", "image": "a.png", "max_pixels": 50176} | ||
| messages = _build_messages(_row("<image> here", images=[item]), "text", True, IMAGE_KEYS) | ||
|
|
||
| assert _content(messages) == [item, {"type": "text", "text": " here"}] | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_single_media_entry_as_string_or_dict_is_wrapped(): | ||
| # Test single string | ||
| messages = _build_messages(_row("What is in <image>?", images="a.png"), "text", True, IMAGE_KEYS) | ||
| assert _content(messages) == [ | ||
| {"type": "text", "text": "What is in "}, | ||
| {"type": "image", "image": "a.png"}, | ||
| {"type": "text", "text": "?"}, | ||
| ] | ||
|
|
||
| # Test single dict | ||
| item = {"type": "image", "image": "a.png", "max_pixels": 50176} | ||
| messages = _build_messages(_row("<image> here", images=item), "text", True, IMAGE_KEYS) | ||
| assert _content(messages) == [item, {"type": "text", "text": " here"}] | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_row_without_media_keeps_its_prompt_intact(): | ||
| """A mixed dataset has rows with no media; those prompts must stay intact. | ||
|
|
||
| Building the split pattern from an empty placeholder set yields ``"()"``, | ||
| which matches the empty string at every position, so the prompt used to come | ||
| back as one ``{"type": "text"}`` dict per character. | ||
| """ | ||
| prompt = "Describe this image." | ||
| messages = _build_messages(_row(prompt, images=None), "text", True, IMAGE_KEYS) | ||
|
|
||
| # Same representation a text-only dataset gets, i.e. no `--multimodal-keys`. | ||
| assert _content(messages) == prompt | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_row_with_an_empty_media_column_keeps_one_text_segment(): | ||
| prompt = "Describe this image." | ||
| messages = _build_messages(_row(prompt, images=[]), "text", True, IMAGE_KEYS) | ||
|
|
||
| assert _content(messages) == [{"type": "text", "text": prompt}] | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_unknown_media_type_is_rejected(): | ||
| """A typo like ``images`` used to be skipped silently: the media never made | ||
| it into the prompt and training ran text-only against a VLM dataset.""" | ||
| with pytest.raises(ValueError, match="Unknown multimodal type 'images'"): | ||
| _build_messages(_row("What is in <image>?", images=["a.png"]), "text", True, {"images": "images"}) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_list_content_row_is_passed_through(): | ||
| """Content already in list-of-dicts form embeds its media inline, so there | ||
| is no placeholder for this function to spend the row's media on.""" | ||
| content = [{"type": "image", "image": "a.png"}, {"type": "text", "text": "What is in it?"}] | ||
| prompt = [{"role": "user", "content": list(content)}] | ||
|
|
||
| messages = _build_messages(_row(prompt, images=["a.png"]), "text", True, IMAGE_KEYS) | ||
|
|
||
| assert _content(messages) == content | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_more_media_than_placeholders_still_raises(): | ||
| with pytest.raises(AssertionError, match="Multimodal data count mismatch"): | ||
| _build_messages(_row("What is in <image>?", images=["a.png", "b.png"]), "text", True, IMAGE_KEYS) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_more_placeholders_than_media_still_raises(): | ||
| with pytest.raises(AssertionError, match="Not enough image data"): | ||
| _build_messages(_row("<image> vs <image>", images=["a.png"]), "text", True, IMAGE_KEYS) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_without_multimodal_keys_the_prompt_is_untouched(): | ||
| prompt = "Describe this image." | ||
|
|
||
| assert _build_messages(_row(prompt), "text", False, None) == prompt | ||
| assert _content(_build_messages(_row(prompt), "text", True, None)) == prompt | ||
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
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.