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

[Core] Zero-copy asdict for InputMetadata #3475

Merged
merged 4 commits into from
Mar 18, 2024

Conversation

Yard1
Copy link
Collaborator

@Yard1 Yard1 commented Mar 18, 2024

PR Checklist (Click to expand. Please read before submitting.)

Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Model] for adding a new model or improving an existing model. Model name should appear in the title.
  • [Frontend] For changes on the vLLM frontend (e.g., OpenAI API server, LLM class, etc.)
  • [Kernel] for changes affecting CUDA kernels or other compute kernels.
  • [Core] for changes in the core vLLM logic (e.g., LLMEngine, AsyncLLMEngine, Scheduler, etc.)
  • [Hardware][Vendor] for hardware-specific changes. Vendor name should appear in the prefix (e.g., [Hardware][AMD]).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • We adhere to Google Python style guide and Google C++ style guide.
  • Pass all linter checks. Please use format.sh to format your code.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
  • Please add documentation to docs/source/ if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.

Notes for Large Changes

Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with rfc-required and might not go through the PR.

What to Expect for the Reviews

The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:

  • After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
  • After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
  • After the review, the reviewer will put an action-required label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.
  • Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.

Thank You

Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!


dataclasses.asdict will deepcopy tensors inside InputMetadata, leading to an avoidable overhead. This PR adds a new method to InputMetadata which will return a dict of fields and their values without copy.

Before:
Screenshot 2024-03-18 at 1 30 04 PM

After:
Screenshot 2024-03-18 at 1 29 56 PM

@@ -527,7 +526,7 @@ def prepare_input_tensors(
"lora_requests": lora_requests,
"lora_mapping": lora_mapping,
}
metadata_dict.update(dataclasses.asdict(input_metadata))
metadata_dict.update(input_metadata.asdict_zerocopy())
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you just do input_metadata.__dict__ to avoid the new method at all?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps, though that may lead to unexpected interactions if InputMetadata has extra properties added and such.

Copy link
Member

Choose a reason for hiding this comment

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

No, because the post init adds self.attn_bias = None.

@youkaichao
Copy link
Member

It seems the deepcopy of dataclasses.asdict is notorious, see python/cpython#88071 .

How about moving to attrs? https://www.attrs.org/en/stable/index.html it is not a builtin package, but just a third-party library.

from attrs import asdict, define
@define
class MyDataClass:
    a: int
    b: list
instance = MyDataClass(1, [2, 3])
data1 = asdict(instance, recurse=False)
assert data1['b'] is instance.b

Its asdict has a recursive parameter.

@youkaichao
Copy link
Member

Here is the whole picture:

import attrs
@attrs.define(slots=False)
class MyDataClass:
    a: int
    b: list
    def __attrs_post_init__(self):
        self.c = None
instance = MyDataClass(1, [2, 3])
data1 = asdict(instance, recurse=False)
assert data1['b'] is instance.b # make sure it is shallow copy
assert "c" not in data1 # make sure asdict only contains annotated fields

@Yard1
Copy link
Collaborator Author

Yard1 commented Mar 18, 2024

I don't think it makes sense to add a third-party library just for this one specific usecase. We can always revisit this if we find ourselves implementing this pattern more times.

@youkaichao
Copy link
Member

Oh, attrs is quite a famous library, and it motivates the builtin dataclasses.

One reason that attrs does not become builtin, is because attrs does not want to be bound with python versions. So you can basically think attrs as an advanced replacement of dataclasses.

e.g. when you want to use new features from dataclasses, you have to upgrade python interpreter; however, with attrs, you just need to update a package.

I vote for attrs because later I plan to refactor vllm/config.py to use dataclass-like features (so that we can easily print/log them for debugging). And I might switch to attrs in all the places.

Open to any feedback :)

@Yard1
Copy link
Collaborator Author

Yard1 commented Mar 18, 2024

I think it's fine if your PR makes use of it, but I don't think it's necessary to couple attrs with this particular PR :)

@youkaichao
Copy link
Member

Then please make sure to rename asdict_zerocopy to _asdict_zerocopy, so that users know this is private and they won't call it.

@WoosukKwon
Copy link
Collaborator

WoosukKwon commented Mar 18, 2024

Then please make sure to rename asdict_zerocopy to _asdict_zerocopy, so that users know this is private and they won't call it.

I think it should be asdict_zerocopy since it's used outside InputMetadata?

Co-authored-by: Woosuk Kwon <woosuk.kwon@berkeley.edu>
Copy link
Collaborator

@WoosukKwon WoosukKwon left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for finding the performance bug. Didn't know the issue in dataclasses.asdict. TIL 😄

@WoosukKwon WoosukKwon enabled auto-merge (squash) March 18, 2024 22:01
@WoosukKwon
Copy link
Collaborator

@Yard1 It seems the CI failed. Could you please double check?

@Yard1
Copy link
Collaborator Author

Yard1 commented Mar 18, 2024

yeah let me see

@Yard1
Copy link
Collaborator Author

Yard1 commented Mar 18, 2024

Should be good now!

@WoosukKwon WoosukKwon enabled auto-merge (squash) March 18, 2024 22:30
@WoosukKwon WoosukKwon merged commit 49eedea into vllm-project:main Mar 18, 2024
29 of 30 checks passed
@Yard1 Yard1 deleted the zero_copy_input_metadata branch March 18, 2024 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants