Skip to content

[Core] Improve Tensor serialisation #18774

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

Conversation

lgeiger
Copy link
Contributor

@lgeiger lgeiger commented May 27, 2025

This improves v1 tensor serialisation by directly relying on torch.frombuffer which removes the need for an temporary numpy array which is a bit easier to read.

Here's a small micro benchmark to verify that this is also faster:

import numpy as np
import torch

from vllm.v1.serial_utils import MsgpackDecoder, MsgpackEncoder

encoder = MsgpackEncoder()
tensor_decoder = MsgpackDecoder(torch.Tensor)
numpy_decoder = MsgpackDecoder(np.ndarray)

array = np.random.rand(1, 3, 896, 896).astype(np.float32)
tensor = torch.tensor(array)
encoded_tensor = encoder.encode(tensor)
encoded_array = encoder.encode(array)

main:

Tensor encode/decode
5.44 μs ± 12.3 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
5.22 μs ± 9.13 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
Array encode/decode
5.46 μs ± 4.99 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
1.43 μs ± 4.35 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

This PR:

Tensor encode/decode
5.03 μs ± 3.81 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
4.41 μs ± 7.52 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
Array encode/decode
5.02 μs ± 23.8 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
1.45 μs ± 3.2 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

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 either: Add ready label to the PR or enable auto-merge.

🚀

@mergify mergify bot added the v1 label May 27, 2025
Copy link
Member

@njhill njhill left a comment

Choose a reason for hiding this comment

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

Thanks @lgeiger, LGTM

@njhill njhill added the ready ONLY add when PR is ready to merge/full CI is needed label May 27, 2025
@DarkLight1337 DarkLight1337 merged commit d73a945 into vllm-project:main May 28, 2025
72 checks passed
@lgeiger lgeiger deleted the improve-tensor-serialisation branch May 28, 2025 02:48
njhill added a commit that referenced this pull request May 28, 2025
@njhill
Copy link
Member

njhill commented May 28, 2025

I'm going to revert this since it broke some tests: #18857. We can look into the reason and open another PR as needed.

@lgeiger
Copy link
Contributor Author

lgeiger commented May 28, 2025

Oh so sorry, about that. I can have a look at it in a bit

@lgeiger
Copy link
Contributor Author

lgeiger commented May 28, 2025

#18860 Should fix it

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 v1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants