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

[TPU] Call torch._sync(param) during weight loading #9437

Merged
merged 3 commits into from
Oct 17, 2024
Merged

Conversation

WoosukKwon
Copy link
Collaborator

@WoosukKwon WoosukKwon commented Oct 17, 2024

During weight loading, we often do something like:

narrowed_tensor = param.data.narrow(0, offset, len)
narrowed_tensor.copy_(real_weight)

expecting narrowed_tensor and param.data to share the same storage. However, on TPUs, narrowed_tensor will lazily propagate to the base tensor, which is param.data, leading to the redundant memory usage. This sometimes causes OOM errors during model loading.

This PR address this problem by adding a post-hook to call torch._sync(param) after the weight loader of each param is called.

When loading Llama3-8B (bf16) on v5e-8,

  • Before this PR: 3.4 GB allocated after weight loading
  • After this PR: 2.0 GB allocated after weight loading

@WoosukKwon WoosukKwon added the tpu Related to Google TPUs label Oct 17, 2024
Copy link

👋 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.

🚀

@WoosukKwon
Copy link
Collaborator Author

Thanks @JackCaoG for finding out the bug and providing the solution.

@@ -28,4 +29,22 @@ def set_weight_attrs(
for key, value in weight_attrs.items():
assert not hasattr(
weight, key), (f"Overwriting existing tensor attribute: {key}")

# NOTE(woosuk): For TPU, param.data.copy_(weight) happens lazily,

Choose a reason for hiding this comment

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

to be more accurate this is because in VLLM we do

narrowed_tensor = param.data.narrow(0, offset, len)
narrowed_tensor.copy_(real_weight)

narrowed_tensor and param.data share the same storage. With functionization, the in place update on the narrowed_tensor will lazily propagate to the base tensor which is param.data.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the elaboration. Fixed the comment!

@JackCaoG
Copy link

lgtm

@WoosukKwon WoosukKwon changed the title [TPU] Ensure torch._sync(param) is called after param.data.copy_() [TPU] Call torch._sync(param) during weight loading Oct 17, 2024
Copy link
Collaborator

@mgoin mgoin 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 referencing the CT issue, LGTM!

@WoosukKwon WoosukKwon merged commit 8e1cddc into main Oct 17, 2024
30 checks passed
@WoosukKwon WoosukKwon deleted the tpu-sync branch October 17, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tpu Related to Google TPUs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants