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

Speed up model loading for generate #709

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

albanD
Copy link
Contributor

@albanD albanD commented Apr 13, 2024

This has not been extensively tested (only mistral 7b) and more of a proposal!

This change does the follow:

  • Create the model on the meta device
  • Load the state dict with assign=True which preserve the properties of the checkpoint (mmap-ed cpu Tensor in this case)
  • Initialize non-persistent buffers remaining on the meta device
  • Move the finalized model to the requested device/dtype

This makes the model loading almost instant on my machine.

Copy link

pytorch-bot bot commented Apr 13, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/torchtune/709

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure

As of commit add24c6 with merge base ada5224 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 13, 2024
@albanD
Copy link
Contributor Author

albanD commented Apr 14, 2024

Any way I can know from the CI logs what is my lint mistake so I can fix it?

@kartikayk
Copy link
Contributor

@albanD thanks so much for putting this up! I'll take a more detailed look tomorrow, but to answer your lint question - you can do the following:

pre-commit install
pre-commit run --all-files

This will fix all of the issues for you.

@ebsmothers
Copy link
Contributor

Thanks for the PR @albanD! Tbh we have already had a fraught relationship with meta device initialization 😅 (see e.g. #317, #418, #514). Our latest status is that we deliberately sacrifice a bit on time-to-first-batch for the sake of keeping code in the model components agnostic to meta device. But generation is an interesting case since the total runtime is much lower than on a finetune with FSDP (which is what we were focusing on previously). Out of curiosity, what is the speedup of meta device vs just initializing directly on GPU in this case?

@albanD
Copy link
Contributor Author

albanD commented Apr 15, 2024

I would need to check once I go back on the machine in question.
The more important bit tbh is that the CPU model was fully using the mmap-ed loaded Tensors and so was not blowing up my scarse RAM :D

@kartikayk I saw that but I don't have pre-commit in my environment :p

model = config.instantiate(model_cfg)

model.load_state_dict(model_state_dict, assign=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

for quantized models we'd need to load after we do quantization I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants