forked from vllm-project/vllm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core][VLM] Stack multimodal tensors to represent multiple images wit…
…hin each prompt (vllm-project#7902)
- Loading branch information
1 parent
b0ce749
commit b7e7162
Showing
15 changed files
with
214 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 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,83 @@ | ||
import torch | ||
|
||
from vllm.multimodal.base import MultiModalInputs, NestedTensors | ||
|
||
|
||
def assert_nested_tensors_equal(expected: NestedTensors, | ||
actual: NestedTensors): | ||
assert type(expected) == type(actual) | ||
if isinstance(expected, torch.Tensor): | ||
assert torch.equal(expected, actual) | ||
else: | ||
for expected_item, actual_item in zip(expected, actual): | ||
assert_nested_tensors_equal(expected_item, actual_item) | ||
|
||
|
||
def assert_multimodal_inputs_equal(expected: MultiModalInputs, | ||
actual: MultiModalInputs): | ||
assert set(expected.keys()) == set(actual.keys()) | ||
for key in expected: | ||
assert_nested_tensors_equal(expected[key], actual[key]) | ||
|
||
|
||
def test_multimodal_input_batch_single_tensor(): | ||
t = torch.rand([1, 2]) | ||
result = MultiModalInputs.batch([{"image": t}]) | ||
assert_multimodal_inputs_equal(result, {"image": t.unsqueeze(0)}) | ||
|
||
|
||
def test_multimodal_input_batch_multiple_tensors(): | ||
a = torch.rand([1, 1, 2]) | ||
b = torch.rand([1, 1, 2]) | ||
c = torch.rand([1, 1, 2]) | ||
result = MultiModalInputs.batch([{"image": a}, {"image": b}, {"image": c}]) | ||
assert_multimodal_inputs_equal(result, {"image": torch.stack([a, b, c])}) | ||
|
||
|
||
def test_multimodal_input_batch_multiple_heterogeneous_tensors(): | ||
a = torch.rand([1, 2, 2]) | ||
b = torch.rand([1, 3, 2]) | ||
c = torch.rand([1, 4, 2]) | ||
result = MultiModalInputs.batch([{"image": a}, {"image": b}, {"image": c}]) | ||
assert_multimodal_inputs_equal(result, {"image": [a, b, c]}) | ||
|
||
|
||
def test_multimodal_input_batch_nested_tensors(): | ||
a = torch.rand([2, 3]) | ||
b = torch.rand([2, 3]) | ||
c = torch.rand([2, 3]) | ||
result = MultiModalInputs.batch([{ | ||
"image": [a] | ||
}, { | ||
"image": [b] | ||
}, { | ||
"image": [c] | ||
}]) | ||
assert_multimodal_inputs_equal(result, { | ||
"image": | ||
torch.stack([a.unsqueeze(0), | ||
b.unsqueeze(0), | ||
c.unsqueeze(0)]) | ||
}) | ||
|
||
|
||
def test_multimodal_input_batch_heterogeneous_lists(): | ||
a = torch.rand([1, 2, 3]) | ||
b = torch.rand([1, 2, 3]) | ||
c = torch.rand([1, 2, 3]) | ||
result = MultiModalInputs.batch([{"image": [a, b]}, {"image": [c]}]) | ||
assert_multimodal_inputs_equal( | ||
result, | ||
{"image": [torch.stack([a, b]), c.unsqueeze(0)]}) | ||
|
||
|
||
def test_multimodal_input_batch_multiple_batchable_lists(): | ||
a = torch.rand([1, 2, 3]) | ||
b = torch.rand([1, 2, 3]) | ||
c = torch.rand([1, 2, 3]) | ||
d = torch.rand([1, 2, 3]) | ||
result = MultiModalInputs.batch([{"image": [a, b]}, {"image": [c, d]}]) | ||
assert_multimodal_inputs_equal( | ||
result, | ||
{"image": torch.stack([torch.stack([a, b]), | ||
torch.stack([c, d])])}) |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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.