Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
62467a83
Unverified
Commit
62467a83
authored
Feb 04, 2025
by
Kero Liang
Committed by
GitHub
Feb 04, 2025
Browse files
Avoid unnecessary multi-modal input data copy when len(batch) == 1 (#12722)
Signed-off-by:
imkero
<
kerorek@outlook.com
>
parent
6469038b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
0 deletions
+16
-0
vllm/multimodal/inputs.py
vllm/multimodal/inputs.py
+16
-0
No files found.
vllm/multimodal/inputs.py
View file @
62467a83
...
@@ -212,6 +212,11 @@ class MultiModalBatchedField(BaseMultiModalField):
...
@@ -212,6 +212,11 @@ class MultiModalBatchedField(BaseMultiModalField):
def
_reduce_data
(
self
,
batch
:
list
[
NestedTensors
])
->
NestedTensors
:
def
_reduce_data
(
self
,
batch
:
list
[
NestedTensors
])
->
NestedTensors
:
if
len
(
batch
)
>
0
and
is_list_of
(
batch
,
torch
.
Tensor
,
check
=
"all"
):
if
len
(
batch
)
>
0
and
is_list_of
(
batch
,
torch
.
Tensor
,
check
=
"all"
):
if
len
(
batch
)
==
1
:
# An optimization when `batch` contains only one tensor:
# - produce exactly same result as `torch.stack(batch)`
# - will achieve zero-copy if the tensor is contiguous
return
batch
[
0
].
unsqueeze
(
0
).
contiguous
()
first_shape
=
batch
[
0
].
shape
first_shape
=
batch
[
0
].
shape
if
all
(
elem
.
shape
==
first_shape
for
elem
in
batch
):
if
all
(
elem
.
shape
==
first_shape
for
elem
in
batch
):
return
torch
.
stack
(
batch
)
return
torch
.
stack
(
batch
)
...
@@ -235,6 +240,11 @@ class MultiModalFlatField(BaseMultiModalField):
...
@@ -235,6 +240,11 @@ class MultiModalFlatField(BaseMultiModalField):
def
_reduce_data
(
self
,
batch
:
list
[
NestedTensors
])
->
NestedTensors
:
def
_reduce_data
(
self
,
batch
:
list
[
NestedTensors
])
->
NestedTensors
:
if
len
(
batch
)
>
0
and
is_list_of
(
batch
,
torch
.
Tensor
,
check
=
"all"
):
if
len
(
batch
)
>
0
and
is_list_of
(
batch
,
torch
.
Tensor
,
check
=
"all"
):
if
len
(
batch
)
==
1
:
# An optimization when `batch` contains only one tensor:
# - produce exactly same result as `torch.concat(batch)`
# - will achieve zero-copy if the tensor is contiguous
return
batch
[
0
].
contiguous
()
first_shape
=
batch
[
0
].
shape
first_shape
=
batch
[
0
].
shape
if
all
(
elem
.
shape
[
1
:]
==
first_shape
[
1
:]
for
elem
in
batch
):
if
all
(
elem
.
shape
[
1
:]
==
first_shape
[
1
:]
for
elem
in
batch
):
return
torch
.
concat
(
batch
)
return
torch
.
concat
(
batch
)
...
@@ -407,6 +417,12 @@ class MultiModalKwargs(UserDict[str, NestedTensors]):
...
@@ -407,6 +417,12 @@ class MultiModalKwargs(UserDict[str, NestedTensors]):
return
stacked
return
stacked
tensors_
=
cast
(
list
[
torch
.
Tensor
],
stacked
)
tensors_
=
cast
(
list
[
torch
.
Tensor
],
stacked
)
if
len
(
tensors_
)
==
1
:
# An optimization when `tensors_` contains only one tensor:
# - produce exactly same result as `torch.stack(tensors_)`
# - will achieve zero-copy if the tensor is contiguous
return
tensors_
[
0
].
unsqueeze
(
0
).
contiguous
()
if
any
(
t
.
shape
!=
tensors_
[
0
].
shape
for
t
in
tensors_
):
if
any
(
t
.
shape
!=
tensors_
[
0
].
shape
for
t
in
tensors_
):
# The tensors have incompatible shapes and can't be stacked.
# The tensors have incompatible shapes and can't be stacked.
return
tensors_
return
tensors_
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment