Skip to content
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

Avoid unnecessary multi-modal input data copy when len(batch) == 1 #12722

Merged
merged 2 commits into from
Feb 4, 2025

Conversation

imkero
Copy link
Contributor

@imkero imkero commented Feb 4, 2025

A len=1 multi-modal data batch (List[torch.Tensor]) can avoid a copy operation by creating view of original tensor data if possible.

  • if len(batch) == 1 then
    • torch.stack(batch) -> batch[0].unsqueeze(0).contiguous()
    • torch.concat(batch) -> batch[0].contiguous()
  • NOTE: ensure contiguous to achieve consistent behaviour

Example code:

import torch

src_tensor = torch.rand((2, 3, 4))
batch = [src_tensor]

assert len(batch) == 1
# only works when batch size = 1

copied_batched_tensor = torch.stack(batch)
print(src_tensor.storage().data_ptr() == copied_batched_tensor.storage().data_ptr())
# False

no_copy_batched_tensor = src_tensor.unsqueeze(0).contiguous()
print(src_tensor.storage().data_ptr() == no_copy_batched_tensor.storage().data_ptr())
# True

print(torch.equal(copied_batched_tensor, no_copy_batched_tensor))
# True

Copy link

github-actions bot commented Feb 4, 2025

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can do one of these:

  • Add ready label to the PR
  • Enable auto-merge.

🚀

Copy link
Member

@DarkLight1337 DarkLight1337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for optimizing this! Can you leave a code comment explaining why we need separate logic?

@imkero imkero changed the title Avoid unnecessary multi-modal input data copy when bs=1 Avoid unnecessary multi-modal input data copy when len(batch) == 1 Feb 4, 2025
Copy link
Member

@ywang96 ywang96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the optimization!

@imkero
Copy link
Contributor Author

imkero commented Feb 4, 2025

Thanks for optimizing this! Can you leave a code comment explaining why we need separate logic?

Sure! I will update it soon.

Signed-off-by: imkero <[email protected]>
@DarkLight1337 DarkLight1337 added the ready ONLY add when PR is ready to merge/full CI is needed label Feb 4, 2025
@DarkLight1337 DarkLight1337 merged commit 62467a8 into vllm-project:main Feb 4, 2025
46 of 48 checks passed
fxmarty-amd pushed a commit to fxmarty-amd/vllm that referenced this pull request Feb 7, 2025
ShangmingCai pushed a commit to ShangmingCai/vllm that referenced this pull request Feb 10, 2025
panf2333 pushed a commit to yottalabsai/vllm that referenced this pull request Feb 18, 2025
kerthcet pushed a commit to kerthcet/vllm that referenced this pull request Feb 21, 2025
lk-chen pushed a commit to lk-chen/vllm that referenced this pull request Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants