Skip to content

[BugFix] Support bf16 in zero-copy tensor serialization #16860

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

Closed
wants to merge 1 commit into from

Conversation

njhill
Copy link
Member

@njhill njhill commented Apr 18, 2025

Follow-on from #13790 and #16432.

Numpy doesn't support bfloat16 so we convert to/from a fp16 view.

Numpy doesn't support bfloat16 so we convert to/from a fp16 view.

Signed-off-by: Nick Hill <[email protected]>
@njhill njhill added the bug Something isn't working label Apr 18, 2025
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 Apr 18, 2025
if obj.dtype != torch.bfloat16:
return self._encode_ndarray(obj.numpy())
# Numpy doesn't support as bf16 so send as fp16 view.
_, shape, data = self._encode_ndarray(obj.view(torch.float16).numpy())
Copy link
Member

Choose a reason for hiding this comment

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

Might be dumb question, but is this conversion lossless? I thought we need to upcast it to fp32 to preserve precision since bf16 and fp16 have different exponents

Copy link
Contributor

@p88h p88h Apr 18, 2025

Choose a reason for hiding this comment

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

torch.view doesn't alter the data, it's more like a type cast in C.

@njhill -- What I thought of doing here is also this, but perhaps simplified / generalized more ->

  1. check if tensor is_contiguous
  2. store shape & dtype on the tensor
  3. create a view of the tensor as one-dimensional nbytes 8-bit int array
  4. encode that using numpy (since torch doesn't expose it's buffers directly)

And do that in reverse on the decode path. Torch has a bunch of weird 'bits' types that Numpy doesn't support, so probably doing this encoding for every single type doesn't make much sense. WDYT ?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ywang96 yes like @p88h said this isn't actually a conversion, it's just temporarily pretending that the raw bf16 data is raw fp16 data.

@p88h I like that idea for a general case but I'm not sure there's any other unsupported types that we care about here tbh, and it means some additional conversions for the common/supported types.

Copy link
Member Author

Choose a reason for hiding this comment

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

@p88h shall we get this merged to address the immediate issue and then you could open a follow-on PR for the above? Then we could microbenchmark it for the supported type cases...

Copy link
Contributor

@p88h p88h Apr 18, 2025

Choose a reason for hiding this comment

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

this is the more generic variant: #16866

There isn't any more conversions - view() is basically free, the only real difference is that this will now send a longer dtype string since it will use torch naming.

As for what types are there it is quite a bunch - https://pytorch.org/docs/stable/tensors.html
All of the quantized types, custom 8bit floats are not supported by numpy.

@njhill njhill added the ready ONLY add when PR is ready to merge/full CI is needed label Apr 18, 2025
sfc-gh-spasko pushed a commit to p88h/vllm that referenced this pull request Apr 18, 2025
@njhill
Copy link
Member Author

njhill commented Apr 19, 2025

Replaced by #16866

@njhill njhill closed this Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 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